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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <stack> | 9 #include <stack> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 | 13 |
14 namespace ui { | 14 namespace ui { |
15 | 15 |
16 // 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 |
17 // not include the node itself, only the descendants. The following illustrates | 17 // not include the node itself, only the descendants. The following illustrates |
18 // typical usage: | 18 // typical usage: |
19 // while (iterator.has_next()) { | 19 // while (iterator.has_next()) { |
20 // Node* node = iterator.Next(); | 20 // Node* node = iterator.Next(); |
21 // // do something with node. | 21 // // do something with node. |
22 // } | 22 // } |
23 template <class NodeType> | 23 template <class NodeType> |
24 class TreeNodeIterator { | 24 class TreeNodeIterator { |
25 public: | 25 public: |
26 explicit TreeNodeIterator(NodeType* node) { | 26 // This contructor accepts an optional filter function |prune| which could be |
| 27 // used to prune complete branches of the tree. The filter function will be |
| 28 // evaluated on each tree node and if it evaluates to true the node and all |
| 29 // its descendants will be skipped by the iterator. |
| 30 TreeNodeIterator(NodeType* node, bool (*prune)(NodeType*)) |
| 31 : prune_(prune) { |
| 32 int index = 0; |
| 33 |
| 34 // Move forward through the children list until the first non prunable node. |
| 35 // This is to satisfy the iterator invariant that the current index in the |
| 36 // Position at the top of the _positions list must point to a node the |
| 37 // iterator will be returning. |
| 38 for (; index < node->child_count(); ++index) |
| 39 if (!prune || !prune(node->GetChild(index))) |
| 40 break; |
| 41 |
| 42 if (index < node->child_count()) |
| 43 positions_.push(Position<NodeType>(node, index)); |
| 44 } |
| 45 |
| 46 explicit TreeNodeIterator(NodeType* node) : prune_(NULL) { |
27 if (!node->empty()) | 47 if (!node->empty()) |
28 positions_.push(Position<NodeType>(node, 0)); | 48 positions_.push(Position<NodeType>(node, 0)); |
29 } | 49 } |
30 | 50 |
31 // Returns true if there are more descendants. | 51 // Returns true if there are more descendants. |
32 bool has_next() const { return !positions_.empty(); } | 52 bool has_next() const { return !positions_.empty(); } |
33 | 53 |
34 // Returns the next descendant. | 54 // Returns the next descendant. |
35 NodeType* Next() { | 55 NodeType* Next() { |
36 if (!has_next()) { | 56 if (!has_next()) { |
37 NOTREACHED(); | 57 NOTREACHED(); |
38 return NULL; | 58 return NULL; |
39 } | 59 } |
40 | 60 |
| 61 // There must always be a valid node in the current Position index. |
41 NodeType* result = positions_.top().node->GetChild(positions_.top().index); | 62 NodeType* result = positions_.top().node->GetChild(positions_.top().index); |
42 | 63 |
43 // Make sure we don't attempt to visit result again. | 64 // Make sure we don't attempt to visit result again. |
44 positions_.top().index++; | 65 positions_.top().index++; |
45 | 66 |
46 // Iterate over result's children. | 67 // Iterate over result's children. |
47 positions_.push(Position<NodeType>(result, 0)); | 68 positions_.push(Position<NodeType>(result, 0)); |
48 | 69 |
49 // Advance to next position. | 70 // Advance to next valid node by skipping over the pruned nodes and the |
50 while (!positions_.empty() && positions_.top().index >= | 71 // empty Positions. At the end of this loop two cases are possible: |
51 positions_.top().node->child_count()) { | 72 // - the current index of the top() Position points to a valid node |
52 positions_.pop(); | 73 // - the _position list is empty, the iterator has_next() will return false. |
| 74 while (!positions_.empty()) { |
| 75 if (positions_.top().index >= positions_.top().node->child_count()) |
| 76 positions_.pop(); // This Position is all processed, move to the next. |
| 77 else if (prune_ && |
| 78 prune_(positions_.top().node->GetChild(positions_.top().index))) |
| 79 positions_.top().index++; // Prune the branch. |
| 80 else |
| 81 break; // Now positioned at the next node to be returned. |
53 } | 82 } |
54 | 83 |
55 return result; | 84 return result; |
56 } | 85 } |
57 | 86 |
58 private: | 87 private: |
59 template <class PositionNodeType> | 88 template <class PositionNodeType> |
60 struct Position { | 89 struct Position { |
61 Position(PositionNodeType* node, int index) : node(node), index(index) {} | 90 Position(PositionNodeType* node, int index) : node(node), index(index) {} |
62 Position() : node(NULL), index(-1) {} | 91 Position() : node(NULL), index(-1) {} |
63 | 92 |
64 PositionNodeType* node; | 93 PositionNodeType* node; |
65 int index; | 94 int index; |
66 }; | 95 }; |
67 | 96 |
68 std::stack<Position<NodeType> > positions_; | 97 std::stack<Position<NodeType> > positions_; |
| 98 bool (*prune_)(NodeType*); |
69 | 99 |
70 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); | 100 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); |
71 }; | 101 }; |
72 | 102 |
73 } // namespace ui | 103 } // namespace ui |
74 | 104 |
75 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 105 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
OLD | NEW |