gecko-dev/gfx/layers/TreeTraversal.h
Tim Stullich 78588ac104 Bug 1171312 - Add generic tree search algorithms for layers-related data structures. r=botond
--HG--
extra : source : a377fceec2fbab853eb2e847d98789250501aa96
extra : amend_source : 2e18afc459d4a2116a008e2482084d84c7c63e9c
2015-07-21 16:36:08 -07:00

88 lines
2.0 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_layers_TreeTraversal_h
#define mozilla_layers_TreeTraversal_h
namespace mozilla {
namespace layers {
#include <queue>
#include <stack>
/*
* Do a breadth-first search of the tree rooted at |aRoot|, and return the
* first visited node that satisfies |aCondition|, or nullptr if no such node
* was found.
*
* |Node| should have methods GetLastChild() and GetPrevSibling().
*/
template <typename Node, typename Condition>
const Node* BreadthFirstSearch(const Node* aRoot, const Condition& aCondition)
{
if (!aRoot) {
return nullptr;
}
std::queue<const Node*> queue;
queue.push(aRoot);
while (!queue.empty()) {
const Node* node = queue.front();
queue.pop();
if (aCondition(node)) {
return node;
}
for (const Node* child = node->GetLastChild();
child;
child = child->GetPrevSibling()) {
queue.push(child);
}
}
return nullptr;
}
/*
* Do a depth-first search of the tree rooted at |aRoot|, and return the
* first visited node that satisfies |aCondition|, or nullptr if no such node
* was found.
*
* |Node| should have methods GetLastChild() and GetPrevSibling().
*/
template <typename Node, typename Condition>
const Node* DepthFirstSearch(const Node* aRoot, const Condition& aCondition)
{
if (!aRoot) {
return nullptr;
}
std::stack<const Node*> stack;
stack.push(aRoot);
while (!stack.empty()) {
const Node* node = stack.top();
stack.pop();
if (aCondition(node)) {
return node;
}
for (const Node* child = node->GetLastChild();
child;
child = child->GetPrevSibling()) {
stack.push(child);
}
}
return nullptr;
}
}
}
#endif // mozilla_layers_TreeTraversal_h