Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active January 29, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/8da6a9aed00e27b2a5a156b7d95e3e99 to your computer and use it in GitHub Desktop.
Save deque-blog/8da6a9aed00e27b2a5a156b7d95e3e99 to your computer and use it in GitHub Desktop.
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