| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401 |
- // Copyright (C) 2020 T. Zachary Laine
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_PARSER_DETAIL_TEXT_TRIE_HPP
- #define BOOST_PARSER_DETAIL_TEXT_TRIE_HPP
- #include <boost/parser/detail/text/config.hpp>
- #include <boost/parser/detail/text/trie_fwd.hpp>
- #include <algorithm>
- #include <memory>
- #include <type_traits>
- #include <vector>
- namespace boost::parser::detail { namespace text {
- /** An optional reference. Its optionality is testable, via the operator
- bool() members, and it is implicitly convertible to the underlying
- value type. */
- template<typename T, bool Const = std::is_const<T>::value>
- struct optional_ref
- {
- private:
- T * t_;
- public:
- optional_ref() : t_(nullptr) {}
- optional_ref(T & t) : t_(&t) {}
- template<typename U>
- auto operator=(U && u)
- -> decltype(*this->t_ = static_cast<U &&>(u), *this)
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- *t_ = static_cast<U &&>(u);
- return *this;
- }
- explicit operator bool() const & { return t_ != nullptr; }
- explicit operator bool() & { return t_ != nullptr; }
- explicit operator bool() && { return t_ != nullptr; }
- T const & operator*() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- T const * operator->() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return t_;
- }
- operator T const &() const &
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- operator T const &() const &&
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- T & operator*()
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- T * operator->()
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return t_;
- }
- operator T &() &
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- operator T &() &&
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- };
- template<typename T>
- struct optional_ref<T, true>
- {
- private:
- T * t_;
- public:
- optional_ref() : t_(nullptr) {}
- optional_ref(T & t) : t_(&t) {}
- explicit operator bool() const & { return t_ != nullptr; }
- explicit operator bool() && { return t_ != nullptr; }
- T & operator*() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- T * operator->() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return t_;
- }
- operator T &() const &
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- operator T &() const &&
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- };
- template<>
- struct optional_ref<bool, false>
- {
- private:
- bool * t_;
- public:
- optional_ref() : t_(nullptr) {}
- optional_ref(bool & t) : t_(&t) {}
- template<typename U>
- auto operator=(U && u)
- -> decltype(*this->t_ = static_cast<U &&>(u), *this)
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- *t_ = static_cast<U &&>(u);
- return *this;
- }
- explicit operator bool() const & { return t_ != nullptr; }
- explicit operator bool() && { return t_ != nullptr; }
- bool const & operator*() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- bool const * operator->() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return t_;
- }
- bool & operator*()
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- bool * operator->()
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return t_;
- }
- };
- template<>
- struct optional_ref<bool const, true>
- {
- private:
- bool const * t_;
- public:
- optional_ref() : t_(nullptr) {}
- optional_ref(bool const & t) : t_(&t) {}
- explicit operator bool() const & { return t_ != nullptr; }
- explicit operator bool() && { return t_ != nullptr; }
- bool const & operator*() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return *t_;
- }
- bool const * operator->() const
- {
- BOOST_PARSER_DEBUG_ASSERT(t_);
- return t_;
- }
- };
- namespace detail {
- template<
- typename ParentIndexing,
- typename Key,
- typename Value,
- std::size_t KeySize = 0>
- struct trie_node_t;
- struct no_index_within_parent_t
- {
- std::size_t value() const
- {
- BOOST_PARSER_DEBUG_ASSERT(!"This should never be called.");
- return 0;
- }
- template<
- typename Key,
- typename Value,
- typename Iter,
- std::size_t KeySize>
- void insert_at(
- std::unique_ptr<trie_node_t<
- no_index_within_parent_t,
- Key,
- Value,
- KeySize>> const & child,
- std::ptrdiff_t offset,
- Iter it,
- Iter end)
- {}
- template<typename Key, typename Value, std::size_t KeySize>
- void insert_ptr(std::unique_ptr<trie_node_t<
- no_index_within_parent_t,
- Key,
- Value,
- KeySize>> const & child)
- {}
- template<typename Iter>
- void erase(Iter it, Iter end)
- {}
- };
- template<typename Char>
- struct char_range
- {
- Char * first_;
- Char * last_;
- Char * begin() const { return first_; }
- Char * end() const { return last_; }
- };
- struct void_type
- {};
- }
- /** A key/value pair found in a trie or trie_map. */
- template<typename Key, typename Value>
- struct trie_element
- {
- trie_element() {}
- trie_element(Key k, Value v) : key(k), value(v) {}
- template<typename KeyT, typename ValueT>
- trie_element(trie_element<KeyT, ValueT> const & rhs) :
- key(rhs.key), value(rhs.value)
- {}
- template<typename KeyT, typename ValueT>
- trie_element(trie_element<KeyT, ValueT> && rhs) :
- key(std::move(rhs.key)), value(std::move(rhs.value))
- {}
- template<typename KeyT, typename ValueT>
- trie_element & operator=(trie_element<KeyT, ValueT> const & rhs)
- {
- key = rhs.key;
- value = rhs.value;
- return *this;
- }
- template<typename KeyT, typename ValueT>
- trie_element & operator=(trie_element<KeyT, ValueT> && rhs)
- {
- key = std::move(rhs.key);
- value = std::move(rhs.value);
- return *this;
- }
- Key key;
- Value value;
- friend bool
- operator==(trie_element const & lhs, trie_element const & rhs)
- {
- return lhs.key == rhs.key && lhs.value == rhs.value;
- }
- friend bool
- operator!=(trie_element const & lhs, trie_element const & rhs)
- {
- return !(lhs == rhs);
- }
- };
- /** The result type for trie operations that produce a matching
- subsequence. */
- struct trie_match_result
- {
- trie_match_result() : node(nullptr), size(0), match(false), leaf(false)
- {}
- trie_match_result(void const * n, std::ptrdiff_t s, bool m, bool l) :
- node(n),
- size(s),
- match(m),
- leaf(l)
- {}
- /** An opaque pointer to the underlying trie node. */
- void const * node;
- /** The size/length of the match, from the root through `node`,
- inclusive. */
- std::ptrdiff_t size;
- /** True iff this result represents a match. Stated another way,
- `match` is true iff `node` represents an element in the trie
- (whether or not `node` has children). */
- bool match;
- /** True iff `node` is a leaf (that is, that `node` hs no
- children). */
- bool leaf;
- friend bool
- operator==(trie_match_result const & lhs, trie_match_result const & rhs)
- {
- return lhs.node == rhs.node && lhs.size == rhs.size &&
- lhs.match == rhs.match && lhs.leaf == rhs.leaf;
- }
- friend bool
- operator!=(trie_match_result const & lhs, trie_match_result const & rhs)
- {
- return !(lhs == rhs);
- }
- };
- /** A trie, or prefix-tree. A trie contains a set of keys, each of which
- is a sequence, and a set of values, each associated with some key.
- Each node in the trie represents some prefix found in at least one
- member of the set of values contained in the trie. If a certain node
- represents the end of one of the keys, it has an associated value.
- Such a node may or may not have children.
- Complexity of lookups is always O(M), where M is the size of the Key
- being lookep up. Note that this implies that lookup complexity is
- independent of the size of the trie.
- \param Key The key-type; must be a sequence of values comparable via
- Compare()(x, y).
- \param Value The value-type.
- \param Compare The type of the comparison object used to compare
- elements of the key-type.
- \note trie is not a C++ Container, according to the definition in the
- C++ standard, in part because it does not have any iterators. This
- allows trie to do slightly less work when it does insertions and
- erasures. If you need iterators, use trie_map or trie_set. */
- template<
- typename Key,
- typename Value,
- typename Compare,
- std::size_t KeySize>
- struct trie
- {
- private:
- using node_t = detail::
- trie_node_t<detail::no_index_within_parent_t, Key, Value, KeySize>;
- public:
- using key_type = Key;
- using value_type = Value;
- using key_compare = Compare;
- using key_element_type = typename Key::value_type;
- using reference = value_type &;
- using const_reference = value_type const &;
- using size_type = std::ptrdiff_t;
- using difference_type = std::ptrdiff_t;
- using match_result = trie_match_result;
- trie() : size_(0) {}
- trie(Compare const & comp) : size_(0), comp_(comp) {}
- template<typename Iter, typename Sentinel>
- trie(Iter first, Sentinel last, Compare const & comp = Compare()) :
- size_(0), comp_(comp)
- {
- insert(first, last);
- }
- template<typename Range>
- explicit trie(Range r, Compare const & comp = Compare()) :
- size_(0), comp_(comp)
- {
- insert(detail::begin(r), detail::end(r));
- }
- trie(std::initializer_list<trie_element<key_type, value_type>> il) :
- size_(0)
- {
- insert(il);
- }
- trie &
- operator=(std::initializer_list<trie_element<key_type, value_type>> il)
- {
- clear();
- for (auto const & x : il) {
- insert(x);
- }
- return *this;
- }
- bool empty() const { return size_ == 0; }
- size_type size() const { return size_; }
- #ifndef BOOST_TEXT_DOXYGEN
- #define BOOST_TRIE_C_STR_OVERLOAD(rtype, func) \
- template<typename Char, std::size_t N> \
- rtype func(Char const(&chars)[N]) \
- { \
- static_assert( \
- std::is_same<Char, key_element_type>::value, \
- "Only well-formed when Char is Key::value_type."); \
- return func(detail::char_range<Char const>{chars, chars + N - 1}); \
- }
- #define BOOST_TRIE_C_STR_OVERLOAD_QUALS(rtype, func, quals) \
- template<typename Char, std::size_t N> \
- rtype func(Char const(&chars)[N]) quals \
- { \
- static_assert( \
- std::is_same<Char, key_element_type>::value, \
- "Only well-formed when Char is Key::value_type."); \
- return func(detail::char_range<Char const>{chars, chars + N - 1}); \
- }
- #endif
- /** Returns true if `key` is found in *this. */
- template<typename KeyRange>
- bool contains(KeyRange const & key) const
- {
- auto first = detail::begin(key);
- auto const last = detail::end(key);
- auto match = longest_match_impl<false>(first, last);
- return first == last && match.match;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- BOOST_TRIE_C_STR_OVERLOAD_QUALS(bool, contains, const)
- #endif
- /** Returns the longest subsequence of `[first, last)` found in *this,
- whether or not it is a match. */
- template<typename KeyIter, typename Sentinel>
- match_result longest_subsequence(KeyIter first, Sentinel last) const
- {
- return longest_match_impl<false>(first, last);
- }
- /** Returns the longest subsequence of `key` found in *this, whether
- or not it is a match. */
- template<typename KeyRange>
- match_result longest_subsequence(KeyRange const & key) const
- {
- return longest_subsequence(detail::begin(key), detail::end(key));
- }
- #ifndef BOOST_TEXT_DOXYGEN
- BOOST_TRIE_C_STR_OVERLOAD_QUALS(match_result, longest_subsequence, const)
- #endif
- /** Returns the longest matching subsequence of `[first, last)` found
- in *this. */
- template<typename KeyIter, typename Sentinel>
- match_result longest_match(KeyIter first, Sentinel last) const
- {
- return longest_match_impl<true>(first, last);
- }
- /** Returns the longest matching subsequence of `key` found in
- *this. */
- template<typename KeyRange>
- match_result longest_match(KeyRange const & key) const
- {
- return longest_match(detail::begin(key), detail::end(key));
- }
- #ifndef BOOST_TEXT_DOXYGEN
- BOOST_TRIE_C_STR_OVERLOAD_QUALS(match_result, longest_match, const)
- #endif
- /** Returns the result of extending `prev` by one element, `e`. */
- template<typename KeyElementT>
- match_result extend_subsequence(match_result prev, KeyElementT e) const
- {
- auto e_ptr = &e;
- return extend_subsequence_impl<false>(
- match_result{prev}, e_ptr, e_ptr + 1);
- }
- /** Returns the result of extending `prev` by the longest subsequence
- of `[first, last)` found in *this. */
- template<typename KeyIter, typename Sentinel>
- match_result extend_subsequence(
- match_result prev, KeyIter first, Sentinel last) const
- {
- return extend_subsequence_impl<false>(
- match_result{prev}, first, last);
- }
- /** Returns the result of extending `prev` by one element, `e`, if
- that would form a match, and `prev` otherwise. `prev` must be a
- match. */
- template<typename KeyElementT>
- match_result extend_match(match_result prev, KeyElementT e) const
- {
- BOOST_PARSER_DEBUG_ASSERT(prev.match);
- auto e_ptr = &e;
- return extend_subsequence_impl<true>(prev, e_ptr, e_ptr + 1);
- }
- /** Returns the result of extending `prev` by the longest subsequence
- of `[first, last)` found in *this, if that would form a match, and
- `prev` otherwise. `prev` must be a match. */
- template<typename KeyIter, typename Sentinel>
- match_result
- extend_match(match_result prev, KeyIter first, Sentinel last) const
- {
- BOOST_PARSER_DEBUG_ASSERT(prev.match);
- return extend_subsequence_impl<true>(prev, first, last);
- }
- /** Writes the sequence of elements that would advance `prev` by one
- element to `out`, and returns the final value of `out` after the
- writes. */
- template<typename OutIter>
- OutIter copy_next_key_elements(match_result prev, OutIter out) const
- {
- auto node = to_node_ptr(prev.node);
- return node->copy_next_key_elements(out);
- }
- /** Returns an optional reference to the const value associated with
- `key` in *this (if any). */
- template<typename KeyRange>
- optional_ref<value_type const> operator[](KeyRange const & key) const
- {
- auto first = detail::begin(key);
- auto const last = detail::end(key);
- auto match = longest_match_impl<false>(first, last);
- if (first != last || !match.match)
- return {};
- return *to_node_ptr(match.node)->value();
- }
- #ifndef BOOST_TEXT_DOXYGEN
- BOOST_TRIE_C_STR_OVERLOAD_QUALS(
- optional_ref<value_type const>, operator[], const)
- #endif
- /** Returns an optional reference to the const value associated with
- `match` in *this (if any). */
- optional_ref<value_type const> operator[](match_result match) const
- {
- if (!match.match)
- return {};
- return *to_node_ptr(match.node)->value();
- }
- void clear()
- {
- header_ = node_t();
- size_ = 0;
- }
- /** Returns an optional reference to the value associated with `key`
- in *this (if any). */
- template<typename KeyRange>
- optional_ref<value_type> operator[](KeyRange const & key)
- {
- auto first = detail::begin(key);
- auto const last = detail::end(key);
- auto match = longest_match_impl<false>(first, last);
- if (first != last || !match.match)
- return {};
- return *const_cast<node_t *>(to_node_ptr(match.node))->value();
- }
- #ifndef BOOST_TEXT_DOXYGEN
- BOOST_TRIE_C_STR_OVERLOAD(optional_ref<value_type>, operator[])
- #endif
- /** Returns an optional reference to the value associated with `match`
- in *this (if any). */
- optional_ref<value_type> operator[](match_result match)
- {
- if (!match.match)
- return {};
- return *const_cast<node_t *>(to_node_ptr(match.node))->value();
- }
- /** Inserts the key/value pair `[first, last)`, `value` into *this.
- Returns false if the key already exists in *this, true
- otherwise. */
- template<typename KeyIter, typename Sentinel>
- bool insert(KeyIter first, Sentinel last, Value value)
- {
- if (empty()) {
- std::unique_ptr<node_t> new_node(new node_t(&header_));
- header_.insert(std::move(new_node));
- }
- auto match = longest_match_impl<false>(first, last);
- if (first == last && match.match)
- return false;
- auto node = create_children(
- const_cast<node_t *>(to_node_ptr(match.node)), first, last);
- node->value() = std::move(value);
- ++size_;
- return true;
- }
- /** Inserts the key/value pair `key`, `value` into *this. Returns
- false if the key already exists in *this, true otherwise. */
- template<typename KeyRange>
- bool insert(KeyRange const & key, Value value)
- {
- return insert(
- detail::begin(key), detail::end(key), std::move(value));
- }
- template<typename Char, std::size_t N>
- bool insert(Char const (&chars)[N], Value value)
- {
- static_assert(
- std::is_same<Char, key_element_type>::value,
- "Only well-formed when Char is Key::value_type.");
- return insert(
- detail::char_range<Char const>{chars, chars + N - 1},
- std::move(value));
- }
- /** Inserts the sequence of key/value pairs `[first, last)` into
- *this. */
- template<typename Iter, typename Sentinel>
- void insert(Iter first, Sentinel last)
- {
- for (; first != last; ++first) {
- insert(first->key, first->value);
- }
- }
- /** Inserts the element e.key, e.value into *this. */
- bool insert(trie_element<key_type, value_type> const & e)
- {
- return insert(e.key, e.value);
- }
- /** Inserts the sequence of key/value pairs `r` into
- *this. */
- template<typename Range>
- bool insert(Range const & r)
- {
- return insert(detail::begin(r), detail::end(r));
- }
- /** Inserts the sequence of key/value pairs `il` into *this. */
- void
- insert(std::initializer_list<trie_element<key_type, value_type>> il)
- {
- for (auto const & x : il) {
- insert(x);
- }
- }
- /** Inserts the key/value pair `[first, last)`, `value` into *this, or
- assigns `value` over the existing value associated with `[first,
- last)`, if this key is already found in *this. The `inserted`
- field of the result will be true if the operation resulted in a
- new insertion, or false otherwise. */
- template<typename KeyIter, typename Sentinel>
- void insert_or_assign(KeyIter first, Sentinel last, Value value)
- {
- auto it = first;
- auto match = longest_match_impl<false>(it, last);
- if (it == last && match.match) {
- const_cast<Value &>(*to_node_ptr(match.node)->value()) =
- std::move(value);
- }
- insert(first, last, std::move(value));
- }
- /** Inserts the key/value pair `key`, `value` into *this, or assigns
- `value` over the existing value associated with `key`, if `key` is
- already found in *this. The `inserted` field of the result will
- be true if the operation resulted in a new insertion, or false
- otherwise. */
- template<typename KeyRange>
- void insert_or_assign(KeyRange const & key, Value value)
- {
- insert_or_assign(
- detail::begin(key), detail::end(key), std::move(value));
- }
- /** Erases the key/value pair associated with `key` from *this.
- Returns true if the key is found in *this, false otherwise. */
- template<typename KeyRange>
- bool erase(KeyRange const & key)
- {
- auto first = detail::begin(key);
- auto const last = detail::end(key);
- auto match = longest_match_impl<false>(first, last);
- if (first != last || !match.match)
- return false;
- return erase(match);
- }
- /** Erases the key/value pair associated with `match` from *this.
- Returns true if the key is found in *this, false otherwise. */
- bool erase(match_result match)
- {
- auto node = const_cast<node_t *>(to_node_ptr(match.node));
- if (node == &header_)
- return false;
- --size_;
- if (!node->empty()) {
- // node has a value, but also children. Remove the value and
- // return the next-iterator.
- node->value() = std::optional<Value>();
- return true;
- }
- // node has a value, *and* no children. Remove it and all its
- // singular predecessors.
- auto parent = const_cast<node_t *>(node->parent());
- parent->erase(node);
- while (parent->parent() && parent->empty() && !parent->value()) {
- node = parent;
- parent = const_cast<node_t *>(node->parent());
- parent->erase(node);
- }
- return true;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- BOOST_TRIE_C_STR_OVERLOAD(bool, erase)
- #endif
- void swap(trie & other)
- {
- header_.swap(other.header_);
- std::swap(size_, other.size_);
- std::swap(comp_, other.comp_);
- }
- friend bool operator==(trie const & lhs, trie const & rhs)
- {
- if (lhs.size_ != rhs.size_)
- return false;
- return lhs.header_ == rhs.header_;
- }
- friend bool operator!=(trie const & lhs, trie const & rhs)
- {
- return !(lhs == rhs);
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- trie const * const_this() { return const_cast<trie const *>(this); }
- static node_t const * to_node_ptr(void const * ptr)
- {
- return static_cast<node_t const *>(ptr);
- }
- template<bool StopAtMatch, typename KeyIter, typename Sentinel>
- match_result
- longest_match_impl(KeyIter & first, Sentinel last) const
- {
- return extend_subsequence_impl<StopAtMatch>(
- match_result{&header_, 0, false, true}, first, last);
- }
- template<bool StopAtMatch, typename KeyIter, typename Sentinel>
- match_result extend_subsequence_impl(
- match_result prev, KeyIter & first, Sentinel last) const
- {
- if (to_node_ptr(prev.node) == &header_) {
- if (header_.empty())
- return prev;
- prev.node = header_.child(0);
- }
- if (first == last) {
- prev.match = !!to_node_ptr(prev.node)->value();
- prev.leaf = to_node_ptr(prev.node)->empty();
- return prev;
- }
- node_t const * node = to_node_ptr(prev.node);
- size_type size = prev.size;
- node_t const * last_match_node = nullptr;
- size_type last_match_size = 0;
- while (first != last) {
- auto const it = node->find(*first, comp_);
- if (it == node->end())
- break;
- ++first;
- ++size;
- node = it->get();
- if (StopAtMatch && !!node->value()) {
- last_match_node = node;
- last_match_size = size;
- }
- }
- if (StopAtMatch && last_match_node) {
- node = last_match_node;
- size = last_match_size;
- }
- return match_result{node, size, !!node->value(), node->empty()};
- }
- template<typename KeyIter, typename Sentinel>
- node_t * create_children(node_t * node, KeyIter first, Sentinel last)
- {
- auto retval = node;
- for (; first != last; ++first) {
- std::unique_ptr<node_t> new_node(new node_t(retval));
- retval =
- retval->insert(*first, comp_, std::move(new_node))->get();
- }
- return retval;
- }
- node_t header_;
- size_type size_;
- key_compare comp_;
- template<typename Key2, typename Value2, typename Compare2>
- friend struct trie_map;
- #endif
- };
- namespace detail {
- template<
- typename ParentIndexing,
- typename Key,
- typename Value,
- std::size_t KeySize>
- struct trie_node_t
- {
- using children_t = std::vector<std::unique_ptr<trie_node_t>>;
- using iterator = typename children_t::iterator;
- using const_iterator = typename children_t::const_iterator;
- using key_element = typename Key::value_type;
- static_assert(std::is_unsigned<key_element>::value, "");
- trie_node_t() : parent_(nullptr) {}
- trie_node_t(trie_node_t * parent) : parent_(parent) {}
- trie_node_t(trie_node_t const & other) :
- children_(other.children_.size()),
- value_(other.value_),
- parent_(other.parent_),
- index_within_parent_(other.index_within_parent_)
- {
- for (int i = 0, end = (int)children_.size(); i < end; ++i) {
- if (other.children_[i]) {
- children_[i].reset(
- new trie_node_t(*other.children_[i]));
- }
- }
- }
- trie_node_t(trie_node_t && other) : parent_(nullptr)
- {
- swap(other);
- }
- trie_node_t & operator=(trie_node_t const & rhs)
- {
- BOOST_PARSER_DEBUG_ASSERT(
- parent_ == nullptr &&
- "Assignment of trie_node_ts are defined only for the "
- "header node.");
- trie_node_t temp(rhs);
- temp.swap(*this);
- return *this;
- }
- trie_node_t & operator=(trie_node_t && rhs)
- {
- BOOST_PARSER_DEBUG_ASSERT(
- parent_ == nullptr &&
- "Move assignments of trie_node_ts are defined only for the "
- "header node.");
- trie_node_t temp(std::move(rhs));
- temp.swap(*this);
- return *this;
- }
- auto max_size() const { return KeySize; }
- std::optional<Value> const & value() const { return value_; }
- Value & child_value(std::size_t i) const
- {
- return *children_[i]->value_;
- }
- trie_node_t * parent() const { return parent_; }
- bool empty() const { return children_.size() == 0; }
- const_iterator end() const { return children_.end(); }
- std::size_t index_within_parent() const
- {
- return index_within_parent_.value();
- }
- bool before_child_subtree(key_element const & e) const
- {
- return e < key_element(0);
- }
- template<typename Compare>
- const_iterator
- lower_bound(key_element const & e, Compare const &) const
- {
- return children_.empty() ? children_.end() : children_.begin() + e;
- }
- template<typename Compare>
- const_iterator
- find(key_element const & e, Compare const & comp) const
- {
- auto const it = lower_bound(e, comp);
- if (children_.empty() || !*it)
- return children_.end();
- return it;
- }
- template<typename Compare>
- trie_node_t const *
- child(key_element const & e, Compare const &) const
- {
- return children_[e].get();
- }
- trie_node_t const * child(std::size_t i) const
- {
- return children_[i].get();
- }
- key_element const & key(std::size_t i) const
- {
- BOOST_PARSER_DEBUG_ASSERT(key_element(i) == i);
- return key_element(i);
- }
- template<typename OutIter>
- OutIter copy_next_key_elements(OutIter out) const
- {
- for (key_element i = 0, end = (key_element)children_.size();
- i < end;
- ++i) {
- if (children_[i]) {
- *out = i;
- ++out;
- }
- }
- return out;
- }
- void swap(trie_node_t & other)
- {
- BOOST_PARSER_DEBUG_ASSERT(
- parent_ == nullptr &&
- "Swaps of trie_node_ts are defined only for the header "
- "node.");
- children_.swap(other.children_);
- value_.swap(other.value_);
- for (auto const & node : children_) {
- if (node)
- node->parent_ = this;
- }
- for (auto const & node : other.children_) {
- if (node)
- node->parent_ = &other;
- }
- std::swap(index_within_parent_, other.index_within_parent_);
- }
- std::optional<Value> & value() { return value_; }
- template<typename Compare>
- iterator insert(
- key_element const & e,
- Compare const & comp,
- std::unique_ptr<trie_node_t> && child)
- {
- if (children_.empty())
- children_.resize(max_size());
- auto child_it = children_.begin() + e;
- index_within_parent_.insert_at(
- child, e, child_it, children_.end());
- children_[e] = std::move(child);
- return child_it;
- }
- iterator insert(std::unique_ptr<trie_node_t> && child)
- {
- if (children_.empty())
- children_.resize(max_size());
- index_within_parent_.insert_ptr(child);
- return children_.insert(children_.begin(), std::move(child));
- }
- void erase(std::size_t i)
- {
- auto it = children_.begin() + i;
- it->reset(nullptr);
- index_within_parent_.erase(it, children_.end());
- }
- void erase(trie_node_t const * child)
- {
- auto const it = std::find_if(
- children_.begin(),
- children_.end(),
- [child](std::unique_ptr<trie_node_t> const & ptr) {
- return child == ptr.get();
- });
- BOOST_PARSER_DEBUG_ASSERT(it != children_.end());
- erase(it - children_.begin());
- }
- template<typename Compare>
- iterator
- lower_bound(key_element const & e, Compare const &)
- {
- return children_.begin() + e;
- }
- template<typename Compare>
- iterator find(key_element const & e, Compare const & comp)
- {
- if (children_.empty())
- return children_.end();
- auto const it = lower_bound(e, comp);
- if (!*it)
- return children_.end();
- return it;
- }
- template<typename Compare>
- trie_node_t * child(key_element const & e, Compare const &)
- {
- return children_[e].get();
- }
- trie_node_t * child(std::size_t i)
- {
- return children_[i].get();
- }
- friend bool
- operator==(trie_node_t const & lhs, trie_node_t const & rhs)
- {
- if (lhs.value_ != rhs.value_)
- return false;
- return std::equal(
- lhs.children_.begin(),
- lhs.children_.end(),
- rhs.children_.begin(),
- rhs.children_.end(),
- [](auto const & l_ptr, auto const & r_ptr) {
- if (!l_ptr && !r_ptr)
- return true;
- if (!l_ptr || !r_ptr)
- return false;
- return *l_ptr == *r_ptr;
- });
- }
- friend bool
- operator!=(trie_node_t const & lhs, trie_node_t const & rhs)
- {
- return !(lhs == rhs);
- }
- private:
- trie_node_t const * const_this()
- {
- return const_cast<trie_node_t const *>(this);
- }
- key_element const & key(const_iterator it) const
- {
- return key_element(it - children_.begin());
- }
- children_t children_;
- std::optional<Value> value_;
- trie_node_t * parent_;
- ParentIndexing index_within_parent_;
- friend struct index_within_parent_t;
- };
- template<typename ParentIndexing, typename Key, typename Value>
- struct trie_node_t<ParentIndexing, Key, Value, 0>
- {
- using children_t = std::vector<std::unique_ptr<trie_node_t>>;
- using iterator = typename children_t::iterator;
- using const_iterator = typename children_t::const_iterator;
- using key_element = typename Key::value_type;
- using keys_t = std::vector<key_element>;
- using key_iterator = typename keys_t::const_iterator;
- trie_node_t() : parent_(nullptr) {}
- trie_node_t(trie_node_t * parent) : parent_(parent) {}
- trie_node_t(trie_node_t const & other) :
- keys_(other.keys_),
- value_(other.value_),
- parent_(other.parent_),
- index_within_parent_(other.index_within_parent_)
- {
- children_.reserve(other.children_.size());
- for (auto const & node : other.children_) {
- std::unique_ptr<trie_node_t> new_node(
- new trie_node_t(*node));
- children_.push_back(std::move(new_node));
- }
- }
- trie_node_t(trie_node_t && other) : parent_(nullptr)
- {
- swap(other);
- }
- trie_node_t & operator=(trie_node_t const & rhs)
- {
- BOOST_PARSER_DEBUG_ASSERT(
- parent_ == nullptr &&
- "Assignment of trie_node_ts are defined only for the "
- "header node.");
- trie_node_t temp(rhs);
- temp.swap(*this);
- return *this;
- }
- trie_node_t & operator=(trie_node_t && rhs)
- {
- BOOST_PARSER_DEBUG_ASSERT(
- parent_ == nullptr &&
- "Move assignments of trie_node_ts are defined only for the "
- "header node.");
- trie_node_t temp(std::move(rhs));
- temp.swap(*this);
- return *this;
- }
- std::optional<Value> const & value() const { return value_; }
- Value & child_value(std::size_t i) const
- {
- return *children_[i]->value_;
- }
- trie_node_t * parent() const { return parent_; }
- trie_node_t * min_child() const
- {
- return children_.front().get();
- }
- trie_node_t * max_child() const
- {
- return children_.back().get();
- }
- bool empty() const { return children_.size() == 0; }
- std::size_t size() const { return children_.size(); }
- bool min_value() const
- {
- return !!children_.front()->value_;
- }
- bool max_value() const
- {
- return !!children_.back()->value_;
- }
- const_iterator begin() const { return children_.begin(); }
- const_iterator end() const { return children_.end(); }
- key_iterator key_begin() const { return keys_.begin(); }
- key_iterator key_end() const { return keys_.end(); }
- std::size_t index_within_parent() const
- {
- return index_within_parent_.value();
- }
- bool before_child_subtree(key_element const & e) const
- {
- return keys_.empty() || e < keys_.front();
- }
- template<typename Compare>
- const_iterator
- lower_bound(key_element const & e, Compare const & comp) const
- {
- auto const it =
- std::lower_bound(keys_.begin(), keys_.end(), e, comp);
- return children_.begin() + (it - keys_.begin());
- }
- template<typename Compare>
- const_iterator
- find(key_element const & e, Compare const & comp) const
- {
- auto const it = lower_bound(e, comp);
- auto const end_ = end();
- if (it != end_ && comp(e, key(it)))
- return end_;
- return it;
- }
- template<typename Compare>
- trie_node_t const *
- child(key_element const & e, Compare const & comp) const
- {
- auto const it = find(e, comp);
- if (it == children_.end())
- return nullptr;
- return it->get();
- }
- trie_node_t const * child(std::size_t i) const
- {
- return children_[i].get();
- }
- key_element const & key(std::size_t i) const
- {
- return keys_[i];
- }
- template<typename OutIter>
- OutIter copy_next_key_elements(OutIter out) const
- {
- return std::copy(key_begin(), key_end(), out);
- }
- void swap(trie_node_t & other)
- {
- BOOST_PARSER_DEBUG_ASSERT(
- parent_ == nullptr &&
- "Swaps of trie_node_ts are defined only for the header "
- "node.");
- keys_.swap(other.keys_);
- children_.swap(other.children_);
- value_.swap(other.value_);
- for (auto const & node : children_) {
- node->parent_ = this;
- }
- for (auto const & node : other.children_) {
- node->parent_ = &other;
- }
- std::swap(index_within_parent_, other.index_within_parent_);
- }
- std::optional<Value> & value() { return value_; }
- iterator begin() { return children_.begin(); }
- iterator end() { return children_.end(); }
- template<typename Compare>
- iterator insert(
- key_element const & e,
- Compare const & comp,
- std::unique_ptr<trie_node_t> && child)
- {
- BOOST_PARSER_DEBUG_ASSERT(child->empty());
- auto it = std::lower_bound(keys_.begin(), keys_.end(), e, comp);
- it = keys_.insert(it, e);
- auto const offset = it - keys_.begin();
- auto child_it = children_.begin() + offset;
- index_within_parent_.insert_at(
- child, offset, child_it, children_.end());
- return children_.insert(child_it, std::move(child));
- }
- iterator insert(std::unique_ptr<trie_node_t> && child)
- {
- BOOST_PARSER_DEBUG_ASSERT(empty());
- index_within_parent_.insert_ptr(child);
- return children_.insert(children_.begin(), std::move(child));
- }
- void erase(std::size_t i)
- {
- // This empty-keys situation happens only in the header node.
- if (!keys_.empty())
- keys_.erase(keys_.begin() + i);
- auto it = children_.erase(children_.begin() + i);
- index_within_parent_.erase(it, children_.end());
- }
- void erase(trie_node_t const * child)
- {
- auto const it = std::find_if(
- children_.begin(),
- children_.end(),
- [child](std::unique_ptr<trie_node_t> const & ptr) {
- return child == ptr.get();
- });
- BOOST_PARSER_DEBUG_ASSERT(it != children_.end());
- erase(it - children_.begin());
- }
- template<typename Compare>
- iterator
- lower_bound(key_element const & e, Compare const & comp)
- {
- auto const it = const_this()->lower_bound(e, comp);
- return children_.begin() +
- (it - const_iterator(children_.begin()));
- }
- template<typename Compare>
- iterator find(key_element const & e, Compare const & comp)
- {
- auto const it = const_this()->find(e, comp);
- return children_.begin() +
- (it - const_iterator(children_.begin()));
- }
- template<typename Compare>
- trie_node_t *
- child(key_element const & e, Compare const & comp)
- {
- return const_cast<trie_node_t *>(const_this()->child(e, comp));
- }
- trie_node_t * child(std::size_t i)
- {
- return const_cast<trie_node_t *>(const_this()->child(i));
- }
- friend bool
- operator==(trie_node_t const & lhs, trie_node_t const & rhs)
- {
- if (lhs.keys_ != rhs.keys_ || lhs.value_ != rhs.value_)
- return false;
- return std::equal(
- lhs.children_.begin(),
- lhs.children_.end(),
- rhs.children_.begin(),
- rhs.children_.end(),
- [](auto const & l_ptr, auto const & r_ptr) {
- if (!l_ptr && !r_ptr)
- return true;
- if (!l_ptr || !r_ptr)
- return false;
- return *l_ptr == *r_ptr;
- });
- }
- friend bool
- operator!=(trie_node_t const & lhs, trie_node_t const & rhs)
- {
- return !(lhs == rhs);
- }
- private:
- trie_node_t const * const_this()
- {
- return const_cast<trie_node_t const *>(this);
- }
- key_element const & key(const_iterator it) const
- {
- return keys_[it - children_.begin()];
- }
- keys_t keys_;
- children_t children_;
- std::optional<Value> value_;
- trie_node_t * parent_;
- ParentIndexing index_within_parent_;
- friend struct index_within_parent_t;
- };
- }
- }}
- #endif
|