4 * Hewlett-Packard Company
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
15 * Copyright (c) 1996-1998
16 * Silicon Graphics Computer Systems, Inc.
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
31 #ifndef __SGI_STL_INTERNAL_ITERATOR_H
32 #define __SGI_STL_INTERNAL_ITERATOR_H
36 // 24.4.1 Reverse iterators
37 template<typename _Iterator>
38 class reverse_iterator
39 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
40 typename iterator_traits<_Iterator>::value_type,
41 typename iterator_traits<_Iterator>::difference_type,
42 typename iterator_traits<_Iterator>::pointer,
43 typename iterator_traits<_Iterator>::reference>
49 typedef _Iterator iterator_type;
50 typedef typename iterator_traits<_Iterator>::difference_type
52 typedef typename iterator_traits<_Iterator>::reference reference;
53 typedef typename iterator_traits<_Iterator>::pointer pointer;
59 reverse_iterator(iterator_type __x) : _M_current(__x) {}
61 reverse_iterator(const reverse_iterator& __x)
62 : _M_current(__x._M_current) { }
64 template<typename _Iter>
65 reverse_iterator(const reverse_iterator<_Iter>& __x)
66 : _M_current(__x.base()) {}
69 base() const { return _M_current; }
74 _Iterator __tmp = _M_current;
79 operator->() const { return &(operator*()); }
91 reverse_iterator __tmp = *this;
103 reverse_iterator operator--(int)
105 reverse_iterator __tmp = *this;
111 operator+(difference_type __n) const
112 { return reverse_iterator(_M_current - __n); }
115 operator+=(difference_type __n)
122 operator-(difference_type __n) const
123 { return reverse_iterator(_M_current + __n); }
126 operator-=(difference_type __n)
133 operator[](difference_type __n) const { return *(*this + __n); }
136 template<typename _Iterator>
138 operator==(const reverse_iterator<_Iterator>& __x,
139 const reverse_iterator<_Iterator>& __y)
140 { return __x.base() == __y.base(); }
142 template<typename _Iterator>
144 operator<(const reverse_iterator<_Iterator>& __x,
145 const reverse_iterator<_Iterator>& __y)
146 { return __y.base() < __x.base(); }
148 template<typename _Iterator>
150 operator!=(const reverse_iterator<_Iterator>& __x,
151 const reverse_iterator<_Iterator>& __y)
152 { return !(__x == __y); }
154 template<typename _Iterator>
156 operator>(const reverse_iterator<_Iterator>& __x,
157 const reverse_iterator<_Iterator>& __y)
158 { return __y < __x; }
160 template<typename _Iterator>
162 operator<=(const reverse_iterator<_Iterator>& __x,
163 const reverse_iterator<_Iterator>& __y)
164 { return !(__y < __x); }
166 template<typename _Iterator>
168 operator>=(const reverse_iterator<_Iterator>& __x,
169 const reverse_iterator<_Iterator>& __y)
170 { return !(__x < __y); }
172 template<typename _Iterator>
173 inline typename reverse_iterator<_Iterator>::difference_type
174 operator-(const reverse_iterator<_Iterator>& __x,
175 const reverse_iterator<_Iterator>& __y)
176 { return __y.base() - __x.base(); }
178 template<typename _Iterator>
179 inline reverse_iterator<_Iterator>
180 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
181 const reverse_iterator<_Iterator>& __x)
182 { return reverse_iterator<_Iterator>(__x.base() - __n); }
184 // 24.4.2.2.1 back_insert_iterator
185 template<typename _Container>
186 class back_insert_iterator
187 : public iterator<output_iterator_tag, void, void, void, void>
190 _Container* container;
193 typedef _Container container_type;
196 back_insert_iterator(_Container& __x) : container(&__x) {}
198 back_insert_iterator&
199 operator=(const typename _Container::value_type& __value)
201 container->push_back(__value);
205 back_insert_iterator&
206 operator*() { return *this; }
208 back_insert_iterator&
209 operator++() { return *this; }
212 operator++(int) { return *this; }
215 template<typename _Container>
216 inline back_insert_iterator<_Container>
217 back_inserter(_Container& __x)
218 { return back_insert_iterator<_Container>(__x); }
220 template<typename _Container>
221 class front_insert_iterator
222 : public iterator<output_iterator_tag, void, void, void, void>
225 _Container* container;
228 typedef _Container container_type;
230 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
232 front_insert_iterator&
233 operator=(const typename _Container::value_type& __value)
235 container->push_front(__value);
239 front_insert_iterator&
240 operator*() { return *this; }
242 front_insert_iterator&
243 operator++() { return *this; }
245 front_insert_iterator
246 operator++(int) { return *this; }
249 template<typename _Container>
250 inline front_insert_iterator<_Container> front_inserter(_Container& __x)
251 { return front_insert_iterator<_Container>(__x); }
253 template<typename _Container>
254 class insert_iterator
255 : public iterator<output_iterator_tag, void, void, void, void>
258 _Container* container;
259 typename _Container::iterator iter;
262 typedef _Container container_type;
264 insert_iterator(_Container& __x, typename _Container::iterator __i)
265 : container(&__x), iter(__i) {}
268 operator=(const typename _Container::const_reference __value)
270 iter = container->insert(iter, __value);
276 operator*() { return *this; }
279 operator++() { return *this; }
282 operator++(int) { return *this; }
285 template<typename _Container, typename _Iterator>
287 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
289 typedef typename _Container::iterator __iter;
290 return insert_iterator<_Container>(__x, __iter(__i));
293 // This iterator adapter is 'normal' in the sense that it does not
294 // change the semantics of any of the operators of its itererator
295 // parameter. Its primary purpose is to convert an iterator that is
296 // not a class, e.g. a pointer, into an iterator that is a class.
297 // The _Container parameter exists solely so that different containers
298 // using this template can instantiate different types, even if the
299 // _Iterator parameter is the same.
300 template<typename _Iterator, typename _Container>
301 class __normal_iterator
302 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
303 typename iterator_traits<_Iterator>::value_type,
304 typename iterator_traits<_Iterator>::difference_type,
305 typename iterator_traits<_Iterator>::pointer,
306 typename iterator_traits<_Iterator>::reference>
309 _Iterator _M_current;
312 typedef typename iterator_traits<_Iterator>::difference_type
314 typedef typename iterator_traits<_Iterator>::reference reference;
315 typedef typename iterator_traits<_Iterator>::pointer pointer;
317 __normal_iterator() : _M_current(_Iterator()) { }
320 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
322 // Allow iterator to const_iterator conversion
323 template<typename _Iter>
324 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
325 : _M_current(__i.base()) { }
327 // Forward iterator requirements
329 operator*() const { return *_M_current; }
332 operator->() const { return _M_current; }
335 operator++() { ++_M_current; return *this; }
338 operator++(int) { return __normal_iterator(_M_current++); }
340 // Bidirectional iterator requirements
342 operator--() { --_M_current; return *this; }
345 operator--(int) { return __normal_iterator(_M_current--); }
347 // Random access iterator requirements
349 operator[](const difference_type& __n) const
350 { return _M_current[__n]; }
353 operator+=(const difference_type& __n)
354 { _M_current += __n; return *this; }
357 operator+(const difference_type& __n) const
358 { return __normal_iterator(_M_current + __n); }
361 operator-=(const difference_type& __n)
362 { _M_current -= __n; return *this; }
365 operator-(const difference_type& __n) const
366 { return __normal_iterator(_M_current - __n); }
369 operator-(const __normal_iterator& __i) const
370 { return _M_current - __i._M_current; }
373 base() const { return _M_current; }
376 // Forward iterator requirements
377 template<typename _IteratorL, typename _IteratorR, typename _Container>
379 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
380 const __normal_iterator<_IteratorR, _Container>& __rhs)
381 { return __lhs.base() == __rhs.base(); }
383 template<typename _IteratorL, typename _IteratorR, typename _Container>
385 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
386 const __normal_iterator<_IteratorR, _Container>& __rhs)
387 { return !(__lhs == __rhs); }
389 // Random access iterator requirements
390 template<typename _IteratorL, typename _IteratorR, typename _Container>
392 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
393 const __normal_iterator<_IteratorR, _Container>& __rhs)
394 { return __lhs.base() < __rhs.base(); }
396 template<typename _IteratorL, typename _IteratorR, typename _Container>
398 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
399 const __normal_iterator<_IteratorR, _Container>& __rhs)
400 { return __rhs < __lhs; }
402 template<typename _IteratorL, typename _IteratorR, typename _Container>
404 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
405 const __normal_iterator<_IteratorR, _Container>& __rhs)
406 { return !(__rhs < __lhs); }
408 template<typename _IteratorL, typename _IteratorR, typename _Container>
410 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
411 const __normal_iterator<_IteratorR, _Container>& __rhs)
412 { return !(__lhs < __rhs); }
414 template<typename _Iterator, typename _Container>
415 inline __normal_iterator<_Iterator, _Container>
416 operator+(__normal_iterator<_Iterator, _Container>::difference_type __n,
417 const __normal_iterator<_Iterator, _Container>& __i)
418 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }