source: trunk/source/visualization/XXX/include/tree/iterator.h @ 834

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

import all except CVS

File size: 7.6 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/** An iterator that can be used to modify the tree. Most of the methods
32 * provided by this class perform actions relative to the current position
33 * of the iterator.
34 */
35class iterator : public tree_iterator<N, N&, N*>
36{
37  friend class tree;
38
39  private:
40
41    /* this typedef is very convenient and allows Java-like code */
42    typedef tree_iterator<N, N&, N*> super;
43
44  protected:
45
46    iterator(tree_node<N>* current) : super(current) { }
47
48  public:
49
50    /** Creates an iterator that doesn't point to anything yet. */
51    iterator(void) { }
52
53    /** Copy constructor */
54    iterator(const iterator& iter) : super(iter) { }
55
56    /** Converts an iterator to a const_iterator.  This conversion
57     * is one-way; const_iterator does not have a corresponding
58     * conversion function to iterator.
59     */
60    operator const_iterator() const
61    { return const_iterator(this->current); }
62
63    typename super::reference
64    operator * (void) const { return this->current->data; }
65
66    typename super::pointer
67    operator -> (void) const { return &(this->current->data); }
68
69    /** Identical to the splice_after method except deletes the tree
70     * after performing the splice.
71     *
72     * @return An iterator pointing to the root of the absorbed tree.
73     */
74    iterator absorb_after(tree<N>* t)
75    {
76      assert(t != NULL);
77      assert(!t->empty());
78
79      iterator i = splice_after(*t);
80      delete t;
81      return i;
82    }
83
84    /** Identical to the splice_back method except deletes the tree
85     * after performing the splice.
86     *
87     * @return An iterator pointing to the root of the absorbed tree.
88     */
89    iterator absorb_back(tree<N>* t)
90    {
91#if !defined(TREE_NO_ERROR_CHECKING) && defined(TREE_EXCEPTIONS)
92      if (t == NULL || t->empty())
93      {
94        throw typename tree<N>::null_tree_exception("in absorb_back");
95      }
96#else
97      assert(t != NULL);
98      assert(!t->empty());
99#endif
100 
101      iterator i = splice_back(*t);
102      delete t;
103      return i;
104    }
105
106    /** Identical to the splice_before method except deletes the tree
107     * after performing the splice.
108     *
109     * @return An iterator pointing to the root of the absorbed tree.
110     */
111    iterator absorb_before(tree<N>* t)
112    {
113      assert(t != NULL);
114      assert(!t->empty());
115
116      iterator i = splice_before(*t);
117      delete t;
118      return i;
119    }
120
121    /** Identical to the splice_front method except deletes the tree
122     * after performing the splice.
123     *
124     * @return An iterator pointing to the root of the absorbed tree.
125     */
126    iterator absorb_front(tree<N>* t)
127    {
128      assert(t != NULL);
129      assert(!t->empty());
130
131      iterator i = splice_front(*t);
132      delete t;
133      return i;
134    }
135
136    sibling_iterator beginChildren(void) const
137    { return sibling_iterator(iterator(this->current->first_child)); }
138
139    /** Removes all children and grandchildren of this node
140     */
141    void clear(void);
142
143    sibling_iterator endChildren(void) const
144    { return sibling_iterator(iterator(NULL)); } 
145
146    /** Removes this node and returns an iterator pointing
147     * to the next node. */
148    iterator erase(void);
149
150    /** Inserts a new node before this node and returns an
151     * iterator pointing to the new node. */
152    iterator insert(const N& data);
153
154    iterator insert(const tree<N>& t);
155
156    /** Inserts a new node after this node and returns an
157     * iterator pointing to the new node. */
158    iterator insert_after(const N& data);
159
160    iterator insert_after(const tree<N>& t); 
161
162    iterator parent(void) const
163    { return this->current->parent; }
164
165    /** Removes the last child of this node.
166     */
167    void pop_back(void);
168
169    /** Removes the first child of this node.
170     */
171    void pop_front(void);
172
173    /** Adds a node to the end of this position's child list.
174     *
175     * @param data Data for the new node.
176     *
177     * @return An iterator pointing to the new node.
178     */
179    iterator push_back (const N& data);
180
181    /** Adds a copy of a tree to the end of this position's child list.
182     *
183     * @param t The tree to copy.
184     *
185     * @return An iterator pointing to the root of the inserted subtree.
186     */
187    iterator push_back (const tree<N>& t);
188
189    /** Adds a node to the beginning of this position's child list.
190     *
191     * @param data Data for the new node.
192     *
193     * @return An iterator pointing to the new node.
194     */
195    iterator push_front(const N& data);
196
197    /** Adds a copy of a tree to the beginning of this position's child list.
198     *
199     * @param t The tree to copy.
200     *
201     * @return An iterator pointing to the root of the inserted subtree.
202     */
203    iterator push_front(const tree<N>& t);
204
205    /** Replaces the data at this position.
206     *
207     * @param data The new data.
208     *
209     * @return The old data.
210     */
211    N replace(const N& data);
212
213    /** Determines the size of the subtree below and including this position.
214     *
215     * @return The size of this subtree.
216     */
217    size_t size(void) const;
218
219    /** Moves nodes from a tree (without copying) such that
220     * the tree becomes the next sibling of this node.
221     *
222     * @param t The tree to move. It will be empty after this method returns.
223     *
224     * @return An iterator pointing to the root of the spliced tree.
225     */
226    iterator splice_after(tree<N>& t);
227
228    /** Moves nodes from a tree (without copying) such that the tree becomes the last child of this node.
229     *
230     * @param t The tree to move. It will be empty after this method returns.
231     *
232     * @return An iterator pointing to the root of the spliced tree.
233     */ 
234    iterator splice_back(tree<N>& t);
235
236    /** Moves nodes from a tree (without copying) such that the tree becomes the previous child of this node.
237     *
238     * @param t The tree to move. It will be empty after this method returns.
239     *
240     * @return An iterator pointing to the root of the spliced tree.
241     */
242    iterator splice_before(tree<N>& t);
243
244    /** Moves nodes from a tree (without copying) such that the tree becomes the first child of this node.
245     *
246     * @param t The tree to move. It will be empty after this method returns.
247     *
248     * @return An iterator pointing to the root of the spliced tree.
249     */
250    iterator splice_front(tree<N>& t);
251};
Note: See TracBrowser for help on using the repository browser.