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) {
20 for(
size_t j = i; j > 0; --j) {
21 table[j] = table[j-1];
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);
35 template<
class value_type = uliteral_t>
37 const value_type return_value = table[v];
38 for(
size_t j = v; j > 0; --j) {
39 table[j] = table[j-1];
41 table[0] = return_value;
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;
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);
54 os << mtf_encode_char(static_cast<value_type>(c), table, table_size);
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;
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);
67 os << mtf_decode_char(static_cast<value_type>(c), table);
74 Meta m(
"compressor",
"mtf",
"Move To Front Compressor");
Contains the text compression and encoding framework.
Base for data compressors.
void mtf_encode(std::basic_istream< char_type > &is, std::basic_ostream< char_type > &os)
value_type mtf_encode_char(const value_type v, value_type *const table, const size_t table_size)
Encodes a character 'v' 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.
OutputStream as_stream() const
Creates a stream that allows for character-wise output.
An abstraction layer for algorithm output.
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 'v' by Move-To-Front Coding Needs and modifies a lookup table storing ...