OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 5 #ifndef UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
6 #define UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 6 #define UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
7 | 7 |
8 #include <stack> | 8 #include <stack> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 | 13 |
13 namespace ui { | 14 namespace ui { |
14 | 15 |
15 // Iterator that iterates over the descendants of a node. The iteration does | 16 // Iterator that iterates over the descendants of a node. The iteration does |
16 // not include the node itself, only the descendants. The following illustrates | 17 // not include the node itself, only the descendants. The following illustrates |
17 // typical usage: | 18 // typical usage: |
18 // while (iterator.has_next()) { | 19 // while (iterator.has_next()) { |
19 // Node* node = iterator.Next(); | 20 // Node* node = iterator.Next(); |
20 // // do something with node. | 21 // // do something with node. |
21 // } | 22 // } |
22 template <class NodeType> | 23 template <class NodeType> |
23 class TreeNodeIterator { | 24 class TreeNodeIterator { |
24 public: | 25 public: |
| 26 typedef base::Callback<bool(NodeType*)> PruneCallback; |
| 27 |
25 // This contructor accepts an optional filter function |prune| which could be | 28 // This contructor accepts an optional filter function |prune| which could be |
26 // used to prune complete branches of the tree. The filter function will be | 29 // used to prune complete branches of the tree. The filter function will be |
27 // evaluated on each tree node and if it evaluates to true the node and all | 30 // evaluated on each tree node and if it evaluates to true the node and all |
28 // its descendants will be skipped by the iterator. | 31 // its descendants will be skipped by the iterator. |
29 TreeNodeIterator(NodeType* node, bool (*prune)(NodeType*)) | 32 TreeNodeIterator(NodeType* node, const PruneCallback& prune) |
30 : prune_(prune) { | 33 : prune_(prune) { |
31 int index = 0; | 34 int index = 0; |
32 | 35 |
33 // Move forward through the children list until the first non prunable node. | 36 // Move forward through the children list until the first non prunable node. |
34 // This is to satisfy the iterator invariant that the current index in the | 37 // This is to satisfy the iterator invariant that the current index in the |
35 // Position at the top of the _positions list must point to a node the | 38 // Position at the top of the _positions list must point to a node the |
36 // iterator will be returning. | 39 // iterator will be returning. |
37 for (; index < node->child_count(); ++index) | 40 for (; index < node->child_count(); ++index) |
38 if (!prune || !prune(node->GetChild(index))) | 41 if (prune.is_null() || !prune.Run(node->GetChild(index))) |
39 break; | 42 break; |
40 | 43 |
41 if (index < node->child_count()) | 44 if (index < node->child_count()) |
42 positions_.push(Position<NodeType>(node, index)); | 45 positions_.push(Position<NodeType>(node, index)); |
43 } | 46 } |
44 | 47 |
45 explicit TreeNodeIterator(NodeType* node) : prune_(NULL) { | 48 explicit TreeNodeIterator(NodeType* node) { |
46 if (!node->empty()) | 49 if (!node->empty()) |
47 positions_.push(Position<NodeType>(node, 0)); | 50 positions_.push(Position<NodeType>(node, 0)); |
48 } | 51 } |
49 | 52 |
50 // Returns true if there are more descendants. | 53 // Returns true if there are more descendants. |
51 bool has_next() const { return !positions_.empty(); } | 54 bool has_next() const { return !positions_.empty(); } |
52 | 55 |
53 // Returns the next descendant. | 56 // Returns the next descendant. |
54 NodeType* Next() { | 57 NodeType* Next() { |
55 if (!has_next()) { | 58 if (!has_next()) { |
(...skipping 10 matching lines...) Expand all Loading... |
66 // Iterate over result's children. | 69 // Iterate over result's children. |
67 positions_.push(Position<NodeType>(result, 0)); | 70 positions_.push(Position<NodeType>(result, 0)); |
68 | 71 |
69 // Advance to next valid node by skipping over the pruned nodes and the | 72 // Advance to next valid node by skipping over the pruned nodes and the |
70 // empty Positions. At the end of this loop two cases are possible: | 73 // empty Positions. At the end of this loop two cases are possible: |
71 // - the current index of the top() Position points to a valid node | 74 // - the current index of the top() Position points to a valid node |
72 // - the _position list is empty, the iterator has_next() will return false. | 75 // - the _position list is empty, the iterator has_next() will return false. |
73 while (!positions_.empty()) { | 76 while (!positions_.empty()) { |
74 if (positions_.top().index >= positions_.top().node->child_count()) | 77 if (positions_.top().index >= positions_.top().node->child_count()) |
75 positions_.pop(); // This Position is all processed, move to the next. | 78 positions_.pop(); // This Position is all processed, move to the next. |
76 else if (prune_ && | 79 else if (!prune_.is_null() && |
77 prune_(positions_.top().node->GetChild(positions_.top().index))) | 80 prune_.Run(positions_.top().node->GetChild(positions_.top().index))) |
78 positions_.top().index++; // Prune the branch. | 81 positions_.top().index++; // Prune the branch. |
79 else | 82 else |
80 break; // Now positioned at the next node to be returned. | 83 break; // Now positioned at the next node to be returned. |
81 } | 84 } |
82 | 85 |
83 return result; | 86 return result; |
84 } | 87 } |
85 | 88 |
86 private: | 89 private: |
87 template <class PositionNodeType> | 90 template <class PositionNodeType> |
88 struct Position { | 91 struct Position { |
89 Position(PositionNodeType* node, int index) : node(node), index(index) {} | 92 Position(PositionNodeType* node, int index) : node(node), index(index) {} |
90 Position() : node(NULL), index(-1) {} | 93 Position() : node(NULL), index(-1) {} |
91 | 94 |
92 PositionNodeType* node; | 95 PositionNodeType* node; |
93 int index; | 96 int index; |
94 }; | 97 }; |
95 | 98 |
96 std::stack<Position<NodeType> > positions_; | 99 std::stack<Position<NodeType> > positions_; |
97 bool (*prune_)(NodeType*); | 100 PruneCallback prune_; |
98 | 101 |
99 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); | 102 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); |
100 }; | 103 }; |
101 | 104 |
102 } // namespace ui | 105 } // namespace ui |
103 | 106 |
104 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 107 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
OLD | NEW |