tudocomp
– The TU Dortmund Compression Framework
ThueMorseGenerator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/Generator.hpp>
4 
5 namespace tdc {
6 
10 class ThueMorseGenerator : public Generator {
11 
12 public:
13  inline static Meta meta() {
14  Meta m("generator", "thue_morse", "Generates the n-th Thue Morse word.");
15  m.option("n").dynamic();
16  return m;
17  }
18 
19  inline static std::string generate(size_t n) {
20  CHECK_LT(n, 64) << "too long!"; // exception?
21 
22  if(n == 0) return "0";
23 
24  std::string a = "0";
25  a.reserve(1ULL << n); // TODO: wait, was this enough?
26  for(size_t i = 1; i < n; ++i) {
27  const size_t len = a.length();
28  for(size_t j = 0; j < len; ++j) {
29  a.push_back(a[j] == '0' ? '1' : '0');
30  }
31  }
32 
33  DCHECK_LE(a.length(), 1ULL << n);
34  return a;
35  }
36 
37  using Generator::Generator;
38 
39  inline virtual std::string generate() override {
40  return generate(env().option("n").as_integer());
41  }
42 };
43 
44 } //ns
45 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
virtual std::string generate() override
Generates a string based on the environment settings.
len_compact_t len
Definition: LZSSFactors.hpp:38
Generates the n-th Thue Morse word.
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
Base for string generators.
Definition: Generator.hpp:13
static std::string generate(size_t n)
OptionBuilder option(const std::string &name)
Declares an accepted option for this algorithm.
Definition: Meta.hpp:216
void dynamic()
Declares that this option accepts values of a simple type that can be parsed from a string (e...
Definition: Meta.hpp:150