If Boost is available, use boost::unordered_map instead of std::map for

storing child nodes in PhraseDictionaryNodeSCFG.


git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3598 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
pjwilliams 2010-09-30 21:28:30 +00:00
parent 687ddf2d10
commit 2761762434
3 changed files with 132 additions and 4 deletions

View File

@ -20,6 +20,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
#include "Factor.h"
#ifdef HAVE_BOOST
#include <boost/functional/hash.hpp>
#endif
using namespace std;
@ -48,6 +52,14 @@ ostream& operator<<(ostream& out, const Factor& factor)
return out;
}
#ifdef HAVE_BOOST
size_t hash_value(const Factor& f)
{
boost::hash<size_t> hasher;
return hasher(f.GetId());
}
#endif
}

View File

@ -137,6 +137,9 @@ public:
};
#ifdef HAVE_BOOST
size_t hash_value(const Factor &f);
#endif
}
#endif

View File

@ -30,18 +30,131 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "Word.h"
#include "TargetPhraseCollection.h"
#ifdef HAVE_BOOST
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/version.hpp>
#endif
namespace Moses
{
class PhraseDictionarySCFG;
#ifdef HAVE_BOOST
class TerminalHasher
{
public:
// Generate a hash value for a word representing a terminal. It's
// assumed that the same subset of factors will be active for all words
// that are hashed.
size_t operator()(const Word & t) const
{
size_t seed = 0;
for (size_t i = 0; i < MAX_NUM_FACTORS; ++i)
{
const Factor * f = t[i];
if (f)
{
boost::hash_combine(seed, *f);
}
}
return seed;
}
};
class TerminalEqualityPred
{
public:
// Equality predicate for comparing words representing terminals. As
// with the hasher, it's assumed that all words will have the same
// subset of active factors.
bool operator()(const Word & t1, const Word & t2) const
{
for (size_t i = 0; i < MAX_NUM_FACTORS; ++i)
{
const Factor * f1 = t1[i];
const Factor * f2 = t2[i];
if (f1 && f1->Compare(*f2))
{
return false;
}
}
return true;
}
};
class NonTerminalMapKeyHasher
{
public:
size_t operator()(const std::pair<Word, Word> & k) const
{
// Assumes that only the first factor of each Word is relevant.
const Word & w1 = k.first;
const Word & w2 = k.second;
const Factor * f1 = w1[0];
const Factor * f2 = w2[0];
size_t seed = 0;
boost::hash_combine(seed, *f1);
boost::hash_combine(seed, *f2);
return seed;
}
};
class NonTerminalMapKeyEqualityPred
{
public:
bool operator()(const std::pair<Word, Word> & k1,
const std::pair<Word, Word> & k2) const
{
// Compare first non-terminal of each key. Assumes that for Words
// representing non-terminals only the first factor is relevant.
{
const Word & w1 = k1.first;
const Word & w2 = k2.first;
const Factor * f1 = w1[0];
const Factor * f2 = w2[0];
if (f1->Compare(*f2))
{
return false;
}
}
// Compare second non-terminal of each key.
{
const Word & w1 = k1.second;
const Word & w2 = k2.second;
const Factor * f1 = w1[0];
const Factor * f2 = w2[0];
if (f1->Compare(*f2))
{
return false;
}
}
return true;
}
};
#endif
/** One node of the PhraseDictionarySCFG structure
*/
class PhraseDictionaryNodeSCFG
{
typedef std::map<Word, PhraseDictionaryNodeSCFG> TerminalMap;
typedef std::pair<Word, Word> NonTerminalMapKey;
typedef std::map<NonTerminalMapKey, PhraseDictionaryNodeSCFG> NonTerminalMap;
typedef std::pair<Word, Word> NonTerminalMapKey;
#if defined(BOOST_VERSION) && (BOOST_VERSION >= 104200)
typedef boost::unordered_map<Word,
PhraseDictionaryNodeSCFG,
TerminalHasher,
TerminalEqualityPred> TerminalMap;
typedef boost::unordered_map<NonTerminalMapKey,
PhraseDictionaryNodeSCFG,
NonTerminalMapKeyHasher,
NonTerminalMapKeyEqualityPred> NonTerminalMap;
#else
typedef std::map<Word, PhraseDictionaryNodeSCFG> TerminalMap;
typedef std::map<NonTerminalMapKey, PhraseDictionaryNodeSCFG> NonTerminalMap;
#endif
friend std::ostream& operator<<(std::ostream&, const PhraseDictionarySCFG&);
@ -52,7 +165,7 @@ class PhraseDictionaryNodeSCFG
protected:
TerminalMap m_sourceTermMap;
NonTerminalMap m_nonTermMap;
mutable TargetPhraseCollection *m_targetPhraseCollection;
TargetPhraseCollection *m_targetPhraseCollection;
PhraseDictionaryNodeSCFG()
:m_targetPhraseCollection(NULL)