diff options
author | Kenneth Heafield <github@kheafield.com> | 2012-09-11 14:30:16 +0100 |
---|---|---|
committer | Kenneth Heafield <github@kheafield.com> | 2012-09-11 14:31:42 +0100 |
commit | 45c9efc7d558dbe160056f02e74df1fee5d2d0e5 (patch) | |
tree | 0c26a847b78b4c0d86507b21b7338beb98ff1e73 /klm/search/vertex_generator.hh | |
parent | 8882e9ebe158aef382bb5544559ef7f2a553db62 (diff) |
Add search library to cdec (not used yet)
Diffstat (limited to 'klm/search/vertex_generator.hh')
-rw-r--r-- | klm/search/vertex_generator.hh | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/klm/search/vertex_generator.hh b/klm/search/vertex_generator.hh new file mode 100644 index 00000000..8cdf1420 --- /dev/null +++ b/klm/search/vertex_generator.hh @@ -0,0 +1,70 @@ +#ifndef SEARCH_VERTEX_GENERATOR__ +#define SEARCH_VERTEX_GENERATOR__ + +#include "search/edge.hh" +#include "search/edge_generator.hh" + +#include <boost/pool/pool.hpp> +#include <boost/unordered_map.hpp> + +#include <queue> + +namespace lm { +namespace ngram { +class ChartState; +} // namespace ngram +} // namespace lm + +namespace search { + +template <class Model> class Context; +class ContextBase; +class Final; + +class VertexGenerator { + public: + template <class Model> VertexGenerator(Context<Model> &context, Vertex &gen); + + PartialEdge *MallocPartialEdge() { return static_cast<PartialEdge*>(partial_edge_pool_.malloc()); } + void FreePartialEdge(PartialEdge *value) { partial_edge_pool_.free(value); } + + void NewHypothesis(const lm::ngram::ChartState &state, const Edge &from, const PartialEdge &partial); + + private: + // Parallel structure to VertexNode. + struct Trie { + Trie() : under(NULL) {} + + VertexNode *under; + boost::unordered_map<uint64_t, Trie> extend; + }; + + Trie &FindOrInsert(Trie &node, uint64_t added, const lm::ngram::ChartState &state, unsigned char left, bool left_full, unsigned char right, bool right_full); + + Final *CompleteTransition(Trie &node, const lm::ngram::ChartState &state, const Edge &from, const PartialEdge &partial); + + ContextBase &context_; + + std::vector<EdgeGenerator> edges_; + + struct LessByTop : public std::binary_function<const EdgeGenerator *, const EdgeGenerator *, bool> { + bool operator()(const EdgeGenerator *first, const EdgeGenerator *second) const { + return first->Top() < second->Top(); + } + }; + + typedef std::priority_queue<EdgeGenerator*, std::vector<EdgeGenerator*>, LessByTop> Generate; + Generate generate_; + + Trie root_; + + typedef boost::unordered_map<uint64_t, Final*> Existing; + Existing existing_; + + int to_pop_; + + boost::pool<> partial_edge_pool_; +}; + +} // namespace search +#endif // SEARCH_VERTEX_GENERATOR__ |