trie.hpp 7.3 KB

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