-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcircular_iterator.hpp
112 lines (77 loc) · 1.89 KB
/
circular_iterator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef CIRCULAR_ITERATOR_H__
#define CIRCULAR_ITERATOR_H__
#include <iterator>
namespace util {
template<typename T>
class const_circular_iterator : public std::iterator<std::bidirectional_iterator_tag, typename T::value_type> {
private:
typedef typename T::iterator iterator_type;
typedef typename T::value_type value_type;
protected:
iterator_type begin;
iterator_type end;
iterator_type iterator;
public:
const_circular_iterator(T& t) :
iterator(t.begin()),
begin(t.begin()),
end(t.end())
{};
const_circular_iterator(iterator_type b, iterator_type e) :
iterator(b),
begin(b),
end(e)
{};
const_circular_iterator<T>& operator++() {
++iterator;
if (iterator == end)
iterator = begin;
return (*this);
}
const_circular_iterator<T> operator++(int) {
const_circular_iterator<T> t(*this);
++(*this);
return t;
}
const_circular_iterator<T>& operator--() {
if (iterator == begin)
iterator = end;
--iterator;
return (*this);
}
const_circular_iterator<T> operator--(int) {
const_circular_iterator<T> t(*this);
--(*this);
return t;
}
const value_type operator*() const {
return (*iterator);
}
bool operator==(const const_circular_iterator<T>& rhs) const {
return (iterator == rhs.iterator);
}
bool operator!=(const const_circular_iterator<T>& rhs) const {
return ! operator==(rhs);
}
};
template<typename T>
class circular_iterator : public const_circular_iterator<T> {
private:
typedef typename T::iterator iterator_type;
typedef typename T::value_type value_type;
public:
circular_iterator(T& t) :
const_circular_iterator<T>(t)
{};
circular_iterator(iterator_type b, iterator_type e) :
const_circular_iterator<T>(b,e)
{};
const value_type operator*() const {
return *(this->iterator);
}
value_type& operator*() {
return *(this->iterator);
}
};
}; // namespace util
#endif // CIRCULAR_ITERATOR_H__