Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ArithmeticDecompression.h
Go to the documentation of this file.
1
6#pragma once
7
12#include "ArithmeticModel.h"
13
14#include <string/LinearString.h>
15
16namespace stringology {
17
18namespace compression {
19
20template < class SymbolType, class Model >
22 Model m_model;
23
24 unsigned long long m_value = 0;
25 unsigned long long m_low = 0;
26 unsigned long long m_high = 0;
27
28 unsigned long long m_range = 0;
29 unsigned m_prob_count = 0;
30 unsigned m_scaled_value = 0;
31
32 static const unsigned valid_bits = sizeof ( unsigned long long ) * 8 / 2;
33 static const unsigned long long max_code = ~0ull >> valid_bits;
34 static const unsigned long long one_half = ( max_code >> 1 ) + 1;
35 static const unsigned long long one_fourth = ( max_code >> 2 ) + 1;
36 static const unsigned long long three_fourths = one_half + one_fourth;
37
38 template < class Callback >
39 void fillVariables ( Callback && callback ) {
40 for( ; ; ) {
41 if ( m_high < one_half || m_low >= one_half ) {
42 //do nothing, both bits are a zero or both bits are one
43 } else if ( m_low >= one_fourth && m_high < three_fourths ) {
44 m_value -= one_fourth;
45 m_low -= one_fourth;
46 m_high -= one_fourth;
47 } else
48 break;
49 m_low <<= 1;
50 m_high <<= 1;
51 m_value <<= 1;
52
53 m_high++;
54 m_value += callback ( );
55
56 m_low &= max_code;
57 m_high &= max_code;
58 m_value &= max_code;
59 }
60
61 m_range = m_high - m_low + 1;
62 m_prob_count = m_model.getCount ( );
63 m_scaled_value = ( ( m_value - m_low + 1 ) * m_prob_count - 1 ) / m_range;
64 }
65
66public:
67 template < class Callback >
68 ArithmeticDecoder ( Model model, Callback && callback ) : m_model ( std::move ( model ) ) {
69 fillVariables ( std::forward < Callback && > ( callback ) );
70 }
71
72 bool isEof ( ) {
73 return m_model.isEof ( m_scaled_value );
74 }
75
76 template < class Callback >
77 SymbolType decode ( Callback && callback ) {
78 unsigned prob_low;
79 unsigned prob_high;
80 SymbolType c = m_model.getChar ( m_scaled_value, prob_low, prob_high );
81 m_model.update ( c );
82
83 m_high = m_low + m_range * prob_high / m_prob_count - 1;
84 m_low = m_low + m_range * prob_low / m_prob_count;
85
86 fillVariables ( std::forward < Callback && > ( callback ) );
87
88 return c;
89 }
90
91 Model & getModel ( ) {
92 return m_model;
93 }
94};
95
97public:
98 template < class SymbolType >
100 size_t index = 0;
101 auto input = [ & ] ( ) {
102 return index >= source.size ( ) ? 0 : source [ index ++ ] ? 1 : 0;
103 };
104
107
108 while ( ! decoder.isEof ( ) ) {
109 result.push_back ( decoder.decode ( input ) );
110 }
111
113 }
114};
115
116} /* namespace compression */
117
118} /* namespace stringology */
119
Definition: ArithmeticModel.h:13
Definition: set.hpp:44
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
static string::LinearString< SymbolType > decompress(const ext::vector< bool > &source, const ext::set< SymbolType > &alphabet)
Definition: ArithmeticDecompression.h:99
Definition: ArithmeticDecompression.h:21
SymbolType decode(Callback &&callback)
Definition: ArithmeticDecompression.h:77
ArithmeticDecoder(Model model, Callback &&callback)
Definition: ArithmeticDecompression.h:68
Model & getModel()
Definition: ArithmeticDecompression.h:91
bool isEof()
Definition: ArithmeticDecompression.h:72
Definition: BarSymbol.cpp:12
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
int callback(struct dl_phdr_info *info, size_t, void *data)
Definition: simpleStacktrace.cpp:25
Definition: FordFulkerson.hpp:16
Definition: ArithmeticCompression.h:18