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"
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-10-13 17:32:14 +04:00
const Factor * FactorCollection : : AddFactor ( const StringPiece & factorString )
2008-06-11 14:52:57 +04:00
{
2011-09-21 14:26:04 +04:00
// Sorry this is so complicated. Can't we just require everybody to use Boost >= 1.42? The issue is that I can't check BOOST_VERSION unless we have Boost.
2009-08-07 20:47:54 +04:00
# ifdef WITH_THREADS
2012-10-17 14:07:42 +04:00
2011-09-21 14:26:04 +04:00
# if BOOST_VERSION < 104200
2011-09-22 00:35:12 +04:00
FactorFriend to_ins ;
2011-09-24 19:58:23 +04:00
to_ins . in . m_string . assign ( factorString . data ( ) , factorString . size ( ) ) ;
2011-09-21 14:26:04 +04:00
# endif // BOOST_VERSION
2012-11-23 22:52:06 +04:00
{ // read=lock scope
2011-09-20 23:08:42 +04:00
boost : : shared_lock < boost : : shared_mutex > read_lock ( m_accessLock ) ;
2011-09-21 14:26:04 +04:00
# if BOOST_VERSION >= 104200
// If this line doesn't compile, upgrade your Boost.
Set : : const_iterator i = m_set . find ( factorString , HashFactor ( ) , EqualsFactor ( ) ) ;
# else // BOOST_VERSION
Set : : const_iterator i = m_set . find ( to_ins ) ;
# endif // BOOST_VERSION
2011-09-22 12:55:54 +04:00
if ( i ! = m_set . end ( ) ) return & i - > in ;
2011-09-20 23:08:42 +04:00
}
boost : : unique_lock < boost : : shared_mutex > lock ( m_accessLock ) ;
2011-11-14 20:45:36 +04:00
# if BOOST_VERSION >= 104200
2011-09-22 00:35:12 +04:00
FactorFriend to_ins ;
2011-09-24 19:58:23 +04:00
to_ins . in . m_string . assign ( factorString . data ( ) , factorString . size ( ) ) ;
2011-09-21 14:26:04 +04:00
# endif // BOOST_VERSION
2012-10-17 14:07:42 +04:00
2011-09-21 14:26:04 +04:00
# else // WITH_THREADS
2012-10-17 14:07:42 +04:00
# if BOOST_VERSION >= 104200
Set : : const_iterator i = m_set . find ( factorString , HashFactor ( ) , EqualsFactor ( ) ) ;
if ( i ! = m_set . end ( ) ) return & i - > in ;
# endif
2011-09-22 00:35:12 +04:00
FactorFriend to_ins ;
2011-09-24 19:58:23 +04:00
to_ins . in . m_string . assign ( factorString . data ( ) , factorString . size ( ) ) ;
2012-10-17 14:07:42 +04:00
2011-09-21 14:26:04 +04:00
# endif // WITH_THREADS
2011-09-22 00:35:12 +04:00
to_ins . in . m_id = m_factorId ;
2011-09-21 14:26:04 +04:00
std : : pair < Set : : iterator , bool > ret ( m_set . insert ( to_ins ) ) ;
2011-09-20 23:08:42 +04:00
if ( ret . second ) {
2011-09-21 14:26:04 +04:00
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
}