ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SpaceEfficientSortedSet< T > Class Template Reference

a SET of ordered items in blocks of about \( b \) items, indexed by a skip list, the SESSet of Morin More...

#include <ADS/set/SpaceEfficientSortedSet.h>

Public types

using Item = T
 the type of the items

Public member functions

 SpaceEfficientSortedSet (std::size_t b=32, std::uint64_t seed=1)
 the empty set, with blocks of \( b + 1 \) places
 SpaceEfficientSortedSet (const SpaceEfficientSortedSet &)=delete
 no copy, since the blocks are linked by pointers
SpaceEfficientSortedSet & operator= (const SpaceEfficientSortedSet &)=delete
 no copy, since the blocks are linked by pointers
 ~SpaceEfficientSortedSet ()
 frees the blocks
std::size_t size (void) const
 the number \( n \) of items
bool empty (void) const
 true if there is no item
std::size_t capacity (void) const
 the number of places of the blocks, \( n + O( n / b + b ) \)
bool contains (const T &x) const
 true if \( x \) is an item, by lowerBound()
const T * find (const T &x) const
 the address of the item equal to \( x \), nil if there is none (the find(x) of a USet), valid until the next change, by lowerBound()
const T * lowerBound (const T &x) const
 the address of the smallest item not smaller than \( x \), nil if there is none (the find(x) of an SSet), valid until the next change, in \( O( \log n ) \) expected time
bool insert (const T &x)
 \( x \) added, if it is not there, in \( O( \log n + b ) \) amortized expected time; true if it was not there
bool remove (const T &x)
 \( x \) removed, if it is there, in \( O( \log n + b ) \) amortized expected time: a block left with \( b - 2 \) items takes the first item of each next block up to the first one with more than \( b - 1 \) items, or the last one, within \( b \) blocks, and if there is none the items of the \( b \) blocks are gathered into the first \( b - 1 \), the last one freed (the reverse of insert())
template<class F>
void forEach (F &&f) const
 f( x ) on the items in increasing order

Detailed description

template<std::totally_ordered T>
requires std::semiregular< T >
class ADS::SpaceEfficientSortedSet< T >

a SET of ordered items in blocks of about \( b \) items, indexed by a skip list, the SESSet of Morin

Member typedef documentation

◆ Item

template<std::totally_ordered T>
using ADS::SpaceEfficientSortedSet< T >::Item = T

the type of the items

Constructor and destructor documentation

◆ SpaceEfficientSortedSet() [1/2]

template<std::totally_ordered T>
ADS::SpaceEfficientSortedSet< T >::SpaceEfficientSortedSet ( std::size_t b = 32,
std::uint64_t seed = 1 )
explicit

the empty set, with blocks of \( b + 1 \) places

Precondition
\( b \ge 3 \)

◆ SpaceEfficientSortedSet() [2/2]

template<std::totally_ordered T>
ADS::SpaceEfficientSortedSet< T >::SpaceEfficientSortedSet ( const SpaceEfficientSortedSet< T > & )
delete

no copy, since the blocks are linked by pointers

◆ ~SpaceEfficientSortedSet()

template<std::totally_ordered T>
ADS::SpaceEfficientSortedSet< T >::~SpaceEfficientSortedSet ( )

frees the blocks

Member function documentation

◆ capacity()

template<std::totally_ordered T>
std::size_t ADS::SpaceEfficientSortedSet< T >::capacity ( void ) const
inlinenodiscard

the number of places of the blocks, \( n + O( n / b + b ) \)

◆ contains()

template<std::totally_ordered T>
bool ADS::SpaceEfficientSortedSet< T >::contains ( const T & x) const
nodiscard

true if \( x \) is an item, by lowerBound()

◆ empty()

template<std::totally_ordered T>
bool ADS::SpaceEfficientSortedSet< T >::empty ( void ) const
inlinenodiscard

true if there is no item

◆ find()

template<std::totally_ordered T>
const T * ADS::SpaceEfficientSortedSet< T >::find ( const T & x) const
nodiscard

the address of the item equal to \( x \), nil if there is none (the find(x) of a USet), valid until the next change, by lowerBound()

◆ forEach()

template<std::totally_ordered T>
template<class F>
void ADS::SpaceEfficientSortedSet< T >::forEach ( F && f) const

f( x ) on the items in increasing order

◆ insert()

template<std::totally_ordered T>
bool ADS::SpaceEfficientSortedSet< T >::insert ( const T & x)

\( x \) added, if it is not there, in \( O( \log n + b ) \) amortized expected time; true if it was not there

In pseudocode (exercise 4.13 of Morin, beyond the book), the blocks \( B_0 , B_1 , \ldots \) from the block of \( x \) on:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( x ) \cr 2 & \quad B_0 \gets \text{the block of } \mathrm{lowerBound}( x ) \text{, or the last one} \cr 3 & \quad \textbf{if}\ x \in B_0\ \textbf{then return} \cr 4 & \quad \textbf{if}\ |B_0| = b + 1\ \textbf{then} \cr 5 & \qquad r \gets \text{the least } r \ge 1 \text{ with } |B_r| \le b \text{ or } B_r = \textbf{nil} \text{, if } r < b \cr 6 & \qquad \textbf{if}\ r < b\ \textbf{and}\ B_r = \textbf{nil} \ \textbf{then}\ B_r \gets \text{a new last block} \cr 7 & \qquad \textbf{if}\ r < b\ \textbf{then}\ \textbf{for}\ j \gets r \ \textbf{downto}\ 1\ \textbf{do}\ \text{move the last item of } B_{j-1} \text{ to the front of } B_j \cr 8 & \qquad \textbf{else}\ B_b \gets \text{a new block};\ \textbf{for}\ j \gets b\ \textbf{downto}\ 1\ \textbf{do} \cr & \qquad\qquad \textbf{while}\ |B_j| < b\ \textbf{do}\ \text{move the } \text{last item of } B_{j-1} \text{ to the front of } B_j \cr 9 & \qquad \mathrm{insert}( x );\ \textbf{return} \cr 10 & \quad \text{put } x \text{ in its place in } B_0 \end{array}} \]

Postcondition
\( x \) is there

◆ lowerBound()

template<std::totally_ordered T>
const T * ADS::SpaceEfficientSortedSet< T >::lowerBound ( const T & x) const
nodiscard

the address of the smallest item not smaller than \( x \), nil if there is none (the find(x) of an SSet), valid until the next change, in \( O( \log n ) \) expected time

In pseudocode (exercise 4.13 of Morin, beyond the book), \( S \) the skip list of the blocks by their largest items:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBound}( x ) \cr 2 & \quad B \gets \text{the first block of } S \text{ whose largest } \text{item is} \ge x \cr 3 & \quad \textbf{if}\ B = \textbf{nil}\ \textbf{then return nil} \cr 4 & \quad \textbf{return}\ \text{the first item of } B \text{ not } \text{smaller than } x \text{, by binary search} \end{array}} \]

◆ operator=()

template<std::totally_ordered T>
SpaceEfficientSortedSet & ADS::SpaceEfficientSortedSet< T >::operator= ( const SpaceEfficientSortedSet< T > & )
delete

no copy, since the blocks are linked by pointers

◆ remove()

template<std::totally_ordered T>
bool ADS::SpaceEfficientSortedSet< T >::remove ( const T & x)

\( x \) removed, if it is there, in \( O( \log n + b ) \) amortized expected time: a block left with \( b - 2 \) items takes the first item of each next block up to the first one with more than \( b - 1 \) items, or the last one, within \( b \) blocks, and if there is none the items of the \( b \) blocks are gathered into the first \( b - 1 \), the last one freed (the reverse of insert())

In pseudocode (exercise 4.13 of Morin, beyond the book), the blocks \( B_0 , B_1 , \ldots \) from the block of \( x \) on:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( x ) \cr 2 & \quad B_0 \gets \text{the block of } \mathrm{lowerBound}( x ) \cr 3 & \quad \textbf{if}\ B_0 = \textbf{nil}\ \textbf{or}\ x \notin B_0 \ \textbf{then return} \cr 4 & \quad \text{take } x \text{ out of } B_0 \text{, freeing } B_0 \text{ if it is left empty} \cr 5 & \quad \textbf{if}\ |B_0| = b - 2\ \textbf{and}\ B_1 \ne \textbf{nil}\ \textbf{then} \cr 6 & \qquad r \gets \text{the least } r \ge 1 \text{ with } |B_r| \ne b - 1 \text{ or } B_{r+1} = \textbf{nil} \text{, if } r < b \cr 7 & \qquad \textbf{if}\ r < b\ \textbf{then for}\ j \gets 1 \ \textbf{to}\ r\ \textbf{do}\ \text{move the first item of } B_j \text{ to the end of } B_{j-1} \cr 8 & \qquad \textbf{else}\ \text{gather the items of } B_0 , \ldots , B_{b-1} \text{ into } B_0 , \ldots , B_{b-2} \text{, and free } B_{b-1} \cr 9 & \qquad \textbf{if}\ B_r \text{ is left empty}\ \textbf{then} \ \text{free } B_r \end{array}} \]

Postcondition
\( x \) is not there, true if it was

◆ size()

template<std::totally_ordered T>
std::size_t ADS::SpaceEfficientSortedSet< T >::size ( void ) const
inlinenodiscard

the number \( n \) of items


The documentation for this class was generated from the following file: