|
ADS
Algorithms and Data Structures
|
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 ) ) \) | |
a set of integers in \( [ 0 , N ) \) as a boolean vector
| using ADS::BoolSet::const_iterator = SlotIterator< BoolSet , Places > |
the iterator, on the bits set in increasing order
| using ADS::BoolSet::const_reference = long |
an item, by value
| using ADS::BoolSet::difference_type = std::ptrdiff_t |
the distances
| using ADS::BoolSet::Item = long |
the type of the items
| using ADS::BoolSet::iterator = const_iterator |
the same, constant
| using ADS::BoolSet::reference = long |
an item, by value
| using ADS::BoolSet::size_type = std::size_t |
the sizes
| using ADS::BoolSet::value_type = long |
the items
|
explicit |
the empty set of the integers in \( [ 0 , N ) \), the Set( N ) of the book
|
inlinenodiscard |
the iterator on the smallest item
|
inlinenodiscard |
as begin()
|
inlinenodiscard |
as end()
|
nodiscard |
whether \( x \) is in the set, false also for an \( x \) out of \( [ 0 , N ) \), by reading its bit
|
nodiscard |
the size \( N \) of the vector
|
nodiscard |
whether the set has no item
|
inlinenodiscard |
the iterator past the largest item
| 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}} \]
| void ADS::BoolSet::insert | ( | long | x | ) |
\( x \) into the set, by setting its bit
|
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}} \]
|
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}} \]
| void ADS::BoolSet::remove | ( | long | x | ) |
\( x \) out of the set, by clearing its bit
|
nodiscard |
the number of items, kept by the operations, \( O( 1 ) \)
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}} \]
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}} \]
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}} \]
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}} \]