ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PPM.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------- File PPM.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
72/*--------------------------------------------------------------------------*/
73/*----------------------------- DEFINITIONS --------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76#ifndef __ADS_PPM
77 #define __ADS_PPM
78/* self-identification: #endif constant */
79
80/*--------------------------------------------------------------------------*/
81/*------------------------------ INCLUDES ----------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84#include "ADS/Contract.h"
86
87#include <algorithm>
88#include <cstddef>
89#include <cstdint>
90#include <map>
91#include <span>
92#include <string>
93#include <string_view>
94#include <vector>
95
96/*--------------------------------------------------------------------------*/
97/*--------------------------- NAMESPACE ------------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100namespace ADS
101{
102
103/*--------------------------------------------------------------------------*/
104/*----------------------------- CLASS PPMModel -----------------------------*/
105/*--------------------------------------------------------------------------*/
106
110{
111
112/*--------------------------------------------------------------------------*/
113 public:
117 struct Step
118 {
119 std::uint64_t low ,
123 friend bool operator==( const Step & a , const Step & b ) = default;
124 };
125
129 PPMModel( int order , std::uint32_t sigma = 256 );
130
132 [[nodiscard]] int order( void ) const { return( f_order ); }
133
135 [[nodiscard]] std::uint32_t sigma( void ) const { return( f_sigma ); }
136
139 [[nodiscard]] std::map< std::uint32_t , std::uint64_t >
140 counts( const std::vector< std::uint32_t > & ctx ) const;
141
168 [[nodiscard]] std::vector< Step > steps( std::uint32_t c ) const;
169
172 void encode( std::uint32_t c , RangeEncoder & e ) const;
173
176 [[nodiscard]] std::uint32_t decode( RangeDecoder & d ) const;
177
197 void update( std::uint32_t c );
198
199/*--------------------------------------------------------------------------*/
200 private:
201 using Counts = std::map< std::uint32_t , std::uint64_t >;
202
207 template< class Choose >
208 std::uint32_t walk( Choose choose ) const;
209
210 static constexpr std::uint64_t kLimit = std::uint64_t( 1 ) << 16;
211
212 int f_order;
213 std::uint32_t f_sigma;
214 std::map< std::vector< std::uint32_t > , Counts > f_ctx;
215 std::vector< std::uint32_t > f_hist;
216
217 }; // end( class PPMModel )
218
219/*--------------------------------------------------------------------------*/
220
236[[nodiscard]] inline std::vector< std::uint8_t > ppmEncode( std::string_view s ,
237 int K );
238
254[[nodiscard]] inline std::string
255ppmDecode( std::span< const std::uint8_t > code , std::size_t n , int K );
256
257/*--------------------------------------------------------------------------*/
258
259} // end( namespace ADS )
260
261/*--------------------------------------------------------------------------*/
262/*-------------------------- IMPLEMENTATION --------------------------------*/
263/*--------------------------------------------------------------------------*/
264
265#include "ADS/compression/impl/PPM.tpp"
266
267/*--------------------------------------------------------------------------*/
268
269#endif /* PPM.h included */
270
271/*--------------------------------------------------------------------------*/
272/*----------------------------- End File PPM.h -----------------------------*/
273/*--------------------------------------------------------------------------*/
The arithmetic code (chapter 12, Statistical Coding, of Ferragina's book), exact and in integers.
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
std::map< std::uint32_t, std::uint64_t > counts(const std::vector< std::uint32_t > &ctx) const
the counts of the symbols seen after the context ctx (the oldest symbol first), empty if ctx was neve...
std::uint32_t sigma(void) const
the size of the alphabet
Definition PPM.h:135
int order(void) const
the order
Definition PPM.h:132
std::uint32_t decode(RangeDecoder &d) const
the symbol coded next in , after the symbols seen, by the walk of steps() that reads in whether each...
PPMModel(int order, std::uint32_t sigma=256)
the model of order = order on = sigma symbols, with no symbol seen
std::vector< Step > steps(std::uint32_t c) const
the steps that code after the symbols seen: the escapes, then
void encode(std::uint32_t c, RangeEncoder &e) const
codes after the symbols seen, by the steps of steps()
void update(std::uint32_t c)
adds to the symbols seen
the decoder of a RangeEncoder on bits
Definition ArithmeticCoding.h:270
the integer arithmetic encoder of Witten, Neal and Cleary on bits
Definition ArithmeticCoding.h:203
std::string ppmDecode(std::span< const std::uint8_t > code, std::size_t n, int K)
the bytes of the code by PPM of order
std::vector< std::uint8_t > ppmEncode(std::string_view s, int K)
the code of the bytes of by PPM of order
Definition ADT.h:96
a step of the code: the cumulative counts of the symbol or of the escape, out of total
Definition PPM.h:118
std::uint64_t high
the cumulative count after the item
Definition PPM.h:120
std::uint64_t total
the total of the counts, with the escape
Definition PPM.h:121
std::uint64_t low
the cumulative count before the item
Definition PPM.h:119
friend bool operator==(const Step &a, const Step &b)=default
whether two steps are the same