104#ifndef __ADS_AdaptiveRadixTree
105 #define __ADS_AdaptiveRadixTree
125#include <string_view>
189 [[nodiscard]] std::optional< V >
lookup( std::string_view k )
const;
192 [[nodiscard]]
const V *
find( std::string_view k )
const;
195 [[nodiscard]] V *
find( std::string_view k );
218 bool insert( std::string_view k ,
const V & v );
226 [[nodiscard]]
bool contains( std::string_view k )
const;
229 [[nodiscard]] std::size_t
size(
void )
const;
232 [[nodiscard]]
bool empty(
void )
const;
250 [[nodiscard]] std::vector< std::string >
255 static constexpr unsigned MAXP = 8;
258 enum Type : std::uint8_t { N4 , N16 , N48 , N256 };
266 [[nodiscard]]
const char * key(
void )
const {
267 return(
reinterpret_cast< const char *
>(
this + 1 ) );
270 [[nodiscard]] std::string_view view(
void )
const {
271 return( std::string_view( key() , len ) );
279 std::uint16_t count = 0;
280 std::uint32_t plen = 0;
281 unsigned char prefix[ MAXP ];
282 Leaf * term =
nullptr;
284 explicit Node( Type t ) : type( t ) { }
290 unsigned char key[ 4 ];
293 Node4(
void ) : Node( N4 ) { }
299 alignas( 16 )
unsigned char key[ 16 ];
302 Node16(
void ) : Node( N16 ) { }
308 unsigned char index[ 256 ];
311 Node48(
void ) : Node( N48 ) { std::memset( index , 0 , 256 ); }
315 struct Node256 : Node
319 Node256(
void ) : Node( N256 ) {
320 for(
auto & c : child )
326 [[nodiscard]]
static bool isLeaf(
const void * p ) {
327 return(
reinterpret_cast< std::uintptr_t
>( p ) & 1 );
330 [[nodiscard]]
static Leaf * asLeaf(
const void * p ) {
331 return(
reinterpret_cast< Leaf *
>(
332 reinterpret_cast< std::uintptr_t
>( p ) - 1 ) );
335 [[nodiscard]]
static void * tag( Leaf * l ) {
336 return(
reinterpret_cast< void *
>(
337 reinterpret_cast< std::uintptr_t
>( l ) + 1 ) );
348 Slab(
void ) =
default;
350 Slab(
const Slab & ) =
delete;
352 Slab & operator=(
const Slab & ) =
delete;
354 ~Slab() { release(); }
357 [[nodiscard]]
void * take( std::size_t bytes );
359 void give(
void * p , std::size_t bytes )
noexcept;
361 void release(
void )
noexcept;
363 void swap( Slab & o )
noexcept;
367 static constexpr std::size_t STEP = 16 , CLASSES = 32;
374 std::vector< std::pair< char * , std::size_t > > f_blocks;
375 char * f_cur =
nullptr;
376 char * f_end =
nullptr;
377 Free * f_free[ CLASSES + 1 ] = {};
380 [[nodiscard]] Leaf * newLeaf( std::string_view k ,
const V & v );
382 void freeLeaf( Leaf * l )
noexcept;
385 [[nodiscard]] N * make(
void );
387 void freeNode( Node * n )
noexcept;
389 [[nodiscard]]
static void *
const * child(
const Node * n ,
unsigned char c );
391 [[nodiscard]]
static void ** child( Node * n ,
unsigned char c );
393 [[nodiscard]]
static const Leaf * minimum(
const void * p );
395 [[nodiscard]]
static std::uint32_t
396 mismatch(
const Node * n , std::string_view k , std::size_t d );
398 void addChild(
void ** ref , Node * n ,
unsigned char c ,
void * x );
402 void split(
void ** ref , std::size_t d , std::string_view k ,
const V & v );
404 void removeChild(
void ** ref , Node * n ,
unsigned char c ,
void ** slot );
406 void shrink(
void ** ref , Node * n );
408 [[nodiscard]]
const Leaf * search( std::string_view k )
const;
413 static void ahead(
const void * p ,
unsigned char c );
418 static void children(
const Node * n );
420 void destroy(
void * p )
noexcept;
423 static void visit(
const void * p , F & f );
426 static void each(
const Node * n , F && f );
428 [[nodiscard]]
bool valid(
void )
const;
430 [[nodiscard]]
bool valid(
const void * p , std::string &
path ,
431 std::size_t & leaves ,
bool root )
const;
433 void * f_root =
nullptr;
434 std::size_t f_size = 0;
435 ObjectPool< Node4 > f_n4;
436 ObjectPool< Node16 > f_n16;
437 ObjectPool< Node48 > f_n48;
438 ObjectPool< Node256 > f_n256;
495 return(
reference( f_leaf->view() , f_leaf->value ) );
500 f_leaf = step( f_S );
512 return( f_leaf == o.f_leaf );
529 if( isLeaf( root ) ) {
530 f_leaf = asLeaf( root );
533 f_S.push_back( Frame{
static_cast< const Node *
>( root ) , -1 } );
534 f_leaf = step( f_S );
538 static const Leaf * step( std::vector< Frame > & S );
540 std::vector< Frame > f_S;
541 const Leaf * f_leaf =
nullptr;
555#include "ADS/dictionary/impl/AdaptiveRadixTree.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
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 prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The vector kernels of ADS that are inlined in the middle of a search, on Google Highway,...
the iterator on the pairs by increasing keys, by a stack of the nodes on the way to a leaf
Definition AdaptiveRadixTree.h:477
const_iterator(void)=default
the end
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition AdaptiveRadixTree.h:484
reference operator*(void) const
the key of the leaf and its value
Definition AdaptiveRadixTree.h:494
PairRef< std::string_view, const V &, std::string > reference
a reference to an item
Definition AdaptiveRadixTree.h:482
const_iterator & operator++(void)
to the next leaf, by the walk of the stack
Definition AdaptiveRadixTree.h:499
std::pair< std::string, V > value_type
the type of the items, for the standard library
Definition AdaptiveRadixTree.h:480
const_iterator operator++(int)
to the next leaf, returning the iterator before the step
Definition AdaptiveRadixTree.h:504
std::input_iterator_tag iterator_category
the category of the iterator
Definition AdaptiveRadixTree.h:488
friend class AdaptiveRadixTree
Definition AdaptiveRadixTree.h:516
std::forward_iterator_tag iterator_concept
the concept of the iterator, for C++20
Definition AdaptiveRadixTree.h:486
bool operator==(const const_iterator &o) const
equal on the same leaf
Definition AdaptiveRadixTree.h:511
void swap(AdaptiveRadixTree &o) noexcept
exchanges the contents with other in
bool remove(std::string_view k)
removes , shrinking its node to the previous layout when that keeps about a quarter of its places fre...
V * find(std::string_view k)
the value of , to change in place, or nil
AdaptiveRadixTree(AdaptiveRadixTree &&o) noexcept
the items of other, which is left empty
void clear(void)
removes all the keys
V mapped_type
the type of the values
Definition AdaptiveRadixTree.h:453
AdaptiveRadixTree(void)=default
the empty tree
bool empty(void) const
true if there are no keys
std::string Key
the type of the keys
Definition AdaptiveRadixTree.h:148
AdaptiveRadixTree & operator=(AdaptiveRadixTree o) noexcept
the assignment, by copy and swap
void forEach(F &&f) const
f( key , value ) on each pair, by increasing keys
void forPrefix(std::string_view p, F &&f) const
f( key , value ) on each pair whose key begins with , by increasing keys
std::optional< V > lookup(std::string_view k) const
the value of , if there
const_iterator cbegin(void) const
the constant iterator on the first item
Definition AdaptiveRadixTree.h:470
reference const_reference
a constant reference to an item
Definition AdaptiveRadixTree.h:461
AdaptiveRadixTree(const AdaptiveRadixTree &o)
a copy of other
std::size_t size_type
the type of the sizes
Definition AdaptiveRadixTree.h:455
PairRef< std::string_view, const V &, std::string > reference
a reference to an item
Definition AdaptiveRadixTree.h:459
const_iterator iterator
the iterator on the items
Definition AdaptiveRadixTree.h:463
bool contains(std::string_view k) const
true if is there
~AdaptiveRadixTree()
frees the memory of the structure
const V * find(std::string_view k) const
the value of , or nil, by the search of lookup()
std::size_t size(void) const
the number of keys
std::string key_type
the type of the keys
Definition AdaptiveRadixTree.h:451
const_iterator end(void) const
the iterator past the last item
const_iterator cend(void) const
the constant iterator past the last item
Definition AdaptiveRadixTree.h:472
const_iterator begin(void) const
the iterator on the first item
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition AdaptiveRadixTree.h:457
std::pair< std::string, V > value_type
the type of the items, for the standard library
Definition AdaptiveRadixTree.h:449
bool insert(std::string_view k, const V &v)
gives the value ; true if was not there
V Value
the type of the values
Definition AdaptiveRadixTree.h:149
std::vector< std::string > keysWithPrefix(std::string_view p) const
the keys that begin with , in increasing order
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
std::vector< int > path(int r, int s, const std::vector< int > &p)
the path from to in the tree of the parents , empty if there is none (the procedure printPath of th...