trie_impl.hpp 3.0 KB

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