ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BitmapTrie.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BitmapTrie.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_BitmapTrie
63 #define __ADS_BitmapTrie
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
71#include "ADS/Iterators.h"
72
73#include <array>
74#include <bit>
75#include <cstddef>
76#include <cstdint>
77#include <iterator>
78#include <optional>
79#include <string>
80#include <string_view>
81#include <utility>
82#include <variant>
83#include <vector>
84
85/*--------------------------------------------------------------------------*/
86/*--------------------------- NAMESPACE ------------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89namespace ADS
90{
91
92/*--------------------------------------------------------------------------*/
93/*-------------------------- CLASS BitmapTrie ------------------------------*/
94/*--------------------------------------------------------------------------*/
95
98template< class V = std::monostate >
100{
101
102/*--------------------------------------------------------------------------*/
103 public:
104 using Key = std::string;
105 using Item = std::string;
106 using Value = V;
107
109 BitmapTrie( void );
110
113
115 BitmapTrie & operator=( BitmapTrie && o ) noexcept;
116
117 BitmapTrie( const BitmapTrie & ) = delete;
118 BitmapTrie & operator=( const BitmapTrie & ) = delete;
119
122
123/*--------------------------------------------------------------------------*/
124
145 [[nodiscard]] const std::string * lowerBound( std::string_view s ) const;
146
148 [[nodiscard]] const std::string * find( std::string_view s ) const;
149
151 [[nodiscard]] std::optional< V > lookup( std::string_view s ) const;
152
154 [[nodiscard]] bool contains( std::string_view s ) const;
155
180 bool insert( std::string_view s , const V & v = V() );
181
203 bool remove( std::string_view s );
204
206 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
207
209 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
210
212 [[nodiscard]] std::size_t nodes( void ) const { return( f_nodes ); }
213
214/*--------------------------------------------------------------------------*/
215 private:
216 struct Node;
217
219 struct Cell
220 {
221 std::string key;
222 V value{};
223 Cell * prev = nullptr;
224 Cell * next = nullptr;
225 };
226
229 struct Node
230 {
231 std::array< std::uint64_t , 4 > bits{};
232 std::vector< Node * > kids;
233 Cell * term = nullptr;
234 Cell * min = nullptr;
235 Cell * max = nullptr;
236 };
237
238 public:
239/*------------------------- THE STANDARD INTERFACE -------------------------*/
240
241 // a constant view of the pairs ( string , value ) in increasing order,
242 // along the list
243
246 {
247 public:
249 using iterator_category = std::bidirectional_iterator_tag;
251 using value_type = std::pair< std::string , V >;
253 using key_type = std::string;
255 using mapped_type = V;
257 using difference_type = std::ptrdiff_t;
261 using pointer = void;
262
264 const_iterator( void ) = default;
265
267 explicit const_iterator( const Cell * l ) : f_l( l ) { }
268
270 reference operator*( void ) const {
271 return( reference( f_l->key , f_l->value ) );
272 }
273
276 f_l = f_l->next;
277 return( *this );
278 }
279
282 auto t = *this;
283 ++*this;
284 return( t );
285 }
286
289 f_l = f_l->prev;
290 return( *this );
291 }
292
295 auto t = *this;
296 --*this;
297 return( t );
298 }
299
301 bool operator==( const const_iterator & o ) const = default;
302
303 private:
304 const Cell * f_l = nullptr;
305 };
306
310 using size_type = std::size_t;
312 using difference_type = std::ptrdiff_t;
319
321 [[nodiscard]] const_iterator begin( void ) const {
322 return( const_iterator( f_dummy->next ) );
323 }
324
325 [[nodiscard]] const_iterator end( void ) const {
326 return( const_iterator( f_dummy ) );
327 }
328
329 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
331 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
332
334 [[nodiscard]] const_iterator seek( std::string_view s ) const;
335
336/*--------------------------------------------------------------------------*/
337 private:
339 [[nodiscard]] static bool has( const Node * u , unsigned c );
340
342 [[nodiscard]] static std::size_t rank( const Node * u , unsigned c );
343
345 [[nodiscard]] static Node * child( const Node * u , unsigned c );
346
348 [[nodiscard]] static Node * after( const Node * u , unsigned c );
349
351 [[nodiscard]] static bool before( const Node * u , unsigned c );
352
354 [[nodiscard]] Node * node( std::string_view s ) const;
355
357 [[nodiscard]] Cell * successor( std::string_view s ) const;
358
360 static void destroy( Node * u );
361
363 [[nodiscard]] bool consistent( void ) const;
364
367 [[nodiscard]] bool subtree( const Node * u , const Cell *& lo ,
368 const Cell *& hi , std::size_t & n ) const;
369
370 Node * f_root;
371 Cell * f_dummy;
372 std::size_t f_n = 0;
373 std::size_t f_nodes = 1;
374
375 }; // end( class BitmapTrie )
376
377/*--------------------------------------------------------------------------*/
378
379} // end( namespace ADS )
380
381/*--------------------------------------------------------------------------*/
382/*-------------------------- IMPLEMENTATION --------------------------------*/
383/*--------------------------------------------------------------------------*/
384
385#include "ADS/dictionary/impl/BitmapTrie.tpp"
386
387/*--------------------------------------------------------------------------*/
388
389#endif /* BitmapTrie.h included */
390
391/*--------------------------------------------------------------------------*/
392/*------------------------- End File BitmapTrie.h --------------------------*/
393/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
the bidirectional iterator along the list
Definition BitmapTrie.h:246
V mapped_type
the type of the values
Definition BitmapTrie.h:255
const_iterator(void)=default
the end of no trie
std::bidirectional_iterator_tag iterator_category
the category of the iterator
Definition BitmapTrie.h:249
const_iterator & operator++(void)
to the next string
Definition BitmapTrie.h:275
PairRef< const std::string &, const V & > reference
a pair of references, by value
Definition BitmapTrie.h:259
bool operator==(const const_iterator &o) const =default
equal on the same item
void pointer
no pointer: the pairs are made on the fly
Definition BitmapTrie.h:261
reference operator*(void) const
the pair of the item
Definition BitmapTrie.h:270
const_iterator & operator--(void)
to the previous string
Definition BitmapTrie.h:288
const_iterator operator--(int)
to the previous string, returning the iterator before the step
Definition BitmapTrie.h:294
const_iterator(const Cell *l)
on the item
Definition BitmapTrie.h:267
std::string key_type
the type of the keys
Definition BitmapTrie.h:253
std::pair< std::string, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition BitmapTrie.h:251
const_iterator operator++(int)
to the next string, returning the iterator before the step
Definition BitmapTrie.h:281
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition BitmapTrie.h:257
bool insert(std::string_view s, const V &v=V())
associates to ; true if was not there, in time
std::string Item
the items of the SSet, the strings
Definition BitmapTrie.h:105
std::optional< V > lookup(std::string_view s) const
the value of , or nothing, in time
BitmapTrie(BitmapTrie &&o)
the trie of o, which is left empty
const_iterator cend(void) const
as end()
Definition BitmapTrie.h:331
bool contains(std::string_view s) const
true if is there, in time
reference const_reference
a pair of references, by value
Definition BitmapTrie.h:316
const_iterator iterator
the iterator on the items
Definition BitmapTrie.h:318
const std::string * find(std::string_view s) const
the string , nil if it is not there, in time
const_iterator seek(std::string_view s) const
the iterator on the smallest string , end() if none
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition BitmapTrie.h:312
BitmapTrie(void)
the empty trie
BitmapTrie(const BitmapTrie &)=delete
std::size_t size_type
the type of the sizes
Definition BitmapTrie.h:310
std::string Key
the type of the keys
Definition BitmapTrie.h:104
bool remove(std::string_view s)
removes ; true if it was there, in time
V Value
the type of the values
Definition BitmapTrie.h:106
BitmapTrie & operator=(BitmapTrie &&o) noexcept
the trie of o, which gets the strings of this one
~BitmapTrie()
frees the nodes and the strings
std::size_t nodes(void) const
the number of nodes, the root included
Definition BitmapTrie.h:212
const_iterator end(void) const
the iterator past the largest string
Definition BitmapTrie.h:325
bool empty(void) const
true if there are no strings
Definition BitmapTrie.h:209
std::size_t size(void) const
the number of strings
Definition BitmapTrie.h:206
BitmapTrie & operator=(const BitmapTrie &)=delete
typename const_iterator::value_type value_type
the type of the items, for the standard library
Definition BitmapTrie.h:308
const_iterator begin(void) const
the iterator on the smallest string
Definition BitmapTrie.h:321
const_iterator cbegin(void) const
as begin()
Definition BitmapTrie.h:329
const std::string * lowerBound(std::string_view s) const
the smallest string , nil if none (the find( s ) of the SSet of Morin's book), in time
typename const_iterator::reference reference
a pair of references, by value
Definition BitmapTrie.h:314
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