2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_iterator.h
blobd4ea6577a4986ed0c3597bd83ab8a095edd624b0
1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_iterator.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{iterator}
55 * This file implements reverse_iterator, back_insert_iterator,
56 * front_insert_iterator, insert_iterator, __normal_iterator, and their
57 * supporting functions and overloaded operators.
60 #ifndef _STL_ITERATOR_H
61 #define _STL_ITERATOR_H 1
63 #include <bits/cpp_type_traits.h>
64 #include <ext/type_traits.h>
65 #include <bits/move.h>
66 #include <bits/ptr_traits.h>
68 namespace std _GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72 /**
73 * @addtogroup iterators
74 * @{
77 // 24.4.1 Reverse iterators
78 /**
79 * Bidirectional and random access iterators have corresponding reverse
80 * %iterator adaptors that iterate through the data structure in the
81 * opposite direction. They have the same signatures as the corresponding
82 * iterators. The fundamental relation between a reverse %iterator and its
83 * corresponding %iterator @c i is established by the identity:
84 * @code
85 * &*(reverse_iterator(i)) == &*(i - 1)
86 * @endcode
88 * <em>This mapping is dictated by the fact that while there is always a
89 * pointer past the end of an array, there might not be a valid pointer
90 * before the beginning of an array.</em> [24.4.1]/1,2
92 * Reverse iterators can be tricky and surprising at first. Their
93 * semantics make sense, however, and the trickiness is a side effect of
94 * the requirement that the iterators must be safe.
96 template<typename _Iterator>
97 class reverse_iterator
98 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
99 typename iterator_traits<_Iterator>::value_type,
100 typename iterator_traits<_Iterator>::difference_type,
101 typename iterator_traits<_Iterator>::pointer,
102 typename iterator_traits<_Iterator>::reference>
104 protected:
105 _Iterator current;
107 typedef iterator_traits<_Iterator> __traits_type;
109 public:
110 typedef _Iterator iterator_type;
111 typedef typename __traits_type::difference_type difference_type;
112 typedef typename __traits_type::pointer pointer;
113 typedef typename __traits_type::reference reference;
116 * The default constructor value-initializes member @p current.
117 * If it is a pointer, that means it is zero-initialized.
119 // _GLIBCXX_RESOLVE_LIB_DEFECTS
120 // 235 No specification of default ctor for reverse_iterator
121 reverse_iterator() : current() { }
124 * This %iterator will move in the opposite direction that @p x does.
126 explicit
127 reverse_iterator(iterator_type __x) : current(__x) { }
130 * The copy constructor is normal.
132 reverse_iterator(const reverse_iterator& __x)
133 : current(__x.current) { }
136 * A %reverse_iterator across other types can be copied if the
137 * underlying %iterator can be converted to the type of @c current.
139 template<typename _Iter>
140 reverse_iterator(const reverse_iterator<_Iter>& __x)
141 : current(__x.base()) { }
144 * @return @c current, the %iterator used for underlying work.
146 iterator_type
147 base() const
148 { return current; }
151 * @return A reference to the value at @c --current
153 * This requires that @c --current is dereferenceable.
155 * @warning This implementation requires that for an iterator of the
156 * underlying iterator type, @c x, a reference obtained by
157 * @c *x remains valid after @c x has been modified or
158 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
160 reference
161 operator*() const
163 _Iterator __tmp = current;
164 return *--__tmp;
168 * @return A pointer to the value at @c --current
170 * This requires that @c --current is dereferenceable.
172 pointer
173 operator->() const
174 { return &(operator*()); }
177 * @return @c *this
179 * Decrements the underlying iterator.
181 reverse_iterator&
182 operator++()
184 --current;
185 return *this;
189 * @return The original value of @c *this
191 * Decrements the underlying iterator.
193 reverse_iterator
194 operator++(int)
196 reverse_iterator __tmp = *this;
197 --current;
198 return __tmp;
202 * @return @c *this
204 * Increments the underlying iterator.
206 reverse_iterator&
207 operator--()
209 ++current;
210 return *this;
214 * @return A reverse_iterator with the previous value of @c *this
216 * Increments the underlying iterator.
218 reverse_iterator
219 operator--(int)
221 reverse_iterator __tmp = *this;
222 ++current;
223 return __tmp;
227 * @return A reverse_iterator that refers to @c current - @a __n
229 * The underlying iterator must be a Random Access Iterator.
231 reverse_iterator
232 operator+(difference_type __n) const
233 { return reverse_iterator(current - __n); }
236 * @return *this
238 * Moves the underlying iterator backwards @a __n steps.
239 * The underlying iterator must be a Random Access Iterator.
241 reverse_iterator&
242 operator+=(difference_type __n)
244 current -= __n;
245 return *this;
249 * @return A reverse_iterator that refers to @c current - @a __n
251 * The underlying iterator must be a Random Access Iterator.
253 reverse_iterator
254 operator-(difference_type __n) const
255 { return reverse_iterator(current + __n); }
258 * @return *this
260 * Moves the underlying iterator forwards @a __n steps.
261 * The underlying iterator must be a Random Access Iterator.
263 reverse_iterator&
264 operator-=(difference_type __n)
266 current += __n;
267 return *this;
271 * @return The value at @c current - @a __n - 1
273 * The underlying iterator must be a Random Access Iterator.
275 reference
276 operator[](difference_type __n) const
277 { return *(*this + __n); }
280 //@{
282 * @param __x A %reverse_iterator.
283 * @param __y A %reverse_iterator.
284 * @return A simple bool.
286 * Reverse iterators forward many operations to their underlying base()
287 * iterators. Others are implemented in terms of one another.
290 template<typename _Iterator>
291 inline bool
292 operator==(const reverse_iterator<_Iterator>& __x,
293 const reverse_iterator<_Iterator>& __y)
294 { return __x.base() == __y.base(); }
296 template<typename _Iterator>
297 inline bool
298 operator<(const reverse_iterator<_Iterator>& __x,
299 const reverse_iterator<_Iterator>& __y)
300 { return __y.base() < __x.base(); }
302 template<typename _Iterator>
303 inline bool
304 operator!=(const reverse_iterator<_Iterator>& __x,
305 const reverse_iterator<_Iterator>& __y)
306 { return !(__x == __y); }
308 template<typename _Iterator>
309 inline bool
310 operator>(const reverse_iterator<_Iterator>& __x,
311 const reverse_iterator<_Iterator>& __y)
312 { return __y < __x; }
314 template<typename _Iterator>
315 inline bool
316 operator<=(const reverse_iterator<_Iterator>& __x,
317 const reverse_iterator<_Iterator>& __y)
318 { return !(__y < __x); }
320 template<typename _Iterator>
321 inline bool
322 operator>=(const reverse_iterator<_Iterator>& __x,
323 const reverse_iterator<_Iterator>& __y)
324 { return !(__x < __y); }
326 template<typename _Iterator>
327 inline typename reverse_iterator<_Iterator>::difference_type
328 operator-(const reverse_iterator<_Iterator>& __x,
329 const reverse_iterator<_Iterator>& __y)
330 { return __y.base() - __x.base(); }
332 template<typename _Iterator>
333 inline reverse_iterator<_Iterator>
334 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
335 const reverse_iterator<_Iterator>& __x)
336 { return reverse_iterator<_Iterator>(__x.base() - __n); }
338 // _GLIBCXX_RESOLVE_LIB_DEFECTS
339 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
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.base() == __y.base(); }
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.base() < __x.base(); }
352 template<typename _IteratorL, typename _IteratorR>
353 inline bool
354 operator!=(const reverse_iterator<_IteratorL>& __x,
355 const reverse_iterator<_IteratorR>& __y)
356 { return !(__x == __y); }
358 template<typename _IteratorL, typename _IteratorR>
359 inline bool
360 operator>(const reverse_iterator<_IteratorL>& __x,
361 const reverse_iterator<_IteratorR>& __y)
362 { return __y < __x; }
364 template<typename _IteratorL, typename _IteratorR>
365 inline bool
366 operator<=(const reverse_iterator<_IteratorL>& __x,
367 const reverse_iterator<_IteratorR>& __y)
368 { return !(__y < __x); }
370 template<typename _IteratorL, typename _IteratorR>
371 inline bool
372 operator>=(const reverse_iterator<_IteratorL>& __x,
373 const reverse_iterator<_IteratorR>& __y)
374 { return !(__x < __y); }
376 template<typename _IteratorL, typename _IteratorR>
377 #if __cplusplus >= 201103L
378 // DR 685.
379 inline auto
380 operator-(const reverse_iterator<_IteratorL>& __x,
381 const reverse_iterator<_IteratorR>& __y)
382 -> decltype(__y.base() - __x.base())
383 #else
384 inline typename reverse_iterator<_IteratorL>::difference_type
385 operator-(const reverse_iterator<_IteratorL>& __x,
386 const reverse_iterator<_IteratorR>& __y)
387 #endif
388 { return __y.base() - __x.base(); }
389 //@}
391 #if __cplusplus >= 201103L
392 // Same as C++14 make_reverse_iterator but used in C++03 mode too.
393 template<typename _Iterator>
394 inline reverse_iterator<_Iterator>
395 __make_reverse_iterator(_Iterator __i)
396 { return reverse_iterator<_Iterator>(__i); }
398 # if __cplusplus > 201103L
399 # define __cpp_lib_make_reverse_iterator 201402
401 // _GLIBCXX_RESOLVE_LIB_DEFECTS
402 // DR 2285. make_reverse_iterator
403 /// Generator function for reverse_iterator.
404 template<typename _Iterator>
405 inline reverse_iterator<_Iterator>
406 make_reverse_iterator(_Iterator __i)
407 { return reverse_iterator<_Iterator>(__i); }
408 # endif
409 #endif
411 #if __cplusplus >= 201103L
412 template<typename _Iterator>
413 auto
414 __niter_base(reverse_iterator<_Iterator> __it)
415 -> decltype(__make_reverse_iterator(__niter_base(__it.base())))
416 { return __make_reverse_iterator(__niter_base(__it.base())); }
417 #endif
419 // 24.4.2.2.1 back_insert_iterator
421 * @brief Turns assignment into insertion.
423 * These are output iterators, constructed from a container-of-T.
424 * Assigning a T to the iterator appends it to the container using
425 * push_back.
427 * Tip: Using the back_inserter function to create these iterators can
428 * save typing.
430 template<typename _Container>
431 class back_insert_iterator
432 : public iterator<output_iterator_tag, void, void, void, void>
434 protected:
435 _Container* container;
437 public:
438 /// A nested typedef for the type of whatever container you used.
439 typedef _Container container_type;
441 /// The only way to create this %iterator is with a container.
442 explicit
443 back_insert_iterator(_Container& __x) : container(&__x) { }
446 * @param __value An instance of whatever type
447 * container_type::const_reference is; presumably a
448 * reference-to-const T for container<T>.
449 * @return This %iterator, for chained operations.
451 * This kind of %iterator doesn't really have a @a position in the
452 * container (you can think of the position as being permanently at
453 * the end, if you like). Assigning a value to the %iterator will
454 * always append the value to the end of the container.
456 #if __cplusplus < 201103L
457 back_insert_iterator&
458 operator=(typename _Container::const_reference __value)
460 container->push_back(__value);
461 return *this;
463 #else
464 back_insert_iterator&
465 operator=(const typename _Container::value_type& __value)
467 container->push_back(__value);
468 return *this;
471 back_insert_iterator&
472 operator=(typename _Container::value_type&& __value)
474 container->push_back(std::move(__value));
475 return *this;
477 #endif
479 /// Simply returns *this.
480 back_insert_iterator&
481 operator*()
482 { return *this; }
484 /// Simply returns *this. (This %iterator does not @a move.)
485 back_insert_iterator&
486 operator++()
487 { return *this; }
489 /// Simply returns *this. (This %iterator does not @a move.)
490 back_insert_iterator
491 operator++(int)
492 { return *this; }
496 * @param __x A container of arbitrary type.
497 * @return An instance of back_insert_iterator working on @p __x.
499 * This wrapper function helps in creating back_insert_iterator instances.
500 * Typing the name of the %iterator requires knowing the precise full
501 * type of the container, which can be tedious and impedes generic
502 * programming. Using this function lets you take advantage of automatic
503 * template parameter deduction, making the compiler match the correct
504 * types for you.
506 template<typename _Container>
507 inline back_insert_iterator<_Container>
508 back_inserter(_Container& __x)
509 { return back_insert_iterator<_Container>(__x); }
512 * @brief Turns assignment into insertion.
514 * These are output iterators, constructed from a container-of-T.
515 * Assigning a T to the iterator prepends it to the container using
516 * push_front.
518 * Tip: Using the front_inserter function to create these iterators can
519 * save typing.
521 template<typename _Container>
522 class front_insert_iterator
523 : public iterator<output_iterator_tag, void, void, void, void>
525 protected:
526 _Container* container;
528 public:
529 /// A nested typedef for the type of whatever container you used.
530 typedef _Container container_type;
532 /// The only way to create this %iterator is with a container.
533 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
536 * @param __value An instance of whatever type
537 * container_type::const_reference is; presumably a
538 * reference-to-const T for container<T>.
539 * @return This %iterator, for chained operations.
541 * This kind of %iterator doesn't really have a @a position in the
542 * container (you can think of the position as being permanently at
543 * the front, if you like). Assigning a value to the %iterator will
544 * always prepend the value to the front of the container.
546 #if __cplusplus < 201103L
547 front_insert_iterator&
548 operator=(typename _Container::const_reference __value)
550 container->push_front(__value);
551 return *this;
553 #else
554 front_insert_iterator&
555 operator=(const typename _Container::value_type& __value)
557 container->push_front(__value);
558 return *this;
561 front_insert_iterator&
562 operator=(typename _Container::value_type&& __value)
564 container->push_front(std::move(__value));
565 return *this;
567 #endif
569 /// Simply returns *this.
570 front_insert_iterator&
571 operator*()
572 { return *this; }
574 /// Simply returns *this. (This %iterator does not @a move.)
575 front_insert_iterator&
576 operator++()
577 { return *this; }
579 /// Simply returns *this. (This %iterator does not @a move.)
580 front_insert_iterator
581 operator++(int)
582 { return *this; }
586 * @param __x A container of arbitrary type.
587 * @return An instance of front_insert_iterator working on @p x.
589 * This wrapper function helps in creating front_insert_iterator instances.
590 * Typing the name of the %iterator requires knowing the precise full
591 * type of the container, which can be tedious and impedes generic
592 * programming. Using this function lets you take advantage of automatic
593 * template parameter deduction, making the compiler match the correct
594 * types for you.
596 template<typename _Container>
597 inline front_insert_iterator<_Container>
598 front_inserter(_Container& __x)
599 { return front_insert_iterator<_Container>(__x); }
602 * @brief Turns assignment into insertion.
604 * These are output iterators, constructed from a container-of-T.
605 * Assigning a T to the iterator inserts it in the container at the
606 * %iterator's position, rather than overwriting the value at that
607 * position.
609 * (Sequences will actually insert a @e copy of the value before the
610 * %iterator's position.)
612 * Tip: Using the inserter function to create these iterators can
613 * save typing.
615 template<typename _Container>
616 class insert_iterator
617 : public iterator<output_iterator_tag, void, void, void, void>
619 protected:
620 _Container* container;
621 typename _Container::iterator iter;
623 public:
624 /// A nested typedef for the type of whatever container you used.
625 typedef _Container container_type;
628 * The only way to create this %iterator is with a container and an
629 * initial position (a normal %iterator into the container).
631 insert_iterator(_Container& __x, typename _Container::iterator __i)
632 : container(&__x), iter(__i) {}
635 * @param __value An instance of whatever type
636 * container_type::const_reference is; presumably a
637 * reference-to-const T for container<T>.
638 * @return This %iterator, for chained operations.
640 * This kind of %iterator maintains its own position in the
641 * container. Assigning a value to the %iterator will insert the
642 * value into the container at the place before the %iterator.
644 * The position is maintained such that subsequent assignments will
645 * insert values immediately after one another. For example,
646 * @code
647 * // vector v contains A and Z
649 * insert_iterator i (v, ++v.begin());
650 * i = 1;
651 * i = 2;
652 * i = 3;
654 * // vector v contains A, 1, 2, 3, and Z
655 * @endcode
657 #if __cplusplus < 201103L
658 insert_iterator&
659 operator=(typename _Container::const_reference __value)
661 iter = container->insert(iter, __value);
662 ++iter;
663 return *this;
665 #else
666 insert_iterator&
667 operator=(const typename _Container::value_type& __value)
669 iter = container->insert(iter, __value);
670 ++iter;
671 return *this;
674 insert_iterator&
675 operator=(typename _Container::value_type&& __value)
677 iter = container->insert(iter, std::move(__value));
678 ++iter;
679 return *this;
681 #endif
683 /// Simply returns *this.
684 insert_iterator&
685 operator*()
686 { return *this; }
688 /// Simply returns *this. (This %iterator does not @a move.)
689 insert_iterator&
690 operator++()
691 { return *this; }
693 /// Simply returns *this. (This %iterator does not @a move.)
694 insert_iterator&
695 operator++(int)
696 { return *this; }
700 * @param __x A container of arbitrary type.
701 * @return An instance of insert_iterator working on @p __x.
703 * This wrapper function helps in creating insert_iterator instances.
704 * Typing the name of the %iterator requires knowing the precise full
705 * type of the container, which can be tedious and impedes generic
706 * programming. Using this function lets you take advantage of automatic
707 * template parameter deduction, making the compiler match the correct
708 * types for you.
710 template<typename _Container, typename _Iterator>
711 inline insert_iterator<_Container>
712 inserter(_Container& __x, _Iterator __i)
714 return insert_iterator<_Container>(__x,
715 typename _Container::iterator(__i));
718 // @} group iterators
720 _GLIBCXX_END_NAMESPACE_VERSION
721 } // namespace
723 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
725 _GLIBCXX_BEGIN_NAMESPACE_VERSION
727 // This iterator adapter is @a normal in the sense that it does not
728 // change the semantics of any of the operators of its iterator
729 // parameter. Its primary purpose is to convert an iterator that is
730 // not a class, e.g. a pointer, into an iterator that is a class.
731 // The _Container parameter exists solely so that different containers
732 // using this template can instantiate different types, even if the
733 // _Iterator parameter is the same.
734 using std::iterator_traits;
735 using std::iterator;
736 template<typename _Iterator, typename _Container>
737 class __normal_iterator
739 protected:
740 _Iterator _M_current;
742 typedef iterator_traits<_Iterator> __traits_type;
744 public:
745 typedef _Iterator iterator_type;
746 typedef typename __traits_type::iterator_category iterator_category;
747 typedef typename __traits_type::value_type value_type;
748 typedef typename __traits_type::difference_type difference_type;
749 typedef typename __traits_type::reference reference;
750 typedef typename __traits_type::pointer pointer;
752 _GLIBCXX_CONSTEXPR __normal_iterator() _GLIBCXX_NOEXCEPT
753 : _M_current(_Iterator()) { }
755 explicit
756 __normal_iterator(const _Iterator& __i) _GLIBCXX_NOEXCEPT
757 : _M_current(__i) { }
759 // Allow iterator to const_iterator conversion
760 template<typename _Iter>
761 __normal_iterator(const __normal_iterator<_Iter,
762 typename __enable_if<
763 (std::__are_same<_Iter, typename _Container::pointer>::__value),
764 _Container>::__type>& __i) _GLIBCXX_NOEXCEPT
765 : _M_current(__i.base()) { }
767 // Forward iterator requirements
768 reference
769 operator*() const _GLIBCXX_NOEXCEPT
770 { return *_M_current; }
772 pointer
773 operator->() const _GLIBCXX_NOEXCEPT
774 { return _M_current; }
776 __normal_iterator&
777 operator++() _GLIBCXX_NOEXCEPT
779 ++_M_current;
780 return *this;
783 __normal_iterator
784 operator++(int) _GLIBCXX_NOEXCEPT
785 { return __normal_iterator(_M_current++); }
787 // Bidirectional iterator requirements
788 __normal_iterator&
789 operator--() _GLIBCXX_NOEXCEPT
791 --_M_current;
792 return *this;
795 __normal_iterator
796 operator--(int) _GLIBCXX_NOEXCEPT
797 { return __normal_iterator(_M_current--); }
799 // Random access iterator requirements
800 reference
801 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
802 { return _M_current[__n]; }
804 __normal_iterator&
805 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
806 { _M_current += __n; return *this; }
808 __normal_iterator
809 operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
810 { return __normal_iterator(_M_current + __n); }
812 __normal_iterator&
813 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
814 { _M_current -= __n; return *this; }
816 __normal_iterator
817 operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
818 { return __normal_iterator(_M_current - __n); }
820 const _Iterator&
821 base() const _GLIBCXX_NOEXCEPT
822 { return _M_current; }
825 // Note: In what follows, the left- and right-hand-side iterators are
826 // allowed to vary in types (conceptually in cv-qualification) so that
827 // comparison between cv-qualified and non-cv-qualified iterators be
828 // valid. However, the greedy and unfriendly operators in std::rel_ops
829 // will make overload resolution ambiguous (when in scope) if we don't
830 // provide overloads whose operands are of the same type. Can someone
831 // remind me what generic programming is about? -- Gaby
833 // Forward iterator requirements
834 template<typename _IteratorL, typename _IteratorR, typename _Container>
835 inline bool
836 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
837 const __normal_iterator<_IteratorR, _Container>& __rhs)
838 _GLIBCXX_NOEXCEPT
839 { return __lhs.base() == __rhs.base(); }
841 template<typename _Iterator, typename _Container>
842 inline bool
843 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
844 const __normal_iterator<_Iterator, _Container>& __rhs)
845 _GLIBCXX_NOEXCEPT
846 { return __lhs.base() == __rhs.base(); }
848 template<typename _IteratorL, typename _IteratorR, typename _Container>
849 inline bool
850 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
851 const __normal_iterator<_IteratorR, _Container>& __rhs)
852 _GLIBCXX_NOEXCEPT
853 { return __lhs.base() != __rhs.base(); }
855 template<typename _Iterator, typename _Container>
856 inline bool
857 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
858 const __normal_iterator<_Iterator, _Container>& __rhs)
859 _GLIBCXX_NOEXCEPT
860 { return __lhs.base() != __rhs.base(); }
862 // Random access iterator requirements
863 template<typename _IteratorL, typename _IteratorR, typename _Container>
864 inline bool
865 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
866 const __normal_iterator<_IteratorR, _Container>& __rhs)
867 _GLIBCXX_NOEXCEPT
868 { return __lhs.base() < __rhs.base(); }
870 template<typename _Iterator, typename _Container>
871 inline bool
872 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
873 const __normal_iterator<_Iterator, _Container>& __rhs)
874 _GLIBCXX_NOEXCEPT
875 { return __lhs.base() < __rhs.base(); }
877 template<typename _IteratorL, typename _IteratorR, typename _Container>
878 inline bool
879 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
880 const __normal_iterator<_IteratorR, _Container>& __rhs)
881 _GLIBCXX_NOEXCEPT
882 { return __lhs.base() > __rhs.base(); }
884 template<typename _Iterator, typename _Container>
885 inline bool
886 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
887 const __normal_iterator<_Iterator, _Container>& __rhs)
888 _GLIBCXX_NOEXCEPT
889 { return __lhs.base() > __rhs.base(); }
891 template<typename _IteratorL, typename _IteratorR, typename _Container>
892 inline bool
893 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
894 const __normal_iterator<_IteratorR, _Container>& __rhs)
895 _GLIBCXX_NOEXCEPT
896 { return __lhs.base() <= __rhs.base(); }
898 template<typename _Iterator, typename _Container>
899 inline bool
900 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
901 const __normal_iterator<_Iterator, _Container>& __rhs)
902 _GLIBCXX_NOEXCEPT
903 { return __lhs.base() <= __rhs.base(); }
905 template<typename _IteratorL, typename _IteratorR, typename _Container>
906 inline bool
907 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
908 const __normal_iterator<_IteratorR, _Container>& __rhs)
909 _GLIBCXX_NOEXCEPT
910 { return __lhs.base() >= __rhs.base(); }
912 template<typename _Iterator, typename _Container>
913 inline bool
914 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
915 const __normal_iterator<_Iterator, _Container>& __rhs)
916 _GLIBCXX_NOEXCEPT
917 { return __lhs.base() >= __rhs.base(); }
919 // _GLIBCXX_RESOLVE_LIB_DEFECTS
920 // According to the resolution of DR179 not only the various comparison
921 // operators but also operator- must accept mixed iterator/const_iterator
922 // parameters.
923 template<typename _IteratorL, typename _IteratorR, typename _Container>
924 #if __cplusplus >= 201103L
925 // DR 685.
926 inline auto
927 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
928 const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept
929 -> decltype(__lhs.base() - __rhs.base())
930 #else
931 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
932 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
933 const __normal_iterator<_IteratorR, _Container>& __rhs)
934 #endif
935 { return __lhs.base() - __rhs.base(); }
937 template<typename _Iterator, typename _Container>
938 inline typename __normal_iterator<_Iterator, _Container>::difference_type
939 operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
940 const __normal_iterator<_Iterator, _Container>& __rhs)
941 _GLIBCXX_NOEXCEPT
942 { return __lhs.base() - __rhs.base(); }
944 template<typename _Iterator, typename _Container>
945 inline __normal_iterator<_Iterator, _Container>
946 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
947 __n, const __normal_iterator<_Iterator, _Container>& __i)
948 _GLIBCXX_NOEXCEPT
949 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
951 _GLIBCXX_END_NAMESPACE_VERSION
952 } // namespace
954 namespace std _GLIBCXX_VISIBILITY(default)
956 _GLIBCXX_BEGIN_NAMESPACE_VERSION
958 template<typename _Iterator, typename _Container>
959 _Iterator
960 __niter_base(__gnu_cxx::__normal_iterator<_Iterator, _Container> __it)
961 { return __it.base(); }
963 _GLIBCXX_END_NAMESPACE_VERSION
964 } // namespace
966 #if __cplusplus >= 201103L
968 namespace std _GLIBCXX_VISIBILITY(default)
970 _GLIBCXX_BEGIN_NAMESPACE_VERSION
973 * @addtogroup iterators
974 * @{
977 // 24.4.3 Move iterators
979 * Class template move_iterator is an iterator adapter with the same
980 * behavior as the underlying iterator except that its dereference
981 * operator implicitly converts the value returned by the underlying
982 * iterator's dereference operator to an rvalue reference. Some
983 * generic algorithms can be called with move iterators to replace
984 * copying with moving.
986 template<typename _Iterator>
987 class move_iterator
989 protected:
990 _Iterator _M_current;
992 typedef iterator_traits<_Iterator> __traits_type;
993 typedef typename __traits_type::reference __base_ref;
995 public:
996 typedef _Iterator iterator_type;
997 typedef typename __traits_type::iterator_category iterator_category;
998 typedef typename __traits_type::value_type value_type;
999 typedef typename __traits_type::difference_type difference_type;
1000 // NB: DR 680.
1001 typedef _Iterator pointer;
1002 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1003 // 2106. move_iterator wrapping iterators returning prvalues
1004 typedef typename conditional<is_reference<__base_ref>::value,
1005 typename remove_reference<__base_ref>::type&&,
1006 __base_ref>::type reference;
1008 move_iterator()
1009 : _M_current() { }
1011 explicit
1012 move_iterator(iterator_type __i)
1013 : _M_current(__i) { }
1015 template<typename _Iter>
1016 move_iterator(const move_iterator<_Iter>& __i)
1017 : _M_current(__i.base()) { }
1019 iterator_type
1020 base() const
1021 { return _M_current; }
1023 reference
1024 operator*() const
1025 { return static_cast<reference>(*_M_current); }
1027 pointer
1028 operator->() const
1029 { return _M_current; }
1031 move_iterator&
1032 operator++()
1034 ++_M_current;
1035 return *this;
1038 move_iterator
1039 operator++(int)
1041 move_iterator __tmp = *this;
1042 ++_M_current;
1043 return __tmp;
1046 move_iterator&
1047 operator--()
1049 --_M_current;
1050 return *this;
1053 move_iterator
1054 operator--(int)
1056 move_iterator __tmp = *this;
1057 --_M_current;
1058 return __tmp;
1061 move_iterator
1062 operator+(difference_type __n) const
1063 { return move_iterator(_M_current + __n); }
1065 move_iterator&
1066 operator+=(difference_type __n)
1068 _M_current += __n;
1069 return *this;
1072 move_iterator
1073 operator-(difference_type __n) const
1074 { return move_iterator(_M_current - __n); }
1076 move_iterator&
1077 operator-=(difference_type __n)
1079 _M_current -= __n;
1080 return *this;
1083 reference
1084 operator[](difference_type __n) const
1085 { return std::move(_M_current[__n]); }
1088 // Note: See __normal_iterator operators note from Gaby to understand
1089 // why there are always 2 versions for most of the move_iterator
1090 // operators.
1091 template<typename _IteratorL, typename _IteratorR>
1092 inline bool
1093 operator==(const move_iterator<_IteratorL>& __x,
1094 const move_iterator<_IteratorR>& __y)
1095 { return __x.base() == __y.base(); }
1097 template<typename _Iterator>
1098 inline bool
1099 operator==(const move_iterator<_Iterator>& __x,
1100 const move_iterator<_Iterator>& __y)
1101 { return __x.base() == __y.base(); }
1103 template<typename _IteratorL, typename _IteratorR>
1104 inline bool
1105 operator!=(const move_iterator<_IteratorL>& __x,
1106 const move_iterator<_IteratorR>& __y)
1107 { return !(__x == __y); }
1109 template<typename _Iterator>
1110 inline bool
1111 operator!=(const move_iterator<_Iterator>& __x,
1112 const move_iterator<_Iterator>& __y)
1113 { return !(__x == __y); }
1115 template<typename _IteratorL, typename _IteratorR>
1116 inline bool
1117 operator<(const move_iterator<_IteratorL>& __x,
1118 const move_iterator<_IteratorR>& __y)
1119 { return __x.base() < __y.base(); }
1121 template<typename _Iterator>
1122 inline bool
1123 operator<(const move_iterator<_Iterator>& __x,
1124 const move_iterator<_Iterator>& __y)
1125 { return __x.base() < __y.base(); }
1127 template<typename _IteratorL, typename _IteratorR>
1128 inline bool
1129 operator<=(const move_iterator<_IteratorL>& __x,
1130 const move_iterator<_IteratorR>& __y)
1131 { return !(__y < __x); }
1133 template<typename _Iterator>
1134 inline bool
1135 operator<=(const move_iterator<_Iterator>& __x,
1136 const move_iterator<_Iterator>& __y)
1137 { return !(__y < __x); }
1139 template<typename _IteratorL, typename _IteratorR>
1140 inline bool
1141 operator>(const move_iterator<_IteratorL>& __x,
1142 const move_iterator<_IteratorR>& __y)
1143 { return __y < __x; }
1145 template<typename _Iterator>
1146 inline bool
1147 operator>(const move_iterator<_Iterator>& __x,
1148 const move_iterator<_Iterator>& __y)
1149 { return __y < __x; }
1151 template<typename _IteratorL, typename _IteratorR>
1152 inline bool
1153 operator>=(const move_iterator<_IteratorL>& __x,
1154 const move_iterator<_IteratorR>& __y)
1155 { return !(__x < __y); }
1157 template<typename _Iterator>
1158 inline bool
1159 operator>=(const move_iterator<_Iterator>& __x,
1160 const move_iterator<_Iterator>& __y)
1161 { return !(__x < __y); }
1163 // DR 685.
1164 template<typename _IteratorL, typename _IteratorR>
1165 inline auto
1166 operator-(const move_iterator<_IteratorL>& __x,
1167 const move_iterator<_IteratorR>& __y)
1168 -> decltype(__x.base() - __y.base())
1169 { return __x.base() - __y.base(); }
1171 template<typename _Iterator>
1172 inline auto
1173 operator-(const move_iterator<_Iterator>& __x,
1174 const move_iterator<_Iterator>& __y)
1175 -> decltype(__x.base() - __y.base())
1176 { return __x.base() - __y.base(); }
1178 template<typename _Iterator>
1179 inline move_iterator<_Iterator>
1180 operator+(typename move_iterator<_Iterator>::difference_type __n,
1181 const move_iterator<_Iterator>& __x)
1182 { return __x + __n; }
1184 template<typename _Iterator>
1185 inline move_iterator<_Iterator>
1186 make_move_iterator(_Iterator __i)
1187 { return move_iterator<_Iterator>(__i); }
1189 template<typename _Iterator, typename _ReturnType
1190 = typename conditional<__move_if_noexcept_cond
1191 <typename iterator_traits<_Iterator>::value_type>::value,
1192 _Iterator, move_iterator<_Iterator>>::type>
1193 inline _ReturnType
1194 __make_move_if_noexcept_iterator(_Iterator __i)
1195 { return _ReturnType(__i); }
1197 // @} group iterators
1199 template<typename _Iterator>
1200 auto
1201 __niter_base(move_iterator<_Iterator> __it)
1202 -> decltype(make_move_iterator(__niter_base(__it.base())))
1203 { return make_move_iterator(__niter_base(__it.base())); }
1205 _GLIBCXX_END_NAMESPACE_VERSION
1206 } // namespace
1208 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1209 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1210 std::__make_move_if_noexcept_iterator(_Iter)
1211 #else
1212 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1213 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1214 #endif // C++11
1216 #endif