tudocomp
– The TU Dortmund Compression Framework
StreamingStrategy.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 StreamingStrategy: public Algorithm {
10 public:
12 
13  inline static Meta meta() {
14  Meta m("lz78u_strategy", "streaming");
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  typename ref_coder_t::Encoder m_ref_coder;
22  typename string_coder_t::Encoder m_string_coder;
23 
24  std::shared_ptr<BitOStream> m_out;
25 
26  public:
27  inline Compression(Env&& env,
28  Env&& ref_env,
29  std::shared_ptr<BitOStream> out):
30  Algorithm(std::move(env)),
31  m_ref_coder(std::move(ref_env), out, NoLiterals()),
32  m_string_coder(std::move(this->env().env_for_option("string_coder")), out, NoLiterals()),
33  m_out(out) {}
34 
35  inline void encode_ref(size_t ref, Range ref_range) {
36  DVLOG(2) << "encode ref: " << ref;
37  m_ref_coder.encode(ref, ref_range);
38  }
39 
40  inline void encode_char(uliteral_t c) {
41  DVLOG(2) << "encode char: '" << c << "' (" << int(c) << ")";
42  m_string_coder.encode(c, literal_r);
43  }
44 
45  inline void encode_str(View str) {
46  for (auto c : str) {
47  encode_char(c);
48  }
49  encode_char(0);
50  }
51 
52  inline void encode_sep(bool val) {
53  DVLOG(2) << "encode sep: " << int(val == 1);
54  m_out->write_bit(val);
55  }
56  };
57 
58  template<typename ref_coder_t>
59  class Decompression: public Algorithm {
60  typename ref_coder_t::Decoder m_ref_coder;
61  typename string_coder_t::Decoder m_string_coder;
62 
63  std::vector<uliteral_t> m_buf;
64  std::shared_ptr<BitIStream> m_in;
65  public:
66  inline Decompression(Env&& env,
67  Env&& ref_env,
68  std::shared_ptr<BitIStream> in):
69  Algorithm(std::move(env)),
70  m_ref_coder(std::move(ref_env), in),
71  m_string_coder(std::move(this->env().env_for_option("string_coder")), in),
72  m_in(in) {}
73 
74  inline size_t decode_ref(Range ref_range) {
75  return m_ref_coder.template decode<size_t>(ref_range);
76  }
77 
79  return m_string_coder.template decode<uliteral_t>(literal_r);
80  }
81 
82  inline View decode_str() {
83  m_buf.clear();
84  while (true) {
85  auto c = decode_char();
86  if (c == 0) {
87  break;
88  }
89  m_buf.push_back(c);
90  }
91  return m_buf;
92  }
93 
94  inline bool decode_sep() {
95  return m_in->read_bit();
96  }
97 
98  inline bool eof() {
99  return m_ref_coder.eof();
100  }
101  };
102 };
103 
104 
105 }
106 }
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
A const view into a slice of memory.
uint8_t uliteral_t
Type to represent signed single literals.
Definition: def.hpp:131
Decompression(Env &&env, Env &&ref_env, std::shared_ptr< BitIStream > in)
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
Compression(Env &&env, Env &&ref_env, std::shared_ptr< BitOStream > out)
void clear()
Sets the size to 0.
void templated(const std::string &accepted_type)
Declares that this option accepts values of the specified Algorithm type T.
Definition: Meta.hpp:93
void encode_ref(size_t ref, Range ref_range)
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