| 1 | /*
|
|---|
| 2 | Copyright (c) 2003-2006, Troy Aaron Johnson
|
|---|
| 3 | All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | modification, are permitted provided that the following conditions
|
|---|
| 7 | are met:
|
|---|
| 8 |
|
|---|
| 9 | * Redistributions of source code must retain the above copyright
|
|---|
| 10 | notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * Neither the name of Troy Aaron Johnson nor the names of any
|
|---|
| 15 | contributors may be used to endorse or promote products derived from
|
|---|
| 16 | this software without specific prior written permission.
|
|---|
| 17 |
|
|---|
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|---|
| 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|---|
| 22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|---|
| 23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|---|
| 24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|---|
| 25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|---|
| 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|---|
| 27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|---|
| 28 | POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | class bfs_iterator : public virtual iterator
|
|---|
| 32 | {
|
|---|
| 33 | friend class tree;
|
|---|
| 34 |
|
|---|
| 35 | private:
|
|---|
| 36 |
|
|---|
| 37 | std::list<tree_node<N>*> queue;
|
|---|
| 38 |
|
|---|
| 39 | typedef bool (*PruningPredicate)(const iterator& iter);
|
|---|
| 40 | PruningPredicate prune_pred;
|
|---|
| 41 |
|
|---|
| 42 | explicit bfs_iterator(tree_node<N>* current) : iterator(current), prune_pred(NULL) { }
|
|---|
| 43 |
|
|---|
| 44 | public:
|
|---|
| 45 |
|
|---|
| 46 | bfs_iterator(void) : prune_pred(NULL) { }
|
|---|
| 47 | bfs_iterator(const iterator& iter) : iterator(iter), prune_pred(NULL) { }
|
|---|
| 48 |
|
|---|
| 49 | bfs_iterator endBfs(void) { return bfs_iterator(NULL); }
|
|---|
| 50 |
|
|---|
| 51 | bfs_iterator& operator ++ (void)
|
|---|
| 52 | {
|
|---|
| 53 | /* this line necessary for g++ 3.4 onward */
|
|---|
| 54 | tree_node<N>*& current = this->current;
|
|---|
| 55 |
|
|---|
| 56 | assert(current != NULL);
|
|---|
| 57 |
|
|---|
| 58 | if (prune_pred == NULL || !prune_pred(*this))
|
|---|
| 59 | {
|
|---|
| 60 | for (tree_node<N>* p = current->first_child;
|
|---|
| 61 | p != NULL; p = p->next_sibling)
|
|---|
| 62 | {
|
|---|
| 63 | queue.push_front(p);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | if (queue.empty())
|
|---|
| 68 | {
|
|---|
| 69 | current = NULL;
|
|---|
| 70 | }
|
|---|
| 71 | else
|
|---|
| 72 | {
|
|---|
| 73 | current = queue.back();
|
|---|
| 74 | queue.pop_back();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return *this;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** When the iterator points to a node, and the pruning predicate
|
|---|
| 81 | * returns true, the children of the node are not added to the BFS
|
|---|
| 82 | * queue. True means prune everything beneath the node passed
|
|---|
| 83 | * to the predicate. A NULL predicate disables pruning. */
|
|---|
| 84 | void pruneOn(PruningPredicate prune_pred)
|
|---|
| 85 | {
|
|---|
| 86 | this->prune_pred = prune_pred;
|
|---|
| 87 | }
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|