2012-10-15 16:58:33 +04:00
|
|
|
#ifndef SEARCH_EDGE_GENERATOR__
|
|
|
|
#define SEARCH_EDGE_GENERATOR__
|
|
|
|
|
|
|
|
#include "search/edge.hh"
|
2012-10-16 15:57:18 +04:00
|
|
|
#include "search/types.hh"
|
2012-10-15 16:58:33 +04:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
namespace lm {
|
|
|
|
namespace ngram {
|
|
|
|
class ChartState;
|
|
|
|
} // namespace ngram
|
|
|
|
} // namespace lm
|
|
|
|
|
|
|
|
namespace search {
|
|
|
|
|
|
|
|
template <class Model> class Context;
|
|
|
|
|
|
|
|
class EdgeGenerator {
|
|
|
|
public:
|
2012-10-17 19:49:41 +04:00
|
|
|
EdgeGenerator() {}
|
2012-10-15 16:58:33 +04:00
|
|
|
|
2012-10-17 19:49:41 +04:00
|
|
|
PartialEdge AllocateEdge(Arity arity) {
|
2012-10-18 21:54:38 +04:00
|
|
|
return PartialEdge(partial_edge_pool_, arity);
|
2012-10-15 16:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-10-17 19:49:41 +04:00
|
|
|
void AddEdge(PartialEdge edge) {
|
|
|
|
generate_.push(edge);
|
2012-10-15 16:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-10-17 19:49:41 +04:00
|
|
|
bool Empty() const { return generate_.empty(); }
|
|
|
|
|
|
|
|
// Pop. If there's a complete hypothesis, return it. Otherwise return an invalid PartialEdge.
|
|
|
|
template <class Model> PartialEdge Pop(Context<Model> &context);
|
|
|
|
|
|
|
|
template <class Model, class Output> void Search(Context<Model> &context, Output &output) {
|
|
|
|
unsigned to_pop = context.PopLimit();
|
|
|
|
while (to_pop > 0 && !generate_.empty()) {
|
|
|
|
PartialEdge got(Pop(context));
|
|
|
|
if (got.Valid()) {
|
|
|
|
output.NewHypothesis(got);
|
|
|
|
--to_pop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.FinishedSearch();
|
|
|
|
}
|
2012-10-15 16:58:33 +04:00
|
|
|
|
|
|
|
private:
|
2012-10-18 21:54:38 +04:00
|
|
|
util::Pool partial_edge_pool_;
|
2012-10-15 16:58:33 +04:00
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
typedef std::priority_queue<PartialEdge> Generate;
|
2012-10-15 16:58:33 +04:00
|
|
|
Generate generate_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace search
|
|
|
|
#endif // SEARCH_EDGE_GENERATOR__
|