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
<_Container
>&
199 operator=(const typename
_Container::value_type
& __value
)
201 container
->push_back(__value
);
205 back_insert_iterator
<_Container
>&
206 operator*() { return *this; }
208 back_insert_iterator
<_Container
>&
209 operator++() { return *this; }
211 back_insert_iterator
<_Container
>&
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
) {}
231 front_insert_iterator
<_Container
>&
232 operator=(const typename
_Container::value_type
& __value
) {
233 container
->push_front(__value
);
236 front_insert_iterator
<_Container
>& operator*() { return *this; }
237 front_insert_iterator
<_Container
>& operator++() { return *this; }
238 front_insert_iterator
<_Container
>& operator++(int) { return *this; }
241 template<typename _Container
>
242 inline front_insert_iterator
<_Container
> front_inserter(_Container
& __x
)
243 { return front_insert_iterator
<_Container
>(__x
); }
245 template<typename _Container
>
246 class insert_iterator
247 : public iterator
<output_iterator_tag
, void, void, void, void>
250 _Container
* container
;
251 typename
_Container::iterator iter
;
254 typedef _Container container_type
;
256 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
257 : container(&__x
), iter(__i
) {}
259 insert_iterator
<_Container
>&
260 operator=(const typename
_Container::value_type
& __value
) {
261 iter
= container
->insert(iter
, __value
);
265 insert_iterator
<_Container
>& operator*() { return *this; }
266 insert_iterator
<_Container
>& operator++() { return *this; }
267 insert_iterator
<_Container
>& operator++(int) { return *this; }
270 template<typename _Container
, typename _Iterator
>
272 insert_iterator
<_Container
> inserter(_Container
& __x
, _Iterator __i
)
274 typedef typename
_Container::iterator __iter
;
275 return insert_iterator
<_Container
>(__x
, __iter(__i
));
279 template<typename _Tp
, typename _CharT
= char,
280 typename _Traits
= char_traits
<_CharT
>, typename _Dist
= ptrdiff_t>
281 class istream_iterator
282 : public iterator
<input_iterator_tag
, _Tp
, _Dist
, const _Tp
*, const _Tp
&>
285 typedef _CharT char_type
;
286 typedef _Traits traits_type
;
287 typedef basic_istream
<_CharT
, _Traits
> istream_type
;
290 istream_type
* _M_stream
;
295 istream_iterator() : _M_stream(0), _M_ok(false) {}
296 istream_iterator(istream_type
& __s
) : _M_stream(&__s
) { _M_read(); }
299 operator*() const { return _M_value
; }
302 operator->() const { return &(operator*()); }
306 { _M_read(); return *this; }
311 istream_iterator __tmp
= *this;
317 _M_equal(const istream_iterator
& __x
) const
318 { return (_M_ok
== __x
._M_ok
) && (!_M_ok
|| _M_stream
== __x
._M_stream
);}
323 _M_ok
= (_M_stream
&& *_M_stream
) ? true : false;
326 *_M_stream
>> _M_value
;
327 _M_ok
= *_M_stream
? true : false;
332 template<typename _Tp
, typename _CharT
, typename _Traits
, typename _Dist
>
334 operator==(const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __x
,
335 const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __y
)
336 { return __x
._M_equal(__y
); }
338 template <class _Tp
, class _CharT
, class _Traits
, class _Dist
>
340 operator!=(const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __x
,
341 const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __y
)
342 { return !__x
._M_equal(__y
); }
345 template<typename _Tp
, typename _CharT
= char,
346 typename _Traits
= char_traits
<_CharT
> >
347 class ostream_iterator
348 : public iterator
<output_iterator_tag
, void, void, void, void>
351 typedef _CharT char_type
;
352 typedef _Traits traits_type
;
353 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
356 ostream_type
* _M_stream
;
357 const _CharT
* _M_string
;
360 ostream_iterator(ostream_type
& __s
) : _M_stream(&__s
), _M_string(0) {}
361 ostream_iterator(ostream_type
& __s
, const _CharT
* __c
)
362 : _M_stream(&__s
), _M_string(__c
) { }
365 operator=(const _Tp
& __value
)
367 *_M_stream
<< __value
;
368 if (_M_string
) *_M_stream
<< _M_string
;
373 operator*() { return *this; }
376 operator++() { return *this; }
379 operator++(int) { return *this; }
383 // This iterator adapter is 'normal' in the sense that it does not
384 // change the semantics of any of the operators of its itererator
385 // parameter. Its primary purpose is to convert an iterator that is
386 // not a class, e.g. a pointer, into an iterator that is a class.
387 // The _Container parameter exists solely so that different containers
388 // using this template can instantiate different types, even if the
389 // _Iterator parameter is the same.
390 template<typename _Iterator
, typename _Container
>
391 class __normal_iterator
392 : public iterator
<typename iterator_traits
<_Iterator
>::iterator_category
,
393 typename iterator_traits
<_Iterator
>::value_type
,
394 typename iterator_traits
<_Iterator
>::difference_type
,
395 typename iterator_traits
<_Iterator
>::pointer
,
396 typename iterator_traits
<_Iterator
>::reference
>
399 _Iterator _M_current
;
402 typedef typename iterator_traits
<_Iterator
>::difference_type
404 typedef typename iterator_traits
<_Iterator
>::reference reference
;
405 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
407 __normal_iterator() : _M_current(_Iterator()) { }
410 __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
412 // Allow iterator to const_iterator conversion
413 template<typename _Iter
>
414 inline __normal_iterator(const __normal_iterator
<_Iter
, _Container
>& __i
)
415 : _M_current(__i
.base()) { }
417 // Forward iterator requirements
419 operator*() const { return *_M_current
; }
422 operator->() const { return _M_current
; }
425 operator++() { ++_M_current
; return *this; }
428 operator++(int) { return __normal_iterator(_M_current
++); }
430 // Bidirectional iterator requirements
432 operator--() { --_M_current
; return *this; }
435 operator--(int) { return __normal_iterator(_M_current
--); }
437 // Random access iterator requirements
439 operator[](const difference_type
& __n
) const
440 { return _M_current
[__n
]; }
443 operator+=(const difference_type
& __n
)
444 { _M_current
+= __n
; return *this; }
447 operator+(const difference_type
& __n
) const
448 { return __normal_iterator(_M_current
+ __n
); }
451 operator-=(const difference_type
& __n
)
452 { _M_current
-= __n
; return *this; }
455 operator-(const difference_type
& __n
) const
456 { return __normal_iterator(_M_current
- __n
); }
459 operator-(const __normal_iterator
& __i
) const
460 { return _M_current
- __i
._M_current
; }
463 base() const { return _M_current
; }
466 // Forward iterator requirements
467 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
469 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
470 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
471 { return __lhs
.base() == __rhs
.base(); }
473 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
475 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
476 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
477 { return !(__lhs
== __rhs
); }
479 // Random access iterator requirements
480 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
482 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
483 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
484 { return __lhs
.base() < __rhs
.base(); }
486 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
488 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
489 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
490 { return __rhs
< __lhs
; }
492 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
494 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
495 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
496 { return !(__rhs
< __lhs
); }
498 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
500 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
501 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
502 { return !(__lhs
< __rhs
); }
504 template<typename _Iterator
, typename _Container
>
505 inline __normal_iterator
<_Iterator
, _Container
>
506 operator+(__normal_iterator
<_Iterator
, _Container
>::difference_type __n
,
507 const __normal_iterator
<_Iterator
, _Container
>& __i
)
508 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }