_iterator.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996-1998
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999 
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted 
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _STLP_INTERNAL_ITERATOR_H
00031 #define _STLP_INTERNAL_ITERATOR_H
00032 
00033 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
00034 # include <stl/_iterator_base.h>
00035 #endif
00036 
00037 _STLP_BEGIN_NAMESPACE
00038 
00039 #if defined ( _STLP_CLASS_PARTIAL_SPECIALIZATION )
00040 // This is the new version of reverse_iterator, as defined in the
00041 //  draft C++ standard.  It relies on the iterator_traits template,
00042 //  which in turn relies on partial specialization.  The class
00043 //  reverse_bidirectional_iterator is no longer part of the draft
00044 //  standard, but it is retained for backward compatibility.
00045 
00046 template <class _Iterator>
00047 class reverse_iterator : 
00048   public iterator<typename iterator_traits<_Iterator>::iterator_category,
00049                   typename iterator_traits<_Iterator>::value_type,
00050                   typename iterator_traits<_Iterator>::difference_type,
00051                   typename iterator_traits<_Iterator>::pointer,
00052                   typename iterator_traits<_Iterator>::reference>
00053 {
00054 protected:
00055   _Iterator current;
00056   typedef reverse_iterator<_Iterator> _Self;
00057 public:
00058   typedef typename iterator_traits<_Iterator>::iterator_category  iterator_category;
00059   typedef typename iterator_traits<_Iterator>::value_type value_type;
00060   typedef typename iterator_traits<_Iterator>::difference_type difference_type;
00061   typedef typename iterator_traits<_Iterator>::pointer pointer;
00062   typedef typename iterator_traits<_Iterator>::reference reference;
00063   typedef _Iterator iterator_type;
00064 public:
00065   reverse_iterator() {}
00066   explicit reverse_iterator(iterator_type __x) : current(__x) {}
00067   reverse_iterator(const _Self& __x) : current(__x.current) {}
00068   _Self& operator = (const _Self& __x) { current = __x.base(); return *this; } 
00069 #ifdef _STLP_MEMBER_TEMPLATES
00070   template <class _Iter>
00071   reverse_iterator(const reverse_iterator<_Iter>& __x) : current(__x.base()) {}
00072   template <class _Iter>
00073   _Self& operator = (const reverse_iterator<_Iter>& __x) { current = __x.base(); return *this; } 
00074 #endif /* _STLP_MEMBER_TEMPLATES */
00075     
00076   iterator_type base() const { return current; }
00077   reference operator*() const {
00078     _Iterator __tmp = current;
00079     return *--__tmp;
00080   }
00081   _STLP_DEFINE_ARROW_OPERATOR
00082   _Self& operator++() {
00083     --current;
00084     return *this;
00085   }
00086   _Self operator++(int) {
00087     _Self __tmp = *this;
00088     --current;
00089     return __tmp;
00090   }
00091   _Self& operator--() {
00092     ++current;
00093     return *this;
00094   }
00095   _Self operator--(int) {
00096     _Self __tmp = *this;
00097     ++current;
00098     return __tmp;
00099   }
00100 
00101   _Self operator+(difference_type __n) const {
00102     return _Self(current - __n);
00103   }
00104   _Self& operator+=(difference_type __n) {
00105     current -= __n;
00106     return *this;
00107   }
00108   _Self operator-(difference_type __n) const {
00109     return _Self(current + __n);
00110   }
00111   _Self& operator-=(difference_type __n) {
00112     current += __n;
00113     return *this;
00114   }
00115   reference operator[](difference_type __n) const { return *(*this + __n); }  
00116 }; 
00117  
00118 template <class _Iterator>
00119 inline bool  _STLP_CALL operator==(const reverse_iterator<_Iterator>& __x, 
00120                        const reverse_iterator<_Iterator>& __y) {
00121   return __x.base() == __y.base();
00122 }
00123 
00124 template <class _Iterator>
00125 inline bool _STLP_CALL operator<(const reverse_iterator<_Iterator>& __x, 
00126                       const reverse_iterator<_Iterator>& __y) {
00127   return __y.base() < __x.base();
00128 }
00129 
00130 #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00131 
00132 template <class _Iterator>
00133 inline bool _STLP_CALL operator!=(const reverse_iterator<_Iterator>& __x, 
00134                        const reverse_iterator<_Iterator>& __y) {
00135   return !(__x == __y);
00136 }
00137 
00138 template <class _Iterator>
00139 inline bool _STLP_CALL operator>(const reverse_iterator<_Iterator>& __x, 
00140                       const reverse_iterator<_Iterator>& __y) {
00141   return __y < __x;
00142 }
00143 
00144 template <class _Iterator>
00145 inline bool _STLP_CALL operator<=(const reverse_iterator<_Iterator>& __x, 
00146                        const reverse_iterator<_Iterator>& __y) {
00147   return !(__y < __x);
00148 }
00149 
00150 template <class _Iterator>
00151 inline bool _STLP_CALL operator>=(const reverse_iterator<_Iterator>& __x, 
00152                       const reverse_iterator<_Iterator>& __y) {
00153   return !(__x < __y);
00154 }
00155 
00156 #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
00157 
00158 template <class _Iterator>
00159 # ifdef __SUNPRO_CC
00160 inline ptrdiff_t _STLP_CALL
00161 # else
00162 inline typename reverse_iterator<_Iterator>::difference_type _STLP_CALL
00163 # endif
00164 operator-(const reverse_iterator<_Iterator>& __x, 
00165           const reverse_iterator<_Iterator>& __y) {
00166   return __y.base() - __x.base();
00167 }
00168 
00169 template <class _Iterator, class _DifferenceType>
00170 inline reverse_iterator<_Iterator>  _STLP_CALL
00171 operator+(_DifferenceType n,const reverse_iterator<_Iterator>& x) {
00172   return x.operator+(n);
00173 }
00174 
00175 # endif
00176 
00177 template <class _Container>
00178 class back_insert_iterator 
00179   : public iterator<output_iterator_tag,void,void,void,void>
00180 {
00181 protected:
00182   _Container* container;
00183 public:
00184   typedef _Container          container_type;
00185   typedef output_iterator_tag iterator_category;
00186 
00187   explicit back_insert_iterator(_Container& __x) : container(&__x) {}
00188   back_insert_iterator<_Container>&
00189   operator=(const typename _Container::value_type& __value) { 
00190     container->push_back(__value);
00191     return *this;
00192   }
00193   back_insert_iterator<_Container>& operator*() { return *this; }
00194   back_insert_iterator<_Container>& operator++() { return *this; }
00195   back_insert_iterator<_Container>& operator++(int) { return *this; }
00196 };
00197 
00198 template <class _Container>
00199 inline back_insert_iterator<_Container>  _STLP_CALL back_inserter(_Container& __x) {
00200   return back_insert_iterator<_Container>(__x);
00201 }
00202 
00203 template <class _Container>
00204 class front_insert_iterator 
00205   : public iterator<output_iterator_tag,void,void,void,void>
00206 {
00207 protected:
00208   _Container* container;
00209 public:
00210   typedef _Container          container_type;
00211   typedef output_iterator_tag iterator_category;
00212   explicit front_insert_iterator(_Container& __x) : container(&__x) {}
00213   front_insert_iterator<_Container>&
00214   operator=(const typename _Container::value_type& __value) { 
00215     container->push_front(__value);
00216     return *this;
00217   }
00218   front_insert_iterator<_Container>& operator*() { return *this; }
00219   front_insert_iterator<_Container>& operator++() { return *this; }
00220   front_insert_iterator<_Container>& operator++(int) { return *this; }
00221 };
00222 
00223 template <class _Container>
00224 inline front_insert_iterator<_Container>  _STLP_CALL front_inserter(_Container& __x) {
00225   return front_insert_iterator<_Container>(__x);
00226 }
00227 
00228 template <class _Container>
00229 class insert_iterator 
00230   : public iterator<output_iterator_tag,void,void,void,void>
00231 {
00232 protected:
00233   _Container* container;
00234   typename _Container::iterator iter;
00235 public:
00236   typedef _Container          container_type;
00237   typedef output_iterator_tag iterator_category;
00238   insert_iterator(_Container& __x, typename _Container::iterator __i) 
00239     : container(&__x), iter(__i) {}
00240   insert_iterator<_Container>&
00241   operator=(const typename _Container::value_type& __value) { 
00242     iter = container->insert(iter, __value);
00243     ++iter;
00244     return *this;
00245   }
00246   insert_iterator<_Container>& operator*() { return *this; }
00247   insert_iterator<_Container>& operator++() { return *this; }
00248   insert_iterator<_Container>& operator++(int) { return *this; }
00249 };
00250 
00251 template <class _Container, class _Iterator>
00252 inline insert_iterator<_Container>  _STLP_CALL
00253 inserter(_Container& __x, _Iterator __i)
00254 {
00255   typedef typename _Container::iterator __iter;
00256   return insert_iterator<_Container>(__x, __iter(__i));
00257 }
00258 
00259 _STLP_END_NAMESPACE
00260 
00261 #if ! defined ( _STLP_CLASS_PARTIAL_SPECIALIZATION ) || defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
00262 # include <stl/_iterator_old.h>
00263 #endif /* __NO_PARTIAL_SPEC || ANACHRONISMS */
00264 
00265 #endif /* _STLP_INTERNAL_ITERATOR_H */
00266 
00267 // Local Variables:
00268 // mode:C++
00269 // End:

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