| 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 // This contructor accepts an optional filter function |prune| which could be | 26 explicit TreeNodeIterator(NodeType* node) { |
| 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) { | |
| 47 if (!node->empty()) | 27 if (!node->empty()) |
| 48 positions_.push(Position<NodeType>(node, 0)); | 28 positions_.push(Position<NodeType>(node, 0)); |
| 49 } | 29 } |
| 50 | 30 |
| 51 // Returns true if there are more descendants. | 31 // Returns true if there are more descendants. |
| 52 bool has_next() const { return !positions_.empty(); } | 32 bool has_next() const { return !positions_.empty(); } |
| 53 | 33 |
| 54 // Returns the next descendant. | 34 // Returns the next descendant. |
| 55 NodeType* Next() { | 35 NodeType* Next() { |
| 56 if (!has_next()) { | 36 if (!has_next()) { |
| 57 NOTREACHED(); | 37 NOTREACHED(); |
| 58 return NULL; | 38 return NULL; |
| 59 } | 39 } |
| 60 | 40 |
| 61 // There must always be a valid node in the current Position index. | 41 // There must always be a valid node in the current Position index. |
| 62 NodeType* result = positions_.top().node->GetChild(positions_.top().index); | 42 NodeType* result = positions_.top().node->GetChild(positions_.top().index); |
| 63 | 43 |
| 64 // Make sure we don't attempt to visit result again. | 44 // Make sure we don't attempt to visit result again. |
| 65 positions_.top().index++; | 45 positions_.top().index++; |
| 66 | 46 |
| 67 // Iterate over result's children. | 47 // Iterate over result's children. |
| 68 positions_.push(Position<NodeType>(result, 0)); | 48 positions_.push(Position<NodeType>(result, 0)); |
| 69 | 49 |
| 70 // Advance to next valid node by skipping over the pruned nodes and the | 50 while (!positions_.empty() && |
| 71 // empty Positions. At the end of this loop two cases are possible: | 51 positions_.top().index >= positions_.top().node->child_count()) { |
| 72 // - the current index of the top() Position points to a valid node | 52 positions_.pop(); // This Position is all processed, move to the next. |
| 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. | |
| 82 } | 53 } |
| 83 | 54 |
| 84 return result; | 55 return result; |
| 85 } | 56 } |
| 86 | 57 |
| 87 private: | 58 private: |
| 88 template <class PositionNodeType> | 59 template <class PositionNodeType> |
| 89 struct Position { | 60 struct Position { |
| 90 Position(PositionNodeType* node, int index) : node(node), index(index) {} | 61 Position(PositionNodeType* node, int index) : node(node), index(index) {} |
| 91 Position() : node(NULL), index(-1) {} | 62 Position() : node(NULL), index(-1) {} |
| 92 | 63 |
| 93 PositionNodeType* node; | 64 PositionNodeType* node; |
| 94 int index; | 65 int index; |
| 95 }; | 66 }; |
| 96 | 67 |
| 97 std::stack<Position<NodeType> > positions_; | 68 std::stack<Position<NodeType> > positions_; |
| 98 bool (*prune_)(NodeType*); | |
| 99 | 69 |
| 100 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); | 70 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); |
| 101 }; | 71 }; |
| 102 | 72 |
| 103 } // namespace ui | 73 } // namespace ui |
| 104 | 74 |
| 105 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 75 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
| OLD | NEW |