trie.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // trie.hpp
  3. // trie
  4. //
  5. // Created by Sam Jaffe on 6/16/17.
  6. //
  7. #pragma once
  8. #include "pointers/const_propogating_ptr.hpp"
  9. #include "pointers/not_null.hpp"
  10. #include "pointers/value_ptr.hpp"
  11. #include <map>
  12. template <typename K, typename V, typename Compare = std::less<K>> class trie;
  13. namespace detail {
  14. template <typename Trie, typename Iter> class trie_iterator_base;
  15. template <typename Iter, typename Trie, typename KS>
  16. Iter find_impl(Trie * tr, KS const & keys);
  17. }
  18. template <typename Trie, typename Iter> class trie_iterator;
  19. template <typename Trie, typename Iter> class trie_reverse_iterator;
  20. /*
  21. * \brief A trie is a type of associative container where each element is
  22. * locatable by a string of keys, including a zero-length string. Sometimes
  23. * known as a prefix-tree.
  24. * \tparam K The key type. The container is indexable by both single keys, and
  25. * iterable collections of keys e.g. std::initializer_list<K>.
  26. * \tparam V The value type of stored data.
  27. * \tparam Compare The comparator function, used by the backing std::map object
  28. * to order this container.
  29. *
  30. * In principle, a trie<K, V> is equal to a std::map<std::vector<K>, V> with the
  31. * additional features of being able to locate all elements whose keys start
  32. * with the key fragment { K1, K2, ..., Kn }.
  33. *
  34. * Because the conceptual structure of a trie is a tree, both pre-order and
  35. * port-order iteration are supported. In-order traversal is not supported,
  36. * because it doesn't translate well with non-binary trees.
  37. */
  38. template <typename K, typename V, typename Compare> class trie {
  39. public:
  40. using key_type = K;
  41. using mapped_type = V;
  42. using key_compare = Compare;
  43. using map_type =
  44. std::map<K, pointers::const_propogating_ptr<pointers::value_ptr<trie>>,
  45. Compare>;
  46. using local_iterator = typename map_type::iterator;
  47. using local_const_iterator = typename map_type::const_iterator;
  48. using local_reverse_iterator = typename map_type::reverse_iterator;
  49. using local_const_reverse_iterator =
  50. typename map_type::const_reverse_iterator;
  51. using iterator = trie_iterator<trie, local_iterator>;
  52. using const_iterator = trie_iterator<trie const, local_const_iterator>;
  53. using post_iterator = trie_reverse_iterator<trie, local_iterator>;
  54. using const_post_iterator =
  55. trie_reverse_iterator<trie const, local_const_iterator>;
  56. using reverse_iterator = trie_reverse_iterator<trie, local_reverse_iterator>;
  57. using const_reverse_iterator =
  58. trie_reverse_iterator<trie const, local_const_reverse_iterator>;
  59. private:
  60. template <typename KS>
  61. using element_type = std::decay_t<decltype(*std::begin(std::declval<KS>()))>;
  62. template <typename KS>
  63. using is_collection_t =
  64. std::enable_if_t<std::is_same<K, element_type<KS>>::value>;
  65. using impl_iterator = detail::trie_iterator_base<trie, local_iterator>;
  66. using impl_const_iterator =
  67. detail::trie_iterator_base<trie const, local_const_iterator>;
  68. private:
  69. mapped_type value_{};
  70. map_type impl_{};
  71. public:
  72. trie() = default;
  73. trie(trie const & other);
  74. trie(trie && other) : trie() { swap(*this, other); }
  75. trie(mapped_type const & root, std::map<K, trie, Compare> && children);
  76. ~trie() { clear(); }
  77. trie & operator=(mapped_type const & value);
  78. trie & operator=(trie const & value);
  79. trie & operator=(trie && value);
  80. operator mapped_type &() { return value(); }
  81. operator mapped_type const &() const { return value(); }
  82. mapped_type & value() { return value_; }
  83. mapped_type const & value() const { return value_; }
  84. template <typename KS, typename = is_collection_t<KS>>
  85. trie & operator[](KS const & keys) {
  86. return *emplace(keys).first.parent_trie_.top();
  87. }
  88. trie & operator[](key_type const & key) {
  89. return *emplace(key).first.parent_trie_.top();
  90. }
  91. trie & operator[](std::initializer_list<key_type> keys) {
  92. return operator[]<>(keys);
  93. }
  94. template <typename KS>
  95. std::pair<iterator, bool> insert(KS const & keys, mapped_type const & value) {
  96. return emplace_impl(keys, value);
  97. }
  98. std::pair<iterator, bool> insert(key_type const & key,
  99. mapped_type const & value) {
  100. return emplace_impl({key}, value);
  101. }
  102. std::pair<iterator, bool> insert(std::initializer_list<key_type> keys,
  103. mapped_type const & value) {
  104. return emplace_impl(keys, value);
  105. }
  106. template <typename KS, typename... Args>
  107. std::pair<iterator, bool> emplace(KS && keys, Args &&... args) {
  108. return emplace_impl(keys, std::forward<Args>(args)...);
  109. }
  110. template <typename... Args>
  111. std::pair<iterator, bool> emplace(key_type const & key, Args &&... args) {
  112. return emplace_impl({key}, std::forward<Args>(args)...);
  113. }
  114. template <typename... Args>
  115. std::pair<iterator, bool> emplace(key_type && key, Args &&... args) {
  116. return emplace_impl({key}, std::forward<Args>(args)...);
  117. }
  118. template <typename... Args>
  119. std::pair<iterator, bool> emplace(std::initializer_list<key_type> keys,
  120. Args &&... args) {
  121. return emplace_impl(keys, std::forward<Args>(args)...);
  122. }
  123. iterator begin() { return {this}; }
  124. iterator end() { return {}; }
  125. const_iterator begin() const { return {this}; }
  126. const_iterator end() const { return {}; }
  127. const_iterator cbegin() const { return begin(); }
  128. const_iterator cend() const { return end(); }
  129. reverse_iterator rbegin() { return {this}; }
  130. reverse_iterator rend() { return {}; }
  131. const_reverse_iterator rbegin() const { return {this}; }
  132. const_reverse_iterator rend() const { return {}; }
  133. const_reverse_iterator crbegin() const { return rbegin(); }
  134. const_reverse_iterator crend() const { return rend(); }
  135. local_iterator local_begin() { return impl_.begin(); }
  136. local_iterator local_end() { return impl_.end(); }
  137. local_const_iterator local_begin() const { return impl_.begin(); }
  138. local_const_iterator local_end() const { return impl_.end(); }
  139. local_reverse_iterator local_rbegin() { return impl_.rbegin(); }
  140. local_reverse_iterator local_rend() { return impl_.rend(); }
  141. local_const_reverse_iterator local_rbegin() const { return impl_.rbegin(); }
  142. local_const_reverse_iterator local_rend() const { return impl_.rend(); }
  143. template <typename KS> iterator find(KS const & keys) {
  144. return detail::find_impl<impl_iterator>(this, keys);
  145. }
  146. iterator find(key_type const & key) {
  147. return find_impl<impl_iterator>(this, {key});
  148. }
  149. iterator find(std::initializer_list<key_type> keys) {
  150. return find_impl<impl_iterator>(this, keys);
  151. }
  152. template <typename KS> const_iterator find(KS const & keys) const {
  153. return detail::find_impl<impl_const_iterator>(this, keys);
  154. }
  155. const_iterator find(key_type const & key) const {
  156. return find_impl<impl_const_iterator>(this, {key});
  157. }
  158. const_iterator find(std::initializer_list<key_type> keys) const {
  159. return find_impl<impl_const_iterator>(this, keys);
  160. }
  161. template <typename KS> void erase(KS const & keys) { drop(find(keys)); }
  162. void erase(key_type const & key) { drop(find(key)); }
  163. void erase(std::initializer_list<key_type> keys) { drop(find(keys)); }
  164. void clear();
  165. private:
  166. void drop(iterator it);
  167. template <typename Iter, typename Trie, typename KS>
  168. friend Iter detail::find_impl(Trie * tr, KS const & keys);
  169. template <typename Iter, typename Trie>
  170. static Iter find_impl(Trie * tr, std::initializer_list<key_type> keys) {
  171. return detail::find_impl<Iter>(tr, keys);
  172. }
  173. template <typename KS, typename... Args>
  174. std::pair<impl_iterator, bool> emplace_impl(KS && keys, Args &&... args);
  175. template <typename... Args>
  176. std::pair<impl_iterator, bool>
  177. emplace_impl(std::initializer_list<key_type> keys, Args &&... args) {
  178. return emplace_impl<std::initializer_list<key_type>>(
  179. std::move(keys), std::forward<Args>(args)...);
  180. }
  181. template <typename Key>
  182. void insert_impl(impl_iterator & out, bool & create, Key && key);
  183. friend bool operator==(trie const & lhs, trie const & rhs) {
  184. const_iterator it1 = lhs.begin(), it2 = rhs.begin();
  185. const_iterator const end1 = lhs.end(), end2 = lhs.end();
  186. for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
  187. if (it1 != it2) { return false; }
  188. }
  189. return it1 == end1 && it2 == end2;
  190. }
  191. friend bool operator!=(trie const & lhs, trie const & rhs) {
  192. return !(lhs == rhs);
  193. }
  194. friend void swap(trie & lhs, trie & rhs) {
  195. using std::swap;
  196. swap(lhs.value_, rhs.value_);
  197. swap(lhs.impl_, rhs.impl_);
  198. }
  199. };
  200. #include "trie.tpp"