trie.hpp 8.3 KB

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