mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-12-28 22:45:50 +03:00
add HypothesisColl
This commit is contained in:
parent
acf5a1cebd
commit
71b8ca7b1e
115
contrib/other-builds/moses2/HypothesisColl.cpp
Normal file
115
contrib/other-builds/moses2/HypothesisColl.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* HypothesisColl.cpp
|
||||
*
|
||||
* Created on: 26 Feb 2016
|
||||
* Author: hieu
|
||||
*/
|
||||
#include <algorithm>
|
||||
#include <boost/foreach.hpp>
|
||||
#include "HypothesisColl.h"
|
||||
#include "ManagerBase.h"
|
||||
#include "System.h"
|
||||
|
||||
namespace Moses2 {
|
||||
|
||||
HypothesisColl::HypothesisColl(const ManagerBase &mgr)
|
||||
:m_coll(MemPoolAllocator<const HypothesisBase*>(mgr.GetPool()))
|
||||
,m_sortedHypos(NULL)
|
||||
{}
|
||||
|
||||
StackAdd HypothesisColl::Add(const HypothesisBase *hypo)
|
||||
{
|
||||
std::pair<_HCType::iterator, bool> addRet = m_coll.insert(hypo);
|
||||
|
||||
// CHECK RECOMBINATION
|
||||
if (addRet.second) {
|
||||
// equiv hypo doesn't exists
|
||||
return StackAdd(true, NULL);
|
||||
}
|
||||
else {
|
||||
const HypothesisBase *hypoExisting = *addRet.first;
|
||||
if (hypo->GetFutureScore() > hypoExisting->GetFutureScore()) {
|
||||
// incoming hypo is better than the one we have
|
||||
const HypothesisBase *const &hypoExisting1 = *addRet.first;
|
||||
const HypothesisBase *&hypoExisting2 = const_cast<const HypothesisBase *&>(hypoExisting1);
|
||||
hypoExisting2 = hypo;
|
||||
|
||||
return StackAdd(true, const_cast<HypothesisBase*>(hypoExisting));
|
||||
}
|
||||
else {
|
||||
// already storing the best hypo. discard incoming hypo
|
||||
return StackAdd(false, const_cast<HypothesisBase*>(hypo));
|
||||
}
|
||||
}
|
||||
|
||||
assert(false);
|
||||
}
|
||||
|
||||
Hypotheses &HypothesisColl::GetSortedAndPruneHypos(const ManagerBase &mgr) const
|
||||
{
|
||||
if (m_sortedHypos == NULL) {
|
||||
// create sortedHypos first
|
||||
MemPool &pool = mgr.GetPool();
|
||||
m_sortedHypos = new (pool.Allocate<Hypotheses>()) Hypotheses(pool, m_coll.size());
|
||||
|
||||
size_t ind = 0;
|
||||
BOOST_FOREACH(const HypothesisBase *hypo, m_coll) {
|
||||
(*m_sortedHypos)[ind] = hypo;
|
||||
++ind;
|
||||
}
|
||||
|
||||
SortAndPruneHypos(mgr);
|
||||
}
|
||||
|
||||
return *m_sortedHypos;
|
||||
}
|
||||
|
||||
void HypothesisColl::SortAndPruneHypos(const ManagerBase &mgr) const
|
||||
{
|
||||
size_t stackSize = mgr.system.stackSize;
|
||||
Recycler<HypothesisBase*> &recycler = mgr.GetHypoRecycle();
|
||||
|
||||
/*
|
||||
cerr << "UNSORTED hypos:" << endl;
|
||||
for (size_t i = 0; i < hypos.size(); ++i) {
|
||||
const Hypothesis *hypo = hypos[i];
|
||||
cerr << *hypo << endl;
|
||||
}
|
||||
cerr << endl;
|
||||
*/
|
||||
Hypotheses::iterator iterMiddle;
|
||||
iterMiddle = (stackSize == 0 || m_sortedHypos->size() < stackSize)
|
||||
? m_sortedHypos->end()
|
||||
: m_sortedHypos->begin() + stackSize;
|
||||
|
||||
std::partial_sort(m_sortedHypos->begin(), iterMiddle, m_sortedHypos->end(),
|
||||
HypothesisFutureScoreOrderer());
|
||||
|
||||
// prune
|
||||
if (stackSize && m_sortedHypos->size() > stackSize) {
|
||||
for (size_t i = stackSize; i < m_sortedHypos->size(); ++i) {
|
||||
HypothesisBase *hypo = const_cast<HypothesisBase*>((*m_sortedHypos)[i]);
|
||||
recycler.Recycle(hypo);
|
||||
}
|
||||
m_sortedHypos->resize(stackSize);
|
||||
}
|
||||
|
||||
/*
|
||||
cerr << "sorted hypos:" << endl;
|
||||
for (size_t i = 0; i < hypos.size(); ++i) {
|
||||
const Hypothesis *hypo = hypos[i];
|
||||
cerr << hypo << " " << *hypo << endl;
|
||||
}
|
||||
cerr << endl;
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
void HypothesisColl::Clear()
|
||||
{
|
||||
m_sortedHypos = NULL;
|
||||
m_coll.clear();
|
||||
}
|
||||
|
||||
|
||||
} /* namespace Moses2 */
|
51
contrib/other-builds/moses2/HypothesisColl.h
Normal file
51
contrib/other-builds/moses2/HypothesisColl.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* HypothesisColl.h
|
||||
*
|
||||
* Created on: 26 Feb 2016
|
||||
* Author: hieu
|
||||
*/
|
||||
#pragma once
|
||||
#include <boost/unordered_set.hpp>
|
||||
#include "HypothesisBase.h"
|
||||
#include "MemPool.h"
|
||||
#include "legacy/Util2.h"
|
||||
|
||||
namespace Moses2 {
|
||||
|
||||
class ManagerBase;
|
||||
|
||||
typedef Array<const HypothesisBase*> Hypotheses;
|
||||
|
||||
class HypothesisColl
|
||||
{
|
||||
public:
|
||||
typedef boost::unordered_set<const HypothesisBase*,
|
||||
UnorderedComparer<HypothesisBase>,
|
||||
UnorderedComparer<HypothesisBase>,
|
||||
MemPoolAllocator<const HypothesisBase*>
|
||||
> _HCType;
|
||||
|
||||
HypothesisColl(const ManagerBase &mgr);
|
||||
|
||||
StackAdd Add(const HypothesisBase *hypo);
|
||||
|
||||
_HCType &GetColl()
|
||||
{ return m_coll; }
|
||||
|
||||
const _HCType &GetColl() const
|
||||
{ return m_coll; }
|
||||
|
||||
void Clear();
|
||||
|
||||
Hypotheses &GetSortedAndPruneHypos(const ManagerBase &mgr) const;
|
||||
|
||||
protected:
|
||||
_HCType m_coll;
|
||||
mutable Hypotheses *m_sortedHypos;
|
||||
|
||||
void SortAndPruneHypos(const ManagerBase &mgr) const;
|
||||
|
||||
};
|
||||
|
||||
} /* namespace Moses2 */
|
||||
|
Loading…
Reference in New Issue
Block a user