|
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm> |
|
#include <boost/foreach.hpp> |
|
#include "Stack.h" |
|
#include "../Hypothesis.h" |
|
#include "../Manager.h" |
|
#include "../../Scores.h" |
|
#include "../../System.h" |
|
|
|
using namespace std; |
|
|
|
namespace Moses2 |
|
{ |
|
|
|
namespace NSCubePruningCardinalStack |
|
{ |
|
|
|
|
|
Stack::Stack(const Manager &mgr) |
|
:m_mgr(mgr) |
|
,m_coll() |
|
{ |
|
} |
|
|
|
Stack::~Stack() |
|
{ |
|
|
|
} |
|
|
|
void Stack::Add(const Hypothesis *hypo, Recycler<Hypothesis*> &hypoRecycle) |
|
{ |
|
std::pair<_HCType::iterator, bool> addRet = m_coll.insert(hypo); |
|
|
|
|
|
if (addRet.second) { |
|
|
|
} else { |
|
const Hypothesis *hypoExisting = *addRet.first; |
|
if (hypo->GetScores().GetTotalScore() > hypoExisting->GetScores().GetTotalScore()) { |
|
|
|
const Hypothesis *const &hypoExisting1 = *addRet.first; |
|
const Hypothesis *&hypoExisting2 = const_cast<const Hypothesis *&>(hypoExisting1); |
|
hypoExisting2 = hypo; |
|
|
|
Hypothesis *hypoToBeDeleted = const_cast<Hypothesis*>(hypoExisting); |
|
hypoRecycle.Recycle(hypoToBeDeleted); |
|
} else { |
|
|
|
Hypothesis *hypoToBeDeleted = const_cast<Hypothesis*>(hypo); |
|
hypoRecycle.Recycle(hypoToBeDeleted); |
|
} |
|
} |
|
} |
|
|
|
std::vector<const Hypothesis*> Stack::GetBestHypos(size_t num) const |
|
{ |
|
std::vector<const Hypothesis*> ret; |
|
ret.insert(ret.end(), m_coll.begin(), m_coll.end()); |
|
|
|
std::vector<const Hypothesis*>::iterator iterMiddle; |
|
iterMiddle = (num == 0 || ret.size() < num) |
|
? ret.end() |
|
: ret.begin()+num; |
|
|
|
std::partial_sort(ret.begin(), iterMiddle, ret.end(), |
|
HypothesisFutureScoreOrderer()); |
|
|
|
return ret; |
|
} |
|
|
|
size_t Stack::GetHypoSize() const |
|
{ |
|
return m_coll.size(); |
|
} |
|
|
|
void Stack::Clear() |
|
{ |
|
|
|
m_coll.clear(); |
|
} |
|
|
|
Stack::SortedHypos Stack::GetSortedAndPruneHypos(const Manager &mgr) const |
|
{ |
|
SortedHypos ret; |
|
|
|
MemPool &pool = mgr.GetPool(); |
|
|
|
|
|
Hypotheses *allHypos = new (pool.Allocate<Hypotheses>()) Hypotheses(pool, GetHypoSize()); |
|
size_t i = 0; |
|
BOOST_FOREACH(const Hypothesis *hypo, m_coll) { |
|
(*allHypos)[i++] = hypo; |
|
} |
|
SortAndPruneHypos(mgr, *allHypos); |
|
|
|
|
|
BOOST_FOREACH(const Hypothesis *hypo, *allHypos) { |
|
HypoCoverage key(&hypo->GetBitmap(), hypo->GetInputPath().range.GetEndPos()); |
|
|
|
Hypotheses *hypos; |
|
SortedHypos::iterator iter; |
|
iter = ret.find(key); |
|
if (iter == ret.end()) { |
|
hypos = new (pool.Allocate<Hypotheses>()) Hypotheses(pool); |
|
ret[key] = hypos; |
|
} else { |
|
hypos = iter->second; |
|
} |
|
hypos->push_back(hypo); |
|
} |
|
|
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Stack::SortAndPruneHypos(const Manager &mgr, Hypotheses &hypos) const |
|
{ |
|
size_t stackSize = mgr.system.stackSize; |
|
Recycler<Hypothesis*> &recycler = mgr.GetHypoRecycle(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hypotheses::iterator iterMiddle; |
|
iterMiddle = (stackSize == 0 || hypos.size() < stackSize) |
|
? hypos.end() |
|
: hypos.begin() + stackSize; |
|
|
|
std::partial_sort(hypos.begin(), iterMiddle, hypos.end(), |
|
HypothesisFutureScoreOrderer()); |
|
|
|
|
|
if (stackSize && hypos.size() > stackSize) { |
|
for (size_t i = stackSize; i < hypos.size(); ++i) { |
|
Hypothesis *hypo = const_cast<Hypothesis*>(hypos[i]); |
|
recycler.Recycle(hypo); |
|
} |
|
hypos.resize(stackSize); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|