16#include <string/LinearString.h>
20namespace compression {
22template <
class SymbolType,
class Model >
26 unsigned m_pending_bits = 0;
27 unsigned long long m_low = 0;
28 unsigned long long m_high = max_code;
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;
36 template <
class Callback >
37 inline static void put_bit_plus_pending ( Callback &&
callback,
bool bit,
unsigned & m_pending_bits) {
39 while ( m_pending_bits > 0 ) {
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;
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 ) {
72 template <
class Callback >
76 unsigned prob_count = m_model.getCount ( );
78 m_model.getProbability ( symbol, prob_low, prob_high );
79 m_model.update ( symbol );
81 encode ( prob_low, prob_high, prob_count, std::forward < Callback && > (
callback ) );
84 template <
class Callback >
88 unsigned prob_count = m_model.getCount ( );
90 m_model.getProbabilityEof ( prob_low, prob_high );
92 encode ( prob_low, prob_high, prob_count, std::forward < Callback && > (
callback ) );
95 put_bit_plus_pending ( std::forward < Callback && > (
callback ), m_low >= one_fourth, m_pending_bits);
105 template <
class SymbolType >
110 auto output = [&] (
bool bit ) {
114 for (
size_t index = 0; index < source.
getContent ( ).size ( ); ++ index ) {
115 arithmeticCoder.encode ( source.
getContent ( ) [ index ], output );
117 arithmeticCoder.
finalize ( output );
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
Definition: ArithmeticCompression.h:103
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