xref: /aosp_15_r20/external/marisa-trie/lib/marisa/grimoire/trie/louds-trie.h (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1*ab8db090SAndroid Build Coastguard Worker #ifndef MARISA_GRIMOIRE_TRIE_LOUDS_TRIE_H_
2*ab8db090SAndroid Build Coastguard Worker #define MARISA_GRIMOIRE_TRIE_LOUDS_TRIE_H_
3*ab8db090SAndroid Build Coastguard Worker 
4*ab8db090SAndroid Build Coastguard Worker #include "marisa/keyset.h"
5*ab8db090SAndroid Build Coastguard Worker #include "marisa/agent.h"
6*ab8db090SAndroid Build Coastguard Worker #include "marisa/grimoire/vector.h"
7*ab8db090SAndroid Build Coastguard Worker #include "marisa/grimoire/trie/config.h"
8*ab8db090SAndroid Build Coastguard Worker #include "marisa/grimoire/trie/key.h"
9*ab8db090SAndroid Build Coastguard Worker #include "marisa/grimoire/trie/tail.h"
10*ab8db090SAndroid Build Coastguard Worker #include "marisa/grimoire/trie/cache.h"
11*ab8db090SAndroid Build Coastguard Worker 
12*ab8db090SAndroid Build Coastguard Worker namespace marisa {
13*ab8db090SAndroid Build Coastguard Worker namespace grimoire {
14*ab8db090SAndroid Build Coastguard Worker namespace trie {
15*ab8db090SAndroid Build Coastguard Worker 
16*ab8db090SAndroid Build Coastguard Worker class LoudsTrie  {
17*ab8db090SAndroid Build Coastguard Worker  public:
18*ab8db090SAndroid Build Coastguard Worker   LoudsTrie();
19*ab8db090SAndroid Build Coastguard Worker   ~LoudsTrie();
20*ab8db090SAndroid Build Coastguard Worker 
21*ab8db090SAndroid Build Coastguard Worker   void build(Keyset &keyset, int flags);
22*ab8db090SAndroid Build Coastguard Worker 
23*ab8db090SAndroid Build Coastguard Worker   void map(Mapper &mapper);
24*ab8db090SAndroid Build Coastguard Worker   void read(Reader &reader);
25*ab8db090SAndroid Build Coastguard Worker   void write(Writer &writer) const;
26*ab8db090SAndroid Build Coastguard Worker 
27*ab8db090SAndroid Build Coastguard Worker   bool lookup(Agent &agent) const;
28*ab8db090SAndroid Build Coastguard Worker   void reverse_lookup(Agent &agent) const;
29*ab8db090SAndroid Build Coastguard Worker   bool common_prefix_search(Agent &agent) const;
30*ab8db090SAndroid Build Coastguard Worker   bool predictive_search(Agent &agent) const;
31*ab8db090SAndroid Build Coastguard Worker 
num_tries()32*ab8db090SAndroid Build Coastguard Worker   std::size_t num_tries() const {
33*ab8db090SAndroid Build Coastguard Worker     return config_.num_tries();
34*ab8db090SAndroid Build Coastguard Worker   }
num_keys()35*ab8db090SAndroid Build Coastguard Worker   std::size_t num_keys() const {
36*ab8db090SAndroid Build Coastguard Worker     return size();
37*ab8db090SAndroid Build Coastguard Worker   }
num_nodes()38*ab8db090SAndroid Build Coastguard Worker   std::size_t num_nodes() const {
39*ab8db090SAndroid Build Coastguard Worker     return (louds_.size() / 2) - 1;
40*ab8db090SAndroid Build Coastguard Worker   }
41*ab8db090SAndroid Build Coastguard Worker 
cache_level()42*ab8db090SAndroid Build Coastguard Worker   CacheLevel cache_level() const {
43*ab8db090SAndroid Build Coastguard Worker     return config_.cache_level();
44*ab8db090SAndroid Build Coastguard Worker   }
tail_mode()45*ab8db090SAndroid Build Coastguard Worker   TailMode tail_mode() const {
46*ab8db090SAndroid Build Coastguard Worker     return config_.tail_mode();
47*ab8db090SAndroid Build Coastguard Worker   }
node_order()48*ab8db090SAndroid Build Coastguard Worker   NodeOrder node_order() const {
49*ab8db090SAndroid Build Coastguard Worker     return config_.node_order();
50*ab8db090SAndroid Build Coastguard Worker   }
51*ab8db090SAndroid Build Coastguard Worker 
empty()52*ab8db090SAndroid Build Coastguard Worker   bool empty() const {
53*ab8db090SAndroid Build Coastguard Worker     return size() == 0;
54*ab8db090SAndroid Build Coastguard Worker   }
size()55*ab8db090SAndroid Build Coastguard Worker   std::size_t size() const {
56*ab8db090SAndroid Build Coastguard Worker     return terminal_flags_.num_1s();
57*ab8db090SAndroid Build Coastguard Worker   }
58*ab8db090SAndroid Build Coastguard Worker   std::size_t total_size() const;
59*ab8db090SAndroid Build Coastguard Worker   std::size_t io_size() const;
60*ab8db090SAndroid Build Coastguard Worker 
61*ab8db090SAndroid Build Coastguard Worker   void clear();
62*ab8db090SAndroid Build Coastguard Worker   void swap(LoudsTrie &rhs);
63*ab8db090SAndroid Build Coastguard Worker 
64*ab8db090SAndroid Build Coastguard Worker  private:
65*ab8db090SAndroid Build Coastguard Worker   BitVector louds_;
66*ab8db090SAndroid Build Coastguard Worker   BitVector terminal_flags_;
67*ab8db090SAndroid Build Coastguard Worker   BitVector link_flags_;
68*ab8db090SAndroid Build Coastguard Worker   Vector<UInt8> bases_;
69*ab8db090SAndroid Build Coastguard Worker   FlatVector extras_;
70*ab8db090SAndroid Build Coastguard Worker   Tail tail_;
71*ab8db090SAndroid Build Coastguard Worker   scoped_ptr<LoudsTrie> next_trie_;
72*ab8db090SAndroid Build Coastguard Worker   Vector<Cache> cache_;
73*ab8db090SAndroid Build Coastguard Worker   std::size_t cache_mask_;
74*ab8db090SAndroid Build Coastguard Worker   std::size_t num_l1_nodes_;
75*ab8db090SAndroid Build Coastguard Worker   Config config_;
76*ab8db090SAndroid Build Coastguard Worker   Mapper mapper_;
77*ab8db090SAndroid Build Coastguard Worker 
78*ab8db090SAndroid Build Coastguard Worker   void build_(Keyset &keyset, const Config &config);
79*ab8db090SAndroid Build Coastguard Worker 
80*ab8db090SAndroid Build Coastguard Worker   template <typename T>
81*ab8db090SAndroid Build Coastguard Worker   void build_trie(Vector<T> &keys,
82*ab8db090SAndroid Build Coastguard Worker       Vector<UInt32> *terminals, const Config &config, std::size_t trie_id);
83*ab8db090SAndroid Build Coastguard Worker   template <typename T>
84*ab8db090SAndroid Build Coastguard Worker   void build_current_trie(Vector<T> &keys,
85*ab8db090SAndroid Build Coastguard Worker       Vector<UInt32> *terminals, const Config &config, std::size_t trie_id);
86*ab8db090SAndroid Build Coastguard Worker   template <typename T>
87*ab8db090SAndroid Build Coastguard Worker   void build_next_trie(Vector<T> &keys,
88*ab8db090SAndroid Build Coastguard Worker       Vector<UInt32> *terminals, const Config &config, std::size_t trie_id);
89*ab8db090SAndroid Build Coastguard Worker   template <typename T>
90*ab8db090SAndroid Build Coastguard Worker   void build_terminals(const Vector<T> &keys,
91*ab8db090SAndroid Build Coastguard Worker       Vector<UInt32> *terminals) const;
92*ab8db090SAndroid Build Coastguard Worker 
93*ab8db090SAndroid Build Coastguard Worker   void reserve_cache(const Config &config, std::size_t trie_id,
94*ab8db090SAndroid Build Coastguard Worker       std::size_t num_keys);
95*ab8db090SAndroid Build Coastguard Worker   template <typename T>
96*ab8db090SAndroid Build Coastguard Worker   void cache(std::size_t parent, std::size_t child,
97*ab8db090SAndroid Build Coastguard Worker       float weight, char label);
98*ab8db090SAndroid Build Coastguard Worker   void fill_cache();
99*ab8db090SAndroid Build Coastguard Worker 
100*ab8db090SAndroid Build Coastguard Worker   void map_(Mapper &mapper);
101*ab8db090SAndroid Build Coastguard Worker   void read_(Reader &reader);
102*ab8db090SAndroid Build Coastguard Worker   void write_(Writer &writer) const;
103*ab8db090SAndroid Build Coastguard Worker 
104*ab8db090SAndroid Build Coastguard Worker   inline bool find_child(Agent &agent) const;
105*ab8db090SAndroid Build Coastguard Worker   inline bool predictive_find_child(Agent &agent) const;
106*ab8db090SAndroid Build Coastguard Worker 
107*ab8db090SAndroid Build Coastguard Worker   inline void restore(Agent &agent, std::size_t node_id) const;
108*ab8db090SAndroid Build Coastguard Worker   inline bool match(Agent &agent, std::size_t node_id) const;
109*ab8db090SAndroid Build Coastguard Worker   inline bool prefix_match(Agent &agent, std::size_t node_id) const;
110*ab8db090SAndroid Build Coastguard Worker 
111*ab8db090SAndroid Build Coastguard Worker   void restore_(Agent &agent, std::size_t node_id) const;
112*ab8db090SAndroid Build Coastguard Worker   bool match_(Agent &agent, std::size_t node_id) const;
113*ab8db090SAndroid Build Coastguard Worker   bool prefix_match_(Agent &agent, std::size_t node_id) const;
114*ab8db090SAndroid Build Coastguard Worker 
115*ab8db090SAndroid Build Coastguard Worker   inline std::size_t get_cache_id(std::size_t node_id, char label) const;
116*ab8db090SAndroid Build Coastguard Worker   inline std::size_t get_cache_id(std::size_t node_id) const;
117*ab8db090SAndroid Build Coastguard Worker 
118*ab8db090SAndroid Build Coastguard Worker   inline std::size_t get_link(std::size_t node_id) const;
119*ab8db090SAndroid Build Coastguard Worker   inline std::size_t get_link(std::size_t node_id,
120*ab8db090SAndroid Build Coastguard Worker       std::size_t link_id) const;
121*ab8db090SAndroid Build Coastguard Worker 
122*ab8db090SAndroid Build Coastguard Worker   inline std::size_t update_link_id(std::size_t link_id,
123*ab8db090SAndroid Build Coastguard Worker       std::size_t node_id) const;
124*ab8db090SAndroid Build Coastguard Worker 
125*ab8db090SAndroid Build Coastguard Worker   // Disallows copy and assignment.
126*ab8db090SAndroid Build Coastguard Worker   LoudsTrie(const LoudsTrie &);
127*ab8db090SAndroid Build Coastguard Worker   LoudsTrie &operator=(const LoudsTrie &);
128*ab8db090SAndroid Build Coastguard Worker };
129*ab8db090SAndroid Build Coastguard Worker 
130*ab8db090SAndroid Build Coastguard Worker }  // namespace trie
131*ab8db090SAndroid Build Coastguard Worker }  // namespace grimoire
132*ab8db090SAndroid Build Coastguard Worker }  // namespace marisa
133*ab8db090SAndroid Build Coastguard Worker 
134*ab8db090SAndroid Build Coastguard Worker #endif  // MARISA_GRIMOIRE_TRIE_LOUDS_TRIE_H_
135