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

The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits are packed in words of 64 bits, the bit \( i \) being the bit \( i \bmod 64 \) (from the least significant one) of the word \( \lfloor i / 64 \rfloor \), so that a field of up to 64 bits is read or written with two shifts at most. More...

#include "ADS/Contract.h"
#include "ADS/SimdInline.h"
#include "ADS/combinatorial/Bitwise.h"
#include "ADS/external/MappedFile.h"
#include "ADS/memory/HugePageAllocator.h"
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/succinct/impl/BitVector.tpp"

Go to the source code of this file.

Classes

class  ADS::SubscriptIterator< O, T >
 the random-access constant iterator on the items T of the owner O, read by ( *o )[ i ] More...
class  ADS::BitVector
 a vector of bits packed in words of 64 bits More...

Namespaces

namespace  ADS

Typedefs

using ADS::Words = std::vector< Word , detail::LineAllocator< Word > >
 the words of a BitVector, on lines of the cache

Functions

constexpr int ADS::selectInWord (Word x, int k)
 the place of the \( k \)-th 1 of the word \( x \), counting \( k \) from 0 and the places from the least significant bit

Detailed description

The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits are packed in words of 64 bits, the bit \( i \) being the bit \( i \bmod 64 \) (from the least significant one) of the word \( \lfloor i / 64 \rfloor \), so that a field of up to 64 bits is read or written with two shifts at most.

Besides the random access to a bit, in \( O( 1 ) \) time, BitVector appends a bit or a field of \( \mathit{len} \) bits, push_back() and append(), in \( O( 1 ) \) amortized time, and reads a field of \( \mathit{len} \) bits, get(), in \( O( 1 ) \) time; the space is \( n + O( 512 ) \) bits for \( n \) bits, the vector of words growing as std::vector does. The words are whole lines of the cache of 64 bytes, with one word at least past the last bit, and they start on a line (on a huge page from 2 MB on, which spares the misses of the TLB of a random access); the bits past the size are 0 (the invariant). Hence count() and the rank of a word [see RankSelect.h] need no mask at the end, a field is read with no test of the end, and a directory may read the words of a line of its own. Unlike std::vector< bool >, whose layout is left to the implementation, BitVector exposes its words, which is what a rank directory or a code reading a word at a time needs.

selectInWord() gives the place of the \( k \)-th 1 of a word by the broadword method of Vigna: the sideways sums of the bytes [see Bitwise.h] are accumulated by one multiplication, the byte holding the \( k \)-th 1 is found by a comparison of the 8 bytes in parallel and the trailing zeros, and the 1 inside it in a table of 2 KB indexed by the byte and the rank, in \( O( 1 ) \) time, with no loop and no instruction of a particular processor; with ADS_WITH_HIGHWAY, on the targets whose compress of 64 bytes is one instruction (AVX-512 VBMI2), the places of the 1s are the indices of the lanes compressed by the mask of the word, and the \( k \)-th is read by a permutation of the bytes. It is the last step of every select in ADS, in place of the tables of the small blocks of the classical analysis (Jacobson, Clark). The kernels that the directories of rank and select share are here too, in detail: the 1s of a prefix of a line of 512 bits, one kernel with a variant on the vectors of Highway and a scalar one; the copy and the count of the words of a chunk and the size of the chunks of a parallel construction; and the checks of a select and of the ranks on the concept detail::RankSelectBits. SubscriptIterator is the random-access constant iterator of a structure read by its operator[], the bits of a BitVector or the items of a compressed sequence [see EliasFano.h].

The contracts [see Contract.h]: the places and the fields are in the vector, a field has at most 64 bits and its value fits in them, and selectInWord() needs \( k \) smaller than the 1s of the word; the invariant is that the bits past the size are 0. The functions are defined in BitVector.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 15, Compressed Data Structures, doi:10.1017/9781009128933
G. Jacobson, Space-efficient static trees and graphs, Proc. 30th FOCS, 549-554, 1989, doi:10.1109/SFCS.1989.63533
S. Vigna, Broadword implementation of rank/select queries, Proc. WEA 2008, LNCS 5038, 154-168, 2008, doi:10.1007/978-3-540-68552-4_12
S. Gog, M. Petri, Optimized succinct data structures for massive data, Software: Practice and Experience 44(11), 1287-1314, 2014, doi:10.1002/spe.2198
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa