tudocomp
– The TU Dortmund Compression Framework
Coder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/io.hpp>
4 #include <tudocomp/Algorithm.hpp>
5 #include <tudocomp/Env.hpp>
6 #include <tudocomp/Literal.hpp>
7 #include <tudocomp/Range.hpp>
8 
9 namespace tdc {
10 
14 class Encoder : public Algorithm {
15 
16 protected:
18  std::shared_ptr<BitOStream> m_out;
19 
20 public:
28  template<typename literals_t>
29  inline Encoder(
30  Env&& env,
31  std::shared_ptr<BitOStream> out,
32  literals_t&& literals)
33  : Algorithm(std::move(env)), m_out(out) {
34  }
35 
43  template<typename literals_t>
44  inline Encoder(Env&& env, Output& out, literals_t&& literals)
45  : Encoder(
46  std::move(env),
47  std::make_shared<BitOStream>(out),
48  literals) {
49  }
50 
60  template<typename value_t>
61  inline void encode(value_t v, const Range& r) {
62  m_out->write_int(v - r.min(), bits_for(r.max() - r.min()));
63  }
64 
74  template<typename value_t>
75  inline void encode(value_t v, const BitRange& r) {
76  m_out->write_bit(v);
77  }
78 
79  inline const std::shared_ptr<BitOStream>& stream() {
80  return m_out;
81  }
82 };
83 
87 class Decoder : public Algorithm {
88 
89 protected:
91  std::shared_ptr<BitIStream> m_in;
92 
93 public:
98  inline Decoder(Env&& env, std::shared_ptr<BitIStream> in)
99  : Algorithm(std::move(env)), m_in(in) {
100  }
101 
106  inline Decoder(Env&& env, Input& in)
107  : Decoder(std::move(env), std::make_shared<BitIStream>(in)) {
108  }
109 
113  inline bool eof() const {
114  return m_in->eof();
115  }
116 
126  template<typename value_t>
127  inline value_t decode(const Range& r) {
128  return value_t(r.min()) +
129  m_in->read_int<value_t>(bits_for(r.max() - r.min()));
130  }
131 
139  template<typename value_t>
140  inline value_t decode(const BitRange& r) {
141  return value_t(m_in->read_bit());
142  }
143 
144  inline const std::shared_ptr<BitIStream>& stream() {
145  return m_in;
146  }
147 };
148 
156 #define DECODER_CTOR(env, in) \
157  inline Decoder(Env&& env, Input& in) \
158  : Decoder(std::move(env), \
159  std::make_shared<BitIStream>(in)) {} \
160  inline Decoder(Env&& env, std::shared_ptr<BitIStream> in) \
161  : tdc::Decoder(std::move(env), in)
162 
163 }
164 
Represents a generic range of positive integers.
Definition: Range.hpp:16
Encoder(Env &&env, std::shared_ptr< BitOStream > out, literals_t &&literals)
Constructor.
Definition: Coder.hpp:29
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
constexpr uint_fast8_t bits_for(size_t n)
Computes the number of bits required to store the given integer value.
Represents a compiler-level fixed range.
Definition: Range.hpp:83
Wrapper for input streams that provides bitwise reading functionality.
Definition: BitIStream.hpp:16
std::shared_ptr< BitIStream > m_in
The underlying bit input stream.
Definition: Coder.hpp:91
Encoder(Env &&env, Output &out, literals_t &&literals)
Convenience constructor.
Definition: Coder.hpp:44
Decoder(Env &&env, Input &in)
Convenience constructor.
Definition: Coder.hpp:106
size_t max() const
Yields the range&#39;s maximum value.
Definition: Range.hpp:41
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
Wrapper for output streams that provides bitwise writing functionality.
Definition: BitOStream.hpp:17
size_t min() const
Yields the range&#39;s minimum value.
Definition: Range.hpp:37
An abstraction layer for algorithm output.
Definition: Output.hpp:23
const std::shared_ptr< BitIStream > & stream()
Definition: Coder.hpp:144
bool eof() const
Tests whether the end of the bit input stream has been reached.
Definition: Coder.hpp:113
std::shared_ptr< BitOStream > m_out
The underlying bit output stream.
Definition: Coder.hpp:18
value_t decode(const Range &r)
Decodes an arbitrary-range integer value.
Definition: Coder.hpp:127
Decoder(Env &&env, std::shared_ptr< BitIStream > in)
Constructor.
Definition: Coder.hpp:98
Base for data encoders.
Definition: Coder.hpp:14
value_t decode(const BitRange &r)
Decodes a bit.
Definition: Coder.hpp:140
void encode(value_t v, const BitRange &r)
Encodes a bit.
Definition: Coder.hpp:75
Local environment for a compression/encoding/decompression call.
void encode(value_t v, const Range &r)
Encodes an arbitrary-range integer value.
Definition: Coder.hpp:61
Base for data decoders.
Definition: Coder.hpp:87
Interface for algorithms.
Definition: Algorithm.hpp:15
const std::shared_ptr< BitOStream > & stream()
Definition: Coder.hpp:79
An abstraction layer for algorithm input.
Definition: Input.hpp:37