| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // trie_impl.hpp
- // trie
- //
- // Created by Sam Jaffe on 6/16/17.
- //
- #pragma once
- #include "trie.hpp"
- #include "trie_iterator.hpp"
- template <typename K, typename V, typename C>
- trie<K, V, C>::trie(mapped_type const & value)
- : value_(value) {
-
- }
- // n := total elements
- // d := average depth
- // Stack: O(1)
- // Operations: O(n*d)
- template <typename K, typename V, typename C>
- trie<K, V, C>::trie(trie const & other) {
- for (const_iterator it = other.begin(), end = other.end(); it != end; ++it) {
- insert(it.keys, *it);
- }
- }
- template <typename K, typename V, typename C>
- auto trie<K, V, C>::operator=(mapped_type const & value) -> self_t & {
- value_ = value;
- return *this;
- }
- 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);
- }
- return *this;
- }
- template <typename K, typename V, typename C>
- template <typename KS, typename>
- auto trie<K, V, C>::operator[](KS const & keys) -> self_t & {
- self_t * rec = this;
- for ( key_type const & key : keys ) {
- rec = &(*rec)[key];
- }
- return *rec;
- }
- 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);
- if ( it == impl_.end() || key_compare()(key, it->first) ) {
- it = impl_.emplace_hint(it, key, make_value<self_t>());
- }
- return *(it->second);
- }
- 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;
- for ( key_type const & key : keys ) {
- rec = &(*rec)[key];
- }
- return *rec;
- }
- template <typename K, typename V, typename C>
- template <typename KS>
- auto trie<K, V, C>::insert(KS const & keys, mapped_type const & value) -> void {
- operator[](keys) = value;
- }
- template <typename K, typename V, typename C>
- auto trie<K, V, C>::insert(std::initializer_list<key_type> keys, mapped_type const & value) -> void {
- operator[](keys) = value;
- }
- template <typename K, typename V, typename C>
- auto trie<K, V, C>::insert(key_type const & key, mapped_type const & value) -> std::pair<iterator, bool> {
- 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 };
- }
- return { it, false };
- }
|