blob: e09fd834d215be0b816a6cca98193b53a8ddbfd3 [file] [log] [blame]
Marshall Clow98760c12014-01-16 16:58:45 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Howard Hinnantaa698082010-07-16 19:08:36 +000010#ifndef ITERATORS_H
11#define ITERATORS_H
12
13#include <iterator>
Marshall Clow01c6bbd2014-09-17 04:09:35 +000014#include <cassert>
Howard Hinnantaa698082010-07-16 19:08:36 +000015
Eric Fiselier62a0e012014-10-27 20:26:25 +000016#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
17#define DELETE_FUNCTION = delete
18#else
19#define DELETE_FUNCTION
20#endif
21
Howard Hinnantaa698082010-07-16 19:08:36 +000022template <class It>
Howard Hinnant27405f92010-08-14 18:14:02 +000023class output_iterator
24{
25 It it_;
26
27 template <class U> friend class output_iterator;
28public:
29 typedef std::output_iterator_tag iterator_category;
Marshall Clow304c31b2013-01-09 17:20:02 +000030 typedef void value_type;
Howard Hinnant27405f92010-08-14 18:14:02 +000031 typedef typename std::iterator_traits<It>::difference_type difference_type;
32 typedef It pointer;
33 typedef typename std::iterator_traits<It>::reference reference;
34
35 It base() const {return it_;}
36
Marshall Clowba1920f2013-01-03 02:29:29 +000037 output_iterator () {}
Howard Hinnant27405f92010-08-14 18:14:02 +000038 explicit output_iterator(It it) : it_(it) {}
39 template <class U>
40 output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
41
42 reference operator*() const {return *it_;}
43
44 output_iterator& operator++() {++it_; return *this;}
45 output_iterator operator++(int)
46 {output_iterator tmp(*this); ++(*this); return tmp;}
Eric Fiselierb9919752014-10-27 19:28:20 +000047
48 template <class T>
Eric Fiselier62a0e012014-10-27 20:26:25 +000049 void operator,(T const &) DELETE_FUNCTION;
Howard Hinnant27405f92010-08-14 18:14:02 +000050};
51
52template <class It>
Howard Hinnantaa698082010-07-16 19:08:36 +000053class input_iterator
54{
55 It it_;
56
57 template <class U> friend class input_iterator;
58public:
59 typedef std::input_iterator_tag iterator_category;
60 typedef typename std::iterator_traits<It>::value_type value_type;
61 typedef typename std::iterator_traits<It>::difference_type difference_type;
62 typedef It pointer;
63 typedef typename std::iterator_traits<It>::reference reference;
64
65 It base() const {return it_;}
66
67 input_iterator() : it_() {}
68 explicit input_iterator(It it) : it_(it) {}
69 template <class U>
70 input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
71
72 reference operator*() const {return *it_;}
73 pointer operator->() const {return it_;}
74
75 input_iterator& operator++() {++it_; return *this;}
76 input_iterator operator++(int)
77 {input_iterator tmp(*this); ++(*this); return tmp;}
78
79 friend bool operator==(const input_iterator& x, const input_iterator& y)
80 {return x.it_ == y.it_;}
81 friend bool operator!=(const input_iterator& x, const input_iterator& y)
82 {return !(x == y);}
Eric Fiselierb9919752014-10-27 19:28:20 +000083
84 template <class T>
Eric Fiselier62a0e012014-10-27 20:26:25 +000085 void operator,(T const &) DELETE_FUNCTION;
Howard Hinnantaa698082010-07-16 19:08:36 +000086};
87
88template <class T, class U>
89inline
90bool
91operator==(const input_iterator<T>& x, const input_iterator<U>& y)
92{
93 return x.base() == y.base();
94}
95
96template <class T, class U>
97inline
98bool
99operator!=(const input_iterator<T>& x, const input_iterator<U>& y)
100{
101 return !(x == y);
102}
103
104template <class It>
105class forward_iterator
106{
107 It it_;
108
109 template <class U> friend class forward_iterator;
110public:
111 typedef std::forward_iterator_tag iterator_category;
112 typedef typename std::iterator_traits<It>::value_type value_type;
113 typedef typename std::iterator_traits<It>::difference_type difference_type;
114 typedef It pointer;
115 typedef typename std::iterator_traits<It>::reference reference;
116
117 It base() const {return it_;}
118
119 forward_iterator() : it_() {}
120 explicit forward_iterator(It it) : it_(it) {}
121 template <class U>
122 forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
123
124 reference operator*() const {return *it_;}
125 pointer operator->() const {return it_;}
126
127 forward_iterator& operator++() {++it_; return *this;}
128 forward_iterator operator++(int)
129 {forward_iterator tmp(*this); ++(*this); return tmp;}
130
131 friend bool operator==(const forward_iterator& x, const forward_iterator& y)
132 {return x.it_ == y.it_;}
133 friend bool operator!=(const forward_iterator& x, const forward_iterator& y)
134 {return !(x == y);}
Eric Fiselierb9919752014-10-27 19:28:20 +0000135
136 template <class T>
Eric Fiselier62a0e012014-10-27 20:26:25 +0000137 void operator,(T const &) DELETE_FUNCTION;
Howard Hinnantaa698082010-07-16 19:08:36 +0000138};
139
140template <class T, class U>
141inline
142bool
143operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
144{
145 return x.base() == y.base();
146}
147
148template <class T, class U>
149inline
150bool
151operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
152{
153 return !(x == y);
154}
155
156template <class It>
157class bidirectional_iterator
158{
159 It it_;
160
161 template <class U> friend class bidirectional_iterator;
162public:
163 typedef std::bidirectional_iterator_tag iterator_category;
164 typedef typename std::iterator_traits<It>::value_type value_type;
165 typedef typename std::iterator_traits<It>::difference_type difference_type;
166 typedef It pointer;
167 typedef typename std::iterator_traits<It>::reference reference;
168
169 It base() const {return it_;}
170
171 bidirectional_iterator() : it_() {}
172 explicit bidirectional_iterator(It it) : it_(it) {}
173 template <class U>
174 bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
175
176 reference operator*() const {return *it_;}
177 pointer operator->() const {return it_;}
178
179 bidirectional_iterator& operator++() {++it_; return *this;}
180 bidirectional_iterator operator++(int)
181 {bidirectional_iterator tmp(*this); ++(*this); return tmp;}
182
183 bidirectional_iterator& operator--() {--it_; return *this;}
184 bidirectional_iterator operator--(int)
185 {bidirectional_iterator tmp(*this); --(*this); return tmp;}
Eric Fiselierb9919752014-10-27 19:28:20 +0000186
187 template <class T>
Eric Fiselier62a0e012014-10-27 20:26:25 +0000188 void operator,(T const &) DELETE_FUNCTION;
Howard Hinnantaa698082010-07-16 19:08:36 +0000189};
190
191template <class T, class U>
192inline
193bool
194operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
195{
196 return x.base() == y.base();
197}
198
199template <class T, class U>
200inline
201bool
202operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
203{
204 return !(x == y);
205}
206
207template <class It>
208class random_access_iterator
209{
210 It it_;
211
212 template <class U> friend class random_access_iterator;
213public:
214 typedef std::random_access_iterator_tag iterator_category;
215 typedef typename std::iterator_traits<It>::value_type value_type;
216 typedef typename std::iterator_traits<It>::difference_type difference_type;
217 typedef It pointer;
218 typedef typename std::iterator_traits<It>::reference reference;
219
220 It base() const {return it_;}
221
222 random_access_iterator() : it_() {}
223 explicit random_access_iterator(It it) : it_(it) {}
224 template <class U>
225 random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
226
227 reference operator*() const {return *it_;}
228 pointer operator->() const {return it_;}
229
230 random_access_iterator& operator++() {++it_; return *this;}
231 random_access_iterator operator++(int)
232 {random_access_iterator tmp(*this); ++(*this); return tmp;}
233
234 random_access_iterator& operator--() {--it_; return *this;}
235 random_access_iterator operator--(int)
236 {random_access_iterator tmp(*this); --(*this); return tmp;}
237
238 random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
239 random_access_iterator operator+(difference_type n) const
240 {random_access_iterator tmp(*this); tmp += n; return tmp;}
241 friend random_access_iterator operator+(difference_type n, random_access_iterator x)
242 {x += n; return x;}
243 random_access_iterator& operator-=(difference_type n) {return *this += -n;}
244 random_access_iterator operator-(difference_type n) const
245 {random_access_iterator tmp(*this); tmp -= n; return tmp;}
246
247 reference operator[](difference_type n) const {return it_[n];}
Eric Fiselierb9919752014-10-27 19:28:20 +0000248
249 template <class T>
Eric Fiselier62a0e012014-10-27 20:26:25 +0000250 void operator,(T const &) DELETE_FUNCTION;
Howard Hinnantaa698082010-07-16 19:08:36 +0000251};
252
253template <class T, class U>
254inline
255bool
256operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
257{
258 return x.base() == y.base();
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
271bool
272operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
273{
274 return x.base() < y.base();
275}
276
277template <class T, class U>
278inline
279bool
280operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
281{
282 return !(y < x);
283}
284
285template <class T, class U>
286inline
287bool
288operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
289{
290 return y < x;
291}
292
293template <class T, class U>
294inline
295bool
296operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
297{
298 return !(x < y);
299}
300
301template <class T, class U>
302inline
303typename std::iterator_traits<T>::difference_type
304operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
305{
306 return x.base() - y.base();
307}
308
Marshall Clow8226d0b2013-01-04 18:24:04 +0000309template <class Iter>
310inline Iter base(output_iterator<Iter> i) { return i.base(); }
311
312template <class Iter>
313inline Iter base(input_iterator<Iter> i) { return i.base(); }
314
315template <class Iter>
316inline Iter base(forward_iterator<Iter> i) { return i.base(); }
317
318template <class Iter>
319inline Iter base(bidirectional_iterator<Iter> i) { return i.base(); }
320
321template <class Iter>
322inline Iter base(random_access_iterator<Iter> i) { return i.base(); }
323
Howard Hinnant171771a2013-07-08 21:06:38 +0000324template <class Iter> // everything else
Marshall Clow8226d0b2013-01-04 18:24:04 +0000325inline Iter base(Iter i) { return i; }
326
Eric Fiselier62a0e012014-10-27 20:26:25 +0000327#undef DELETE_FUNCTION
328
Howard Hinnantbbd80862010-08-22 00:45:01 +0000329#endif // ITERATORS_H