00001 // $Id$ 00002 00003 /*********************************************************************** 00004 Moses - factored phrase-based language decoder 00005 Copyright (C) 2006 University of Edinburgh 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 ***********************************************************************/ 00021 00022 #ifndef moses_TargetPhraseCollection_h 00023 #define moses_TargetPhraseCollection_h 00024 00025 #include <vector> 00026 #include <iostream> 00027 #include "TargetPhrase.h" 00028 #include "Util.h" 00029 #include <boost/shared_ptr.hpp> 00030 00031 namespace Moses 00032 { 00033 00035 class TargetPhraseCollection 00036 { 00037 protected: 00038 friend std::ostream& operator<<(std::ostream &, const TargetPhraseCollection &); 00039 00040 typedef std::vector<const TargetPhrase*> CollType; 00041 CollType m_collection; 00042 00043 public: 00044 // iters 00045 typedef CollType::iterator iterator; 00046 typedef CollType::const_iterator const_iterator; 00047 typedef boost::shared_ptr<TargetPhraseCollection> shared_ptr; 00048 typedef boost::shared_ptr<TargetPhraseCollection const> shared_const_ptr; 00049 00050 TargetPhrase const* 00051 operator[](size_t const i) const { 00052 return m_collection.at(i); 00053 } 00054 00055 iterator begin() { 00056 return m_collection.begin(); 00057 } 00058 iterator end() { 00059 return m_collection.end(); 00060 } 00061 const_iterator begin() const { 00062 return m_collection.begin(); 00063 } 00064 const_iterator end() const { 00065 return m_collection.end(); 00066 } 00067 00068 TargetPhraseCollection() { 00069 } 00070 00071 TargetPhraseCollection(const TargetPhraseCollection ©); 00072 00073 virtual ~TargetPhraseCollection() { 00074 Remove(); 00075 } 00076 00077 const CollType &GetCollection() const { 00078 return m_collection; 00079 } 00080 00082 void Remove(const size_t pos) { 00083 if (pos < m_collection.size()) { 00084 m_collection.erase(begin() + pos); 00085 } 00086 } 00087 00089 const TargetPhrase* GetTargetPhrase(const size_t pos) const { 00090 return m_collection[pos]; 00091 } 00092 00094 void NthElement(size_t tableLimit); 00095 00097 size_t GetSize() const { 00098 return m_collection.size(); 00099 } 00101 bool IsEmpty() const { 00102 return m_collection.empty(); 00103 } 00105 void Add(TargetPhrase *targetPhrase) { 00106 m_collection.push_back(targetPhrase); 00107 } 00108 00109 void Prune(bool adhereTableLimit, size_t tableLimit); 00110 void Sort(bool adhereTableLimit, size_t tableLimit); 00111 00112 void Remove() { 00113 RemoveAllInColl(m_collection); 00114 } 00115 void Detach() { 00116 m_collection.clear(); 00117 } 00118 00119 }; 00120 00121 00122 // LEGACY 00123 // DO NOT USE. NOT LEGACY CODE 00124 class TargetPhraseCollectionWithSourcePhrase : public TargetPhraseCollection 00125 { 00126 protected: 00127 //friend std::ostream& operator<<(std::ostream &, const TargetPhraseCollectionWithSourcePhrase &); 00128 00129 // TODO boost::ptr_vector 00130 std::vector<Phrase> m_sourcePhrases; 00131 00132 public: 00133 typedef boost::shared_ptr<TargetPhraseCollectionWithSourcePhrase> shared_ptr; 00134 typedef boost::shared_ptr<TargetPhraseCollectionWithSourcePhrase const> shared_const_ptr; 00135 00136 const std::vector<Phrase> &GetSourcePhrases() const { 00137 return m_sourcePhrases; 00138 } 00139 00140 void Add(TargetPhrase *targetPhrase); 00141 void Add(TargetPhrase *targetPhrase, const Phrase &sourcePhrase); 00142 }; 00143 00144 struct CompareTargetPhrase { 00145 bool operator() (const TargetPhrase *a, const TargetPhrase *b) const; 00146 bool operator() (const TargetPhrase &a, const TargetPhrase &b) const; 00147 }; 00148 00149 } 00150 00151 #endif