tudocomp
– The TU Dortmund Compression Framework
PlainSLPCoder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/Algorithm.hpp>
7 
8 namespace tdc {namespace esp {
9  class PlainSLPCoder: public Algorithm {
10  public:
11  inline static Meta meta() {
12  Meta m("slp_coder", "plain");
13  return m;
14  };
15 
17 
18  inline void encode(DebugContext& debug, SLP&& slp, Output& output) const {
19  debug.encode_start();
20  auto max_val = slp.rules.size() + esp::GRAMMAR_PD_ELLIDED_PREFIX - 1;
21  auto bit_width = bits_for(max_val);
22  debug.encode_max_value(max_val, bit_width);
23  debug.encode_root_node(slp.root_rule);
24 
25  BitOStream bout(output.as_stream());
26  // Write header
27  // bit width
28  DCHECK_GE(bit_width, 1);
29  DCHECK_LE(bit_width, 63); // 64-bit sizes are
30  // restricted to 63 or less in practice
31 
32  if (slp.empty) {
33  bit_width = 0;
34  DCHECK(slp.rules.empty());
35  DCHECK_EQ(slp.root_rule, 0);
36  }
37 
38  bout.write_int(bit_width, 6);
39 
40  // root rule
41  bout.write_int(slp.root_rule, bit_width);
42 
43  // Write rules
44  debug.encode_rule_start();
45  for (auto& rule : slp.rules) {
46  debug.encode_rule(rule);
47  DCHECK_LE(rule[0], max_val);
48  DCHECK_LE(rule[1], max_val);
49  bout.write_int(rule[0], bit_width);
50  bout.write_int(rule[1], bit_width);
51  }
52  }
53 
54  inline SLP decode(Input& input) const {
55  BitIStream bin(input.as_stream());
56 
57  auto bit_width = bin.read_int<size_t>(6);
58  bool empty = (bit_width == 0);
59 
60  auto root_rule = bin.read_int<size_t>(bit_width);
61 
62  //std::cout << "in: Root rule: " << root_rule << "\n";
63 
64  esp::SLP slp;
65  slp.empty = empty;
66  slp.root_rule = root_rule;
67  slp.rules.reserve(std::pow(2, bit_width)); // TODO: Make more exact
68 
69  while (!bin.eof()) {
70  auto a = bin.read_int<size_t>(bit_width);
71  auto b = bin.read_int<size_t>(bit_width);
72  auto array = std::array<size_t, 2>{{ a, b, }};
73 
74  //std::cout << "IN: " << vec_to_debug_string(array) << "\n";
75 
76  slp.rules.push_back(array);
77  }
78 
79  return slp;
80  }
81  };
82 }}
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.
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
size_t GRAMMAR_PD_ELLIDED_PREFIX
Definition: SLP.hpp:10
void encode_root_node(size_t node)
Wrapper for input streams that provides bitwise reading functionality.
Definition: BitIStream.hpp:16
void encode(DebugContext &debug, SLP &&slp, Output &output) const
InputStream as_stream() const
Creates a stream that allows for character-wise reading of the input.
Definition: Input.hpp:264
Algorithm(Algorithm const &)=default
OutputStream as_stream() const
Creates a stream that allows for character-wise output.
void encode_max_value(size_t value, size_t bits)
Wrapper for output streams that provides bitwise writing functionality.
Definition: BitOStream.hpp:17
An abstraction layer for algorithm output.
Definition: Output.hpp:23
bool empty
Definition: SLP.hpp:15
SLP decode(Input &input) const
Interface for algorithms.
Definition: Algorithm.hpp:15
T read_int(size_t amount=sizeof(T) *CHAR_BIT)
Reads the integer value of the next amount bits in MSB first order.
Definition: BitIStream.hpp:119
void encode_rule(const T &rule)
An abstraction layer for algorithm input.
Definition: Input.hpp:37