1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_iterator.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
60 * This file implements reverse_iterator, back_insert_iterator,
61 * front_insert_iterator, insert_iterator, __normal_iterator, and their
62 * supporting functions and overloaded operators.
70 // 24.4.1 Reverse iterators
72 * "Bidirectional and random access iterators have corresponding reverse
73 * %iterator adaptors that iterate through the data structure in the
74 * opposite direction. They have the same signatures as the corresponding
75 * iterators. The fundamental relation between a reverse %iterator and its
76 * corresponding %iterator @c i is established by the identity:
78 * &*(reverse_iterator(i)) == &*(i - 1)
81 * This mapping is dictated by the fact that while there is always a
82 * pointer past the end of an array, there might not be a valid pointer
83 * before the beginning of an array." [24.4.1]/1,2
85 * Reverse iterators can be tricky and surprising at first. Their
86 * semantics make sense, however, and the trickiness is a side effect of
87 * the requirement that the iterators must be safe.
89 template<typename _Iterator
>
90 class reverse_iterator
91 : public iterator
<typename iterator_traits
<_Iterator
>::iterator_category
,
92 typename iterator_traits
<_Iterator
>::value_type
,
93 typename iterator_traits
<_Iterator
>::difference_type
,
94 typename iterator_traits
<_Iterator
>::pointer
,
95 typename iterator_traits
<_Iterator
>::reference
>
101 typedef _Iterator iterator_type
;
102 typedef typename iterator_traits
<_Iterator
>::difference_type
104 typedef typename iterator_traits
<_Iterator
>::reference reference
;
105 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
109 * The default constructor default-initializes member @p current.
110 * If it is a pointer, that means it is zero-initialized.
112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
113 // 235 No specification of default ctor for reverse_iterator
114 reverse_iterator() : current() { }
117 * This %iterator will move in the opposite direction that @p x does.
120 reverse_iterator(iterator_type __x
) : current(__x
) { }
123 * The copy constructor is normal.
125 reverse_iterator(const reverse_iterator
& __x
)
126 : current(__x
.current
) { }
129 * A reverse_iterator across other types can be copied in the normal
132 template<typename _Iter
>
133 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
134 : current(__x
.base()) { }
137 * @return @c current, the %iterator used for underlying work.
140 base() const { return current
; }
150 _Iterator __tmp
= current
;
160 operator->() const { return &(operator*()); }
182 reverse_iterator __tmp
= *this;
204 reverse_iterator
operator--(int)
206 reverse_iterator __tmp
= *this;
217 operator+(difference_type __n
) const
218 { return reverse_iterator(current
- __n
); }
226 operator+=(difference_type __n
)
238 operator-(difference_type __n
) const
239 { return reverse_iterator(current
+ __n
); }
247 operator-=(difference_type __n
)
259 operator[](difference_type __n
) const { return *(*this + __n
); }
264 * @param x A %reverse_iterator.
265 * @param y A %reverse_iterator.
266 * @return A simple bool.
268 * Reverse iterators forward many operations to their underlying base()
269 * iterators. Others are implemented in terms of one another.
272 template<typename _Iterator
>
274 operator==(const reverse_iterator
<_Iterator
>& __x
,
275 const reverse_iterator
<_Iterator
>& __y
)
276 { return __x
.base() == __y
.base(); }
278 template<typename _Iterator
>
280 operator<(const reverse_iterator
<_Iterator
>& __x
,
281 const reverse_iterator
<_Iterator
>& __y
)
282 { return __y
.base() < __x
.base(); }
284 template<typename _Iterator
>
286 operator!=(const reverse_iterator
<_Iterator
>& __x
,
287 const reverse_iterator
<_Iterator
>& __y
)
288 { return !(__x
== __y
); }
290 template<typename _Iterator
>
292 operator>(const reverse_iterator
<_Iterator
>& __x
,
293 const reverse_iterator
<_Iterator
>& __y
)
294 { return __y
< __x
; }
296 template<typename _Iterator
>
298 operator<=(const reverse_iterator
<_Iterator
>& __x
,
299 const reverse_iterator
<_Iterator
>& __y
)
300 { return !(__y
< __x
); }
302 template<typename _Iterator
>
304 operator>=(const reverse_iterator
<_Iterator
>& __x
,
305 const reverse_iterator
<_Iterator
>& __y
)
306 { return !(__x
< __y
); }
308 template<typename _Iterator
>
309 inline typename reverse_iterator
<_Iterator
>::difference_type
310 operator-(const reverse_iterator
<_Iterator
>& __x
,
311 const reverse_iterator
<_Iterator
>& __y
)
312 { return __y
.base() - __x
.base(); }
314 template<typename _Iterator
>
315 inline reverse_iterator
<_Iterator
>
316 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
317 const reverse_iterator
<_Iterator
>& __x
)
318 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
321 // 24.4.2.2.1 back_insert_iterator
323 * @brief Turns assignment into insertion.
325 * These are output iterators, constructed from a container-of-T.
326 * Assigning a T to the iterator appends it to the container using
329 * Tip: Using the back_inserter function to create these iterators can
332 template<typename _Container
>
333 class back_insert_iterator
334 : public iterator
<output_iterator_tag
, void, void, void, void>
337 _Container
* container
;
340 /// A nested typedef for the type of whatever container you used.
341 typedef _Container container_type
;
343 /// The only way to create this %iterator is with a container.
345 back_insert_iterator(_Container
& __x
) : container(&__x
) { }
348 * @param value An instance of whatever type
349 * container_type::const_reference is; presumably a
350 * reference-to-const T for container<T>.
351 * @return This %iterator, for chained operations.
353 * This kind of %iterator doesn't really have a "position" in the
354 * container (you can think of the position as being permanently at
355 * the end, if you like). Assigning a value to the %iterator will
356 * always append the value to the end of the container.
358 back_insert_iterator
&
359 operator=(typename
_Container::const_reference __value
)
361 container
->push_back(__value
);
365 /// Simply returns *this.
366 back_insert_iterator
&
367 operator*() { return *this; }
369 /// Simply returns *this. (This %iterator does not "move".)
370 back_insert_iterator
&
371 operator++() { return *this; }
373 /// Simply returns *this. (This %iterator does not "move".)
375 operator++(int) { return *this; }
379 * @param x A container of arbitrary type.
380 * @return An instance of back_insert_iterator working on @p x.
382 * This wrapper function helps in creating back_insert_iterator instances.
383 * Typing the name of the %iterator requires knowing the precise full
384 * type of the container, which can be tedious and impedes generic
385 * programming. Using this function lets you take advantage of automatic
386 * template parameter deduction, making the compiler match the correct
389 template<typename _Container
>
390 inline back_insert_iterator
<_Container
>
391 back_inserter(_Container
& __x
)
392 { return back_insert_iterator
<_Container
>(__x
); }
395 * @brief Turns assignment into insertion.
397 * These are output iterators, constructed from a container-of-T.
398 * Assigning a T to the iterator prepends it to the container using
401 * Tip: Using the front_inserter function to create these iterators can
404 template<typename _Container
>
405 class front_insert_iterator
406 : public iterator
<output_iterator_tag
, void, void, void, void>
409 _Container
* container
;
412 /// A nested typedef for the type of whatever container you used.
413 typedef _Container container_type
;
415 /// The only way to create this %iterator is with a container.
416 explicit front_insert_iterator(_Container
& __x
) : container(&__x
) { }
419 * @param value An instance of whatever type
420 * container_type::const_reference is; presumably a
421 * reference-to-const T for container<T>.
422 * @return This %iterator, for chained operations.
424 * This kind of %iterator doesn't really have a "position" in the
425 * container (you can think of the position as being permanently at
426 * the front, if you like). Assigning a value to the %iterator will
427 * always prepend the value to the front of the container.
429 front_insert_iterator
&
430 operator=(typename
_Container::const_reference __value
)
432 container
->push_front(__value
);
436 /// Simply returns *this.
437 front_insert_iterator
&
438 operator*() { return *this; }
440 /// Simply returns *this. (This %iterator does not "move".)
441 front_insert_iterator
&
442 operator++() { return *this; }
444 /// Simply returns *this. (This %iterator does not "move".)
445 front_insert_iterator
446 operator++(int) { return *this; }
450 * @param x A container of arbitrary type.
451 * @return An instance of front_insert_iterator working on @p x.
453 * This wrapper function helps in creating front_insert_iterator instances.
454 * Typing the name of the %iterator requires knowing the precise full
455 * type of the container, which can be tedious and impedes generic
456 * programming. Using this function lets you take advantage of automatic
457 * template parameter deduction, making the compiler match the correct
460 template<typename _Container
>
461 inline front_insert_iterator
<_Container
>
462 front_inserter(_Container
& __x
)
463 { return front_insert_iterator
<_Container
>(__x
); }
466 * @brief Turns assignment into insertion.
468 * These are output iterators, constructed from a container-of-T.
469 * Assigning a T to the iterator inserts it in the container at the
470 * %iterator's position, rather than overwriting the value at that
473 * (Sequences will actually insert a @e copy of the value before the
474 * %iterator's position.)
476 * Tip: Using the inserter function to create these iterators can
479 template<typename _Container
>
480 class insert_iterator
481 : public iterator
<output_iterator_tag
, void, void, void, void>
484 _Container
* container
;
485 typename
_Container::iterator iter
;
488 /// A nested typedef for the type of whatever container you used.
489 typedef _Container container_type
;
492 * The only way to create this %iterator is with a container and an
493 * initial position (a normal %iterator into the container).
495 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
496 : container(&__x
), iter(__i
) {}
499 * @param value An instance of whatever type
500 * container_type::const_reference is; presumably a
501 * reference-to-const T for container<T>.
502 * @return This %iterator, for chained operations.
504 * This kind of %iterator maintains its own position in the
505 * container. Assigning a value to the %iterator will insert the
506 * value into the container at the place before the %iterator.
508 * The position is maintained such that subsequent assignments will
509 * insert values immediately after one another. For example,
511 * // vector v contains A and Z
513 * insert_iterator i (v, ++v.begin());
518 * // vector v contains A, 1, 2, 3, and Z
522 operator=(const typename
_Container::const_reference __value
)
524 iter
= container
->insert(iter
, __value
);
529 /// Simply returns *this.
531 operator*() { return *this; }
533 /// Simply returns *this. (This %iterator does not "move".)
535 operator++() { return *this; }
537 /// Simply returns *this. (This %iterator does not "move".)
539 operator++(int) { return *this; }
543 * @param x A container of arbitrary type.
544 * @return An instance of insert_iterator working on @p x.
546 * This wrapper function helps in creating insert_iterator instances.
547 * Typing the name of the %iterator requires knowing the precise full
548 * type of the container, which can be tedious and impedes generic
549 * programming. Using this function lets you take advantage of automatic
550 * template parameter deduction, making the compiler match the correct
553 template<typename _Container
, typename _Iterator
>
554 inline insert_iterator
<_Container
>
555 inserter(_Container
& __x
, _Iterator __i
)
557 return insert_iterator
<_Container
>(__x
,
558 typename
_Container::iterator(__i
));
564 // This iterator adapter is 'normal' in the sense that it does not
565 // change the semantics of any of the operators of its iterator
566 // parameter. Its primary purpose is to convert an iterator that is
567 // not a class, e.g. a pointer, into an iterator that is a class.
568 // The _Container parameter exists solely so that different containers
569 // using this template can instantiate different types, even if the
570 // _Iterator parameter is the same.
571 using std::iterator_traits
;
573 template<typename _Iterator
, typename _Container
>
574 class __normal_iterator
577 _Iterator _M_current
;
580 typedef typename iterator_traits
<_Iterator
>::iterator_category
582 typedef typename iterator_traits
<_Iterator
>::value_type value_type
;
583 typedef typename iterator_traits
<_Iterator
>::difference_type
585 typedef typename iterator_traits
<_Iterator
>::reference reference
;
586 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
588 __normal_iterator() : _M_current(_Iterator()) { }
591 __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
593 // Allow iterator to const_iterator conversion
594 template<typename _Iter
>
595 inline __normal_iterator(const __normal_iterator
<_Iter
, _Container
>& __i
)
596 : _M_current(__i
.base()) { }
598 // Forward iterator requirements
600 operator*() const { return *_M_current
; }
603 operator->() const { return _M_current
; }
606 operator++() { ++_M_current
; return *this; }
609 operator++(int) { return __normal_iterator(_M_current
++); }
611 // Bidirectional iterator requirements
613 operator--() { --_M_current
; return *this; }
616 operator--(int) { return __normal_iterator(_M_current
--); }
618 // Random access iterator requirements
620 operator[](const difference_type
& __n
) const
621 { return _M_current
[__n
]; }
624 operator+=(const difference_type
& __n
)
625 { _M_current
+= __n
; return *this; }
628 operator+(const difference_type
& __n
) const
629 { return __normal_iterator(_M_current
+ __n
); }
632 operator-=(const difference_type
& __n
)
633 { _M_current
-= __n
; return *this; }
636 operator-(const difference_type
& __n
) const
637 { return __normal_iterator(_M_current
- __n
); }
640 base() const { return _M_current
; }
643 // Note: In what follows, the left- and right-hand-side iterators are
644 // allowed to vary in types (conceptually in cv-qualification) so that
645 // comparaison between cv-qualified and non-cv-qualified iterators be
646 // valid. However, the greedy and unfriendly operators in std::rel_ops
647 // will make overload resolution ambiguous (when in scope) if we don't
648 // provide overloads whose operands are of the same type. Can someone
649 // remind me what generic programming is about? -- Gaby
651 // Forward iterator requirements
652 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
654 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
655 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
656 { return __lhs
.base() == __rhs
.base(); }
658 template<typename _Iterator
, typename _Container
>
660 operator==(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
661 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
662 { return __lhs
.base() == __rhs
.base(); }
664 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
666 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
667 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
668 { return __lhs
.base() != __rhs
.base(); }
670 template<typename _Iterator
, typename _Container
>
672 operator!=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
673 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
674 { return __lhs
.base() != __rhs
.base(); }
676 // Random access iterator requirements
677 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
679 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
680 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
681 { return __lhs
.base() < __rhs
.base(); }
683 template<typename _Iterator
, typename _Container
>
685 operator<(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
686 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
687 { return __lhs
.base() < __rhs
.base(); }
689 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
691 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
692 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
693 { return __lhs
.base() > __rhs
.base(); }
695 template<typename _Iterator
, typename _Container
>
697 operator>(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
698 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
699 { return __lhs
.base() > __rhs
.base(); }
701 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
703 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
704 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
705 { return __lhs
.base() <= __rhs
.base(); }
707 template<typename _Iterator
, typename _Container
>
709 operator<=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
710 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
711 { return __lhs
.base() <= __rhs
.base(); }
713 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
715 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
716 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
717 { return __lhs
.base() >= __rhs
.base(); }
719 template<typename _Iterator
, typename _Container
>
721 operator>=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
722 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
723 { return __lhs
.base() >= __rhs
.base(); }
725 // _GLIBCXX_RESOLVE_LIB_DEFECTS
726 // According to the resolution of DR179 not only the various comparison
727 // operators but also operator- must accept mixed iterator/const_iterator
729 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
730 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
731 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
732 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
733 { return __lhs
.base() - __rhs
.base(); }
735 template<typename _Iterator
, typename _Container
>
736 inline __normal_iterator
<_Iterator
, _Container
>
737 operator+(typename __normal_iterator
<_Iterator
, _Container
>::difference_type __n
,
738 const __normal_iterator
<_Iterator
, _Container
>& __i
)
739 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
740 } // namespace __gnu_cxx