From 9942fec457f7f9ee565ef5c165282df9bf38b0e5 Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Tue, 14 Jun 2016 21:30:12 +0100 Subject: [PATCH] use pointer for SeenPositionItem --- .../moses2/SCFG/CubePruning/Misc.cpp | 41 ++++++++++++++++--- .../moses2/SCFG/CubePruning/Misc.h | 12 +++--- contrib/other-builds/moses2/SCFG/Manager.cpp | 7 ++-- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/contrib/other-builds/moses2/SCFG/CubePruning/Misc.cpp b/contrib/other-builds/moses2/SCFG/CubePruning/Misc.cpp index fe8746159..f43a1ad7a 100644 --- a/contrib/other-builds/moses2/SCFG/CubePruning/Misc.cpp +++ b/contrib/other-builds/moses2/SCFG/CubePruning/Misc.cpp @@ -82,9 +82,10 @@ void QueueItem::CreateNext( if (tpInd + 1 < tps->GetSize()) { const SCFG::TargetPhraseImpl &tp = (*tps)[tpInd + 1]; - SeenPositionItem seenItem(tp, hypoIndColl); + SeenPositionItem *seenItem = new (pool.Allocate()) SeenPositionItem(tp, hypoIndColl); bool unseen = seenPositions.Add(seenItem); + unseen = true; if (unseen) { QueueItem *item = QueueItem::Create(pool, mgr); item->Init(pool, *symbolBind, *tps, tpInd + 1); @@ -103,10 +104,11 @@ void QueueItem::CreateNext( size_t hypoInd = hypoIndColl[i] + 1; if (hypoInd < hypos.GetSize()) { - SeenPositionItem seenItem(tp, hypoIndColl); - seenItem.hypoIndColl[i] = hypoInd; + SeenPositionItem *seenItem = new (pool.Allocate()) SeenPositionItem(tp, hypoIndColl); + seenItem->hypoIndColl[i] = hypoInd; bool unseen = seenPositions.Add(seenItem); + unseen = true; if (unseen) { QueueItem *item = QueueItem::Create(pool, mgr); item->Init(pool, *symbolBind, *tps, tpInd); @@ -142,15 +144,42 @@ void SeenPositionItem::Debug(std::ostream &out, const System &system) const } } +bool SeenPositionItem::operator==(const SeenPositionItem &compare) const +{ + bool ret = (tp == compare.tp); + if (!ret) { + return false; + } + + if (hypoIndColl.size() != compare.hypoIndColl.size()) { + return false; + } + + for (size_t i = 0; i < hypoIndColl.size(); ++i) { + if (hypoIndColl[i] != compare.hypoIndColl[i]) { + return false; + } + } + + return true; +} + +size_t SeenPositionItem::hash() const +{ + return hash_value(*this); +} + size_t hash_value(const SeenPositionItem& obj) { - size_t ret = boost::hash_value(obj.hypoIndColl); - boost::hash_combine(ret, (size_t) obj.tp); + size_t ret = (size_t) obj.tp; + for (size_t i = 0; i < obj.hypoIndColl.size(); ++i) { + boost::hash_combine(ret, obj.hypoIndColl[i]); + } return ret; } //////////////////////////////////////////////////////// -bool SeenPositions::Add(const SeenPositionItem &item) +bool SeenPositions::Add(const SeenPositionItem *item) { std::pair ret = m_coll.insert(item); return ret.second; diff --git a/contrib/other-builds/moses2/SCFG/CubePruning/Misc.h b/contrib/other-builds/moses2/SCFG/CubePruning/Misc.h index cc4996412..f26f9d3b2 100644 --- a/contrib/other-builds/moses2/SCFG/CubePruning/Misc.h +++ b/contrib/other-builds/moses2/SCFG/CubePruning/Misc.h @@ -99,11 +99,8 @@ public: SeenPositionItem(const SCFG::TargetPhraseImpl &vtp, const Vector &vhypoIndColl); - virtual bool operator==(const SeenPositionItem &compare) const - { - bool ret = (tp == compare.tp) && (hypoIndColl == compare.hypoIndColl); - return ret; - } + bool operator==(const SeenPositionItem &compare) const; + size_t hash() const; void Debug(std::ostream &out, const System &system) const; @@ -116,14 +113,15 @@ size_t hash_value(const SeenPositionItem& obj); class SeenPositions { public: - bool Add(const SeenPositionItem &item); + bool Add(const SeenPositionItem *item); void clear() { m_coll.clear(); } protected: - typedef boost::unordered_set Coll; + typedef boost::unordered_set, UnorderedComparer > Coll; Coll m_coll; }; diff --git a/contrib/other-builds/moses2/SCFG/Manager.cpp b/contrib/other-builds/moses2/SCFG/Manager.cpp index 63f24a766..d12b57ae6 100644 --- a/contrib/other-builds/moses2/SCFG/Manager.cpp +++ b/contrib/other-builds/moses2/SCFG/Manager.cpp @@ -25,8 +25,8 @@ namespace SCFG { Manager::Manager(System &sys, const TranslationTask &task, - const std::string &inputStr, long translationId) : - ManagerBase(sys, task, inputStr, translationId) + const std::string &inputStr, long translationId) +:ManagerBase(sys, task, inputStr, translationId) { } @@ -78,7 +78,8 @@ void Manager::Decode() Decode(path, stack); //cerr << "AFTER DECODE path=" << path << endl; - if (startPos == 0 && phraseSize == 2) { + if ((startPos == 0 && phraseSize == 2) + || startPos == 2 && phraseSize == 1) { cerr << "STACK:" << endl; stack.Debug(cerr, system); }