tudocomp
– The TU Dortmund Compression Framework
ChainCompressor.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <tudocomp/Env.hpp>
5 #include <tudocomp/Registry.hpp>
6 #include <tudocomp/io.hpp>
9 #include <vector>
10 #include <memory>
11 
12 namespace tdc {
13 
14 class ChainCompressor: public Compressor {
15 public:
16  inline static Meta meta() {
17  Meta m("compressor", "chain");
18  m.option("first").dynamic_compressor();
19  m.option("second").dynamic_compressor();
20  return m;
21  }
22 
24  inline ChainCompressor() = delete;
25 
27  inline ChainCompressor(Env&& env):
28  Compressor(std::move(env)) {}
29 
30  template<class F>
31  inline void chain(Input& input, Output& output, bool reverse, F f) {
32  string_ref first_algo = "first";
33  string_ref second_algo = "second";
34  if (reverse) {
35  std::swap(first_algo, second_algo);
36  }
37 
38  auto run = [&](Input& i, Output& o, string_ref option) {
39  auto& option_value = env().option(option);
40  DCHECK(option_value.is_algorithm());
41 
42  auto av = option_value.as_algorithm();
43 
44  auto textds_flags = av.textds_flags();
45 
46  DVLOG(1) << "dynamic creation of" << av.name() << "\n";
47  auto compressor = create_algo_with_registry_dynamic(
48  tdc_algorithms::COMPRESSOR_REGISTRY, av);
49 
50  f(i, o, *compressor, textds_flags);
51  };
52 
53  std::vector<uint8_t> between_buf;
54  {
55  Output between(between_buf);
56  run(input, between, first_algo);
57  }
58  DLOG(INFO) << "Buffer between chain: " << vec_to_debug_string(between_buf);
59  {
60  Input between(between_buf);
61  run(between, output, second_algo);
62  }
63  }
64 
69  inline virtual void compress(Input& input, Output& output) override final {
70  chain(input, output, false, [](Input& i,
71  Output& o,
72  Compressor& c,
74  bool res = flags.has_restrictions();
75  if (res) {
76  auto i2 = Input(i, flags);
77  c.compress(i2, o);
78  } else {
79  c.compress(i, o);
80  }
81  });
82  }
83 
88  inline virtual void decompress(Input& input, Output& output) override final {
89  chain(input, output, true, [](Input& i,
90  Output& o,
91  Compressor& c,
93  bool res = flags.has_restrictions();
94  if (res) {
95  auto o2 = Output(o, flags);
96  c.decompress(i, o2);
97  } else {
98  c.decompress(i, o);
99  }
100  });
101  }
102 };
103 
104 }
105 
ChainCompressor()=delete
No default construction allowed.
virtual void compress(Input &input, Output &output) override final
Compress inp into out.
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
ChainCompressor(Env &&env)
Construct the class with an environment and the algorithms to chain.
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
io::Input Input
Convenience shortcut to io::Input.
Definition: io.hpp:17
std::string vec_to_debug_string(const T &s, size_t indent=0)
Builds the string representation of a vector of byte values, sorrounded by square brackets ([ and ])...
A const view into a slice of memory.
const OptionValue & option(const std::string &option) const
Get an option of this algorithm.
Definition: Env.hpp:44
Base for data compressors.
Definition: Compressor.hpp:19
void chain(Input &input, Output &output, bool reverse, F f)
ds::InputRestrictionsAndFlags textds_flags() const
void swap(IntVector< T > &lhs, IntVector< T > &rhs)
Definition: IntVector.hpp:532
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
An abstraction layer for algorithm output.
Definition: Output.hpp:23
void dynamic_compressor()
Declares that this option accepts values of a arbitrary Compressor type, dispatched at runtime...
Definition: Meta.hpp:203
virtual void compress(Input &input, Output &output)=0
Compress the given input to the given output.
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.
const AlgorithmValue & as_algorithm() const
virtual void decompress(Input &input, Output &output)=0
Decompress the given input to the given output.
virtual void decompress(Input &input, Output &output) override final
Decompress inp into out.
io::Output Output
Convenience shortcut to io::Output.
Definition: io.hpp:20
An abstraction layer for algorithm input.
Definition: Input.hpp:37