summaryrefslogtreecommitdiff
path: root/klm/search/edge_generator.hh
blob: 582c78b7b3952a5cbbb8273c55632cf1e8c90112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef SEARCH_EDGE_GENERATOR__
#define SEARCH_EDGE_GENERATOR__

#include "search/edge.hh"
#include "search/note.hh"
#include "search/types.hh"

#include <queue>

namespace lm {
namespace ngram {
class ChartState;
} // namespace ngram
} // namespace lm

namespace search {

template <class Model> class Context;

class EdgeGenerator {
  public:
    EdgeGenerator() {}

    PartialEdge AllocateEdge(Arity arity) {
      return PartialEdge(partial_edge_pool_, arity);
    }

    void AddEdge(PartialEdge edge) {
      generate_.push(edge);
    }

    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();
    }

  private:
    util::Pool partial_edge_pool_;

    typedef std::priority_queue<PartialEdge> Generate;
    Generate generate_;
};

} // namespace search
#endif // SEARCH_EDGE_GENERATOR__