HSDS
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros
trie.hpp
Go to the documentation of this file.
1 
6 #if !defined(HSDS_TRIE_HPP_)
7 #define HSDS_TRIE_HPP_
8 
9 #include <vector>
10 #include "hsds/vector.hpp"
11 #include "hsds/bit-vector.hpp"
12 
17 namespace hsds {
18 
22 class Trie {
23 public:
24  typedef uint64_t id_t;
25  static const id_t NOT_FOUND = ~(0ULL);
26  static const id_t CAN_NOT_TRAVERSE = ~(0ULL) - 1;
27 
28  typedef struct Result {
29  id_t id;
30  uint64_t depth;
31 
32  Result(id_t id_, uint64_t depth_) :
33  id(id_), depth(depth_) {
34  }
35  } Result;
36 
40  Trie();
41 
45  virtual ~Trie();
46 
53  void build(std::vector<std::string>& keyList, bool useTailTrie = false);
54 
64  id_t exactMatchSearch(const char* str, size_t len) const;
65 
74  void commonPrefixSearch(const char* str, size_t len, std::vector<id_t>& retIDs,
75  uint64_t limit = DEFAULT_LIMIT_VALUE) const;
76 
85  void commonPrefixSearch(const char* str, size_t len, std::vector<Result>& results, uint64_t limit =
86  DEFAULT_LIMIT_VALUE) const;
87 
96  void predictiveSearch(const char* str, size_t len, std::vector<id_t>& retIDs,
97  uint64_t limit = DEFAULT_LIMIT_VALUE) const;
98 
112  id_t traverse(const char* str, size_t len, uint64_t& nodePos, uint64_t& zeros, size_t& keyPos) const;
113 
120  void decodeKey(id_t id, std::string& key) const;
121 
127  void swap(Trie& x);
128 
132  void clear();
133 
138  size_t size() const;
139 
147  void save(std::ostream& os) const throw (hsds::Exception);
148 
156  void load(std::istream& is) throw (hsds::Exception);
157 
168  uint64_t map(void* ptr, uint64_t mapSize) throw (hsds::Exception);
169 
170 private:
171  static const uint64_t DEFAULT_LIMIT_VALUE = ~(0ULL);
172  BitVector louds_;
173  BitVector terminal_;
174  BitVector tail_;
175 
176  Vector<Vector<char> > vtails_;
177  Vector<uint8_t> edges_;
178  size_t numOfKeys_;
179  bool isReady_;
180 
181  Trie* vtailTrie_;
182  BitVector tailIDs_;
183  uint64_t tailIDSize_;
184 
185  void build(hsds::Vector<Vector<char> >& keyList);// for build tail trie
186  void buildTailTrie();
187  bool isLeaf(uint64_t pos) const;
188  void getChild(uint8_t c, uint64_t& pos, uint64_t& zeros) const;
189  void getParent(uint8_t& c, uint64_t& pos, uint64_t& zeros) const;
190  void enumerateAll(uint64_t pos, uint64_t zeros, std::vector<id_t>& retIDs, size_t limit) const;
191  bool tailMatch(const char* str, size_t len, size_t depth, uint64_t tailID, size_t& retLen) const;
192  Vector<char> getTail(uint64_t i) const;
193 };
194 
195 }
196 
197 #endif