tudocomp
– The TU Dortmund Compression Framework
BufferingStrategy.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "pre_header.hpp"
4 
5 namespace tdc {
6 namespace lz78u {
7 
8 template<typename string_coder_t>
9 class BufferingStrategy: public Algorithm {
10 public:
12 
13  inline static Meta meta() {
14  Meta m("lz78u_strategy", "buffering");
15  m.option("string_coder").templated<string_coder_t>("coder");
16  return m;
17  }
18 
19  template<typename ref_coder_t>
20  class Compression: public Algorithm {
21  Env m_ref_env;
22  std::shared_ptr<BitOStream> m_out;
23 
24  // TODO Optimization: Replace with something that just stores the increment points
25  std::vector<Range> m_ref_ranges;
26  // TODO Optimization: Replace with variable bit width
27  std::vector<size_t> m_refs;
28  // TODO Optimization: Replace with bitvector
29  std::vector<bool> m_seps;
30  std::vector<uliteral_t> m_chars;
31 
32  // TODO Optimization: Replace with 2-bit vector, or remove completely
33  // by hardcoding the encoding scheme in the strategy
34  // TODO Optimization: Encode "encode_str" case more compactly by using single code?
35  std::vector<uint8_t> m_stream;
36 
37  public:
38  inline Compression(Env&& env,
39  Env&& ref_env,
40  std::shared_ptr<BitOStream> out):
41  Algorithm(std::move(env)),
42  m_ref_env(std::move(ref_env)),
43  m_out(out) {}
44 
45  inline void encode_ref(size_t ref, Range ref_range) {
46  m_refs.push_back(ref);
47  m_ref_ranges.push_back(ref_range);
48  m_stream.push_back(0);
49  }
50 
51  inline void encode_sep(bool val) {
52  m_seps.push_back(val);
53  m_stream.push_back(1);
54  }
55 
56  inline void encode_char(uliteral_t c) {
57  m_chars.push_back(c);
58  m_stream.push_back(2);
59  }
60 
61  inline void encode_str(View str) {
62  for (auto c : str) {
63  encode_char(c);
64  }
65  encode_char(0);
66  }
67 
68  inline ~Compression() {
69  typename ref_coder_t::Encoder ref_coder {
70  std::move(m_ref_env),
71  m_out,
72  NoLiterals()
73  };
74  typename string_coder_t::Encoder string_coder {
75  std::move(this->env().env_for_option("string_coder")),
76  m_out,
77  ViewLiterals(m_chars)
78  };
79 
80  auto refs_i = m_refs.begin();
81  auto ref_ranges_i = m_ref_ranges.begin();
82  auto seps_i = m_seps.begin();
83  auto chars_i = m_chars.begin();
84 
85  for (auto kind : m_stream) {
86  switch (kind) {
87  case 0: {
88  auto ref = *(refs_i++);
89  auto ref_range = *(ref_ranges_i++);
90  ref_coder.encode(ref, ref_range);
91 
92  break;
93  }
94  case 1: {
95  auto sep = *(seps_i++);
96  m_out->write_bit(sep);
97 
98  break;
99  }
100  case 2: {
101  auto chr = *(chars_i++);
102  string_coder.encode(chr, literal_r);
103 
104  break;
105  }
106  }
107  }
108 
109  DCHECK(refs_i == m_refs.end());
110  DCHECK(ref_ranges_i == m_ref_ranges.end());
111  DCHECK(seps_i == m_seps.end());
112  DCHECK(chars_i == m_chars.end());
113  }
114  };
115 
116  template<typename ref_coder_t>
117  class Decompression: public Algorithm {
118  typename ref_coder_t::Decoder m_ref_coder;
119  typename string_coder_t::Decoder m_string_coder;
120 
121  std::vector<uliteral_t> m_buf;
122  std::shared_ptr<BitIStream> m_in;
123  public:
125  Env&& ref_env,
126  std::shared_ptr<BitIStream> in):
127  Algorithm(std::move(env)),
128  m_ref_coder(std::move(ref_env), in),
129  m_string_coder(std::move(this->env().env_for_option("string_coder")), in),
130  m_in(in) {}
131 
132  inline size_t decode_ref(Range ref_range) {
133  return m_ref_coder.template decode<size_t>(ref_range);
134  }
135 
137  return m_string_coder.template decode<uliteral_t>(literal_r);
138  }
139 
140  inline View decode_str() {
141  m_buf.clear();
142  while (true) {
143  auto c = decode_char();
144  if (c == 0) {
145  break;
146  }
147  m_buf.push_back(c);
148  }
149  return m_buf;
150  }
151 
152  inline bool decode_sep() {
153  return m_in->read_bit();
154  }
155 
156  inline bool eof() {
157  return m_ref_coder.eof();
158  }
159  };
160 };
161 
162 
163 }
164 }
Represents a generic range of positive integers.
Definition: Range.hpp:16
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
void encode_ref(size_t ref, Range ref_range)
A const view into a slice of memory.
uint8_t uliteral_t
Type to represent signed single literals.
Definition: def.hpp:131
A literal iterator that yields every character from a View.
Definition: Literal.hpp:41
Algorithm(Algorithm const &)=default
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
An empty literal iterator that yields no literals whatsoever.
Definition: Literal.hpp:37
constexpr auto literal_r
Global predefined reange for literals.
Definition: Range.hpp:111
Decompression(Env &&env, Env &&ref_env, std::shared_ptr< BitIStream > in)
void clear()
Sets the size to 0.
Compression(Env &&env, Env &&ref_env, std::shared_ptr< BitOStream > out)
void templated(const std::string &accepted_type)
Declares that this option accepts values of the specified Algorithm type T.
Definition: Meta.hpp:93
OptionBuilder option(const std::string &name)
Declares an accepted option for this algorithm.
Definition: Meta.hpp:216
Local environment for a compression/encoding/decompression call.
Interface for algorithms.
Definition: Algorithm.hpp:15