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

a set of integers of 32 bits as a Roaring bitmap More...

#include <ADS/set/RoaringBitmap.h>

Classes

class  const_iterator
 the iterator on the items in increasing order, forward More...

Public types

using Item = std::uint32_t
 the type of the items
using value_type = std::uint32_t
 the items
using size_type = std::size_t
 the sizes
using difference_type = std::ptrdiff_t
 the distances
using reference = std::uint32_t
 an item, by value
using const_reference = std::uint32_t
 an item, by value
using iterator = const_iterator
 the same, constant

Public member functions

 RoaringBitmap (void)=default
 the empty set
 RoaringBitmap (const RoaringBitmap &o)
 a copy, with containers of its own
 RoaringBitmap (RoaringBitmap &&o) noexcept=default
 the containers of o, which is left empty
RoaringBitmap & operator= (RoaringBitmap o) noexcept
 the containers of o, a copy or a move (the copy and swap idiom)
 RoaringBitmap (const std::uint32_t *a, std::size_t n)
 the set of the \( n \) strictly increasing items of \( a[ 0 .. n ) \), a container per chunk in one scan
bool contains (std::uint32_t x) const
 whether \( x \) is in the set
bool insert (std::uint32_t x)
 \( x \) into the set; true if it was not there
bool remove (std::uint32_t x)
 \( x \) out of the set; true if it was there
std::size_t size (void) const
 the number of items
bool empty (void) const
 whether the set has no item
void clear (void)
 all the items removed
void swap (RoaringBitmap &o) noexcept
 the items of this set and of o exchanged, \( O( 1 ) \)
std::size_t containers (void) const
 the containers \( c \)
std::size_t bitmaps (void) const
 the containers that are bitmaps
std::size_t bytes (void) const
 the bytes of the containers
template<class F>
void forEach (F &&f) const
 f( x ) on the items in increasing order
std::vector< std::uint32_t > items (void) const
 the items in increasing order
std::size_t rank (std::uint32_t x) const
 the items smaller than \( x \): the counts of the containers before that of \( x \), \( O( c ) \), and the place of \( x \) in it
std::size_t portableSizeInBytes (void) const
 the bytes of the set in the portable format of Roaring: 8, then 8 per container, 2 per item of an array and 8192 per bitmap
std::size_t writePortable (std::span< std::byte > out) const
 the set written at the start of \( \mathit{out} \) in the portable format of Roaring, with no container of runs; the bytes written
std::vector< std::byte > writePortable (void) const
 the set in the portable format of Roaring, in an array of its own
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()
const_iterator lowerBound (std::uint32_t x) const
 the first item not smaller than \( x \), end() if none
const_iterator select (std::size_t i) const
 the item of rank \( i \) (from 0), end() if \( i \ge \) size()

Static public member functions

static RoaringBitmap readPortable (std::span< const std::byte > in, std::size_t *used=nullptr)
 the set written in the portable format of Roaring at the start of \( \mathit{in} \), its containers of runs read as arrays or bitmaps; the bytes read in *used, if used is not null

Static public attributes

static constexpr std::uint32_t ARRAY = 4096
 the items of an array container at most

Friends

RoaringBitmap setUnion (const RoaringBitmap &A, const RoaringBitmap &B)
 the union, by the merge of the containers
RoaringBitmap setIntersection (const RoaringBitmap &A, const RoaringBitmap &B)
 the intersection, by the merge of the containers
RoaringBitmap setDifference (const RoaringBitmap &A, const RoaringBitmap &B)
 the difference, by the merge of the containers
RoaringBitmap setSymmetricDifference (const RoaringBitmap &A, const RoaringBitmap &B)
 the symmetric difference, by the merge of the containers

Detailed description

a set of integers of 32 bits as a Roaring bitmap

Member typedef documentation

◆ const_reference

using ADS::RoaringBitmap::const_reference = std::uint32_t

an item, by value

◆ difference_type

using ADS::RoaringBitmap::difference_type = std::ptrdiff_t

the distances

◆ Item

using ADS::RoaringBitmap::Item = std::uint32_t

the type of the items

◆ iterator

the same, constant

◆ reference

using ADS::RoaringBitmap::reference = std::uint32_t

an item, by value

◆ size_type

using ADS::RoaringBitmap::size_type = std::size_t

the sizes

◆ value_type

using ADS::RoaringBitmap::value_type = std::uint32_t

the items

Constructor and destructor documentation

◆ RoaringBitmap() [1/4]

ADS::RoaringBitmap::RoaringBitmap ( void )
default

the empty set

◆ RoaringBitmap() [2/4]

ADS::RoaringBitmap::RoaringBitmap ( const RoaringBitmap & o)

a copy, with containers of its own

◆ RoaringBitmap() [3/4]

ADS::RoaringBitmap::RoaringBitmap ( RoaringBitmap && o)
defaultnoexcept

the containers of o, which is left empty

◆ RoaringBitmap() [4/4]

ADS::RoaringBitmap::RoaringBitmap ( const std::uint32_t * a,
std::size_t n )

the set of the \( n \) strictly increasing items of \( a[ 0 .. n ) \), a container per chunk in one scan

Precondition
\( a \) strictly increasing (an audit)

Member function documentation

◆ begin()

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

the iterator on the smallest item

◆ bitmaps()

std::size_t ADS::RoaringBitmap::bitmaps ( void ) const
nodiscard

the containers that are bitmaps

◆ bytes()

std::size_t ADS::RoaringBitmap::bytes ( void ) const
nodiscard

the bytes of the containers

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ clear()

void ADS::RoaringBitmap::clear ( void )

all the items removed

◆ containers()

std::size_t ADS::RoaringBitmap::containers ( void ) const
nodiscard

the containers \( c \)

◆ contains()

bool ADS::RoaringBitmap::contains ( std::uint32_t x) const
nodiscard

whether \( x \) is in the set

In pseudocode (Chambi et al.), \( K \) the sorted chunks of the containers:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{contains}( x ) \cr 2 & \quad i \gets \text{the first place of } K \text{ not smaller than } \lfloor x / 2^{16} \rfloor \text{, by a binary search} \cr 3 & \quad \textbf{if}\ \text{there is none, or } K[ i ] \ne \lfloor x / 2^{16} \rfloor\ \textbf{then return false} \cr 4 & \quad v \gets x \bmod 2^{16} \cr 5 & \quad \textbf{if}\ \text{the container } i \text{ is a bitmap } \ \textbf{then return}\ \text{its bit } v \cr 6 & \quad \textbf{return}\ v \text{ is in its array, by a binary search} \end{array}} \]

◆ empty()

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

whether the set has no item

◆ end()

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

the iterator past the largest item

◆ forEach()

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

f( x ) on the items in increasing order

◆ insert()

bool ADS::RoaringBitmap::insert ( std::uint32_t x)

\( x \) into the set; true if it was not there

In pseudocode (Chambi et al.):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( x ) \cr 2 & \quad i \gets \text{the container of } \lfloor x / 2^{16} \rfloor \text{, as in contains} \cr 3 & \quad \textbf{if}\ \text{there is none }\ \textbf{then} \ \text{insert at } i \text{ the array } ( x \bmod 2^{16} ); \ \textbf{return true} \cr 4 & \quad \text{set the bit, or insert into the array, } x \bmod 2^{16} \cr 5 & \quad \textbf{if}\ \text{the array passed 4096 items } \ \textbf{then}\ \text{turn it into a bitmap} \cr 6 & \quad \textbf{return}\ \text{whether } x \text{ was added} \end{array}} \]

Postcondition
\( x \) is in the set

◆ items()

std::vector< std::uint32_t > ADS::RoaringBitmap::items ( void ) const
nodiscard

the items in increasing order

◆ lowerBound()

const_iterator ADS::RoaringBitmap::lowerBound ( std::uint32_t x) const
nodiscard

the first item not smaller than \( x \), end() if none

In pseudocode (Chambi et al.):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBound}( x ) \cr 2 & \quad i \gets \text{the first container whose chunk is not smaller } \text{than } \lfloor x / 2^{16} \rfloor \cr 3 & \quad \textbf{if}\ \text{the chunk of } i \text{ is } \lfloor x / 2^{16} \rfloor\ \textbf{then}\ \text{start at the first item of } i \text{ not smaller than } x \cr 4 & \quad \textbf{else}\ \text{start at the first item of } i \cr 5 & \quad \textbf{return}\ \text{the iterator there, moved to the next } \text{container while there is no item} \end{array}} \]

◆ operator=()

RoaringBitmap & ADS::RoaringBitmap::operator= ( RoaringBitmap o)
noexcept

the containers of o, a copy or a move (the copy and swap idiom)

◆ portableSizeInBytes()

std::size_t ADS::RoaringBitmap::portableSizeInBytes ( void ) const
nodiscard

the bytes of the set in the portable format of Roaring: 8, then 8 per container, 2 per item of an array and 8192 per bitmap

◆ rank()

std::size_t ADS::RoaringBitmap::rank ( std::uint32_t x) const
nodiscard

the items smaller than \( x \): the counts of the containers before that of \( x \), \( O( c ) \), and the place of \( x \) in it

In pseudocode (Chambi et al.), \( | X_j | \) the items of the container \( j \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rank}( x ) \cr 2 & \quad i \gets \text{the first container whose chunk is not smaller } \text{than } \lfloor x / 2^{16} \rfloor \cr 3 & \quad \rho \gets \sum_{j < i} | X_j | \cr 4 & \quad \textbf{if}\ \text{the chunk of } i \text{ is } \lfloor x / 2^{16} \rfloor\ \textbf{then}\ \rho \gets \rho + \text{the items of } X_i \text{ below } x \bmod 2^{16} \cr 5 & \quad \textbf{return}\ \rho \end{array}} \]

◆ readPortable()

RoaringBitmap ADS::RoaringBitmap::readPortable ( std::span< const std::byte > in,
std::size_t * used = nullptr )
staticnodiscard

the set written in the portable format of Roaring at the start of \( \mathit{in} \), its containers of runs read as arrays or bitmaps; the bytes read in *used, if used is not null

Exceptions
PersistErrorif \( \mathit{in} \) does not start with a set in that format: a cookie unknown, a field past the end, chunks or items not increasing, a count unlike the items

◆ remove()

bool ADS::RoaringBitmap::remove ( std::uint32_t x)

\( x \) out of the set; true if it was there

In pseudocode (Chambi et al.), the converse of insert():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( x ) \cr 2 & \quad i \gets \text{the container of } \lfloor x / 2^{16} \rfloor ;\ \textbf{if}\ \text{there is none }\ \textbf{then return false} \cr 3 & \quad \text{clear the bit, or remove from the array, } x \bmod 2^{16} \cr 4 & \quad \textbf{if}\ \text{the container is empty }\ \textbf{then} \ \text{remove it} \cr 5 & \quad \textbf{else if}\ \text{a bitmap fell to 4096 items } \ \textbf{then}\ \text{turn it into an array} \cr 6 & \quad \textbf{return}\ \text{whether } x \text{ was there} \end{array}} \]

Postcondition
\( x \) is not in the set

◆ select()

const_iterator ADS::RoaringBitmap::select ( std::size_t i) const
nodiscard

the item of rank \( i \) (from 0), end() if \( i \ge \) size()

In pseudocode (Chambi et al.), \( | X_j | \) the items of the container \( j \) and \( W_k \) the words of a bitmap:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{select}( i ) \cr 2 & \quad j \gets 0;\ \textbf{while}\ i \ge | X_j |\ \textbf{do}\ i \gets i - | X_j |;\ j \gets j + 1 \cr 3 & \quad \textbf{if}\ X_j \text{ is an array }\ \textbf{then}\ v \gets X_j[ i ] \cr 4 & \quad \textbf{else} \cr 5 & \qquad k \gets 0;\ \textbf{while}\ i \ge \mathrm{popcount}( W_k ) \ \textbf{do}\ i \gets i - \mathrm{popcount}( W_k );\ k \gets k + 1 \cr 6 & \qquad v \gets 64 k + \text{the place of the bit set of rank } i \text{ of } W_k \cr 7 & \quad \textbf{return}\ \mathrm{lowerBound}( 2^{16} \cdot \text{the chunk of } X_j + v ) \end{array}} \]

◆ size()

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

the number of items

◆ swap()

void ADS::RoaringBitmap::swap ( RoaringBitmap & o)
noexcept

the items of this set and of o exchanged, \( O( 1 ) \)

◆ writePortable() [1/2]

std::size_t ADS::RoaringBitmap::writePortable ( std::span< std::byte > out) const

the set written at the start of \( \mathit{out} \) in the portable format of Roaring, with no container of runs; the bytes written

Precondition
\( | \mathit{out} | \ge \) portableSizeInBytes()

◆ writePortable() [2/2]

std::vector< std::byte > ADS::RoaringBitmap::writePortable ( void ) const
nodiscard

the set in the portable format of Roaring, in an array of its own

◆ setDifference

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

the difference, by the merge of the containers

In pseudocode (Chambi et al.), the containers merged by their chunks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setDifference}( A , B ) \cr 2 & \quad C \gets \text{an empty set} \cr 3 & \quad \textbf{foreach}\ \text{chunk } k \text{ of } A \text{ or } B \text{, in increasing order}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \text{both have } k\ \textbf{then}\ X \gets \text{the difference of their two containers} \cr 5 & \qquad \textbf{else}\ X \gets \text{a copy of the container of } A \text{, if it has it} \cr 6 & \qquad \textbf{if}\ X \text{ is not empty }\ \textbf{then} \ \text{append } X \text{ to } C \cr 7 & \quad \textbf{return}\ C \end{array}} \]

◆ setIntersection

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

the intersection, by the merge of the containers

In pseudocode (Chambi et al.), the containers merged by their chunks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setIntersection}( A , B ) \cr 2 & \quad C \gets \text{an empty set} \cr 3 & \quad \textbf{foreach}\ \text{chunk } k \text{ of } A \text{ or } B \text{, in increasing order}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \text{both have } k\ \textbf{then}\ X \gets \text{the intersection of their two containers} \cr 5 & \qquad \textbf{else}\ X \gets \text{nothing} \cr 6 & \qquad \textbf{if}\ X \text{ is not empty }\ \textbf{then} \ \text{append } X \text{ to } C \cr 7 & \quad \textbf{return}\ C \end{array}} \]

◆ setSymmetricDifference

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

the symmetric difference, by the merge of the containers

In pseudocode (Chambi et al.), the containers merged by their chunks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setSymmetricDifference}( A , B ) \cr 2 & \quad C \gets \text{an empty set} \cr 3 & \quad \textbf{foreach}\ \text{chunk } k \text{ of } A \text{ or } B \text{, in increasing order}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \text{both have } k\ \textbf{then}\ X \gets \text{the symmetric difference of their two containers} \cr 5 & \qquad \textbf{else}\ X \gets \text{a copy of the container of the } \text{one that has it} \cr 6 & \qquad \textbf{if}\ X \text{ is not empty }\ \textbf{then} \ \text{append } X \text{ to } C \cr 7 & \quad \textbf{return}\ C \end{array}} \]

◆ setUnion

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

the union, by the merge of the containers

In pseudocode (Chambi et al.), the containers merged by their chunks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setUnion}( A , B ) \cr 2 & \quad C \gets \text{an empty set} \cr 3 & \quad \textbf{foreach}\ \text{chunk } k \text{ of } A \text{ or } B \text{, in increasing order}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \text{both have } k\ \textbf{then}\ X \gets \text{the union of their two containers} \cr 5 & \qquad \textbf{else}\ X \gets \text{a copy of the container of the } \text{one that has it} \cr 6 & \qquad \textbf{if}\ X \text{ is not empty }\ \textbf{then} \ \text{append } X \text{ to } C \cr 7 & \quad \textbf{return}\ C \end{array}} \]

Member data documentation

◆ ARRAY

std::uint32_t ADS::RoaringBitmap::ARRAY = 4096
staticconstexpr

the items of an array container at most


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