The Pedigree Project  0.1
Iterator.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_UTILITIES_ITERATOR_H
21 #define KERNEL_UTILITIES_ITERATOR_H
22 
23 #include "pedigree/kernel/utilities/cpp.h"
24 #include "pedigree/kernel/utilities/template.h"
25 
39 template <
40  typename originalT, class Struct,
41  Struct *(Struct::*FunctionPrev)() = &Struct::previous,
42  Struct *(Struct::*FunctionNext)() = &Struct::next, typename T = originalT>
43 class Iterator
44 {
47  template <
48  typename _originalT, class _Struct,
49  _Struct *(_Struct::*_FunctionPrev)(),
50  _Struct *(_Struct::*_FunctionNext)(), typename _T>
51  friend class Iterator;
52 
54  template <
55  typename _originalT, class _Struct,
56  _Struct *(_Struct::*_FunctionPrev)(),
57  _Struct *(_Struct::*_FunctionNext)(), typename _T1, typename _T2>
58  friend bool operator==(
60  &x1,
62  &x2);
63 
64  public:
73 
76  {
77  }
81  {
82  }
85  template <typename T2>
89  {
90  }
93  Iterator(Struct *Node) : m_Node(Node), m_Default()
94  {
95  }
98  {
99  }
100 
104  {
105  m_Node = x.m_Node;
106  m_Default = x.m_Default;
107  return *this;
108  }
111  {
112  if (m_Node)
113  m_Node = (m_Node->*FunctionNext)();
114  return *this;
115  }
118  {
119  if (m_Node)
120  {
121  Struct *newNode = (m_Node->*FunctionPrev)();
122  if (newNode)
123  m_Node = newNode;
124  }
125  return *this;
126  }
129  {
130  // Verify that we actually have a valid node
131  if (m_Node)
132  return m_Node->value;
133  else
134  {
135  return m_Default;
136  }
137  }
140  {
141  return *this;
142  }
143 
145  operator Const()
146  {
147  return Const(m_Node);
148  }
149 
151  Struct *__getNode()
152  {
153  return m_Node;
154  }
155 
156  protected:
158  Struct *m_Node;
161 };
162 
175 template <
176  typename originalT, class Struct,
177  Struct *(Struct::*FunctionPrev)() = &Struct::previous,
178  Struct *(Struct::*FunctionNext)() = &Struct::next, typename K = originalT,
179  typename T = originalT>
181 {
184  template <
185  typename _originalT, class _Struct,
186  _Struct *(_Struct::*_FunctionPrev)(),
187  _Struct *(_Struct::*_FunctionNext)(), typename _K, typename _T>
188  friend class TreeIterator;
189 
191  template <
192  typename _originalT, class _Struct,
193  _Struct *(_Struct::*_FunctionPrev)(),
194  _Struct *(_Struct::*_FunctionNext)(), typename _K1, typename _T1,
195  typename _K2, typename _T2>
196  friend bool operator==(
197  const TreeIterator<
198  _originalT, _Struct, _FunctionPrev, _FunctionNext, _K1, _T1> &x1,
199  const TreeIterator<
200  _originalT, _Struct, _FunctionPrev, _FunctionNext, _K2, _T2> &x2);
201 
202  public:
204  typedef TreeIterator<
205  originalT, Struct, FunctionPrev, FunctionNext, K, T const>
211  typedef TreeIterator<
212  originalT, Struct, FunctionNext, FunctionPrev, K, T const>
214 
217  {
218  }
222  {
223  }
226  template <typename K2, typename T2>
228  originalT, Struct, FunctionPrev, FunctionNext, K2, T2> &x)
229  : m_Node(x.m_Node)
230  {
231  }
234  TreeIterator(Struct *Node) : m_Node(Node)
235  {
236  }
239  {
240  }
241 
245  {
246  m_Node = x.m_Node;
247  return *this;
248  }
251  {
252  m_Node = (m_Node->*FunctionNext)();
253  return *this;
254  }
257  {
258  m_Node = (m_Node->*FunctionPrev)();
259  return *this;
260  }
263  {
264  return m_Node->value;
265  }
268  {
269  return m_Node->value;
270  }
271 
273  operator Const()
274  {
275  return Const(m_Node);
276  }
277 
279  Struct *__getNode()
280  {
281  return m_Node;
282  }
283 
284  K key()
285  {
286  if (m_Node)
287  {
288  if (m_Node->value)
289  return m_Node->value->key;
290  }
291  return 0;
292  }
293 
294  T value()
295  {
296  if (m_Node)
297  {
298  if (m_Node->value)
299  return m_Node->value->element;
300  }
301  return 0;
302  }
303 
304  const T &value(const T &failed=T()) const
305  {
306  if (m_Node)
307  {
308  if (m_Node->value)
309  return m_Node->value->element;
310  }
311  return failed;
312  }
313 
314  protected:
316  Struct *m_Node;
317 };
318 
323 template <
324  typename originalT, class Struct, Struct *(Struct::*FunctionPrev)(),
325  Struct *(Struct::*FunctionNext)(), typename T1, typename T2>
329 {
330  if (x1.m_Node != x2.m_Node)
331  return false;
332  return true;
333 }
334 
339 template <
340  typename originalT, class Struct, Struct *(Struct::*FunctionPrev)(),
341  Struct *(Struct::*FunctionNext)(), typename K1, typename T1, typename K2,
342  typename T2>
345  &x1,
347  &x2)
348 {
349  if (!x2.m_Node)
350  {
351  if (!x1.m_Node)
352  return false;
353  else if (x1.m_Node->value)
354  return false;
355  return true;
356  }
357  return (x1.m_Node->value == x2.m_Node->value);
358 }
359 
362 #endif
T operator*()
Definition: Iterator.h:262
TreeIterator(Struct *Node)
Definition: Iterator.h:234
TreeIterator< originalT, Struct, FunctionNext, FunctionPrev, K, T > Reverse
Definition: Iterator.h:209
Struct * __getNode()
Definition: Iterator.h:151
Iterator & operator=(const Iterator &x)
Definition: Iterator.h:103
TreeIterator & operator++()
Definition: Iterator.h:250
T m_Default
Definition: Iterator.h:160
TreeIterator & operator--()
Definition: Iterator.h:256
TreeIterator< originalT, Struct, FunctionPrev, FunctionNext, K, T const > Const
Definition: Iterator.h:206
T operator->()
Definition: Iterator.h:267
Iterator(Struct *Node)
Definition: Iterator.h:93
TreeIterator(const TreeIterator &x)
Definition: Iterator.h:221
TreeIterator< originalT, Struct, FunctionNext, FunctionPrev, K, T const > ConstReverse
Definition: Iterator.h:213
friend bool operator==(const Iterator< _originalT, _Struct, _FunctionPrev, _FunctionNext, _T1 > &x1, const Iterator< _originalT, _Struct, _FunctionPrev, _FunctionNext, _T2 > &x2)
TreeIterator(const TreeIterator< originalT, Struct, FunctionPrev, FunctionNext, K2, T2 > &x)
Definition: Iterator.h:227
T & operator->()
Definition: Iterator.h:139
~Iterator()
Definition: Iterator.h:97
Struct * __getNode()
Definition: Iterator.h:279
Iterator & operator--()
Definition: Iterator.h:117
Iterator & operator++()
Definition: Iterator.h:110
Struct * m_Node
Definition: Iterator.h:158
Iterator(const Iterator &x)
Definition: Iterator.h:80
Iterator()
Definition: Iterator.h:75
Iterator< originalT, Struct, FunctionPrev, FunctionNext, T const > Const
Definition: Iterator.h:67
An iterator applicable for many data structures.
Definition: Iterator.h:43
Iterator(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x)
Definition: Iterator.h:86
Struct * m_Node
Definition: Iterator.h:316
T & operator*()
Definition: Iterator.h:128
An iterator applicable for many data structures.
Definition: Iterator.h:180
TreeIterator & operator=(const TreeIterator &x)
Definition: Iterator.h:244
Iterator< originalT, Struct, FunctionNext, FunctionPrev, T > Reverse
Definition: Iterator.h:69
Iterator< originalT, Struct, FunctionNext, FunctionPrev, T const > ConstReverse
Definition: Iterator.h:72