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>
56 typedef reverse_iterator<_Iterator> _Self;
58 typedef typename iterator_traits<_Iterator>::iterator_category iterator_category;
59 typedef typename iterator_traits<_Iterator>::value_type value_type;
60 typedef typename iterator_traits<_Iterator>::difference_type difference_type;
61 typedef typename iterator_traits<_Iterator>::pointer pointer;
62 typedef typename iterator_traits<_Iterator>::reference reference;
63 typedef _Iterator iterator_type;
66 explicit reverse_iterator(iterator_type __x) : current(__x) {}
67 reverse_iterator(const _Self& __x) : current(__x.current) {}
68 _Self& operator = (const _Self& __x) { current = __x.base(); return *this; }
69 #ifdef _STLP_MEMBER_TEMPLATES
70 template <class _Iter>
71 reverse_iterator(const reverse_iterator<_Iter>& __x) : current(__x.base()) {}
72 template <class _Iter>
73 _Self& operator = (const reverse_iterator<_Iter>& __x) { current = __x.base(); return *this; }
74 #endif /* _STLP_MEMBER_TEMPLATES */
76 iterator_type base() const { return current; }
77 reference operator*() const {
78 _Iterator __tmp = current;
81 _STLP_DEFINE_ARROW_OPERATOR
86 _Self operator++(int) {
95 _Self operator--(int) {
101 _Self operator+(difference_type __n) const {
102 return _Self(current - __n);
104 _Self& operator+=(difference_type __n) {
108 _Self operator-(difference_type __n) const {
109 return _Self(current + __n);
111 _Self& operator-=(difference_type __n) {
115 reference operator[](difference_type __n) const { return *(*this + __n); }
118 template <class _Iterator>
119 inline bool _STLP_CALL operator==(const reverse_iterator<_Iterator>& __x,
120 const reverse_iterator<_Iterator>& __y) {
121 return __x.base() == __y.base();
124 template <class _Iterator>
125 inline bool _STLP_CALL operator<(const reverse_iterator<_Iterator>& __x,
126 const reverse_iterator<_Iterator>& __y) {
127 return __y.base() < __x.base();
130 #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
132 template <class _Iterator>
133 inline bool _STLP_CALL operator!=(const reverse_iterator<_Iterator>& __x,
134 const reverse_iterator<_Iterator>& __y) {
135 return !(__x == __y);
138 template <class _Iterator>
139 inline bool _STLP_CALL operator>(const reverse_iterator<_Iterator>& __x,
140 const reverse_iterator<_Iterator>& __y) {
144 template <class _Iterator>
145 inline bool _STLP_CALL operator<=(const reverse_iterator<_Iterator>& __x,
146 const reverse_iterator<_Iterator>& __y) {
150 template <class _Iterator>
151 inline bool _STLP_CALL operator>=(const reverse_iterator<_Iterator>& __x,
152 const reverse_iterator<_Iterator>& __y) {
156 #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
158 template <class _Iterator>
160 inline ptrdiff_t _STLP_CALL
162 inline typename reverse_iterator<_Iterator>::difference_type _STLP_CALL
164 operator-(const reverse_iterator<_Iterator>& __x,
165 const reverse_iterator<_Iterator>& __y) {
166 return __y.base() - __x.base();
169 template <class _Iterator, class _DifferenceType>
170 inline reverse_iterator<_Iterator> _STLP_CALL
171 operator+(_DifferenceType n,const reverse_iterator<_Iterator>& x) {
172 return x.operator+(n);
177 template <class _Container>
178 class back_insert_iterator
179 : public iterator<output_iterator_tag,void,void,void,void>
182 _Container* container;
184 typedef _Container container_type;
185 typedef output_iterator_tag iterator_category;
187 explicit back_insert_iterator(_Container& __x) : container(&__x) {}
188 back_insert_iterator<_Container>&
189 operator=(const typename _Container::value_type& __val) {
190 container->push_back(__val);
193 back_insert_iterator<_Container>& operator*() { return *this; }
194 back_insert_iterator<_Container>& operator++() { return *this; }
195 back_insert_iterator<_Container> operator++(int) { return *this; }
198 template <class _Container>
199 inline back_insert_iterator<_Container> _STLP_CALL back_inserter(_Container& __x) {
200 return back_insert_iterator<_Container>(__x);
203 template <class _Container>
204 class front_insert_iterator
205 : public iterator<output_iterator_tag,void,void,void,void>
208 _Container* container;
210 typedef _Container container_type;
211 typedef output_iterator_tag iterator_category;
212 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
213 front_insert_iterator<_Container>&
214 operator=(const typename _Container::value_type& __val) {
215 container->push_front(__val);
218 front_insert_iterator<_Container>& operator*() { return *this; }
219 front_insert_iterator<_Container>& operator++() { return *this; }
220 front_insert_iterator<_Container>& operator++(int) { return *this; }
223 template <class _Container>
224 inline front_insert_iterator<_Container> _STLP_CALL front_inserter(_Container& __x) {
225 return front_insert_iterator<_Container>(__x);
228 template <class _Container>
229 class insert_iterator
230 : public iterator<output_iterator_tag,void,void,void,void>
233 _Container* container;
234 typename _Container::iterator iter;
236 typedef _Container container_type;
237 typedef output_iterator_tag iterator_category;
238 insert_iterator(_Container& __x, typename _Container::iterator __i)
239 : container(&__x), iter(__i) {}
240 insert_iterator<_Container>&
241 operator=(const typename _Container::value_type& __val) {
242 iter = container->insert(iter, __val);
246 insert_iterator<_Container>& operator*() { return *this; }
247 insert_iterator<_Container>& operator++() { return *this; }
248 insert_iterator<_Container>& operator++(int) { return *this; }
251 template <class _Container, class _Iterator>
252 inline insert_iterator<_Container> _STLP_CALL
253 inserter(_Container& __x, _Iterator __i)
255 typedef typename _Container::iterator __iter;
256 return insert_iterator<_Container>(__x, __iter(__i));
261 #if ! defined ( _STLP_CLASS_PARTIAL_SPECIALIZATION ) || defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
262 # include <stl/_iterator_old.h>
263 #endif /* __NO_PARTIAL_SPEC || ANACHRONISMS */
265 #endif /* _STLP_INTERNAL_ITERATOR_H */