|
ADS
Algorithms and Data Structures
|
The codes of the integers of chapter 11, Integer Coding, of Ferragina's book: self-delimiting binary codewords for a sequence of positive integers, short for the small ones, written one after the other on a stream of bits [see BitStream.h] or of bytes. More...
#include "ADS/Contract.h"#include "ADS/combinatorial/Bitwise.h"#include "ADS/succinct/BitStream.h"#include "ADS/succinct/BitVector.h"#include <algorithm>#include <bit>#include <cstddef>#include <cstdint>#include <limits>#include <vector>#include "ADS/compression/impl/IntegerCodes.tpp"Go to the source code of this file.
Classes | |
| struct | ADS::PForDeltaBlock |
| a block of values coded by PForDelta More... | |
Namespaces | |
| namespace | ADS |
Functions | |
| constexpr int | ADS::binaryLength (std::uint64_t x) |
| \( | B( x ) | \), the bits of the binary code of \( x \), \( 1 + \lfloor \log_2 x \rfloor \) | |
| constexpr std::uint64_t | ADS::signedToNatural (std::int64_t x) |
| \( x \) as a natural number: \( 2 x \) if \( x \ge 0 \), \( -2 x + 1 \) if \( x < 0 \) | |
| constexpr std::int64_t | ADS::naturalToSigned (std::uint64_t y) |
| the inverse of signedToNatural(): \( y / 2 \) if \( y \) is even, \( -( y - 1 ) / 2 \) if odd | |
| std::vector< std::uint64_t > | ADS::dGaps (const std::vector< std::uint64_t > &x) |
| the d-gaps of a non-decreasing sequence: the first item, then the differences between consecutive items | |
| std::vector< std::uint64_t > | ADS::prefixSums (const std::vector< std::uint64_t > &g) |
| the sequence of the d-gaps \( g \), i.e., their prefix sums | |
| void | ADS::encodeUnary (BitWriter &w, std::uint64_t x) |
| \( U( x ) \), \( x - 1 \) zeros and a 1 | |
| std::uint64_t | ADS::decodeUnary (BitReader &r) |
| the integer \( x \ge 1 \) whose \( U( x ) \) is next in \( r \): one plus the zeros before the next 1 | |
| constexpr std::uint64_t | ADS::unaryLength (std::uint64_t x) |
| the bits of \( U( x ) \), i.e., \( x \) | |
| void | ADS::encodeGamma (BitWriter &w, std::uint64_t x) |
| \( \gamma( x ) \), \( | B( x ) | - 1 \) zeros and \( B( x ) \) | |
| std::uint64_t | ADS::decodeGamma (BitReader &r) |
| the integer whose \( \gamma \) is next in \( r \) | |
| constexpr int | ADS::gammaLength (std::uint64_t x) |
| the bits of \( \gamma( x ) \), \( 2 \lfloor \log_2 x \rfloor + 1 \) | |
| void | ADS::encodeDelta (BitWriter &w, std::uint64_t x) |
| \( \delta( x ) \), \( \gamma( | B( x ) | ) \) and \( B( x ) \) | |
| std::uint64_t | ADS::decodeDelta (BitReader &r) |
| the integer whose \( \delta \) is next in \( r \) | |
| constexpr int | ADS::deltaLength (std::uint64_t x) |
| the bits of \( \delta( x ) \), \( 1 + 2 \lfloor \log_2 | B( x ) | \rfloor + | B( x ) | \) | |
| void | ADS::encodeRice (BitWriter &w, std::uint64_t x, int k) |
| \( R_k( x ) \), \( U( q + 1 ) \) and the remainder \( r \) in \( k \) bits, \( x - 1 = q 2^k + r \) | |
| std::uint64_t | ADS::decodeRice (BitReader &r, int k) |
| the integer whose \( R_k \) is next in \( r \): \( q 2^k + r + 1 \), \( q + 1 \) read in unary and \( r \) in \( k \) bits | |
| constexpr std::uint64_t | ADS::riceLength (std::uint64_t x, int k) |
| the bits of \( R_k( x ) \), \( \lfloor ( x - 1 ) / 2^k \rfloor + 1 + k \) | |
| void | ADS::encodeGolomb (BitWriter &w, std::uint64_t x, std::uint64_t b) |
| Golomb's code of modulus \( b \), \( U( q + 1 ) \) and the remainder \( r \) in the truncated binary code, \( x - 1 = q b + r \). | |
| std::uint64_t | ADS::decodeGolomb (BitReader &r, std::uint64_t b) |
| the integer whose Golomb's code of modulus \( b \) is next in \( r \) | |
| constexpr std::uint64_t | ADS::golombLength (std::uint64_t x, std::uint64_t b) |
| the bits of Golomb's code of \( x \) of modulus \( b \) | |
| int | ADS::pforDeltaWidth (const std::vector< std::uint64_t > &x, std::uint64_t base, double f=0.9) |
| the smallest \( b \) whose range \( [ \mathit{base} , \mathit{base} + 2^b - 2 ] \) holds at least the fraction \( f \) of the values (all of them if \( f = 1 \)), at most 63 | |
| PForDeltaBlock | ADS::encodePForDelta (const std::vector< std::uint64_t > &x, int b, std::uint64_t base=0) |
| the values \( x \) in slots of \( b \) bits after subtracting base | |
| std::vector< std::uint64_t > | ADS::decodePForDelta (const PForDeltaBlock &p) |
| the values of the block \( p \): each slot plus the base, or the next exception for an escape | |
| void | ADS::encodeVByte (std::vector< std::uint8_t > &out, std::uint64_t x) |
| the variable-byte code of \( x \) appended to out | |
| std::uint64_t | ADS::decodeVByte (const std::vector< std::uint8_t > &in, std::size_t &p) |
| the integer coded from \( \mathit{in}[ p ] \), \( p \) moved past it: the groups of 7 bits gathered up to the byte below \( 2^7 \) | |
| constexpr int | ADS::vbyteLength (std::uint64_t x) |
| the bytes of the variable-byte code of \( x \), \( \lceil | B( x ) | / 7 \rceil \), and 1 for \( x = 0 \) | |
| void | ADS::encodeDense (std::vector< std::uint32_t > &out, std::uint64_t x, std::uint32_t s, std::uint32_t c) |
| the \( ( s , c ) \)-dense code of \( x \) appended to out, a word per item: the continuers \( s , \ldots , s + c - 1 \) and then a stopper \( 0 , \ldots , s - 1 \) | |
| std::uint64_t | ADS::decodeDense (const std::vector< std::uint32_t > &in, std::size_t &p, std::uint32_t s, std::uint32_t c) |
| the integer coded from \( \mathit{in}[ p ] \), \( p \) moved past it | |
| constexpr int | ADS::denseLength (std::uint64_t x, std::uint32_t s, std::uint32_t c) |
| the words of the \( ( s , c ) \)-dense code of \( x \) | |
| std::uint32_t | ADS::denseOptimalS (const std::vector< std::uint64_t > &f, int bits=8) |
| the number \( s \) of stoppers, \( c = 2^{\mathit{bits}} - s \), that minimizes the words of the integers \( 0 , 1 , \ldots \) of frequencies \( f \), by trying each \( s \) | |
| void | ADS::encodeBounded (BitWriter &w, std::uint64_t x, std::uint64_t a, std::uint64_t b) |
| \( x - a \) in \( \lceil \log_2( b - a + 1 ) \rceil \) bits, the BinaryCode of chapter 11, Integer Coding, of Ferragina's book | |
| std::uint64_t | ADS::decodeBounded (BitReader &r, std::uint64_t a, std::uint64_t b) |
| the integer in \( [ a , b ] \) coded next in \( r \) by encodeBounded() | |
| void | ADS::encodeInterpolative (BitWriter &w, const std::vector< std::uint64_t > &x, std::uint64_t low, std::uint64_t hi) |
| the interpolative code of \( x \), which \( \mathit{low} \) and \( \mathit{hi} \) bound | |
| std::vector< std::uint64_t > | ADS::decodeInterpolative (BitReader &r, std::size_t n, std::uint64_t low, std::uint64_t hi) |
| the \( n \) integers coded in \( [ \mathit{low} , \mathit{hi} ] \), by the recursion of encodeInterpolative() with decodeBounded() in place of encodeBounded() | |
The codes of the integers of chapter 11, Integer Coding, of Ferragina's book: self-delimiting binary codewords for a sequence of positive integers, short for the small ones, written one after the other on a stream of bits [see BitStream.h] or of bytes.
Each code is optimal, by Shannon, for the distribution \( P[ x ] = 2^{-| c( x ) |} \), and the choice among them is the choice of the distribution of the data (e.g., the d-gaps of the lists of an inverted index, dGaps()). With \( B( x ) \) the binary code of \( x \), of \( | B( x ) | = 1 + \lfloor \log_2 x \rfloor \) bits:
The Elias-Fano code of the same chapter, which can be searched in place, is in EliasFano.h. signedToNatural() maps an integer of any sign to a natural number as the same chapter does (\( x \) to \( 2 x \), \( -x \) to \( 2 x + 1 \)), so that the codes of the positive integers apply to it.
Each code writes its bits with the most significant first, so that the stream equals the codeword on paper (\( \gamma( 9 ) = 0001001 \), \( R_4( 83 ) = 0000010010 \)), and has a function of its length, the formula of the same chapter, which the encoder ensures. The decoders read a word at a time [see BitStream.h]; the fast decoders of the literature (e.g., the tables of the unary parts, or the SIMD decoding of the variable-byte code by Lemire and Boytsov) are refinements of these.
The contracts [see Contract.h]: the integers are positive where the code needs it (unary, gamma, delta, Rice, Golomb), the parameters are in range (\( 0 \le k < 64 \), \( b \ge 1 \), \( s , c \ge 1 \)), the interpolative code gets a strictly increasing sequence in \( [ \mathit{low} , \mathit{hi} ] \), and each encoder ensures that it wrote as many bits as its length function says; the decoding of an encoding is the audit of PForDelta. The functions are defined in IntegerCodes.tpp.