trie_impl.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. template <typename K, typename V, typename C>
  11. trie<K, V, C>::trie(mapped_type const & value)
  12. : value_(value) {
  13. }
  14. // n := total elements
  15. // d := average depth
  16. // Stack: O(1)
  17. // Operations: O(n)
  18. template <typename K, typename V, typename C>
  19. trie<K, V, C>::trie(trie const & other) : value_(other.value_) {
  20. iterator current{this};
  21. for (const_iterator it = ++other.begin(), end = other.end(); it != end; ++it) {
  22. while (current.keys.size() >= it.keys.size()) { current.pop(); }
  23. auto tmp = current.stk.top()->insert(it.keys.back(), *it).first;
  24. current.push(tmp.iters.top());
  25. }
  26. }
  27. template <typename K, typename V, typename C>
  28. trie<K, V, C>::~trie() {
  29. clear();
  30. }
  31. template <typename K, typename V, typename C>
  32. auto trie<K, V, C>::operator=(mapped_type const & value) -> self_t & {
  33. value_ = value;
  34. return *this;
  35. }
  36. template <typename K, typename V, typename C>
  37. auto trie<K, V, C>::operator=(trie const & other) -> self_t & {
  38. trie tmp{other};
  39. swap(*this, tmp);
  40. return *this;
  41. }
  42. // n := total elements
  43. // d := average depth
  44. // Operations: O(d*log(n/d))
  45. template <typename K, typename V, typename C>
  46. template <typename KS, typename>
  47. auto trie<K, V, C>::operator[](KS const & keys) -> self_t & {
  48. self_t * rec = this;
  49. for ( key_type const & key : keys ) {
  50. rec = &(*rec)[key];
  51. }
  52. return *rec;
  53. }
  54. // n := total elements
  55. // d := average depth
  56. // Operations: O(log(n))
  57. template <typename K, typename V, typename C>
  58. auto trie<K, V, C>::operator[](key_type const & key) -> self_t & {
  59. auto it = impl_.lower_bound(key);
  60. if ( it == impl_.end() || key_compare()(key, it->first) ) {
  61. it = impl_.emplace_hint(it, key, make_value<self_t>());
  62. }
  63. return *(it->second);
  64. }
  65. // n := total elements
  66. // d := average depth
  67. // Operations: O(d*log(n/d))
  68. template <typename K, typename V, typename C>
  69. auto trie<K, V, C>::operator[](std::initializer_list<key_type> keys) -> self_t & {
  70. self_t * rec = this;
  71. for ( key_type const & key : keys ) {
  72. rec = &(*rec)[key];
  73. }
  74. return *rec;
  75. }
  76. template <typename K, typename V, typename C>
  77. template <typename KS>
  78. auto trie<K, V, C>::insert(KS const & keys, mapped_type const & value) -> void {
  79. operator[](keys) = value;
  80. }
  81. template <typename K, typename V, typename C>
  82. auto trie<K, V, C>::insert(std::initializer_list<key_type> keys, mapped_type const & value) -> void {
  83. operator[](keys) = value;
  84. }
  85. // n := total elements
  86. // d := average depth
  87. // Operations: O(log(n))
  88. template <typename K, typename V, typename C>
  89. auto trie<K, V, C>::insert(key_type const & key, mapped_type const & value) -> std::pair<iterator, bool> {
  90. std::pair<iterator, bool> rval{this, false};
  91. auto it = impl_.lower_bound(key);
  92. if ( it == impl_.end() || key_compare()(key, it->first) ) {
  93. rval.second = true;
  94. it = impl_.emplace_hint(it, key, make_value<self_t>(value));
  95. }
  96. rval.first.push(make_end_aware_iterator(it, impl_.end()));
  97. return rval;
  98. }
  99. // n := total elements
  100. // d := average depth
  101. // Stack: O(1)
  102. // Operations: O(n)
  103. template <typename K, typename V, typename C>
  104. void trie<K, V, C>::clear() {
  105. for (reverse_iterator it = rbegin(), end = rend(); it != end && !it.iters.empty(); ++it) {
  106. auto ptr = std::move(it.iters.top()->second);
  107. ptr->impl_.clear();
  108. }
  109. value_ = mapped_type{};
  110. impl_.clear();
  111. }