1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
68 #include <bits/cpp_type_traits.h>
72 // 24.4.1 Reverse iterators
74 * "Bidirectional and random access iterators have corresponding reverse
75 * %iterator adaptors that iterate through the data structure in the
76 * opposite direction. They have the same signatures as the corresponding
77 * iterators. The fundamental relation between a reverse %iterator and its
78 * corresponding %iterator @c i is established by the identity:
80 * &*(reverse_iterator(i)) == &*(i - 1)
83 * This mapping is dictated by the fact that while there is always a
84 * pointer past the end of an array, there might not be a valid pointer
85 * before the beginning of an array." [24.4.1]/1,2
87 * Reverse iterators can be tricky and surprising at first. Their
88 * semantics make sense, however, and the trickiness is a side effect of
89 * the requirement that the iterators must be safe.
91 template<typename _Iterator
>
92 class reverse_iterator
93 : public iterator
<typename iterator_traits
<_Iterator
>::iterator_category
,
94 typename iterator_traits
<_Iterator
>::value_type
,
95 typename iterator_traits
<_Iterator
>::difference_type
,
96 typename iterator_traits
<_Iterator
>::pointer
,
97 typename iterator_traits
<_Iterator
>::reference
>
103 typedef _Iterator iterator_type
;
104 typedef typename iterator_traits
<_Iterator
>::difference_type
106 typedef typename iterator_traits
<_Iterator
>::reference reference
;
107 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
111 * The default constructor default-initializes member @p current.
112 * If it is a pointer, that means it is zero-initialized.
114 // _GLIBCXX_RESOLVE_LIB_DEFECTS
115 // 235 No specification of default ctor for reverse_iterator
116 reverse_iterator() : current() { }
119 * This %iterator will move in the opposite direction that @p x does.
122 reverse_iterator(iterator_type __x
) : current(__x
) { }
125 * The copy constructor is normal.
127 reverse_iterator(const reverse_iterator
& __x
)
128 : current(__x
.current
) { }
131 * A reverse_iterator across other types can be copied in the normal
134 template<typename _Iter
>
135 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
136 : current(__x
.base()) { }
139 * @return @c current, the %iterator used for underlying work.
153 _Iterator __tmp
= current
;
164 { return &(operator*()); }
186 reverse_iterator __tmp
= *this;
208 reverse_iterator
operator--(int)
210 reverse_iterator __tmp
= *this;
221 operator+(difference_type __n
) const
222 { return reverse_iterator(current
- __n
); }
230 operator+=(difference_type __n
)
242 operator-(difference_type __n
) const
243 { return reverse_iterator(current
+ __n
); }
251 operator-=(difference_type __n
)
263 operator[](difference_type __n
) const
264 { return *(*this + __n
); }
269 * @param x A %reverse_iterator.
270 * @param y A %reverse_iterator.
271 * @return A simple bool.
273 * Reverse iterators forward many operations to their underlying base()
274 * iterators. Others are implemented in terms of one another.
277 template<typename _Iterator
>
279 operator==(const reverse_iterator
<_Iterator
>& __x
,
280 const reverse_iterator
<_Iterator
>& __y
)
281 { return __x
.base() == __y
.base(); }
283 template<typename _Iterator
>
285 operator<(const reverse_iterator
<_Iterator
>& __x
,
286 const reverse_iterator
<_Iterator
>& __y
)
287 { return __y
.base() < __x
.base(); }
289 template<typename _Iterator
>
291 operator!=(const reverse_iterator
<_Iterator
>& __x
,
292 const reverse_iterator
<_Iterator
>& __y
)
293 { return !(__x
== __y
); }
295 template<typename _Iterator
>
297 operator>(const reverse_iterator
<_Iterator
>& __x
,
298 const reverse_iterator
<_Iterator
>& __y
)
299 { return __y
< __x
; }
301 template<typename _Iterator
>
303 operator<=(const reverse_iterator
<_Iterator
>& __x
,
304 const reverse_iterator
<_Iterator
>& __y
)
305 { return !(__y
< __x
); }
307 template<typename _Iterator
>
309 operator>=(const reverse_iterator
<_Iterator
>& __x
,
310 const reverse_iterator
<_Iterator
>& __y
)
311 { return !(__x
< __y
); }
313 template<typename _Iterator
>
314 inline typename reverse_iterator
<_Iterator
>::difference_type
315 operator-(const reverse_iterator
<_Iterator
>& __x
,
316 const reverse_iterator
<_Iterator
>& __y
)
317 { return __y
.base() - __x
.base(); }
319 template<typename _Iterator
>
320 inline reverse_iterator
<_Iterator
>
321 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
322 const reverse_iterator
<_Iterator
>& __x
)
323 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
326 // 24.4.2.2.1 back_insert_iterator
328 * @brief Turns assignment into insertion.
330 * These are output iterators, constructed from a container-of-T.
331 * Assigning a T to the iterator appends it to the container using
334 * Tip: Using the back_inserter function to create these iterators can
337 template<typename _Container
>
338 class back_insert_iterator
339 : public iterator
<output_iterator_tag
, void, void, void, void>
342 _Container
* container
;
345 /// A nested typedef for the type of whatever container you used.
346 typedef _Container container_type
;
348 /// The only way to create this %iterator is with a container.
350 back_insert_iterator(_Container
& __x
) : container(&__x
) { }
353 * @param value An instance of whatever type
354 * container_type::const_reference is; presumably a
355 * reference-to-const T for container<T>.
356 * @return This %iterator, for chained operations.
358 * This kind of %iterator doesn't really have a "position" in the
359 * container (you can think of the position as being permanently at
360 * the end, if you like). Assigning a value to the %iterator will
361 * always append the value to the end of the container.
363 back_insert_iterator
&
364 operator=(typename
_Container::const_reference __value
)
366 container
->push_back(__value
);
370 /// Simply returns *this.
371 back_insert_iterator
&
375 /// Simply returns *this. (This %iterator does not "move".)
376 back_insert_iterator
&
380 /// Simply returns *this. (This %iterator does not "move".)
387 * @param x A container of arbitrary type.
388 * @return An instance of back_insert_iterator working on @p x.
390 * This wrapper function helps in creating back_insert_iterator instances.
391 * Typing the name of the %iterator requires knowing the precise full
392 * type of the container, which can be tedious and impedes generic
393 * programming. Using this function lets you take advantage of automatic
394 * template parameter deduction, making the compiler match the correct
397 template<typename _Container
>
398 inline back_insert_iterator
<_Container
>
399 back_inserter(_Container
& __x
)
400 { return back_insert_iterator
<_Container
>(__x
); }
403 * @brief Turns assignment into insertion.
405 * These are output iterators, constructed from a container-of-T.
406 * Assigning a T to the iterator prepends it to the container using
409 * Tip: Using the front_inserter function to create these iterators can
412 template<typename _Container
>
413 class front_insert_iterator
414 : public iterator
<output_iterator_tag
, void, void, void, void>
417 _Container
* container
;
420 /// A nested typedef for the type of whatever container you used.
421 typedef _Container container_type
;
423 /// The only way to create this %iterator is with a container.
424 explicit front_insert_iterator(_Container
& __x
) : container(&__x
) { }
427 * @param value An instance of whatever type
428 * container_type::const_reference is; presumably a
429 * reference-to-const T for container<T>.
430 * @return This %iterator, for chained operations.
432 * This kind of %iterator doesn't really have a "position" in the
433 * container (you can think of the position as being permanently at
434 * the front, if you like). Assigning a value to the %iterator will
435 * always prepend the value to the front of the container.
437 front_insert_iterator
&
438 operator=(typename
_Container::const_reference __value
)
440 container
->push_front(__value
);
444 /// Simply returns *this.
445 front_insert_iterator
&
449 /// Simply returns *this. (This %iterator does not "move".)
450 front_insert_iterator
&
454 /// Simply returns *this. (This %iterator does not "move".)
455 front_insert_iterator
461 * @param x A container of arbitrary type.
462 * @return An instance of front_insert_iterator working on @p x.
464 * This wrapper function helps in creating front_insert_iterator instances.
465 * Typing the name of the %iterator requires knowing the precise full
466 * type of the container, which can be tedious and impedes generic
467 * programming. Using this function lets you take advantage of automatic
468 * template parameter deduction, making the compiler match the correct
471 template<typename _Container
>
472 inline front_insert_iterator
<_Container
>
473 front_inserter(_Container
& __x
)
474 { return front_insert_iterator
<_Container
>(__x
); }
477 * @brief Turns assignment into insertion.
479 * These are output iterators, constructed from a container-of-T.
480 * Assigning a T to the iterator inserts it in the container at the
481 * %iterator's position, rather than overwriting the value at that
484 * (Sequences will actually insert a @e copy of the value before the
485 * %iterator's position.)
487 * Tip: Using the inserter function to create these iterators can
490 template<typename _Container
>
491 class insert_iterator
492 : public iterator
<output_iterator_tag
, void, void, void, void>
495 _Container
* container
;
496 typename
_Container::iterator iter
;
499 /// A nested typedef for the type of whatever container you used.
500 typedef _Container container_type
;
503 * The only way to create this %iterator is with a container and an
504 * initial position (a normal %iterator into the container).
506 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
507 : container(&__x
), iter(__i
) {}
510 * @param value An instance of whatever type
511 * container_type::const_reference is; presumably a
512 * reference-to-const T for container<T>.
513 * @return This %iterator, for chained operations.
515 * This kind of %iterator maintains its own position in the
516 * container. Assigning a value to the %iterator will insert the
517 * value into the container at the place before the %iterator.
519 * The position is maintained such that subsequent assignments will
520 * insert values immediately after one another. For example,
522 * // vector v contains A and Z
524 * insert_iterator i (v, ++v.begin());
529 * // vector v contains A, 1, 2, 3, and Z
533 operator=(const typename
_Container::const_reference __value
)
535 iter
= container
->insert(iter
, __value
);
540 /// Simply returns *this.
545 /// Simply returns *this. (This %iterator does not "move".)
550 /// Simply returns *this. (This %iterator does not "move".)
557 * @param x A container of arbitrary type.
558 * @return An instance of insert_iterator working on @p x.
560 * This wrapper function helps in creating insert_iterator instances.
561 * Typing the name of the %iterator requires knowing the precise full
562 * type of the container, which can be tedious and impedes generic
563 * programming. Using this function lets you take advantage of automatic
564 * template parameter deduction, making the compiler match the correct
567 template<typename _Container
, typename _Iterator
>
568 inline insert_iterator
<_Container
>
569 inserter(_Container
& __x
, _Iterator __i
)
571 return insert_iterator
<_Container
>(__x
,
572 typename
_Container::iterator(__i
));
578 // This iterator adapter is 'normal' in the sense that it does not
579 // change the semantics of any of the operators of its iterator
580 // parameter. Its primary purpose is to convert an iterator that is
581 // not a class, e.g. a pointer, into an iterator that is a class.
582 // The _Container parameter exists solely so that different containers
583 // using this template can instantiate different types, even if the
584 // _Iterator parameter is the same.
585 using std::iterator_traits
;
587 template<typename _Iterator
, typename _Container
>
588 class __normal_iterator
591 _Iterator _M_current
;
594 typedef typename iterator_traits
<_Iterator
>::iterator_category
596 typedef typename iterator_traits
<_Iterator
>::value_type value_type
;
597 typedef typename iterator_traits
<_Iterator
>::difference_type
599 typedef typename iterator_traits
<_Iterator
>::reference reference
;
600 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
602 __normal_iterator() : _M_current(_Iterator()) { }
605 __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
607 // Allow iterator to const_iterator conversion
608 template<typename _Iter
>
609 __normal_iterator(const __normal_iterator
<_Iter
,
610 typename
std::__enable_if
<_Container
,
611 (std::__are_same
<_Iter
,
612 typename
_Container::pointer
>::__value
)
614 : _M_current(__i
.base()) { }
616 // Forward iterator requirements
619 { return *_M_current
; }
623 { return _M_current
; }
634 { return __normal_iterator(_M_current
++); }
636 // Bidirectional iterator requirements
646 { return __normal_iterator(_M_current
--); }
648 // Random access iterator requirements
650 operator[](const difference_type
& __n
) const
651 { return _M_current
[__n
]; }
654 operator+=(const difference_type
& __n
)
655 { _M_current
+= __n
; return *this; }
658 operator+(const difference_type
& __n
) const
659 { return __normal_iterator(_M_current
+ __n
); }
662 operator-=(const difference_type
& __n
)
663 { _M_current
-= __n
; return *this; }
666 operator-(const difference_type
& __n
) const
667 { return __normal_iterator(_M_current
- __n
); }
671 { return _M_current
; }
674 // Note: In what follows, the left- and right-hand-side iterators are
675 // allowed to vary in types (conceptually in cv-qualification) so that
676 // comparaison between cv-qualified and non-cv-qualified iterators be
677 // valid. However, the greedy and unfriendly operators in std::rel_ops
678 // will make overload resolution ambiguous (when in scope) if we don't
679 // provide overloads whose operands are of the same type. Can someone
680 // remind me what generic programming is about? -- Gaby
682 // Forward iterator requirements
683 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
685 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
686 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
687 { return __lhs
.base() == __rhs
.base(); }
689 template<typename _Iterator
, typename _Container
>
691 operator==(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
692 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
693 { return __lhs
.base() == __rhs
.base(); }
695 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
697 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
698 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
699 { return __lhs
.base() != __rhs
.base(); }
701 template<typename _Iterator
, typename _Container
>
703 operator!=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
704 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
705 { return __lhs
.base() != __rhs
.base(); }
707 // Random access iterator requirements
708 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
710 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
711 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
712 { return __lhs
.base() < __rhs
.base(); }
714 template<typename _Iterator
, typename _Container
>
716 operator<(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
717 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
718 { return __lhs
.base() < __rhs
.base(); }
720 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
722 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
723 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
724 { return __lhs
.base() > __rhs
.base(); }
726 template<typename _Iterator
, typename _Container
>
728 operator>(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
729 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
730 { return __lhs
.base() > __rhs
.base(); }
732 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
734 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
735 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
736 { return __lhs
.base() <= __rhs
.base(); }
738 template<typename _Iterator
, typename _Container
>
740 operator<=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
741 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
742 { return __lhs
.base() <= __rhs
.base(); }
744 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
746 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
747 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
748 { return __lhs
.base() >= __rhs
.base(); }
750 template<typename _Iterator
, typename _Container
>
752 operator>=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
753 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
754 { return __lhs
.base() >= __rhs
.base(); }
756 // _GLIBCXX_RESOLVE_LIB_DEFECTS
757 // According to the resolution of DR179 not only the various comparison
758 // operators but also operator- must accept mixed iterator/const_iterator
760 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
761 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
762 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
763 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
764 { return __lhs
.base() - __rhs
.base(); }
766 template<typename _Iterator
, typename _Container
>
767 inline __normal_iterator
<_Iterator
, _Container
>
768 operator+(typename __normal_iterator
<_Iterator
, _Container
>::difference_type
769 __n
, const __normal_iterator
<_Iterator
, _Container
>& __i
)
770 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
771 } // namespace __gnu_cxx