1 |
// Iterator |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#ifndef _Iterator_hpp_ |
8 |
#define _Iterator_hpp_ |
9 |
|
10 |
#include "NullNode.hpp" |
11 |
|
12 |
template<typename Type> |
13 |
class DoublyLinkedList; |
14 |
|
15 |
template<typename Type> |
16 |
class Iterator |
17 |
{ |
18 |
private: |
19 |
NullNode<Type>* front; |
20 |
NullNode<Type>* back; |
21 |
Node<Type>* here; |
22 |
public: |
23 |
Iterator(NullNode<Type>* front, NullNode<Type>* back, bool forward = true) : front(front), back(back) { here = forward ? front->getNext() : back->getPrevious(); } |
24 |
bool end(void) { return here == back; } |
25 |
bool begin(void) { return here == front; } |
26 |
Iterator<Type>& operator++() { here = here->getNext(); return *this; } |
27 |
Iterator<Type> operator++(int); |
28 |
Iterator<Type>& operator--() { here = here->getPrevious(); return *this; } |
29 |
Iterator<Type> operator--(int); |
30 |
const Type& operator*() { return here->getValue(); } |
31 |
const Type* operator->() { return &here->getValue(); } |
32 |
}; |
33 |
|
34 |
template<typename Type> |
35 |
Iterator<Type> Iterator<Type>::operator++(int) |
36 |
{ |
37 |
Iterator itor(*this); |
38 |
|
39 |
++*this; |
40 |
|
41 |
return itor; |
42 |
} |
43 |
|
44 |
template<typename Type> |
45 |
Iterator<Type> Iterator<Type>::operator--(int) |
46 |
{ |
47 |
Iterator itor(*this); |
48 |
|
49 |
--*this; |
50 |
|
51 |
return itor; |
52 |
} |
53 |
|
54 |
#endif // _Iterator_hpp_ |