Last active
January 29, 2017 14:32
-
-
Save deque-blog/8da6a9aed00e27b2a5a156b7d95e3e99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Tag, typename Step> | |
expression optimize_op(op<Tag, expression> const& e, int neutral, Step step) | |
{ | |
int res = neutral; | |
std::vector<expression> subs; | |
for (expression const& sub: e.rands()) | |
{ | |
if (auto* i = get_as_cst(sub.get())) | |
{ | |
res = step(res, *i); | |
} | |
else | |
{ | |
subs.push_back(sub); | |
} | |
} | |
if (subs.empty()) return cst(res); | |
if (res != neutral) subs.push_back(cst(res)); | |
if (subs.size() == 1) return subs.front(); | |
return expression(op<Tag, expression>(subs)); | |
} | |
template<typename Range> | |
bool has_zero(Range const& subs) | |
{ | |
return end(subs) != boost::find_if(subs, [](expression const& sub) { | |
auto* i = get_as_cst(sub.get()); | |
return i && *i == 0; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment