ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SpaceEfficientSortedSet.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------- File SpaceEfficientSortedSet.h ---------------------*/
3/*--------------------------------------------------------------------------*/
62/*--------------------------------------------------------------------------*/
63/*----------------------------- DEFINITIONS --------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#ifndef __ADS_SpaceEfficientSortedSet
67 #define __ADS_SpaceEfficientSortedSet
68/* self-identification: #endif constant */
69
70/*--------------------------------------------------------------------------*/
71/*------------------------------ INCLUDES ----------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#include "ADS/Contract.h"
76
77#include <concepts>
78#include <cstddef>
79#include <memory>
80#include <optional>
81#include <utility>
82
83/*--------------------------------------------------------------------------*/
84/*--------------------------- NAMESPACE ------------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87namespace ADS
88{
89
90/*--------------------------------------------------------------------------*/
91/*--------------------- CLASS SpaceEfficientSortedSet ----------------------*/
92/*--------------------------------------------------------------------------*/
93
96template< std::totally_ordered T >
97 requires std::semiregular< T >
99{
100
101/*--------------------------------------------------------------------------*/
102 public:
103 using Item = T;
104
107 explicit SpaceEfficientSortedSet( std::size_t b = 32 ,
108 std::uint64_t seed = 1 );
109
112
116
119
121 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
122
124 [[nodiscard]] bool empty( void ) const { return( f_size == 0 ); }
125
127 [[nodiscard]] std::size_t capacity( void ) const {
128 return( f_blocks * ( f_b + 1 ) );
129 }
130
132 [[nodiscard]] bool contains( const T & x ) const;
133
136 [[nodiscard]] const T * find( const T & x ) const;
137
154 [[nodiscard]] const T * lowerBound( const T & x ) const;
155
184 bool insert( const T & x );
185
219 bool remove( const T & x );
220
222 template< class F >
223 void forEach( F && f ) const;
224
225/*--------------------------------------------------------------------------*/
226 private:
229 struct Block
230 {
231 std::unique_ptr< T[] > a;
232 std::size_t h = 0;
233 std::size_t s = 0;
234 Block * prev = nullptr;
235 Block * next = nullptr;
236 };
237
240 struct Key
241 {
242 const SpaceEfficientSortedSet * o;
243 const Block * b;
244 const T * x;
246 const T & item( void ) const { return( b ? o->at( b , b->s - 1 ) : *x ); }
248 friend bool operator==( const Key & p , const Key & q ) {
249 return( p.item() == q.item() );
250 }
252 friend bool operator<( const Key & p , const Key & q ) {
253 return( p.item() < q.item() );
254 }
256 friend bool operator>( const Key & p , const Key & q ) { return( q < p ); }
258 friend bool operator<=( const Key & p , const Key & q ) {
259 return( ! ( q < p ) );
260 }
262 friend bool operator>=( const Key & p , const Key & q ) {
263 return( ! ( p < q ) );
264 }
265 };
266
268 const T & at( const Block * B , std::size_t i ) const {
269 return( B->a[ ( B->h + i ) % ( f_b + 1 ) ] );
270 }
272 T & at( Block * B , std::size_t i ) {
273 return( B->a[ ( B->h + i ) % ( f_b + 1 ) ] );
274 }
275
277 Block * blockOf( const T & x ) const;
278
280 std::size_t place( const Block * B , const T & x ) const;
281
283 Block * newBlock( Block * B );
284
286 void freeBlock( Block * B );
287
289 void shiftForward( Block * B , Block * C );
290
292 void shiftBack( Block * B , Block * C );
293
295 [[nodiscard]] bool valid( void ) const;
296
297 std::size_t f_b;
298 std::size_t f_size = 0;
299 std::size_t f_blocks = 0;
300 Block * f_first = nullptr;
301 Block * f_last = nullptr;
302 SkipList< Key , Block * > f_index;
303
304 }; // end( class SpaceEfficientSortedSet )
305
306/*--------------------------------------------------------------------------*/
307
308} // end( namespace ADS )
309
310/*--------------------------------------------------------------------------*/
311/*-------------------------- IMPLEMENTATION --------------------------------*/
312/*--------------------------------------------------------------------------*/
313
314#include "ADS/set/impl/SpaceEfficientSortedSet.tpp"
315
316/*--------------------------------------------------------------------------*/
317
318#endif /* SpaceEfficientSortedSet.h included */
319
320/*--------------------------------------------------------------------------*/
321/*------------------- End File SpaceEfficientSortedSet.h -------------------*/
322/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The skip list of Pugh, beyond the book: a sorted list of the keys with express lanes,...
a SET of ordered items in blocks of about items, indexed by a skip list, the SESSet of Morin
Definition SpaceEfficientSortedSet.h:99
bool contains(const T &x) const
true if is an item, by lowerBound()
const T * lowerBound(const T &x) const
the address of the smallest item not smaller than , nil if there is none (the find(x) of an SSet),...
bool empty(void) const
true if there is no item
Definition SpaceEfficientSortedSet.h:124
std::size_t capacity(void) const
the number of places of the blocks,
Definition SpaceEfficientSortedSet.h:127
T Item
the type of the items
Definition SpaceEfficientSortedSet.h:103
std::size_t size(void) const
the number of items
Definition SpaceEfficientSortedSet.h:121
SpaceEfficientSortedSet & operator=(const SpaceEfficientSortedSet &)=delete
no copy, since the blocks are linked by pointers
SpaceEfficientSortedSet(std::size_t b=32, std::uint64_t seed=1)
the empty set, with blocks of places
const T * find(const T &x) const
the address of the item equal to , nil if there is none (the find(x) of a USet), valid until the next...
bool remove(const T &x)
removed, if it is there, in amortized expected time: a block left with items takes the first item o...
bool insert(const T &x)
added, if it is not there, in amortized expected time; true if it was not there
~SpaceEfficientSortedSet()
frees the blocks
SpaceEfficientSortedSet(const SpaceEfficientSortedSet &)=delete
no copy, since the blocks are linked by pointers
void forEach(F &&f) const
f( x ) on the items in increasing order
Definition ADT.h:96