00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_FactorCollection_h
00023 #define moses_FactorCollection_h
00024
00025 #ifdef WITH_THREADS
00026 #include <boost/thread/shared_mutex.hpp>
00027 #endif
00028
00029 #include "util/murmur_hash.hh"
00030 #include <boost/unordered_set.hpp>
00031
00032 #include <functional>
00033 #include <string>
00034
00035 #include "util/string_piece.hh"
00036 #include "Factor.h"
00037
00038 namespace Moses
00039 {
00040
00041
00042
00043
00044
00045
00046
00047
00048 struct FactorFriend {
00049 Factor in;
00050 };
00051
00060 class FactorCollection
00061 {
00062 friend std::ostream& operator<<(std::ostream&, const FactorCollection&);
00063
00064 struct HashFactor : public std::unary_function<const FactorFriend &, std::size_t> {
00065 std::size_t operator()(const StringPiece &str) const {
00066 return util::MurmurHashNative(str.data(), str.size());
00067 }
00068 std::size_t operator()(const FactorFriend &factor) const {
00069 return (*this)(factor.in.GetString());
00070 }
00071 };
00072 struct EqualsFactor : public std::binary_function<const FactorFriend &, const FactorFriend &, bool> {
00073 bool operator()(const FactorFriend &left, const FactorFriend &right) const {
00074 return left.in.GetString() == right.in.GetString();
00075 }
00076 bool operator()(const FactorFriend &left, const StringPiece &right) const {
00077 return left.in.GetString() == right;
00078 }
00079 bool operator()(const StringPiece &left, const FactorFriend &right) const {
00080 return left == right.in.GetString();
00081 }
00082 };
00083 typedef boost::unordered_set<FactorFriend, HashFactor, EqualsFactor> Set;
00084 Set m_set;
00085
00086 static FactorCollection s_instance;
00087 #ifdef WITH_THREADS
00088
00089 mutable boost::shared_mutex m_accessLock;
00090 #endif
00091
00092 size_t m_factorId;
00094
00095 FactorCollection()
00096 :m_factorId(0)
00097 {}
00098
00099 public:
00100 static FactorCollection& Instance() {
00101 return s_instance;
00102 }
00103
00104 ~FactorCollection();
00105
00109 const Factor *AddFactor(const StringPiece &factorString);
00110
00111
00112 const Factor *AddFactor(FactorDirection , FactorType , const StringPiece &factorString) {
00113 return AddFactor(factorString);
00114 }
00115
00116 TO_STRING();
00117
00118 };
00119
00120
00121 }
00122 #endif