trie_impl.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // trie_impl.hpp
  3. // trie
  4. //
  5. // Created by Sam Jaffe on 6/16/17.
  6. //
  7. #pragma once
  8. #include "trie.hpp"
  9. #include "trie_iterator.hpp"
  10. // n := total elements
  11. // d := average depth
  12. // Stack: O(1)
  13. // Operations: O(n)
  14. template <typename K, typename V, typename C>
  15. trie<K, V, C>::trie(trie const & other) : value_(other.value_) {
  16. impl_iterator current{this};
  17. for (const_iterator it = ++const_iterator{&other}, end = {}; it != end; ++it) {
  18. while (current.keys.size() >= it.keys.size()) { current.pop(); }
  19. auto tmp = current.stk.top()->insert(it.keys.back(), *it).first;
  20. current.push(tmp.iters.top());
  21. }
  22. }
  23. template <typename K, typename V, typename C>
  24. auto trie<K, V, C>::operator=(mapped_type const & value) -> self_t & {
  25. value_ = value;
  26. return *this;
  27. }
  28. template <typename K, typename V, typename C>
  29. auto trie<K, V, C>::operator=(trie const & other) -> self_t & {
  30. trie tmp{other};
  31. swap(*this, tmp);
  32. return *this;
  33. }
  34. template <typename K, typename V, typename C>
  35. auto trie<K, V, C>::operator=(trie && other) -> self_t & {
  36. swap(*this, other);
  37. return *this;
  38. }
  39. // n := total elements
  40. // d := average depth
  41. // Operations: O(log(n)/d)
  42. template <typename K, typename V, typename C>
  43. template <typename Key>
  44. void trie<K, V, C>::insert_impl(impl_iterator & out, bool & create, Key && key) {
  45. auto it = impl_.lower_bound(key);
  46. if ( it == impl_.end() || key_compare()(key, it->first) ) {
  47. create = true;
  48. it = impl_.emplace_hint(it, key, make_value<self_t>());
  49. }
  50. out.push({ it, impl_.end() });
  51. }
  52. // n := total elements
  53. // d := average depth
  54. // Operations: O(log(n))
  55. template <typename K, typename V, typename C>
  56. template <typename KS, typename... Args>
  57. auto trie<K, V, C>::emplace_impl(KS && keys, Args &&... args) -> std::pair<impl_iterator, bool> {
  58. impl_iterator it{this};
  59. bool create{false};
  60. for ( auto && key : keys ) {
  61. it.stk.top()->insert_impl(it, create, std::forward<decltype(key)>(key));
  62. }
  63. if (create) {
  64. it.stk.top()->value_ = { std::forward<Args>(args)... };
  65. }
  66. return { std::move(it) , create };
  67. }
  68. // n := total elements
  69. // d := average depth
  70. // Stack: O(1)
  71. // Operations: O(n)
  72. template <typename K, typename V, typename C>
  73. void trie<K, V, C>::clear() {
  74. for (reverse_iterator it = rbegin(), end = rend(); it != end && !it.iters.empty(); ++it) {
  75. auto ptr = std::move(it.iters.top()->second);
  76. ptr->impl_.clear();
  77. }
  78. value_ = mapped_type{};
  79. impl_.clear();
  80. }
  81. template <typename K, typename V, typename C>
  82. void trie<K, V, C>::drop(iterator it) {
  83. if (it == end()) return;
  84. it.stk.top()->clear();
  85. if (!it.iters.empty()) {
  86. auto to_erase = it.iters.top().current();
  87. it.pop();
  88. it.stk.top()->impl_.erase(to_erase);
  89. }
  90. }
  91. namespace detail {
  92. template <typename Iter, typename Trie, typename KS>
  93. Iter find_impl(Trie * tr, KS const & keys) {
  94. Iter rval{tr};
  95. for (auto & key : keys) {
  96. auto & top = rval.stk.top()->impl_;
  97. auto it = top.find(key), end = top.end();
  98. if ( it == end ) { return {}; }
  99. rval.push({it, end});
  100. }
  101. return rval;
  102. }
  103. }