blob: 3d2f5a8b6ff38a1b3349872e575230094397c41e [file] [log] [blame]
Howard Hinnantc1198c32010-07-16 19:08:36 +00001#ifndef ITERATORS_H
2#define ITERATORS_H
3
4#include <iterator>
5
6template <class It>
Howard Hinnant48b242a2010-08-14 18:14:02 +00007class output_iterator
8{
9 It it_;
10
11 template <class U> friend class output_iterator;
12public:
13 typedef std::output_iterator_tag iterator_category;
14 typedef typename std::iterator_traits<It>::value_type value_type;
15 typedef typename std::iterator_traits<It>::difference_type difference_type;
16 typedef It pointer;
17 typedef typename std::iterator_traits<It>::reference reference;
18
19 It base() const {return it_;}
20
21 explicit output_iterator(It it) : it_(it) {}
22 template <class U>
23 output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
24
25 reference operator*() const {return *it_;}
26
27 output_iterator& operator++() {++it_; return *this;}
28 output_iterator operator++(int)
29 {output_iterator tmp(*this); ++(*this); return tmp;}
30};
31
32template <class It>
Howard Hinnantc1198c32010-07-16 19:08:36 +000033class input_iterator
34{
35 It it_;
36
37 template <class U> friend class input_iterator;
38public:
39 typedef std::input_iterator_tag iterator_category;
40 typedef typename std::iterator_traits<It>::value_type value_type;
41 typedef typename std::iterator_traits<It>::difference_type difference_type;
42 typedef It pointer;
43 typedef typename std::iterator_traits<It>::reference reference;
44
45 It base() const {return it_;}
46
47 input_iterator() : it_() {}
48 explicit input_iterator(It it) : it_(it) {}
49 template <class U>
50 input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
51
52 reference operator*() const {return *it_;}
53 pointer operator->() const {return it_;}
54
55 input_iterator& operator++() {++it_; return *this;}
56 input_iterator operator++(int)
57 {input_iterator tmp(*this); ++(*this); return tmp;}
58
59 friend bool operator==(const input_iterator& x, const input_iterator& y)
60 {return x.it_ == y.it_;}
61 friend bool operator!=(const input_iterator& x, const input_iterator& y)
62 {return !(x == y);}
63};
64
65template <class T, class U>
66inline
67bool
68operator==(const input_iterator<T>& x, const input_iterator<U>& y)
69{
70 return x.base() == y.base();
71}
72
73template <class T, class U>
74inline
75bool
76operator!=(const input_iterator<T>& x, const input_iterator<U>& y)
77{
78 return !(x == y);
79}
80
81template <class It>
82class forward_iterator
83{
84 It it_;
85
86 template <class U> friend class forward_iterator;
87public:
88 typedef std::forward_iterator_tag iterator_category;
89 typedef typename std::iterator_traits<It>::value_type value_type;
90 typedef typename std::iterator_traits<It>::difference_type difference_type;
91 typedef It pointer;
92 typedef typename std::iterator_traits<It>::reference reference;
93
94 It base() const {return it_;}
95
96 forward_iterator() : it_() {}
97 explicit forward_iterator(It it) : it_(it) {}
98 template <class U>
99 forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
100
101 reference operator*() const {return *it_;}
102 pointer operator->() const {return it_;}
103
104 forward_iterator& operator++() {++it_; return *this;}
105 forward_iterator operator++(int)
106 {forward_iterator tmp(*this); ++(*this); return tmp;}
107
108 friend bool operator==(const forward_iterator& x, const forward_iterator& y)
109 {return x.it_ == y.it_;}
110 friend bool operator!=(const forward_iterator& x, const forward_iterator& y)
111 {return !(x == y);}
112};
113
114template <class T, class U>
115inline
116bool
117operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
118{
119 return x.base() == y.base();
120}
121
122template <class T, class U>
123inline
124bool
125operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
126{
127 return !(x == y);
128}
129
130template <class It>
131class bidirectional_iterator
132{
133 It it_;
134
135 template <class U> friend class bidirectional_iterator;
136public:
137 typedef std::bidirectional_iterator_tag iterator_category;
138 typedef typename std::iterator_traits<It>::value_type value_type;
139 typedef typename std::iterator_traits<It>::difference_type difference_type;
140 typedef It pointer;
141 typedef typename std::iterator_traits<It>::reference reference;
142
143 It base() const {return it_;}
144
145 bidirectional_iterator() : it_() {}
146 explicit bidirectional_iterator(It it) : it_(it) {}
147 template <class U>
148 bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
149
150 reference operator*() const {return *it_;}
151 pointer operator->() const {return it_;}
152
153 bidirectional_iterator& operator++() {++it_; return *this;}
154 bidirectional_iterator operator++(int)
155 {bidirectional_iterator tmp(*this); ++(*this); return tmp;}
156
157 bidirectional_iterator& operator--() {--it_; return *this;}
158 bidirectional_iterator operator--(int)
159 {bidirectional_iterator tmp(*this); --(*this); return tmp;}
160};
161
162template <class T, class U>
163inline
164bool
165operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
166{
167 return x.base() == y.base();
168}
169
170template <class T, class U>
171inline
172bool
173operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
174{
175 return !(x == y);
176}
177
178template <class It>
179class random_access_iterator
180{
181 It it_;
182
183 template <class U> friend class random_access_iterator;
184public:
185 typedef std::random_access_iterator_tag iterator_category;
186 typedef typename std::iterator_traits<It>::value_type value_type;
187 typedef typename std::iterator_traits<It>::difference_type difference_type;
188 typedef It pointer;
189 typedef typename std::iterator_traits<It>::reference reference;
190
191 It base() const {return it_;}
192
193 random_access_iterator() : it_() {}
194 explicit random_access_iterator(It it) : it_(it) {}
195 template <class U>
196 random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
197
198 reference operator*() const {return *it_;}
199 pointer operator->() const {return it_;}
200
201 random_access_iterator& operator++() {++it_; return *this;}
202 random_access_iterator operator++(int)
203 {random_access_iterator tmp(*this); ++(*this); return tmp;}
204
205 random_access_iterator& operator--() {--it_; return *this;}
206 random_access_iterator operator--(int)
207 {random_access_iterator tmp(*this); --(*this); return tmp;}
208
209 random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
210 random_access_iterator operator+(difference_type n) const
211 {random_access_iterator tmp(*this); tmp += n; return tmp;}
212 friend random_access_iterator operator+(difference_type n, random_access_iterator x)
213 {x += n; return x;}
214 random_access_iterator& operator-=(difference_type n) {return *this += -n;}
215 random_access_iterator operator-(difference_type n) const
216 {random_access_iterator tmp(*this); tmp -= n; return tmp;}
217
218 reference operator[](difference_type n) const {return it_[n];}
219};
220
221template <class T, class U>
222inline
223bool
224operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
225{
226 return x.base() == y.base();
227}
228
229template <class T, class U>
230inline
231bool
232operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
233{
234 return !(x == y);
235}
236
237template <class T, class U>
238inline
239bool
240operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
241{
242 return x.base() < y.base();
243}
244
245template <class T, class U>
246inline
247bool
248operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
249{
250 return !(y < x);
251}
252
253template <class T, class U>
254inline
255bool
256operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
257{
258 return y < x;
259}
260
261template <class T, class U>
262inline
263bool
264operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
265{
266 return !(x < y);
267}
268
269template <class T, class U>
270inline
271typename std::iterator_traits<T>::difference_type
272operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
273{
274 return x.base() - y.base();
275}
276
Howard Hinnantf36101d2010-08-22 00:45:01 +0000277#endif // ITERATORS_H