Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

stl_iterator.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1996-1998
00016  * Silicon Graphics Computer Systems, Inc.
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Silicon Graphics makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  */
00026 
00027 /* NOTE: This is an internal header file, included by other STL headers.
00028  *   You should not attempt to use it directly.
00029  */
00030 
00031 #ifndef __SGI_STL_INTERNAL_ITERATOR_H
00032 #define __SGI_STL_INTERNAL_ITERATOR_H
00033 
00034 namespace std
00035 {
00036 
00037 template <class _Container>
00038 class back_insert_iterator {
00039 protected:
00040   _Container* container;
00041 public:
00042   typedef _Container          container_type;
00043   typedef output_iterator_tag iterator_category;
00044   typedef void                value_type;
00045   typedef void                difference_type;
00046   typedef void                pointer;
00047   typedef void                reference;
00048 
00049   explicit back_insert_iterator(_Container& __x) : container(&__x) {}
00050   back_insert_iterator<_Container>&
00051   operator=(const typename _Container::value_type& __value) { 
00052     container->push_back(__value);
00053     return *this;
00054   }
00055   back_insert_iterator<_Container>& operator*() { return *this; }
00056   back_insert_iterator<_Container>& operator++() { return *this; }
00057   back_insert_iterator<_Container>& operator++(int) { return *this; }
00058 };
00059 
00060 template <class _Container>
00061 inline back_insert_iterator<_Container> back_inserter(_Container& __x) {
00062   return back_insert_iterator<_Container>(__x);
00063 }
00064 
00065 template <class _Container>
00066 class front_insert_iterator {
00067 protected:
00068   _Container* container;
00069 public:
00070   typedef _Container          container_type;
00071   typedef output_iterator_tag iterator_category;
00072   typedef void                value_type;
00073   typedef void                difference_type;
00074   typedef void                pointer;
00075   typedef void                reference;
00076 
00077   explicit front_insert_iterator(_Container& __x) : container(&__x) {}
00078   front_insert_iterator<_Container>&
00079   operator=(const typename _Container::value_type& __value) { 
00080     container->push_front(__value);
00081     return *this;
00082   }
00083   front_insert_iterator<_Container>& operator*() { return *this; }
00084   front_insert_iterator<_Container>& operator++() { return *this; }
00085   front_insert_iterator<_Container>& operator++(int) { return *this; }
00086 };
00087 
00088 template <class _Container>
00089 inline front_insert_iterator<_Container> front_inserter(_Container& __x) {
00090   return front_insert_iterator<_Container>(__x);
00091 }
00092 
00093 template <class _Container>
00094 class insert_iterator {
00095 protected:
00096   _Container* container;
00097   typename _Container::iterator iter;
00098 public:
00099   typedef _Container          container_type;
00100   typedef output_iterator_tag iterator_category;
00101   typedef void                value_type;
00102   typedef void                difference_type;
00103   typedef void                pointer;
00104   typedef void                reference;
00105 
00106   insert_iterator(_Container& __x, typename _Container::iterator __i) 
00107     : container(&__x), iter(__i) {}
00108   insert_iterator<_Container>&
00109   operator=(const typename _Container::value_type& __value) { 
00110     iter = container->insert(iter, __value);
00111     ++iter;
00112     return *this;
00113   }
00114   insert_iterator<_Container>& operator*() { return *this; }
00115   insert_iterator<_Container>& operator++() { return *this; }
00116   insert_iterator<_Container>& operator++(int) { return *this; }
00117 };
00118 
00119 template <class _Container, class _Iterator>
00120 inline 
00121 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
00122 {
00123   typedef typename _Container::iterator __iter;
00124   return insert_iterator<_Container>(__x, __iter(__i));
00125 }
00126 
00127 template <class _BidirectionalIterator, class _Tp, class _Reference = _Tp&, 
00128           class _Distance = ptrdiff_t> 
00129 class reverse_bidirectional_iterator {
00130   typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp, 
00131                                          _Reference, _Distance>  _Self;
00132 protected:
00133   _BidirectionalIterator current;
00134 public:
00135   typedef bidirectional_iterator_tag iterator_category;
00136   typedef _Tp                        value_type;
00137   typedef _Distance                  difference_type;
00138   typedef _Tp*                       pointer;
00139   typedef _Reference                 reference;
00140 
00141   reverse_bidirectional_iterator() {}
00142   explicit reverse_bidirectional_iterator(_BidirectionalIterator __x)
00143     : current(__x) {}
00144   _BidirectionalIterator base() const { return current; }
00145   _Reference operator*() const {
00146     _BidirectionalIterator __tmp = current;
00147     return *--__tmp;
00148   }
00149   pointer operator->() const { return &(operator*()); }
00150   _Self& operator++() {
00151     --current;
00152     return *this;
00153   }
00154   _Self operator++(int) {
00155     _Self __tmp = *this;
00156     --current;
00157     return __tmp;
00158   }
00159   _Self& operator--() {
00160     ++current;
00161     return *this;
00162   }
00163   _Self operator--(int) {
00164     _Self __tmp = *this;
00165     ++current;
00166     return __tmp;
00167   }
00168 };
00169 
00170 template <class _BiIter, class _Tp, class _Ref, class _Distance>
00171 inline bool operator==(
00172     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x, 
00173     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
00174 {
00175   return __x.base() == __y.base();
00176 }
00177 
00178 template <class _BiIter, class _Tp, class _Ref, class _Distance>
00179 inline bool operator!=(
00180     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x, 
00181     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
00182 {
00183   return !(__x == __y);
00184 }
00185 
00186 
00187 // This is the new version of reverse_iterator, as defined in the
00188 //  draft C++ standard.  It relies on the iterator_traits template,
00189 //  which in turn relies on partial specialization.  The class
00190 //  reverse_bidirectional_iterator is no longer part of the draft
00191 //  standard, but it is retained for backward compatibility.
00192 
00193 template <class _Iterator>
00194 class reverse_iterator 
00195 {
00196 protected:
00197   _Iterator current;
00198 public:
00199   typedef typename iterator_traits<_Iterator>::iterator_category
00200           iterator_category;
00201   typedef typename iterator_traits<_Iterator>::value_type
00202           value_type;
00203   typedef typename iterator_traits<_Iterator>::difference_type
00204           difference_type;
00205   typedef typename iterator_traits<_Iterator>::pointer
00206           pointer;
00207   typedef typename iterator_traits<_Iterator>::reference
00208           reference;
00209 
00210   typedef _Iterator iterator_type;
00211   typedef reverse_iterator<_Iterator> _Self;
00212 
00213 public:
00214   reverse_iterator() {}
00215   explicit reverse_iterator(iterator_type __x) : current(__x) {}
00216 
00217   reverse_iterator(const _Self& __x) : current(__x.current) {}
00218   template <class _Iter>
00219   reverse_iterator(const reverse_iterator<_Iter>& __x)
00220     : current(__x.base()) {}
00221     
00222   iterator_type base() const { return current; }
00223   reference operator*() const {
00224     _Iterator __tmp = current;
00225     return *--__tmp;
00226   }
00227   pointer operator->() const { return &(operator*()); }
00228 
00229   _Self& operator++() {
00230     --current;
00231     return *this;
00232   }
00233   _Self operator++(int) {
00234     _Self __tmp = *this;
00235     --current;
00236     return __tmp;
00237   }
00238   _Self& operator--() {
00239     ++current;
00240     return *this;
00241   }
00242   _Self operator--(int) {
00243     _Self __tmp = *this;
00244     ++current;
00245     return __tmp;
00246   }
00247 
00248   _Self operator+(difference_type __n) const {
00249     return _Self(current - __n);
00250   }
00251   _Self& operator+=(difference_type __n) {
00252     current -= __n;
00253     return *this;
00254   }
00255   _Self operator-(difference_type __n) const {
00256     return _Self(current + __n);
00257   }
00258   _Self& operator-=(difference_type __n) {
00259     current += __n;
00260     return *this;
00261   }
00262   reference operator[](difference_type __n) const { return *(*this + __n); }  
00263 }; 
00264  
00265 template <class _Iterator>
00266 inline bool operator==(const reverse_iterator<_Iterator>& __x, 
00267                        const reverse_iterator<_Iterator>& __y) {
00268   return __x.base() == __y.base();
00269 }
00270 
00271 template <class _Iterator>
00272 inline bool operator<(const reverse_iterator<_Iterator>& __x, 
00273                       const reverse_iterator<_Iterator>& __y) {
00274   return __y.base() < __x.base();
00275 }
00276 
00277 template <class _Iterator>
00278 inline bool operator!=(const reverse_iterator<_Iterator>& __x, 
00279                        const reverse_iterator<_Iterator>& __y) {
00280   return !(__x == __y);
00281 }
00282 
00283 template <class _Iterator>
00284 inline bool operator>(const reverse_iterator<_Iterator>& __x, 
00285                       const reverse_iterator<_Iterator>& __y) {
00286   return __y < __x;
00287 }
00288 
00289 template <class _Iterator>
00290 inline bool operator<=(const reverse_iterator<_Iterator>& __x, 
00291                        const reverse_iterator<_Iterator>& __y) {
00292   return !(__y < __x);
00293 }
00294 
00295 template <class _Iterator>
00296 inline bool operator>=(const reverse_iterator<_Iterator>& __x, 
00297                       const reverse_iterator<_Iterator>& __y) {
00298   return !(__x < __y);
00299 }
00300 
00301 template <class _Iterator>
00302 inline typename reverse_iterator<_Iterator>::difference_type
00303 operator-(const reverse_iterator<_Iterator>& __x, 
00304           const reverse_iterator<_Iterator>& __y) {
00305   return __y.base() - __x.base();
00306 }
00307 
00308 template <class _Iterator>
00309 inline reverse_iterator<_Iterator> 
00310 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
00311           const reverse_iterator<_Iterator>& __x) {
00312   return reverse_iterator<_Iterator>(__x.base() - __n);
00313 }
00314 
00315 
00316 template <class _Tp, 
00317           class _CharT = char, class _Traits = char_traits<_CharT>,
00318           class _Dist = ptrdiff_t> 
00319 class istream_iterator {
00320 public:
00321   typedef _CharT                         char_type;
00322   typedef _Traits                        traits_type;
00323   typedef basic_istream<_CharT, _Traits> istream_type;
00324 
00325   typedef input_iterator_tag             iterator_category;
00326   typedef _Tp                            value_type;
00327   typedef _Dist                          difference_type;
00328   typedef const _Tp*                     pointer;
00329   typedef const _Tp&                     reference;
00330 
00331   istream_iterator() : _M_stream(0), _M_ok(false) {}
00332   istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
00333 
00334   reference operator*() const { return _M_value; }
00335   pointer operator->() const { return &(operator*()); }
00336 
00337   istream_iterator& operator++() { 
00338     _M_read(); 
00339     return *this;
00340   }
00341   istream_iterator operator++(int)  {
00342     istream_iterator __tmp = *this;
00343     _M_read();
00344     return __tmp;
00345   }
00346 
00347   bool _M_equal(const istream_iterator& __x) const
00348     { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00349 
00350 private:
00351   istream_type* _M_stream;
00352   _Tp _M_value;
00353   bool _M_ok;
00354 
00355   void _M_read() {
00356     _M_ok = (_M_stream && *_M_stream) ? true : false;
00357     if (_M_ok) {
00358       *_M_stream >> _M_value;
00359       _M_ok = *_M_stream ? true : false;
00360     }
00361   }
00362 };
00363 
00364 template <class _Tp, class _CharT, class _Traits, class _Dist>
00365 inline bool 
00366 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00367            const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
00368   return __x._M_equal(__y);
00369 }
00370 
00371 template <class _Tp, class _CharT, class _Traits, class _Dist>
00372 inline bool 
00373 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00374            const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
00375   return !__x._M_equal(__y);
00376 }
00377 
00378 
00379 template <class _Tp,
00380           class _CharT = char, class _Traits = char_traits<_CharT> >
00381 class ostream_iterator {
00382 public:
00383   typedef _CharT                         char_type;
00384   typedef _Traits                        traits_type;
00385   typedef basic_ostream<_CharT, _Traits> ostream_type;
00386 
00387   typedef output_iterator_tag            iterator_category;
00388   typedef void                           value_type;
00389   typedef void                           difference_type;
00390   typedef void                           pointer;
00391   typedef void                           reference;
00392 
00393   ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
00394   ostream_iterator(ostream_type& __s, const _CharT* __c) 
00395     : _M_stream(&__s), _M_string(__c)  {}
00396   ostream_iterator<_Tp>& operator=(const _Tp& __value) { 
00397     *_M_stream << __value;
00398     if (_M_string) *_M_stream << _M_string;
00399     return *this;
00400   }
00401   ostream_iterator<_Tp>& operator*() { return *this; }
00402   ostream_iterator<_Tp>& operator++() { return *this; } 
00403   ostream_iterator<_Tp>& operator++(int) { return *this; } 
00404 private:
00405   ostream_type* _M_stream;
00406   const _CharT* _M_string;
00407 };
00408 
00409 
00410 // This iterator adapter is 'normal' in the sense that it does not
00411 // change the semantics of any of the operators of its itererator
00412 // parameter.  Its primary purpose is to convert an iterator that is
00413 // not a class, e.g. a pointer, into an iterator that is a class.
00414 // The _Container parameter exists solely so that different containers
00415 // using this template can instantiate different types, even if the
00416 // _Iterator parameter is the same.
00417 template<typename _Iterator, typename _Container>
00418 class __normal_iterator
00419   : public iterator<iterator_traits<_Iterator>::iterator_category,
00420                     iterator_traits<_Iterator>::value_type,
00421                     iterator_traits<_Iterator>::difference_type,
00422                     iterator_traits<_Iterator>::pointer,
00423                     iterator_traits<_Iterator>::reference>
00424 {
00425 
00426 protected:
00427   _Iterator _M_current;
00428 
00429 public:
00430   typedef __normal_iterator<_Iterator, _Container> normal_iterator_type;
00431   typedef iterator_traits<_Iterator>            __traits_type;
00432   typedef typename __traits_type::iterator_category     iterator_category;
00433   typedef typename __traits_type::value_type        value_type;
00434   typedef typename __traits_type::difference_type   difference_type;
00435   typedef typename __traits_type::pointer           pointer;
00436   typedef typename __traits_type::reference         reference;
00437 
00438   __normal_iterator() : _M_current(_Iterator()) { }
00439 
00440   explicit __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
00441 
00442   // Allow iterator to const_iterator conversion
00443   template<typename _Iter>
00444   inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
00445     : _M_current(__i.base()) { }
00446 
00447   // Forward iterator requirements
00448   reference
00449   operator*() const { return *_M_current; }
00450 
00451   pointer
00452   operator->() const { return _M_current; }
00453 
00454   normal_iterator_type&
00455   operator++() { ++_M_current; return *this; }
00456 
00457   normal_iterator_type
00458   operator++(int) { return __normal_iterator(_M_current++); }
00459 
00460   // Bidirectional iterator requirements
00461   normal_iterator_type&
00462   operator--() { --_M_current; return *this; }
00463 
00464   normal_iterator_type
00465   operator--(int) { return __normal_iterator(_M_current--); }
00466 
00467   // Random access iterator requirements
00468   reference
00469   operator[](const difference_type& __n) const
00470   { return _M_current[__n]; }
00471 
00472   normal_iterator_type&
00473   operator+=(const difference_type& __n)
00474   { _M_current += __n; return *this; }
00475 
00476   normal_iterator_type
00477   operator+(const difference_type& __n) const
00478   { return __normal_iterator(_M_current + __n); }
00479 
00480   normal_iterator_type&
00481   operator-=(const difference_type& __n)
00482   { _M_current -= __n; return *this; }
00483 
00484   normal_iterator_type
00485   operator-(const difference_type& __n) const
00486   { return __normal_iterator(_M_current - __n); }
00487 
00488   difference_type
00489   operator-(const normal_iterator_type& __i) const
00490   { return _M_current - __i._M_current; }
00491 
00492   const _Iterator& 
00493   base() const { return _M_current; }
00494 };
00495 
00496 // forward iterator requirements
00497 
00498 template<typename _IteratorL, typename _IteratorR, typename _Container>
00499 inline bool
00500 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
00501        const __normal_iterator<_IteratorR, _Container>& __rhs)
00502 { return __lhs.base() == __rhs.base(); }
00503 
00504 template<typename _IteratorL, typename _IteratorR, typename _Container>
00505 inline bool
00506 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
00507        const __normal_iterator<_IteratorR, _Container>& __rhs)
00508 { return !(__lhs == __rhs); }
00509 
00510 // random access iterator requirements
00511 
00512 template<typename _IteratorL, typename _IteratorR, typename _Container>
00513 inline bool 
00514 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
00515       const __normal_iterator<_IteratorR, _Container>& __rhs)
00516 { return __lhs.base() < __rhs.base(); }
00517 
00518 template<typename _IteratorL, typename _IteratorR, typename _Container>
00519 inline bool
00520 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
00521       const __normal_iterator<_IteratorR, _Container>& __rhs)
00522 { return __rhs < __lhs; }
00523 
00524 template<typename _IteratorL, typename _IteratorR, typename _Container>
00525 inline bool
00526 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
00527        const __normal_iterator<_IteratorR, _Container>& __rhs)
00528 { return !(__rhs < __lhs); }
00529 
00530 template<typename _IteratorL, typename _IteratorR, typename _Container>
00531 inline bool
00532 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
00533        const __normal_iterator<_IteratorR, _Container>& __rhs)
00534 { return !(__lhs < __rhs); }
00535 
00536 template<typename _Iterator, typename _Container>
00537 inline __normal_iterator<_Iterator, _Container>
00538 operator+(__normal_iterator<_Iterator, _Container>::difference_type __n,
00539           const __normal_iterator<_Iterator, _Container>& __i)
00540 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
00541 
00542 } // namespace std
00543 
00544 #endif /* __SGI_STL_INTERNAL_ITERATOR_H */
00545 
00546 // Local Variables:
00547 // mode:C++
00548 // End:

Generated at Tue May 1 16:28:39 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001