dReal4
symbolic_variables.h
1 #pragma once
2 
3 #include <cstddef>
4 #include <functional>
5 #include <initializer_list>
6 #include <ostream>
7 #include <set>
8 #include <string>
9 
10 #include "dreal/symbolic/symbolic_variable.h"
11 
12 namespace dreal {
13 namespace drake {
14 namespace symbolic {
15 
16 /** Represents a set of variables.
17  *
18  * This class is based on std::set<Variable>. The intent is to add things that
19  * we need including set-union (Variables::insert, operator+, operator+=),
20  * set-minus (Variables::erase, operator-, operator-=), and subset/superset
21  * checking functions (Variables::IsSubsetOf, Variables::IsSupersetOf,
22  * Variables::IsStrictSubsetOf, Variables::IsStrictSupersetOf).
23  */
24 
25 class Variables {
26  public:
27  Variables(const Variables&) = default;
28  Variables& operator=(const Variables&) = default;
29  Variables(Variables&&) = default;
30  Variables& operator=(Variables&&) = default;
31 
32  typedef typename std::set<Variable>::size_type size_type;
33  typedef typename std::set<Variable>::iterator iterator;
34  typedef typename std::set<Variable>::const_iterator const_iterator;
35  typedef typename std::set<Variable>::reverse_iterator reverse_iterator;
36  typedef typename std::set<Variable>::const_reverse_iterator
37  const_reverse_iterator;
38 
39  /** Default constructor. */
40  Variables() = default;
41 
42  /** Default destructor. */
43  ~Variables() = default;
44 
45  /** List constructor. */
46  Variables(std::initializer_list<Variable> init);
47 
48  /** Returns hash value. */
49  size_t get_hash() const;
50 
51  /** Returns the number of elements. */
52  size_type size() const { return vars_.size(); }
53 
54  /** Checks if this set is empty or not. */
55  bool empty() const { return vars_.empty(); }
56 
57  /** Returns string representation of Variables. */
58  std::string to_string() const;
59 
60  /** Returns an iterator to the beginning. */
61  iterator begin() { return vars_.begin(); }
62  /** Returns an iterator to the end. */
63  iterator end() { return vars_.end(); }
64  /** Returns an iterator to the beginning. */
65  const_iterator begin() const { return vars_.cbegin(); }
66  /** Returns an iterator to the end. */
67  const_iterator end() const { return vars_.cend(); }
68  /** Returns a const iterator to the beginning. */
69  const_iterator cbegin() const { return vars_.cbegin(); }
70  /** Returns a const iterator to the end. */
71  const_iterator cend() const { return vars_.cend(); }
72  /** Returns a reverse iterator to the beginning. */
73  reverse_iterator rbegin() { return vars_.rbegin(); }
74  /** Returns a reverse iterator to the end. */
75  reverse_iterator rend() { return vars_.rend(); }
76  /** Returns a reverse iterator to the beginning. */
77  const_reverse_iterator rbegin() const { return vars_.crbegin(); }
78  /** Returns a reverse iterator to the end. */
79  const_reverse_iterator rend() const { return vars_.crend(); }
80  /** Returns a const reverse-iterator to the beginning. */
81  const_reverse_iterator crbegin() const { return vars_.crbegin(); }
82  /** Returns a const reverse-iterator to the end. */
83  const_reverse_iterator crend() const { return vars_.crend(); }
84 
85  /** Inserts a variable @p var into a set. */
86  void insert(const Variable& var) { vars_.insert(var); }
87  /** Inserts variables in [@p first, @p last) into a set. */
88  template <class InputIt>
89  void insert(InputIt first, InputIt last) {
90  vars_.insert(first, last);
91  }
92  /** Inserts variables in @p vars into a set. */
93  void insert(const Variables& vars) { vars_.insert(vars.begin(), vars.end()); }
94 
95  /** Erases @p key from a set. Return number of erased elements (0 or 1). */
96  size_type erase(const Variable& key) { return vars_.erase(key); }
97 
98  /** Erases variables in @p vars from a set. Return number of erased
99  elements ([0, vars.size()]). */
100  size_type erase(const Variables& vars);
101 
102  /** Finds element with specific key. */
103  iterator find(const Variable& key) { return vars_.find(key); }
104  const_iterator find(const Variable& key) const { return vars_.find(key); }
105 
106  /** Return true if @p key is included in the Variables. */
107  bool include(const Variable& key) const { return find(key) != end(); }
108 
109  /** Return true if @p vars is a subset of the Variables. */
110  bool IsSubsetOf(const Variables& vars) const;
111  /** Return true if @p vars is a superset of the Variables. */
112  bool IsSupersetOf(const Variables& vars) const;
113  /** Return true if @p vars is a strict subset of the Variables. */
114  bool IsStrictSubsetOf(const Variables& vars) const;
115  /** Return true if @p vars is a strict superset of the Variables. */
116  bool IsStrictSupersetOf(const Variables& vars) const;
117 
118  friend bool operator==(const Variables& vars1, const Variables& vars2);
119 
120  friend bool operator<(const Variables& vars1, const Variables& vars2);
121 
122  friend std::ostream& operator<<(std::ostream&, const Variables& vars);
123 
124  friend Variables intersect(const Variables& vars1, const Variables& vars2);
125 
126  private:
127  /* Constructs from std::set<Variable>. */
128  explicit Variables(std::set<Variable> vars);
129 
130  std::set<Variable> vars_;
131 };
132 
133 /** Updates @p var1 with the result of set-union(@p var1, @p var2). */
134 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
135 Variables operator+=(Variables& vars1, const Variables& vars2);
136 /** Updates @p vars with the result of set-union(@p vars, { @p var }). */
137 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
138 Variables operator+=(Variables& vars, const Variable& var);
139 /** Returns set-union of @p var1 and @p var2. */
140 Variables operator+(Variables vars1, const Variables& vars2);
141 /** Returns set-union of @p vars and {@p var}. */
142 Variables operator+(Variables vars, const Variable& var);
143 /** Returns set-union of {@p var} and @p vars. */
144 Variables operator+(const Variable& var, Variables vars);
145 
146 /** Updates @p var1 with the result of set-minus(@p var1, @p var2). */
147 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
148 Variables operator-=(Variables& vars1, const Variables& vars2);
149 /** Updates @p vars with the result of set-minus(@p vars, {@p var}). */
150 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
151 Variables operator-=(Variables& vars, const Variable& var);
152 /** Returns set-minus(@p var1, @p vars2). */
153 Variables operator-(Variables vars1, const Variables& vars2);
154 /** Returns set-minus(@p vars, { @p var }). */
155 Variables operator-(Variables vars, const Variable& var);
156 
157 /** Returns the intersection of @p vars1 and @p vars2.
158  *
159  * This function has a time complexity of `O(N₁ + N₂)` where `N₁` and `N₂` are
160  * the size of @p vars1 and @p vars2 respectively.
161  */
162 Variables intersect(const Variables& vars1, const Variables& vars2);
163 
164 } // namespace symbolic
165 
166 /** Computes the hash value of a symbolic variables. */
167 template <>
168 struct hash_value<symbolic::Variables> {
169  size_t operator()(const symbolic::Variables& vars) const {
170  return vars.get_hash();
171  }
172 };
173 
174 } // namespace drake
175 } // namespace dreal
const_reverse_iterator rend() const
Returns a reverse iterator to the end.
Definition: symbolic_variables.h:79
friend Variables intersect(const Variables &vars1, const Variables &vars2)
Returns the intersection of vars1 and vars2.
Definition: symbolic_variables.cc:132
const_iterator begin() const
Returns an iterator to the beginning.
Definition: symbolic_variables.h:65
iterator begin()
Returns an iterator to the beginning.
Definition: symbolic_variables.h:61
size_type erase(const Variable &key)
Erases key from a set.
Definition: symbolic_variables.h:96
iterator find(const Variable &key)
Finds element with specific key.
Definition: symbolic_variables.h:103
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
const_reverse_iterator crbegin() const
Returns a const reverse-iterator to the beginning.
Definition: symbolic_variables.h:81
const_iterator cbegin() const
Returns a const iterator to the beginning.
Definition: symbolic_variables.h:69
void insert(const Variable &var)
Inserts a variable var into a set.
Definition: symbolic_variables.h:86
std::string to_string() const
Returns string representation of Variables.
Definition: symbolic_variables.cc:35
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Computes the hash value of v using std::hash.
Definition: hash.h:35
const_reverse_iterator rbegin() const
Returns a reverse iterator to the beginning.
Definition: symbolic_variables.h:77
bool IsStrictSubsetOf(const Variables &vars) const
Return true if vars is a strict subset of the Variables.
Definition: symbolic_variables.cc:58
const_iterator end() const
Returns an iterator to the end.
Definition: symbolic_variables.h:67
bool IsSupersetOf(const Variables &vars) const
Return true if vars is a superset of the Variables.
Definition: symbolic_variables.cc:54
reverse_iterator rbegin()
Returns a reverse iterator to the beginning.
Definition: symbolic_variables.h:73
void insert(const Variables &vars)
Inserts variables in vars into a set.
Definition: symbolic_variables.h:93
iterator end()
Returns an iterator to the end.
Definition: symbolic_variables.h:63
size_type size() const
Returns the number of elements.
Definition: symbolic_variables.h:52
const_iterator cend() const
Returns a const iterator to the end.
Definition: symbolic_variables.h:71
bool IsSubsetOf(const Variables &vars) const
Return true if vars is a subset of the Variables.
Definition: symbolic_variables.cc:49
bool include(const Variable &key) const
Return true if key is included in the Variables.
Definition: symbolic_variables.h:107
bool empty() const
Checks if this set is empty or not.
Definition: symbolic_variables.h:55
reverse_iterator rend()
Returns a reverse iterator to the end.
Definition: symbolic_variables.h:75
const_reverse_iterator crend() const
Returns a const reverse-iterator to the end.
Definition: symbolic_variables.h:83
Variables()=default
Default constructor.
Represents a set of variables.
Definition: symbolic_variables.h:25
bool IsStrictSupersetOf(const Variables &vars) const
Return true if vars is a strict superset of the Variables.
Definition: symbolic_variables.cc:65
void insert(InputIt first, InputIt last)
Inserts variables in [first, last) into a set.
Definition: symbolic_variables.h:89
size_t get_hash() const
Returns hash value.
Definition: symbolic_variables.cc:31
~Variables()=default
Default destructor.