ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Trie.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Trie.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
44/*--------------------------------------------------------------------------*/
45/*----------------------------- DEFINITIONS --------------------------------*/
46/*--------------------------------------------------------------------------*/
47
48#ifndef __ADS_Trie
49 #define __ADS_Trie
50/* self-identification: #endif constant */
51
52/*--------------------------------------------------------------------------*/
53/*------------------------------ INCLUDES ----------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56#include "ADS/Contract.h"
57#include "ADS/Iterators.h"
58
59#include <cstddef>
60#include <iterator>
61#include <map>
62#include <memory>
63#include <optional>
64#include <string>
65#include <string_view>
66#include <utility>
67#include <vector>
68
69/*--------------------------------------------------------------------------*/
70/*--------------------------- NAMESPACE ------------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73namespace ADS
74{
75
76namespace detail
77{
78
79/*--------------------------------------------------------------------------*/
80/*-------------------------- CLASS TrieIterator ----------------------------*/
81/*--------------------------------------------------------------------------*/
82
88template< class N , class V , bool Labeled >
89class TrieIterator
90{
91 public:
93 using reference = PairRef< std::string , const V & >;
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;
102
104 TrieIterator( void ) = default;
105
107 explicit TrieIterator( const N * root ) {
108 if( ! root )
109 return;
110 f_S.push_back( Frame{ root , root->next.begin() , 0 } );
111 if( ! root->value )
112 advance();
113 }
114
116 reference operator*( void ) const {
117 return( reference( f_key , *f_S.back().node->value ) );
118 }
119
121 TrieIterator & operator++( void ) {
122 advance();
123 return( *this );
124 }
126 TrieIterator operator++( int ) {
127 auto t = *this;
128 ++*this;
129 return( t );
130 }
131
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 ) ) );
137 }
138
139 private:
140 struct Frame
141 {
142 const N * node;
143 typename decltype( N::next )::const_iterator child;
144 std::size_t length;
145 };
146
148 void advance( void ) {
149 while( ! f_S.empty() ) {
150 Frame & f = f_S.back();
151 if( f.child == f.node->next.end() ) { // its subtree is done
152 f_key.resize( f.length );
153 f_S.pop_back();
154 continue;
155 }
156 const std::size_t len = f_key.size();
157 const auto & [ c , x ] = *f.child++;
158 if constexpr( Labeled )
159 f_key += x->label;
160 else
161 f_key += c;
162 f_S.push_back( Frame{ x.get() , x->next.begin() , len } );
163 if( x->value )
164 return;
165 }
166 }
167
168 std::vector< Frame > f_S;
169 std::string f_key;
170
171 }; // end( class TrieIterator )
172
173} // end( namespace detail )
174
175/*--------------------------------------------------------------------------*/
176/*------------------------------ CLASS Trie --------------------------------*/
177/*--------------------------------------------------------------------------*/
178
180template< class V >
181class Trie
182{
183
184/*--------------------------------------------------------------------------*/
185 public:
186 using Key = std::string;
187 using Value = V;
188
204 [[nodiscard]] std::optional< V > lookup( std::string_view k ) const;
205
209 void insert( std::string_view k , const V & v );
210
214 void remove( std::string_view k );
215
217 [[nodiscard]] bool contains( std::string_view k ) const;
218
220 [[nodiscard]] std::size_t size( void ) const;
221
224 [[nodiscard]] std::vector< std::string >
225 keysWithPrefix( std::string_view p ) const;
226
228 [[nodiscard]] std::size_t nodes( void ) const;
229
230/*--------------------------------------------------------------------------*/
231 private:
232 struct Node
233 {
235 std::map< char , std::unique_ptr< Node > > next;
236 std::optional< V > value;
237 };
238
239 public:
240/*------------------------- THE STANDARD INTERFACE -------------------------*/
241
242 // a constant view of the pairs ( key , value ) in lexicographic order,
243 // forward; the key is rebuilt along the path
244
246 using value_type = std::pair< std::string , V >;
248 using key_type = std::string;
250 using mapped_type = V;
252 using size_type = std::size_t;
254 using difference_type = std::ptrdiff_t;
260 using const_iterator = detail::TrieIterator< Node , V , false >;
263
265 [[nodiscard]] const_iterator begin( void ) const {
266 return( const_iterator( &f_root ) );
267 }
268
269 [[nodiscard]] const_iterator end( void ) const {
270 return( const_iterator() );
271 }
272
273 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
275 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
276
277 private:
279 [[nodiscard]] const Node * find( std::string_view p ) const;
280
283 bool remove( Node * x , std::string_view k , std::size_t d );
284
285 static void collect( const Node * x , std::string & s ,
286 std::vector< std::string > & out );
287
288 static std::size_t count( const Node * x );
289
290 Node f_root;
291 std::size_t f_size = 0;
292
293 }; // end( class Trie )
294
295/*--------------------------------------------------------------------------*/
296/*---------------------------- CLASS RadixTree -----------------------------*/
297/*--------------------------------------------------------------------------*/
298
300template< class V >
302{
303
304/*--------------------------------------------------------------------------*/
305 public:
306 using Key = std::string;
307 using Value = V;
308
311 [[nodiscard]] std::optional< V > lookup( std::string_view k ) const;
312
338 void insert( std::string_view k , const V & v );
339
343 void remove( std::string_view k );
344
346 [[nodiscard]] bool contains( std::string_view k ) const;
347
349 [[nodiscard]] std::size_t size( void ) const;
350
352 [[nodiscard]] std::vector< std::string >
353 keysWithPrefix( std::string_view p ) const;
354
356 [[nodiscard]] std::size_t nodes( void ) const;
357
358/*--------------------------------------------------------------------------*/
359 private:
360 struct Node
361 {
362 std::string label;
364 std::map< char , std::unique_ptr< Node > > next;
365 std::optional< V > value;
366 };
367
368 public:
369/*------------------------- THE STANDARD INTERFACE -------------------------*/
370
371 // a constant view of the pairs ( key , value ) in lexicographic order,
372 // forward; the key is rebuilt along the path
373
375 using value_type = std::pair< std::string , V >;
377 using key_type = std::string;
379 using mapped_type = V;
381 using size_type = std::size_t;
383 using difference_type = std::ptrdiff_t;
389 using const_iterator = detail::TrieIterator< Node , V , true >;
392
394 [[nodiscard]] const_iterator begin( void ) const {
395 return( const_iterator( &f_root ) );
396 }
397
398 [[nodiscard]] const_iterator end( void ) const {
399 return( const_iterator() );
400 }
401
402 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
404 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
405
406 private:
409 bool remove( Node * x , std::string_view k );
410
412 static void compress( Node * x );
413
414 static void collect( const Node * x , std::string & s ,
415 std::vector< std::string > & out );
416
417 static std::size_t count( const Node * x );
418
420 static bool compact( const Node * x , bool root );
421
422 Node f_root;
423 std::size_t f_size = 0;
424
425 }; // end( class RadixTree )
426
427/*--------------------------------------------------------------------------*/
428
429} // end( namespace ADS )
430
431/*--------------------------------------------------------------------------*/
432/*-------------------------- IMPLEMENTATION --------------------------------*/
433/*--------------------------------------------------------------------------*/
434
435#include "ADS/dictionary/impl/Trie.tpp"
436
437/*--------------------------------------------------------------------------*/
438
439#endif /* Trie.h included */
440
441/*--------------------------------------------------------------------------*/
442/*---------------------------- End File Trie.h -----------------------------*/
443/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96