Chromium Code Reviews| Index: ui/base/models/tree_node_iterator.h |
| diff --git a/ui/base/models/tree_node_iterator.h b/ui/base/models/tree_node_iterator.h |
| index 03e68ab9b8f7f09853aa4c4a9f8ca9efe4ce9ef0..b4d70ed93b862bb9be17277558c09ba44d4a60bc 100644 |
| --- a/ui/base/models/tree_node_iterator.h |
| +++ b/ui/base/models/tree_node_iterator.h |
| @@ -23,7 +23,23 @@ namespace ui { |
| template <class NodeType> |
| class TreeNodeIterator { |
| public: |
| - explicit TreeNodeIterator(NodeType* node) { |
| + // This contructor accepts an optional filter function |prune| which could be |
| + // used to prune complete branches of the tree. The filter function will be |
| + // evaluated on each of the tree node and if it evaluate to true this node and |
|
sky
2011/10/14 19:50:23
'each of the tree node' -> 'each tree node' or 'ea
noyau (Ping after 24h)
2011/10/17 12:42:48
Done.
|
| + // all its descendants will be skipped by the iterator. |
| + explicit TreeNodeIterator(NodeType* node, bool (*prune)(NodeType*)) |
|
sky
2011/10/14 19:50:23
remove explicit
noyau (Ping after 24h)
2011/10/17 12:42:48
Done.
|
| + : prune_(prune) { |
| + int index; |
|
sky
2011/10/14 19:50:23
set index to 0 rather than in the for loop.
noyau (Ping after 24h)
2011/10/17 12:42:48
Done.
|
| + |
| + for (index = 0; index < node->child_count(); ++index) |
|
sky
2011/10/14 19:50:23
Add a comment as to what this for and if are doing
noyau (Ping after 24h)
2011/10/17 12:42:48
Done.
|
| + if (!prune || !prune(node->GetChild(index))) |
| + break; |
| + |
| + if (index < node->child_count()) |
| + positions_.push(Position<NodeType>(node, index)); |
| + } |
| + |
| + explicit TreeNodeIterator(NodeType* node) : prune_(NULL) { |
| if (!node->empty()) |
| positions_.push(Position<NodeType>(node, 0)); |
| } |
| @@ -47,9 +63,14 @@ class TreeNodeIterator { |
| positions_.push(Position<NodeType>(result, 0)); |
| // Advance to next position. |
| - while (!positions_.empty() && positions_.top().index >= |
| - positions_.top().node->child_count()) { |
| - positions_.pop(); |
| + while (!positions_.empty()) { |
| + if (positions_.top().index >= positions_.top().node->child_count()) |
| + positions_.pop(); // This position is all processed. |
| + else if (prune_ && |
| + prune_(positions_.top().node->GetChild(positions_.top().index))) |
| + positions_.top().index++; // Prune the branch. |
| + else |
| + break; // We are positioned at the next item to be processed; |
| } |
| return result; |
| @@ -66,6 +87,7 @@ class TreeNodeIterator { |
| }; |
| std::stack<Position<NodeType> > positions_; |
| + bool (*prune_)(NodeType*); |
| DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); |
| }; |