00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <boost/version.hpp>
00023 #ifdef WITH_THREADS
00024 #include <boost/thread/locks.hpp>
00025 #endif
00026 #include <ostream>
00027 #include <string>
00028 #include "FactorCollection.h"
00029 #include "Util.h"
00030 #include "util/pool.hh"
00031
00032 using namespace std;
00033
00034 namespace Moses
00035 {
00036 FactorCollection FactorCollection::s_instance;
00037
00038 const Factor *FactorCollection::AddFactor(const StringPiece &factorString, bool isNonTerminal)
00039 {
00040 FactorFriend to_ins;
00041 to_ins.in.m_string = factorString;
00042 to_ins.in.m_id = (isNonTerminal) ? m_factorIdNonTerminal : m_factorId;
00043 Set & set = (isNonTerminal) ? m_set : m_setNonTerminal;
00044
00045 #ifdef WITH_THREADS
00046 {
00047
00048 boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
00049 Set::const_iterator i = set.find(to_ins);
00050 if (i != set.end()) return &i->in;
00051 }
00052 boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
00053 #endif // WITH_THREADS
00054 std::pair<Set::iterator, bool> ret(set.insert(to_ins));
00055 if (ret.second) {
00056 ret.first->in.m_string.set(
00057 memcpy(m_string_backing.Allocate(factorString.size()), factorString.data(), factorString.size()),
00058 factorString.size());
00059 if (isNonTerminal) {
00060 m_factorIdNonTerminal++;
00061 UTIL_THROW_IF2(m_factorIdNonTerminal >= moses_MaxNumNonterminals, "Number of non-terminals exceeds maximum size reserved. Adjust parameter moses_MaxNumNonterminals, then recompile");
00062 } else {
00063 m_factorId++;
00064 }
00065 }
00066 return &ret.first->in;
00067 }
00068
00069 const Factor *FactorCollection::GetFactor(const StringPiece &factorString, bool isNonTerminal)
00070 {
00071 FactorFriend to_find;
00072 to_find.in.m_string = factorString;
00073 to_find.in.m_id = (isNonTerminal) ? m_factorIdNonTerminal : m_factorId;
00074 Set & set = (isNonTerminal) ? m_set : m_setNonTerminal;
00075 {
00076
00077 #ifdef WITH_THREADS
00078 boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
00079 #endif // WITH_THREADS
00080 Set::const_iterator i = set.find(to_find);
00081 if (i != set.end()) return &i->in;
00082 }
00083 return NULL;
00084 }
00085
00086
00087 FactorCollection::~FactorCollection() {}
00088
00089 TO_STRING_BODY(FactorCollection);
00090
00091
00092 ostream& operator<<(ostream& out, const FactorCollection& factorCollection)
00093 {
00094 #ifdef WITH_THREADS
00095 boost::shared_lock<boost::shared_mutex> lock(factorCollection.m_accessLock);
00096 #endif
00097 for (FactorCollection::Set::const_iterator i = factorCollection.m_set.begin(); i != factorCollection.m_set.end(); ++i) {
00098 out << i->in;
00099 }
00100 return out;
00101 }
00102
00103 }
00104
00105