tudocomp
– The TU Dortmund Compression Framework
AlgorithmPattern.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 #include <utility>
6 #include <vector>
7 
8 namespace tdc {
10  namespace pattern {
11  /*
12  Representation of an algorithm consisting of only those
13  options that are relevant for selecting a statically
14  defined implementation
15  */
16  class Algorithm;
17  class Arg;
18  class Algorithm {
19  std::string m_name;
20  std::vector<Arg> m_arguments;
21  public:
22  inline Algorithm() {}
23  inline Algorithm(std::string&& name, std::vector<Arg>&& args):
24  m_name(std::move(name)),
25  m_arguments(std::move(args)) {}
26  inline Algorithm(const Algorithm& other):
27  m_name(other.m_name),
28  m_arguments(other.m_arguments) {}
29 
30  inline std::string& name() {
31  return m_name;
32  }
33  inline const std::string& name() const {
34  return m_name;
35  }
36  inline std::vector<Arg>& arguments() {
37  return m_arguments;
38  }
39  inline const std::vector<Arg>& arguments() const {
40  return m_arguments;
41  }
42  inline std::string to_string(bool omit_keyword = false) const;
43  };
44  class Arg {
45  std::string m_name;
46  Algorithm m_algorithm;
47  public:
48  inline Arg(std::string&& name, Algorithm&& alg):
49  m_name(std::move(name)),
50  m_algorithm(std::move(alg)) {}
51  inline std::string& name() {
52  return m_name;
53  }
54  inline const std::string& name() const {
55  return m_name;
56  }
57  inline Algorithm& algorithm() {
58  return m_algorithm;
59  }
60  inline const Algorithm& algorithm() const {
61  return m_algorithm;
62  }
63  inline std::string to_string(bool omit_keyword = false) const;
64  };
65 
66  inline std::ostream& operator<<(std::ostream& os,
67  const Algorithm& x) {
68  os << x.to_string();
69  return os;
70  }
71  inline std::ostream& operator<<(std::ostream& os,
72  const Arg& x) {
73  os << x.to_string();
74  return os;
75  }
76 
77  inline std::string Algorithm::to_string(bool omit_keyword) const {
78  std::stringstream ss;
79  ss << name();
80  if (arguments().size() > 0) {
81  ss << "(";
82  bool first = true;
83  for (auto& a: arguments()) {
84  if (!first) {
85  ss << ", ";
86  }
87  ss << a.to_string(omit_keyword);
88  first = false;
89  }
90  ss << ")";
91  }
92  return ss.str();
93  }
94 
95  inline std::string Arg::to_string(bool omit_keyword) const {
96  std::stringstream ss;
97  if (omit_keyword) {
98  ss << algorithm().to_string(omit_keyword);
99  } else {
100  ss << name() << " = " << algorithm().to_string(omit_keyword);
101  }
102  return ss.str();
103  }
104 
105  inline bool operator==(const Algorithm &lhs, const Algorithm &rhs);
106  inline bool operator<(const Algorithm &lhs, const Algorithm &rhs);
107  inline bool operator==(const Arg &lhs, const Arg &rhs);
108  inline bool operator<(const Arg &lhs, const Arg &rhs);
109 
110  inline bool operator!=(const Algorithm &lhs, const Algorithm &rhs) {
111  return !(lhs == rhs);
112  }
113  inline bool operator!=(const Arg &lhs, const Arg &rhs) {
114  return !(lhs == rhs);
115  }
116 
117  inline bool operator==(const Algorithm &lhs, const Algorithm &rhs) {
118  if (lhs.name() != rhs.name()) return false;
119  if (lhs.arguments() != rhs.arguments()) return false;
120  return true;
121  }
122  inline bool operator<(const Algorithm &lhs, const Algorithm &rhs) {
123  if (lhs.name() != rhs.name()) return lhs.name() < rhs.name();
124  if (lhs.arguments() != rhs.arguments()) return lhs.arguments() < rhs.arguments();
125  return false;
126  }
127  inline bool operator==(const Arg &lhs, const Arg &rhs) {
128  if (lhs.name() != rhs.name()) return false;
129  if (lhs.algorithm() != rhs.algorithm()) return false;
130  return true;
131  }
132  inline bool operator<(const Arg &lhs, const Arg &rhs) {
133  if (lhs.name() != rhs.name()) return lhs.name() < rhs.name();
134  if (lhs.algorithm() != rhs.algorithm()) return lhs.algorithm() < rhs.algorithm();
135  return false;
136  }
137  }
139 }
bool operator<(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
bool operator!=(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
std::ostream & operator<<(std::ostream &os, const uint_impl_t< N > &v)
Definition: uint_t.hpp:135
bool operator==(const ConstGenericView< uliteral_t > &lhs, const ConstGenericView< uliteral_t > &rhs)
std::string to_string(tdc::uint_impl_t< N > value)
Definition: uint_t.hpp:241