trie.hpp 7.1 KB

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