OSDN Git Service

* include/bits/stl_iterator.h (__normal_iterator<>): Qualify
[pf3gnuchains/gcc-fork.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<typename iterator_traits<_Iterator>::iterator_category,
447                     typename iterator_traits<_Iterator>::value_type,
448                     typename iterator_traits<_Iterator>::difference_type,
449                     typename iterator_traits<_Iterator>::pointer,
450                     typename iterator_traits<_Iterator>::reference>
451 {
452   typedef iterator_traits<_Iterator> _Traits;
453   
454 protected:
455   _Iterator _M_current;
456
457 public:
458   typedef __normal_iterator<_Iterator, _Container> normal_iterator_type;
459   typedef typename _Traits::iterator_category iterator_category;
460   typedef typename _Traits::value_type value_type;
461   typedef typename _Traits::difference_type difference_type;
462   typedef typename _Traits::pointer pointer;
463   typedef typename _Traits::reference reference;
464
465   __normal_iterator() : _M_current(_Iterator()) { }
466
467   explicit __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
468
469   // Allow iterator to const_iterator conversion
470   template<typename _Iter>
471   inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
472     : _M_current(__i.base()) { }
473
474   // Forward iterator requirements
475   reference
476   operator*() const { return *_M_current; }
477
478   pointer
479   operator->() const { return _M_current; }
480
481   normal_iterator_type&
482   operator++() { ++_M_current; return *this; }
483
484   normal_iterator_type
485   operator++(int) { return __normal_iterator(_M_current++); }
486
487   // Bidirectional iterator requirements
488   normal_iterator_type&
489   operator--() { --_M_current; return *this; }
490
491   normal_iterator_type
492   operator--(int) { return __normal_iterator(_M_current--); }
493
494   // Random access iterator requirements
495   reference
496   operator[](const difference_type& __n) const
497   { return _M_current[__n]; }
498
499   normal_iterator_type&
500   operator+=(const difference_type& __n)
501   { _M_current += __n; return *this; }
502
503   normal_iterator_type
504   operator+(const difference_type& __n) const
505   { return __normal_iterator(_M_current + __n); }
506
507   normal_iterator_type&
508   operator-=(const difference_type& __n)
509   { _M_current -= __n; return *this; }
510
511   normal_iterator_type
512   operator-(const difference_type& __n) const
513   { return __normal_iterator(_M_current - __n); }
514
515   difference_type
516   operator-(const normal_iterator_type& __i) const
517   { return _M_current - __i._M_current; }
518
519   const _Iterator& 
520   base() const { return _M_current; }
521 };
522
523 // forward iterator requirements
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.base() == __rhs.base(); }
530
531 template<typename _IteratorL, typename _IteratorR, typename _Container>
532 inline bool
533 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
534            const __normal_iterator<_IteratorR, _Container>& __rhs)
535 { return !(__lhs == __rhs); }
536
537 // random access iterator requirements
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 __lhs.base() < __rhs.base(); }
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 !(__rhs < __lhs); }
556
557 template<typename _IteratorL, typename _IteratorR, typename _Container>
558 inline bool
559 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
560            const __normal_iterator<_IteratorR, _Container>& __rhs)
561 { return !(__lhs < __rhs); }
562
563 template<typename _Iterator, typename _Container>
564 inline __normal_iterator<_Iterator, _Container>
565 operator+(__normal_iterator<_Iterator, _Container>::difference_type __n,
566           const __normal_iterator<_Iterator, _Container>& __i)
567 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
568
569 } // namespace std
570
571 #endif /* __SGI_STL_INTERNAL_ITERATOR_H */
572
573 // Local Variables:
574 // mode:C++
575 // End: