pb framework for using boost::unordered_set instead of std::set

This commit is contained in:
Hieu Hoang 2015-10-12 14:31:29 +01:00
parent b0248f8ad5
commit 6b182ee5e9
4 changed files with 46 additions and 2 deletions

View File

@ -15,6 +15,11 @@ public:
virtual int Compare(const FFState& other) const = 0;
virtual size_t hash() const = 0;
virtual bool operator==(const FFState& other) const = 0;
virtual bool operator!=(const FFState& other) const
{
return !(*this == other);
}
};
class DummyState : public FFState

View File

@ -647,6 +647,28 @@ GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor) const
return ret;
}
size_t Hypothesis::hash() const
{
size_t seed = 0;
BOOST_FOREACH(const FFState *state, m_ffStates) {
size_t hash = state->hash();
boost::hash_combine(seed ,hash);
}
return seed;
}
bool Hypothesis::operator==(const Hypothesis& other) const
{
for (size_t i = 0; i < m_ffStates.size(); ++i) {
const FFState &thisState = *m_ffStates[i];
const FFState &otherState = *other.m_ffStates[i];
if (thisState != otherState) {
return false;
}
}
return true;
}
#ifdef HAVE_XMLRPC_C
void
Hypothesis::

View File

@ -288,13 +288,16 @@ public:
// creates a map of TARGET positions which should be replaced by word using placeholder
std::map<size_t, const Moses::Factor*> GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor) const;
// for unordered_set in stack
size_t hash() const;
bool operator==(const Hypothesis& other) const;
#ifdef HAVE_XMLRPC_C
void OutputWordAlignment(std::vector<xmlrpc_c::value>& out) const;
void OutputLocalWordAlignment(std::vector<xmlrpc_c::value>& dest) const;
#endif
};
std::ostream& operator<<(std::ostream& out, const Hypothesis& hypothesis);
@ -335,5 +338,18 @@ public:
}
};
class HypothesisRecombinationUnordered
{
public:
size_t operator()(const Hypothesis* hypo) const {
return hypo->hash();
}
bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
return (*hypoA) == (*hypoB);
}
};
}
#endif

View File

@ -3,6 +3,7 @@
#include <vector>
#include <set>
#include <boost/unordered_set.hpp>
#include "Hypothesis.h"
#include "WordsBitmap.h"
@ -18,7 +19,7 @@ class HypothesisStack
{
protected:
typedef std::set< Hypothesis*, HypothesisRecombinationOrderer > _HCType;
typedef boost::unordered_set< Hypothesis*, HypothesisRecombinationUnordered, HypothesisRecombinationUnordered > _HCType;
_HCType m_hypos; /**< contains hypotheses */
Manager& m_manager;