ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
YFastTrie.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File YFastTrie.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
53/*--------------------------------------------------------------------------*/
54/*----------------------------- DEFINITIONS --------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57#ifndef __ADS_YFastTrie
58 #define __ADS_YFastTrie
59/* self-identification: #endif constant */
60
61/*--------------------------------------------------------------------------*/
62/*------------------------------ INCLUDES ----------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#include "ADS/Contract.h"
68#include "ADS/tree/Treap.h"
69
70#include <concepts>
71#include <cstddef>
72#include <cstdint>
73#include <limits>
74#include <optional>
75#include <variant>
76
77/*--------------------------------------------------------------------------*/
78/*--------------------------- NAMESPACE ------------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81namespace ADS
82{
83
84/*--------------------------------------------------------------------------*/
85/*--------------------------- CLASS YFastTrie ------------------------------*/
86/*--------------------------------------------------------------------------*/
87
90template< std::unsigned_integral U , class V = std::monostate ,
91 int W = std::numeric_limits< U >::digits >
92 requires TrieWidth< U , W >
94{
95
96/*--------------------------------------------------------------------------*/
97 public:
98 using Key = U;
99 using Item = U;
100 using Value = V;
101
103 explicit YFastTrie( std::uint64_t seed = 1 );
104
107
109 YFastTrie & operator=( YFastTrie && o ) noexcept;
110
111 YFastTrie( const YFastTrie & ) = delete;
112 YFastTrie & operator=( const YFastTrie & ) = delete;
113
116
117/*--------------------------------------------------------------------------*/
118
134 [[nodiscard]] const U * lowerBound( U x ) const;
135
139 [[nodiscard]] const U * find( U x ) const;
140
143 [[nodiscard]] std::optional< V > lookup( U x ) const;
144
147 [[nodiscard]] bool contains( U x ) const;
148
171 bool insert( U x , const V & v = V() );
172
193 bool remove( U x );
194
196 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
197
199 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
200
202 [[nodiscard]] std::size_t groups( void ) const { return( f_x.size() ); }
203
205 template< class F >
206 void forEach( F && f ) const;
207
208/*--------------------------------------------------------------------------*/
209 private:
210 using Group = Treap< U , V >;
211
213 static constexpr U TOP = ( W == std::numeric_limits< U >::digits )
214 ? std::numeric_limits< U >::max()
215 : U( ( U( 1 ) << W ) - 1 );
216
219 [[nodiscard]] static const U * ceiling( const Group & T , U x );
220
223 [[nodiscard]] bool consistent( void ) const;
224
226 SplitMix64 f_g;
227 std::size_t f_n = 0;
228
229 }; // end( class YFastTrie )
230
231/*--------------------------------------------------------------------------*/
232
233} // end( namespace ADS )
234
235/*--------------------------------------------------------------------------*/
236/*-------------------------- IMPLEMENTATION --------------------------------*/
237/*--------------------------------------------------------------------------*/
238
239#include "ADS/dictionary/impl/YFastTrie.tpp"
240
241/*--------------------------------------------------------------------------*/
242
243#endif /* YFastTrie.h included */
244
245/*--------------------------------------------------------------------------*/
246/*------------------------- End File YFastTrie.h ---------------------------*/
247/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Hashing with random tables, beyond the book:
The treap of Seidel and Aragon, beyond the book: a binary search tree on the keys that is also a heap...
The x-fast tries of Willard, beyond the book: SORTED SETs of the integers of bits,...
the generator SplitMix64, a UniformRandomBitGenerator
Definition RandomHashing.h:122
a DICTIONARY on a treap, with the operations based on join [see Joinable]
Definition Treap.h:111
the x-fast trie: a binary trie with a hash table of the prefixes per depth
Definition XFastTrie.h:124
std::size_t groups(void) const
the number of groups, the last one included
Definition YFastTrie.h:202
YFastTrie(std::uint64_t seed=1)
the empty trie, its coins drawn from the seed
std::optional< V > lookup(U x) const
the value of , or nothing
bool insert(U x, const V &v=V())
associates to ; true if was not there, in expected amortized time
YFastTrie(const YFastTrie &)=delete
void forEach(F &&f) const
f( x , v ) on the pairs by increasing keys
~YFastTrie()
frees the groups
YFastTrie & operator=(const YFastTrie &)=delete
bool empty(void) const
true if there are no keys
Definition YFastTrie.h:199
U Item
the items of the SSet, the keys
Definition YFastTrie.h:99
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
const U * find(U x) const
the key , nil if it is not there, in expected time
YFastTrie(YFastTrie &&o)
the trie of o, which is left empty
U Key
the type of the keys
Definition YFastTrie.h:98
bool remove(U x)
removes ; true if it was there, in expected amortized time
YFastTrie & operator=(YFastTrie &&o) noexcept
the trie of o, which gets the keys of this one
bool contains(U x) const
true if is there
V Value
the type of the values
Definition YFastTrie.h:100
std::size_t size(void) const
the number of keys
Definition YFastTrie.h:196
a monoid with the inverse G::inverse( a ), i.e., combine( inverse( a) , a ) and combine( a ,...
Definition Algebra.h:130
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
Definition ADT.h:96