Experimental Code
Code Written To Experiment With Various Techniques And Otherwise Not Very Useful...
Iterator.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Komodo Does Inc
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 - Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 
14 #ifndef ITERATOR_CPP
15 #define ITERATOR_CPP
16 
17 /**
18  * This file is to be included at the end of Iterator.h
19  */
20 
21 namespace Experiment {
22 
23  /** Destroy iterator */
24  template<typename T, typename List, typename Node>
26  list->removeIterator(this);
27  }
28 
29  /** Create Iterator for theList at the value of at
30  *
31  * \param theList to iterate
32  * \param at start iterating from at
33  */
34  // \todo Concel from public access
35  template<typename T, typename List, typename Node>
36  Iterator<T, List, Node>::Iterator(List* theList, Node* at)
37  : list(theList), current(at)
38  {
39  list->addIterator(this);
40  }
41 
42  /** Create Iterator at the same position of the same list as rhs
43  *
44  * \param rhs to create at the same position of the same list as
45  */
46  template<typename T, typename List, typename Node>
48  : list(rhs.list), current(rhs.current)
49  {
50  list->addIterator(this);
51  }
52 
53  /** Update Iterator to be at the same position of the same list as rhs
54  *
55  * \param rhs to update to the the same position of the same list as
56  */
57  template<typename T, typename List, typename Node>
59  {
60  list->removeIterator(this);
61 
62  list = rhs.list;
63  current = rhs.current;
64 
65  list->addIterator(this);
66 
67  return *this;
68  }
69 
70  /** \return true if this iterator is at the same position in the same list as rhs,
71  * otherwise false
72  *
73  * \param rhs to compare position against
74  */
75  template<typename T, typename List, typename Node>
77  return rhs.current == current;
78  }
79 
80  /** \return true if this iterator is not at the same position in the same list as rhs,
81  * otherwise false
82  *
83  * \param rhs to compare position against
84  */
85  template<typename T, typename List, typename Node>
87  return !(*this == rhs);
88  }
89 
90  /** Advance this iterator one position and return this iterator.
91  *
92  * \return this iterator after advancement
93  */
94  template<typename T, typename List, typename Node>
96  current = current->getNext();
97  return *this;
98  }
99 
100  /** Advance this iterator one position and return a temporary iterator at the
101  * original position.
102  *
103  * \return the temporary iterator at the original position
104  */
105  template<typename T, typename List, typename Node>
107  Iterator copy(list, current);
108  current = current->getNext();
109  return copy;
110  }
111 
112  /** Advance this iterator positions, if possible, and return this iterator
113  *
114  * \param positions to advance
115  *
116  * \return this iterator
117  */
118  template<typename T, typename List, typename Node>
120  if(positions < 0) {
121  positions = -positions;
122  for(int i = 0; i < positions; ++i) {
123  current = current->getPrevious();
124  }
125  } else {
126  for(int i = 0; i < positions; ++i) {
127  current = current->getNext();
128  }
129  }
130  return *this;
131  }
132 
133  /** \return an Iterator advanced positions from this iterator
134  *
135  * \param positions to advance the returned iterator
136  */
137  template<typename T, typename List, typename Node>
139  Iterator<T, List, Node> iter(list, current);
140  iter += positions;
141  return iter;
142  }
143 
144  /** Decrement this iterator one position and return this iterator.
145  *
146  * \return this iterator after decrement
147  */
148  template<typename T, typename List, typename Node>
150  current = current->getPrevious();
151  return *this;
152  }
153 
154  /** Decrement this iterator one position and return a temporary iterator at the
155  * original position.
156  *
157  * \return the temporary iterator at the original position
158  */
159  template<typename T, typename List, typename Node>
161  Iterator copy(list, current);
162  current = current->getPrevious();
163  return copy;
164  }
165 
166  /** Decrement this iterator positions, if possible, and return this iterator
167  *
168  * \param positions to advance
169  *
170  * \return this iterator
171  */
172  template<typename T, typename List, typename Node>
174  (*this) += -positions;
175  return *this;
176  }
177 
178  /** \return an Iterator decremented positions from this iterator
179  *
180  * \param positions to decrement the returned iterator
181  */
182  template<typename T, typename List, typename Node>
184  Iterator iter(list, current);
185  iter -= positions;
186  return iter;
187  }
188 
189  /** Swap the values at this and other iterators
190  *
191  * \param other iterator to swap values with
192  */
193  template<typename T, typename List, typename Node>
195  if(!current->valid() || !other.current->valid()) {
196  return;
197  }
198 
199  current->swapWith(other.current);
200  list->notifyItersSwapOccurred(current, other.current);
201  }
202 
203  /** Move the value at this itherator before the value at other
204  *
205  * \param other iterator to move the value of this iterator before
206  */
207  template<typename T, typename List, typename Node>
209  if(!current->valid()) {
210  return;
211  }
212 
213  Node* next = current->getNext();
214  current->moveBefore(other.current);
215 
216  other.list->notifyItersInsertedBefore(1, current->getNext());
217  list->notifyItersRemovedBefore(1, next);
218 
219  *this = list->end();
220  }
221 
222  /** @return the value at this iterator, if any
223  *
224  * \throw std::out_of_range if this is not at a valid position
225  */
226  template<typename T, typename List, typename Node>
228  if(NULL == current) {
229  throw std::out_of_range("this position has no value");
230  }
231 
232  return **current;
233  }
234 
235  /** @return a pointer to the value at this iterator, if any
236  *
237  * \throw std::out_of_range if this is not at a valid position
238  */
239  template<typename T, typename List, typename Node>
241  return &operator*();
242  }
243 
244  /** Update this iterator, if appropriate, that the values at a and b
245  * swapped positions.
246  *
247  * \param a Node that swapped with b
248  * \param b Node that swapped with a
249  *
250  * \todo Conceal from public access.
251  */
252  template<typename T, typename List, typename Node>
253  void Iterator<T, List, Node>::swapOccurred(Node* a, Node* b) {
254  if(a == current) {
255  current = b;
256  } else if(b == current) {
257  current = a;
258  }
259  }
260 
261  /** Update this iterator, if appropriate, that count positions were added before
262  * after
263  *
264  * \param count of items inserted before after
265  * \param after node that has had count items added before it
266  *
267  * \todo Conceal from public access.
268  */
269  template<typename T, typename List, typename Node>
270  void Iterator<T, List, Node>::insertedBefore(Node* after, int count) {
271  if(after == current) {
272  (*this) -= count;
273  }
274  }
275 
276  /** Update this iterator, if appropriate, that count positions were removed before
277  * after
278  *
279  * \param count of items removed before after
280  * \param after node that has had count items removed before it
281  *
282  * \todo Conceal from public access.
283  */
284  template<typename T, typename List, typename Node>
285  void Iterator<T, List, Node>::removedBefore(Node* after, int count) {
286  if(after == current) {
287  (*this) += count;
288  }
289  }
290 
291 } // namespace Experiment
292 
293 #endif // ITERATOR_CPP
Iterator class that works genericly on a List containing Node elements with data of type T.
Definition: Iterator.h:33
~Iterator()
Destroy iterator.
Definition: Iterator.cpp:25
void removedBefore(Node *after, int count)
Update this iterator, if appropriate, that count positions were removed before after.
Definition: Iterator.cpp:285
bool operator!=(const Iterator &rhs) const
Definition: Iterator.cpp:86
Iterator & operator=(const Iterator &rhs)
Update Iterator to be at the same position of the same list as rhs.
Definition: Iterator.cpp:58
bool operator==(const Iterator &rhs) const
Definition: Iterator.cpp:76
Iterator & operator-=(int positions)
Decrement this iterator positions, if possible, and return this iterator.
Definition: Iterator.cpp:173
Iterator & operator++()
Advance this iterator one position and return this iterator.
Definition: Iterator.cpp:95
Iterator operator+(int positions)
Definition: Iterator.cpp:138
void insertedBefore(Node *after, int count)
Update this iterator, if appropriate, that count positions were added before after.
Definition: Iterator.cpp:270
Iterator & operator+=(int positions)
Advance this iterator positions, if possible, and return this iterator.
Definition: Iterator.cpp:119
void moveBefore(Iterator &other)
Move the value at this itherator before the value at other.
Definition: Iterator.cpp:208
void swapWith(Iterator &other)
Swap the values at this and other iterators.
Definition: Iterator.cpp:194
Iterator(List *theList, Node *at)
Create Iterator for theList at the value of at.
Definition: Iterator.cpp:36
Iterator operator-(int positions)
Definition: Iterator.cpp:183
void swapOccurred(Node *a, Node *b)
Update this iterator, if appropriate, that the values at a and b swapped positions.
Definition: Iterator.cpp:253
Iterator & operator--()
Decrement this iterator one position and return this iterator.
Definition: Iterator.cpp:149
This file is to be included at the end of Iterator.h.