1 #ifndef MARISA_GRIMOIRE_TRIE_STATE_H_
2 #define MARISA_GRIMOIRE_TRIE_STATE_H_
3 
4 #include "marisa/grimoire/vector.h"
5 #include "marisa/grimoire/trie/history.h"
6 
7 namespace marisa {
8 namespace grimoire {
9 namespace trie {
10 
11 // A search agent has its internal state and the status codes are defined
12 // below.
13 typedef enum StatusCode {
14   MARISA_READY_TO_ALL,
15   MARISA_READY_TO_COMMON_PREFIX_SEARCH,
16   MARISA_READY_TO_PREDICTIVE_SEARCH,
17   MARISA_END_OF_COMMON_PREFIX_SEARCH,
18   MARISA_END_OF_PREDICTIVE_SEARCH,
19 } StatusCode;
20 
21 class State {
22  public:
State()23   State()
24       : key_buf_(), history_(), node_id_(0), query_pos_(0),
25         history_pos_(0), status_code_(MARISA_READY_TO_ALL) {}
26 
set_node_id(std::size_t node_id)27   void set_node_id(std::size_t node_id) {
28     MARISA_DEBUG_IF(node_id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
29     node_id_ = (UInt32)node_id;
30   }
set_query_pos(std::size_t query_pos)31   void set_query_pos(std::size_t query_pos) {
32     MARISA_DEBUG_IF(query_pos > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
33     query_pos_ = (UInt32)query_pos;
34   }
set_history_pos(std::size_t history_pos)35   void set_history_pos(std::size_t history_pos) {
36     MARISA_DEBUG_IF(history_pos > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
37     history_pos_ = (UInt32)history_pos;
38   }
set_status_code(StatusCode status_code)39   void set_status_code(StatusCode status_code) {
40     status_code_ = status_code;
41   }
42 
node_id()43   std::size_t node_id() const {
44     return node_id_;
45   }
query_pos()46   std::size_t query_pos() const {
47     return query_pos_;
48   }
history_pos()49   std::size_t history_pos() const {
50     return history_pos_;
51   }
status_code()52   StatusCode status_code() const {
53     return status_code_;
54   }
55 
key_buf()56   const Vector<char> &key_buf() const {
57     return key_buf_;
58   }
history()59   const Vector<History> &history() const {
60     return history_;
61   }
62 
key_buf()63   Vector<char> &key_buf() {
64     return key_buf_;
65   }
history()66   Vector<History> &history() {
67     return history_;
68   }
69 
reset()70   void reset() {
71     status_code_ = MARISA_READY_TO_ALL;
72   }
73 
lookup_init()74   void lookup_init() {
75     node_id_ = 0;
76     query_pos_ = 0;
77     status_code_ = MARISA_READY_TO_ALL;
78   }
reverse_lookup_init()79   void reverse_lookup_init() {
80     key_buf_.resize(0);
81     key_buf_.reserve(32);
82     status_code_ = MARISA_READY_TO_ALL;
83   }
common_prefix_search_init()84   void common_prefix_search_init() {
85     node_id_ = 0;
86     query_pos_ = 0;
87     status_code_ = MARISA_READY_TO_COMMON_PREFIX_SEARCH;
88   }
predictive_search_init()89   void predictive_search_init() {
90     key_buf_.resize(0);
91     key_buf_.reserve(64);
92     history_.resize(0);
93     history_.reserve(4);
94     node_id_ = 0;
95     query_pos_ = 0;
96     history_pos_ = 0;
97     status_code_ = MARISA_READY_TO_PREDICTIVE_SEARCH;
98   }
99 
100  private:
101   Vector<char> key_buf_;
102   Vector<History> history_;
103   UInt32 node_id_;
104   UInt32 query_pos_;
105   UInt32 history_pos_;
106   StatusCode status_code_;
107 
108   // Disallows copy and assignment.
109   State(const State &);
110   State &operator=(const State &);
111 };
112 
113 }  // namespace trie
114 }  // namespace grimoire
115 }  // namespace marisa
116 
117 #endif  // MARISA_GRIMOIRE_TRIE_STATE_H_
118