|
ADS
Algorithms and Data Structures
|
Prediction by partial matching (chapter 12, Statistical Coding, of Ferragina's book), the model of contexts of Cleary and Witten in the version C of Moffat, on the arithmetic coder [see ArithmeticCoding.h]. More...
#include "ADS/Contract.h"#include "ADS/compression/ArithmeticCoding.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <map>#include <span>#include <string>#include <string_view>#include <vector>#include "ADS/compression/impl/PPM.tpp"Go to the source code of this file.
Classes | |
| class | ADS::PPMModel |
| the model of contexts of PPM, version C with exclusion, of order \( K \) on the symbols \( [ 0 , \sigma ) \) More... | |
| struct | ADS::PPMModel::Step |
| a step of the code: the cumulative counts \( [ \mathit{low} , \mathit{high} ) \) of the symbol or of the escape, out of total More... | |
Namespaces | |
| namespace | ADS |
Functions | |
| std::vector< std::uint8_t > | ADS::ppmEncode (std::string_view s, int K) |
| the code of the bytes of \( s \) by PPM of order \( K \) | |
| std::string | ADS::ppmDecode (std::span< const std::uint8_t > code, std::size_t n, int K) |
| the \( n \) bytes of the code by PPM of order \( K \) | |
Prediction by partial matching (chapter 12, Statistical Coding, of Ferragina's book), the model of contexts of Cleary and Witten in the version C of Moffat, on the arithmetic coder [see ArithmeticCoding.h].
For each context \( \alpha \) of length \( 0 \le l \le K \) the model counts how many times each symbol has followed \( \alpha \) in the text seen so far. The symbol \( S[ i ] \) is coded in the longest context \( S[ i - l .. i - 1 ] \) that has already seen it, starting from \( l = \min( K , i ) \): each context on the way that does not predict it emits an escape, whose count in version C is the number \( q \) of the distinct symbols seen in the context, so that a symbol of count \( c( \sigma ) \) in a context seen \( n \) times has probability \( c( \sigma ) / ( n + q ) \) and the escape \( q / ( n + q ) \); after \( K + 1 \) escapes, the context of order \( -1 \) gives the same probability to every symbol. By the principle of exclusion, a context that escapes tells the decoder that the symbol is none of those it predicts, so the shorter contexts leave them out of their counts (the escape keeps its count \( q \), as in the example of the same chapter), and the context of order \( -1 \) gives \( 1 / ( |\Sigma| - q ) \); a context whose symbols are all excluded emits nothing. After each symbol the counts of all its contexts grow by one, and those of a context are halved when their total passes \( 2^{16} \). The decoder repeats the same walk, reading the escapes instead of writing them.
Coding \( S[ i ] \) takes \( O( K ( K + \sigma_K ) \log n ) \) time, with \( \sigma_K \) the largest number of symbols seen in a context, the contexts being in a balanced tree keyed by their strings; the practice keeps them in a trie of the contexts with links to the suffixes (Moffat), where the next context is a step away. The compression approaches the entropy of order \( K \) [see Entropy.h], and that of the text grows with \( K \) up to 4 or 5 symbols, beyond which the escapes cost more than the longer contexts gain, as the same chapter observes. The escape methods of the literature are A, \( 1 / ( n + 1 ) \), and B, \( q / n \), of Cleary and Witten; C, here; D, \( q / 2 n \); P, X and XC of Witten and Bell, from the symbols seen once; the PPMd of Shkarin estimates it from the statistics of similar contexts, and PPM* of Cleary, Teahan and Witten uses contexts of unbounded length.
The contracts [see Contract.h]: \( K \ge 0 \), \( 1 \le |\Sigma| \le 2^{16} \), a symbol in \( [ 0 , |\Sigma| ) \); the decoding of an encoding gives the text back, an audit. The operations are defined in PPM.tpp.