ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
IntegerCodes.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File IntegerCodes.h --------------------------*/
3/*--------------------------------------------------------------------------*/
119/*--------------------------------------------------------------------------*/
120/*----------------------------- DEFINITIONS --------------------------------*/
121/*--------------------------------------------------------------------------*/
122
123#ifndef __ADS_IntegerCodes
124 #define __ADS_IntegerCodes
125/* self-identification: #endif constant */
126
127/*--------------------------------------------------------------------------*/
128/*------------------------------ INCLUDES ----------------------------------*/
129/*--------------------------------------------------------------------------*/
130
131#include "ADS/Contract.h"
135
136#include <algorithm>
137#include <bit>
138#include <cstddef>
139#include <cstdint>
140#include <limits>
141#include <vector>
142
143/*--------------------------------------------------------------------------*/
144/*--------------------------- NAMESPACE ------------------------------------*/
145/*--------------------------------------------------------------------------*/
146
147namespace ADS
148{
149
150/*---------------------------- PREPROCESSING -------------------------------*/
151
155[[nodiscard]] constexpr int binaryLength( std::uint64_t x );
156
160[[nodiscard]] constexpr std::uint64_t signedToNatural( std::int64_t x );
161
164[[nodiscard]] constexpr std::int64_t naturalToSigned( std::uint64_t y );
165
169[[nodiscard]] inline std::vector< std::uint64_t >
170dGaps( const std::vector< std::uint64_t > & x );
171
173[[nodiscard]] inline std::vector< std::uint64_t >
174prefixSums( const std::vector< std::uint64_t > & g );
175
176/*------------------------------ UNARY, GAMMA, DELTA -----------------------*/
177
181inline void encodeUnary( BitWriter & w , std::uint64_t x );
182
185[[nodiscard]] inline std::uint64_t decodeUnary( BitReader & r );
186
188[[nodiscard]] constexpr std::uint64_t unaryLength( std::uint64_t x );
189
204inline void encodeGamma( BitWriter & w , std::uint64_t x );
205
219[[nodiscard]] inline std::uint64_t decodeGamma( BitReader & r );
220
223[[nodiscard]] constexpr int gammaLength( std::uint64_t x );
224
238inline void encodeDelta( BitWriter & w , std::uint64_t x );
239
252[[nodiscard]] inline std::uint64_t decodeDelta( BitReader & r );
253
257[[nodiscard]] constexpr int deltaLength( std::uint64_t x );
258
259/*------------------------------ RICE, GOLOMB ------------------------------*/
260
277inline void encodeRice( BitWriter & w , std::uint64_t x , int k );
278
283[[nodiscard]] inline std::uint64_t decodeRice( BitReader & r , int k );
284
288[[nodiscard]] constexpr std::uint64_t riceLength( std::uint64_t x , int k );
289
309inline void encodeGolomb( BitWriter & w , std::uint64_t x , std::uint64_t b );
310
327[[nodiscard]] inline std::uint64_t decodeGolomb( BitReader & r ,
328 std::uint64_t b );
329
332[[nodiscard]] constexpr std::uint64_t golombLength( std::uint64_t x ,
333 std::uint64_t b );
334
335/*-------------------------------- PFORDELTA -------------------------------*/
336
339{
340 int b = 1;
341 std::uint64_t base = 0;
343 std::vector< std::uint64_t > exceptions;
344
346 [[nodiscard]] std::size_t size( void ) const;
347
349 [[nodiscard]] std::size_t codeBits( void ) const;
350 };
351
357[[nodiscard]] inline int
358pforDeltaWidth( const std::vector< std::uint64_t > & x , std::uint64_t base ,
359 double f = 0.9 );
360
382[[nodiscard]] inline PForDeltaBlock
383encodePForDelta( const std::vector< std::uint64_t > & x , int b ,
384 std::uint64_t base = 0 );
385
388[[nodiscard]] inline std::vector< std::uint64_t >
390
391/*---------------------- VARIABLE BYTE, (s,c)-DENSE ------------------------*/
392
407inline void encodeVByte( std::vector< std::uint8_t > & out , std::uint64_t x );
408
412[[nodiscard]] inline std::uint64_t
413decodeVByte( const std::vector< std::uint8_t > & in , std::size_t & p );
414
417[[nodiscard]] constexpr int vbyteLength( std::uint64_t x );
418
437inline void encodeDense( std::vector< std::uint32_t > & out , std::uint64_t x ,
438 std::uint32_t s , std::uint32_t c );
439
454[[nodiscard]] inline std::uint64_t
455decodeDense( const std::vector< std::uint32_t > & in , std::size_t & p ,
456 std::uint32_t s , std::uint32_t c );
457
460[[nodiscard]] constexpr int denseLength( std::uint64_t x , std::uint32_t s ,
461 std::uint32_t c );
462
479[[nodiscard]] inline std::uint32_t
480denseOptimalS( const std::vector< std::uint64_t > & f , int bits = 8 );
481
482/*------------------------------ INTERPOLATIVE -----------------------------*/
483
487inline void encodeBounded( BitWriter & w , std::uint64_t x , std::uint64_t a ,
488 std::uint64_t b );
489
493[[nodiscard]] inline std::uint64_t
494decodeBounded( BitReader & r , std::uint64_t a , std::uint64_t b );
495
519 const std::vector< std::uint64_t > & x ,
520 std::uint64_t low , std::uint64_t hi );
521
525[[nodiscard]] inline std::vector< std::uint64_t >
526decodeInterpolative( BitReader & r , std::size_t n , std::uint64_t low ,
527 std::uint64_t hi );
528
529/*--------------------------------------------------------------------------*/
530
531} // end( namespace ADS )
532
533/*--------------------------------------------------------------------------*/
534/*-------------------------- IMPLEMENTATION --------------------------------*/
535/*--------------------------------------------------------------------------*/
536
537#include "ADS/compression/impl/IntegerCodes.tpp"
538
539/*--------------------------------------------------------------------------*/
540
541#endif /* IntegerCodes.h included */
542
543/*--------------------------------------------------------------------------*/
544/*------------------------- End File IntegerCodes.h ------------------------*/
545/*--------------------------------------------------------------------------*/
The streams of bits on which the integer codes are written and read [see IntegerCodes....
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book: op...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
a stream of bits read forward from a place of a BitVector, which must outlive the reader
Definition BitStream.h:149
a vector of bits packed in words of 64 bits
Definition BitVector.h:479
a stream of bits written at its end, on a BitVector
Definition BitStream.h:82
void encodeGamma(BitWriter &w, std::uint64_t x)
, zeros and
void encodeInterpolative(BitWriter &w, const std::vector< std::uint64_t > &x, std::uint64_t low, std::uint64_t hi)
the interpolative code of , which and bound
void encodeDense(std::vector< std::uint32_t > &out, std::uint64_t x, std::uint32_t s, std::uint32_t c)
the -dense code of appended to out, a word per item: the continuers and then a stopper
constexpr std::uint64_t signedToNatural(std::int64_t x)
as a natural number: if , if
int pforDeltaWidth(const std::vector< std::uint64_t > &x, std::uint64_t base, double f=0.9)
the smallest whose range holds at least the fraction of the values (all of them if ),...
std::uint64_t decodeBounded(BitReader &r, std::uint64_t a, std::uint64_t b)
the integer in coded next in by encodeBounded()
std::vector< std::uint64_t > dGaps(const std::vector< std::uint64_t > &x)
the d-gaps of a non-decreasing sequence: the first item, then the differences between consecutive ite...
std::vector< std::uint64_t > prefixSums(const std::vector< std::uint64_t > &g)
the sequence of the d-gaps , i.e., their prefix sums
std::uint64_t decodeGolomb(BitReader &r, std::uint64_t b)
the integer whose Golomb's code of modulus is next in
void encodeRice(BitWriter &w, std::uint64_t x, int k)
, and the remainder in bits,
std::uint64_t decodeDelta(BitReader &r)
the integer whose is next in
void encodeGolomb(BitWriter &w, std::uint64_t x, std::uint64_t b)
Golomb's code of modulus , and the remainder in the truncated binary code, .
constexpr std::uint64_t unaryLength(std::uint64_t x)
the bits of , i.e.,
std::vector< std::uint64_t > decodePForDelta(const PForDeltaBlock &p)
the values of the block : each slot plus the base, or the next exception for an escape
std::uint64_t decodeDense(const std::vector< std::uint32_t > &in, std::size_t &p, std::uint32_t s, std::uint32_t c)
the integer coded from , moved past it
void encodeUnary(BitWriter &w, std::uint64_t x)
, zeros and a 1
void encodeBounded(BitWriter &w, std::uint64_t x, std::uint64_t a, std::uint64_t b)
in bits, the BinaryCode of chapter 11, Integer Coding, of Ferragina's book
constexpr std::uint64_t golombLength(std::uint64_t x, std::uint64_t b)
the bits of Golomb's code of of modulus
std::uint64_t decodeGamma(BitReader &r)
the integer whose is next in
void encodeVByte(std::vector< std::uint8_t > &out, std::uint64_t x)
the variable-byte code of appended to out
void encodeDelta(BitWriter &w, std::uint64_t x)
, and
constexpr int denseLength(std::uint64_t x, std::uint32_t s, std::uint32_t c)
the words of the -dense code of
constexpr std::int64_t naturalToSigned(std::uint64_t y)
the inverse of signedToNatural(): if is even, if odd
std::vector< std::uint64_t > decodeInterpolative(BitReader &r, std::size_t n, std::uint64_t low, std::uint64_t hi)
the integers coded in , by the recursion of encodeInterpolative() with decodeBounded() in place of e...
PForDeltaBlock encodePForDelta(const std::vector< std::uint64_t > &x, int b, std::uint64_t base=0)
the values in slots of bits after subtracting base
constexpr int binaryLength(std::uint64_t x)
, the bits of the binary code of ,
constexpr int deltaLength(std::uint64_t x)
the bits of ,
std::uint64_t decodeRice(BitReader &r, int k)
the integer whose is next in : , read in unary and in bits
constexpr std::uint64_t riceLength(std::uint64_t x, int k)
the bits of ,
constexpr int gammaLength(std::uint64_t x)
the bits of ,
std::uint64_t decodeUnary(BitReader &r)
the integer whose is next in : one plus the zeros before the next 1
std::uint64_t decodeVByte(const std::vector< std::uint8_t > &in, std::size_t &p)
the integer coded from , moved past it: the groups of 7 bits gathered up to the byte below
constexpr int vbyteLength(std::uint64_t x)
the bytes of the variable-byte code of , , and 1 for
std::uint32_t denseOptimalS(const std::vector< std::uint64_t > &f, int bits=8)
the number of stoppers, , that minimizes the words of the integers of frequencies ,...
Definition ADT.h:96
a block of values coded by PForDelta
Definition IntegerCodes.h:339
BitVector slots
the slots of bits
Definition IntegerCodes.h:342
std::vector< std::uint64_t > exceptions
the values out of range
Definition IntegerCodes.h:343
std::size_t size(void) const
the number of values
std::size_t codeBits(void) const
the bits of the code, and 64 for each exception
int b
the bits of a slot
Definition IntegerCodes.h:340
std::uint64_t base
subtracted from the values in range
Definition IntegerCodes.h:341