|
ADS
Algorithms and Data Structures
|
The codes of the integers of chapter 11, Integer Coding, of Ferragina's book. More...
Files | |
| file | ADS/compression/IntegerCodes.h |
| 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. | |
Classes | |
| struct | ADS::PForDeltaBlock |
| a block of values coded by PForDelta More... | |
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.
The definitions, the invariants and the costs of the area are in Theory of compression.
|
nodiscardconstexpr |
\( | B( x ) | \), the bits of the binary code of \( x \), \( 1 + \lfloor \log_2 x \rfloor \)
|
inlinenodiscard |
the integer in \( [ a , b ] \) coded next in \( r \) by encodeBounded()
|
inlinenodiscard |
the integer whose \( \delta \) is next in \( r \)
In pseudocode (Elias' delta code, chapter 11, Integer Coding, of Ferragina's book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decodeDelta}( r ) \cr 2 & \quad L \gets \mathrm{decodeGamma}( r );\ \textbf{return} \ \text{the next } L \text{ bits, as a binary number} \end{array}} \]
|
inlinenodiscard |
the integer coded from \( \mathit{in}[ p ] \), \( p \) moved past it
In pseudocode (the dense codes, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decodeDense}( \mathit{in} , p , s , c ) \cr 2 & \quad y \gets 0 \cr 3 & \quad \textbf{loop} \cr 4 & \qquad d \gets \mathit{in}[ p ];\ p \gets p + 1 \cr 5 & \qquad \textbf{if}\ d < s\ \textbf{then return}\ y s + d \cr 6 & \qquad y \gets y c + ( d - s ) + 1 \end{array}} \]
|
inlinenodiscard |
the integer whose \( \gamma \) is next in \( r \)
In pseudocode (Elias' gamma code, chapter 11, Integer Coding, of Ferragina's book), the zeros giving the length:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decodeGamma}( r ) \cr 2 & \quad L \gets \text{the zeros before the next 1} \cr 3 & \quad \textbf{return}\ 2^L + \text{the next } L \text{ bits, as a} \text{ binary number} \end{array}} \]
|
inlinenodiscard |
the integer whose Golomb's code of modulus \( b \) is next in \( r \)
In pseudocode (Golomb's code, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decodeGolomb}( r , b ) \cr 2 & \quad q \gets \mathrm{decodeUnary}( r ) - 1;\ v \gets 0 \cr 3 & \quad \textbf{if}\ b > 1\ \textbf{then} \cr 4 & \qquad c \gets \lceil \log_2 b \rceil;\ t \gets 2^c - b; \ v \gets \text{the next } c - 1 \text{ bits} \cr 5 & \qquad \textbf{if}\ v \ge t\ \textbf{then}\ v \gets 2 v + \text{the next bit} - t \cr 6 & \quad \textbf{return}\ q b + v + 1 \end{array}} \]
|
inlinenodiscard |
the \( n \) integers coded in \( [ \mathit{low} , \mathit{hi} ] \), by the recursion of encodeInterpolative() with decodeBounded() in place of encodeBounded()
|
inlinenodiscard |
the values of the block \( p \): each slot plus the base, or the next exception for an escape
|
inlinenodiscard |
the integer whose \( R_k \) is next in \( r \): \( q 2^k + r + 1 \), \( q + 1 \) read in unary and \( r \) in \( k \) bits
|
inlinenodiscard |
the integer \( x \ge 1 \) whose \( U( x ) \) is next in \( r \): one plus the zeros before the next 1
|
inlinenodiscard |
the integer coded from \( \mathit{in}[ p ] \), \( p \) moved past it: the groups of 7 bits gathered up to the byte below \( 2^7 \)
|
nodiscardconstexpr |
the bits of \( \delta( x ) \), \( 1 + 2 \lfloor \log_2 | B( x ) | \rfloor + | B( x ) | \)
|
nodiscardconstexpr |
the words of the \( ( s , c ) \)-dense code of \( x \)
|
inlinenodiscard |
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 \)
In pseudocode (the dense codes, beyond the book), in \( O( 2^{\mathit{bits}} |f| \log |f| ) \) time:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{denseOptimalS}( f , \mathit{bits} ) \cr 2 & \quad W \gets 2^{\mathit{bits}} \cr 3 & \quad \textbf{return}\ \text{the least } s \in [ 1 , W ) \text{ that} \text{ minimizes } \sum_x f[ x ] \cdot \mathrm{denseLength}( x , s , W - s ) \end{array}} \]
|
inlinenodiscard |
the d-gaps of a non-decreasing sequence: the first item, then the differences between consecutive items
|
inline |
\( x - a \) in \( \lceil \log_2( b - a + 1 ) \rceil \) bits, the BinaryCode of chapter 11, Integer Coding, of Ferragina's book
|
inline |
\( \delta( x ) \), \( \gamma( | B( x ) | ) \) and \( B( x ) \)
In pseudocode (Elias' delta code, chapter 11, Integer Coding, of Ferragina's book, the leading 1 of \( B( x ) \) kept):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeDelta}( w , x ) \cr 2 & \quad L \gets | B( x ) |;\ \mathrm{encodeGamma}( w , L ); \ \text{write the } L \text{ bits of } x \end{array}} \]
|
inline |
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 \)
In pseudocode (the dense codes of Brisaboa, Farina, Navarro and Parama, beyond the book), the words found from the last:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeDense}( \mathit{out} , x , s , c ) \cr 2 & \quad C \gets ( x \bmod s );\ y \gets \lfloor x / s \rfloor \cr 3 & \quad \textbf{while}\ y > 0\ \textbf{do}\ y \gets y - 1;\ C \gets ( s + y \bmod c ) \cdot C;\ y \gets \lfloor y / c \rfloor \cr 4 & \quad \text{append } C \text{ to } \mathit{out} \end{array}} \]
|
inline |
\( \gamma( x ) \), \( | B( x ) | - 1 \) zeros and \( B( x ) \)
In pseudocode (Elias' gamma code, chapter 11, Integer Coding, of Ferragina's book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeGamma}( w , x ) \cr 2 & \quad L \gets \lfloor \log_2 x \rfloor \cr 3 & \quad \text{write } L \text{ zeros};\ \text{write the } L + 1 \text{ bits of } x \text{, the most significant first} \end{array}} \]
|
inline |
Golomb's code of modulus \( b \), \( U( q + 1 ) \) and the remainder \( r \) in the truncated binary code, \( x - 1 = q b + r \).
In pseudocode (Golomb's code, beyond the book), the truncated binary code giving the first \( t \) remainders one bit less:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeGolomb}( w , x , b ) \cr 2 & \quad q \gets \lfloor ( x - 1 ) / b \rfloor;\ r \gets ( x - 1 ) \bmod b;\ \mathrm{encodeUnary}( w , q + 1 ) \cr 3 & \quad \textbf{if}\ b > 1\ \textbf{then} \cr 4 & \qquad c \gets \lceil \log_2 b \rceil;\ t \gets 2^c - b \cr 5 & \qquad \textbf{if}\ r < t\ \textbf{then}\ \text{write } r \text{ in } c - 1 \text{ bits}\ \textbf{else}\ \text{write } r + t \text{ in } c \text{ bits} \end{array}} \]
|
inline |
the interpolative code of \( x \), which \( \mathit{low} \) and \( \mathit{hi} \) bound
In pseudocode (the interpolative code of Moffat and Stuiver, chapter 11, Integer Coding, of Ferragina's book), on \( x[ l .. r - 1 ] \), starting from \( l = 0 \) and \( r = |x| \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{interpolative}( w , x , l , r , \mathit{low} , \mathit{hi} ) \cr 2 & \quad \textbf{if}\ l \ge r\ \textbf{then return} \cr 3 & \quad m \gets \lfloor ( l + r - 1 ) / 2 \rfloor \cr 4 & \quad \mathrm{encodeBounded}( w , x[ m ] , \mathit{low} + m - l , \mathit{hi} - ( r - 1 - m ) ) \cr 5 & \quad \mathrm{interpolative}( w , x , l , m , \mathit{low} , x[ m ] - 1 ) \cr 6 & \quad \mathrm{interpolative}( w , x , m + 1 , r , x[ m ] + 1 , \mathit{hi} ) \end{array}} \]
|
inlinenodiscard |
the values \( x \) in slots of \( b \) bits after subtracting base
In pseudocode (PForDelta, chapter 11, Integer Coding, of Ferragina's book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{encodePForDelta}( x , b , \mathit{base} ) \cr 2 & \quad e \gets 2^b - 1\quad \triangleright\ \text{the escape} \cr 3 & \quad \textbf{foreach}\ v \in x\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathit{base} \le v < \mathit{base} + e \ \textbf{then}\ \text{append } v - \mathit{base} \text{ in } b \text{ bits to the slots} \cr 5 & \qquad \textbf{else}\ \text{append } e \text{ to the slots, and } v \text{ to the exceptions} \cr 6 & \quad \textbf{return}\ ( b , \mathit{base} , \text{the slots, the } \text{exceptions} ) \end{array}} \]
|
inline |
\( R_k( x ) \), \( U( q + 1 ) \) and the remainder \( r \) in \( k \) bits, \( x - 1 = q 2^k + r \)
In pseudocode (Rice's code, chapter 11, Integer Coding, of Ferragina's book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeRice}( w , x , k ) \cr 2 & \quad q \gets \lfloor ( x - 1 ) / 2^k \rfloor;\ r \gets ( x - 1 ) \bmod 2^k \cr 3 & \quad \mathrm{encodeUnary}( w , q + 1 );\ \text{write the } k \text{ bits of } r \end{array}} \]
|
inline |
\( U( x ) \), \( x - 1 \) zeros and a 1
|
inline |
the variable-byte code of \( x \) appended to out
In pseudocode (the variable-byte code, chapter 11, Integer Coding, of Ferragina's book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeVByte}( \mathit{out} , x ) \cr 2 & \quad k \gets \max( 1 , \lceil | B( x ) | / 7 \rceil ) \cr 3 & \quad \textbf{for}\ i \gets k - 1\ \textbf{downto}\ 0\ \textbf{do} \ \text{append the byte } \lfloor x / 2^{7 i} \rfloor \bmod 2^7 + ( 2^7 \text{ if } i > 0 ) \end{array}} \]
|
nodiscardconstexpr |
the bits of \( \gamma( x ) \), \( 2 \lfloor \log_2 x \rfloor + 1 \)
|
nodiscardconstexpr |
the bits of Golomb's code of \( x \) of modulus \( b \)
|
nodiscardconstexpr |
the inverse of signedToNatural(): \( y / 2 \) if \( y \) is even, \( -( y - 1 ) / 2 \) if odd
|
inlinenodiscard |
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
|
inlinenodiscard |
the sequence of the d-gaps \( g \), i.e., their prefix sums
|
nodiscardconstexpr |
the bits of \( R_k( x ) \), \( \lfloor ( x - 1 ) / 2^k \rfloor + 1 + k \)
|
nodiscardconstexpr |
\( x \) as a natural number: \( 2 x \) if \( x \ge 0 \), \( -2 x + 1 \) if \( x < 0 \)
|
nodiscardconstexpr |
the bits of \( U( x ) \), i.e., \( x \)
|
nodiscardconstexpr |
the bytes of the variable-byte code of \( x \), \( \lceil | B( x ) | / 7 \rceil \), and 1 for \( x = 0 \)