summaryrefslogtreecommitdiff
path: root/klm/search/nbest.hh
blob: cb7651bc2ee0a4b4e3b5e9f3561116a34ad16296 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef SEARCH_NBEST__
#define SEARCH_NBEST__

#include "search/applied.hh"
#include "search/config.hh"
#include "search/edge.hh"

#include <boost/pool/object_pool.hpp>

#include <cstddef>
#include <queue>
#include <vector>

#include <assert.h>

namespace search {

class NBestList;

class NBestList {
  private:
    class RevealedRef {
      public: 
        explicit RevealedRef(History history) 
          : in_(static_cast<NBestList*>(history)), index_(0) {}

      private:
        friend class NBestList;

        NBestList *in_;
        std::size_t index_;
    };
    
    typedef GenericApplied<RevealedRef> QueueEntry;

  public:
    NBestList(std::vector<PartialEdge> &existing, util::Pool &entry_pool, std::size_t keep);

    Score TopAfterConstructor() const;

    const std::vector<Applied> &Extract(util::Pool &pool, std::size_t n);

  private:
    Score Visit(util::Pool &pool, std::size_t index);

    Applied Get(util::Pool &pool, std::size_t index);

    void MoveTop(util::Pool &pool);

    typedef std::vector<Applied> Revealed;
    Revealed revealed_;

    typedef std::priority_queue<QueueEntry> Queue;
    Queue queue_;
};

class NBest {
  public:
    typedef std::vector<PartialEdge> Combine;

    explicit NBest(const NBestConfig &config) : config_(config) {}

    void Add(std::vector<PartialEdge> &existing, PartialEdge addition) const {
      existing.push_back(addition);
    }

    NBestComplete Complete(std::vector<PartialEdge> &partials);

    const std::vector<Applied> &Extract(History root);

  private:
    const NBestConfig config_;

    boost::object_pool<NBestList> list_pool_;

    util::Pool entry_pool_;
};

} // namespace search

#endif // SEARCH_NBEST__