mdds
trie_map.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * Copyright (c) 2015-2020 Kohei Yoshida
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  ************************************************************************/
28 
29 #ifndef INCLUDED_MDDS_TRIE_MAP_HPP
30 #define INCLUDED_MDDS_TRIE_MAP_HPP
31 
32 #include "trie_map_itr.hpp"
33 
34 #include <vector>
35 #include <string>
36 #include <deque>
37 #include <map>
38 #include <memory>
39 
40 namespace mdds {
41 
42 namespace trie {
43 
47 template<typename ContainerT>
49 {
51  using key_type = ContainerT;
52 
60 
66  using key_unit_type = typename key_type::value_type;
67 
77  static key_buffer_type to_key_buffer(const key_unit_type* str, size_t length)
78  {
79  return key_buffer_type(str, length);
80  }
81 
91  {
92  return key_buffer_type(key);
93  }
94 
95  static const key_unit_type* buffer_data(const key_buffer_type& buf)
96  {
97  return buf.data();
98  }
99 
100  static size_t buffer_size(const key_buffer_type& buf)
101  {
102  return buf.size();
103  }
104 
112  static void push_back(key_buffer_type& buffer, key_unit_type c)
113  {
114  buffer.push_back(c);
115  }
116 
123  static void pop_back(key_buffer_type& buffer)
124  {
125  buffer.pop_back();
126  }
127 
136  static key_type to_key(const key_buffer_type& buf)
137  {
138  return buf;
139  }
140 };
141 
143 
145 template<typename T>
147 {
148  static constexpr bool variable_size = false;
149 
150  static constexpr size_t value_size = sizeof(T);
151 
152  static void write(std::ostream& os, const T& v);
153 
154  static void read(std::istream& is, size_t n, T& v);
155 };
156 
158 template<typename T>
160 {
161  static constexpr bool variable_size = true;
162 
163  static void write(std::ostream& os, const T& v);
164 
165  static void read(std::istream& is, size_t n, T& v);
166 };
167 
172 template<typename T>
174 {
176 
177  static constexpr bool variable_size = true;
178 
179  static void write(std::ostream& os, const T& v);
180 
181  static void read(std::istream& is, size_t n, T& v);
182 };
183 
191 template<typename T, typename U = void>
193 
194 template<typename T>
195 struct value_serializer<T, typename std::enable_if<has_value_type<T>::value>::type> : numeric_sequence_value_serializer<T> {};
196 
197 template<>
198 struct value_serializer<std::string> : variable_value_serializer<std::string> {};
199 
200 } // namespace trie
201 
202 template<typename _KeyTrait, typename _ValueT>
204 
211 template<typename _KeyTrait, typename _ValueT>
212 class trie_map
213 {
214  friend class packed_trie_map<_KeyTrait, _ValueT>;
215  friend class trie::detail::iterator_base<trie_map, true>;
216  friend class trie::detail::iterator_base<trie_map, false>;
217  friend class trie::detail::const_iterator<trie_map>;
218  friend class trie::detail::iterator<trie_map>;
219  friend class trie::detail::search_results<trie_map>;
222 
223 public:
225  typedef _KeyTrait key_trait_type;
226  typedef typename key_trait_type::key_type key_type;
227  typedef typename key_trait_type::key_buffer_type key_buffer_type;
228  typedef typename key_trait_type::key_unit_type key_unit_type;
229  typedef _ValueT value_type;
230  typedef size_t size_type;
231  typedef std::pair<key_type, value_type> key_value_type;
232 
236 
237 private:
238 
239  struct trie_node
240  {
241  typedef std::map<key_unit_type, trie_node> children_type;
242 
243  children_type children;
244  value_type value;
245  bool has_value;
246 
247  trie_node();
248  trie_node(const trie_node& other);
249  trie_node(trie_node&& other);
250 
251  void swap(trie_node& other);
252  };
253 
254  template<bool _IsConst>
255  struct stack_item
256  {
257  using _is_const = bool_constant<_IsConst>;
258 
259  using child_pos_type =
260  typename get_iterator_type<
261  typename trie_node::children_type, _is_const>::type;
262 
263  using trie_node_type = typename const_or_not<trie_node, _is_const>::type;
264 
265  trie_node_type* node;
266  child_pos_type child_pos;
267 
268  stack_item(trie_node_type* _node, const child_pos_type& _child_pos) :
269  node(_node), child_pos(_child_pos) {}
270 
271  bool operator== (const stack_item& r) const
272  {
273  return node == r.node && child_pos == r.child_pos;
274  }
275 
276  bool operator!= (const stack_item& r) const
277  {
278  return !operator== (r);
279  }
280  };
281 
282  using const_node_stack_type = std::vector<stack_item<true>>;
283  using node_stack_type = std::vector<stack_item<false>>;
284 
285 public:
286 
290  trie_map();
291 
292  trie_map(const trie_map& other);
293 
294  trie_map(trie_map&& other);
295 
296  const_iterator begin() const;
297 
298  iterator begin();
299 
300  const_iterator end() const;
301 
302  iterator end();
303 
304  trie_map& operator= (trie_map other);
305 
306  void swap(trie_map& other);
307 
314  void insert(const key_type& key, const value_type& value);
315 
324  void insert(const key_unit_type* key, size_type len, const value_type& value);
325 
335  bool erase(const key_unit_type* key, size_type len);
336 
345  const_iterator find(const key_type& key) const;
346 
357  const_iterator find(const key_unit_type* input, size_type len) const;
358 
367  iterator find(const key_type& key);
368 
379  iterator find(const key_unit_type* input, size_type len);
380 
391  search_results prefix_search(const key_type& prefix) const;
392 
405  search_results prefix_search(const key_unit_type* prefix, size_type len) const;
406 
412  size_type size() const;
413 
414  bool empty() const noexcept;
415 
419  void clear();
420 
428  packed_type pack() const;
429 
430 private:
431  void insert_into_tree(
432  trie_node& node, const key_unit_type* key, const key_unit_type* key_end, const value_type& value);
433 
434  const trie_node* find_prefix_node(
435  const trie_node& node, const key_unit_type* prefix, const key_unit_type* prefix_end) const;
436 
437  template<bool _IsConst>
438  void find_prefix_node_with_stack(
439  std::vector<stack_item<_IsConst>>& node_stack,
440  const_t<trie_node, _IsConst>& node,
441  const key_unit_type* prefix,
442  const key_unit_type* prefix_end) const;
443 
444  template<bool _IsConst>
445  key_buffer_type build_key_buffer_from_node_stack(
446  const std::vector<stack_item<_IsConst>>& node_stack) const;
447 
448  void count_values(size_type& n, const trie_node& node) const;
449 
450 private:
451  trie_node m_root;
452 };
453 
464 template<typename _KeyTrait, typename _ValueT>
465 class packed_trie_map
466 {
467  friend class trie::detail::packed_iterator_base<packed_trie_map>;
468  friend class trie::detail::packed_search_results<packed_trie_map>;
469 
470 public:
471  typedef _KeyTrait key_trait_type;
472  typedef typename key_trait_type::key_type key_type;
473  typedef typename key_trait_type::key_buffer_type key_buffer_type;
474  typedef typename key_trait_type::key_unit_type key_unit_type;
475  typedef _ValueT value_type;
476  typedef size_t size_type;
477  typedef std::pair<key_type, value_type> key_value_type;
480 
485  struct entry
486  {
487  const key_unit_type* key;
488  size_type keylen;
489  value_type value;
490 
491  entry(const key_unit_type* _key, size_type _keylen, value_type _value) :
492  key(_key), keylen(_keylen), value(_value) {}
493  };
494 
495 private:
496  struct trie_node
497  {
498  key_unit_type key;
499  const value_type* value;
500 
501  std::deque<trie_node*> children;
502 
503  trie_node(key_unit_type _key) : key(_key), value(nullptr) {}
504  };
505 
506  struct stack_item
507  {
508  const uintptr_t* node_pos;
509  const uintptr_t* child_pos;
510  const uintptr_t* child_end;
511 
512  stack_item(const uintptr_t* _node_pos, const uintptr_t* _child_pos, const uintptr_t* _child_end) :
513  node_pos(_node_pos), child_pos(_child_pos), child_end(_child_end) {}
514 
515  bool operator== (const stack_item& other) const
516  {
517  return node_pos == other.node_pos && child_pos == other.child_pos;
518  }
519 
520  bool operator!= (const stack_item& other) const
521  {
522  return !operator==(other);
523  }
524 
525  bool has_value() const
526  {
527  const value_type* pv = reinterpret_cast<const value_type*>(*node_pos);
528  return pv;
529  }
530 
531  const value_type* get_value() const
532  {
533  return reinterpret_cast<const value_type*>(*node_pos);
534  }
535  };
536 
537  typedef std::vector<stack_item> node_stack_type;
538 
539  typedef std::deque<trie_node> node_pool_type;
540  typedef std::vector<uintptr_t> packed_type;
541  typedef std::deque<value_type> value_store_type;
542  typedef std::vector<std::tuple<size_t, key_unit_type>> child_offsets_type;
543 
544 public:
545 
546  packed_trie_map();
547 
558  packed_trie_map(const entry* entries, size_type entry_size);
559 
566  packed_trie_map(const trie_map<key_trait_type, value_type>& other);
567 
568  packed_trie_map(const packed_trie_map& other);
569 
570  packed_trie_map(packed_trie_map&& other);
571 
572  packed_trie_map& operator= (packed_trie_map other);
573 
574  bool operator== (const packed_trie_map& other) const;
575 
576  bool operator!= (const packed_trie_map& other) const;
577 
578  const_iterator begin() const;
579 
580  const_iterator end() const;
581 
582  const_iterator cbegin() const;
583 
584  const_iterator cend() const;
585 
594  const_iterator find(const key_type& key) const;
595 
606  const_iterator find(const key_unit_type* input, size_type len) const;
607 
617  search_results prefix_search(const key_type& prefix) const;
618 
631  search_results prefix_search(const key_unit_type* prefix, size_type len) const;
632 
638  size_type size() const noexcept;
639 
640  bool empty() const noexcept;
641 
642  void swap(packed_trie_map& other);
643 
649  template<typename _Func = trie::value_serializer<value_type>>
650  void save_state(std::ostream& os) const;
651 
658  template<typename _Func = trie::value_serializer<value_type>>
659  void load_state(std::istream& is);
660 
666  void dump_structure() const;
667 
668 private:
669  node_stack_type get_root_stack() const;
670 
671  void traverse_range(
672  trie_node& root, node_pool_type& node_pool, const entry* start, const entry* end,
673  size_type pos);
674 
675  size_type compact_node(const trie_node& node);
676  size_type compact_node(const typename trie_map<_KeyTrait, _ValueT>::trie_node& node);
677 
678  void push_child_offsets(size_type offset, const child_offsets_type& child_offsets);
679 
680  void compact(const trie_node& root);
681  void compact(const typename trie_map<_KeyTrait, _ValueT>::trie_node& root);
682 
683  const uintptr_t* find_prefix_node(
684  const uintptr_t* p, const key_unit_type* prefix, const key_unit_type* prefix_end) const;
685 
686  void find_prefix_node_with_stack(
687  node_stack_type& node_stack,
688  const uintptr_t* p, const key_unit_type* prefix, const key_unit_type* prefix_end) const;
689 
690  template<typename _Handler>
691  void traverse_tree(_Handler hdl) const;
692 
693  template<typename _Handler>
694  void traverse_buffer(_Handler hdl) const;
695 
696 #ifdef MDDS_TRIE_MAP_DEBUG
697  void dump_node(key_buffer_type& buffer, const trie_node& node) const;
698  void dump_trie(const trie_node& root) const;
699  void dump_packed_trie() const;
700 #endif
701 
702 private:
703  value_store_type m_value_store;
704  packed_type m_packed;
705 };
706 
707 }
708 
709 #include "trie_map_def.inl"
710 
711 #endif
712 
713 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: trie_map_itr.hpp:503
static key_buffer_type to_key_buffer(const key_unit_type *str, size_t length)
Definition: trie_map.hpp:77
Definition: trie_map_itr.hpp:354
typename key_type::value_type key_unit_type
Definition: trie_map.hpp:66
Definition: trie_map.hpp:192
key_type key_buffer_type
Definition: trie_map.hpp:59
ContainerT key_type
Definition: trie_map.hpp:51
Definition: trie_map_itr.hpp:85
Definition: trie_map_itr.hpp:351
static key_buffer_type to_key_buffer(const key_type &key)
Definition: trie_map.hpp:90
Definition: trie_map.hpp:485
Definition: trie_map_itr.hpp:82
Definition: trie_map.hpp:203
Definition: trie_map.hpp:159
Definition: global.hpp:136
static void push_back(key_buffer_type &buffer, key_unit_type c)
Definition: trie_map.hpp:112
static void pop_back(key_buffer_type &buffer)
Definition: trie_map.hpp:123
Definition: trie_map.hpp:212
Definition: global.hpp:154
Definition: flat_segment_tree.hpp:46
Definition: trie_map.hpp:48
static key_type to_key(const key_buffer_type &buf)
Definition: trie_map.hpp:136
Definition: trie_map_itr.hpp:67
Definition: trie_map.hpp:146
Definition: trie_map_itr.hpp:500