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