diff --git a/moses/PrefixTreeMap.cpp b/moses/PrefixTreeMap.cpp index c8edce726..ee7565d8b 100644 --- a/moses/PrefixTreeMap.cpp +++ b/moses/PrefixTreeMap.cpp @@ -5,6 +5,8 @@ #include #endif +using namespace std; + namespace Moses { void GenericCandidate::readBin(FILE* f) @@ -62,6 +64,17 @@ void Candidates::readBin(FILE* f) const LabelId PrefixTreeMap::MagicWord = std::numeric_limits::max() - 1; +////////////////////////////////////////////////////////////////// +PrefixTreeMap::~PrefixTreeMap() { + if(m_FileSrc) { + fClose(m_FileSrc); + } + if(m_FileTgt) { + fClose(m_FileTgt); + } + FreeMemory(); +} + void PrefixTreeMap::FreeMemory() { @@ -75,20 +88,21 @@ void PrefixTreeMap::FreeMemory() m_PtrPool.reset(); } -static WordVoc* ReadVoc(const std::string& filename) +WordVoc &ReadVoc(std::map &vocs, const std::string& filename) { - static std::map vocs; #ifdef WITH_THREADS boost::mutex mutex; boost::mutex::scoped_lock lock(mutex); #endif - std::map::iterator vi = vocs.find(filename); + std::map::iterator vi = vocs.find(filename); if (vi == vocs.end()) { - WordVoc* voc = new WordVoc(); - voc->Read(filename); - vocs[filename] = voc; + WordVoc &voc = vocs[filename]; + voc.Read(filename); + return voc; + } + else { + return vi->second; } - return vocs[filename]; } int PrefixTreeMap::Read(const std::string& fileNameStem, int numVocs) @@ -133,7 +147,7 @@ int PrefixTreeMap::Read(const std::string& fileNameStem, int numVocs) sprintf(num, "%d", i); //m_Voc[i] = new WordVoc(); //m_Voc[i]->Read(ifv + num); - m_Voc[i] = ReadVoc(ifv + num); + m_Voc[i] = &ReadVoc(m_vocs, ifv + num); } TRACE_ERR("binary file loaded, default OFF_T: "<< PTF::getDefault()<<"\n"); diff --git a/moses/PrefixTreeMap.h b/moses/PrefixTreeMap.h index 06066878d..d6262ca65 100644 --- a/moses/PrefixTreeMap.h +++ b/moses/PrefixTreeMap.h @@ -99,18 +99,11 @@ public: PrefixTreeMap() : m_FileSrc(0), m_FileTgt(0) { PTF::setDefault(InvalidOffT); } - ~PrefixTreeMap() { - if(m_FileSrc) { - fClose(m_FileSrc); - } - if(m_FileTgt) { - fClose(m_FileTgt); - } - FreeMemory(); - } + ~PrefixTreeMap(); + public: static const LabelId MagicWord; -public: + void FreeMemory(); int Read(const std::string& fileNameStem, int numVocs = -1); @@ -135,6 +128,7 @@ private: std::vector m_Voc; ObjectPool m_PtrPool; + std::map m_vocs; }; }