feat(util/rb_tree): add find_if method
This commit is contained in:
parent
d2cbd25985
commit
50e4c6f252
1 changed files with 16 additions and 0 deletions
|
@ -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) {
|
||||
if (n) {
|
||||
out << "(";
|
||||
|
@ -348,6 +361,9 @@ public:
|
|||
template<typename F>
|
||||
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
|
||||
void display(std::ostream & out) const { display(out, m_root.m_ptr); }
|
||||
|
||||
|
|
Loading…
Reference in a new issue