|
ADS
Algorithms and Data Structures
|
The compressed bitmap of Chambi, Lemire, Kaser and Godin (Roaring), beyond the book: a SET of integers of 32 bits [see ADT.h], the engineered counterpart of the boolean vector of section 8.1 [see BoolSet.h] for universes too large for a bit per integer and sets too dense for a sorted list [see VectorIntersection.h]. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/Simd.h"#include "ADS/external/MappedFile.h"#include "ADS/set/BoolSet.h"#include <cstddef>#include <bit>#include <cstdint>#include <iterator>#include <memory>#include <span>#include <vector>#include "ADS/set/impl/RoaringBitmap.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RoaringBitmap |
| a set of integers of 32 bits as a Roaring bitmap More... | |
| class | ADS::RoaringBitmap::const_iterator |
| the iterator on the items in increasing order, forward More... | |
Namespaces | |
| namespace | ADS |
Functions | |
| RoaringBitmap | ADS::setUnion (const RoaringBitmap &A, const RoaringBitmap &B) |
| the union, by the merge of the containers | |
| RoaringBitmap | ADS::setIntersection (const RoaringBitmap &A, const RoaringBitmap &B) |
| the intersection, by the merge of the containers | |
| RoaringBitmap | ADS::setDifference (const RoaringBitmap &A, const RoaringBitmap &B) |
| the difference, by the merge of the containers | |
| RoaringBitmap | ADS::setSymmetricDifference (const RoaringBitmap &A, const RoaringBitmap &B) |
| the symmetric difference, by the merge of the containers | |
The compressed bitmap of Chambi, Lemire, Kaser and Godin (Roaring), beyond the book: a SET of integers of 32 bits [see ADT.h], the engineered counterpart of the boolean vector of section 8.1 [see BoolSet.h] for universes too large for a bit per integer and sets too dense for a sorted list [see VectorIntersection.h].
The universe is cut into chunks of \( 2^{16} \) integers by their top 16 bits, and each chunk that holds some item is a container of the low 16 bits of its items, kept in a vector sorted by the top bits:
So no container takes more than 8 KB, and a set of \( n \) items takes at most about \( 2 n \) bytes and at most the bits of its universe, whichever is less. A search, an insertion and a removal find the container by a binary search with no branch on the chunks, kept apart in an array of their own, \( O( \log c ) \) for \( c \) containers, and then search an array in the same way (or test a bit), and shift it (at most 4096 items, a few lines of cache) or set a bit; an array that passes 4096 items becomes a bitmap and a bitmap that falls to 4096 an array. The operations of two sets merge the vectors of the containers by their chunks and combine the containers of the same chunk:
Compared with the literature: Roaring adds containers of runs (the intervals of consecutive integers, for sets with long runs) and the lazy unions of many bitmaps whose counts are computed at the end; the bitmaps of the other families (WAH, Concise, EWAH) compress runs of words and are slower to combine.
rank( x ), the items smaller than \( x \), sums the counts of the containers before that of \( x \) and adds the place of \( x \) in it, \( O( c + 4096 ) \); select( i ) finds the container of the rank \( i \) by the same counts and the item in it; lowerBound( x ) starts an iterator at the first item not smaller than \( x \).
The iterators visit the items in increasing order; an insertion or a removal invalidates them. The contracts [see Contract.h]: after insert() the item is there and after remove() it is not; the constructor from an array needs strictly increasing items, an audit; the invariant is that the containers are sorted by their chunks, not empty, arrays of at most 4096 sorted items and bitmaps of more, with their counts right, an \( O( n ) \) check at the level 3 on the sets of up to 4096 items. The operations are defined in RoaringBitmap.tpp.
The set is written and read in the portable format of Roaring, the one that CRoaring, the Java, Go and Rust implementations, and the systems built on them (Lucene, Spark, Druid, ClickHouse) share, so that a file written by any of them is read here and the converse. In that format, little-endian whatever the processor, the set is a cookie of 32 bits, the number \( c \) of the containers, \( c \) pairs of 16 bits (the chunk and the items less 1), \( c \) offsets of 32 bits from the start, and the containers in the order of their chunks: an array as its items of 16 bits, a bitmap as its 1024 words. A third kind of container, the sorted runs of consecutive items (an interval as its first item and its length less 1), is marked by a bit in a bitmap of \( \lceil c / 8 \rceil \) bytes after a cookie that also holds \( c - 1 \), and then the offsets are written only for \( c \ge 4 \); readPortable() reads such a container into an array or a bitmap, by its items, since this set has no runs, and writePortable() writes the first two kinds alone, with the cookie 12346 and the offsets. A reading checks the bounds of every field against the size of the bytes and the invariant of the containers (increasing chunks and items, counts equal to the items), throwing PersistError at the first one that fails, so that a truncated or corrupted input never reads past its end. The conversions to the bitmaps of CRoaring are in ADS/interop/croaring/CRoaring.h.