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

Rank and select on a static vector of bits \( B \) of \( m \) bits in 3.125% of \( m \) bits more for the rank and less than 0.8% for the select of each value, the compact counterpart of RankSelect [see RankSelect.h], which spends about 50% for the fastest queries: the flat directory of Kurpicz, after the poppy of Zhou, Andersen and Kaminsky. More...

#include "ADS/Contract.h"
#include "ADS/external/MappedFile.h"
#include "ADS/parallel/Executor.h"
#include "ADS/succinct/BitVector.h"
#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>
#include <string_view>
#include <vector>
#include "ADS/succinct/impl/CompactRankSelect.tpp"

Go to the source code of this file.

Classes

class  ADS::CompactRankSelect
 a static vector of bits with rank and select in 3.125% of its bits more for the rank and less than 0.8% for each select More...

Namespaces

namespace  ADS

Detailed description

Rank and select on a static vector of bits \( B \) of \( m \) bits in 3.125% of \( m \) bits more for the rank and less than 0.8% for the select of each value, the compact counterpart of RankSelect [see RankSelect.h], which spends about 50% for the fastest queries: the flat directory of Kurpicz, after the poppy of Zhou, Andersen and Kaminsky.

The bits are cut in superblocks of 4096 bits, each one in 8 blocks of 512 bits, a line of the cache [see BitVector.h]. One entry of 128 bits per superblock holds the 1s before it since its top region of \( 2^{32} \) bits, in 32 bits, and the 1s before each of its blocks since the superblock, \( c_0 = 0 , c_1 , \ldots , c_7 \le 3584 \), in 12 bits each; a count of 64 bits per top region completes the rank. Hence

\[ \mathrm{rank}_1( i ) = T[ \lfloor i / 2^{32} \rfloor ] + S[ \lfloor i / 4096 \rfloor ] + c_{\lfloor i / 512 \rfloor \bmod 8} + \sum_{j < 8} | w_j \wedge ( 2^{\min( 64 , \max( 0 , o - 64 j ) )} - 1 ) | , \]

with \( o = i \bmod 512 \), \( w_j \) the words of the block and \( | x | \) the 1s of the word \( x \): one access to the entries, one to the line of the block, and the sideways sums of its 8 words cut at \( i \), with no branch (with AVX2, the bytes of the line before the one of \( i \) counted 32 at a time by a table of the 16 nibbles, the method of Mula, Kurz and Lemire; both variants are the one kernel detail::lineRank() of BitVector.h); \( 128 / 4096 = 3.125 \% \) of \( m \) bits.

The select keeps the superblock of every 4096-th bit \( b \), 32 bits each, at most \( 32 / 4096 < 0.8 \% \) of \( m \) bits for each value, and finds the \( k \)-th bit \( b \) by a binary search on the entries between two samples, then the block by the 7 counts of the superblock and the word by the sums of the 8 words of its line, both by comparisons with no branch, and the bit by selectInWord(); \( O( \log( m / 4096 ) ) \) steps in the worst case and \( O( 1 ) \) where the bits \( b \) are spread evenly (a sample every 2 to 8 superblocks for a density between 1/2 and 1/8). The trade with RankSelect is the one of the literature: a sum of up to 8 words and a search of the entries instead of one word and an inventory, in 15 times fewer bits.

The directory and the samples are built in one pass on the words, a superblock at a time with no test on the words that exist; from words in memory the words are copied in the same pass, above \( 2^{23} \) bits, as in RankSelect.

The contracts [see Contract.h]: \( i \le m \) for the rank and \( 1 \le k \) up to the number of the bits \( b \) for the select; the postconditions are that the bit at \( \mathrm{select}_b( k ) \) is \( b \) and that its rank is \( k - 1 \), and the audit of the construction is that the directory agrees with a scan. The functions are defined in CompactRankSelect.tpp.

See also
F. Kurpicz, Engineering compact data structures for rank and select queries on bit vectors, Proc. 29th SPIRE, LNCS 13617, 257-272, 2022, doi:10.1007/978-3-031-20643-6_19
D. Zhou, D. G. Andersen, M. Kaminsky, Space-efficient, high-performance rank and select structures on uncompressed bit sequences, Proc. 12th SEA, LNCS 7933, 151-163, 2013, doi:10.1007/978-3-642-38527-8_15
G. Navarro, Compact Data Structures, Cambridge University Press, 2016, chapter 4, doi:10.1017/CBO9781316588284
W. Mula, N. Kurz, D. Lemire, Faster population counts using AVX2 instructions, The Computer Journal 61(1), 111-120, 2018, doi:10.1093/comjnl/bxx046
P. Pandey, M. A. Bender, R. Johnson, A fast x86 implementation of select, arXiv:1706.00990, 2017
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa