Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ArithmeticCompression.h
Go to the documentation of this file.
1
6#pragma once
7
12#include "ArithmeticModel.h"
13
14#include <alib/vector>
15
16#include <string/LinearString.h>
17
18namespace stringology {
19
20namespace compression {
21
22template < class SymbolType, class Model >
24 Model m_model;
25
26 unsigned m_pending_bits = 0;
27 unsigned long long m_low = 0;
28 unsigned long long m_high = max_code;
29
30 static const unsigned valid_bits = sizeof ( unsigned long long ) * 8 / 2;
31 static const unsigned long long max_code = ~0ull >> valid_bits;
32 static const unsigned long long one_half = ( max_code >> 1 ) + 1;
33 static const unsigned long long one_fourth = ( max_code >> 2 ) + 1;
34 static const unsigned long long three_fourths = one_half + one_fourth;
35
36 template < class Callback >
37 inline static void put_bit_plus_pending ( Callback && callback, bool bit, unsigned & m_pending_bits) {
38 callback ( bit );
39 while ( m_pending_bits > 0 ) {
40 callback ( ! bit );
41 -- m_pending_bits;
42 }
43 }
44
45 template < class Callback >
46 void encode ( unsigned prob_low, unsigned prob_high, unsigned prob_count, Callback callback ) {
47 unsigned long long range = m_high - m_low + 1;
48 m_high = m_low + range * prob_high / prob_count - 1;
49 m_low = m_low + range * prob_low / prob_count;
50 for ( ; ; ) {
51 if ( m_high < one_half || m_low >= one_half ) {
52 put_bit_plus_pending ( std::forward < Callback && > ( callback ), m_low >= one_half, m_pending_bits );
53 } else if ( m_low >= one_fourth && m_high < three_fourths ) {
54 m_pending_bits++;
55 m_low -= one_fourth;
56 m_high -= one_fourth;
57 } else
58 break;
59 m_high <<= 1;
60 m_high++;
61 m_low <<= 1;
62
63 m_low &= max_code;
64 m_high &= max_code;
65 }
66 }
67
68public:
69 ArithmeticCoder ( Model model ) : m_model ( std::move ( model ) ) {
70 }
71
72 template < class Callback >
73 void encode ( const SymbolType & symbol, Callback && callback ) {
74 unsigned prob_low;
75 unsigned prob_high;
76 unsigned prob_count = m_model.getCount ( );
77
78 m_model.getProbability ( symbol, prob_low, prob_high );
79 m_model.update ( symbol );
80
81 encode ( prob_low, prob_high, prob_count, std::forward < Callback && > ( callback ) );
82 }
83
84 template < class Callback >
85 void finalize ( Callback && callback ) {
86 unsigned prob_low;
87 unsigned prob_high;
88 unsigned prob_count = m_model.getCount ( );
89
90 m_model.getProbabilityEof ( prob_low, prob_high );
91
92 encode ( prob_low, prob_high, prob_count, std::forward < Callback && > ( callback ) );
93
94 m_pending_bits++;
95 put_bit_plus_pending ( std::forward < Callback && > ( callback ), m_low >= one_fourth, m_pending_bits);
96 }
97
98 Model & getModel ( ) {
99 return m_model;
100 }
101};
102
104public:
105 template < class SymbolType >
109
110 auto output = [&] ( bool bit ) {
111 result.push_back ( bit );
112 };
113
114 for ( size_t index = 0; index < source.getContent ( ).size ( ); ++ index ) {
115 arithmeticCoder.encode ( source.getContent ( ) [ index ], output );
116 }
117 arithmeticCoder.finalize ( output );
118
119 return result;
120 }
121
122};
123
124} /* namespace compression */
125
126} /* namespace stringology */
127
Definition: ArithmeticModel.h:13
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Linear string.
Definition: LinearString.h:57
const ext::set< SymbolType > & getAlphabet() const &
Definition: LinearString.h:103
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
static ext::vector< bool > compress(const string::LinearString< SymbolType > &source)
Definition: ArithmeticCompression.h:106
Definition: ArithmeticCompression.h:23
ArithmeticCoder(Model model)
Definition: ArithmeticCompression.h:69
void encode(const SymbolType &symbol, Callback &&callback)
Definition: ArithmeticCompression.h:73
void finalize(Callback &&callback)
Definition: ArithmeticCompression.h:85
Model & getModel()
Definition: ArithmeticCompression.h:98
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
auto range(Container &&cont) -> decltype(std::forward< Container >(cont).range())
Definition: range.hpp:247
int callback(struct dl_phdr_info *info, size_t, void *data)
Definition: simpleStacktrace.cpp:25
Definition: FordFulkerson.hpp:16
Definition: ArithmeticCompression.h:18