2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
2011-09-20 23:29:24 +04:00
|
|
|
#include <ostream>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <string>
|
|
|
|
#include "FactorCollection.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2008-06-11 14:52:57 +04:00
|
|
|
FactorCollection FactorCollection::s_instance;
|
|
|
|
|
2011-09-20 23:08:42 +04:00
|
|
|
bool FactorCollection::Exists(FactorDirection direction, FactorType factorType, const string &factorString) const
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2009-08-07 20:47:54 +04:00
|
|
|
#ifdef WITH_THREADS
|
2011-02-24 16:14:42 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(m_accessLock);
|
|
|
|
#endif
|
2011-09-20 23:08:42 +04:00
|
|
|
return m_map.find(factorString) != m_map.end();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const Factor *FactorCollection::AddFactor(FactorDirection direction
|
2011-02-24 16:14:42 +03:00
|
|
|
, FactorType factorType
|
|
|
|
, const string &factorString)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2009-08-07 20:47:54 +04:00
|
|
|
#ifdef WITH_THREADS
|
2011-09-20 23:08:42 +04:00
|
|
|
{
|
|
|
|
boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
|
|
|
|
Map::const_iterator i = m_map.find(factorString);
|
|
|
|
if (i != m_map.end()) return &i->second;
|
|
|
|
}
|
|
|
|
boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
|
2009-08-07 20:47:54 +04:00
|
|
|
#endif
|
2011-09-20 23:08:42 +04:00
|
|
|
std::pair<std::string, Factor> to_ins(factorString, Factor());
|
|
|
|
std::pair<Map::iterator, bool> ret(m_map.insert(to_ins));
|
|
|
|
if (ret.second) {
|
|
|
|
Factor &factor = ret.first->second;
|
|
|
|
factor.m_id = m_factorId++;
|
|
|
|
factor.m_ptrString = &ret.first->first;
|
|
|
|
}
|
|
|
|
return &ret.first->second;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2011-09-20 23:29:24 +04:00
|
|
|
FactorCollection::~FactorCollection() {}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
TO_STRING_BODY(FactorCollection);
|
|
|
|
|
|
|
|
// friend
|
|
|
|
ostream& operator<<(ostream& out, const FactorCollection& factorCollection)
|
|
|
|
{
|
2011-09-20 23:08:42 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(factorCollection.m_accessLock);
|
|
|
|
#endif
|
|
|
|
for (FactorCollection::Map::const_iterator i = factorCollection.m_map.begin(); i != factorCollection.m_map.end(); ++i) {
|
|
|
|
out << i->second;
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
return out;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|