/home/ntakagi/work/STLport-5.1.5/stlport/stl/_iterator.h

Go to the documentation of this file.
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 protected:
00054   _Iterator current;
00055   typedef reverse_iterator<_Iterator> _Self;
00056 public:
00057   typedef typename iterator_traits<_Iterator>::iterator_category  iterator_category;
00058   typedef typename iterator_traits<_Iterator>::value_type value_type;
00059   typedef typename iterator_traits<_Iterator>::difference_type difference_type;
00060   typedef typename iterator_traits<_Iterator>::pointer pointer;
00061   typedef typename iterator_traits<_Iterator>::reference reference;
00062   typedef _Iterator iterator_type;
00063 public:
00064   reverse_iterator() {}
00065   explicit reverse_iterator(iterator_type __x) : current(__x) {}
00066   reverse_iterator(const _Self& __x) : current(__x.current) {}
00067   _Self& operator = (const _Self& __x) { current = __x.base(); return *this; }
00068 #  if defined (_STLP_MEMBER_TEMPLATES)
00069   template <class _Iter>
00070   reverse_iterator(const reverse_iterator<_Iter>& __x) : current(__x.base()) {}
00071   template <class _Iter>
00072   _Self& operator = (const reverse_iterator<_Iter>& __x) { current = __x.base(); return *this; }
00073 #  endif /* _STLP_MEMBER_TEMPLATES */
00074 
00075   iterator_type base() const { return current; }
00076   reference operator*() const {
00077     _Iterator __tmp = current;
00078     return *--__tmp;
00079   }
00080   _STLP_DEFINE_ARROW_OPERATOR
00081   _Self& operator++() {
00082     --current;
00083     return *this;
00084   }
00085   _Self operator++(int) {
00086     _Self __tmp = *this;
00087     --current;
00088     return __tmp;
00089   }
00090   _Self& operator--() {
00091     ++current;
00092     return *this;
00093   }
00094   _Self operator--(int) {
00095     _Self __tmp = *this;
00096     ++current;
00097     return __tmp;
00098   }
00099 
00100   _Self operator+(difference_type __n) const { return _Self(current - __n); }
00101   _Self& operator+=(difference_type __n) {
00102     current -= __n;
00103     return *this;
00104   }
00105   _Self operator-(difference_type __n) const { return _Self(current + __n); }
00106   _Self& operator-=(difference_type __n) {
00107     current += __n;
00108     return *this;
00109   }
00110   reference operator[](difference_type __n) const { return *(*this + __n); }
00111 };
00112 
00113 template <class _Iterator>
00114 inline bool  _STLP_CALL operator==(const reverse_iterator<_Iterator>& __x,
00115                                    const reverse_iterator<_Iterator>& __y)
00116 { return __x.base() == __y.base(); }
00117 
00118 template <class _Iterator>
00119 inline bool _STLP_CALL operator<(const reverse_iterator<_Iterator>& __x,
00120                                  const reverse_iterator<_Iterator>& __y)
00121 { return __y.base() < __x.base(); }
00122 
00123 #  if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
00124 template <class _Iterator>
00125 inline bool _STLP_CALL operator!=(const reverse_iterator<_Iterator>& __x,
00126                                   const reverse_iterator<_Iterator>& __y)
00127 { return !(__x == __y); }
00128 
00129 template <class _Iterator>
00130 inline bool _STLP_CALL operator>(const reverse_iterator<_Iterator>& __x,
00131                                  const reverse_iterator<_Iterator>& __y)
00132 { return __y < __x; }
00133 
00134 template <class _Iterator>
00135 inline bool _STLP_CALL operator<=(const reverse_iterator<_Iterator>& __x,
00136                                   const reverse_iterator<_Iterator>& __y)
00137 { return !(__y < __x); }
00138 
00139 template <class _Iterator>
00140 inline bool _STLP_CALL operator>=(const reverse_iterator<_Iterator>& __x,
00141                                   const reverse_iterator<_Iterator>& __y)
00142 { return !(__x < __y); }
00143 #  endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
00144 
00145 template <class _Iterator>
00146 #  if defined (__SUNPRO_CC)
00147 inline ptrdiff_t _STLP_CALL
00148 #  else
00149 inline typename reverse_iterator<_Iterator>::difference_type _STLP_CALL
00150 #  endif
00151 operator-(const reverse_iterator<_Iterator>& __x,
00152           const reverse_iterator<_Iterator>& __y)
00153 { return __y.base() - __x.base(); }
00154 
00155 template <class _Iterator, class _DifferenceType>
00156 inline reverse_iterator<_Iterator>  _STLP_CALL
00157 operator+(_DifferenceType n,const reverse_iterator<_Iterator>& x)
00158 { return x.operator+(n); }
00159 #endif
00160 
00161 template <class _Container>
00162 class back_insert_iterator
00163   : public iterator<output_iterator_tag, void, void, void, void> {
00164   typedef back_insert_iterator<_Container> _Self;
00165 protected:
00166   //c is a Standard name (24.4.2.1), do no make it STLport naming convention compliant.
00167   _Container *container;
00168 public:
00169   typedef _Container          container_type;
00170   typedef output_iterator_tag iterator_category;
00171 
00172   explicit back_insert_iterator(_Container& __x) : container(&__x) {}
00173 
00174   _Self& operator=(const _Self& __other) {
00175     container = __other.container;
00176     return *this;
00177   }
00178   _Self& operator=(const typename _Container::value_type& __val) {
00179     container->push_back(__val);
00180     return *this;
00181   }
00182   _Self& operator*() { return *this; }
00183   _Self& operator++() { return *this; }
00184   _Self  operator++(int) { return *this; }
00185 };
00186 
00187 template <class _Container>
00188 inline back_insert_iterator<_Container>  _STLP_CALL back_inserter(_Container& __x)
00189 { return back_insert_iterator<_Container>(__x); }
00190 
00191 template <class _Container>
00192 class front_insert_iterator
00193   : public iterator<output_iterator_tag, void, void, void, void> {
00194   typedef front_insert_iterator<_Container> _Self;
00195 protected:
00196   //c is a Standard name (24.4.2.3), do no make it STLport naming convention compliant.
00197   _Container *container;
00198 public:
00199   typedef _Container          container_type;
00200   typedef output_iterator_tag iterator_category;
00201   explicit front_insert_iterator(_Container& __x) : container(&__x) {}
00202 
00203   _Self& operator=(const _Self& __other) {
00204     container = __other.container;
00205     return *this;
00206   }
00207   _Self& operator=(const typename _Container::value_type& __val) {
00208     container->push_front(__val);
00209     return *this;
00210   }
00211   _Self& operator*() { return *this; }
00212   _Self& operator++() { return *this; }
00213   _Self  operator++(int) { return *this; }
00214 };
00215 
00216 template <class _Container>
00217 inline front_insert_iterator<_Container>  _STLP_CALL front_inserter(_Container& __x)
00218 { return front_insert_iterator<_Container>(__x); }
00219 
00220 template <class _Container>
00221 class insert_iterator
00222   : public iterator<output_iterator_tag, void, void, void, void> {
00223   typedef insert_iterator<_Container> _Self;
00224 protected:
00225   //container is a Standard name (24.4.2.5), do no make it STLport naming convention compliant.
00226   _Container *container;
00227   typename _Container::iterator _M_iter;
00228 public:
00229   typedef _Container          container_type;
00230   typedef output_iterator_tag iterator_category;
00231   insert_iterator(_Container& __x, typename _Container::iterator __i)
00232     : container(&__x), _M_iter(__i) {}
00233 
00234   _Self& operator=(_Self const& __other) {
00235     container = __other.container;
00236     _M_iter = __other._M_iter;
00237     return *this;
00238   }
00239   _Self& operator=(const typename _Container::value_type& __val) {
00240     _M_iter = container->insert(_M_iter, __val);
00241     ++_M_iter;
00242     return *this;
00243   }
00244   _Self& operator*() { return *this; }
00245   _Self& operator++() { return *this; }
00246   _Self& operator++(int) { return *this; }
00247 };
00248 
00249 template <class _Container, class _Iterator>
00250 inline insert_iterator<_Container>  _STLP_CALL
00251 inserter(_Container& __x, _Iterator __i) {
00252   typedef typename _Container::iterator __iter;
00253   return insert_iterator<_Container>(__x, __iter(__i));
00254 }
00255 
00256 _STLP_END_NAMESPACE
00257 
00258 #if ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) || defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
00259 #  include <stl/_iterator_old.h>
00260 #endif
00261 
00262 #endif /* _STLP_INTERNAL_ITERATOR_H */
00263 
00264 // Local Variables:
00265 // mode:C++
00266 // End:



Generated on Mon Mar 10 15:32:26 2008 by  doxygen 1.5.1