trie.hpp 7.5 KB

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