Skip to content
Snippets Groups Projects
Commit de0e7a7c authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify

parent 556fbf72
No related branches found
No related tags found
1 merge request!49First compression algorithms
...@@ -43,12 +43,13 @@ public: ...@@ -43,12 +43,13 @@ public:
   
for ( size_t index = 0; index < source.size ( ) + 1; ++ index ) { for ( size_t index = 0; index < source.size ( ) + 1; ++ index ) {
   
unsigned prob_low, prob_high, prob_count; unsigned prob_low, prob_high;
unsigned prob_count = model.getCount ( );
   
if ( index >= source.size ( ) ) if ( index >= source.size ( ) )
std::tie ( prob_low, prob_high, prob_count ) = model.getProbabilityEof ( ); std::tie ( prob_low, prob_high ) = model.getProbabilityEof ( );
else { else {
std::tie ( prob_low, prob_high, prob_count ) = model.getProbability ( source [ index ] ); std::tie ( prob_low, prob_high ) = model.getProbability ( source [ index ] );
model.update ( source [ index ] ); model.update ( source [ index ] );
} }
   
......
...@@ -60,8 +60,9 @@ public: ...@@ -60,8 +60,9 @@ public:
break; break;
   
char c; char c;
unsigned prob_low, prob_high, prob_count; unsigned prob_low, prob_high;
std::tie ( prob_low, prob_high, prob_count, c ) = model.getChar ( scaled_value ); unsigned prob_count = model.getCount ( );
std::tie ( prob_low, prob_high, c ) = model.getChar ( scaled_value );
model.update ( c ); model.update ( c );
   
result += c; result += c;
......
...@@ -8,57 +8,52 @@ ...@@ -8,57 +8,52 @@
   
template < class SymbolType > template < class SymbolType >
class ArithmeticModel { class ArithmeticModel {
ext::map < ext::variant < void, SymbolType >, unsigned > m_low_cumulative_frequency; ext::map < SymbolType, unsigned > m_high_cumulative_frequency;
unsigned m_global_high; unsigned m_global_high; // EOF is with probability 1/n (ie its low is m_global_high - 1 and hight is m_global_high) with cumulative frequency right below the m_global_high
   
public: public:
ArithmeticModel ( const ext::set < SymbolType > & alphabet ) { ArithmeticModel ( const ext::set < SymbolType > & alphabet ) {
m_low_cumulative_frequency.insert ( std::make_pair ( ext::variant < void, SymbolType >::template from < void > ( ), 0 ) );
for ( const SymbolType & symbol : alphabet )
m_low_cumulative_frequency.insert ( std::make_pair ( symbol, 0 ) );
unsigned frequency = 0; unsigned frequency = 0;
for ( std::pair < const ext::variant < void, SymbolType >, unsigned > & entry : m_low_cumulative_frequency ) for ( const SymbolType & symbol : alphabet )
entry.second = frequency ++; m_high_cumulative_frequency.insert ( std::make_pair ( symbol, ++ frequency ) );
m_global_high = frequency + 1;
m_global_high = frequency;
} }
   
void update ( const ext::variant < void, SymbolType > & symbol ) { void update ( const ext::variant < void, SymbolType > & symbol ) {
for ( auto i = std::next ( m_low_cumulative_frequency.find ( symbol ) ); i != m_low_cumulative_frequency.end ( ) ; ++ i ) for ( auto i = m_high_cumulative_frequency.find ( symbol ); i != m_high_cumulative_frequency.end ( ) ; ++ i )
i->second += 1; i->second += 1;
m_global_high += 1; m_global_high += 1;
} }
   
std::tuple < unsigned, unsigned, unsigned > getProbability ( const ext::variant < void, SymbolType > & c ) const { std::tuple < unsigned, unsigned > getProbability ( const SymbolType & c ) const {
auto i = m_low_cumulative_frequency.find ( c ); auto i = m_high_cumulative_frequency.find ( c );
unsigned high_prob = m_global_high; unsigned low_prob = 0;
if ( std::next ( i ) != m_low_cumulative_frequency.end ( ) ) {
high_prob = std::next ( i )->second; if ( i != m_high_cumulative_frequency.begin ( ) )
} low_prob = std::prev ( i )->second;
return std::make_tuple ( i->second, high_prob, m_global_high );
return std::make_tuple ( low_prob, i->second );
} }
   
std::tuple < unsigned, unsigned, unsigned > getProbabilityEof ( ) const { std::tuple < unsigned, unsigned > getProbabilityEof ( ) const {
return getProbability ( ext::variant < void, SymbolType >::template from < void > ( ) ); return std::make_tuple ( m_global_high - 1, m_global_high );
} }
   
std::tuple < unsigned, unsigned, unsigned, SymbolType > getChar ( unsigned scaled_value ) const { std::tuple < unsigned, unsigned, SymbolType > getChar ( unsigned scaled_value ) const {
for ( auto i = m_low_cumulative_frequency.begin ( ); std::next ( i ) != m_low_cumulative_frequency.end ( ); ++ i ) for ( auto i = m_high_cumulative_frequency.begin ( ); i != m_high_cumulative_frequency.end ( ); ++ i )
if ( scaled_value < std::next ( i )->second ) { if ( scaled_value < i->second ) {
unsigned high_prob = m_global_high; unsigned low_prob = 0;
if ( std::next ( i ) != m_low_cumulative_frequency.end ( ) ) {
high_prob = std::next ( i )->second; if ( i != m_high_cumulative_frequency.begin ( ) )
} low_prob = std::prev ( i )->second;
return std::make_tuple ( i->second, high_prob, m_global_high, i->first.template get < SymbolType > ( ) );
return std::make_tuple ( low_prob, i->second, i->first );
} }
throw std::logic_error("error"); throw std::logic_error("error");
} }
   
bool isEof ( unsigned scaled_value ) const { bool isEof ( unsigned scaled_value ) const {
unsigned prob_low, prob_high, prob_count; return scaled_value == m_global_high - 1;
std::tie ( prob_low, prob_high, prob_count ) = getProbabilityEof ( );
return scaled_value >= prob_low && scaled_value < prob_high;
} }
   
unsigned getCount ( ) const { unsigned getCount ( ) const {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment