|
ADS
Algorithms and Data Structures
|
Rank and select on a static vector of bits \( B \) of \( m \) bits with \( n \) 1s (chapter 15, Compressed Data Structures, of Ferragina's book): \( \mathrm{rank}_b( i ) \), the number of the bits \( b \) in \( B[ 0 , i ) \), and \( \mathrm{select}_b( k ) \), the place of the \( k \)-th bit \( b \), the primitives on which every succinct structure of ADS is built (Elias-Fano, the trees by levels and by parentheses, the graphs). More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/combinatorial/Bitwise.h"#include "ADS/external/MappedFile.h"#include "ADS/parallel/Executor.h"#include "ADS/succinct/BitVector.h"#include <algorithm>#include <atomic>#include <bit>#include <cstddef>#include <cstdint>#include <limits>#include <memory>#include <mutex>#include <span>#include <string_view>#include <type_traits>#include <utility>#include <vector>#include "ADS/succinct/impl/RankSelect.tpp"Go to the source code of this file.
Classes | |
| struct | ADS::RankSelectOptions |
| the values whose select a directory answers: a structure that needs only rank, or one of the selects, spares the inventories of the others More... | |
| class | ADS::BasicRankSelect< Lazy > |
| a static vector of bits with rank and select in \( O( 1 ) \) time, the inventories of select built in the constructor if not Lazy, and at the first select of each value if Lazy More... | |
Namespaces | |
| namespace | ADS |
Typedefs | |
| using | ADS::RankSelect = BasicRankSelect< false > |
| the directory with its inventories of select built in the constructor, the fastest select | |
| using | ADS::LazyRankSelect = BasicRankSelect< true > |
| the directory with its inventories of select built at the first select of each value, the fastest construction (for a directory that may never select, or that selects only some values) | |
Rank and select on a static vector of bits \( B \) of \( m \) bits with \( n \) 1s (chapter 15, Compressed Data Structures, of Ferragina's book): \( \mathrm{rank}_b( i ) \), the number of the bits \( b \) in \( B[ 0 , i ) \), and \( \mathrm{select}_b( k ) \), the place of the \( k \)-th bit \( b \), the primitives on which every succinct structure of ADS is built (Elias-Fano, the trees by levels and by parentheses, the graphs).
The rank is the two-level directory of Jacobson, with the sizes that suit the words and the caches of today: for each block of \( Z = 256 \) bits, one word of 64 bits holds the absolute rank of the block in its 40 high bits and the ranks of its words 1, 2 and 3 relative to the block in 8 bits each, and the 1s of the word up to \( i \) are the sideways sum of the masked word, which takes the place of the table of the ranks of all the small blocks of the same chapter (a table of \( 2^z z \log z \) bits is affordable only with \( z = \log m / 2 \), while the sum of 64 bits is one instruction); one access to the directory and one to the bits, no branch, \( O( 1 ) \) time, with \( 64 / 256 = 25 \% \) of \( m \) bits more. The classical analysis sets \( Z = \log^2 m \) and \( z = \log m / 2 \), so that the directory takes \( o( m ) \) bits with the rank in \( O( 1 ) \) time; with constant blocks it takes a constant fraction, the trade of the practical designs: the rank9 of Vigna and the directory of Gog and Petri take the same 25% with two words per 512 bits, the poppy of Zhou, Andersen and Kaminsky and the flat rank of Kurpicz 3% with a sum of up to 8 words per query [see CompactRankSelect.h]. Above \( 2^{40} \) bits a third level of 64 bits counts every \( 2^{40} \) bits.
The select is the inventory of the simple select of Vigna, for the 1s and for the 0s: the 1s are cut in groups of \( 2^\ell \), with \( \ell = \lfloor \log_2( 1.5 \cdot 4096 \, k / m ) \rfloor \) for \( k \) of them, so that a group spans between 3072 and 6144 bits on average; for each the place of its first 1 is kept in 64 bits, and the places of its 1s of rank multiple of \( 2^{\ell - 5} \) in 16 bits from it, a line of the cache. \( \mathrm{select}_1( k ) \) reads the place of the 1 of rank \( k \) rounded down to \( 2^{\ell - 5} \), counts the rest of the 1s word by word, by the sideways sums, and ends in the word by the broadword selectInWord() [see BitVector.h]: three accesses to memory and a few words, \( O( 1 ) \) time where the 1s are spread evenly, with about \( 576 / 6144 \) to \( 576 / 3072 \), 9.4% to 18.8%, of \( m \) bits more for each value. A group that spans \( 2^{16} \) bits or more, whose offsets do not fit, is sparse, and its 1s are found by a binary search on the directory of rank between its ends, \( O( \log( m / 256 ) ) \) in the worst case. The same chapter gives the scheme of Clark, which splits the blocks of \( \log^2 m \) 1s into sparse ones, whose places are written down, and dense ones, split again with \( ( \log \log m )^2 \) 1s and ended by a table: \( O( 1 ) \) time in the worst case and \( o( m ) \) bits, at the price of three levels whose constants are large for the sizes of practice; the inventories with constant groups are what the practical designs use (Vigna; Gog and Petri; the darray of Okanohara and Sadakane). A group of at most 32 bits \( b \), below about 1% of them, keeps the place of each one, and the select reads no bit at all.
The directory is built in one pass on the words: the sideways sums \( p_0 , \ldots , p_3 \) of the words of a block, at most 64 each, are put in the bytes of a word, and one product gives their prefix sums in the bytes, which do not overflow,
\[ ( p_0 + 2^8 p_1 + 2^{16} p_2 ) \cdot ( 1 + 2^8 + 2^{16} ) \bmod 2^{24} = p_0 + 2^8 ( p_0 + p_1 ) + 2^{16} ( p_0 + p_1 + p_2 ) , \]
the three relative ranks of the entry; from words in memory the words are copied in the same pass, above \( 2^{23} \) bits, where reading them twice costs more than a copy in the caches. The inventories of select are built in the constructor (RankSelect, the default), or by the first select of each value (LazyRankSelect), in a pass on the directory, so that a structure that only ranks never pays for them; the first call builds an inventory once, whichever the threads that call it (with std::call_once, and a flag read with acquire semantics before it), and a copy keeps the inventories already built. The lazy form costs the select 10 to 15%: the load of the flag with acquire semantics makes the compiler read the fields of the inventory again at each query, which the eager form, with no flag at all (the template parameter Lazy), keeps in plain fields.
The places count from 0: \( \mathrm{rank}_1( i ) \) here is \( \mathrm{Rank}_1( i ) \) of Ferragina's book, which counts the places from 1, on the places \( 1 , \ldots , i \), and \( \mathrm{select}_1( k ) \) here is \( \mathrm{Select}_1( k ) - 1 \).
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 RankSelect.tpp.