ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryTrie.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BinaryTrie.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
65/*--------------------------------------------------------------------------*/
66/*----------------------------- DEFINITIONS --------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69#ifndef __ADS_BinaryTrie
70 #define __ADS_BinaryTrie
71/* self-identification: #endif constant */
72
73/*--------------------------------------------------------------------------*/
74/*------------------------------ INCLUDES ----------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77#include "ADS/Contract.h"
78#include "ADS/Iterators.h"
79#include "ADS/Portable.h"
82
83#include <array>
84#include <concepts>
85#include <cstddef>
86#include <iterator>
87#include <limits>
88#include <optional>
89#include <type_traits>
90#include <utility>
91#include <variant>
92
93/*--------------------------------------------------------------------------*/
94/*--------------------------- NAMESPACE ------------------------------------*/
95/*--------------------------------------------------------------------------*/
96
97namespace ADS
98{
99
102template< class U , int W >
103concept TrieWidth = std::unsigned_integral< U > && ( W >= 1 ) &&
104 ( W <= std::numeric_limits< U >::digits );
105
106namespace detail
107{
108
111template< class U , int W >
112[[nodiscard]] constexpr U triePrefix( U x , int i )
113{
114 return( i == 0 ? U( 0 ) : U( x >> ( W - i ) ) );
115 }
116
119template< class U , int W >
120[[nodiscard]] constexpr int trieBit( U x , int i )
121{
122 return( int( ( x >> ( W - 1 - i ) ) & 1u ) );
123 }
124
126template< class U , int W >
127[[nodiscard]] constexpr bool trieKey( U x )
128{
129 if constexpr( W == std::numeric_limits< U >::digits )
130 return( true );
131 else
132 return( ( x >> W ) == 0 );
133 }
134
137template< class U , class N , int W , bool Hashed >
138struct TrieTables
139{ };
140
143template< class U , class N , int W >
144struct TrieTables< U , N , W , true >
145{
146 std::array< FlatHashTable< U , N * > , W + 1 > t;
147 };
148
149/*--------------------------------------------------------------------------*/
150/*-------------------------- CLASS JumpTrie --------------------------------*/
151/*--------------------------------------------------------------------------*/
152
157template< std::unsigned_integral U , class V , int W , bool Hashed >
158 requires TrieWidth< U , W >
159class JumpTrie
160{
161
162/*--------------------------------------------------------------------------*/
163 public:
164 using Key = U;
165 using Item = U;
166 using Value = V;
167
169 JumpTrie( void );
170
172 JumpTrie( JumpTrie && o );
173
175 JumpTrie & operator=( JumpTrie && o ) noexcept;
176
177 JumpTrie( const JumpTrie & ) = delete;
178 JumpTrie & operator=( const JumpTrie & ) = delete;
179
181 ~JumpTrie();
182
183/*--------------------------------------------------------------------------*/
184
206 [[nodiscard]] const U * lowerBound( U x ) const;
207
212 [[nodiscard]] const U * find( U x ) const;
213
216 [[nodiscard]] std::optional< V > lookup( U x ) const;
217
220 [[nodiscard]] bool contains( U x ) const;
221
247 bool insert( U x , const V & v = V() );
248
274 bool remove( U x );
275
277 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
278
280 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
281
283 [[nodiscard]] std::size_t nodes( void ) const { return( f_nodes ); }
284
286 [[nodiscard]] std::optional< U > min( void ) const;
287
289 [[nodiscard]] std::optional< U > max( void ) const;
290
292 void clear( void );
293
294/*--------------------------------------------------------------------------*/
295 protected:
298 struct Node
299 {
300 Node * child[ 2 ] = { nullptr , nullptr };
302 Node * jump = nullptr;
303 Node * parent = nullptr;
304 U key = 0;
305 V value{};
306 };
307
308 public:
309/*------------------------- THE STANDARD INTERFACE -------------------------*/
310
311 // a constant view of the pairs ( key , value ) by increasing key, along
312 // the list of the leaves
313
315 class const_iterator
316 {
317 public:
319 using iterator_category = std::bidirectional_iterator_tag;
321 using value_type = std::pair< U , V >;
323 using key_type = U;
325 using mapped_type = V;
327 using difference_type = std::ptrdiff_t;
329 using reference = PairRef< const U & , const V & >;
331 using pointer = void;
332
334 const_iterator( void ) = default;
335
337 explicit const_iterator( const Node * l ) : f_l( l ) { }
338
340 reference operator*( void ) const {
341 return( reference( f_l->key , f_l->value ) );
342 }
343
345 const_iterator & operator++( void ) {
346 f_l = f_l->child[ 1 ];
347 return( *this );
348 }
349
351 const_iterator operator++( int ) {
352 auto t = *this;
353 ++*this;
354 return( t );
355 }
356
358 const_iterator & operator--( void ) {
359 f_l = f_l->child[ 0 ];
360 return( *this );
361 }
362
364 const_iterator operator--( int ) {
365 auto t = *this;
366 --*this;
367 return( t );
368 }
369
371 bool operator==( const const_iterator & o ) const = default;
372
373 private:
374 const Node * f_l = nullptr;
375 };
376
378 using value_type = typename const_iterator::value_type;
380 using size_type = std::size_t;
382 using difference_type = std::ptrdiff_t;
384 using reference = typename const_iterator::reference;
386 using const_reference = reference;
388 using iterator = const_iterator;
389
391 [[nodiscard]] const_iterator begin( void ) const {
392 return( const_iterator( f_dummy->child[ 1 ] ) );
393 }
395 [[nodiscard]] const_iterator end( void ) const {
396 return( const_iterator( f_dummy ) );
397 }
399 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
401 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
402
405 [[nodiscard]] const_iterator seek( U x ) const;
406
407/*--------------------------------------------------------------------------*/
408 protected:
414 [[nodiscard]] std::pair< Node * , int >
415 deepest( U x , int lo = 0 , int hi = W + 1 , Node * u = nullptr ) const;
416
419 [[nodiscard]] Node * successor( U x , Node * u , int i ) const;
420
423 [[nodiscard]] Node * node( U p , int i ) const;
424
428 [[nodiscard]] Node * leftmost( Node * u , int i ) const;
429
433 [[nodiscard]] bool consistent( U x ) const;
434
438 [[nodiscard]] bool subtree( const Node * u , int i , U p , const Node *& lo ,
439 const Node *& hi , std::size_t & n ,
440 std::size_t & m ) const;
441
443 void destroy( Node * u , int i );
444
445 ObjectPool< Node > f_pool{ 1 << 16 };
446 Node * f_root = nullptr;
447 Node * f_dummy = nullptr;
448 std::size_t f_n = 0;
449 std::size_t f_nodes = 1;
450 ADS_NO_UNIQUE_ADDRESS TrieTables< U , Node , W , Hashed > f_t;
451
452 }; // end( class JumpTrie )
453
454} // end( namespace detail )
455
456/*--------------------------------------------------------------------------*/
457/*-------------------------- CLASS BinaryTrie ------------------------------*/
458/*--------------------------------------------------------------------------*/
459
462template< std::unsigned_integral U , class V = std::monostate ,
463 int W = std::numeric_limits< U >::digits >
464 requires TrieWidth< U , W >
465class BinaryTrie : public detail::JumpTrie< U , V , W , false >
466{
467 public:
468 using detail::JumpTrie< U , V , W , false >::JumpTrie;
469 };
470
471/*--------------------------------------------------------------------------*/
472/*------------------------ CLASS SimpleBinaryTrie --------------------------*/
473/*--------------------------------------------------------------------------*/
474
477template< std::unsigned_integral U , class V = std::monostate ,
478 int W = std::numeric_limits< U >::digits >
479 requires TrieWidth< U , W >
481{
482
483/*--------------------------------------------------------------------------*/
484 public:
485 using Key = U;
486 using Item = U;
487 using Value = V;
488
490 SimpleBinaryTrie( void ) = default;
491
494
497
500
503
528 [[nodiscard]] const U * lowerBound( U x ) const;
529
532 [[nodiscard]] const U * find( U x ) const;
533
536 [[nodiscard]] std::optional< V > lookup( U x ) const;
537
540 [[nodiscard]] bool contains( U x ) const;
541
546 bool insert( U x , const V & v = V() );
547
552 bool remove( U x );
553
555 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
556
558 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
559
561 [[nodiscard]] std::size_t nodes( void ) const { return( f_nodes ); }
562
564 template< class F >
565 void forEach( F && f ) const;
566
567/*--------------------------------------------------------------------------*/
568 private:
570 struct Node
571 {
572 Node * child[ 2 ] = { nullptr , nullptr };
573 };
574
576 struct Leaf : Node
577 {
578 U key = 0;
579 V value{};
580 };
581
583 [[nodiscard]] const Leaf * leaf( U x ) const;
584
586 static void destroy( Node * u , int i );
587
589 template< class F >
590 static void visit( const Node * u , int i , F & f );
591
592 Node f_root;
593 std::size_t f_n = 0;
594 std::size_t f_nodes = 1;
595
596 }; // end( class SimpleBinaryTrie )
597
598/*--------------------------------------------------------------------------*/
599
600} // end( namespace ADS )
601
602/*--------------------------------------------------------------------------*/
603/*-------------------------- IMPLEMENTATION --------------------------------*/
604/*--------------------------------------------------------------------------*/
605
606#include "ADS/dictionary/impl/BinaryTrie.tpp"
607
608/*--------------------------------------------------------------------------*/
609
610#endif /* BinaryTrie.h included */
611
612/*--------------------------------------------------------------------------*/
613/*------------------------- End File BinaryTrie.h --------------------------*/
614/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A hash table with open addressing and control bytes, beyond the book: the engineered DICTIONARY of AD...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and fre...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
a binary trie of the integers of bits with the list of the leaves and the jump pointers,...
Definition BinaryTrie.h:466
bool remove(U x)
removes ; true if it was there, by removing the nodes of its path left with no child,...
SimpleBinaryTrie & operator=(const SimpleBinaryTrie &)=delete
bool insert(U x, const V &v=V())
associates to ; true if was not there, by adding the missing nodes of its path, time
SimpleBinaryTrie(const SimpleBinaryTrie &)=delete
std::size_t size(void) const
the number of keys
Definition BinaryTrie.h:555
const U * find(U x) const
the key , nil if it is not there, in time
std::size_t nodes(void) const
the number of nodes, the root and the leaves included
Definition BinaryTrie.h:561
~SimpleBinaryTrie()
frees the nodes
SimpleBinaryTrie & operator=(SimpleBinaryTrie &&o) noexcept
the trie of o, which is left empty; the nodes of this one are freed
SimpleBinaryTrie(SimpleBinaryTrie &&o) noexcept
the trie of o, which is left empty
bool empty(void) const
true if there are no keys
Definition BinaryTrie.h:558
V Value
the type of the values
Definition BinaryTrie.h:487
void forEach(F &&f) const
f( x , v ) on the pairs by increasing keys
std::optional< V > lookup(U x) const
the value of , or nothing
const U * lowerBound(U x) const
the smallest key , nil if none (the find( x ) of the SSet of Morin's book), in time
U Item
the items of the SSet, the keys
Definition BinaryTrie.h:486
SimpleBinaryTrie(void)=default
the empty trie
U Key
the type of the keys
Definition BinaryTrie.h:485
bool contains(U x) const
true if is there
the number of bits of the keys of a trie on U: , if it is between 1 and the bits of U
Definition BinaryTrie.h:103
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
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96