trie.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = std::map<K, const_propogating_ptr<value_ptr<trie>>, Compare>;
  44. using local_iterator = typename map_type::iterator;
  45. using local_const_iterator = typename map_type::const_iterator;
  46. using local_reverse_iterator = typename map_type::reverse_iterator;
  47. using local_const_reverse_iterator =
  48. typename map_type::const_reverse_iterator;
  49. using iterator = trie_iterator<trie, local_iterator>;
  50. using const_iterator = trie_iterator<trie const, local_const_iterator>;
  51. using post_iterator = trie_reverse_iterator<trie, local_iterator>;
  52. using const_post_iterator =
  53. trie_reverse_iterator<trie const, local_const_iterator>;
  54. using reverse_iterator = trie_reverse_iterator<trie, local_reverse_iterator>;
  55. using const_reverse_iterator =
  56. trie_reverse_iterator<trie const, local_const_reverse_iterator>;
  57. private:
  58. template <typename KS>
  59. using element_type = std::decay_t<decltype(*std::begin(std::declval<KS>()))>;
  60. template <typename KS>
  61. using is_collection_t =
  62. std::enable_if_t<std::is_same<K, element_type<KS>>::value>;
  63. using impl_iterator = detail::trie_iterator_base<trie, local_iterator>;
  64. using impl_const_iterator =
  65. detail::trie_iterator_base<trie const, local_const_iterator>;
  66. private:
  67. mapped_type value_{};
  68. map_type impl_{};
  69. public:
  70. trie() {}
  71. trie(trie const & other);
  72. trie(trie && other) : trie() { swap(*this, other); }
  73. ~trie() { clear(); }
  74. trie & operator=(mapped_type const & value);
  75. trie & operator=(trie const & value);
  76. trie & operator=(trie && value);
  77. operator mapped_type &() { return value(); }
  78. operator mapped_type const &() const { return value(); }
  79. mapped_type & value() { return value_; }
  80. mapped_type const & value() const { return value_; }
  81. template <typename KS, typename = is_collection_t<KS>>
  82. trie & operator[](KS const & keys) {
  83. return *emplace(keys).first.parent_trie_.top();
  84. }
  85. trie & operator[](key_type const & key) {
  86. return *emplace(key).first.parent_trie_.top();
  87. }
  88. trie & operator[](std::initializer_list<key_type> keys) {
  89. return operator[]<>(keys);
  90. }
  91. template <typename KS>
  92. std::pair<iterator, bool> insert(KS const & keys, mapped_type const & value) {
  93. return emplace_impl(keys, value);
  94. }
  95. std::pair<iterator, bool> insert(key_type const & key,
  96. mapped_type const & value) {
  97. return emplace_impl({key}, value);
  98. }
  99. std::pair<iterator, bool> insert(std::initializer_list<key_type> keys,
  100. mapped_type const & value) {
  101. return emplace_impl(keys, value);
  102. }
  103. template <typename KS, typename... Args>
  104. std::pair<iterator, bool> emplace(KS && keys, Args &&... args) {
  105. return emplace_impl(keys, std::forward<Args>(args)...);
  106. }
  107. template <typename... Args>
  108. std::pair<iterator, bool> emplace(key_type const & key, Args &&... args) {
  109. return emplace_impl({key}, std::forward<Args>(args)...);
  110. }
  111. template <typename... Args>
  112. std::pair<iterator, bool> emplace(key_type && key, Args &&... args) {
  113. return emplace_impl({key}, std::forward<Args>(args)...);
  114. }
  115. template <typename... Args>
  116. std::pair<iterator, bool> emplace(std::initializer_list<key_type> keys,
  117. Args &&... args) {
  118. return emplace_impl(keys, std::forward<Args>(args)...);
  119. }
  120. iterator begin() { return {this}; }
  121. iterator end() { return {}; }
  122. const_iterator begin() const { return {this}; }
  123. const_iterator end() const { return {}; }
  124. const_iterator cbegin() const { return begin(); }
  125. const_iterator cend() const { return end(); }
  126. reverse_iterator rbegin() { return {this}; }
  127. reverse_iterator rend() { return {}; }
  128. const_reverse_iterator rbegin() const { return {this}; }
  129. const_reverse_iterator rend() const { return {}; }
  130. const_reverse_iterator crbegin() const { return rbegin(); }
  131. const_reverse_iterator crend() const { return rend(); }
  132. local_iterator local_begin() { return impl_.begin(); }
  133. local_iterator local_end() { return impl_.end(); }
  134. local_const_iterator local_begin() const { return impl_.begin(); }
  135. local_const_iterator local_end() const { return impl_.end(); }
  136. local_reverse_iterator local_rbegin() { return impl_.rbegin(); }
  137. local_reverse_iterator local_rend() { return impl_.rend(); }
  138. local_const_reverse_iterator local_rbegin() const { return impl_.rbegin(); }
  139. local_const_reverse_iterator local_rend() const { return impl_.rend(); }
  140. template <typename KS> iterator find(KS const & keys) {
  141. return detail::find_impl<impl_iterator>(this, keys);
  142. }
  143. iterator find(key_type const & key) {
  144. return find_impl<impl_iterator>(this, {key});
  145. }
  146. iterator find(std::initializer_list<key_type> keys) {
  147. return find_impl<impl_iterator>(this, keys);
  148. }
  149. template <typename KS> const_iterator find(KS const & keys) const {
  150. return detail::find_impl<impl_const_iterator>(this, keys);
  151. }
  152. const_iterator find(key_type const & key) const {
  153. return find_impl<impl_const_iterator>(this, {key});
  154. }
  155. const_iterator find(std::initializer_list<key_type> keys) const {
  156. return find_impl<impl_const_iterator>(this, keys);
  157. }
  158. template <typename KS> void erase(KS const & keys) { drop(find(keys)); }
  159. void erase(key_type const & key) { drop(find(key)); }
  160. void erase(std::initializer_list<key_type> keys) { drop(find(keys)); }
  161. void clear();
  162. private:
  163. void drop(iterator it);
  164. template <typename Iter, typename Trie, typename KS>
  165. friend Iter detail::find_impl(Trie * tr, KS const & keys);
  166. template <typename Iter, typename Trie>
  167. static Iter find_impl(Trie * tr, std::initializer_list<key_type> keys) {
  168. return detail::find_impl<Iter>(tr, keys);
  169. }
  170. template <typename KS, typename... Args>
  171. std::pair<impl_iterator, bool> emplace_impl(KS && keys, Args &&... args);
  172. template <typename... Args>
  173. std::pair<impl_iterator, bool>
  174. emplace_impl(std::initializer_list<key_type> keys, Args &&... args) {
  175. return emplace_impl<std::initializer_list<key_type>>(
  176. std::move(keys), std::forward<Args>(args)...);
  177. }
  178. template <typename Key>
  179. void insert_impl(impl_iterator & out, bool & create, Key && key);
  180. friend bool operator==(trie const & lhs, trie const & rhs) {
  181. const_iterator it1 = lhs.begin(), it2 = rhs.begin();
  182. const_iterator const end1 = lhs.end(), end2 = lhs.end();
  183. for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
  184. if (it1 != it2) { return false; }
  185. }
  186. return it1 == end1 && it2 == end2;
  187. }
  188. friend bool operator!=(trie const & lhs, trie const & rhs) {
  189. return !(lhs == rhs);
  190. }
  191. friend void swap(trie & lhs, trie & rhs) {
  192. using std::swap;
  193. swap(lhs.value_, rhs.value_);
  194. swap(lhs.impl_, rhs.impl_);
  195. }
  196. };
  197. #include "trie.tpp"