tudocomp
– The TU Dortmund Compression Framework
FibonacciGenerator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/Generator.hpp>
4 
5 namespace tdc {
6 
8 class FibonacciGenerator : public Generator {
9 
10 public:
11  inline static Meta meta() {
12  Meta m("generator", "fib", "Generates the n-th Fibonacci word.");
13  m.option("n").dynamic();
14  return m;
15  }
16 
17  inline static std::string generate(size_t n) {
18  if(n == 1) return "b";
19  if(n == 2) return "a";
20 
21  std::string vold = "b";
22  std::string old = "a";
23 
24  vold.reserve(std::pow(1.62, n-1));
25  old.reserve(std::pow(1.62, n));
26 
27  for(size_t i = 2; i < n; ++i) {
28  std::string tmp = old + vold;
29  vold = old;
30  old = tmp;
31  }
32 
33  DCHECK_LE(old.length(), std::pow(1.62, n));
34  DCHECK_LE(vold.length(), std::pow(1.62, n-1));
35  return old;
36  }
37 
38  using Generator::Generator;
39 
40  inline virtual std::string generate() override {
41  return generate(env().option("n").as_integer());
42  }
43 };
44 
45 } //ns
46 
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.
static std::string generate(size_t n)
Generates the n-th Fibonacci 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
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