ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
XFastTrie.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File XFastTrie.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
80/*--------------------------------------------------------------------------*/
81/*----------------------------- DEFINITIONS --------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84#ifndef __ADS_XFastTrie
85 #define __ADS_XFastTrie
86/* self-identification: #endif constant */
87
88/*--------------------------------------------------------------------------*/
89/*------------------------------ INCLUDES ----------------------------------*/
90/*--------------------------------------------------------------------------*/
91
92#include "ADS/Contract.h"
93#include "ADS/Iterators.h"
97
98#include <array>
99#include <concepts>
100#include <cstddef>
101#include <iterator>
102#include <limits>
103#include <optional>
104#include <utility>
105#include <variant>
106
107/*--------------------------------------------------------------------------*/
108/*--------------------------- NAMESPACE ------------------------------------*/
109/*--------------------------------------------------------------------------*/
110
111namespace ADS
112{
113
114/*--------------------------------------------------------------------------*/
115/*--------------------------- CLASS XFastTrie ------------------------------*/
116/*--------------------------------------------------------------------------*/
117
120template< std::unsigned_integral U , class V = std::monostate ,
121 int W = std::numeric_limits< U >::digits >
122 requires TrieWidth< U , W >
123class XFastTrie : public detail::JumpTrie< U , V , W , true >
124{
125
126 using Base = detail::JumpTrie< U , V , W , true >;
127
128/*--------------------------------------------------------------------------*/
129 public:
130 using Base::Base;
131
156 [[nodiscard]] const U * findNear( U x ) const;
157
158 }; // end( class XFastTrie )
159
160/*--------------------------------------------------------------------------*/
161/*------------------------- CLASS ListXFastTrie ----------------------------*/
162/*--------------------------------------------------------------------------*/
163
166template< std::unsigned_integral U , class V = std::monostate ,
167 int W = std::numeric_limits< U >::digits >
168 requires TrieWidth< U , W >
170{
171
172/*--------------------------------------------------------------------------*/
173 public:
174 using Key = U;
175 using Item = U;
176 using Value = V;
177
180
183
186
187 ListXFastTrie( const ListXFastTrie & ) = delete;
188 ListXFastTrie & operator=( const ListXFastTrie & ) = delete;
189
192
193/*--------------------------------------------------------------------------*/
194
218 [[nodiscard]] const U * lowerBound( U x ) const;
219
223 [[nodiscard]] const U * find( U x ) const;
224
232 [[nodiscard]] const U * findNear( U x ) const;
233
236 [[nodiscard]] std::optional< V > lookup( U x ) const;
237
240 [[nodiscard]] bool contains( U x ) const;
241
263 bool insert( U x , const V & v = V() );
264
288 bool remove( U x );
289
291 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
292
294 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
295
297 [[nodiscard]] std::size_t entries( void ) const;
298
299/*--------------------------------------------------------------------------*/
300 private:
302 struct Leaf
303 {
304 Leaf * prev = nullptr;
305 Leaf * next = nullptr;
306 U key = 0;
307 V value{};
308 };
309
311 struct Entry
312 {
313 Leaf * min;
314 Leaf * max;
315 };
316
317 public:
318/*------------------------- THE STANDARD INTERFACE -------------------------*/
319
320 // a constant view of the pairs ( key , value ) by increasing key, along
321 // the list
322
325 {
326 public:
328 using iterator_category = std::bidirectional_iterator_tag;
330 using value_type = std::pair< U , V >;
332 using key_type = U;
334 using mapped_type = V;
336 using difference_type = std::ptrdiff_t;
340 using pointer = void;
341
343 const_iterator( void ) = default;
344
346 explicit const_iterator( const Leaf * l ) : f_l( l ) { }
347
349 reference operator*( void ) const {
350 return( reference( f_l->key , f_l->value ) );
351 }
352
355 f_l = f_l->next;
356 return( *this );
357 }
358
361 auto t = *this;
362 ++*this;
363 return( t );
364 }
365
368 f_l = f_l->prev;
369 return( *this );
370 }
371
374 auto t = *this;
375 --*this;
376 return( t );
377 }
378
380 bool operator==( const const_iterator & o ) const = default;
381
382 private:
383 const Leaf * f_l = nullptr;
384 };
385
389 using size_type = std::size_t;
391 using difference_type = std::ptrdiff_t;
398
400 [[nodiscard]] const_iterator begin( void ) const {
401 return( const_iterator( f_dummy->next ) );
402 }
403
404 [[nodiscard]] const_iterator end( void ) const {
405 return( const_iterator( f_dummy ) );
406 }
407
408 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
410 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
411
414 [[nodiscard]] const_iterator seek( U x ) const;
415
416/*--------------------------------------------------------------------------*/
417 private:
420 [[nodiscard]] bool entry( U p , int i , Entry & e ) const;
421
424 [[nodiscard]] Leaf * follow( U x , const Entry & e , int l ) const;
425
427 [[nodiscard]] Leaf * successor( U x ) const;
428
431 [[nodiscard]] int deepest( U x , int lo , int hi , Entry & e ) const;
432
435 [[nodiscard]] bool consistent( U x ) const;
436
437 ObjectPool< Leaf > f_pool{ 1 << 16 };
438 Leaf * f_dummy = nullptr;
439 std::size_t f_n = 0;
440 std::array< FlatHashTable< U , Entry > , W + 1 > f_t;
442
443 }; // end( class ListXFastTrie )
444
445/*--------------------------------------------------------------------------*/
446
447} // end( namespace ADS )
448
449/*--------------------------------------------------------------------------*/
450/*-------------------------- IMPLEMENTATION --------------------------------*/
451/*--------------------------------------------------------------------------*/
452
453#include "ADS/dictionary/impl/XFastTrie.tpp"
454
455/*--------------------------------------------------------------------------*/
456
457#endif /* XFastTrie.h included */
458
459/*--------------------------------------------------------------------------*/
460/*------------------------- End File XFastTrie.h ---------------------------*/
461/*--------------------------------------------------------------------------*/
The binary tries of the integers of bits, beyond the book: a SORTED SET of keys ,...
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 bidirectional iterator along the list
Definition XFastTrie.h:325
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition XFastTrie.h:336
V mapped_type
the type of the values
Definition XFastTrie.h:334
std::bidirectional_iterator_tag iterator_category
the category of the iterator
Definition XFastTrie.h:328
reference operator*(void) const
the pair of the key
Definition XFastTrie.h:349
void pointer
no pointer: the pairs are made on the fly
Definition XFastTrie.h:340
const_iterator & operator--(void)
to the previous key
Definition XFastTrie.h:367
U key_type
the type of the keys
Definition XFastTrie.h:332
const_iterator(const Leaf *l)
on the key
Definition XFastTrie.h:346
bool operator==(const const_iterator &o) const =default
equal on the same key
const_iterator & operator++(void)
to the next key
Definition XFastTrie.h:354
PairRef< const U &, const V & > reference
a pair of references, by value
Definition XFastTrie.h:338
const_iterator(void)=default
the end of no trie
const_iterator operator++(int)
to the next key, returning the iterator before the step
Definition XFastTrie.h:360
const_iterator operator--(int)
to the previous key, returning the iterator before the step
Definition XFastTrie.h:373
std::pair< U, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition XFastTrie.h:330
ListXFastTrie(void)
the empty trie
const_iterator begin(void) const
the iterator on the smallest key
Definition XFastTrie.h:400
ListXFastTrie & operator=(ListXFastTrie &&o) noexcept
the trie of o, which is left empty; the keys of this one are freed
bool insert(U x, const V &v=V())
associates to ; true if was not there, in expected time
ListXFastTrie(ListXFastTrie &&o)
the trie of o, which is left empty
std::size_t size(void) const
the number of keys
Definition XFastTrie.h:291
typename const_iterator::reference reference
a pair of references, by value
Definition XFastTrie.h:393
std::size_t entries(void) const
the number of entries of the tables
bool empty(void) const
true if there are no keys
Definition XFastTrie.h:294
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition XFastTrie.h:391
const_iterator seek(U x) const
the iterator on the smallest key , end() if none
U Key
the type of the keys
Definition XFastTrie.h:174
ListXFastTrie & operator=(const ListXFastTrie &)=delete
~ListXFastTrie()
frees the keys
bool contains(U x) const
true if is there, in expected time
const U * find(U x) const
the key , nil if it is not there, in expected time
typename const_iterator::value_type value_type
the type of the items, for the standard library
Definition XFastTrie.h:387
const_iterator end(void) const
the iterator past the largest key
Definition XFastTrie.h:404
const_iterator iterator
the iterator on the items
Definition XFastTrie.h:397
U Item
the items of the SSet, the keys
Definition XFastTrie.h:175
ListXFastTrie(const ListXFastTrie &)=delete
const_iterator cend(void) const
as end()
Definition XFastTrie.h:410
std::optional< V > lookup(U x) const
the value of , or nothing, in expected time
V Value
the type of the values
Definition XFastTrie.h:176
const U * lowerBound(U x) const
the smallest key , nil if none (the find( x ) of the SSet of Morin's book), in expected time
bool remove(U x)
removes ; true if it was there, in expected time
std::size_t size_type
the type of the sizes
Definition XFastTrie.h:389
const_iterator cbegin(void) const
as begin()
Definition XFastTrie.h:408
const U * findNear(U x) const
the smallest key , nil if none, in expected time
reference const_reference
a pair of references, by value
Definition XFastTrie.h:395
the x-fast trie: a binary trie with a hash table of the prefixes per depth
Definition XFastTrie.h:124
const U * findNear(U x) const
the smallest key , nil if none, in expected time
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
Definition ADT.h:96