trie.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. private:
  40. using self_t = trie<K, V, Compare>;
  41. using impl_value_type = const_propogating_ptr<value_ptr<self_t>>;
  42. using backing_t = std::map<K, impl_value_type, Compare>;
  43. template <typename KS>
  44. using element_type = std::decay_t<decltype(*std::begin(std::declval<KS>()))>;
  45. template <typename KS>
  46. using is_collection_t =
  47. std::enable_if_t<std::is_same<K, element_type<KS>>::value>;
  48. public:
  49. using key_type = K;
  50. using mapped_type = V;
  51. using key_compare = Compare;
  52. using local_iterator = typename backing_t::iterator;
  53. using local_const_iterator = typename backing_t::const_iterator;
  54. using local_reverse_iterator = typename backing_t::reverse_iterator;
  55. using local_const_reverse_iterator =
  56. typename backing_t::const_reverse_iterator;
  57. using iterator = trie_iterator<self_t, local_iterator>;
  58. using const_iterator = trie_iterator<self_t const, local_const_iterator>;
  59. using post_iterator = trie_reverse_iterator<self_t, local_iterator>;
  60. using const_post_iterator =
  61. trie_reverse_iterator<self_t const, local_const_iterator>;
  62. using reverse_iterator =
  63. trie_reverse_iterator<self_t, local_reverse_iterator>;
  64. using const_reverse_iterator =
  65. trie_reverse_iterator<self_t const, local_const_reverse_iterator>;
  66. private:
  67. using impl_iterator = detail::trie_iterator_base<self_t, local_iterator>;
  68. using impl_const_iterator =
  69. detail::trie_iterator_base<self_t const, local_const_iterator>;
  70. public:
  71. trie() {}
  72. trie(trie const & other);
  73. trie(trie && other) : trie() { swap(*this, other); }
  74. ~trie() { clear(); }
  75. self_t & operator=(mapped_type const & value);
  76. self_t & operator=(trie const & value);
  77. self_t & 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. self_t & operator[](KS const & keys) {
  84. return *emplace(keys).first.parent_trie_.top();
  85. }
  86. self_t & operator[](key_type const & key) {
  87. return *emplace(key).first.parent_trie_.top();
  88. }
  89. self_t & 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.keys_.empty() && !it2.keys_.empty() &&
  186. it1.keys_.back() != it2.keys_.back()) {
  187. return false;
  188. } else if (it1.root().value_ != it2.root().value_) {
  189. return false;
  190. }
  191. }
  192. return it1 == end1 && it2 == end2;
  193. }
  194. friend bool operator!=(trie const & lhs, trie const & rhs) {
  195. return !(lhs == rhs);
  196. }
  197. friend void swap(trie & lhs, trie & rhs) {
  198. using std::swap;
  199. swap(lhs.value_, rhs.value_);
  200. swap(lhs.impl_, rhs.impl_);
  201. }
  202. mapped_type value_{};
  203. backing_t impl_{};
  204. };
  205. #include "trie.tpp"