feat(util/rb_tree): add find_if method

This commit is contained in:
Leonardo de Moura 2014-10-17 12:53:43 -07:00
parent d2cbd25985
commit 50e4c6f252

View file

@ -235,6 +235,19 @@ class rb_tree : public CMP {
} }
} }
template<typename F>
static optional<T> find_if(F && f, node_cell const * n) {
if (n) {
if (auto r = find_if(f, n->m_left.m_ptr))
return r;
if (f(n->m_value))
return optional<T>(n->m_value);
if (auto r = find_if(f, n->m_right.m_ptr))
return r;
}
return optional<T>();
}
static void display(std::ostream & out, node_cell const * n) { static void display(std::ostream & out, node_cell const * n) {
if (n) { if (n) {
out << "("; out << "(";
@ -348,6 +361,9 @@ public:
template<typename F> template<typename F>
void for_each(F && f) const { for_each(f, m_root.m_ptr); } void for_each(F && f) const { for_each(f, m_root.m_ptr); }
template<typename F>
optional<T> find_if(F && f) const { return find_if(f, m_root.m_ptr); }
// For debugging purposes // For debugging purposes
void display(std::ostream & out) const { display(out, m_root.m_ptr); } void display(std::ostream & out) const { display(out, m_root.m_ptr); }