stl_iterator.h

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 __STL_BEGIN_NAMESPACE
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 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00061 
00062 template <class _Container>
00063 inline output_iterator_tag
00064 iterator_category(const back_insert_iterator<_Container>&)
00065 {
00066   return output_iterator_tag();
00067 }
00068 
00069 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00070 
00071 template <class _Container>
00072 inline back_insert_iterator<_Container> back_inserter(_Container& __x) {
00073   return back_insert_iterator<_Container>(__x);
00074 }
00075 
00076 template <class _Container>
00077 class front_insert_iterator {
00078 protected:
00079   _Container* container;
00080 public:
00081   typedef _Container          container_type;
00082   typedef output_iterator_tag iterator_category;
00083   typedef void                value_type;
00084   typedef void                difference_type;
00085   typedef void                pointer;
00086   typedef void                reference;
00087 
00088   explicit front_insert_iterator(_Container& __x) : container(&__x) {}
00089   front_insert_iterator<_Container>&
00090   operator=(const typename _Container::value_type& __value) { 
00091     container->push_front(__value);
00092     return *this;
00093   }
00094   front_insert_iterator<_Container>& operator*() { return *this; }
00095   front_insert_iterator<_Container>& operator++() { return *this; }
00096   front_insert_iterator<_Container>& operator++(int) { return *this; }
00097 };
00098 
00099 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00100 
00101 template <class _Container>
00102 inline output_iterator_tag
00103 iterator_category(const front_insert_iterator<_Container>&)
00104 {
00105   return output_iterator_tag();
00106 }
00107 
00108 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00109 
00110 template <class _Container>
00111 inline front_insert_iterator<_Container> front_inserter(_Container& __x) {
00112   return front_insert_iterator<_Container>(__x);
00113 }
00114 
00115 template <class _Container>
00116 class insert_iterator {
00117 protected:
00118   _Container* container;
00119   typename _Container::iterator iter;
00120 public:
00121   typedef _Container          container_type;
00122   typedef output_iterator_tag iterator_category;
00123   typedef void                value_type;
00124   typedef void                difference_type;
00125   typedef void                pointer;
00126   typedef void                reference;
00127 
00128   insert_iterator(_Container& __x, typename _Container::iterator __i) 
00129     : container(&__x), iter(__i) {}
00130   insert_iterator<_Container>&
00131   operator=(const typename _Container::value_type& __value) { 
00132     iter = container->insert(iter, __value);
00133     ++iter;
00134     return *this;
00135   }
00136   insert_iterator<_Container>& operator*() { return *this; }
00137   insert_iterator<_Container>& operator++() { return *this; }
00138   insert_iterator<_Container>& operator++(int) { return *this; }
00139 };
00140 
00141 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00142 
00143 template <class _Container>
00144 inline output_iterator_tag
00145 iterator_category(const insert_iterator<_Container>&)
00146 {
00147   return output_iterator_tag();
00148 }
00149 
00150 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00151 
00152 template <class _Container, class _Iterator>
00153 inline 
00154 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
00155 {
00156   typedef typename _Container::iterator __iter;
00157   return insert_iterator<_Container>(__x, __iter(__i));
00158 }
00159 
00160 #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
00161 template <class _BidirectionalIterator, class _Tp, class _Reference = _Tp&, 
00162           class _Distance = ptrdiff_t> 
00163 #else
00164 template <class _BidirectionalIterator, class _Tp, class _Reference, 
00165           class _Distance> 
00166 #endif
00167 class reverse_bidirectional_iterator {
00168   typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp, 
00169                                          _Reference, _Distance>  _Self;
00170 protected:
00171   _BidirectionalIterator current;
00172 public:
00173   typedef bidirectional_iterator_tag iterator_category;
00174   typedef _Tp                        value_type;
00175   typedef _Distance                  difference_type;
00176   typedef _Tp*                       pointer;
00177   typedef _Reference                 reference;
00178 
00179   reverse_bidirectional_iterator() {}
00180   explicit reverse_bidirectional_iterator(_BidirectionalIterator __x)
00181     : current(__x) {}
00182   _BidirectionalIterator base() const { return current; }
00183   _Reference operator*() const {
00184     _BidirectionalIterator __tmp = current;
00185     return *--__tmp;
00186   }
00187 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00188   pointer operator->() const { return &(operator*()); }
00189 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
00190   _Self& operator++() {
00191     --current;
00192     return *this;
00193   }
00194   _Self operator++(int) {
00195     _Self __tmp = *this;
00196     --current;
00197     return __tmp;
00198   }
00199   _Self& operator--() {
00200     ++current;
00201     return *this;
00202   }
00203   _Self operator--(int) {
00204     _Self __tmp = *this;
00205     ++current;
00206     return __tmp;
00207   }
00208 };
00209 
00210 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00211 
00212 template <class _BidirectionalIterator, class _Tp, class _Reference, 
00213           class _Distance>
00214 inline bidirectional_iterator_tag
00215 iterator_category(const reverse_bidirectional_iterator<_BidirectionalIterator,
00216                                                        _Tp, _Reference, 
00217                                                        _Distance>&) 
00218 {
00219   return bidirectional_iterator_tag();
00220 }
00221 
00222 template <class _BidirectionalIterator, class _Tp, class _Reference, 
00223           class _Distance>
00224 inline _Tp*
00225 value_type(const reverse_bidirectional_iterator<_BidirectionalIterator, _Tp,
00226                                                _Reference, _Distance>&)
00227 {
00228   return (_Tp*) 0;
00229 }
00230 
00231 template <class _BidirectionalIterator, class _Tp, class _Reference, 
00232           class _Distance>
00233 inline _Distance*
00234 distance_type(const reverse_bidirectional_iterator<_BidirectionalIterator, 
00235                                                    _Tp,
00236                                                    _Reference, _Distance>&)
00237 {
00238   return (_Distance*) 0;
00239 }
00240 
00241 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00242 
00243 template <class _BiIter, class _Tp, class _Ref, class _Distance>
00244 inline bool operator==(
00245     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x, 
00246     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
00247 {
00248   return __x.base() == __y.base();
00249 }
00250 
00251 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00252 
00253 template <class _BiIter, class _Tp, class _Ref, class _Distance>
00254 inline bool operator!=(
00255     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x, 
00256     const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
00257 {
00258   return !(__x == __y);
00259 }
00260 
00261 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00262 
00263 
00264 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
00265 
00266 // This is the new version of reverse_iterator, as defined in the
00267 //  draft C++ standard.  It relies on the iterator_traits template,
00268 //  which in turn relies on partial specialization.  The class
00269 //  reverse_bidirectional_iterator is no longer part of the draft
00270 //  standard, but it is retained for backward compatibility.
00271 
00272 template <class _Iterator>
00273 class reverse_iterator 
00274 {
00275 protected:
00276   _Iterator current;
00277 public:
00278   typedef typename iterator_traits<_Iterator>::iterator_category
00279           iterator_category;
00280   typedef typename iterator_traits<_Iterator>::value_type
00281           value_type;
00282   typedef typename iterator_traits<_Iterator>::difference_type
00283           difference_type;
00284   typedef typename iterator_traits<_Iterator>::pointer
00285           pointer;
00286   typedef typename iterator_traits<_Iterator>::reference
00287           reference;
00288 
00289   typedef _Iterator iterator_type;
00290   typedef reverse_iterator<_Iterator> _Self;
00291 
00292 public:
00293   reverse_iterator() {}
00294   explicit reverse_iterator(iterator_type __x) : current(__x) {}
00295 
00296   reverse_iterator(const _Self& __x) : current(__x.current) {}
00297 #ifdef __STL_MEMBER_TEMPLATES
00298   template <class _Iter>
00299   reverse_iterator(const reverse_iterator<_Iter>& __x)
00300     : current(__x.base()) {}
00301 #endif /* __STL_MEMBER_TEMPLATES */
00302     
00303   iterator_type base() const { return current; }
00304   reference operator*() const {
00305     _Iterator __tmp = current;
00306     return *--__tmp;
00307   }
00308 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00309   pointer operator->() const { return &(operator*()); }
00310 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
00311 
00312   _Self& operator++() {
00313     --current;
00314     return *this;
00315   }
00316   _Self operator++(int) {
00317     _Self __tmp = *this;
00318     --current;
00319     return __tmp;
00320   }
00321   _Self& operator--() {
00322     ++current;
00323     return *this;
00324   }
00325   _Self operator--(int) {
00326     _Self __tmp = *this;
00327     ++current;
00328     return __tmp;
00329   }
00330 
00331   _Self operator+(difference_type __n) const {
00332     return _Self(current - __n);
00333   }
00334   _Self& operator+=(difference_type __n) {
00335     current -= __n;
00336     return *this;
00337   }
00338   _Self operator-(difference_type __n) const {
00339     return _Self(current + __n);
00340   }
00341   _Self& operator-=(difference_type __n) {
00342     current += __n;
00343     return *this;
00344   }
00345   reference operator[](difference_type __n) const { return *(*this + __n); }  
00346 }; 
00347  
00348 template <class _Iterator>
00349 inline bool operator==(const reverse_iterator<_Iterator>& __x, 
00350                        const reverse_iterator<_Iterator>& __y) {
00351   return __x.base() == __y.base();
00352 }
00353 
00354 template <class _Iterator>
00355 inline bool operator<(const reverse_iterator<_Iterator>& __x, 
00356                       const reverse_iterator<_Iterator>& __y) {
00357   return __y.base() < __x.base();
00358 }
00359 
00360 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00361 
00362 template <class _Iterator>
00363 inline bool operator!=(const reverse_iterator<_Iterator>& __x, 
00364                        const reverse_iterator<_Iterator>& __y) {
00365   return !(__x == __y);
00366 }
00367 
00368 template <class _Iterator>
00369 inline bool operator>(const reverse_iterator<_Iterator>& __x, 
00370                       const reverse_iterator<_Iterator>& __y) {
00371   return __y < __x;
00372 }
00373 
00374 template <class _Iterator>
00375 inline bool operator<=(const reverse_iterator<_Iterator>& __x, 
00376                        const reverse_iterator<_Iterator>& __y) {
00377   return !(__y < __x);
00378 }
00379 
00380 template <class _Iterator>
00381 inline bool operator>=(const reverse_iterator<_Iterator>& __x, 
00382                       const reverse_iterator<_Iterator>& __y) {
00383   return !(__x < __y);
00384 }
00385 
00386 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00387 
00388 template <class _Iterator>
00389 inline typename reverse_iterator<_Iterator>::difference_type
00390 operator-(const reverse_iterator<_Iterator>& __x, 
00391           const reverse_iterator<_Iterator>& __y) {
00392   return __y.base() - __x.base();
00393 }
00394 
00395 template <class _Iterator>
00396 inline reverse_iterator<_Iterator> 
00397 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
00398           const reverse_iterator<_Iterator>& __x) {
00399   return reverse_iterator<_Iterator>(__x.base() - __n);
00400 }
00401 
00402 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00403 
00404 // This is the old version of reverse_iterator, as found in the original
00405 //  HP STL.  It does not use partial specialization.
00406 
00407 #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
00408 template <class _RandomAccessIterator, class _Tp, class _Reference = _Tp&,
00409           class _Distance = ptrdiff_t> 
00410 #else
00411 template <class _RandomAccessIterator, class _Tp, class _Reference,
00412           class _Distance> 
00413 #endif
00414 class reverse_iterator {
00415   typedef reverse_iterator<_RandomAccessIterator, _Tp, _Reference, _Distance>
00416         _Self;
00417 protected:
00418   _RandomAccessIterator current;
00419 public:
00420   typedef random_access_iterator_tag iterator_category;
00421   typedef _Tp                        value_type;
00422   typedef _Distance                  difference_type;
00423   typedef _Tp*                       pointer;
00424   typedef _Reference                 reference;
00425 
00426   reverse_iterator() {}
00427   explicit reverse_iterator(_RandomAccessIterator __x) : current(__x) {}
00428   _RandomAccessIterator base() const { return current; }
00429   _Reference operator*() const { return *(current - 1); }
00430 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00431   pointer operator->() const { return &(operator*()); }
00432 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
00433   _Self& operator++() {
00434     --current;
00435     return *this;
00436   }
00437   _Self operator++(int) {
00438     _Self __tmp = *this;
00439     --current;
00440     return __tmp;
00441   }
00442   _Self& operator--() {
00443     ++current;
00444     return *this;
00445   }
00446   _Self operator--(int) {
00447     _Self __tmp = *this;
00448     ++current;
00449     return __tmp;
00450   }
00451   _Self operator+(_Distance __n) const {
00452     return _Self(current - __n);
00453   }
00454   _Self& operator+=(_Distance __n) {
00455     current -= __n;
00456     return *this;
00457   }
00458   _Self operator-(_Distance __n) const {
00459     return _Self(current + __n);
00460   }
00461   _Self& operator-=(_Distance __n) {
00462     current += __n;
00463     return *this;
00464   }
00465   _Reference operator[](_Distance __n) const { return *(*this + __n); }
00466 };
00467 
00468 template <class _RandomAccessIterator, class _Tp, 
00469           class _Reference, class _Distance>
00470 inline random_access_iterator_tag
00471 iterator_category(const reverse_iterator<_RandomAccessIterator, _Tp,
00472                                          _Reference, _Distance>&)
00473 {
00474   return random_access_iterator_tag();
00475 }
00476 
00477 template <class _RandomAccessIterator, class _Tp,
00478           class _Reference, class _Distance>
00479 inline _Tp* value_type(const reverse_iterator<_RandomAccessIterator, _Tp,
00480                                               _Reference, _Distance>&)
00481 {
00482   return (_Tp*) 0;
00483 }
00484 
00485 template <class _RandomAccessIterator, class _Tp,
00486           class _Reference, class _Distance>
00487 inline _Distance* 
00488 distance_type(const reverse_iterator<_RandomAccessIterator, 
00489                                      _Tp, _Reference, _Distance>&)
00490 {
00491   return (_Distance*) 0;
00492 }
00493 
00494 
00495 template <class _RandomAccessIterator, class _Tp,
00496           class _Reference, class _Distance>
00497 inline bool 
00498 operator==(const reverse_iterator<_RandomAccessIterator, _Tp,
00499                                   _Reference, _Distance>& __x, 
00500            const reverse_iterator<_RandomAccessIterator, _Tp,
00501                                   _Reference, _Distance>& __y)
00502 {
00503   return __x.base() == __y.base();
00504 }
00505 
00506 template <class _RandomAccessIterator, class _Tp,
00507           class _Reference, class _Distance>
00508 inline bool 
00509 operator<(const reverse_iterator<_RandomAccessIterator, _Tp,
00510                                  _Reference, _Distance>& __x, 
00511           const reverse_iterator<_RandomAccessIterator, _Tp,
00512                                  _Reference, _Distance>& __y)
00513 {
00514   return __y.base() < __x.base();
00515 }
00516 
00517 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00518 
00519 template <class _RandomAccessIterator, class _Tp,
00520           class _Reference, class _Distance>
00521 inline bool 
00522 operator!=(const reverse_iterator<_RandomAccessIterator, _Tp,
00523                                   _Reference, _Distance>& __x, 
00524            const reverse_iterator<_RandomAccessIterator, _Tp,
00525                                   _Reference, _Distance>& __y) {
00526   return !(__x == __y);
00527 }
00528 
00529 template <class _RandomAccessIterator, class _Tp,
00530           class _Reference, class _Distance>
00531 inline bool 
00532 operator>(const reverse_iterator<_RandomAccessIterator, _Tp,
00533                                  _Reference, _Distance>& __x, 
00534           const reverse_iterator<_RandomAccessIterator, _Tp,
00535                                  _Reference, _Distance>& __y) {
00536   return __y < __x;
00537 }
00538 
00539 template <class _RandomAccessIterator, class _Tp,
00540           class _Reference, class _Distance>
00541 inline bool 
00542 operator<=(const reverse_iterator<_RandomAccessIterator, _Tp,
00543                                   _Reference, _Distance>& __x, 
00544            const reverse_iterator<_RandomAccessIterator, _Tp,
00545                                   _Reference, _Distance>& __y) {
00546   return !(__y < __x);
00547 }
00548 
00549 template <class _RandomAccessIterator, class _Tp,
00550           class _Reference, class _Distance>
00551 inline bool 
00552 operator>=(const reverse_iterator<_RandomAccessIterator, _Tp,
00553                                   _Reference, _Distance>& __x, 
00554            const reverse_iterator<_RandomAccessIterator, _Tp,
00555                                   _Reference, _Distance>& __y) {
00556   return !(__x < __y);
00557 }
00558 
00559 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00560 
00561 template <class _RandomAccessIterator, class _Tp,
00562           class _Reference, class _Distance>
00563 inline _Distance 
00564 operator-(const reverse_iterator<_RandomAccessIterator, _Tp,
00565                                  _Reference, _Distance>& __x, 
00566           const reverse_iterator<_RandomAccessIterator, _Tp,
00567                                  _Reference, _Distance>& __y)
00568 {
00569   return __y.base() - __x.base();
00570 }
00571 
00572 template <class _RandAccIter, class _Tp, class _Ref, class _Dist>
00573 inline reverse_iterator<_RandAccIter, _Tp, _Ref, _Dist> 
00574 operator+(_Dist __n,
00575           const reverse_iterator<_RandAccIter, _Tp, _Ref, _Dist>& __x)
00576 {
00577   return reverse_iterator<_RandAccIter, _Tp, _Ref, _Dist>(__x.base() - __n);
00578 }
00579 
00580 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00581 
00582 #ifndef UNDER_CE
00583 
00584 // istream_iterator and ostream_iterator look very different if we're
00585 // using new, templatized iostreams than if we're using the old cfront
00586 // version.
00587 
00588 #ifdef __STL_USE_NEW_IOSTREAMS
00589 
00590 template <class _Tp, 
00591           class _CharT = char, class _Traits = char_traits<_CharT>,
00592           class _Dist = ptrdiff_t> 
00593 class istream_iterator {
00594 public:
00595   typedef _CharT                         char_type;
00596   typedef _Traits                        traits_type;
00597   typedef basic_istream<_CharT, _Traits> istream_type;
00598 
00599   typedef input_iterator_tag             iterator_category;
00600   typedef _Tp                            value_type;
00601   typedef _Dist                          difference_type;
00602   typedef const _Tp*                     pointer;
00603   typedef const _Tp&                     reference;
00604 
00605   istream_iterator() : _M_stream(0), _M_ok(false) {}
00606   istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
00607 
00608   reference operator*() const { return _M_value; }
00609   pointer operator->() const { return &(operator*()); }
00610 
00611   istream_iterator& operator++() { 
00612     _M_read(); 
00613     return *this;
00614   }
00615   istream_iterator operator++(int)  {
00616     istream_iterator __tmp = *this;
00617     _M_read();
00618     return __tmp;
00619   }
00620 
00621   bool _M_equal(const istream_iterator& __x) const
00622     { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00623 
00624 private:
00625   istream_type* _M_stream;
00626   _Tp _M_value;
00627   bool _M_ok;
00628 
00629   void _M_read() {
00630     _M_ok = (_M_stream && *_M_stream) ? true : false;
00631     if (_M_ok) {
00632       *_M_stream >> _M_value;
00633       _M_ok = *_M_stream ? true : false;
00634     }
00635   }
00636 };
00637 
00638 template <class _Tp, class _CharT, class _Traits, class _Dist>
00639 inline bool 
00640 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00641            const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
00642   return __x._M_equal(__y);
00643 }
00644 
00645 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00646 
00647 template <class _Tp, class _CharT, class _Traits, class _Dist>
00648 inline bool 
00649 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00650            const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
00651   return !__x._M_equal(__y);
00652 }
00653 
00654 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00655 
00656 template <class _Tp,
00657           class _CharT = char, class _Traits = char_traits<_CharT> >
00658 class ostream_iterator {
00659 public:
00660   typedef _CharT                         char_type;
00661   typedef _Traits                        traits_type;
00662   typedef basic_ostream<_CharT, _Traits> ostream_type;
00663 
00664   typedef output_iterator_tag            iterator_category;
00665   typedef void                           value_type;
00666   typedef void                           difference_type;
00667   typedef void                           pointer;
00668   typedef void                           reference;
00669 
00670   ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
00671   ostream_iterator(ostream_type& __s, const _CharT* __c) 
00672     : _M_stream(&__s), _M_string(__c)  {}
00673   ostream_iterator<_Tp>& operator=(const _Tp& __value) { 
00674     *_M_stream << __value;
00675     if (_M_string) *_M_stream << _M_string;
00676     return *this;
00677   }
00678   ostream_iterator<_Tp>& operator*() { return *this; }
00679   ostream_iterator<_Tp>& operator++() { return *this; } 
00680   ostream_iterator<_Tp>& operator++(int) { return *this; } 
00681 private:
00682   ostream_type* _M_stream;
00683   const _CharT* _M_string;
00684 };
00685 
00686 // The default template argument is declared in iosfwd
00687 
00688 // We do not read any characters until operator* is called.  The first
00689 // time operator* is called, it calls getc.  Subsequent calls to getc 
00690 // return a cached character, and calls to operator++ use snextc.  Before
00691 // operator* or operator++ has been called, _M_is_initialized is false.
00692 template<class _CharT, class _Traits>
00693 class istreambuf_iterator
00694   : public iterator<input_iterator_tag, _CharT,
00695                     typename _Traits::off_type, _CharT*, _CharT&>
00696 {
00697 public:
00698   typedef _CharT                           char_type;
00699   typedef _Traits                          traits_type;
00700   typedef typename _Traits::int_type       int_type;
00701   typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00702   typedef basic_istream<_CharT, _Traits>   istream_type;
00703 
00704 public:
00705   istreambuf_iterator(streambuf_type* __p = 0) { this->_M_init(__p); }
00706   istreambuf_iterator(istream_type& __is) { this->_M_init(__is.rdbuf()); }
00707 
00708   char_type operator*() const 
00709     { return _M_is_initialized ? _M_c : _M_dereference_aux(); }
00710 
00711   istreambuf_iterator& operator++() { this->_M_nextc(); return *this; }
00712   istreambuf_iterator  operator++(int) {
00713     if (!_M_is_initialized)
00714       _M_postincr_aux();
00715     istreambuf_iterator __tmp = *this;
00716     this->_M_nextc();
00717     return __tmp;
00718   }
00719 
00720   bool equal(const istreambuf_iterator& __i) const {
00721     return this->_M_is_initialized && __i._M_is_initialized
00722       ? this->_M_eof == __i._M_eof
00723       : this->_M_equal_aux(__i);
00724   }
00725 
00726 private:
00727   void _M_init(streambuf_type* __p) {
00728     _M_buf = __p;
00729     _M_eof = !__p;
00730     _M_is_initialized = _M_eof;
00731   }
00732 
00733   char_type _M_dereference_aux() const;
00734   bool _M_equal_aux(const istreambuf_iterator&) const;
00735   void _M_postincr_aux();
00736 
00737   void _M_nextc() {
00738     int_type __c = _M_buf->snextc();
00739     _M_c = traits_type::to_char_type(__c);    
00740     _M_eof = traits_type::eq_int_type(__c, traits_type::eof());
00741     _M_is_initialized = true;
00742   }
00743 
00744   void _M_getc() const {
00745     int_type __c = _M_buf->sgetc();
00746     _M_c = traits_type::to_char_type(__c);
00747     _M_eof = traits_type::eq_int_type(__c, traits_type::eof());
00748     _M_is_initialized = true;
00749   }
00750 
00751 private:
00752   streambuf_type* _M_buf;
00753   mutable _CharT _M_c;
00754   mutable bool _M_eof : 1;
00755   mutable bool _M_is_initialized : 1;
00756 };
00757 
00758 template<class _CharT, class _Traits>
00759 _CharT istreambuf_iterator<_CharT, _Traits>::_M_dereference_aux() const
00760 {
00761   this->_M_getc();
00762   return _M_c;
00763 }
00764 
00765 template<class _CharT, class _Traits>
00766 bool istreambuf_iterator<_CharT, _Traits>
00767   ::_M_equal_aux(const istreambuf_iterator& __i) const
00768 {
00769   if (!this->_M_is_initialized)
00770     this->_M_getc();
00771   if (!__i._M_is_initialized)
00772     __i._M_getc();
00773 
00774   return this->_M_eof == __i._M_eof;
00775 }
00776 
00777 template<class _CharT, class _Traits>
00778 void istreambuf_iterator<_CharT, _Traits>::_M_postincr_aux()
00779 {
00780   this->_M_getc();
00781 }
00782 
00783 template<class _CharT, class _Traits>
00784 inline bool operator==(const istreambuf_iterator<_CharT, _Traits>& __x,
00785                        const istreambuf_iterator<_CharT, _Traits>& __y) {
00786   return __x.equal(__y);
00787 }
00788 
00789 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00790 
00791 template<class _CharT, class _Traits>
00792 inline bool operator!=(const istreambuf_iterator<_CharT, _Traits>& __x,
00793                        const istreambuf_iterator<_CharT, _Traits>& __y) {
00794   return !__x.equal(__y);
00795 }
00796 
00797 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00798 
00799 // The default template argument is declared in iosfwd
00800 template<class _CharT, class _Traits>
00801 class ostreambuf_iterator
00802   : public iterator<output_iterator_tag, void, void, void, void>
00803 {
00804 public:
00805   typedef _CharT                           char_type;
00806   typedef _Traits                          traits_type;
00807   typedef typename _Traits::int_type       int_type;
00808   typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00809   typedef basic_ostream<_CharT, _Traits>   ostream_type;
00810 
00811 public:
00812   ostreambuf_iterator(streambuf_type* __buf) : _M_buf(__buf), _M_ok(__buf) {}
00813   ostreambuf_iterator(ostream_type& __o)
00814     : _M_buf(__o.rdbuf()), _M_ok(__o.rdbuf() != 0) {}
00815 
00816   ostreambuf_iterator& operator=(char_type __c) {
00817     _M_ok = _M_ok && !traits_type::eq_int_type(_M_buf->sputc(__c),
00818                                                traits_type::eof());
00819     return *this;
00820   }    
00821   
00822   ostreambuf_iterator& operator*()     { return *this; }
00823   ostreambuf_iterator& operator++()    { return *this; }
00824   ostreambuf_iterator& operator++(int) { return *this; }
00825 
00826   bool failed() const { return !_M_ok; }
00827 
00828 private:
00829   streambuf_type* _M_buf;
00830   bool _M_ok;
00831 };
00832 
00833 #else /* __STL_USE_NEW_IOSTREAMS */
00834 
00835 template <class _Tp, class _Dist = ptrdiff_t> class istream_iterator;
00836 
00837 template <class _Tp, class _Dist>
00838 inline bool operator==(const istream_iterator<_Tp, _Dist>&,
00839                        const istream_iterator<_Tp, _Dist>&);
00840 
00841 template <class _Tp, class _Dist>
00842 class istream_iterator {
00843 #ifdef __STL_TEMPLATE_FRIENDS
00844   template <class _T1, class _D1>
00845   friend bool operator==(const istream_iterator<_T1, _D1>&,
00846                          const istream_iterator<_T1, _D1>&);
00847 #else /* __STL_TEMPLATE_FRIENDS */
00848   friend bool __STD_QUALIFIER
00849   operator== __STL_NULL_TMPL_ARGS (const istream_iterator&,
00850                                    const istream_iterator&);
00851 #endif /* __STL_TEMPLATE_FRIENDS */
00852 
00853 protected:
00854   istream* _M_stream;
00855   _Tp _M_value;
00856   bool _M_end_marker;
00857   void _M_read() {
00858     _M_end_marker = (*_M_stream) ? true : false;
00859     if (_M_end_marker) *_M_stream >> _M_value;
00860     _M_end_marker = (*_M_stream) ? true : false;
00861   }
00862 public:
00863   typedef input_iterator_tag  iterator_category;
00864   typedef _Tp                 value_type;
00865   typedef _Dist               difference_type;
00866   typedef const _Tp*          pointer;
00867   typedef const _Tp&          reference;
00868 
00869   istream_iterator() : _M_stream(&cin), _M_end_marker(false) {}
00870   istream_iterator(istream& __s) : _M_stream(&__s) { _M_read(); }
00871   reference operator*() const { return _M_value; }
00872 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00873   pointer operator->() const { return &(operator*()); }
00874 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
00875   istream_iterator<_Tp, _Dist>& operator++() { 
00876     _M_read(); 
00877     return *this;
00878   }
00879   istream_iterator<_Tp, _Dist> operator++(int)  {
00880     istream_iterator<_Tp, _Dist> __tmp = *this;
00881     _M_read();
00882     return __tmp;
00883   }
00884 };
00885 
00886 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00887 
00888 template <class _Tp, class _Dist>
00889 inline input_iterator_tag 
00890 iterator_category(const istream_iterator<_Tp, _Dist>&)
00891 {
00892   return input_iterator_tag();
00893 }
00894 
00895 template <class _Tp, class _Dist>
00896 inline _Tp* 
00897 value_type(const istream_iterator<_Tp, _Dist>&) { return (_Tp*) 0; }
00898 
00899 template <class _Tp, class _Dist>
00900 inline _Dist* 
00901 distance_type(const istream_iterator<_Tp, _Dist>&) { return (_Dist*)0; }
00902 
00903 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00904 
00905 template <class _Tp, class _Distance>
00906 inline bool operator==(const istream_iterator<_Tp, _Distance>& __x,
00907                        const istream_iterator<_Tp, _Distance>& __y) {
00908   return (__x._M_stream == __y._M_stream &&
00909           __x._M_end_marker == __y._M_end_marker) ||
00910          __x._M_end_marker == false && __y._M_end_marker == false;
00911 }
00912 
00913 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00914 
00915 template <class _Tp, class _Distance>
00916 inline bool operator!=(const istream_iterator<_Tp, _Distance>& __x,
00917                        const istream_iterator<_Tp, _Distance>& __y) {
00918   return !(__x == __y);
00919 }
00920 
00921 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00922 
00923 template <class _Tp>
00924 class ostream_iterator {
00925 protected:
00926   ostream* _M_stream;
00927   const char* _M_string;
00928 public:
00929   typedef output_iterator_tag iterator_category;
00930   typedef void                value_type;
00931   typedef void                difference_type;
00932   typedef void                pointer;
00933   typedef void                reference;
00934 
00935   ostream_iterator(ostream& __s) : _M_stream(&__s), _M_string(0) {}
00936   ostream_iterator(ostream& __s, const char* __c) 
00937     : _M_stream(&__s), _M_string(__c)  {}
00938   ostream_iterator<_Tp>& operator=(const _Tp& __value) { 
00939     *_M_stream << __value;
00940     if (_M_string) *_M_stream << _M_string;
00941     return *this;
00942   }
00943   ostream_iterator<_Tp>& operator*() { return *this; }
00944   ostream_iterator<_Tp>& operator++() { return *this; } 
00945   ostream_iterator<_Tp>& operator++(int) { return *this; } 
00946 };
00947 
00948 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00949 
00950 template <class _Tp>
00951 inline output_iterator_tag 
00952 iterator_category(const ostream_iterator<_Tp>&) {
00953   return output_iterator_tag();
00954 }
00955 
00956 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00957 
00958 #endif /* __STL_USE_NEW_IOSTREAMS */
00959 
00960 #endif //UNDER_CE
00961 
00962 __STL_END_NAMESPACE
00963 
00964 #endif /* __SGI_STL_INTERNAL_ITERATOR_H */
00965 
00966 // Local Variables:
00967 // mode:C++
00968 // End:

Generated on Mon Jun 5 10:20:44 2006 for Intelligence.kdevelop by  doxygen 1.4.6