carl  24.04
Computer ARithmetic Library
Variables.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Variable.h"
6 
7 #include <algorithm>
8 #include <variant>
9 #include <vector>
10 
11 namespace carl {
12 
14  bool filter_bool = true;
15  bool filter_real = true;
16  bool filter_int = true;
17  bool filter_uninterpreted = true;
18  bool filter_bitvector = true;
19 
20 public:
22  return variable_type_filter();
23  }
24  static variable_type_filter excluding(std::initializer_list<VariableType> i) {
25  auto res = variable_type_filter();
26  for (const auto t : i) {
27  if (t == VariableType::VT_BOOL) res.filter_bool = false;
28  else if (t == VariableType::VT_REAL) res.filter_real = false;
29  else if (t == VariableType::VT_INT) res.filter_int = false;
30  else if (t == VariableType::VT_UNINTERPRETED) res.filter_uninterpreted = false;
31  else if (t == VariableType::VT_BITVECTOR) res.filter_bitvector = false;
32  }
33  return res;
34  }
35  static variable_type_filter only(std::initializer_list<VariableType> i) {
36  auto res = variable_type_filter();
37  res.filter_bool = false;
38  res.filter_real = false;
39  res.filter_int = false;
40  res.filter_uninterpreted = false;
41  res.filter_bitvector = false;
42  for (const auto t : i) {
43  if (t == VariableType::VT_BOOL) res.filter_bool = true;
44  else if (t == VariableType::VT_REAL) res.filter_real = true;
45  else if (t == VariableType::VT_INT) res.filter_int = true;
46  else if (t == VariableType::VT_UNINTERPRETED) res.filter_uninterpreted = true;
47  else if (t == VariableType::VT_BITVECTOR) res.filter_bitvector = true;
48  }
49  return res;
50  }
51  static auto boolean() {
52  return only({VariableType::VT_BOOL});
53  }
54  static auto integer() {
55  return only({VariableType::VT_INT});
56  }
57  static auto real() {
58  return only({VariableType::VT_REAL});
59  }
60  static auto arithmetic() {
62  }
63  static auto bitvector() {
65  }
66  static auto uninterpreted() {
68  }
69 
70  bool apply(VariableType v) const {
71  return (
74  (filter_int && v == VariableType::VT_INT) ||
77  );
78  }
79  bool apply(Variable v) const {
80  return apply(v.type());
81  }
82 };
84 public:
85  friend bool operator==(const carlVariables& lhs, const carlVariables& rhs);
86  friend std::ostream& operator<<(std::ostream& os, const carlVariables& vars);
87  using iterator = std::vector<Variable>::iterator;
88  using const_iterator = std::vector<Variable>::const_iterator;
89 private:
90  mutable std::vector<Variable> mVariables;
91  mutable std::size_t mAddedSinceCompact = 0;
93 
94  void compact(bool force = false) const {
95  if ((force && mAddedSinceCompact > 0) || (mAddedSinceCompact > mVariables.size() / 2)) {
96  std::sort(mVariables.begin(), mVariables.end());
97  mVariables.erase(std::unique(mVariables.begin(), mVariables.end()), mVariables.end());
99  }
100  }
101 public:
103  explicit carlVariables(std::initializer_list<Variable> i, variable_type_filter filter = variable_type_filter::all()) : mFilter(filter) {
104  add(i);
105  }
106  template<typename Iterator>
108  add(b, e);
109  }
110 
111  auto begin() const {
112  compact(true);
113  return mVariables.begin();
114  }
115  auto end() const {
116  return mVariables.end();
117  }
118  auto begin() {
119  compact(true);
120  return mVariables.begin();
121  }
122  auto end() {
123  return mVariables.end();
124  }
125 
126  bool empty() const {
127  return mVariables.empty();
128  }
129  std::size_t size() const {
130  compact(true);
131  return mVariables.size();
132  }
133  void clear() {
134  mVariables.clear();
135  mAddedSinceCompact = 0;
136  }
137  bool has(Variable var) const {
138  return std::find(mVariables.begin(), mVariables.end(), var) != mVariables.end();
139  }
140 
141  void add(Variable v) {
142  if (mFilter.apply(v)) {
143  mVariables.emplace_back(v);
145  compact();
146  }
147  }
148  template<typename Iterator>
149  void add(const Iterator& b, const Iterator& e) {
150  std::for_each(b, e,
151  [this](const auto& v){
152  if (mFilter.apply(v)) {
153  mVariables.emplace_back(v);
154  ++mAddedSinceCompact;
155  }
156  }
157  );
158  compact();
159  }
160  void add(std::initializer_list<Variable> i) {
161  add(i.begin(), i.end());
162  }
163 
164  void erase(Variable v) {
165  mVariables.erase(std::remove(mVariables.begin(), mVariables.end(), v), mVariables.end());
166  }
167 
168  const std::vector<Variable>& as_vector() const {
169  compact(true);
170  return mVariables;
171  }
172 
173  std::set<Variable> as_set() const {
174  compact(true);
175  return std::set<Variable>(mVariables.begin(), mVariables.end());
176  }
177 
179  return carlVariables(begin(), end(), f);
180  }
181 
182  auto boolean() const {
184  }
185  auto integer() const {
187  }
188  auto real() const {
190  }
191  auto arithmetic() const {
193  }
194  auto bitvector() const {
196  }
197  auto uninterpreted() const {
199  }
200 };
201 
202 inline void swap(Variable& lhs, Variable& rhs) {
203  auto tmp = lhs;
204  lhs = rhs;
205  rhs = tmp;
206 }
207 
208 inline bool operator==(const carlVariables& lhs, const carlVariables& rhs) {
209  lhs.compact(true);
210  rhs.compact(true);
211  return lhs.mVariables == rhs.mVariables;
212 }
213 inline std::ostream& operator<<(std::ostream& os, const carlVariables& vars) {
214  return os << vars.mVariables;
215 }
216 
217 /// Return the variables as collected by the methods above.
218 template<typename T>
219 inline carlVariables variables(const T& t) {
220  carlVariables vars;
221  variables(t, vars);
222  return vars;
223 }
224 
225 template<typename T>
226 inline carlVariables boolean_variables(const T& t) {
228  variables(t, vars);
229  return vars;
230 }
231 
232 template<typename T>
233 inline carlVariables integer_variables(const T& t) {
235  variables(t, vars);
236  return vars;
237 }
238 
239 template<typename T>
240 inline carlVariables real_variables(const T& t) {
242  variables(t, vars);
243  return vars;
244 }
245 
246 template<typename T>
249  variables(t, vars);
250  return vars;
251 }
252 
253 template<typename T>
254 inline carlVariables bitvector_variables(const T& t) {
256  variables(t, vars);
257  return vars;
258 }
259 
260 template<typename T>
263  variables(t, vars);
264  return vars;
265 }
266 
267 }
carl is the main namespace for the library.
carlVariables arithmetic_variables(const T &t)
Definition: Variables.h:247
void swap(Variable &lhs, Variable &rhs)
Definition: Variables.h:202
carlVariables real_variables(const T &t)
Definition: Variables.h:240
carlVariables uninterpreted_variables(const T &t)
Definition: Variables.h:261
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
carlVariables bitvector_variables(const T &t)
Definition: Variables.h:254
VariableType
Several types of variables are supported.
Definition: Variable.h:28
carlVariables boolean_variables(const T &t)
Definition: Variables.h:226
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
carlVariables integer_variables(const T &t)
Definition: Variables.h:233
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
PositionIteratorType Iterator
Definition: OPBImporter.cpp:23
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
constexpr VariableType type() const noexcept
Retrieves the type of the variable.
Definition: Variable.h:131
bool apply(Variable v) const
Definition: Variables.h:79
static auto integer()
Definition: Variables.h:54
static auto bitvector()
Definition: Variables.h:63
static auto arithmetic()
Definition: Variables.h:60
static variable_type_filter excluding(std::initializer_list< VariableType > i)
Definition: Variables.h:24
static auto boolean()
Definition: Variables.h:51
static variable_type_filter only(std::initializer_list< VariableType > i)
Definition: Variables.h:35
static auto uninterpreted()
Definition: Variables.h:66
bool apply(VariableType v) const
Definition: Variables.h:70
static variable_type_filter all()
Definition: Variables.h:21
void add(const Iterator &b, const Iterator &e)
Definition: Variables.h:149
friend std::ostream & operator<<(std::ostream &os, const carlVariables &vars)
Definition: Variables.h:213
std::size_t mAddedSinceCompact
Definition: Variables.h:91
void erase(Variable v)
Definition: Variables.h:164
carlVariables(const Iterator &b, const Iterator &e, variable_type_filter filter=variable_type_filter::all())
Definition: Variables.h:107
auto end() const
Definition: Variables.h:115
const std::vector< Variable > & as_vector() const
Definition: Variables.h:168
std::set< Variable > as_set() const
Definition: Variables.h:173
variable_type_filter mFilter
Definition: Variables.h:92
void add(std::initializer_list< Variable > i)
Definition: Variables.h:160
auto bitvector() const
Definition: Variables.h:194
std::size_t size() const
Definition: Variables.h:129
auto arithmetic() const
Definition: Variables.h:191
void add(Variable v)
Definition: Variables.h:141
carlVariables filter(variable_type_filter &&f) const
Definition: Variables.h:178
std::vector< Variable > mVariables
Definition: Variables.h:90
std::vector< Variable >::const_iterator const_iterator
Definition: Variables.h:88
bool has(Variable var) const
Definition: Variables.h:137
void compact(bool force=false) const
Definition: Variables.h:94
std::vector< Variable >::iterator iterator
Definition: Variables.h:87
auto real() const
Definition: Variables.h:188
bool empty() const
Definition: Variables.h:126
carlVariables(std::initializer_list< Variable > i, variable_type_filter filter=variable_type_filter::all())
Definition: Variables.h:103
carlVariables(variable_type_filter filter=variable_type_filter::all())
Definition: Variables.h:102
auto uninterpreted() const
Definition: Variables.h:197
auto boolean() const
Definition: Variables.h:182
friend bool operator==(const carlVariables &lhs, const carlVariables &rhs)
Definition: Variables.h:208
auto begin() const
Definition: Variables.h:111
auto integer() const
Definition: Variables.h:185