Experimental Code
Code Written To Experiment With Various Techniques And Otherwise Not Very Useful...
Iterator.h
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_H
15 #define ITERATOR_H
16 
17 /** \file
18  * Iterator definition.
19  */
20 
21 namespace Experiment {
22 
23  /** Iterator class that works genericly on a List containing Node elements
24  * with data of type T.
25  *
26  * These are created by the lists themselves.
27  *
28  * \tparam T the type of data held by the Nodes
29  * \tparam List the type of list this iterates
30  * \tparam Node the type of Node in the list
31  */
32  template <class T, class List, class Node>
33  class Iterator {
34  public:
35  ~Iterator();
36 
37  Iterator(List* theList, Node* at);
38  Iterator(const Iterator& rhs);
39  Iterator& operator=(const Iterator& rhs);
40 
41  public:
42 
43  bool operator==(const Iterator& rhs) const;
44  bool operator!=(const Iterator& rhs) const;
45 
47  Iterator operator++(int);
48 
49  Iterator& operator+=(int positions);
50  Iterator operator+(int positions);
51 
53  Iterator operator--(int);
54 
55  Iterator& operator-=(int positions);
56  Iterator operator-(int positions);
57 
58  void swapWith(Iterator& other);
59  void moveBefore(Iterator& other);
60 
61  T& operator*();
62  T* operator->();
63 
64  void swapOccurred(Node* a, Node* b) ;
65  void insertedBefore(Node* after, int count);
66  void removedBefore(Node* after, int count);
67 
68  private:
69  /** The list we are iterating to trigger iterator notifications on */
70  List* list;
71 
72  /** The current Node we are pointing at */
73  Node *current;
74  };
75 
76  /** Swap the values at a and b.
77  *
78  * \param a the Iterator to swap values with b
79  * \param b the Iterator to swap values with a
80  *
81  * \tparam T the type of data held by the Nodes
82  * \tparam List the type of list this iterates
83  * \tparam Node the type of Node in the list
84  */
85  template <class T, class List, class Node>
87  a.swapWith(b);
88  }
89 
90  /** Move the value at a before b.
91  *
92  * \param a the Iterator to move the value of before b
93  * \param b the Iterator to move the value of a before
94  *
95  * \tparam T the type of data held by the Nodes
96  * \tparam List the type of list this iterates
97  * \tparam Node the type of Node in the list
98  */
99  template <class T, class List, class Node>
101  a.moveBefore(b);
102  }
103 
104 } // namespace Experiment
105 
106 #include "Iterator.cpp"
107 
108 #endif // ITERATOR_H
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.
void moveBefore(Iterator< T, List, Node > &a, Iterator< T, List, Node > &b)
Move the value at a before b.
Definition: Iterator.h:100
void swap(Iterator< T, List, Node > &a, Iterator< T, List, Node > &b)
Swap the values at a and b.
Definition: Iterator.h:86