Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / libstdc++-v3 / include / bits / stl_iterator.h
blob4c4630a06896f849d8d123e221568f7d856789e1
1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32 * Copyright (c) 1994
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.
65 #ifndef _ITERATOR_H
66 #define _ITERATOR_H 1
68 #include <bits/cpp_type_traits.h>
70 namespace std
72 // 24.4.1 Reverse iterators
73 /**
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:
79 * @code
80 * &*(reverse_iterator(i)) == &*(i - 1)
81 * @endcode
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>
99 protected:
100 _Iterator current;
102 public:
103 typedef _Iterator iterator_type;
104 typedef typename iterator_traits<_Iterator>::difference_type
105 difference_type;
106 typedef typename iterator_traits<_Iterator>::reference reference;
107 typedef typename iterator_traits<_Iterator>::pointer pointer;
109 public:
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.
121 explicit
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
132 * fashion.
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.
141 iterator_type
142 base() const
143 { return current; }
146 * @return TODO
148 * @doctodo
150 reference
151 operator*() const
153 _Iterator __tmp = current;
154 return *--__tmp;
158 * @return TODO
160 * @doctodo
162 pointer
163 operator->() const
164 { return &(operator*()); }
167 * @return TODO
169 * @doctodo
171 reverse_iterator&
172 operator++()
174 --current;
175 return *this;
179 * @return TODO
181 * @doctodo
183 reverse_iterator
184 operator++(int)
186 reverse_iterator __tmp = *this;
187 --current;
188 return __tmp;
192 * @return TODO
194 * @doctodo
196 reverse_iterator&
197 operator--()
199 ++current;
200 return *this;
204 * @return TODO
206 * @doctodo
208 reverse_iterator
209 operator--(int)
211 reverse_iterator __tmp = *this;
212 ++current;
213 return __tmp;
217 * @return TODO
219 * @doctodo
221 reverse_iterator
222 operator+(difference_type __n) const
223 { return reverse_iterator(current - __n); }
226 * @return TODO
228 * @doctodo
230 reverse_iterator&
231 operator+=(difference_type __n)
233 current -= __n;
234 return *this;
238 * @return TODO
240 * @doctodo
242 reverse_iterator
243 operator-(difference_type __n) const
244 { return reverse_iterator(current + __n); }
247 * @return TODO
249 * @doctodo
251 reverse_iterator&
252 operator-=(difference_type __n)
254 current += __n;
255 return *this;
259 * @return TODO
261 * @doctodo
263 reference
264 operator[](difference_type __n) const
265 { return *(*this + __n); }
268 //@{
270 * @param x A %reverse_iterator.
271 * @param y A %reverse_iterator.
272 * @return A simple bool.
274 * Reverse iterators forward many operations to their underlying base()
275 * iterators. Others are implemented in terms of one another.
278 template<typename _Iterator>
279 inline bool
280 operator==(const reverse_iterator<_Iterator>& __x,
281 const reverse_iterator<_Iterator>& __y)
282 { return __x.base() == __y.base(); }
284 template<typename _Iterator>
285 inline bool
286 operator<(const reverse_iterator<_Iterator>& __x,
287 const reverse_iterator<_Iterator>& __y)
288 { return __y.base() < __x.base(); }
290 template<typename _Iterator>
291 inline bool
292 operator!=(const reverse_iterator<_Iterator>& __x,
293 const reverse_iterator<_Iterator>& __y)
294 { return !(__x == __y); }
296 template<typename _Iterator>
297 inline bool
298 operator>(const reverse_iterator<_Iterator>& __x,
299 const reverse_iterator<_Iterator>& __y)
300 { return __y < __x; }
302 template<typename _Iterator>
303 inline bool
304 operator<=(const reverse_iterator<_Iterator>& __x,
305 const reverse_iterator<_Iterator>& __y)
306 { return !(__y < __x); }
308 template<typename _Iterator>
309 inline bool
310 operator>=(const reverse_iterator<_Iterator>& __x,
311 const reverse_iterator<_Iterator>& __y)
312 { return !(__x < __y); }
314 template<typename _Iterator>
315 inline typename reverse_iterator<_Iterator>::difference_type
316 operator-(const reverse_iterator<_Iterator>& __x,
317 const reverse_iterator<_Iterator>& __y)
318 { return __y.base() - __x.base(); }
320 template<typename _Iterator>
321 inline reverse_iterator<_Iterator>
322 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
323 const reverse_iterator<_Iterator>& __x)
324 { return reverse_iterator<_Iterator>(__x.base() - __n); }
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
328 template<typename _IteratorL, typename _IteratorR>
329 inline bool
330 operator==(const reverse_iterator<_IteratorL>& __x,
331 const reverse_iterator<_IteratorR>& __y)
332 { return __x.base() == __y.base(); }
334 template<typename _IteratorL, typename _IteratorR>
335 inline bool
336 operator<(const reverse_iterator<_IteratorL>& __x,
337 const reverse_iterator<_IteratorR>& __y)
338 { return __y.base() < __x.base(); }
340 template<typename _IteratorL, typename _IteratorR>
341 inline bool
342 operator!=(const reverse_iterator<_IteratorL>& __x,
343 const reverse_iterator<_IteratorR>& __y)
344 { return !(__x == __y); }
346 template<typename _IteratorL, typename _IteratorR>
347 inline bool
348 operator>(const reverse_iterator<_IteratorL>& __x,
349 const reverse_iterator<_IteratorR>& __y)
350 { return __y < __x; }
352 template<typename _IteratorL, typename _IteratorR>
353 inline bool
354 operator<=(const reverse_iterator<_IteratorL>& __x,
355 const reverse_iterator<_IteratorR>& __y)
356 { return !(__y < __x); }
358 template<typename _IteratorL, typename _IteratorR>
359 inline bool
360 operator>=(const reverse_iterator<_IteratorL>& __x,
361 const reverse_iterator<_IteratorR>& __y)
362 { return !(__x < __y); }
364 template<typename _IteratorL, typename _IteratorR>
365 inline typename reverse_iterator<_IteratorL>::difference_type
366 operator-(const reverse_iterator<_IteratorL>& __x,
367 const reverse_iterator<_IteratorR>& __y)
368 { return __y.base() - __x.base(); }
369 //@}
371 // 24.4.2.2.1 back_insert_iterator
373 * @brief Turns assignment into insertion.
375 * These are output iterators, constructed from a container-of-T.
376 * Assigning a T to the iterator appends it to the container using
377 * push_back.
379 * Tip: Using the back_inserter function to create these iterators can
380 * save typing.
382 template<typename _Container>
383 class back_insert_iterator
384 : public iterator<output_iterator_tag, void, void, void, void>
386 protected:
387 _Container* container;
389 public:
390 /// A nested typedef for the type of whatever container you used.
391 typedef _Container container_type;
393 /// The only way to create this %iterator is with a container.
394 explicit
395 back_insert_iterator(_Container& __x) : container(&__x) { }
398 * @param value An instance of whatever type
399 * container_type::const_reference is; presumably a
400 * reference-to-const T for container<T>.
401 * @return This %iterator, for chained operations.
403 * This kind of %iterator doesn't really have a "position" in the
404 * container (you can think of the position as being permanently at
405 * the end, if you like). Assigning a value to the %iterator will
406 * always append the value to the end of the container.
408 back_insert_iterator&
409 operator=(typename _Container::const_reference __value)
411 container->push_back(__value);
412 return *this;
415 /// Simply returns *this.
416 back_insert_iterator&
417 operator*()
418 { return *this; }
420 /// Simply returns *this. (This %iterator does not "move".)
421 back_insert_iterator&
422 operator++()
423 { return *this; }
425 /// Simply returns *this. (This %iterator does not "move".)
426 back_insert_iterator
427 operator++(int)
428 { return *this; }
432 * @param x A container of arbitrary type.
433 * @return An instance of back_insert_iterator working on @p x.
435 * This wrapper function helps in creating back_insert_iterator instances.
436 * Typing the name of the %iterator requires knowing the precise full
437 * type of the container, which can be tedious and impedes generic
438 * programming. Using this function lets you take advantage of automatic
439 * template parameter deduction, making the compiler match the correct
440 * types for you.
442 template<typename _Container>
443 inline back_insert_iterator<_Container>
444 back_inserter(_Container& __x)
445 { return back_insert_iterator<_Container>(__x); }
448 * @brief Turns assignment into insertion.
450 * These are output iterators, constructed from a container-of-T.
451 * Assigning a T to the iterator prepends it to the container using
452 * push_front.
454 * Tip: Using the front_inserter function to create these iterators can
455 * save typing.
457 template<typename _Container>
458 class front_insert_iterator
459 : public iterator<output_iterator_tag, void, void, void, void>
461 protected:
462 _Container* container;
464 public:
465 /// A nested typedef for the type of whatever container you used.
466 typedef _Container container_type;
468 /// The only way to create this %iterator is with a container.
469 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
472 * @param value An instance of whatever type
473 * container_type::const_reference is; presumably a
474 * reference-to-const T for container<T>.
475 * @return This %iterator, for chained operations.
477 * This kind of %iterator doesn't really have a "position" in the
478 * container (you can think of the position as being permanently at
479 * the front, if you like). Assigning a value to the %iterator will
480 * always prepend the value to the front of the container.
482 front_insert_iterator&
483 operator=(typename _Container::const_reference __value)
485 container->push_front(__value);
486 return *this;
489 /// Simply returns *this.
490 front_insert_iterator&
491 operator*()
492 { return *this; }
494 /// Simply returns *this. (This %iterator does not "move".)
495 front_insert_iterator&
496 operator++()
497 { return *this; }
499 /// Simply returns *this. (This %iterator does not "move".)
500 front_insert_iterator
501 operator++(int)
502 { return *this; }
506 * @param x A container of arbitrary type.
507 * @return An instance of front_insert_iterator working on @p x.
509 * This wrapper function helps in creating front_insert_iterator instances.
510 * Typing the name of the %iterator requires knowing the precise full
511 * type of the container, which can be tedious and impedes generic
512 * programming. Using this function lets you take advantage of automatic
513 * template parameter deduction, making the compiler match the correct
514 * types for you.
516 template<typename _Container>
517 inline front_insert_iterator<_Container>
518 front_inserter(_Container& __x)
519 { return front_insert_iterator<_Container>(__x); }
522 * @brief Turns assignment into insertion.
524 * These are output iterators, constructed from a container-of-T.
525 * Assigning a T to the iterator inserts it in the container at the
526 * %iterator's position, rather than overwriting the value at that
527 * position.
529 * (Sequences will actually insert a @e copy of the value before the
530 * %iterator's position.)
532 * Tip: Using the inserter function to create these iterators can
533 * save typing.
535 template<typename _Container>
536 class insert_iterator
537 : public iterator<output_iterator_tag, void, void, void, void>
539 protected:
540 _Container* container;
541 typename _Container::iterator iter;
543 public:
544 /// A nested typedef for the type of whatever container you used.
545 typedef _Container container_type;
548 * The only way to create this %iterator is with a container and an
549 * initial position (a normal %iterator into the container).
551 insert_iterator(_Container& __x, typename _Container::iterator __i)
552 : container(&__x), iter(__i) {}
555 * @param value An instance of whatever type
556 * container_type::const_reference is; presumably a
557 * reference-to-const T for container<T>.
558 * @return This %iterator, for chained operations.
560 * This kind of %iterator maintains its own position in the
561 * container. Assigning a value to the %iterator will insert the
562 * value into the container at the place before the %iterator.
564 * The position is maintained such that subsequent assignments will
565 * insert values immediately after one another. For example,
566 * @code
567 * // vector v contains A and Z
569 * insert_iterator i (v, ++v.begin());
570 * i = 1;
571 * i = 2;
572 * i = 3;
574 * // vector v contains A, 1, 2, 3, and Z
575 * @endcode
577 insert_iterator&
578 operator=(const typename _Container::const_reference __value)
580 iter = container->insert(iter, __value);
581 ++iter;
582 return *this;
585 /// Simply returns *this.
586 insert_iterator&
587 operator*()
588 { return *this; }
590 /// Simply returns *this. (This %iterator does not "move".)
591 insert_iterator&
592 operator++()
593 { return *this; }
595 /// Simply returns *this. (This %iterator does not "move".)
596 insert_iterator&
597 operator++(int)
598 { return *this; }
602 * @param x A container of arbitrary type.
603 * @return An instance of insert_iterator working on @p x.
605 * This wrapper function helps in creating insert_iterator instances.
606 * Typing the name of the %iterator requires knowing the precise full
607 * type of the container, which can be tedious and impedes generic
608 * programming. Using this function lets you take advantage of automatic
609 * template parameter deduction, making the compiler match the correct
610 * types for you.
612 template<typename _Container, typename _Iterator>
613 inline insert_iterator<_Container>
614 inserter(_Container& __x, _Iterator __i)
616 return insert_iterator<_Container>(__x,
617 typename _Container::iterator(__i));
619 } // namespace std
621 namespace __gnu_cxx
623 // This iterator adapter is 'normal' in the sense that it does not
624 // change the semantics of any of the operators of its iterator
625 // parameter. Its primary purpose is to convert an iterator that is
626 // not a class, e.g. a pointer, into an iterator that is a class.
627 // The _Container parameter exists solely so that different containers
628 // using this template can instantiate different types, even if the
629 // _Iterator parameter is the same.
630 using std::iterator_traits;
631 using std::iterator;
632 template<typename _Iterator, typename _Container>
633 class __normal_iterator
635 protected:
636 _Iterator _M_current;
638 public:
639 typedef typename iterator_traits<_Iterator>::iterator_category
640 iterator_category;
641 typedef typename iterator_traits<_Iterator>::value_type value_type;
642 typedef typename iterator_traits<_Iterator>::difference_type
643 difference_type;
644 typedef typename iterator_traits<_Iterator>::reference reference;
645 typedef typename iterator_traits<_Iterator>::pointer pointer;
647 __normal_iterator() : _M_current(_Iterator()) { }
649 explicit
650 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
652 // Allow iterator to const_iterator conversion
653 template<typename _Iter>
654 __normal_iterator(const __normal_iterator<_Iter,
655 typename std::__enable_if<_Container,
656 (std::__are_same<_Iter,
657 typename _Container::pointer>::__value)
658 >::__type>& __i)
659 : _M_current(__i.base()) { }
661 // Forward iterator requirements
662 reference
663 operator*() const
664 { return *_M_current; }
666 pointer
667 operator->() const
668 { return _M_current; }
670 __normal_iterator&
671 operator++()
673 ++_M_current;
674 return *this;
677 __normal_iterator
678 operator++(int)
679 { return __normal_iterator(_M_current++); }
681 // Bidirectional iterator requirements
682 __normal_iterator&
683 operator--()
685 --_M_current;
686 return *this;
689 __normal_iterator
690 operator--(int)
691 { return __normal_iterator(_M_current--); }
693 // Random access iterator requirements
694 reference
695 operator[](const difference_type& __n) const
696 { return _M_current[__n]; }
698 __normal_iterator&
699 operator+=(const difference_type& __n)
700 { _M_current += __n; return *this; }
702 __normal_iterator
703 operator+(const difference_type& __n) const
704 { return __normal_iterator(_M_current + __n); }
706 __normal_iterator&
707 operator-=(const difference_type& __n)
708 { _M_current -= __n; return *this; }
710 __normal_iterator
711 operator-(const difference_type& __n) const
712 { return __normal_iterator(_M_current - __n); }
714 const _Iterator&
715 base() const
716 { return _M_current; }
719 // Note: In what follows, the left- and right-hand-side iterators are
720 // allowed to vary in types (conceptually in cv-qualification) so that
721 // comparaison between cv-qualified and non-cv-qualified iterators be
722 // valid. However, the greedy and unfriendly operators in std::rel_ops
723 // will make overload resolution ambiguous (when in scope) if we don't
724 // provide overloads whose operands are of the same type. Can someone
725 // remind me what generic programming is about? -- Gaby
727 // Forward iterator requirements
728 template<typename _IteratorL, typename _IteratorR, typename _Container>
729 inline bool
730 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
731 const __normal_iterator<_IteratorR, _Container>& __rhs)
732 { return __lhs.base() == __rhs.base(); }
734 template<typename _Iterator, typename _Container>
735 inline bool
736 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
737 const __normal_iterator<_Iterator, _Container>& __rhs)
738 { return __lhs.base() == __rhs.base(); }
740 template<typename _IteratorL, typename _IteratorR, typename _Container>
741 inline bool
742 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
743 const __normal_iterator<_IteratorR, _Container>& __rhs)
744 { return __lhs.base() != __rhs.base(); }
746 template<typename _Iterator, typename _Container>
747 inline bool
748 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
749 const __normal_iterator<_Iterator, _Container>& __rhs)
750 { return __lhs.base() != __rhs.base(); }
752 // Random access iterator requirements
753 template<typename _IteratorL, typename _IteratorR, typename _Container>
754 inline bool
755 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
756 const __normal_iterator<_IteratorR, _Container>& __rhs)
757 { return __lhs.base() < __rhs.base(); }
759 template<typename _Iterator, typename _Container>
760 inline bool
761 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
762 const __normal_iterator<_Iterator, _Container>& __rhs)
763 { return __lhs.base() < __rhs.base(); }
765 template<typename _IteratorL, typename _IteratorR, typename _Container>
766 inline bool
767 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
768 const __normal_iterator<_IteratorR, _Container>& __rhs)
769 { return __lhs.base() > __rhs.base(); }
771 template<typename _Iterator, typename _Container>
772 inline bool
773 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
774 const __normal_iterator<_Iterator, _Container>& __rhs)
775 { return __lhs.base() > __rhs.base(); }
777 template<typename _IteratorL, typename _IteratorR, typename _Container>
778 inline bool
779 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
780 const __normal_iterator<_IteratorR, _Container>& __rhs)
781 { return __lhs.base() <= __rhs.base(); }
783 template<typename _Iterator, typename _Container>
784 inline bool
785 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
786 const __normal_iterator<_Iterator, _Container>& __rhs)
787 { return __lhs.base() <= __rhs.base(); }
789 template<typename _IteratorL, typename _IteratorR, typename _Container>
790 inline bool
791 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
792 const __normal_iterator<_IteratorR, _Container>& __rhs)
793 { return __lhs.base() >= __rhs.base(); }
795 template<typename _Iterator, typename _Container>
796 inline bool
797 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
798 const __normal_iterator<_Iterator, _Container>& __rhs)
799 { return __lhs.base() >= __rhs.base(); }
801 // _GLIBCXX_RESOLVE_LIB_DEFECTS
802 // According to the resolution of DR179 not only the various comparison
803 // operators but also operator- must accept mixed iterator/const_iterator
804 // parameters.
805 template<typename _IteratorL, typename _IteratorR, typename _Container>
806 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
807 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
808 const __normal_iterator<_IteratorR, _Container>& __rhs)
809 { return __lhs.base() - __rhs.base(); }
811 template<typename _Iterator, typename _Container>
812 inline __normal_iterator<_Iterator, _Container>
813 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
814 __n, const __normal_iterator<_Iterator, _Container>& __i)
815 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
816 } // namespace __gnu_cxx
818 #endif
820 // Local Variables:
821 // mode:C++
822 // End: