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

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()

Detailed description

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 unary code \( U( x ) = 0^{x-1} 1 \), \( x \) bits, optimal for \( 2^{-x} \);
  • Elias' \( \gamma( x ) = 0^{| B( x ) | - 1} B( x ) \), \( 2 \lfloor \log_2 x \rfloor + 1 \) bits, optimal for \( 1 / ( 2 x^2 ) \);
  • Elias' \( \delta( x ) = \gamma( | B( x ) | ) B( x ) \), \( 1 + 2 \lfloor \log_2 | B( x ) | \rfloor + | B( x ) | \) bits, \( 1 + o( 1 ) \) times the binary code; the same chapter keeps the leading 1 of \( B( x ) \), which Elias drops since the length tells it, and so do these functions (one bit more than Elias' code);
  • Rice's \( R_k( x ) = U( q + 1 ) B_k( r ) \), \( q = \lfloor ( x - 1 ) / 2^k \rfloor \) and \( r \) the remainder in \( k \) bits, \( q + 1 + k \) bits, and Golomb's code of modulus \( b \), of which Rice's is the case \( b = 2^k \), whose remainder is in the truncated binary code (\( \lfloor \log_2 b \rfloor \) or \( \lceil \log_2 b \rceil \) bits): optimal for the geometric distribution \( ( 1 - p )^{x-1} p \) with \( b \approx \ln 2 / p \) (Gallager and van Voorhis);
  • PForDelta, a block of values coded in \( b \) bits each after subtracting a base, the values out of \( [ \mathit{base} , \mathit{base} + 2^b - 2 ] \) being exceptions marked by the escape \( 2^b - 1 \) and written apart in 64 bits, with \( b \) chosen so that 90% of the values fit; it is decoded with no branch per value, the design of Zukowski, Heman, Nes and Boncz, whose exceptions are linked in place of the escapes, and of the later OptPFD of Yan, Ding and Suel, which chooses \( b \) per block;
  • the variable-byte code, \( B( x ) \) in groups of 7 bits from the most significant, each in a byte whose high bit is 1 except in the last, \( 8 \lceil | B( x ) | / 7 \rceil \) bits, and the \( ( s , c ) \)-dense codes of Brisaboa, Farina, Navarro and Parama, which split the \( s + c = 2^w \) values of a word of \( w \) bits into \( s \) stoppers and \( c \) continuers: \( s c^{k-1} \) integers take \( k \) words, and denseOptimalS() gives the \( s \) of the smallest output for given frequencies (the same chapter recalls that the minimum is unique); the variable-byte code has \( s = c = 128 \) but wastes the codes of the leading groups of zeros, which the dense code with \( s = c = 128 \) does not;
  • the interpolative code of Moffat and Stuiver, for a strictly increasing sequence in \( [ \mathit{low} , \mathit{hi} ] \): the middle item is written in \( \lceil \log_2( \mathit{hi} - \mathit{low} - r + l + 1 ) \rceil \) bits between the bounds left by the items around it, then the two halves recursively, no bit at all for a run of consecutive integers; it adapts to the clusters of the sequence and is not prefix-free, which is why it can beat Huffman's code, optimal only among the static prefix-free codes, at the price of a slow, recursive decoding.

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.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 11, Integer Coding, doi:10.1017/9781009128933
P. Elias, Universal codeword sets and representations of the integers, IEEE Transactions on Information Theory 21(2), 194-203, 1975, doi:10.1109/TIT.1975.1055349
S. W. Golomb, Run-length encodings, IEEE Transactions on Information Theory 12(3), 399-401, 1966, doi:10.1109/TIT.1966.1053907
R. G. Gallager, D. C. van Voorhis, Optimal source codes for geometrically distributed integer alphabets, IEEE Transactions on Information Theory 21(2), 228-230, 1975, doi:10.1109/TIT.1975.1055357
R. F. Rice, Some practical universal noiseless coding techniques, JPL Publication 79-22, Jet Propulsion Laboratory, 1979
M. Zukowski, S. Heman, N. Nes, P. Boncz, Super-scalar RAM-CPU cache compression, Proc. 22nd ICDE, 59, 2006, doi:10.1109/ICDE.2006.150
H. Yan, S. Ding, T. Suel, Inverted index compression and query processing with optimized document ordering, Proc. 18th WWW, 401-410, 2009, doi:10.1145/1526709.1526764
N. R. Brisaboa, A. Farina, G. Navarro, J. R. Parama, Lightweight natural language text compression, Information Retrieval 10(1), 1-33, 2007, doi:10.1007/s10791-006-9001-9
A. Moffat, L. Stuiver, Binary interpolative coding for effective index compression, Information Retrieval 3(1), 25-47, 2000, doi:10.1023/A:1013002601898
D. Lemire, L. Boytsov, Decoding billions of integers per second through vectorization, Software: Practice and Experience 45(1), 1-29, 2015, doi:10.1002/spe.2203
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa