source: trunk/source/visualization/XXX/include/tree/tree_iterator.h @ 1202

Last change on this file since 1202 was 834, checked in by garnier, 16 years ago

import all except CVS

File size: 4.2 KB
Line 
1/*
2Copyright (c) 2003-2006, Troy Aaron Johnson
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are 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
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28POSSIBILITY OF SUCH DAMAGE.
29*/
30
31/** The base template for all iterators.
32 */
33template <class T, class R, class P>
34class tree_iterator
35{
36  friend class tree;
37
38  protected:
39
40    /* The node pointed to by this iterator. */
41    tree_node<N>* current; 
42
43    /* This is the depth of an iterator relative to its starting position.
44       The starting depth is 0, the depth of the parent is -1, the depth
45       of any child is 1, the depth of any grandchild is 2, etc. */
46    long iter_depth;
47
48    /** Allows derived classes to create an iterator
49     * from a pointer to a node.  This constructor
50     * is not explicit because the tree_iterator class
51     * contains the == and != operators, which are used
52     * throughout the template to compare iterators
53     * to NULL.  The tree_iterator class is not visible
54     * to the user, so this implicit constructor is
55     * entirely for my convenience when coding the template.
56     *
57     * @param current Position for the new iterator
58     */ 
59    tree_iterator(tree_node<N>* current) : current(current), iter_depth(0L) { }
60
61    /** Destroys the node pointed to by this iterator.
62     */
63    void destroy(void)
64    {
65      delete current;
66      current = NULL;
67      iter_depth = 0L;
68    }
69
70    /* increment and decrement methods used by both the iterator
71       and the const_iterator versions of various iterators */
72 
73    static inline void post_inc(tree_node<N>*& current, long& depth);
74    static inline void post_dec(tree_node<N>*& current, long& depth);
75
76    static inline void pre_inc(tree_node<N>*& current, long& depth);
77    static inline void pre_dec(tree_node<N>*& current, long& depth);
78 
79    static inline void sib_inc(tree_node<N>*& current);
80    static inline void sib_dec(tree_node<N>*& current);
81 
82  public:
83
84    /* The following typedefs are required for compatibility
85     * with standard containers. */
86   
87    typedef std::bidirectional_iterator_tag iterator_category;
88    typedef T                               value_type;
89    typedef R                               reference;
90    typedef P                               pointer;
91    typedef size_t                          size_type;
92    typedef ptrdiff_t                       difference_type;
93
94    tree_iterator(void) : current(NULL), iter_depth(0L) { }
95
96    tree_iterator(const tree_iterator& iter)
97      : current(iter.current), iter_depth(iter.iter_depth) { }
98
99    /* note that depth is not relevant when comparing iterators */
100
101    bool operator == (const tree_iterator& iter) const
102    { return this->current == iter.current; }
103
104    bool operator != (const tree_iterator& iter) const
105    { return this->current != iter.current; }
106
107    long depth(void) const { return iter_depth; }
108
109    bool leaf(void) const { return this->first_child == NULL; }
110};
Note: See TracBrowser for help on using the repository browser.