ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RoaringBitmap.h File Reference

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

Detailed description

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:

  • an array container, the sorted array of the low bits, when the chunk holds at most 4096 items (2 bytes per item, at most 8 KB);
  • a bitmap container, the \( 2^{16} \) bits of the chunk in 1024 words (8 KB), when it holds more.

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:

  • two bitmaps word by word, the bits of the result counted in the same pass (by the vectors of the processor with Highway [see Simd.h]), a result of at most 4096 items then written as an array;
  • a bitmap and an array by testing, setting, clearing or flipping the bits of the array in a copy of the bitmap;
  • two arrays, with ADS_WITH_HIGHWAY, by blocks of \( L \le 8 \) items, \( L \) the lanes of 16 bits of the vectors of the processor found at run time [see Simd.h]: for the intersection and the difference, a block of each array compared all against all by the comparisons with its \( L \) rotations (the method of Schlegel, Willhalm and Lehner, who use one comparison of strings of SSE4.2), the items of the block of the first array found, or those not found in any block of the second one, compressed and stored, and the block that ends first moving on; for the union, the two blocks merged by the network of minima and maxima of Inoue and Taura, the \( L \) smallest written but those equal to the one before them, and the next block taken from the array with the smaller head; for the symmetric difference, the union of the two differences (by a merge with no branch on short arrays); a union or a symmetric difference of more than 4096 items becoming a bitmap.

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.

See also
S. Chambi, D. Lemire, O. Kaser, R. Godin, Better bitmap performance with Roaring bitmaps, Software: Practice and Experience 46(5), 709-719, 2016, doi:10.1002/spe.2325
D. Lemire, O. Kaser, N. Kurz, L. Deri, C. O'Hara, F. Saint-Jacques, G. Ssi-Yan-Kai, Roaring bitmaps: implementation of an optimized software library, Software: Practice and Experience 48(4), 867-895, 2018, doi:10.1002/spe.2560
The Roaring format specification, https://github.com/RoaringBitmap/RoaringFormatSpec
B. Schlegel, T. Willhalm, W. Lehner, Fast sorted-set intersection using SIMD instructions, Proceedings of ADMS 2011, 1-8, 2011
H. Inoue, K. Taura, SIMD- and cache-friendly algorithm for sorting an array of structures, Proceedings of the VLDB Endowment 8(11), 1274-1285, 2015, doi:10.14778/2809974.2809988
W. Muła, N. Kurz, D. Lemire, Faster population counts using AVX2 instructions, The Computer Journal 61(1), 111-120, 2018, doi:10.1093/comjnl/bxx046
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa