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

The streams of bits on which the integer codes are written and read [see IntegerCodes.h]: BitWriter appends bits to a BitVector [see BitVector.h] and BitReader reads them back from a place that moves forward, both in \( O( 1 ) \) time per field of up to 64 bits. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/combinatorial/Bitwise.h"
#include "ADS/succinct/BitVector.h"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <utility>
#include "ADS/succinct/impl/BitStream.tpp"

Go to the source code of this file.

Classes

class  ADS::BitWriter
 a stream of bits written at its end, on a BitVector More...
class  ADS::BitReader
 a stream of bits read forward from a place of a BitVector, which must outlive the reader More...

Namespaces

namespace  ADS

Detailed description

The streams of bits on which the integer codes are written and read [see IntegerCodes.h]: BitWriter appends bits to a BitVector [see BitVector.h] and BitReader reads them back from a place that moves forward, both in \( O( 1 ) \) time per field of up to 64 bits.

  • write( v , len ) and read( len ) move a field with its least significant bit first, the order of the words, with two shifts;
  • writeBinary( v , len ) and readBinary( len ) move it with its most significant bit first, the order in which the codes are written on paper (\( \gamma( 9 ) = 0001001 \)), so that the bits of the stream read from the first one are the codewords of chapter 11, Integer Coding, of Ferragina's book; the field is turned around by three rounds of masks and a swap of the bytes, in a constant expression as well;
  • writeUnary( q ) writes \( q \) zeros and a 1, the unary code \( U( q + 1 ) \) of the same chapter, and readUnary() counts the zeros up to the 1 a word at a time, by the trailing zeros of the word, rather than a bit at a time, in \( O( 1 + q / 64 ) \) time.

Writing the codes into words of 64 bits, rather than bytes, is what the fast decoders of the literature do (e.g., the bit streams of the WebGraph framework of Boldi and Vigna and of the succinct libraries): a codeword of up to 64 bits spans at most two words, and the unary part is decoded with one instruction per word.

The contracts [see Contract.h]: a field has at most 64 bits and its value fits in them, and a reader never reads past the end of its bits. The functions are defined in BitStream.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 11, Integer Coding, doi:10.1017/9781009128933
P. Boldi, S. Vigna, The WebGraph framework I: compression techniques, Proc. 13th WWW, 595-602, 2004, doi:10.1145/988672.988752
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa