1 /*
2  *
3  * Copyright (c) 1994
4  * Hewlett-Packard Company
5  *
6  * Copyright (c) 1996-1998
7  * Silicon Graphics Computer Systems, Inc.
8  *
9  * Copyright (c) 1997
10  * Moscow Center for SPARC Technology
11  *
12  * Copyright (c) 1999
13  * Boris Fomitchev
14  *
15  * This material is provided "as is", with absolutely no warranty expressed
16  * or implied. Any use is at your own risk.
17  *
18  * Permission to use or copy this software for any purpose is hereby granted
19  * without fee, provided the above notices are retained on all copies.
20  * Permission to modify the code and to distribute modified code is granted,
21  * provided the above notices are retained, and a notice that the code was
22  * modified is included with the above copyright notice.
23  *
24  */
25 
26 /* NOTE: This is an internal header file, included by other STL headers.
27  *   You should not attempt to use it directly.
28  */
29 
30 #ifndef _STLP_INTERNAL_ITERATOR_H
31 #define _STLP_INTERNAL_ITERATOR_H
32 
33 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
34 #  include <stl/_iterator_base.h>
35 #endif
36 
37 _STLP_BEGIN_NAMESPACE
38 
39 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
40 // This is the new version of reverse_iterator, as defined in the
41 //  draft C++ standard.  It relies on the iterator_traits template,
42 //  which in turn relies on partial specialization.  The class
43 //  reverse_bidirectional_iterator is no longer part of the draft
44 //  standard, but it is retained for backward compatibility.
45 
46 template <class _Iterator>
47 class reverse_iterator :
48   public iterator<typename iterator_traits<_Iterator>::iterator_category,
49                   typename iterator_traits<_Iterator>::value_type,
50                   typename iterator_traits<_Iterator>::difference_type,
51                   typename iterator_traits<_Iterator>::pointer,
52                   typename iterator_traits<_Iterator>::reference> {
53 protected:
54   _Iterator current;
55   typedef reverse_iterator<_Iterator> _Self;
56 public:
57   typedef typename iterator_traits<_Iterator>::difference_type difference_type;
58   // pointer type required for arrow operator hidden behind _STLP_DEFINE_ARROW_OPERATOR:
59   typedef typename iterator_traits<_Iterator>::pointer pointer;
60   typedef typename iterator_traits<_Iterator>::reference reference;
61   typedef _Iterator iterator_type;
62 public:
reverse_iterator()63   reverse_iterator() {}
reverse_iterator(iterator_type __x)64   explicit reverse_iterator(iterator_type __x) : current(__x) {}
reverse_iterator(const _Self & __x)65   reverse_iterator(const _Self& __x) : current(__x.current) {}
66   _Self& operator = (const _Self& __x) { current = __x.base(); return *this; }
67 #  if defined (_STLP_MEMBER_TEMPLATES)
68   template <class _Iter>
reverse_iterator(const reverse_iterator<_Iter> & __x)69   reverse_iterator(const reverse_iterator<_Iter>& __x) : current(__x.base()) {}
70   template <class _Iter>
71   _Self& operator = (const reverse_iterator<_Iter>& __x) { current = __x.base(); return *this; }
72 #  endif /* _STLP_MEMBER_TEMPLATES */
73 
base()74   iterator_type base() const { return current; }
75   reference operator*() const {
76     _Iterator __tmp = current;
77     return *--__tmp;
78   }
79   _STLP_DEFINE_ARROW_OPERATOR
80   _Self& operator++() {
81     --current;
82     return *this;
83   }
84   _Self operator++(int) {
85     _Self __tmp = *this;
86     --current;
87     return __tmp;
88   }
89   _Self& operator--() {
90     ++current;
91     return *this;
92   }
93   _Self operator--(int) {
94     _Self __tmp = *this;
95     ++current;
96     return __tmp;
97   }
98 
99   _Self operator+(difference_type __n) const { return _Self(current - __n); }
100   _Self& operator+=(difference_type __n) {
101     current -= __n;
102     return *this;
103   }
104   _Self operator-(difference_type __n) const { return _Self(current + __n); }
105   _Self& operator-=(difference_type __n) {
106     current += __n;
107     return *this;
108   }
109   reference operator[](difference_type __n) const { return *(*this + __n); }
110 };
111 
112 template <class _Iterator>
113 inline bool  _STLP_CALL operator==(const reverse_iterator<_Iterator>& __x,
114                                    const reverse_iterator<_Iterator>& __y)
115 { return __x.base() == __y.base(); }
116 
117 template <class _Iterator>
118 inline bool _STLP_CALL operator<(const reverse_iterator<_Iterator>& __x,
119                                  const reverse_iterator<_Iterator>& __y)
120 { return __y.base() < __x.base(); }
121 
122 #  if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
123 template <class _Iterator>
124 inline bool _STLP_CALL operator!=(const reverse_iterator<_Iterator>& __x,
125                                   const reverse_iterator<_Iterator>& __y)
126 { return !(__x == __y); }
127 
128 template <class _Iterator>
129 inline bool _STLP_CALL operator>(const reverse_iterator<_Iterator>& __x,
130                                  const reverse_iterator<_Iterator>& __y)
131 { return __y < __x; }
132 
133 template <class _Iterator>
134 inline bool _STLP_CALL operator<=(const reverse_iterator<_Iterator>& __x,
135                                   const reverse_iterator<_Iterator>& __y)
136 { return !(__y < __x); }
137 
138 template <class _Iterator>
139 inline bool _STLP_CALL operator>=(const reverse_iterator<_Iterator>& __x,
140                                   const reverse_iterator<_Iterator>& __y)
141 { return !(__x < __y); }
142 #  endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
143 
144 template <class _Iterator>
145 #  if defined (__SUNPRO_CC)
146 inline ptrdiff_t _STLP_CALL
147 #  else
148 inline typename reverse_iterator<_Iterator>::difference_type _STLP_CALL
149 #  endif
150 operator-(const reverse_iterator<_Iterator>& __x,
151           const reverse_iterator<_Iterator>& __y)
152 { return __y.base() - __x.base(); }
153 
154 template <class _Iterator, class _DifferenceType>
155 inline reverse_iterator<_Iterator>  _STLP_CALL
156 operator+(_DifferenceType n,const reverse_iterator<_Iterator>& x)
157 { return x.operator+(n); }
158 #endif
159 
160 template <class _Container>
161 class back_insert_iterator
162   : public iterator<output_iterator_tag, void, void, void, void> {
163   typedef back_insert_iterator<_Container> _Self;
164 protected:
165   //c is a Standard name (24.4.2.1), do no make it STLport naming convention compliant.
166   _Container *container;
167 public:
168   typedef _Container          container_type;
169   typedef output_iterator_tag iterator_category;
170 
back_insert_iterator(_Container & __x)171   explicit back_insert_iterator(_Container& __x) : container(&__x) {}
172 
173   _Self& operator=(const _Self& __other) {
174     container = __other.container;
175     return *this;
176   }
177   _Self& operator=(const typename _Container::value_type& __val) {
178     container->push_back(__val);
179     return *this;
180   }
181   _Self& operator*() { return *this; }
182   _Self& operator++() { return *this; }
183   _Self  operator++(int) { return *this; }
184 };
185 
186 template <class _Container>
back_inserter(_Container & __x)187 inline back_insert_iterator<_Container>  _STLP_CALL back_inserter(_Container& __x)
188 { return back_insert_iterator<_Container>(__x); }
189 
190 template <class _Container>
191 class front_insert_iterator
192   : public iterator<output_iterator_tag, void, void, void, void> {
193   typedef front_insert_iterator<_Container> _Self;
194 protected:
195   //c is a Standard name (24.4.2.3), do no make it STLport naming convention compliant.
196   _Container *container;
197 public:
198   typedef _Container          container_type;
199   typedef output_iterator_tag iterator_category;
front_insert_iterator(_Container & __x)200   explicit front_insert_iterator(_Container& __x) : container(&__x) {}
201 
202   _Self& operator=(const _Self& __other) {
203     container = __other.container;
204     return *this;
205   }
206   _Self& operator=(const typename _Container::value_type& __val) {
207     container->push_front(__val);
208     return *this;
209   }
210   _Self& operator*() { return *this; }
211   _Self& operator++() { return *this; }
212   _Self  operator++(int) { return *this; }
213 };
214 
215 template <class _Container>
front_inserter(_Container & __x)216 inline front_insert_iterator<_Container>  _STLP_CALL front_inserter(_Container& __x)
217 { return front_insert_iterator<_Container>(__x); }
218 
219 template <class _Container>
220 class insert_iterator
221   : public iterator<output_iterator_tag, void, void, void, void> {
222   typedef insert_iterator<_Container> _Self;
223 protected:
224   //container is a Standard name (24.4.2.5), do no make it STLport naming convention compliant.
225   _Container *container;
226   typename _Container::iterator _M_iter;
227 public:
228   typedef _Container          container_type;
229   typedef output_iterator_tag iterator_category;
insert_iterator(_Container & __x,typename _Container::iterator __i)230   insert_iterator(_Container& __x, typename _Container::iterator __i)
231     : container(&__x), _M_iter(__i) {}
232 
233   _Self& operator=(_Self const& __other) {
234     container = __other.container;
235     _M_iter = __other._M_iter;
236     return *this;
237   }
238   _Self& operator=(const typename _Container::value_type& __val) {
239     _M_iter = container->insert(_M_iter, __val);
240     ++_M_iter;
241     return *this;
242   }
243   _Self& operator*() { return *this; }
244   _Self& operator++() { return *this; }
245   _Self& operator++(int) { return *this; }
246 };
247 
248 template <class _Container, class _Iterator>
249 inline insert_iterator<_Container>  _STLP_CALL
inserter(_Container & __x,_Iterator __i)250 inserter(_Container& __x, _Iterator __i) {
251   typedef typename _Container::iterator __iter;
252   return insert_iterator<_Container>(__x, __iter(__i));
253 }
254 
255 _STLP_END_NAMESPACE
256 
257 #if ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) || defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
258 #  include <stl/_iterator_old.h>
259 #endif
260 
261 #endif /* _STLP_INTERNAL_ITERATOR_H */
262 
263 // Local Variables:
264 // mode:C++
265 // End:
266