/* * Stack.cpp * * Created on: 24 Oct 2015 * Author: hieu */ #include #include #include "Stack.h" #include "Hypothesis.h" #include "../Scores.h" using namespace std; namespace Moses2 { Stack::Stack() { // TODO Auto-generated constructor stub } Stack::~Stack() { // TODO Auto-generated destructor stub } void Stack::Add(const Hypothesis *hypo, Recycler &hypoRecycle) { StackAdd added = Add(hypo); if (added.toBeDeleted) { hypoRecycle.Recycle(added.toBeDeleted); } } StackAdd Stack::Add(const Hypothesis *hypo) { std::pair addRet = m_hypos.insert(hypo); if (addRet.second) { // equiv hypo doesn't exists return StackAdd(true, NULL); } else { const Hypothesis *hypoExisting = *addRet.first; if (hypo->GetScores().GetTotalScore() > hypoExisting->GetScores().GetTotalScore()) { // incoming hypo is better than the one we have const Hypothesis *const &hypoExisting1 = *addRet.first; const Hypothesis *&hypoExisting2 = const_cast(hypoExisting1); hypoExisting2 = hypo; return StackAdd(true, const_cast(hypoExisting)); /* const_cast(hypo)->Swap(*const_cast(hypoExisting)); return StackAdd(true, const_cast(hypo)); */ } else { // already storing the best hypo. discard incoming hypo return StackAdd(false, const_cast(hypo)); } } } std::vector Stack::GetBestHyposAndPrune(size_t num, Recycler &recycler) const { std::vector ret = GetBestHypos(num); if (num && ret.size() > num) { for (size_t i = num; i < ret.size(); ++i) { Hypothesis *hypo = const_cast(ret[i]); recycler.Recycle(hypo); } ret.resize(num); } return ret; } std::vector Stack::GetBestHypos(size_t num) const { std::vector ret(m_hypos.begin(), m_hypos.end()); std::vector::iterator iterMiddle; iterMiddle = (num == 0 || ret.size() < num) ? ret.end() : ret.begin()+num; std::partial_sort(ret.begin(), iterMiddle, ret.end(), HypothesisFutureScoreOrderer()); return ret; } }