stl_iterator.h (reverse_iterator): Inherit from iterator.
[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<class _Iterator>
38 class reverse_iterator
39 : public iterator<iterator_traits<_Iterator>::iterator_category,
40 iterator_traits<_Iterator>::value_type,
41 iterator_traits<_Iterator>::difference_type,
42 iterator_traits<_Iterator>::pointer,
43 iterator_traits<_Iterator>::reference>
44 {
45 protected:
46 _Iterator current;
47
48 public:
49 typedef iterator_traits<_Iterator> __traits_type;
50 typedef typename __traits_type::iterator_category iterator_category;
51 typedef typename __traits_type::value_type value_type;
52 typedef typename __traits_type::difference_type difference_type;
53 typedef typename __traits_type::pointer pointer;
54 typedef typename __traits_type::reference reference;
55
56 typedef _Iterator iterator_type;
57 typedef reverse_iterator<_Iterator> _Self;
58
59 public:
60 reverse_iterator() {}
61
62 explicit
63 reverse_iterator(iterator_type __x) : current(__x) {}
64
65 reverse_iterator(const _Self& __x) : current(__x.current) {}
66
67 template <class _Iter>
68 reverse_iterator(const reverse_iterator<_Iter>& __x)
69 : current(__x.base()) {}
70
71 iterator_type
72 base() const { return current; }
73
74 reference
75 operator*() const
76 {
77 _Iterator __tmp = current;
78 return *--__tmp;
79 }
80
81 pointer
82 operator->() const { return &(operator*()); }
83
84 _Self&
85 operator++()
86 {
87 --current;
88 return *this;
89 }
90
91 _Self
92 operator++(int)
93 {
94 _Self __tmp = *this;
95 --current;
96 return __tmp;
97 }
98
99 _Self&
100 operator--()
101 {
102 ++current;
103 return *this;
104 }
105
106 _Self operator--(int)
107 {
108 _Self __tmp = *this;
109 ++current;
110 return __tmp;
111 }
112
113 _Self
114 operator+(difference_type __n) const
115 { return _Self(current - __n); }
116
117 _Self&
118 operator+=(difference_type __n)
119 {
120 current -= __n;
121 return *this;
122 }
123
124 _Self
125 operator-(difference_type __n) const
126 { return _Self(current + __n); }
127
128 _Self&
129 operator-=(difference_type __n)
130 {
131 current += __n;
132 return *this;
133 }
134
135 reference
136 operator[](difference_type __n) const { return *(*this + __n); }
137 };
138
139 template<class _Iterator>
140 inline bool
141 operator==(const reverse_iterator<_Iterator>& __x,
142 const reverse_iterator<_Iterator>& __y)
143 { return __x.base() == __y.base(); }
144
145 template <class _Iterator>
146 inline bool
147 operator<(const reverse_iterator<_Iterator>& __x,
148 const reverse_iterator<_Iterator>& __y)
149 { return __y.base() < __x.base(); }
150
151 template <class _Iterator>
152 inline bool
153 operator!=(const reverse_iterator<_Iterator>& __x,
154 const reverse_iterator<_Iterator>& __y)
155 { return !(__x == __y); }
156
157 template <class _Iterator>
158 inline bool
159 operator>(const reverse_iterator<_Iterator>& __x,
160 const reverse_iterator<_Iterator>& __y)
161 { return __y < __x; }
162
163 template <class _Iterator>
164 inline bool
165 operator<=(const reverse_iterator<_Iterator>& __x,
166 const reverse_iterator<_Iterator>& __y)
167 { return !(__y < __x); }
168
169 template <class _Iterator>
170 inline bool
171 operator>=(const reverse_iterator<_Iterator>& __x,
172 const reverse_iterator<_Iterator>& __y)
173 { return !(__x < __y); }
174
175 template<class _Iterator>
176 inline typename reverse_iterator<_Iterator>::difference_type
177 operator-(const reverse_iterator<_Iterator>& __x,
178 const reverse_iterator<_Iterator>& __y)
179 { return __y.base() - __x.base(); }
180
181 template <class _Iterator>
182 inline reverse_iterator<_Iterator>
183 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
184 const reverse_iterator<_Iterator>& __x)
185 { return reverse_iterator<_Iterator>(__x.base() - __n); }
186
187 // 24.4.2.2.1 back_insert_iterator
188 template <class _Container>
189 class back_insert_iterator
190 : public iterator<output_iterator_tag, void, void, void, void>
191 {
192 protected:
193 _Container* container;
194
195 public:
196 typedef _Container container_type;
197
198 explicit
199 back_insert_iterator(_Container& __x) : container(&__x) {}
200
201 back_insert_iterator<_Container>&
202 operator=(const typename _Container::value_type& __value)
203 {
204 container->push_back(__value);
205 return *this;
206 }
207
208 back_insert_iterator<_Container>&
209 operator*() { return *this; }
210
211 back_insert_iterator<_Container>&
212 operator++() { return *this; }
213
214 back_insert_iterator<_Container>&
215 operator++(int) { return *this; }
216 };
217
218 template <class _Container>
219 inline back_insert_iterator<_Container>
220 back_inserter(_Container& __x)
221 { return back_insert_iterator<_Container>(__x); }
222
223 template <class _Container>
224 class front_insert_iterator
225 : public iterator<output_iterator_tag, void, void, void, void>
226 {
227 protected:
228 _Container* container;
229
230 public:
231 typedef _Container container_type;
232
233 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
234 front_insert_iterator<_Container>&
235 operator=(const typename _Container::value_type& __value) {
236 container->push_front(__value);
237 return *this;
238 }
239 front_insert_iterator<_Container>& operator*() { return *this; }
240 front_insert_iterator<_Container>& operator++() { return *this; }
241 front_insert_iterator<_Container>& operator++(int) { return *this; }
242 };
243
244 template <class _Container>
245 inline front_insert_iterator<_Container> front_inserter(_Container& __x) {
246 return front_insert_iterator<_Container>(__x);
247 }
248
249 template <class _Container>
250 class insert_iterator
251 : public iterator<output_iterator_tag, void, void, void, void>
252 {
253 protected:
254 _Container* container;
255 typename _Container::iterator iter;
256
257 public:
258 typedef _Container container_type;
259
260 insert_iterator(_Container& __x, typename _Container::iterator __i)
261 : container(&__x), iter(__i) {}
262
263 insert_iterator<_Container>&
264 operator=(const typename _Container::value_type& __value) {
265 iter = container->insert(iter, __value);
266 ++iter;
267 return *this;
268 }
269 insert_iterator<_Container>& operator*() { return *this; }
270 insert_iterator<_Container>& operator++() { return *this; }
271 insert_iterator<_Container>& operator++(int) { return *this; }
272 };
273
274 template <class _Container, class _Iterator>
275 inline
276 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
277 {
278 typedef typename _Container::iterator __iter;
279 return insert_iterator<_Container>(__x, __iter(__i));
280 }
281
282 template <class _BidirectionalIterator, class _Tp, class _Reference = _Tp&,
283 class _Distance = ptrdiff_t>
284 class reverse_bidirectional_iterator {
285 typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp,
286 _Reference, _Distance> _Self;
287 protected:
288 _BidirectionalIterator current;
289 public:
290 typedef bidirectional_iterator_tag iterator_category;
291 typedef _Tp value_type;
292 typedef _Distance difference_type;
293 typedef _Tp* pointer;
294 typedef _Reference reference;
295
296 reverse_bidirectional_iterator() {}
297 explicit reverse_bidirectional_iterator(_BidirectionalIterator __x)
298 : current(__x) {}
299 _BidirectionalIterator base() const { return current; }
300 _Reference operator*() const {
301 _BidirectionalIterator __tmp = current;
302 return *--__tmp;
303 }
304 pointer operator->() const { return &(operator*()); }
305 _Self& operator++() {
306 --current;
307 return *this;
308 }
309 _Self operator++(int) {
310 _Self __tmp = *this;
311 --current;
312 return __tmp;
313 }
314 _Self& operator--() {
315 ++current;
316 return *this;
317 }
318 _Self operator--(int) {
319 _Self __tmp = *this;
320 ++current;
321 return __tmp;
322 }
323 };
324
325 template <class _BiIter, class _Tp, class _Ref, class _Distance>
326 inline bool operator==(
327 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x,
328 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
329 {
330 return __x.base() == __y.base();
331 }
332
333 template <class _BiIter, class _Tp, class _Ref, class _Distance>
334 inline bool operator!=(
335 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x,
336 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
337 {
338 return !(__x == __y);
339 }
340
341
342
343 template <class _Tp,
344 class _CharT = char, class _Traits = char_traits<_CharT>,
345 class _Dist = ptrdiff_t>
346 class istream_iterator {
347 public:
348 typedef _CharT char_type;
349 typedef _Traits traits_type;
350 typedef basic_istream<_CharT, _Traits> istream_type;
351
352 typedef input_iterator_tag iterator_category;
353 typedef _Tp value_type;
354 typedef _Dist difference_type;
355 typedef const _Tp* pointer;
356 typedef const _Tp& reference;
357
358 istream_iterator() : _M_stream(0), _M_ok(false) {}
359 istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
360
361 reference operator*() const { return _M_value; }
362 pointer operator->() const { return &(operator*()); }
363
364 istream_iterator& operator++() {
365 _M_read();
366 return *this;
367 }
368 istream_iterator operator++(int) {
369 istream_iterator __tmp = *this;
370 _M_read();
371 return __tmp;
372 }
373
374 bool _M_equal(const istream_iterator& __x) const
375 { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
376
377 private:
378 istream_type* _M_stream;
379 _Tp _M_value;
380 bool _M_ok;
381
382 void _M_read() {
383 _M_ok = (_M_stream && *_M_stream) ? true : false;
384 if (_M_ok) {
385 *_M_stream >> _M_value;
386 _M_ok = *_M_stream ? true : false;
387 }
388 }
389 };
390
391 template <class _Tp, class _CharT, class _Traits, class _Dist>
392 inline bool
393 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
394 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
395 return __x._M_equal(__y);
396 }
397
398 template <class _Tp, class _CharT, class _Traits, class _Dist>
399 inline bool
400 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
401 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
402 return !__x._M_equal(__y);
403 }
404
405
406 template <class _Tp,
407 class _CharT = char, class _Traits = char_traits<_CharT> >
408 class ostream_iterator {
409 public:
410 typedef _CharT char_type;
411 typedef _Traits traits_type;
412 typedef basic_ostream<_CharT, _Traits> ostream_type;
413
414 typedef output_iterator_tag iterator_category;
415 typedef void value_type;
416 typedef void difference_type;
417 typedef void pointer;
418 typedef void reference;
419
420 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
421 ostream_iterator(ostream_type& __s, const _CharT* __c)
422 : _M_stream(&__s), _M_string(__c) {}
423 ostream_iterator<_Tp>& operator=(const _Tp& __value) {
424 *_M_stream << __value;
425 if (_M_string) *_M_stream << _M_string;
426 return *this;
427 }
428 ostream_iterator<_Tp>& operator*() { return *this; }
429 ostream_iterator<_Tp>& operator++() { return *this; }
430 ostream_iterator<_Tp>& operator++(int) { return *this; }
431 private:
432 ostream_type* _M_stream;
433 const _CharT* _M_string;
434 };
435
436
437 // This iterator adapter is 'normal' in the sense that it does not
438 // change the semantics of any of the operators of its itererator
439 // parameter. Its primary purpose is to convert an iterator that is
440 // not a class, e.g. a pointer, into an iterator that is a class.
441 // The _Container parameter exists solely so that different containers
442 // using this template can instantiate different types, even if the
443 // _Iterator parameter is the same.
444 template<typename _Iterator, typename _Container>
445 class __normal_iterator
446 : public iterator<iterator_traits<_Iterator>::iterator_category,
447 iterator_traits<_Iterator>::value_type,
448 iterator_traits<_Iterator>::difference_type,
449 iterator_traits<_Iterator>::pointer,
450 iterator_traits<_Iterator>::reference>
451 {
452
453 protected:
454 _Iterator _M_current;
455
456 public:
457 typedef __normal_iterator<_Iterator, _Container> normal_iterator_type;
458
459 __normal_iterator() : _M_current(_Iterator()) { }
460
461 explicit __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
462
463 // Allow iterator to const_iterator conversion
464 template<typename _Iter>
465 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
466 : _M_current(__i.base()) { }
467
468 // Forward iterator requirements
469 reference
470 operator*() const { return *_M_current; }
471
472 pointer
473 operator->() const { return _M_current; }
474
475 normal_iterator_type&
476 operator++() { ++_M_current; return *this; }
477
478 normal_iterator_type
479 operator++(int) { return __normal_iterator(_M_current++); }
480
481 // Bidirectional iterator requirements
482 normal_iterator_type&
483 operator--() { --_M_current; return *this; }
484
485 normal_iterator_type
486 operator--(int) { return __normal_iterator(_M_current--); }
487
488 // Random access iterator requirements
489 reference
490 operator[](const difference_type& __n) const
491 { return _M_current[__n]; }
492
493 normal_iterator_type&
494 operator+=(const difference_type& __n)
495 { _M_current += __n; return *this; }
496
497 normal_iterator_type
498 operator+(const difference_type& __n) const
499 { return __normal_iterator(_M_current + __n); }
500
501 normal_iterator_type&
502 operator-=(const difference_type& __n)
503 { _M_current -= __n; return *this; }
504
505 normal_iterator_type
506 operator-(const difference_type& __n) const
507 { return __normal_iterator(_M_current - __n); }
508
509 difference_type
510 operator-(const normal_iterator_type& __i) const
511 { return _M_current - __i._M_current; }
512
513 const _Iterator&
514 base() const { return _M_current; }
515 };
516
517 // forward iterator requirements
518
519 template<typename _IteratorL, typename _IteratorR, typename _Container>
520 inline bool
521 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
522 const __normal_iterator<_IteratorR, _Container>& __rhs)
523 { return __lhs.base() == __rhs.base(); }
524
525 template<typename _IteratorL, typename _IteratorR, typename _Container>
526 inline bool
527 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
528 const __normal_iterator<_IteratorR, _Container>& __rhs)
529 { return !(__lhs == __rhs); }
530
531 // random access iterator requirements
532
533 template<typename _IteratorL, typename _IteratorR, typename _Container>
534 inline bool
535 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
536 const __normal_iterator<_IteratorR, _Container>& __rhs)
537 { return __lhs.base() < __rhs.base(); }
538
539 template<typename _IteratorL, typename _IteratorR, typename _Container>
540 inline bool
541 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
542 const __normal_iterator<_IteratorR, _Container>& __rhs)
543 { return __rhs < __lhs; }
544
545 template<typename _IteratorL, typename _IteratorR, typename _Container>
546 inline bool
547 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
548 const __normal_iterator<_IteratorR, _Container>& __rhs)
549 { return !(__rhs < __lhs); }
550
551 template<typename _IteratorL, typename _IteratorR, typename _Container>
552 inline bool
553 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
554 const __normal_iterator<_IteratorR, _Container>& __rhs)
555 { return !(__lhs < __rhs); }
556
557 template<typename _Iterator, typename _Container>
558 inline __normal_iterator<_Iterator, _Container>
559 operator+(__normal_iterator<_Iterator, _Container>::difference_type __n,
560 const __normal_iterator<_Iterator, _Container>& __i)
561 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
562
563 } // namespace std
564
565 #endif /* __SGI_STL_INTERNAL_ITERATOR_H */
566
567 // Local Variables:
568 // mode:C++
569 // End: