refactor(kernel/find_fn): simplify find_fn module
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
314c0822de
commit
42867d6fcd
1 changed files with 13 additions and 28 deletions
|
@ -9,34 +9,19 @@ Author: Leonardo de Moura
|
|||
#include "kernel/expr.h"
|
||||
|
||||
namespace lean {
|
||||
class find_fn {
|
||||
template<typename P>
|
||||
struct pred_fn {
|
||||
optional<expr> & m_result;
|
||||
P m_p;
|
||||
pred_fn(optional<expr> & result, P const & p):m_result(result), m_p(p) {}
|
||||
bool operator()(expr const & e, unsigned offset) {
|
||||
if (m_result) {
|
||||
return false; // already found result, stop the search
|
||||
} else if (m_p(e, offset)) {
|
||||
m_result = e;
|
||||
return false; // stop the search
|
||||
} else {
|
||||
return true; // continue the search
|
||||
}
|
||||
}
|
||||
};
|
||||
optional<expr> m_result;
|
||||
for_each_fn m_proc;
|
||||
public:
|
||||
template<typename P> find_fn(P const & p):m_proc(pred_fn<P>(m_result, p)) {}
|
||||
optional<expr> operator()(expr const & e) { m_proc(e); return m_result; }
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Return a subexpression of \c e that satisfies the predicate \c p.
|
||||
*/
|
||||
/** \brief Return a subexpression of \c e that satisfies the predicate \c p. */
|
||||
template<typename P> optional<expr> find(expr const & e, P p) {
|
||||
return find_fn(p)(e);
|
||||
optional<expr> result;
|
||||
for_each(e, [&](expr const & e, unsigned offset) {
|
||||
if (result) {
|
||||
return false;
|
||||
} else if (p(e, offset)) {
|
||||
result = e;
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue