69#ifndef __ADS_BinaryTrie
70 #define __ADS_BinaryTrie
102template<
class U ,
int W >
103concept TrieWidth = std::unsigned_integral< U > && ( W >= 1 ) &&
104 ( W <= std::numeric_limits< U >::digits );
111template<
class U ,
int W >
112[[nodiscard]]
constexpr U triePrefix( U x ,
int i )
114 return( i == 0 ? U( 0 ) : U( x >> ( W - i ) ) );
119template<
class U ,
int W >
120[[nodiscard]]
constexpr int trieBit( U x ,
int i )
122 return(
int( ( x >> ( W - 1 - i ) ) & 1u ) );
126template<
class U ,
int W >
127[[nodiscard]]
constexpr bool trieKey( U x )
129 if constexpr( W == std::numeric_limits< U >::digits )
132 return( ( x >> W ) == 0 );
137template<
class U ,
class N ,
int W ,
bool Hashed >
143template<
class U ,
class N ,
int W >
144struct TrieTables< U , N , W , true >
146 std::array< FlatHashTable< U , N * > , W + 1 > t;
157template< std::
unsigned_
integral U ,
class V ,
int W ,
bool Hashed >
158 requires TrieWidth< U , W >
172 JumpTrie( JumpTrie && o );
175 JumpTrie & operator=( JumpTrie && o )
noexcept;
177 JumpTrie(
const JumpTrie & ) =
delete;
178 JumpTrie & operator=(
const JumpTrie & ) =
delete;
206 [[nodiscard]]
const U * lowerBound( U x )
const;
212 [[nodiscard]]
const U * find( U x )
const;
216 [[nodiscard]] std::optional< V > lookup( U x )
const;
220 [[nodiscard]]
bool contains( U x )
const;
247 bool insert( U x ,
const V & v = V() );
277 [[nodiscard]] std::size_t size(
void )
const {
return( f_n ); }
280 [[nodiscard]]
bool empty(
void )
const {
return( f_n == 0 ); }
283 [[nodiscard]] std::size_t nodes(
void )
const {
return( f_nodes ); }
286 [[nodiscard]] std::optional< U > min(
void )
const;
289 [[nodiscard]] std::optional< U > max(
void )
const;
300 Node * child[ 2 ] = { nullptr ,
nullptr };
302 Node * jump =
nullptr;
303 Node * parent =
nullptr;
319 using iterator_category = std::bidirectional_iterator_tag;
321 using value_type = std::pair< U , V >;
325 using mapped_type = V;
327 using difference_type = std::ptrdiff_t;
331 using pointer = void;
334 const_iterator(
void ) =
default;
337 explicit const_iterator(
const Node * l ) : f_l( l ) { }
340 reference operator*(
void )
const {
341 return( reference( f_l->key , f_l->value ) );
345 const_iterator & operator++(
void ) {
346 f_l = f_l->child[ 1 ];
351 const_iterator operator++(
int ) {
358 const_iterator & operator--(
void ) {
359 f_l = f_l->child[ 0 ];
364 const_iterator operator--(
int ) {
371 bool operator==(
const const_iterator & o )
const =
default;
374 const Node * f_l =
nullptr;
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;
391 [[nodiscard]] const_iterator begin(
void )
const {
392 return( const_iterator( f_dummy->child[ 1 ] ) );
395 [[nodiscard]] const_iterator end(
void )
const {
396 return( const_iterator( f_dummy ) );
399 [[nodiscard]] const_iterator cbegin(
void )
const {
return( begin() ); }
401 [[nodiscard]] const_iterator cend(
void )
const {
return( end() ); }
405 [[nodiscard]] const_iterator seek( U x )
const;
414 [[nodiscard]] std::pair< Node * , int >
415 deepest( U x ,
int lo = 0 ,
int hi = W + 1 , Node * u =
nullptr )
const;
419 [[nodiscard]] Node * successor( U x , Node * u ,
int i )
const;
423 [[nodiscard]] Node * node( U p ,
int i )
const;
428 [[nodiscard]] Node * leftmost( Node * u ,
int i )
const;
433 [[nodiscard]]
bool consistent( U x )
const;
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;
443 void destroy( Node * u ,
int i );
445 ObjectPool< Node > f_pool{ 1 << 16 };
446 Node * f_root =
nullptr;
447 Node * f_dummy =
nullptr;
449 std::size_t f_nodes = 1;
462template< std::unsigned_integral U ,
class V = std::monostate ,
463 int W = std::numeric_limits< U >::digits >
465class BinaryTrie :
public detail::JumpTrie< U , V , W , false >
468 using detail::JumpTrie< U , V , W ,
false >::JumpTrie;
477template< std::unsigned_integral U ,
class V = std::monostate ,
478 int W = std::numeric_limits< U >::digits >
532 [[nodiscard]]
const U *
find( U x )
const;
536 [[nodiscard]] std::optional< V >
lookup( U x )
const;
555 [[nodiscard]] std::size_t
size(
void )
const {
return( f_n ); }
558 [[nodiscard]]
bool empty(
void )
const {
return( f_n == 0 ); }
561 [[nodiscard]] std::size_t
nodes(
void )
const {
return( f_nodes ); }
572 Node * child[ 2 ] = { nullptr ,
nullptr };
583 [[nodiscard]]
const Leaf * leaf( U x )
const;
586 static void destroy( Node * u ,
int i );
590 static void visit(
const Node * u ,
int i , F & f );
594 std::size_t f_nodes = 1;
606#include "ADS/dictionary/impl/BinaryTrie.tpp"
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