Skip to content

Commit 24c4418

Browse files
authored
simplify expression contains functions (#306)
1 parent 50090b5 commit 24c4418

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/form/expression.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,24 @@ bool Expression::contains(const Expression& e) const {
105105
if (*this == e) {
106106
return true;
107107
}
108-
for (auto& c : children) {
109-
if (c.contains(e)) {
110-
return true;
111-
}
112-
}
113-
return false;
108+
return std::any_of(children.begin(), children.end(),
109+
[&](const Expression& c) { return c.contains(e); });
114110
}
115111

116112
bool Expression::contains(Type t) const {
117113
if (type == t) {
118114
return true;
119115
}
120-
for (auto& c : children) {
121-
if (c.contains(t)) {
122-
return true;
123-
}
116+
return std::any_of(children.begin(), children.end(),
117+
[&](const Expression& c) { return c.contains(t); });
118+
}
119+
120+
bool Expression::contains(Type t, const std::string& name) const {
121+
if (type == t && this->name == name) {
122+
return true;
124123
}
125-
return false;
124+
return std::any_of(children.begin(), children.end(),
125+
[&](const Expression& c) { return c.contains(t, name); });
126126
}
127127

128128
size_t Expression::numTerms() const {

src/form/expression.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Expression {
4949

5050
bool contains(Type t) const;
5151

52+
bool contains(Type t, const std::string& name) const;
53+
5254
size_t numTerms() const;
5355

5456
Expression& newChild(const Expression& e);

0 commit comments

Comments
 (0)