tudocomp
– The TU Dortmund Compression Framework
MTFCompressor.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/util.hpp>
5 #include <tudocomp/Env.hpp>
6 #include <numeric>
7 #include <tudocomp/def.hpp>
8 
9 namespace tdc {
10 
11 
16 template<class value_type = uliteral_t>
17 value_type mtf_encode_char(const value_type v, value_type*const table, const size_t table_size) {
18  for(size_t i = 0; i < table_size; ++i) {
19  if(table[i] == v) {
20  for(size_t j = i; j > 0; --j) {
21  table[j] = table[j-1];
22  }
23  table[0] = v;
24  return i;
25  }
26  }
27  DCHECK(false) << v << "(" << static_cast<size_t>(static_cast<typename std::make_unsigned<value_type>::type>(v)) << " not in " << arr_to_debug_string(table,table_size);
28  return 0;
29 }
30 
35 template<class value_type = uliteral_t>
36 value_type mtf_decode_char(const value_type v, value_type*const table) {
37  const value_type return_value = table[v];
38  for(size_t j = v; j > 0; --j) {
39  table[j] = table[j-1];
40  }
41  table[0] = return_value;
42  return return_value;
43 }
44 
45 template<class char_type = uliteral_t>
46 void mtf_encode(std::basic_istream<char_type>& is, std::basic_ostream<char_type>& os) {
47  typedef typename std::make_unsigned<char_type>::type value_type; // -> default: uint8_t
48  static constexpr size_t table_size = std::numeric_limits<value_type>::max()+1;
49  value_type table[table_size];
50  std::iota(table, table+table_size, 0);
51 
52  char_type c;
53  while(is.get(c)) {
54  os << mtf_encode_char(static_cast<value_type>(c), table, table_size);
55  }
56 }
57 
58 template<class char_type = uliteral_t>
59 void mtf_decode(std::basic_istream<char_type>& is, std::basic_ostream<char_type>& os) {
60  typedef typename std::make_unsigned<char_type>::type value_type; // -> default: uint8_t
61  static constexpr size_t table_size = std::numeric_limits<value_type>::max()+1;
62  value_type table[table_size];
63  std::iota(table, table+table_size, 0);
64 
65  char_type c;
66  while(is.get(c)) {
67  os << mtf_decode_char(static_cast<value_type>(c), table);
68  }
69 }
70 
71 class MTFCompressor : public Compressor {
72 public:
73  inline static Meta meta() {
74  Meta m("compressor", "mtf", "Move To Front Compressor");
75  return m;
76  }
77  inline MTFCompressor(Env&& env)
78  : Compressor(std::move(env)) {
79  }
80 
81  inline virtual void compress(Input& input, Output& output) override {
82  auto is = input.as_stream();
83  auto os = output.as_stream();
84  mtf_encode(is,os);
85  }
86  inline virtual void decompress(Input& input, Output& output) override {
87  auto is = input.as_stream();
88  auto os = output.as_stream();
89  mtf_decode(is,os);
90  }
91 };
92 
93 
94 }//ns
95 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
static Meta meta()
Base for data compressors.
Definition: Compressor.hpp:19
void mtf_encode(std::basic_istream< char_type > &is, std::basic_ostream< char_type > &os)
MTFCompressor(Env &&env)
InputStream as_stream() const
Creates a stream that allows for character-wise reading of the input.
Definition: Input.hpp:264
value_type mtf_encode_char(const value_type v, value_type *const table, const size_t table_size)
Encodes a character &#39;v&#39; by Move-To-Front Coding Needs and modifies a lookup table storing the last-us...
virtual void decompress(Input &input, Output &output) override
Decompress the given input to the given output.
void mtf_decode(std::basic_istream< char_type > &is, std::basic_ostream< char_type > &os)
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
OutputStream as_stream() const
Creates a stream that allows for character-wise output.
An abstraction layer for algorithm output.
Definition: Output.hpp:23
std::string arr_to_debug_string(const T *s, size_t length)
Builds the string representation of an array of printable values, sorrounded by square brackets ([ an...
Local environment for a compression/encoding/decompression call.
virtual void compress(Input &input, Output &output) override
Compress the given input to the given output.
value_type mtf_decode_char(const value_type v, value_type *const table)
Decodes a character encoded as &#39;v&#39; by Move-To-Front Coding Needs and modifies a lookup table storing ...
An abstraction layer for algorithm input.
Definition: Input.hpp:37