tudocomp
– The TU Dortmund Compression Framework
ASCIICoder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 #include <tudocomp/Coder.hpp>
5 
6 namespace tdc {
7 
15 class ASCIICoder : public Algorithm {
16 public:
19  inline static Meta meta() {
20  Meta m("coder", "ascii", "Simple ASCII encoding");
21  return m;
22  }
23 
25  ASCIICoder() = delete;
27 
29  class Encoder : public tdc::Encoder {
30  public:
32 
33  template<typename value_t>
34  inline void encode(value_t v, const Range&) {
35  std::ostringstream s;
36  s << v;
37  for(uint8_t c : s.str()) m_out->write_int(c);
38  m_out->write_int(':');
39  }
40 
41  template<typename value_t>
42  inline void encode(value_t v, const LiteralRange&) {
43  m_out->write_int(uint8_t(v));
44  }
45 
46  template<typename value_t>
47  inline void encode(value_t v, const BitRange&) {
48  m_out->write_int(v ? '1' : '0');
49  }
50  };
51 
53  class Decoder : public tdc::Decoder {
54  public:
56 
57  template<typename value_t>
58  inline value_t decode(const Range&) {
59  std::ostringstream os;
60  for(uint8_t c = m_in->read_int<uint8_t>();
61  c >= '0' && c <= '9';
62  c = m_in->read_int<uint8_t>()) {
63 
64  os << c;
65  }
66 
67  std::string s = os.str();
68 
69  value_t v;
70  std::istringstream is(s);
71  is >> v;
72  return v;
73  }
74 
75  template<typename value_t>
76  inline value_t decode(const LiteralRange&) {
77  return value_t(m_in->read_int<uint8_t>());
78  }
79 
80  template<typename value_t>
81  inline value_t decode(const BitRange&) {
82  uint8_t b = m_in->read_int<uint8_t>();
83  return (b != '0');
84  }
85  };
86 };
87 
88 template<>
89 inline void ASCIICoder::Encoder::encode<uliteral_t>(uliteral_t v, const Range& r) {
90  encode(size_t(v), r);
91 }
92 
93 template<>
94 inline uliteral_t ASCIICoder::Decoder::decode<uliteral_t>(const Range& r) {
95  return uliteral_t(decode<size_t>(r));
96 }
97 
98 }
99 
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
Represents the range of valid tdc::uliteral_t values.
Definition: Range.hpp:90
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
Represents a compiler-level fixed range.
Definition: Range.hpp:83
Decodes data from an ASCII character stream.
Definition: ASCIICoder.hpp:53
value_t decode(const LiteralRange &)
Definition: ASCIICoder.hpp:76
uint8_t uliteral_t
Type to represent signed single literals.
Definition: def.hpp:131
void encode(value_t v, const BitRange &)
Definition: ASCIICoder.hpp:47
void encode(value_t v, const Range &)
Definition: ASCIICoder.hpp:34
Defines data encoding to and decoding from a stream of ASCII characters.
Definition: ASCIICoder.hpp:15
value_t decode(const BitRange &)
Definition: ASCIICoder.hpp:81
static Meta meta()
Yields the coder&#39;s meta information.
Definition: ASCIICoder.hpp:19
std::shared_ptr< BitOStream > m_out
The underlying bit output stream.
Definition: Coder.hpp:18
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 Range &)
Definition: ASCIICoder.hpp:58
void encode(value_t v, const LiteralRange &)
Definition: ASCIICoder.hpp:42
Base for data decoders.
Definition: Coder.hpp:87
Interface for algorithms.
Definition: Algorithm.hpp:15
Encodes data to an ASCII character stream.
Definition: ASCIICoder.hpp:29