4 * Hewlett-Packard Company
6 * Copyright (c) 1996-1998
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
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.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #ifndef _STLP_INTERNAL_ITERATOR_H
31 #define _STLP_INTERNAL_ITERATOR_H
33 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
34 # include <stl/_iterator_base.h>
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.
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> {
55 typedef reverse_iterator<_Iterator> _Self;
57 typedef typename iterator_traits<_Iterator>::iterator_category iterator_category;
58 typedef typename iterator_traits<_Iterator>::value_type value_type;
59 typedef typename iterator_traits<_Iterator>::difference_type difference_type;
60 typedef typename iterator_traits<_Iterator>::pointer pointer;
61 typedef typename iterator_traits<_Iterator>::reference reference;
62 typedef _Iterator iterator_type;
65 explicit reverse_iterator(iterator_type __x) : current(__x) {}
66 reverse_iterator(const _Self& __x) : current(__x.current) {}
67 _Self& operator = (const _Self& __x) { current = __x.base(); return *this; }
68 # if defined (_STLP_MEMBER_TEMPLATES)
69 template <class _Iter>
70 reverse_iterator(const reverse_iterator<_Iter>& __x) : current(__x.base()) {}
71 template <class _Iter>
72 _Self& operator = (const reverse_iterator<_Iter>& __x) { current = __x.base(); return *this; }
73 # endif /* _STLP_MEMBER_TEMPLATES */
75 iterator_type base() const { return current; }
76 reference operator*() const {
77 _Iterator __tmp = current;
80 _STLP_DEFINE_ARROW_OPERATOR
85 _Self operator++(int) {
94 _Self operator--(int) {
100 _Self operator+(difference_type __n) const { return _Self(current - __n); }
101 _Self& operator+=(difference_type __n) {
105 _Self operator-(difference_type __n) const { return _Self(current + __n); }
106 _Self& operator-=(difference_type __n) {
110 reference operator[](difference_type __n) const { return *(*this + __n); }
113 template <class _Iterator>
114 inline bool _STLP_CALL operator==(const reverse_iterator<_Iterator>& __x,
115 const reverse_iterator<_Iterator>& __y)
116 { return __x.base() == __y.base(); }
118 template <class _Iterator>
119 inline bool _STLP_CALL operator<(const reverse_iterator<_Iterator>& __x,
120 const reverse_iterator<_Iterator>& __y)
121 { return __y.base() < __x.base(); }
123 # if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
124 template <class _Iterator>
125 inline bool _STLP_CALL operator!=(const reverse_iterator<_Iterator>& __x,
126 const reverse_iterator<_Iterator>& __y)
127 { return !(__x == __y); }
129 template <class _Iterator>
130 inline bool _STLP_CALL operator>(const reverse_iterator<_Iterator>& __x,
131 const reverse_iterator<_Iterator>& __y)
132 { return __y < __x; }
134 template <class _Iterator>
135 inline bool _STLP_CALL operator<=(const reverse_iterator<_Iterator>& __x,
136 const reverse_iterator<_Iterator>& __y)
137 { return !(__y < __x); }
139 template <class _Iterator>
140 inline bool _STLP_CALL operator>=(const reverse_iterator<_Iterator>& __x,
141 const reverse_iterator<_Iterator>& __y)
142 { return !(__x < __y); }
143 # endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
145 template <class _Iterator>
146 # if defined (__SUNPRO_CC)
147 inline ptrdiff_t _STLP_CALL
149 inline typename reverse_iterator<_Iterator>::difference_type _STLP_CALL
151 operator-(const reverse_iterator<_Iterator>& __x,
152 const reverse_iterator<_Iterator>& __y)
153 { return __y.base() - __x.base(); }
155 template <class _Iterator, class _DifferenceType>
156 inline reverse_iterator<_Iterator> _STLP_CALL
157 operator+(_DifferenceType n,const reverse_iterator<_Iterator>& x)
158 { return x.operator+(n); }
161 template <class _Container>
162 class back_insert_iterator
163 : public iterator<output_iterator_tag, void, void, void, void> {
164 typedef back_insert_iterator<_Container> _Self;
166 //c is a Standard name (24.4.2.1), do no make it STLport naming convention compliant.
167 _Container *container;
169 typedef _Container container_type;
170 typedef output_iterator_tag iterator_category;
172 explicit back_insert_iterator(_Container& __x) : container(&__x) {}
174 _Self& operator=(const _Self& __other) {
175 container = __other.container;
178 _Self& operator=(const typename _Container::value_type& __val) {
179 container->push_back(__val);
182 _Self& operator*() { return *this; }
183 _Self& operator++() { return *this; }
184 _Self operator++(int) { return *this; }
187 template <class _Container>
188 inline back_insert_iterator<_Container> _STLP_CALL back_inserter(_Container& __x)
189 { return back_insert_iterator<_Container>(__x); }
191 template <class _Container>
192 class front_insert_iterator
193 : public iterator<output_iterator_tag, void, void, void, void> {
194 typedef front_insert_iterator<_Container> _Self;
196 //c is a Standard name (24.4.2.3), do no make it STLport naming convention compliant.
197 _Container *container;
199 typedef _Container container_type;
200 typedef output_iterator_tag iterator_category;
201 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
203 _Self& operator=(const _Self& __other) {
204 container = __other.container;
207 _Self& operator=(const typename _Container::value_type& __val) {
208 container->push_front(__val);
211 _Self& operator*() { return *this; }
212 _Self& operator++() { return *this; }
213 _Self operator++(int) { return *this; }
216 template <class _Container>
217 inline front_insert_iterator<_Container> _STLP_CALL front_inserter(_Container& __x)
218 { return front_insert_iterator<_Container>(__x); }
220 template <class _Container>
221 class insert_iterator
222 : public iterator<output_iterator_tag, void, void, void, void> {
223 typedef insert_iterator<_Container> _Self;
225 //container is a Standard name (24.4.2.5), do no make it STLport naming convention compliant.
226 _Container *container;
227 typename _Container::iterator _M_iter;
229 typedef _Container container_type;
230 typedef output_iterator_tag iterator_category;
231 insert_iterator(_Container& __x, typename _Container::iterator __i)
232 : container(&__x), _M_iter(__i) {}
234 _Self& operator=(_Self const& __other) {
235 container = __other.container;
236 _M_iter = __other._M_iter;
239 _Self& operator=(const typename _Container::value_type& __val) {
240 _M_iter = container->insert(_M_iter, __val);
244 _Self& operator*() { return *this; }
245 _Self& operator++() { return *this; }
246 _Self& operator++(int) { return *this; }
249 template <class _Container, class _Iterator>
250 inline insert_iterator<_Container> _STLP_CALL
251 inserter(_Container& __x, _Iterator __i) {
252 typedef typename _Container::iterator __iter;
253 return insert_iterator<_Container>(__x, __iter(__i));
258 #if ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) || defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
259 # include <stl/_iterator_old.h>
262 #endif /* _STLP_INTERNAL_ITERATOR_H */