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 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.
| |
29 // all its descendants will be skipped by the iterator. | |
30 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.
| |
31 : prune_(prune) { | |
32 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.
| |
33 | |
34 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.
| |
35 if (!prune || !prune(node->GetChild(index))) | |
36 break; | |
37 | |
38 if (index < node->child_count()) | |
39 positions_.push(Position<NodeType>(node, index)); | |
40 } | |
41 | |
42 explicit TreeNodeIterator(NodeType* node) : prune_(NULL) { | |
27 if (!node->empty()) | 43 if (!node->empty()) |
28 positions_.push(Position<NodeType>(node, 0)); | 44 positions_.push(Position<NodeType>(node, 0)); |
29 } | 45 } |
30 | 46 |
31 // Returns true if there are more descendants. | 47 // Returns true if there are more descendants. |
32 bool has_next() const { return !positions_.empty(); } | 48 bool has_next() const { return !positions_.empty(); } |
33 | 49 |
34 // Returns the next descendant. | 50 // Returns the next descendant. |
35 NodeType* Next() { | 51 NodeType* Next() { |
36 if (!has_next()) { | 52 if (!has_next()) { |
37 NOTREACHED(); | 53 NOTREACHED(); |
38 return NULL; | 54 return NULL; |
39 } | 55 } |
40 | 56 |
41 NodeType* result = positions_.top().node->GetChild(positions_.top().index); | 57 NodeType* result = positions_.top().node->GetChild(positions_.top().index); |
42 | 58 |
43 // Make sure we don't attempt to visit result again. | 59 // Make sure we don't attempt to visit result again. |
44 positions_.top().index++; | 60 positions_.top().index++; |
45 | 61 |
46 // Iterate over result's children. | 62 // Iterate over result's children. |
47 positions_.push(Position<NodeType>(result, 0)); | 63 positions_.push(Position<NodeType>(result, 0)); |
48 | 64 |
49 // Advance to next position. | 65 // Advance to next position. |
50 while (!positions_.empty() && positions_.top().index >= | 66 while (!positions_.empty()) { |
51 positions_.top().node->child_count()) { | 67 if (positions_.top().index >= positions_.top().node->child_count()) |
52 positions_.pop(); | 68 positions_.pop(); // This position is all processed. |
69 else if (prune_ && | |
70 prune_(positions_.top().node->GetChild(positions_.top().index))) | |
71 positions_.top().index++; // Prune the branch. | |
72 else | |
73 break; // We are positioned at the next item to be processed; | |
53 } | 74 } |
54 | 75 |
55 return result; | 76 return result; |
56 } | 77 } |
57 | 78 |
58 private: | 79 private: |
59 template <class PositionNodeType> | 80 template <class PositionNodeType> |
60 struct Position { | 81 struct Position { |
61 Position(PositionNodeType* node, int index) : node(node), index(index) {} | 82 Position(PositionNodeType* node, int index) : node(node), index(index) {} |
62 Position() : node(NULL), index(-1) {} | 83 Position() : node(NULL), index(-1) {} |
63 | 84 |
64 PositionNodeType* node; | 85 PositionNodeType* node; |
65 int index; | 86 int index; |
66 }; | 87 }; |
67 | 88 |
68 std::stack<Position<NodeType> > positions_; | 89 std::stack<Position<NodeType> > positions_; |
90 bool (*prune_)(NodeType*); | |
69 | 91 |
70 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); | 92 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); |
71 }; | 93 }; |
72 | 94 |
73 } // namespace ui | 95 } // namespace ui |
74 | 96 |
75 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 97 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
OLD | NEW |