]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_iterator.h
sbuf_iter.h: Change to..
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_iterator.h
1 /*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 *
15 * Copyright (c) 1996-1998
16 * Silicon Graphics Computer Systems, Inc.
17 *
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
25 */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
29 */
30
31 #ifndef __SGI_STL_INTERNAL_ITERATOR_H
32 #define __SGI_STL_INTERNAL_ITERATOR_H
33
34 namespace std
35 {
36 // 24.4.1 Reverse iterators
37 template<typename _Iterator>
38 class reverse_iterator
39 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
40 typename iterator_traits<_Iterator>::value_type,
41 typename iterator_traits<_Iterator>::difference_type,
42 typename iterator_traits<_Iterator>::pointer,
43 typename iterator_traits<_Iterator>::reference>
44 {
45 protected:
46 _Iterator _M_current;
47
48 public:
49 typedef _Iterator iterator_type;
50 typedef typename iterator_traits<_Iterator>::difference_type
51 difference_type;
52 typedef typename iterator_traits<_Iterator>::reference reference;
53 typedef typename iterator_traits<_Iterator>::pointer pointer;
54
55 public:
56 reverse_iterator() {}
57
58 explicit
59 reverse_iterator(iterator_type __x) : _M_current(__x) {}
60
61 reverse_iterator(const reverse_iterator& __x)
62 : _M_current(__x._M_current) { }
63
64 template<typename _Iter>
65 reverse_iterator(const reverse_iterator<_Iter>& __x)
66 : _M_current(__x.base()) {}
67
68 iterator_type
69 base() const { return _M_current; }
70
71 reference
72 operator*() const
73 {
74 _Iterator __tmp = _M_current;
75 return *--__tmp;
76 }
77
78 pointer
79 operator->() const { return &(operator*()); }
80
81 reverse_iterator&
82 operator++()
83 {
84 --_M_current;
85 return *this;
86 }
87
88 reverse_iterator
89 operator++(int)
90 {
91 reverse_iterator __tmp = *this;
92 --_M_current;
93 return __tmp;
94 }
95
96 reverse_iterator&
97 operator--()
98 {
99 ++_M_current;
100 return *this;
101 }
102
103 reverse_iterator operator--(int)
104 {
105 reverse_iterator __tmp = *this;
106 ++_M_current;
107 return __tmp;
108 }
109
110 reverse_iterator
111 operator+(difference_type __n) const
112 { return reverse_iterator(_M_current - __n); }
113
114 reverse_iterator&
115 operator+=(difference_type __n)
116 {
117 _M_current -= __n;
118 return *this;
119 }
120
121 reverse_iterator
122 operator-(difference_type __n) const
123 { return reverse_iterator(_M_current + __n); }
124
125 reverse_iterator&
126 operator-=(difference_type __n)
127 {
128 _M_current += __n;
129 return *this;
130 }
131
132 reference
133 operator[](difference_type __n) const { return *(*this + __n); }
134 };
135
136 template<typename _Iterator>
137 inline bool
138 operator==(const reverse_iterator<_Iterator>& __x,
139 const reverse_iterator<_Iterator>& __y)
140 { return __x.base() == __y.base(); }
141
142 template<typename _Iterator>
143 inline bool
144 operator<(const reverse_iterator<_Iterator>& __x,
145 const reverse_iterator<_Iterator>& __y)
146 { return __y.base() < __x.base(); }
147
148 template<typename _Iterator>
149 inline bool
150 operator!=(const reverse_iterator<_Iterator>& __x,
151 const reverse_iterator<_Iterator>& __y)
152 { return !(__x == __y); }
153
154 template<typename _Iterator>
155 inline bool
156 operator>(const reverse_iterator<_Iterator>& __x,
157 const reverse_iterator<_Iterator>& __y)
158 { return __y < __x; }
159
160 template<typename _Iterator>
161 inline bool
162 operator<=(const reverse_iterator<_Iterator>& __x,
163 const reverse_iterator<_Iterator>& __y)
164 { return !(__y < __x); }
165
166 template<typename _Iterator>
167 inline bool
168 operator>=(const reverse_iterator<_Iterator>& __x,
169 const reverse_iterator<_Iterator>& __y)
170 { return !(__x < __y); }
171
172 template<typename _Iterator>
173 inline typename reverse_iterator<_Iterator>::difference_type
174 operator-(const reverse_iterator<_Iterator>& __x,
175 const reverse_iterator<_Iterator>& __y)
176 { return __y.base() - __x.base(); }
177
178 template<typename _Iterator>
179 inline reverse_iterator<_Iterator>
180 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
181 const reverse_iterator<_Iterator>& __x)
182 { return reverse_iterator<_Iterator>(__x.base() - __n); }
183
184 // 24.4.2.2.1 back_insert_iterator
185 template<typename _Container>
186 class back_insert_iterator
187 : public iterator<output_iterator_tag, void, void, void, void>
188 {
189 protected:
190 _Container* container;
191
192 public:
193 typedef _Container container_type;
194
195 explicit
196 back_insert_iterator(_Container& __x) : container(&__x) {}
197
198 back_insert_iterator&
199 operator=(const typename _Container::value_type& __value)
200 {
201 container->push_back(__value);
202 return *this;
203 }
204
205 back_insert_iterator&
206 operator*() { return *this; }
207
208 back_insert_iterator&
209 operator++() { return *this; }
210
211 back_insert_iterator
212 operator++(int) { return *this; }
213 };
214
215 template<typename _Container>
216 inline back_insert_iterator<_Container>
217 back_inserter(_Container& __x)
218 { return back_insert_iterator<_Container>(__x); }
219
220 template<typename _Container>
221 class front_insert_iterator
222 : public iterator<output_iterator_tag, void, void, void, void>
223 {
224 protected:
225 _Container* container;
226
227 public:
228 typedef _Container container_type;
229
230 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
231
232 front_insert_iterator&
233 operator=(const typename _Container::value_type& __value)
234 {
235 container->push_front(__value);
236 return *this;
237 }
238
239 front_insert_iterator&
240 operator*() { return *this; }
241
242 front_insert_iterator&
243 operator++() { return *this; }
244
245 front_insert_iterator
246 operator++(int) { return *this; }
247 };
248
249 template<typename _Container>
250 inline front_insert_iterator<_Container> front_inserter(_Container& __x)
251 { return front_insert_iterator<_Container>(__x); }
252
253 template<typename _Container>
254 class insert_iterator
255 : public iterator<output_iterator_tag, void, void, void, void>
256 {
257 protected:
258 _Container* container;
259 typename _Container::iterator iter;
260
261 public:
262 typedef _Container container_type;
263
264 insert_iterator(_Container& __x, typename _Container::iterator __i)
265 : container(&__x), iter(__i) {}
266
267 insert_iterator&
268 operator=(const typename _Container::const_reference __value)
269 {
270 iter = container->insert(iter, __value);
271 ++iter;
272 return *this;
273 }
274
275 insert_iterator&
276 operator*() { return *this; }
277
278 insert_iterator&
279 operator++() { return *this; }
280
281 insert_iterator&
282 operator++(int) { return *this; }
283 };
284
285 template<typename _Container, typename _Iterator>
286 inline
287 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
288 {
289 typedef typename _Container::iterator __iter;
290 return insert_iterator<_Container>(__x, __iter(__i));
291 }
292
293 // This iterator adapter is 'normal' in the sense that it does not
294 // change the semantics of any of the operators of its itererator
295 // parameter. Its primary purpose is to convert an iterator that is
296 // not a class, e.g. a pointer, into an iterator that is a class.
297 // The _Container parameter exists solely so that different containers
298 // using this template can instantiate different types, even if the
299 // _Iterator parameter is the same.
300 template<typename _Iterator, typename _Container>
301 class __normal_iterator
302 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
303 typename iterator_traits<_Iterator>::value_type,
304 typename iterator_traits<_Iterator>::difference_type,
305 typename iterator_traits<_Iterator>::pointer,
306 typename iterator_traits<_Iterator>::reference>
307 {
308 protected:
309 _Iterator _M_current;
310
311 public:
312 typedef typename iterator_traits<_Iterator>::difference_type
313 difference_type;
314 typedef typename iterator_traits<_Iterator>::reference reference;
315 typedef typename iterator_traits<_Iterator>::pointer pointer;
316
317 __normal_iterator() : _M_current(_Iterator()) { }
318
319 explicit
320 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
321
322 // Allow iterator to const_iterator conversion
323 template<typename _Iter>
324 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
325 : _M_current(__i.base()) { }
326
327 // Forward iterator requirements
328 reference
329 operator*() const { return *_M_current; }
330
331 pointer
332 operator->() const { return _M_current; }
333
334 __normal_iterator&
335 operator++() { ++_M_current; return *this; }
336
337 __normal_iterator
338 operator++(int) { return __normal_iterator(_M_current++); }
339
340 // Bidirectional iterator requirements
341 __normal_iterator&
342 operator--() { --_M_current; return *this; }
343
344 __normal_iterator
345 operator--(int) { return __normal_iterator(_M_current--); }
346
347 // Random access iterator requirements
348 reference
349 operator[](const difference_type& __n) const
350 { return _M_current[__n]; }
351
352 __normal_iterator&
353 operator+=(const difference_type& __n)
354 { _M_current += __n; return *this; }
355
356 __normal_iterator
357 operator+(const difference_type& __n) const
358 { return __normal_iterator(_M_current + __n); }
359
360 __normal_iterator&
361 operator-=(const difference_type& __n)
362 { _M_current -= __n; return *this; }
363
364 __normal_iterator
365 operator-(const difference_type& __n) const
366 { return __normal_iterator(_M_current - __n); }
367
368 difference_type
369 operator-(const __normal_iterator& __i) const
370 { return _M_current - __i._M_current; }
371
372 const _Iterator&
373 base() const { return _M_current; }
374 };
375
376 // Forward iterator requirements
377 template<typename _IteratorL, typename _IteratorR, typename _Container>
378 inline bool
379 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
380 const __normal_iterator<_IteratorR, _Container>& __rhs)
381 { return __lhs.base() == __rhs.base(); }
382
383 template<typename _IteratorL, typename _IteratorR, typename _Container>
384 inline bool
385 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
386 const __normal_iterator<_IteratorR, _Container>& __rhs)
387 { return !(__lhs == __rhs); }
388
389 // Random access iterator requirements
390 template<typename _IteratorL, typename _IteratorR, typename _Container>
391 inline bool
392 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
393 const __normal_iterator<_IteratorR, _Container>& __rhs)
394 { return __lhs.base() < __rhs.base(); }
395
396 template<typename _IteratorL, typename _IteratorR, typename _Container>
397 inline bool
398 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
399 const __normal_iterator<_IteratorR, _Container>& __rhs)
400 { return __rhs < __lhs; }
401
402 template<typename _IteratorL, typename _IteratorR, typename _Container>
403 inline bool
404 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
405 const __normal_iterator<_IteratorR, _Container>& __rhs)
406 { return !(__rhs < __lhs); }
407
408 template<typename _IteratorL, typename _IteratorR, typename _Container>
409 inline bool
410 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
411 const __normal_iterator<_IteratorR, _Container>& __rhs)
412 { return !(__lhs < __rhs); }
413
414 template<typename _Iterator, typename _Container>
415 inline __normal_iterator<_Iterator, _Container>
416 operator+(__normal_iterator<_Iterator, _Container>::difference_type __n,
417 const __normal_iterator<_Iterator, _Container>& __i)
418 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
419 } // namespace std
420
421 #endif
422
423 // Local Variables:
424 // mode:C++
425 // End: