ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::BoolSet Class Reference

a set of integers in \( [ 0 , N ) \) as a boolean vector More...

#include <ADS/set/BoolSet.h>

Classes

struct  Places
 the places of the table, for SlotIterator More...

Public types

using Item = long
 the type of the items
using value_type = long
 the items
using size_type = std::size_t
 the sizes
using difference_type = std::ptrdiff_t
 the distances
using reference = long
 an item, by value
using const_reference = long
 an item, by value
using const_iterator = SlotIterator< BoolSet , Places >
 the iterator, on the bits set in increasing order
using iterator = const_iterator
 the same, constant

Public member functions

 BoolSet (long N)
 the empty set of the integers in \( [ 0 , N ) \), the Set( N ) of the book
std::size_t size (void) const
 the number of items, kept by the operations, \( O( 1 ) \)
bool empty (void) const
 whether the set has no item
long dim (void) const
 the size \( N \) of the vector
bool contains (long x) const
 whether \( x \) is in the set, false also for an \( x \) out of \( [ 0 , N ) \), by reading its bit
void insert (long x)
 \( x \) into the set, by setting its bit
void remove (long x)
 \( x \) out of the set, by clearing its bit
std::optional< long > min (void) const
 the smallest item, none if the set is empty, \( O( N ) \) time
std::optional< long > max (void) const
 the largest item, none if the set is empty, \( O( N ) \) time
template<class F>
void forEach (F &&f) const
 f( x ) on the items in increasing order, \( O( N ) \) time
const_iterator begin (void) const
 the iterator on the smallest item
const_iterator end (void) const
 the iterator past the largest item
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()

Friends

BoolSet setUnion (const BoolSet &A, const BoolSet &B)
 the union, a set of dimension \( \max( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \)
BoolSet setIntersection (const BoolSet &A, const BoolSet &B)
 the intersection, a set of dimension \( \min( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \)
BoolSet setDifference (const BoolSet &A, const BoolSet &B)
 the difference, a set of dimension A.dim()
BoolSet setSymmetricDifference (const BoolSet &A, const BoolSet &B)
 the symmetric difference, the items in exactly one of \( A \) and \( B \), a set of dimension \( \max( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \)

Detailed description

a set of integers in \( [ 0 , N ) \) as a boolean vector

Member typedef documentation

◆ const_iterator

the iterator, on the bits set in increasing order

◆ const_reference

an item, by value

◆ difference_type

using ADS::BoolSet::difference_type = std::ptrdiff_t

the distances

◆ Item

using ADS::BoolSet::Item = long

the type of the items

◆ iterator

the same, constant

◆ reference

an item, by value

◆ size_type

using ADS::BoolSet::size_type = std::size_t

the sizes

◆ value_type

the items

Constructor and destructor documentation

◆ BoolSet()

ADS::BoolSet::BoolSet ( long N)
explicit

the empty set of the integers in \( [ 0 , N ) \), the Set( N ) of the book

Precondition
\( N \ge 0 \)

Member function documentation

◆ begin()

const_iterator ADS::BoolSet::begin ( void ) const
inlinenodiscard

the iterator on the smallest item

◆ cbegin()

const_iterator ADS::BoolSet::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

const_iterator ADS::BoolSet::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

bool ADS::BoolSet::contains ( long x) const
nodiscard

whether \( x \) is in the set, false also for an \( x \) out of \( [ 0 , N ) \), by reading its bit

◆ dim()

long ADS::BoolSet::dim ( void ) const
nodiscard

the size \( N \) of the vector

◆ empty()

bool ADS::BoolSet::empty ( void ) const
nodiscard

whether the set has no item

◆ end()

const_iterator ADS::BoolSet::end ( void ) const
inlinenodiscard

the iterator past the largest item

◆ forEach()

template<class F>
void ADS::BoolSet::forEach ( F && f) const

f( x ) on the items in increasing order, \( O( N ) \) time

In pseudocode (section 8.1), the bits set of each word taken from the lowest, each step clearing the lowest bit set:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{forEach}( f ) \cr 2 & \quad \textbf{foreach}\ \text{word } w = W[ k ] \text{, by increasing } k\ \textbf{do} \cr 3 & \qquad \textbf{while}\ w \ne 0\ \textbf{do}\ f( 64 k + \mathrm{ctz}( w ) );\ w \gets w \wedge ( w - 1 )\quad \triangleright \ \text{the lowest bit cleared} \end{array}} \]

◆ insert()

void ADS::BoolSet::insert ( long x)

\( x \) into the set, by setting its bit

Precondition
\( x \in [ 0 , N ) \)
Postcondition
\( x \) is in the set

◆ max()

std::optional< long > ADS::BoolSet::max ( void ) const
nodiscard

the largest item, none if the set is empty, \( O( N ) \) time

In pseudocode (section 8.1), the scan of min() from the last word, \( \mathrm{clz} \) the number of the leading zeros:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{max}() \cr 2 & \quad k \gets \text{the last word of } W \text{ that is not zero}; \ \textbf{if}\ \text{none}\ \textbf{then return nil} \cr 3 & \quad \textbf{return}\ 64 k + 63 - \mathrm{clz}( W[ k ] )\quad \triangleright\ \text{its highest bit set} \end{array}} \]

◆ min()

std::optional< long > ADS::BoolSet::min ( void ) const
nodiscard

the smallest item, none if the set is empty, \( O( N ) \) time

In pseudocode (section 8.1), a word \( W[ k ] \) of 64 booleans at a time, \( \mathrm{ctz} \) the number of its trailing zeros:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{min}() \cr 2 & \quad k \gets \text{the first word of } W \text{ that is not zero}; \ \textbf{if}\ \text{none}\ \textbf{then return nil} \cr 3 & \quad \textbf{return}\ 64 k + \mathrm{ctz}( W[ k ] )\quad \triangleright\ \text{its lowest bit set} \end{array}} \]

◆ remove()

void ADS::BoolSet::remove ( long x)

\( x \) out of the set, by clearing its bit

Postcondition
\( x \) is not in the set

◆ size()

std::size_t ADS::BoolSet::size ( void ) const
nodiscard

the number of items, kept by the operations, \( O( 1 ) \)

◆ setDifference

BoolSet setDifference ( const BoolSet & A,
const BoolSet & B )
friend

the difference, a set of dimension A.dim()

In pseudocode (section 8.1), a word of 64 booleans at a time, a word missing in the shorter vector being 0:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setDifference}( A , B ) \cr 2 & \quad C \gets \text{a set of dimension } \mathrm{dim}( A ) \cr 3 & \quad C.W[ k ] \gets A.W[ k ] \wedge \lnot B.W[ k ] \text{ for each word } k \text{ of } C;\ \textbf{return}\ C \end{array}} \]

◆ setIntersection

BoolSet setIntersection ( const BoolSet & A,
const BoolSet & B )
friend

the intersection, a set of dimension \( \min( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \)

In pseudocode (section 8.1), a word of 64 booleans at a time, a word missing in the shorter vector being 0:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setIntersection}( A , B ) \cr 2 & \quad C \gets \text{a set of dimension } \min( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \cr 3 & \quad C.W[ k ] \gets A.W[ k ] \wedge B.W[ k ] \text{ for each word } k \text{ of } C;\ \textbf{return}\ C \end{array}} \]

◆ setSymmetricDifference

BoolSet setSymmetricDifference ( const BoolSet & A,
const BoolSet & B )
friend

the symmetric difference, the items in exactly one of \( A \) and \( B \), a set of dimension \( \max( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \)

In pseudocode (exercise 8.5), a word of 64 booleans at a time, a word missing in the shorter vector being 0:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setSymmetricDifference}( A , B ) \cr 2 & \quad C \gets \text{a set of dimension } \max( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \cr 3 & \quad C.W[ k ] \gets A.W[ k ] \oplus B.W[ k ] \text{ for each word } k \text{ of } C;\ \textbf{return}\ C \end{array}} \]

◆ setUnion

BoolSet setUnion ( const BoolSet & A,
const BoolSet & B )
friend

the union, a set of dimension \( \max( \mathrm{dim}( A ) , \mathrm{dim}( B ) ) \)

In pseudocode (section 8.1), a word of 64 booleans at a time, a word missing in the shorter vector being 0:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setUnion}( A , B ) \cr 2 & \quad C \gets \text{a set of dimension } \max( \mathrm{dim}( A ) , \mathrm{dim}( B ) )\quad \triangleright \ \text{a missing word is zero} \cr 3 & \quad C.W[ k ] \gets A.W[ k ] \vee B.W[ k ] \text{ for each word } k \text{ of } C;\ \textbf{return}\ C \end{array}} \]


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