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<class _Iterator
>
38 class reverse_iterator
39 : public iterator
<iterator_traits
<_Iterator
>::iterator_category
,
40 iterator_traits
<_Iterator
>::value_type
,
41 iterator_traits
<_Iterator
>::difference_type
,
42 iterator_traits
<_Iterator
>::pointer
,
43 iterator_traits
<_Iterator
>::reference
>
49 typedef iterator_traits
<_Iterator
> __traits_type
;
50 typedef typename
__traits_type::iterator_category iterator_category
;
51 typedef typename
__traits_type::value_type value_type
;
52 typedef typename
__traits_type::difference_type difference_type
;
53 typedef typename
__traits_type::pointer pointer
;
54 typedef typename
__traits_type::reference reference
;
56 typedef _Iterator iterator_type
;
57 typedef reverse_iterator
<_Iterator
> _Self
;
63 reverse_iterator(iterator_type __x
) : current(__x
) {}
65 reverse_iterator(const _Self
& __x
) : current(__x
.current
) {}
67 template <class _Iter
>
68 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
69 : current(__x
.base()) {}
72 base() const { return current
; }
77 _Iterator __tmp
= current
;
82 operator->() const { return &(operator*()); }
106 _Self
operator--(int)
114 operator+(difference_type __n
) const
115 { return _Self(current
- __n
); }
118 operator+=(difference_type __n
)
125 operator-(difference_type __n
) const
126 { return _Self(current
+ __n
); }
129 operator-=(difference_type __n
)
136 operator[](difference_type __n
) const { return *(*this + __n
); }
139 template<class _Iterator
>
141 operator==(const reverse_iterator
<_Iterator
>& __x
,
142 const reverse_iterator
<_Iterator
>& __y
)
143 { return __x
.base() == __y
.base(); }
145 template <class _Iterator
>
147 operator<(const reverse_iterator
<_Iterator
>& __x
,
148 const reverse_iterator
<_Iterator
>& __y
)
149 { return __y
.base() < __x
.base(); }
151 template <class _Iterator
>
153 operator!=(const reverse_iterator
<_Iterator
>& __x
,
154 const reverse_iterator
<_Iterator
>& __y
)
155 { return !(__x
== __y
); }
157 template <class _Iterator
>
159 operator>(const reverse_iterator
<_Iterator
>& __x
,
160 const reverse_iterator
<_Iterator
>& __y
)
161 { return __y
< __x
; }
163 template <class _Iterator
>
165 operator<=(const reverse_iterator
<_Iterator
>& __x
,
166 const reverse_iterator
<_Iterator
>& __y
)
167 { return !(__y
< __x
); }
169 template <class _Iterator
>
171 operator>=(const reverse_iterator
<_Iterator
>& __x
,
172 const reverse_iterator
<_Iterator
>& __y
)
173 { return !(__x
< __y
); }
175 template<class _Iterator
>
176 inline typename reverse_iterator
<_Iterator
>::difference_type
177 operator-(const reverse_iterator
<_Iterator
>& __x
,
178 const reverse_iterator
<_Iterator
>& __y
)
179 { return __y
.base() - __x
.base(); }
181 template <class _Iterator
>
182 inline reverse_iterator
<_Iterator
>
183 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
184 const reverse_iterator
<_Iterator
>& __x
)
185 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
187 // 24.4.2.2.1 back_insert_iterator
188 template <class _Container
>
189 class back_insert_iterator
190 : public iterator
<output_iterator_tag
, void, void, void, void>
193 _Container
* container
;
196 typedef _Container container_type
;
199 back_insert_iterator(_Container
& __x
) : container(&__x
) {}
201 back_insert_iterator
<_Container
>&
202 operator=(const typename
_Container::value_type
& __value
)
204 container
->push_back(__value
);
208 back_insert_iterator
<_Container
>&
209 operator*() { return *this; }
211 back_insert_iterator
<_Container
>&
212 operator++() { return *this; }
214 back_insert_iterator
<_Container
>&
215 operator++(int) { return *this; }
218 template <class _Container
>
219 inline back_insert_iterator
<_Container
>
220 back_inserter(_Container
& __x
)
221 { return back_insert_iterator
<_Container
>(__x
); }
223 template <class _Container
>
224 class front_insert_iterator
225 : public iterator
<output_iterator_tag
, void, void, void, void>
228 _Container
* container
;
231 typedef _Container container_type
;
233 explicit front_insert_iterator(_Container
& __x
) : container(&__x
) {}
234 front_insert_iterator
<_Container
>&
235 operator=(const typename
_Container::value_type
& __value
) {
236 container
->push_front(__value
);
239 front_insert_iterator
<_Container
>& operator*() { return *this; }
240 front_insert_iterator
<_Container
>& operator++() { return *this; }
241 front_insert_iterator
<_Container
>& operator++(int) { return *this; }
244 template <class _Container
>
245 inline front_insert_iterator
<_Container
> front_inserter(_Container
& __x
) {
246 return front_insert_iterator
<_Container
>(__x
);
249 template <class _Container
>
250 class insert_iterator
251 : public iterator
<output_iterator_tag
, void, void, void, void>
254 _Container
* container
;
255 typename
_Container::iterator iter
;
258 typedef _Container container_type
;
260 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
261 : container(&__x
), iter(__i
) {}
263 insert_iterator
<_Container
>&
264 operator=(const typename
_Container::value_type
& __value
) {
265 iter
= container
->insert(iter
, __value
);
269 insert_iterator
<_Container
>& operator*() { return *this; }
270 insert_iterator
<_Container
>& operator++() { return *this; }
271 insert_iterator
<_Container
>& operator++(int) { return *this; }
274 template <class _Container
, class _Iterator
>
276 insert_iterator
<_Container
> inserter(_Container
& __x
, _Iterator __i
)
278 typedef typename
_Container::iterator __iter
;
279 return insert_iterator
<_Container
>(__x
, __iter(__i
));
282 template <class _BidirectionalIterator
, class _Tp
, class _Reference
= _Tp
&,
283 class _Distance
= ptrdiff_t>
284 class reverse_bidirectional_iterator
{
285 typedef reverse_bidirectional_iterator
<_BidirectionalIterator
, _Tp
,
286 _Reference
, _Distance
> _Self
;
288 _BidirectionalIterator current
;
290 typedef bidirectional_iterator_tag iterator_category
;
291 typedef _Tp value_type
;
292 typedef _Distance difference_type
;
293 typedef _Tp
* pointer
;
294 typedef _Reference reference
;
296 reverse_bidirectional_iterator() {}
297 explicit reverse_bidirectional_iterator(_BidirectionalIterator __x
)
299 _BidirectionalIterator
base() const { return current
; }
300 _Reference
operator*() const {
301 _BidirectionalIterator __tmp
= current
;
304 pointer
operator->() const { return &(operator*()); }
305 _Self
& operator++() {
309 _Self
operator++(int) {
314 _Self
& operator--() {
318 _Self
operator--(int) {
325 template <class _BiIter
, class _Tp
, class _Ref
, class _Distance
>
326 inline bool operator==(
327 const reverse_bidirectional_iterator
<_BiIter
, _Tp
, _Ref
, _Distance
>& __x
,
328 const reverse_bidirectional_iterator
<_BiIter
, _Tp
, _Ref
, _Distance
>& __y
)
330 return __x
.base() == __y
.base();
333 template <class _BiIter
, class _Tp
, class _Ref
, class _Distance
>
334 inline bool operator!=(
335 const reverse_bidirectional_iterator
<_BiIter
, _Tp
, _Ref
, _Distance
>& __x
,
336 const reverse_bidirectional_iterator
<_BiIter
, _Tp
, _Ref
, _Distance
>& __y
)
338 return !(__x
== __y
);
344 class _CharT
= char, class _Traits
= char_traits
<_CharT
>,
345 class _Dist
= ptrdiff_t>
346 class istream_iterator
{
348 typedef _CharT char_type
;
349 typedef _Traits traits_type
;
350 typedef basic_istream
<_CharT
, _Traits
> istream_type
;
352 typedef input_iterator_tag iterator_category
;
353 typedef _Tp value_type
;
354 typedef _Dist difference_type
;
355 typedef const _Tp
* pointer
;
356 typedef const _Tp
& reference
;
358 istream_iterator() : _M_stream(0), _M_ok(false) {}
359 istream_iterator(istream_type
& __s
) : _M_stream(&__s
) { _M_read(); }
361 reference
operator*() const { return _M_value
; }
362 pointer
operator->() const { return &(operator*()); }
364 istream_iterator
& operator++() {
368 istream_iterator
operator++(int) {
369 istream_iterator __tmp
= *this;
374 bool _M_equal(const istream_iterator
& __x
) const
375 { return (_M_ok
== __x
._M_ok
) && (!_M_ok
|| _M_stream
== __x
._M_stream
); }
378 istream_type
* _M_stream
;
383 _M_ok
= (_M_stream
&& *_M_stream
) ? true : false;
385 *_M_stream
>> _M_value
;
386 _M_ok
= *_M_stream
? true : false;
391 template <class _Tp
, class _CharT
, class _Traits
, class _Dist
>
393 operator==(const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __x
,
394 const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __y
) {
395 return __x
._M_equal(__y
);
398 template <class _Tp
, class _CharT
, class _Traits
, class _Dist
>
400 operator!=(const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __x
,
401 const istream_iterator
<_Tp
, _CharT
, _Traits
, _Dist
>& __y
) {
402 return !__x
._M_equal(__y
);
407 class _CharT
= char, class _Traits
= char_traits
<_CharT
> >
408 class ostream_iterator
{
410 typedef _CharT char_type
;
411 typedef _Traits traits_type
;
412 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
414 typedef output_iterator_tag iterator_category
;
415 typedef void value_type
;
416 typedef void difference_type
;
417 typedef void pointer
;
418 typedef void reference
;
420 ostream_iterator(ostream_type
& __s
) : _M_stream(&__s
), _M_string(0) {}
421 ostream_iterator(ostream_type
& __s
, const _CharT
* __c
)
422 : _M_stream(&__s
), _M_string(__c
) {}
423 ostream_iterator
<_Tp
>& operator=(const _Tp
& __value
) {
424 *_M_stream
<< __value
;
425 if (_M_string
) *_M_stream
<< _M_string
;
428 ostream_iterator
<_Tp
>& operator*() { return *this; }
429 ostream_iterator
<_Tp
>& operator++() { return *this; }
430 ostream_iterator
<_Tp
>& operator++(int) { return *this; }
432 ostream_type
* _M_stream
;
433 const _CharT
* _M_string
;
437 // This iterator adapter is 'normal' in the sense that it does not
438 // change the semantics of any of the operators of its itererator
439 // parameter. Its primary purpose is to convert an iterator that is
440 // not a class, e.g. a pointer, into an iterator that is a class.
441 // The _Container parameter exists solely so that different containers
442 // using this template can instantiate different types, even if the
443 // _Iterator parameter is the same.
444 template<typename _Iterator
, typename _Container
>
445 class __normal_iterator
446 : public iterator
<iterator_traits
<_Iterator
>::iterator_category
,
447 iterator_traits
<_Iterator
>::value_type
,
448 iterator_traits
<_Iterator
>::difference_type
,
449 iterator_traits
<_Iterator
>::pointer
,
450 iterator_traits
<_Iterator
>::reference
>
454 _Iterator _M_current
;
457 typedef __normal_iterator
<_Iterator
, _Container
> normal_iterator_type
;
459 __normal_iterator() : _M_current(_Iterator()) { }
461 explicit __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
463 // Allow iterator to const_iterator conversion
464 template<typename _Iter
>
465 inline __normal_iterator(const __normal_iterator
<_Iter
, _Container
>& __i
)
466 : _M_current(__i
.base()) { }
468 // Forward iterator requirements
470 operator*() const { return *_M_current
; }
473 operator->() const { return _M_current
; }
475 normal_iterator_type
&
476 operator++() { ++_M_current
; return *this; }
479 operator++(int) { return __normal_iterator(_M_current
++); }
481 // Bidirectional iterator requirements
482 normal_iterator_type
&
483 operator--() { --_M_current
; return *this; }
486 operator--(int) { return __normal_iterator(_M_current
--); }
488 // Random access iterator requirements
490 operator[](const difference_type
& __n
) const
491 { return _M_current
[__n
]; }
493 normal_iterator_type
&
494 operator+=(const difference_type
& __n
)
495 { _M_current
+= __n
; return *this; }
498 operator+(const difference_type
& __n
) const
499 { return __normal_iterator(_M_current
+ __n
); }
501 normal_iterator_type
&
502 operator-=(const difference_type
& __n
)
503 { _M_current
-= __n
; return *this; }
506 operator-(const difference_type
& __n
) const
507 { return __normal_iterator(_M_current
- __n
); }
510 operator-(const normal_iterator_type
& __i
) const
511 { return _M_current
- __i
._M_current
; }
514 base() const { return _M_current
; }
517 // forward iterator requirements
519 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
521 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
522 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
523 { return __lhs
.base() == __rhs
.base(); }
525 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
527 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
528 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
529 { return !(__lhs
== __rhs
); }
531 // random access iterator requirements
533 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
535 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
536 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
537 { return __lhs
.base() < __rhs
.base(); }
539 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
541 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
542 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
543 { return __rhs
< __lhs
; }
545 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
547 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
548 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
549 { return !(__rhs
< __lhs
); }
551 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
553 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
554 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
555 { return !(__lhs
< __rhs
); }
557 template<typename _Iterator
, typename _Container
>
558 inline __normal_iterator
<_Iterator
, _Container
>
559 operator+(__normal_iterator
<_Iterator
, _Container
>::difference_type __n
,
560 const __normal_iterator
<_Iterator
, _Container
>& __i
)
561 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
565 #endif /* __SGI_STL_INTERNAL_ITERATOR_H */