88template<
class N ,
class V ,
bool Labeled >
95 using value_type = std::pair< std::string , V >;
97 using difference_type = std::ptrdiff_t;
99 using iterator_concept = std::forward_iterator_tag;
101 using iterator_category = std::input_iterator_tag;
104 TrieIterator(
void ) =
default;
107 explicit TrieIterator(
const N * root ) {
110 f_S.push_back( Frame{ root , root->next.begin() , 0 } );
116 reference operator*(
void )
const {
117 return( reference( f_key , *f_S.back().node->value ) );
121 TrieIterator & operator++(
void ) {
126 TrieIterator operator++(
int ) {
133 bool operator==(
const TrieIterator & o )
const {
134 return( f_S.empty() ? o.f_S.empty()
135 : ( ! o.f_S.empty() &&
136 ( f_S.back().node == o.f_S.back().node ) ) );
143 typename decltype( N::next )::const_iterator child;
148 void advance(
void ) {
149 while( ! f_S.empty() ) {
150 Frame & f = f_S.back();
151 if( f.child == f.node->next.end() ) {
152 f_key.resize( f.length );
156 const std::size_t len = f_key.size();
157 const auto & [ c , x ] = *f.child++;
158 if constexpr( Labeled )
162 f_S.push_back( Frame{ x.get() , x->next.begin() , len } );
168 std::vector< Frame > f_S;
204 [[nodiscard]] std::optional< V >
lookup( std::string_view k )
const;
209 void insert( std::string_view k ,
const V & v );
217 [[nodiscard]]
bool contains( std::string_view k )
const;
220 [[nodiscard]] std::size_t
size(
void )
const;
224 [[nodiscard]] std::vector< std::string >
228 [[nodiscard]] std::size_t
nodes(
void )
const;
235 std::map< char , std::unique_ptr< Node > > next;
236 std::optional< V > value;
279 [[nodiscard]]
const Node * find( std::string_view p )
const;
283 bool remove( Node * x , std::string_view k , std::size_t d );
285 static void collect(
const Node * x , std::string & s ,
286 std::vector< std::string > & out );
288 static std::size_t count(
const Node * x );
291 std::size_t f_size = 0;
311 [[nodiscard]] std::optional< V >
lookup( std::string_view k )
const;
338 void insert( std::string_view k ,
const V & v );
346 [[nodiscard]]
bool contains( std::string_view k )
const;
349 [[nodiscard]] std::size_t
size(
void )
const;
352 [[nodiscard]] std::vector< std::string >
356 [[nodiscard]] std::size_t
nodes(
void )
const;
364 std::map< char , std::unique_ptr< Node > > next;
365 std::optional< V > value;
409 bool remove( Node * x , std::string_view k );
412 static void compress( Node * x );
414 static void collect(
const Node * x , std::string & s ,
415 std::vector< std::string > & out );
417 static std::size_t count(
const Node * x );
420 static bool compact(
const Node * x ,
bool root );
423 std::size_t f_size = 0;
435#include "ADS/dictionary/impl/Trie.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
a DICTIONARY of strings on a compact trie
Definition Trie.h:302
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition Trie.h:383
V mapped_type
the type of the values
Definition Trie.h:379
std::vector< std::string > keysWithPrefix(std::string_view p) const
the keys that begin with , in increasing order
std::size_t size_type
the type of the sizes
Definition Trie.h:381
V Value
the type of the values
Definition Trie.h:307
std::string key_type
the type of the keys
Definition Trie.h:377
const_iterator begin(void) const
the iterator on the first item
Definition Trie.h:394
std::size_t size(void) const
the number of keys
std::optional< V > lookup(std::string_view k) const
the value of the key , or nothing, along the edges whose labels are pieces of
const_iterator iterator
the iterator on the items
Definition Trie.h:391
bool contains(std::string_view k) const
true if the key is there
reference const_reference
a constant reference to an item
Definition Trie.h:387
const_iterator cbegin(void) const
the constant iterator on the first item
Definition Trie.h:402
PairRef< std::string, const V & > reference
a reference to an item
Definition Trie.h:385
std::pair< std::string, V > value_type
the type of the items, for the standard library
Definition Trie.h:375
const_iterator end(void) const
the iterator past the last item
Definition Trie.h:398
void insert(std::string_view k, const V &v)
associates the value to the key
detail::TrieIterator< Node, V, true > const_iterator
the iterator on the items, constant
Definition Trie.h:389
void remove(std::string_view k)
removes the key , if it is there, merging back a node left with a single child and no value
std::string Key
the type of the keys
Definition Trie.h:306
std::size_t nodes(void) const
the number of nodes, the root included
const_iterator cend(void) const
the constant iterator past the last item
Definition Trie.h:404
a DICTIONARY of strings on a trie
Definition Trie.h:182
std::size_t size(void) const
the number of keys
reference const_reference
a constant reference to an item
Definition Trie.h:258
PairRef< std::string, const V & > reference
a reference to an item
Definition Trie.h:256
std::size_t size_type
the type of the sizes
Definition Trie.h:252
void remove(std::string_view k)
removes the key , if it is there, and the nodes of its path left with no value and no child,...
std::vector< std::string > keysWithPrefix(std::string_view p) const
the keys that begin with , in increasing order: those below the node of
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition Trie.h:254
void insert(std::string_view k, const V &v)
associates the value to the key , the path of made by the nodes missing, in time
std::string key_type
the type of the keys
Definition Trie.h:248
detail::TrieIterator< Node, V, false > const_iterator
the iterator on the items, constant
Definition Trie.h:260
std::optional< V > lookup(std::string_view k) const
the value of the key , or nothing, in time
const_iterator cbegin(void) const
the constant iterator on the first item
Definition Trie.h:273
const_iterator iterator
the iterator on the items
Definition Trie.h:262
V mapped_type
the type of the values
Definition Trie.h:250
std::size_t nodes(void) const
the number of nodes, the root included
const_iterator cend(void) const
the constant iterator past the last item
Definition Trie.h:275
bool contains(std::string_view k) const
true if the key is there
const_iterator end(void) const
the iterator past the last item
Definition Trie.h:269
std::string Key
the type of the keys
Definition Trie.h:186
V Value
the type of the values
Definition Trie.h:187
std::pair< std::string, V > value_type
the type of the items, for the standard library
Definition Trie.h:246
const_iterator begin(void) const
the iterator on the first item
Definition Trie.h:265
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648