ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SpaceEfficientSortedSet.h File Reference

The space-efficient sorted set of Morin (his SESSet, exercise 4.13), beyond the book: a SET [see ADT.h] of ordered items and an SSet of Morin, with find() and lowerBound(), that stores \( n \) items in the space of \( n \) items plus \( O( n / b + b ) \) words, \( b \) a parameter. More...

#include "ADS/Contract.h"
#include "ADS/dictionary/SkipList.h"
#include <concepts>
#include <cstddef>
#include <memory>
#include <optional>
#include <utility>
#include "ADS/set/impl/SpaceEfficientSortedSet.tpp"

Go to the source code of this file.

Classes

class  ADS::SpaceEfficientSortedSet< T >
 a SET of ordered items in blocks of about \( b \) items, indexed by a skip list, the SESSet of Morin More...

Namespaces

namespace  ADS

Detailed description

The space-efficient sorted set of Morin (his SESSet, exercise 4.13), beyond the book: a SET [see ADT.h] of ordered items and an SSet of Morin, with find() and lowerBound(), that stores \( n \) items in the space of \( n \) items plus \( O( n / b + b ) \) words, \( b \) a parameter.

The items are kept in order in a list of blocks, as in the SEList of Morin (section 3.3): each block is a circular buffer of \( b + 1 \) places, and every block but the last holds from \( b - 1 \) to \( b + 1 \) items, so that at most 2 places per block and the \( b + 1 \) of the last one are empty. The blocks are indexed by a SkipList [see SkipList.h] ordered by their largest items: since the blocks partition the sorted items into intervals, their order never changes when items move between neighbors, so that the skip list sees only the creation and the removal of a block, and a search compares the item sought with the largest item of a block. A search finds the first block whose largest item is not smaller than \( x \) and then \( x \) in it by binary search, \( O( \log n ) \) expected time.

An insertion into a full block looks for a block that is not full among the next \( b \) ones and moves one item from the end of each block to the front of the next one up to it, \( O( 1 ) \) per block in a circular buffer; if the \( b \) blocks are full a new block follows them and the \( b ( b + 1 ) \) items are spread over the \( b + 1 \) blocks, \( b \) each. A removal that leaves a block with \( b - 2 \) items borrows one item from the next ones in the same way, or, if the next \( b - 1 \) blocks hold \( b - 1 \) items each, gathers the items of the \( b \) blocks into \( b - 1 \) of them and frees the last. A spread or a gather costs \( O( b^2 ) \) and leaves the \( b \) blocks \( \Omega( b ) \) operations away from the next one, hence an insertion or a removal costs \( O( \log n + b ) \) amortized time, the argument of Lemma 3.1 of Morin.

Morin gives no code for it; what the benchmarks are to check is the space, \( n + O( n / b + b ) \) items against the \( 3 n \) words of links and the \( n \) allocations of SkipList, and the time of a search, one search of the skip list on \( n / b \) blocks plus a binary search in a block of contiguous items, against a search of the skip list on \( n \) nodes that misses the cache at each step.

The contracts [see Contract.h]: \( b \ge 3 \); after insert() the item is there and after remove() it is not; the invariant, an \( O( n ) \) check at the level 3, is that the items increase along the blocks and that each block but the last holds from \( b - 1 \) to \( b + 1 \) items. The operations are defined in SpaceEfficientSortedSet.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 3.3 and exercise 4.13
W. Pugh, Skip lists: a probabilistic alternative to balanced trees, Communications of the ACM 33(6), 668-676, 1990, doi:10.1145/78973.78977
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa