tudocomp
– The TU Dortmund Compression Framework
LFSCompressor.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <tuple>
5 
7 #include <tudocomp/util.hpp>
8 #include <tudocomp/io.hpp>
9 
13 
14 
16 
17 
18 
20 
21 
23 
24 namespace tdc {
25 namespace lfs {
26 
27 template<typename comp_strategy_t , typename coding_strat_t = EncodeStrategy<HuffmanCoder, EliasGammaCoder> >
28 class LFSCompressor : public Compressor {
29 private:
30 
31  typedef std::tuple<uint,uint,uint> non_term;
32  typedef std::vector<non_term> non_terminal_symbols;
33  typedef std::vector<std::pair<uint,uint>> rules;
34 
35 
36 public:
37 
38  inline static Meta meta() {
39  Meta m("compressor", "lfs_comp",
40  "LFS compression scheme");
41 
43  m.option("computing_strat").templated<comp_strategy_t>("computing_strat");
44  m.option("coding_strat").templated<coding_strat_t, EncodeStrategy<HuffmanCoder, EliasGammaCoder> >("coding_strat");
45  return m;
46  }
47 
48 
49  inline LFSCompressor(Env&& env):
50  Compressor(std::move(env))
51  {
52  DLOG(INFO) << "Compressor instantiated";
53  }
54  inline virtual void compress(Input& input, Output& output) override {
55 
56  StatPhase::wrap("lfs compressor", [&]{
57 
58 
59  non_terminal_symbols nts_symbols = non_terminal_symbols();
60  rules dictionary = rules();
61  auto in = input.as_view();
62  if(in.size()>1){
63  comp_strategy_t strategy(env().env_for_option("computing_strat"));
64 
65  StatPhase::wrap("computing lrfs", [&]{
66  //compute dictionary and nts.
67  strategy.compute_rules( in, dictionary, nts_symbols);
68 
69  DLOG(INFO)<<"dict size: "<<dictionary.size() << std::endl;
70  DLOG(INFO)<<"symbols:"<<nts_symbols.size()<< std::endl;
71  });
72  StatPhase::log("Number of CFG rules", dictionary.size());
73  }
74 
75 
76 
77 
78 
79  // if(dictionary.size()==0){
80  // return;
81  // }
82 
83 
84  StatPhase::wrap("encoding input", [&]{
85 
86 
87  //StatPhase encode("encoding input");
88  coding_strat_t coding_strategy(env().env_for_option("coding_strat"));
89 
90  coding_strategy.encode(in, output, dictionary, nts_symbols);
91 
92  });
93 
94  });
95 
96 
97  }
98 
99  inline virtual void decompress(Input& input, Output& output) override {
100 
101  coding_strat_t strategy(env().env_for_option("coding_strat"));
102 
103  strategy.decode(input,output);
104  }
105 
106 };
107 
108 }
109 
110 
111 }
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
Base for data compressors.
Definition: Compressor.hpp:19
void needs_sentinel_terminator()
Indicates that this Algorithm requires a null terminator symbol in Input.
Definition: Meta.hpp:271
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
static void log(const char *key, const T &value)
Logs a user statistic for the current phase.
Definition: StatPhase.hpp:218
void templated(const std::string &accepted_type)
Declares that this option accepts values of the specified Algorithm type T.
Definition: Meta.hpp:93
virtual void compress(Input &input, Output &output) override
Compress the given input to the given output.
static auto wrap(const char *title, F func) -> typename std::result_of< F(StatPhase &)>::type
Executes a lambda as a single statistics phase.
Definition: StatPhase.hpp:143
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
Local environment for a compression/encoding/decompression call.
virtual void decompress(Input &input, Output &output) override
Decompress the given input to the given output.
An abstraction layer for algorithm input.
Definition: Input.hpp:37