ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
AdaptiveRadixTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File AdaptiveRadixTree.h ------------------------*/
3/*--------------------------------------------------------------------------*/
100/*--------------------------------------------------------------------------*/
101/*----------------------------- DEFINITIONS --------------------------------*/
102/*--------------------------------------------------------------------------*/
103
104#ifndef __ADS_AdaptiveRadixTree
105 #define __ADS_AdaptiveRadixTree
106/* self-identification: #endif constant */
107
108/*--------------------------------------------------------------------------*/
109/*------------------------------ INCLUDES ----------------------------------*/
110/*--------------------------------------------------------------------------*/
111
112#include "ADS/Contract.h"
113#include "ADS/Iterators.h"
114#include "ADS/SimdInline.h"
117#include "ADS/memory/Prefetch.h"
118
119#include <cstddef>
120#include <cstdint>
121#include <cstring>
122#include <iterator>
123#include <optional>
124#include <string>
125#include <string_view>
126#include <utility>
127#include <vector>
128
129/*--------------------------------------------------------------------------*/
130/*--------------------------- NAMESPACE ------------------------------------*/
131/*--------------------------------------------------------------------------*/
132
133namespace ADS
134{
135
136/*--------------------------------------------------------------------------*/
137/*------------------------ CLASS AdaptiveRadixTree -------------------------*/
138/*--------------------------------------------------------------------------*/
139
142template< class V >
144{
145
146/*--------------------------------------------------------------------------*/
147 public:
148 using Key = std::string;
149 using Value = V;
150
152 AdaptiveRadixTree( void ) = default;
153
156
159
162
165
166/*--------------------------------------------------------------------------*/
167
189 [[nodiscard]] std::optional< V > lookup( std::string_view k ) const;
190
192 [[nodiscard]] const V * find( std::string_view k ) const;
193
195 [[nodiscard]] V * find( std::string_view k );
196
218 bool insert( std::string_view k , const V & v );
219
223 bool remove( std::string_view k );
224
226 [[nodiscard]] bool contains( std::string_view k ) const;
227
229 [[nodiscard]] std::size_t size( void ) const;
230
232 [[nodiscard]] bool empty( void ) const;
233
235 void clear( void );
236
238 void swap( AdaptiveRadixTree & o ) noexcept;
239
241 template< class F >
242 void forEach( F && f ) const;
243
246 template< class F >
247 void forPrefix( std::string_view p , F && f ) const;
248
250 [[nodiscard]] std::vector< std::string >
251 keysWithPrefix( std::string_view p ) const;
252
253/*--------------------------------------------------------------------------*/
254 private:
255 static constexpr unsigned MAXP = 8;
256
258 enum Type : std::uint8_t { N4 , N16 , N48 , N256 };
259
261 struct Leaf
262 {
263 V value;
264 std::uint32_t len;
266 [[nodiscard]] const char * key( void ) const {
267 return( reinterpret_cast< const char * >( this + 1 ) );
268 }
270 [[nodiscard]] std::string_view view( void ) const {
271 return( std::string_view( key() , len ) );
272 }
273 };
274
276 struct Node
277 {
278 Type type;
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 ) { }
285 };
286
288 struct Node4 : Node
289 {
290 unsigned char key[ 4 ];
291 void * child[ 4 ];
293 Node4( void ) : Node( N4 ) { }
294 };
295
297 struct Node16 : Node
298 {
299 alignas( 16 ) unsigned char key[ 16 ];
300 void * child[ 16 ];
302 Node16( void ) : Node( N16 ) { }
303 };
304
306 struct Node48 : Node
307 {
308 unsigned char index[ 256 ];
309 void * child[ 48 ];
311 Node48( void ) : Node( N48 ) { std::memset( index , 0 , 256 ); }
312 };
313
315 struct Node256 : Node
316 {
317 void * child[ 256 ];
319 Node256( void ) : Node( N256 ) {
320 for( auto & c : child )
321 c = nullptr;
322 }
323 };
324
326 [[nodiscard]] static bool isLeaf( const void * p ) {
327 return( reinterpret_cast< std::uintptr_t >( p ) & 1 );
328 }
330 [[nodiscard]] static Leaf * asLeaf( const void * p ) {
331 return( reinterpret_cast< Leaf * >(
332 reinterpret_cast< std::uintptr_t >( p ) - 1 ) );
333 }
335 [[nodiscard]] static void * tag( Leaf * l ) {
336 return( reinterpret_cast< void * >(
337 reinterpret_cast< std::uintptr_t >( l ) + 1 ) );
338 }
339
344 class Slab
345 {
346 public:
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;
364
365 private:
367 static constexpr std::size_t STEP = 16 , CLASSES = 32;
369 struct Free
370 {
371 Free * next;
372 };
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 ] = {};
378 };
379
380 [[nodiscard]] Leaf * newLeaf( std::string_view k , const V & v );
381
382 void freeLeaf( Leaf * l ) noexcept;
383
384 template< class N >
385 [[nodiscard]] N * make( void );
386
387 void freeNode( Node * n ) noexcept;
388
389 [[nodiscard]] static void * const * child( const Node * n , unsigned char c );
390
391 [[nodiscard]] static void ** child( Node * n , unsigned char c );
392
393 [[nodiscard]] static const Leaf * minimum( const void * p );
394
395 [[nodiscard]] static std::uint32_t
396 mismatch( const Node * n , std::string_view k , std::size_t d );
397
398 void addChild( void ** ref , Node * n , unsigned char c , void * x );
399
402 void split( void ** ref , std::size_t d , std::string_view k , const V & v );
403
404 void removeChild( void ** ref , Node * n , unsigned char c , void ** slot );
405
406 void shrink( void ** ref , Node * n );
407
408 [[nodiscard]] const Leaf * search( std::string_view k ) const;
409
413 static void ahead( const void * p , unsigned char c );
414
418 static void children( const Node * n );
419
420 void destroy( void * p ) noexcept;
421
422 template< class F >
423 static void visit( const void * p , F & f );
424
425 template< class F >
426 static void each( const Node * n , F && f );
427
428 [[nodiscard]] bool valid( void ) const;
429
430 [[nodiscard]] bool valid( const void * p , std::string & path ,
431 std::size_t & leaves , bool root ) const;
432
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;
439 Slab f_slab;
440
441/*------------------------- THE STANDARD INTERFACE -------------------------*/
442 public:
443 // a constant view of the pairs ( key , value ) by increasing keys,
444 // forward, a stack of the nodes on the way to a leaf
445
446 class const_iterator;
447
449 using value_type = std::pair< std::string , V >;
451 using key_type = std::string;
453 using mapped_type = V;
455 using size_type = std::size_t;
457 using difference_type = std::ptrdiff_t;
464
466 [[nodiscard]] const_iterator begin( void ) const;
468 [[nodiscard]] const_iterator end( void ) const;
470 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
472 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
473
477 {
478 public:
480 using value_type = std::pair< std::string , V >;
484 using difference_type = std::ptrdiff_t;
486 using iterator_concept = std::forward_iterator_tag;
488 using iterator_category = std::input_iterator_tag;
489
491 const_iterator( void ) = default;
492
494 reference operator*( void ) const {
495 return( reference( f_leaf->view() , f_leaf->value ) );
496 }
497
500 f_leaf = step( f_S );
501 return( *this );
502 }
503
505 auto t = *this;
506 ++*this;
507 return( t );
508 }
509
511 bool operator==( const const_iterator & o ) const {
512 return( f_leaf == o.f_leaf );
513 }
514
515 private:
516 friend class AdaptiveRadixTree;
517
519 struct Frame
520 {
521 const Node * node;
522 int next;
523 };
524
526 explicit const_iterator( const void * root ) {
527 if( ! root )
528 return;
529 if( isLeaf( root ) ) {
530 f_leaf = asLeaf( root );
531 return;
532 }
533 f_S.push_back( Frame{ static_cast< const Node * >( root ) , -1 } );
534 f_leaf = step( f_S );
535 }
536
538 static const Leaf * step( std::vector< Frame > & S );
539
540 std::vector< Frame > f_S;
541 const Leaf * f_leaf = nullptr;
542
543 }; // end( class const_iterator )
544
545 }; // end( class AdaptiveRadixTree )
546
547/*--------------------------------------------------------------------------*/
548
549} // end( namespace ADS )
550
551/*--------------------------------------------------------------------------*/
552/*-------------------------- IMPLEMENTATION --------------------------------*/
553/*--------------------------------------------------------------------------*/
554
555#include "ADS/dictionary/impl/AdaptiveRadixTree.tpp"
556
557/*--------------------------------------------------------------------------*/
558
559#endif /* AdaptiveRadixTree.h included */
560
561/*--------------------------------------------------------------------------*/
562/*---------------------- End File AdaptiveRadixTree.h ----------------------*/
563/*--------------------------------------------------------------------------*/
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
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...
Definition ADT.h:96