2015-10-27 15:08:32 +03:00
|
|
|
/*
|
|
|
|
* ArcList.cpp
|
|
|
|
*
|
|
|
|
* Created on: 26 Oct 2015
|
|
|
|
* Author: hieu
|
|
|
|
*/
|
2015-10-29 18:29:14 +03:00
|
|
|
#include <iostream>
|
2016-08-24 00:38:27 +03:00
|
|
|
#include <sstream>
|
2016-03-16 21:22:25 +03:00
|
|
|
#include <algorithm>
|
2015-10-27 15:08:32 +03:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include "ArcLists.h"
|
2016-03-31 21:23:05 +03:00
|
|
|
#include "HypothesisBase.h"
|
2015-10-27 15:08:32 +03:00
|
|
|
#include "util/exception.hh"
|
|
|
|
|
2015-10-29 18:29:14 +03:00
|
|
|
using namespace std;
|
|
|
|
|
2015-12-10 23:49:30 +03:00
|
|
|
namespace Moses2
|
|
|
|
{
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
ArcLists::ArcLists()
|
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
// TODO Auto-generated constructor stub
|
2015-10-27 15:08:32 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
ArcLists::~ArcLists()
|
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
BOOST_FOREACH(const Coll::value_type &collPair, m_coll) {
|
|
|
|
const ArcList *arcList = collPair.second;
|
|
|
|
delete arcList;
|
|
|
|
}
|
2015-10-27 15:08:32 +03:00
|
|
|
}
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
void ArcLists::AddArc(bool added, const HypothesisBase *currHypo,
|
2017-02-01 03:10:16 +03:00
|
|
|
const HypothesisBase *otherHypo)
|
2015-10-27 15:08:32 +03:00
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
//cerr << added << " " << currHypo << " " << otherHypo << endl;
|
|
|
|
ArcList *arcList;
|
|
|
|
if (added) {
|
|
|
|
// we're winners!
|
|
|
|
if (otherHypo) {
|
|
|
|
// there was a existing losing hypo
|
|
|
|
arcList = &GetAndDetachArcList(otherHypo);
|
|
|
|
} else {
|
|
|
|
// there was no existing hypo
|
|
|
|
arcList = new ArcList;
|
|
|
|
}
|
|
|
|
m_coll[currHypo] = arcList;
|
|
|
|
} else {
|
|
|
|
// we're losers!
|
|
|
|
// there should be a winner, we're not doing beam pruning
|
|
|
|
UTIL_THROW_IF2(otherHypo == NULL, "There must have been a winning hypo");
|
|
|
|
arcList = &GetArcList(otherHypo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// in any case, add the curr hypo
|
|
|
|
arcList->push_back(currHypo);
|
2015-10-27 15:08:32 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 13:10:35 +03:00
|
|
|
ArcList &ArcLists::GetArcList(const HypothesisBase *hypo)
|
2015-10-27 15:08:32 +03:00
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
Coll::iterator iter = m_coll.find(hypo);
|
|
|
|
UTIL_THROW_IF2(iter == m_coll.end(), "Can't find arc list");
|
|
|
|
ArcList &arcList = *iter->second;
|
|
|
|
return arcList;
|
2015-10-27 15:08:32 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 13:10:35 +03:00
|
|
|
const ArcList &ArcLists::GetArcList(const HypothesisBase *hypo) const
|
2016-03-17 20:54:25 +03:00
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
Coll::const_iterator iter = m_coll.find(hypo);
|
|
|
|
|
|
|
|
if (iter == m_coll.end()) {
|
|
|
|
cerr << "looking for:" << hypo << " have " << m_coll.size() << " :";
|
|
|
|
BOOST_FOREACH(const Coll::value_type &collPair, m_coll) {
|
|
|
|
const HypothesisBase *hypo = collPair.first;
|
|
|
|
cerr << hypo << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UTIL_THROW_IF2(iter == m_coll.end(), "Can't find arc list for " << hypo);
|
|
|
|
ArcList &arcList = *iter->second;
|
|
|
|
return arcList;
|
2016-03-17 20:54:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 13:10:35 +03:00
|
|
|
ArcList &ArcLists::GetAndDetachArcList(const HypothesisBase *hypo)
|
2015-10-27 15:08:32 +03:00
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
Coll::iterator iter = m_coll.find(hypo);
|
|
|
|
UTIL_THROW_IF2(iter == m_coll.end(), "Can't find arc list");
|
|
|
|
ArcList &arcList = *iter->second;
|
2015-10-27 15:08:32 +03:00
|
|
|
|
2017-02-01 03:10:16 +03:00
|
|
|
m_coll.erase(iter);
|
2015-10-27 15:08:32 +03:00
|
|
|
|
2017-02-01 03:10:16 +03:00
|
|
|
return arcList;
|
2015-10-27 15:08:32 +03:00
|
|
|
}
|
|
|
|
|
2016-03-16 21:22:25 +03:00
|
|
|
void ArcLists::Sort()
|
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
BOOST_FOREACH(Coll::value_type &collPair, m_coll) {
|
|
|
|
ArcList &list = *collPair.second;
|
|
|
|
std::sort(list.begin(), list.end(), HypothesisFutureScoreOrderer() );
|
|
|
|
}
|
2016-03-16 21:22:25 +03:00
|
|
|
}
|
|
|
|
|
2016-03-31 21:36:33 +03:00
|
|
|
void ArcLists::Delete(const HypothesisBase *hypo)
|
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
//cerr << "hypo=" << hypo->Debug() << endl;
|
|
|
|
//cerr << "m_coll=" << m_coll.size() << endl;
|
|
|
|
Coll::iterator iter = m_coll.find(hypo);
|
|
|
|
UTIL_THROW_IF2(iter == m_coll.end(), "Can't find arc list");
|
|
|
|
ArcList *arcList = iter->second;
|
|
|
|
|
|
|
|
m_coll.erase(iter);
|
|
|
|
delete arcList;
|
2016-03-31 21:36:33 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 00:38:27 +03:00
|
|
|
std::string ArcLists::Debug(const System &system) const
|
|
|
|
{
|
2017-02-01 03:10:16 +03:00
|
|
|
stringstream strm;
|
|
|
|
BOOST_FOREACH(const Coll::value_type &collPair, m_coll) {
|
|
|
|
const ArcList *arcList = collPair.second;
|
|
|
|
strm << arcList << "(" << arcList->size() << ") ";
|
|
|
|
}
|
|
|
|
return strm.str();
|
2016-08-24 00:38:27 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:49:30 +03:00
|
|
|
}
|
|
|
|
|