|
|
@@ -27,6 +27,11 @@ trie<K, V, C>::trie(trie const & other) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+template <typename K, typename V, typename C>
|
|
|
+trie<K, V, C>::~trie() {
|
|
|
+ clear();
|
|
|
+}
|
|
|
+
|
|
|
template <typename K, typename V, typename C>
|
|
|
auto trie<K, V, C>::operator=(mapped_type const & value) -> self_t & {
|
|
|
value_ = value;
|
|
|
@@ -35,12 +40,14 @@ auto trie<K, V, C>::operator=(mapped_type const & value) -> self_t & {
|
|
|
|
|
|
template <typename K, typename V, typename C>
|
|
|
auto trie<K, V, C>::operator=(trie const & other) -> self_t & {
|
|
|
- for (const_iterator it = other.begin(), end = other.end(); it != end; ++it) {
|
|
|
- insert(it.keys, *it);
|
|
|
- }
|
|
|
+ trie tmp{other};
|
|
|
+ swap(*this, tmp);
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
+// n := total elements
|
|
|
+// d := average depth
|
|
|
+// Operations: O(d*log(n/d))
|
|
|
template <typename K, typename V, typename C>
|
|
|
template <typename KS, typename>
|
|
|
auto trie<K, V, C>::operator[](KS const & keys) -> self_t & {
|
|
|
@@ -51,6 +58,9 @@ auto trie<K, V, C>::operator[](KS const & keys) -> self_t & {
|
|
|
return *rec;
|
|
|
}
|
|
|
|
|
|
+// n := total elements
|
|
|
+// d := average depth
|
|
|
+// Operations: O(log(n))
|
|
|
template <typename K, typename V, typename C>
|
|
|
auto trie<K, V, C>::operator[](key_type const & key) -> self_t & {
|
|
|
auto it = impl_.lower_bound(key);
|
|
|
@@ -60,6 +70,9 @@ auto trie<K, V, C>::operator[](key_type const & key) -> self_t & {
|
|
|
return *(it->second);
|
|
|
}
|
|
|
|
|
|
+// n := total elements
|
|
|
+// d := average depth
|
|
|
+// Operations: O(d*log(n/d))
|
|
|
template <typename K, typename V, typename C>
|
|
|
auto trie<K, V, C>::operator[](std::initializer_list<key_type> keys) -> self_t & {
|
|
|
self_t * rec = this;
|
|
|
@@ -85,6 +98,17 @@ auto trie<K, V, C>::insert(key_type const & key, mapped_type const & value) -> s
|
|
|
auto it = impl_.lower_bound(key);
|
|
|
if ( it == impl_.end() || key_compare()(key, it->first) ) {
|
|
|
return { impl_.emplace_hint(it, key, make_value<self_t>(value)), true };
|
|
|
+
|
|
|
+// n := total elements
|
|
|
+// d := average depth
|
|
|
+// Stack: O(1)
|
|
|
+// Operations: O(n)
|
|
|
+template <typename K, typename V, typename C>
|
|
|
+void trie<K, V, C>::clear() {
|
|
|
+ for (reverse_iterator it = rbegin(), end = rend(); it != end && !it.iters.empty(); ++it) {
|
|
|
+ auto ptr = std::move(it.iters.top()->second);
|
|
|
+ ptr->impl_.clear();
|
|
|
}
|
|
|
- return { it, false };
|
|
|
+ value_ = mapped_type{};
|
|
|
+ impl_.clear();
|
|
|
}
|