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-21 14:26:04 +04:00
# include <boost/version.hpp>
2012-10-05 20:38:12 +04:00
# ifdef WITH_THREADS
2012-07-11 13:54:21 +04:00
# include <boost/thread/locks.hpp>
2012-10-05 20:38:12 +04:00
# endif
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"
2013-04-25 22:42:30 +04:00
# include "util/pool.hh"
2008-06-11 14:52:57 +04:00
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 ;
2014-03-21 14:53:15 +04:00
const Factor * FactorCollection : : AddFactor ( const StringPiece & factorString , bool isNonTerminal )
2008-06-11 14:52:57 +04:00
{
2011-09-22 00:35:12 +04:00
FactorFriend to_ins ;
2013-05-29 21:16:15 +04:00
to_ins . in . m_string = factorString ;
2014-03-21 14:53:15 +04:00
to_ins . in . m_id = ( isNonTerminal ) ? m_factorIdNonTerminal : m_factorId ;
Set & set = ( isNonTerminal ) ? m_set : m_setNonTerminal ;
2013-04-25 22:42:30 +04:00
// If we're threaded, hope a read-only lock is sufficient.
# ifdef WITH_THREADS
2013-05-29 21:16:15 +04:00
{
// read=lock scope
2011-09-20 23:08:42 +04:00
boost : : shared_lock < boost : : shared_mutex > read_lock ( m_accessLock ) ;
2014-03-21 14:53:15 +04:00
Set : : const_iterator i = set . find ( to_ins ) ;
if ( i ! = set . end ( ) ) return & i - > in ;
2011-09-20 23:08:42 +04:00
}
boost : : unique_lock < boost : : shared_mutex > lock ( m_accessLock ) ;
2011-09-21 14:26:04 +04:00
# endif // WITH_THREADS
2014-03-21 14:53:15 +04:00
std : : pair < Set : : iterator , bool > ret ( set . insert ( to_ins ) ) ;
2011-09-20 23:08:42 +04:00
if ( ret . second ) {
2013-04-25 22:42:30 +04:00
ret . first - > in . m_string . set (
2013-05-29 21:16:15 +04:00
memcpy ( m_string_backing . Allocate ( factorString . size ( ) ) , factorString . data ( ) , factorString . size ( ) ) ,
factorString . size ( ) ) ;
2014-03-21 14:53:15 +04:00
if ( isNonTerminal ) {
m_factorIdNonTerminal + + ;
UTIL_THROW_IF2 ( m_factorIdNonTerminal > = moses_MaxNumNonterminals , " Number of non-terminals exceeds maximum size reserved. Adjust parameter moses_MaxNumNonterminals, then recompile " ) ;
}
else {
m_factorId + + ;
}
2011-09-20 23:08:42 +04:00
}
2011-09-22 00:35:12 +04:00
return & ret . first - > in ;
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
2011-09-21 14:26:04 +04:00
for ( FactorCollection : : Set : : const_iterator i = factorCollection . m_set . begin ( ) ; i ! = factorCollection . m_set . end ( ) ; + + i ) {
2011-09-22 00:35:12 +04:00
out < < i - > in ;
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
}