tudocomp
– The TU Dortmund Compression Framework
EspCompressor.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
6 
7 #include <tudocomp/util.hpp>
8 #include <tudocomp/Env.hpp>
13 
15 
17 
18 namespace tdc {
19 
20 template<typename slp_coder_t, typename ipd_t = esp::StdUnorderedMapIPD>
21 class EspCompressor: public Compressor {
22 public:
23  inline static Meta meta() {
24  Meta m("compressor", "esp", "ESP based grammar compression");
25  m.option("slp_coder").templated<slp_coder_t, esp::PlainSLPCoder>("slp_coder");
26  m.option("ipd").templated<ipd_t, esp::StdUnorderedMapIPD>("ipd");
27  return m;
28  }
29 
31 
32  inline virtual void compress(Input& input, Output& output) override {
33  using namespace esp;
34 
35  auto phase0 = StatPhase("ESP Compressor");
36 
37  EspContext<ipd_t> context { &env(), true };
38  SLP slp;
39 
40  {
41  auto phase1 = StatPhase("Compress Phase");
42 
43  auto phase2 = StatPhase("Creating input view");
44  auto in = input.as_view();
45 
46  context.debug.input_string(in);
47 
48  phase2.split("ESP Algorithm");
49  slp = context.generate_grammar(std::move(in));
50  }
51 
52  phase0.log_stat("SLP size", slp.rules.size());
53  phase0.log_stat("ext_size2_total", context.ipd_stats.ext_size2_total);
54  phase0.log_stat("ext_size3_total", context.ipd_stats.ext_size3_total);
55  phase0.log_stat("ext_size3_unique", context.ipd_stats.ext_size3_unique);
56  phase0.log_stat("int_size2_total", context.ipd_stats.int_size2_total);
57  phase0.log_stat("int_size2_unique", context.ipd_stats.int_size2_unique);
58 
59  {
60  auto phase1 = StatPhase("Encode Phase");
61 
62  auto phase2 = StatPhase("Creating strategy");
63  const slp_coder_t strategy { this->env().env_for_option("slp_coder") };
64 
65  phase2.split("Encode SLP");
66  strategy.encode(context.debug, std::move(slp), output);
67  }
68 
69  context.debug.print_all();
70  }
71 
72  inline virtual void decompress(Input& input, Output& output) override {
73  auto phase0 = StatPhase("ESP Decompressor");
74 
75  auto phase1 = StatPhase("Creating strategy");
76  const slp_coder_t strategy { this->env().env_for_option("slp_coder") };
77  phase1.split("Decode SLP");
78  auto slp = strategy.decode(input);
79 
80  phase1.split("Create output stream");
81  auto out = output.as_stream();
82 
83  phase1.split("Derive text");
84  if (!slp.empty) {
85  slp.derive_text(out);
86  } else {
87  out << ""_v;
88  }
89  }
90 };
91 
92 }
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
Env env_for_option(const std::string &option) const
Create the environment for a sub algorithm option.
Definition: Env.hpp:34
Base for data compressors.
Definition: Compressor.hpp:19
virtual void decompress(Input &input, Output &output) override
Decompress the given input to the given output.
Provides access to runtime and memory measurement in statistics phases.
Definition: StatPhase.hpp:44
static Meta meta()
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
OutputStream as_stream() const
Creates a stream that allows for character-wise output.
An abstraction layer for algorithm output.
Definition: Output.hpp:23
virtual void compress(Input &input, Output &output) override
Compress the given input to the given output.
void templated(const std::string &accepted_type)
Declares that this option accepts values of the specified Algorithm type T.
Definition: Meta.hpp:93
Compressor(Compressor const &)=default
InputView as_view() const
Provides a view on the input that allows for random access.
Definition: Input.hpp:260
OptionBuilder option(const std::string &name)
Declares an accepted option for this algorithm.
Definition: Meta.hpp:216
An abstraction layer for algorithm input.
Definition: Input.hpp:37