138fd1498Szrj // Iterators -*- C++ -*- 238fd1498Szrj 338fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc. 438fd1498Szrj // 538fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 638fd1498Szrj // software; you can redistribute it and/or modify it under the 738fd1498Szrj // terms of the GNU General Public License as published by the 838fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 938fd1498Szrj // any later version. 1038fd1498Szrj 1138fd1498Szrj // This library is distributed in the hope that it will be useful, 1238fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 1338fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1438fd1498Szrj // GNU General Public License for more details. 1538fd1498Szrj 1638fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 1738fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 1838fd1498Szrj // 3.1, as published by the Free Software Foundation. 1938fd1498Szrj 2038fd1498Szrj // You should have received a copy of the GNU General Public License and 2138fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 2238fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 2338fd1498Szrj // <http://www.gnu.org/licenses/>. 2438fd1498Szrj 2538fd1498Szrj /* 2638fd1498Szrj * 2738fd1498Szrj * Copyright (c) 1994 2838fd1498Szrj * Hewlett-Packard Company 2938fd1498Szrj * 3038fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 3138fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 3238fd1498Szrj * provided that the above copyright notice appear in all copies and 3338fd1498Szrj * that both that copyright notice and this permission notice appear 3438fd1498Szrj * in supporting documentation. Hewlett-Packard Company makes no 3538fd1498Szrj * representations about the suitability of this software for any 3638fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 3738fd1498Szrj * 3838fd1498Szrj * 3938fd1498Szrj * Copyright (c) 1996-1998 4038fd1498Szrj * Silicon Graphics Computer Systems, Inc. 4138fd1498Szrj * 4238fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 4338fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 4438fd1498Szrj * provided that the above copyright notice appear in all copies and 4538fd1498Szrj * that both that copyright notice and this permission notice appear 4638fd1498Szrj * in supporting documentation. Silicon Graphics makes no 4738fd1498Szrj * representations about the suitability of this software for any 4838fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 4938fd1498Szrj */ 5038fd1498Szrj 5138fd1498Szrj /** @file bits/stl_iterator.h 5238fd1498Szrj * This is an internal header file, included by other library headers. 5338fd1498Szrj * Do not attempt to use it directly. @headername{iterator} 5438fd1498Szrj * 5538fd1498Szrj * This file implements reverse_iterator, back_insert_iterator, 5638fd1498Szrj * front_insert_iterator, insert_iterator, __normal_iterator, and their 5738fd1498Szrj * supporting functions and overloaded operators. 5838fd1498Szrj */ 5938fd1498Szrj 6038fd1498Szrj #ifndef _STL_ITERATOR_H 6138fd1498Szrj #define _STL_ITERATOR_H 1 6238fd1498Szrj 6338fd1498Szrj #include <bits/cpp_type_traits.h> 6438fd1498Szrj #include <ext/type_traits.h> 6538fd1498Szrj #include <bits/move.h> 6638fd1498Szrj #include <bits/ptr_traits.h> 6738fd1498Szrj 6838fd1498Szrj #if __cplusplus > 201402L 6938fd1498Szrj # define __cpp_lib_array_constexpr 201603 7038fd1498Szrj #endif 7138fd1498Szrj 7238fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 7338fd1498Szrj { 7438fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 7538fd1498Szrj 7638fd1498Szrj /** 7738fd1498Szrj * @addtogroup iterators 7838fd1498Szrj * @{ 7938fd1498Szrj */ 8038fd1498Szrj 8138fd1498Szrj // 24.4.1 Reverse iterators 8238fd1498Szrj /** 8338fd1498Szrj * Bidirectional and random access iterators have corresponding reverse 8438fd1498Szrj * %iterator adaptors that iterate through the data structure in the 8538fd1498Szrj * opposite direction. They have the same signatures as the corresponding 8638fd1498Szrj * iterators. The fundamental relation between a reverse %iterator and its 8738fd1498Szrj * corresponding %iterator @c i is established by the identity: 8838fd1498Szrj * @code 8938fd1498Szrj * &*(reverse_iterator(i)) == &*(i - 1) 9038fd1498Szrj * @endcode 9138fd1498Szrj * 9238fd1498Szrj * <em>This mapping is dictated by the fact that while there is always a 9338fd1498Szrj * pointer past the end of an array, there might not be a valid pointer 9438fd1498Szrj * before the beginning of an array.</em> [24.4.1]/1,2 9538fd1498Szrj * 9638fd1498Szrj * Reverse iterators can be tricky and surprising at first. Their 9738fd1498Szrj * semantics make sense, however, and the trickiness is a side effect of 9838fd1498Szrj * the requirement that the iterators must be safe. 9938fd1498Szrj */ 10038fd1498Szrj template<typename _Iterator> 10138fd1498Szrj class reverse_iterator 10238fd1498Szrj : public iterator<typename iterator_traits<_Iterator>::iterator_category, 10338fd1498Szrj typename iterator_traits<_Iterator>::value_type, 10438fd1498Szrj typename iterator_traits<_Iterator>::difference_type, 10538fd1498Szrj typename iterator_traits<_Iterator>::pointer, 10638fd1498Szrj typename iterator_traits<_Iterator>::reference> 10738fd1498Szrj { 10838fd1498Szrj protected: 10938fd1498Szrj _Iterator current; 11038fd1498Szrj 11138fd1498Szrj typedef iterator_traits<_Iterator> __traits_type; 11238fd1498Szrj 11338fd1498Szrj public: 11438fd1498Szrj typedef _Iterator iterator_type; 11538fd1498Szrj typedef typename __traits_type::difference_type difference_type; 11638fd1498Szrj typedef typename __traits_type::pointer pointer; 11738fd1498Szrj typedef typename __traits_type::reference reference; 11838fd1498Szrj 11938fd1498Szrj /** 12038fd1498Szrj * The default constructor value-initializes member @p current. 12138fd1498Szrj * If it is a pointer, that means it is zero-initialized. 12238fd1498Szrj */ 12338fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 12438fd1498Szrj // 235 No specification of default ctor for reverse_iterator 125*58e805e6Szrj // 1012. reverse_iterator default ctor should value initialize 12638fd1498Szrj _GLIBCXX17_CONSTEXPR 12738fd1498Szrj reverse_iterator() : current() { } 12838fd1498Szrj 12938fd1498Szrj /** 13038fd1498Szrj * This %iterator will move in the opposite direction that @p x does. 13138fd1498Szrj */ 13238fd1498Szrj explicit _GLIBCXX17_CONSTEXPR 13338fd1498Szrj reverse_iterator(iterator_type __x) : current(__x) { } 13438fd1498Szrj 13538fd1498Szrj /** 13638fd1498Szrj * The copy constructor is normal. 13738fd1498Szrj */ 13838fd1498Szrj _GLIBCXX17_CONSTEXPR 13938fd1498Szrj reverse_iterator(const reverse_iterator& __x) 14038fd1498Szrj : current(__x.current) { } 14138fd1498Szrj 14238fd1498Szrj /** 14338fd1498Szrj * A %reverse_iterator across other types can be copied if the 14438fd1498Szrj * underlying %iterator can be converted to the type of @c current. 14538fd1498Szrj */ 14638fd1498Szrj template<typename _Iter> 14738fd1498Szrj _GLIBCXX17_CONSTEXPR 14838fd1498Szrj reverse_iterator(const reverse_iterator<_Iter>& __x) 14938fd1498Szrj : current(__x.base()) { } 15038fd1498Szrj 15138fd1498Szrj /** 15238fd1498Szrj * @return @c current, the %iterator used for underlying work. 15338fd1498Szrj */ 15438fd1498Szrj _GLIBCXX17_CONSTEXPR iterator_type 15538fd1498Szrj base() const 15638fd1498Szrj { return current; } 15738fd1498Szrj 15838fd1498Szrj /** 15938fd1498Szrj * @return A reference to the value at @c --current 16038fd1498Szrj * 16138fd1498Szrj * This requires that @c --current is dereferenceable. 16238fd1498Szrj * 16338fd1498Szrj * @warning This implementation requires that for an iterator of the 16438fd1498Szrj * underlying iterator type, @c x, a reference obtained by 16538fd1498Szrj * @c *x remains valid after @c x has been modified or 16638fd1498Szrj * destroyed. This is a bug: http://gcc.gnu.org/PR51823 16738fd1498Szrj */ 16838fd1498Szrj _GLIBCXX17_CONSTEXPR reference 16938fd1498Szrj operator*() const 17038fd1498Szrj { 17138fd1498Szrj _Iterator __tmp = current; 17238fd1498Szrj return *--__tmp; 17338fd1498Szrj } 17438fd1498Szrj 17538fd1498Szrj /** 17638fd1498Szrj * @return A pointer to the value at @c --current 17738fd1498Szrj * 17838fd1498Szrj * This requires that @c --current is dereferenceable. 17938fd1498Szrj */ 180*58e805e6Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 181*58e805e6Szrj // 2188. Reverse iterator does not fully support targets that overload & 18238fd1498Szrj _GLIBCXX17_CONSTEXPR pointer 18338fd1498Szrj operator->() const 184*58e805e6Szrj { return std::__addressof(operator*()); } 18538fd1498Szrj 18638fd1498Szrj /** 18738fd1498Szrj * @return @c *this 18838fd1498Szrj * 18938fd1498Szrj * Decrements the underlying iterator. 19038fd1498Szrj */ 19138fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator& 19238fd1498Szrj operator++() 19338fd1498Szrj { 19438fd1498Szrj --current; 19538fd1498Szrj return *this; 19638fd1498Szrj } 19738fd1498Szrj 19838fd1498Szrj /** 19938fd1498Szrj * @return The original value of @c *this 20038fd1498Szrj * 20138fd1498Szrj * Decrements the underlying iterator. 20238fd1498Szrj */ 20338fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator 20438fd1498Szrj operator++(int) 20538fd1498Szrj { 20638fd1498Szrj reverse_iterator __tmp = *this; 20738fd1498Szrj --current; 20838fd1498Szrj return __tmp; 20938fd1498Szrj } 21038fd1498Szrj 21138fd1498Szrj /** 21238fd1498Szrj * @return @c *this 21338fd1498Szrj * 21438fd1498Szrj * Increments the underlying iterator. 21538fd1498Szrj */ 21638fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator& 21738fd1498Szrj operator--() 21838fd1498Szrj { 21938fd1498Szrj ++current; 22038fd1498Szrj return *this; 22138fd1498Szrj } 22238fd1498Szrj 22338fd1498Szrj /** 22438fd1498Szrj * @return A reverse_iterator with the previous value of @c *this 22538fd1498Szrj * 22638fd1498Szrj * Increments the underlying iterator. 22738fd1498Szrj */ 22838fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator 22938fd1498Szrj operator--(int) 23038fd1498Szrj { 23138fd1498Szrj reverse_iterator __tmp = *this; 23238fd1498Szrj ++current; 23338fd1498Szrj return __tmp; 23438fd1498Szrj } 23538fd1498Szrj 23638fd1498Szrj /** 23738fd1498Szrj * @return A reverse_iterator that refers to @c current - @a __n 23838fd1498Szrj * 23938fd1498Szrj * The underlying iterator must be a Random Access Iterator. 24038fd1498Szrj */ 24138fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator 24238fd1498Szrj operator+(difference_type __n) const 24338fd1498Szrj { return reverse_iterator(current - __n); } 24438fd1498Szrj 24538fd1498Szrj /** 24638fd1498Szrj * @return *this 24738fd1498Szrj * 24838fd1498Szrj * Moves the underlying iterator backwards @a __n steps. 24938fd1498Szrj * The underlying iterator must be a Random Access Iterator. 25038fd1498Szrj */ 25138fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator& 25238fd1498Szrj operator+=(difference_type __n) 25338fd1498Szrj { 25438fd1498Szrj current -= __n; 25538fd1498Szrj return *this; 25638fd1498Szrj } 25738fd1498Szrj 25838fd1498Szrj /** 25938fd1498Szrj * @return A reverse_iterator that refers to @c current - @a __n 26038fd1498Szrj * 26138fd1498Szrj * The underlying iterator must be a Random Access Iterator. 26238fd1498Szrj */ 26338fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator 26438fd1498Szrj operator-(difference_type __n) const 26538fd1498Szrj { return reverse_iterator(current + __n); } 26638fd1498Szrj 26738fd1498Szrj /** 26838fd1498Szrj * @return *this 26938fd1498Szrj * 27038fd1498Szrj * Moves the underlying iterator forwards @a __n steps. 27138fd1498Szrj * The underlying iterator must be a Random Access Iterator. 27238fd1498Szrj */ 27338fd1498Szrj _GLIBCXX17_CONSTEXPR reverse_iterator& 27438fd1498Szrj operator-=(difference_type __n) 27538fd1498Szrj { 27638fd1498Szrj current += __n; 27738fd1498Szrj return *this; 27838fd1498Szrj } 27938fd1498Szrj 28038fd1498Szrj /** 28138fd1498Szrj * @return The value at @c current - @a __n - 1 28238fd1498Szrj * 28338fd1498Szrj * The underlying iterator must be a Random Access Iterator. 28438fd1498Szrj */ 28538fd1498Szrj _GLIBCXX17_CONSTEXPR reference 28638fd1498Szrj operator[](difference_type __n) const 28738fd1498Szrj { return *(*this + __n); } 28838fd1498Szrj }; 28938fd1498Szrj 29038fd1498Szrj //@{ 29138fd1498Szrj /** 29238fd1498Szrj * @param __x A %reverse_iterator. 29338fd1498Szrj * @param __y A %reverse_iterator. 29438fd1498Szrj * @return A simple bool. 29538fd1498Szrj * 29638fd1498Szrj * Reverse iterators forward many operations to their underlying base() 29738fd1498Szrj * iterators. Others are implemented in terms of one another. 29838fd1498Szrj * 29938fd1498Szrj */ 30038fd1498Szrj template<typename _Iterator> 30138fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 30238fd1498Szrj operator==(const reverse_iterator<_Iterator>& __x, 30338fd1498Szrj const reverse_iterator<_Iterator>& __y) 30438fd1498Szrj { return __x.base() == __y.base(); } 30538fd1498Szrj 30638fd1498Szrj template<typename _Iterator> 30738fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 30838fd1498Szrj operator<(const reverse_iterator<_Iterator>& __x, 30938fd1498Szrj const reverse_iterator<_Iterator>& __y) 31038fd1498Szrj { return __y.base() < __x.base(); } 31138fd1498Szrj 31238fd1498Szrj template<typename _Iterator> 31338fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 31438fd1498Szrj operator!=(const reverse_iterator<_Iterator>& __x, 31538fd1498Szrj const reverse_iterator<_Iterator>& __y) 31638fd1498Szrj { return !(__x == __y); } 31738fd1498Szrj 31838fd1498Szrj template<typename _Iterator> 31938fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 32038fd1498Szrj operator>(const reverse_iterator<_Iterator>& __x, 32138fd1498Szrj const reverse_iterator<_Iterator>& __y) 32238fd1498Szrj { return __y < __x; } 32338fd1498Szrj 32438fd1498Szrj template<typename _Iterator> 32538fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 32638fd1498Szrj operator<=(const reverse_iterator<_Iterator>& __x, 32738fd1498Szrj const reverse_iterator<_Iterator>& __y) 32838fd1498Szrj { return !(__y < __x); } 32938fd1498Szrj 33038fd1498Szrj template<typename _Iterator> 33138fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 33238fd1498Szrj operator>=(const reverse_iterator<_Iterator>& __x, 33338fd1498Szrj const reverse_iterator<_Iterator>& __y) 33438fd1498Szrj { return !(__x < __y); } 33538fd1498Szrj 33638fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 33738fd1498Szrj // DR 280. Comparison of reverse_iterator to const reverse_iterator. 33838fd1498Szrj template<typename _IteratorL, typename _IteratorR> 33938fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 34038fd1498Szrj operator==(const reverse_iterator<_IteratorL>& __x, 34138fd1498Szrj const reverse_iterator<_IteratorR>& __y) 34238fd1498Szrj { return __x.base() == __y.base(); } 34338fd1498Szrj 34438fd1498Szrj template<typename _IteratorL, typename _IteratorR> 34538fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 34638fd1498Szrj operator<(const reverse_iterator<_IteratorL>& __x, 34738fd1498Szrj const reverse_iterator<_IteratorR>& __y) 34838fd1498Szrj { return __y.base() < __x.base(); } 34938fd1498Szrj 35038fd1498Szrj template<typename _IteratorL, typename _IteratorR> 35138fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 35238fd1498Szrj operator!=(const reverse_iterator<_IteratorL>& __x, 35338fd1498Szrj const reverse_iterator<_IteratorR>& __y) 35438fd1498Szrj { return !(__x == __y); } 35538fd1498Szrj 35638fd1498Szrj template<typename _IteratorL, typename _IteratorR> 35738fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 35838fd1498Szrj operator>(const reverse_iterator<_IteratorL>& __x, 35938fd1498Szrj const reverse_iterator<_IteratorR>& __y) 36038fd1498Szrj { return __y < __x; } 36138fd1498Szrj 36238fd1498Szrj template<typename _IteratorL, typename _IteratorR> 36338fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 36438fd1498Szrj operator<=(const reverse_iterator<_IteratorL>& __x, 36538fd1498Szrj const reverse_iterator<_IteratorR>& __y) 36638fd1498Szrj { return !(__y < __x); } 36738fd1498Szrj 36838fd1498Szrj template<typename _IteratorL, typename _IteratorR> 36938fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 37038fd1498Szrj operator>=(const reverse_iterator<_IteratorL>& __x, 37138fd1498Szrj const reverse_iterator<_IteratorR>& __y) 37238fd1498Szrj { return !(__x < __y); } 37338fd1498Szrj //@} 37438fd1498Szrj 37538fd1498Szrj #if __cplusplus < 201103L 37638fd1498Szrj template<typename _Iterator> 37738fd1498Szrj inline typename reverse_iterator<_Iterator>::difference_type 37838fd1498Szrj operator-(const reverse_iterator<_Iterator>& __x, 37938fd1498Szrj const reverse_iterator<_Iterator>& __y) 38038fd1498Szrj { return __y.base() - __x.base(); } 38138fd1498Szrj 38238fd1498Szrj template<typename _IteratorL, typename _IteratorR> 38338fd1498Szrj inline typename reverse_iterator<_IteratorL>::difference_type 38438fd1498Szrj operator-(const reverse_iterator<_IteratorL>& __x, 38538fd1498Szrj const reverse_iterator<_IteratorR>& __y) 38638fd1498Szrj { return __y.base() - __x.base(); } 38738fd1498Szrj #else 38838fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 38938fd1498Szrj // DR 685. reverse_iterator/move_iterator difference has invalid signatures 39038fd1498Szrj template<typename _IteratorL, typename _IteratorR> 39138fd1498Szrj inline _GLIBCXX17_CONSTEXPR auto 39238fd1498Szrj operator-(const reverse_iterator<_IteratorL>& __x, 39338fd1498Szrj const reverse_iterator<_IteratorR>& __y) 39438fd1498Szrj -> decltype(__y.base() - __x.base()) 39538fd1498Szrj { return __y.base() - __x.base(); } 39638fd1498Szrj #endif 39738fd1498Szrj 39838fd1498Szrj template<typename _Iterator> 39938fd1498Szrj inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator> 40038fd1498Szrj operator+(typename reverse_iterator<_Iterator>::difference_type __n, 40138fd1498Szrj const reverse_iterator<_Iterator>& __x) 40238fd1498Szrj { return reverse_iterator<_Iterator>(__x.base() - __n); } 40338fd1498Szrj 40438fd1498Szrj #if __cplusplus >= 201103L 40538fd1498Szrj // Same as C++14 make_reverse_iterator but used in C++03 mode too. 40638fd1498Szrj template<typename _Iterator> 40738fd1498Szrj inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator> 40838fd1498Szrj __make_reverse_iterator(_Iterator __i) 40938fd1498Szrj { return reverse_iterator<_Iterator>(__i); } 41038fd1498Szrj 41138fd1498Szrj # if __cplusplus > 201103L 41238fd1498Szrj # define __cpp_lib_make_reverse_iterator 201402 41338fd1498Szrj 41438fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 41538fd1498Szrj // DR 2285. make_reverse_iterator 41638fd1498Szrj /// Generator function for reverse_iterator. 41738fd1498Szrj template<typename _Iterator> 41838fd1498Szrj inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator> 41938fd1498Szrj make_reverse_iterator(_Iterator __i) 42038fd1498Szrj { return reverse_iterator<_Iterator>(__i); } 42138fd1498Szrj # endif 42238fd1498Szrj #endif 42338fd1498Szrj 42438fd1498Szrj #if __cplusplus >= 201103L 42538fd1498Szrj template<typename _Iterator> 42638fd1498Szrj auto 42738fd1498Szrj __niter_base(reverse_iterator<_Iterator> __it) 42838fd1498Szrj -> decltype(__make_reverse_iterator(__niter_base(__it.base()))) 42938fd1498Szrj { return __make_reverse_iterator(__niter_base(__it.base())); } 43038fd1498Szrj 43138fd1498Szrj template<typename _Iterator> 43238fd1498Szrj struct __is_move_iterator<reverse_iterator<_Iterator> > 43338fd1498Szrj : __is_move_iterator<_Iterator> 43438fd1498Szrj { }; 43538fd1498Szrj 43638fd1498Szrj template<typename _Iterator> 43738fd1498Szrj auto 43838fd1498Szrj __miter_base(reverse_iterator<_Iterator> __it) 43938fd1498Szrj -> decltype(__make_reverse_iterator(__miter_base(__it.base()))) 44038fd1498Szrj { return __make_reverse_iterator(__miter_base(__it.base())); } 44138fd1498Szrj #endif 44238fd1498Szrj 44338fd1498Szrj // 24.4.2.2.1 back_insert_iterator 44438fd1498Szrj /** 44538fd1498Szrj * @brief Turns assignment into insertion. 44638fd1498Szrj * 44738fd1498Szrj * These are output iterators, constructed from a container-of-T. 44838fd1498Szrj * Assigning a T to the iterator appends it to the container using 44938fd1498Szrj * push_back. 45038fd1498Szrj * 45138fd1498Szrj * Tip: Using the back_inserter function to create these iterators can 45238fd1498Szrj * save typing. 45338fd1498Szrj */ 45438fd1498Szrj template<typename _Container> 45538fd1498Szrj class back_insert_iterator 45638fd1498Szrj : public iterator<output_iterator_tag, void, void, void, void> 45738fd1498Szrj { 45838fd1498Szrj protected: 45938fd1498Szrj _Container* container; 46038fd1498Szrj 46138fd1498Szrj public: 46238fd1498Szrj /// A nested typedef for the type of whatever container you used. 46338fd1498Szrj typedef _Container container_type; 46438fd1498Szrj 46538fd1498Szrj /// The only way to create this %iterator is with a container. 46638fd1498Szrj explicit 46738fd1498Szrj back_insert_iterator(_Container& __x) 46838fd1498Szrj : container(std::__addressof(__x)) { } 46938fd1498Szrj 47038fd1498Szrj /** 47138fd1498Szrj * @param __value An instance of whatever type 47238fd1498Szrj * container_type::const_reference is; presumably a 47338fd1498Szrj * reference-to-const T for container<T>. 47438fd1498Szrj * @return This %iterator, for chained operations. 47538fd1498Szrj * 47638fd1498Szrj * This kind of %iterator doesn't really have a @a position in the 47738fd1498Szrj * container (you can think of the position as being permanently at 47838fd1498Szrj * the end, if you like). Assigning a value to the %iterator will 47938fd1498Szrj * always append the value to the end of the container. 48038fd1498Szrj */ 48138fd1498Szrj #if __cplusplus < 201103L 48238fd1498Szrj back_insert_iterator& 48338fd1498Szrj operator=(typename _Container::const_reference __value) 48438fd1498Szrj { 48538fd1498Szrj container->push_back(__value); 48638fd1498Szrj return *this; 48738fd1498Szrj } 48838fd1498Szrj #else 48938fd1498Szrj back_insert_iterator& 49038fd1498Szrj operator=(const typename _Container::value_type& __value) 49138fd1498Szrj { 49238fd1498Szrj container->push_back(__value); 49338fd1498Szrj return *this; 49438fd1498Szrj } 49538fd1498Szrj 49638fd1498Szrj back_insert_iterator& 49738fd1498Szrj operator=(typename _Container::value_type&& __value) 49838fd1498Szrj { 49938fd1498Szrj container->push_back(std::move(__value)); 50038fd1498Szrj return *this; 50138fd1498Szrj } 50238fd1498Szrj #endif 50338fd1498Szrj 50438fd1498Szrj /// Simply returns *this. 50538fd1498Szrj back_insert_iterator& 50638fd1498Szrj operator*() 50738fd1498Szrj { return *this; } 50838fd1498Szrj 50938fd1498Szrj /// Simply returns *this. (This %iterator does not @a move.) 51038fd1498Szrj back_insert_iterator& 51138fd1498Szrj operator++() 51238fd1498Szrj { return *this; } 51338fd1498Szrj 51438fd1498Szrj /// Simply returns *this. (This %iterator does not @a move.) 51538fd1498Szrj back_insert_iterator 51638fd1498Szrj operator++(int) 51738fd1498Szrj { return *this; } 51838fd1498Szrj }; 51938fd1498Szrj 52038fd1498Szrj /** 52138fd1498Szrj * @param __x A container of arbitrary type. 52238fd1498Szrj * @return An instance of back_insert_iterator working on @p __x. 52338fd1498Szrj * 52438fd1498Szrj * This wrapper function helps in creating back_insert_iterator instances. 52538fd1498Szrj * Typing the name of the %iterator requires knowing the precise full 52638fd1498Szrj * type of the container, which can be tedious and impedes generic 52738fd1498Szrj * programming. Using this function lets you take advantage of automatic 52838fd1498Szrj * template parameter deduction, making the compiler match the correct 52938fd1498Szrj * types for you. 53038fd1498Szrj */ 53138fd1498Szrj template<typename _Container> 53238fd1498Szrj inline back_insert_iterator<_Container> 53338fd1498Szrj back_inserter(_Container& __x) 53438fd1498Szrj { return back_insert_iterator<_Container>(__x); } 53538fd1498Szrj 53638fd1498Szrj /** 53738fd1498Szrj * @brief Turns assignment into insertion. 53838fd1498Szrj * 53938fd1498Szrj * These are output iterators, constructed from a container-of-T. 54038fd1498Szrj * Assigning a T to the iterator prepends it to the container using 54138fd1498Szrj * push_front. 54238fd1498Szrj * 54338fd1498Szrj * Tip: Using the front_inserter function to create these iterators can 54438fd1498Szrj * save typing. 54538fd1498Szrj */ 54638fd1498Szrj template<typename _Container> 54738fd1498Szrj class front_insert_iterator 54838fd1498Szrj : public iterator<output_iterator_tag, void, void, void, void> 54938fd1498Szrj { 55038fd1498Szrj protected: 55138fd1498Szrj _Container* container; 55238fd1498Szrj 55338fd1498Szrj public: 55438fd1498Szrj /// A nested typedef for the type of whatever container you used. 55538fd1498Szrj typedef _Container container_type; 55638fd1498Szrj 55738fd1498Szrj /// The only way to create this %iterator is with a container. 55838fd1498Szrj explicit front_insert_iterator(_Container& __x) 55938fd1498Szrj : container(std::__addressof(__x)) { } 56038fd1498Szrj 56138fd1498Szrj /** 56238fd1498Szrj * @param __value An instance of whatever type 56338fd1498Szrj * container_type::const_reference is; presumably a 56438fd1498Szrj * reference-to-const T for container<T>. 56538fd1498Szrj * @return This %iterator, for chained operations. 56638fd1498Szrj * 56738fd1498Szrj * This kind of %iterator doesn't really have a @a position in the 56838fd1498Szrj * container (you can think of the position as being permanently at 56938fd1498Szrj * the front, if you like). Assigning a value to the %iterator will 57038fd1498Szrj * always prepend the value to the front of the container. 57138fd1498Szrj */ 57238fd1498Szrj #if __cplusplus < 201103L 57338fd1498Szrj front_insert_iterator& 57438fd1498Szrj operator=(typename _Container::const_reference __value) 57538fd1498Szrj { 57638fd1498Szrj container->push_front(__value); 57738fd1498Szrj return *this; 57838fd1498Szrj } 57938fd1498Szrj #else 58038fd1498Szrj front_insert_iterator& 58138fd1498Szrj operator=(const typename _Container::value_type& __value) 58238fd1498Szrj { 58338fd1498Szrj container->push_front(__value); 58438fd1498Szrj return *this; 58538fd1498Szrj } 58638fd1498Szrj 58738fd1498Szrj front_insert_iterator& 58838fd1498Szrj operator=(typename _Container::value_type&& __value) 58938fd1498Szrj { 59038fd1498Szrj container->push_front(std::move(__value)); 59138fd1498Szrj return *this; 59238fd1498Szrj } 59338fd1498Szrj #endif 59438fd1498Szrj 59538fd1498Szrj /// Simply returns *this. 59638fd1498Szrj front_insert_iterator& 59738fd1498Szrj operator*() 59838fd1498Szrj { return *this; } 59938fd1498Szrj 60038fd1498Szrj /// Simply returns *this. (This %iterator does not @a move.) 60138fd1498Szrj front_insert_iterator& 60238fd1498Szrj operator++() 60338fd1498Szrj { return *this; } 60438fd1498Szrj 60538fd1498Szrj /// Simply returns *this. (This %iterator does not @a move.) 60638fd1498Szrj front_insert_iterator 60738fd1498Szrj operator++(int) 60838fd1498Szrj { return *this; } 60938fd1498Szrj }; 61038fd1498Szrj 61138fd1498Szrj /** 61238fd1498Szrj * @param __x A container of arbitrary type. 61338fd1498Szrj * @return An instance of front_insert_iterator working on @p x. 61438fd1498Szrj * 61538fd1498Szrj * This wrapper function helps in creating front_insert_iterator instances. 61638fd1498Szrj * Typing the name of the %iterator requires knowing the precise full 61738fd1498Szrj * type of the container, which can be tedious and impedes generic 61838fd1498Szrj * programming. Using this function lets you take advantage of automatic 61938fd1498Szrj * template parameter deduction, making the compiler match the correct 62038fd1498Szrj * types for you. 62138fd1498Szrj */ 62238fd1498Szrj template<typename _Container> 62338fd1498Szrj inline front_insert_iterator<_Container> 62438fd1498Szrj front_inserter(_Container& __x) 62538fd1498Szrj { return front_insert_iterator<_Container>(__x); } 62638fd1498Szrj 62738fd1498Szrj /** 62838fd1498Szrj * @brief Turns assignment into insertion. 62938fd1498Szrj * 63038fd1498Szrj * These are output iterators, constructed from a container-of-T. 63138fd1498Szrj * Assigning a T to the iterator inserts it in the container at the 63238fd1498Szrj * %iterator's position, rather than overwriting the value at that 63338fd1498Szrj * position. 63438fd1498Szrj * 63538fd1498Szrj * (Sequences will actually insert a @e copy of the value before the 63638fd1498Szrj * %iterator's position.) 63738fd1498Szrj * 63838fd1498Szrj * Tip: Using the inserter function to create these iterators can 63938fd1498Szrj * save typing. 64038fd1498Szrj */ 64138fd1498Szrj template<typename _Container> 64238fd1498Szrj class insert_iterator 64338fd1498Szrj : public iterator<output_iterator_tag, void, void, void, void> 64438fd1498Szrj { 64538fd1498Szrj protected: 64638fd1498Szrj _Container* container; 64738fd1498Szrj typename _Container::iterator iter; 64838fd1498Szrj 64938fd1498Szrj public: 65038fd1498Szrj /// A nested typedef for the type of whatever container you used. 65138fd1498Szrj typedef _Container container_type; 65238fd1498Szrj 65338fd1498Szrj /** 65438fd1498Szrj * The only way to create this %iterator is with a container and an 65538fd1498Szrj * initial position (a normal %iterator into the container). 65638fd1498Szrj */ 65738fd1498Szrj insert_iterator(_Container& __x, typename _Container::iterator __i) 65838fd1498Szrj : container(std::__addressof(__x)), iter(__i) {} 65938fd1498Szrj 66038fd1498Szrj /** 66138fd1498Szrj * @param __value An instance of whatever type 66238fd1498Szrj * container_type::const_reference is; presumably a 66338fd1498Szrj * reference-to-const T for container<T>. 66438fd1498Szrj * @return This %iterator, for chained operations. 66538fd1498Szrj * 66638fd1498Szrj * This kind of %iterator maintains its own position in the 66738fd1498Szrj * container. Assigning a value to the %iterator will insert the 66838fd1498Szrj * value into the container at the place before the %iterator. 66938fd1498Szrj * 67038fd1498Szrj * The position is maintained such that subsequent assignments will 67138fd1498Szrj * insert values immediately after one another. For example, 67238fd1498Szrj * @code 67338fd1498Szrj * // vector v contains A and Z 67438fd1498Szrj * 67538fd1498Szrj * insert_iterator i (v, ++v.begin()); 67638fd1498Szrj * i = 1; 67738fd1498Szrj * i = 2; 67838fd1498Szrj * i = 3; 67938fd1498Szrj * 68038fd1498Szrj * // vector v contains A, 1, 2, 3, and Z 68138fd1498Szrj * @endcode 68238fd1498Szrj */ 68338fd1498Szrj #if __cplusplus < 201103L 68438fd1498Szrj insert_iterator& 68538fd1498Szrj operator=(typename _Container::const_reference __value) 68638fd1498Szrj { 68738fd1498Szrj iter = container->insert(iter, __value); 68838fd1498Szrj ++iter; 68938fd1498Szrj return *this; 69038fd1498Szrj } 69138fd1498Szrj #else 69238fd1498Szrj insert_iterator& 69338fd1498Szrj operator=(const typename _Container::value_type& __value) 69438fd1498Szrj { 69538fd1498Szrj iter = container->insert(iter, __value); 69638fd1498Szrj ++iter; 69738fd1498Szrj return *this; 69838fd1498Szrj } 69938fd1498Szrj 70038fd1498Szrj insert_iterator& 70138fd1498Szrj operator=(typename _Container::value_type&& __value) 70238fd1498Szrj { 70338fd1498Szrj iter = container->insert(iter, std::move(__value)); 70438fd1498Szrj ++iter; 70538fd1498Szrj return *this; 70638fd1498Szrj } 70738fd1498Szrj #endif 70838fd1498Szrj 70938fd1498Szrj /// Simply returns *this. 71038fd1498Szrj insert_iterator& 71138fd1498Szrj operator*() 71238fd1498Szrj { return *this; } 71338fd1498Szrj 71438fd1498Szrj /// Simply returns *this. (This %iterator does not @a move.) 71538fd1498Szrj insert_iterator& 71638fd1498Szrj operator++() 71738fd1498Szrj { return *this; } 71838fd1498Szrj 71938fd1498Szrj /// Simply returns *this. (This %iterator does not @a move.) 72038fd1498Szrj insert_iterator& 72138fd1498Szrj operator++(int) 72238fd1498Szrj { return *this; } 72338fd1498Szrj }; 72438fd1498Szrj 72538fd1498Szrj /** 72638fd1498Szrj * @param __x A container of arbitrary type. 72738fd1498Szrj * @param __i An iterator into the container. 72838fd1498Szrj * @return An instance of insert_iterator working on @p __x. 72938fd1498Szrj * 73038fd1498Szrj * This wrapper function helps in creating insert_iterator instances. 73138fd1498Szrj * Typing the name of the %iterator requires knowing the precise full 73238fd1498Szrj * type of the container, which can be tedious and impedes generic 73338fd1498Szrj * programming. Using this function lets you take advantage of automatic 73438fd1498Szrj * template parameter deduction, making the compiler match the correct 73538fd1498Szrj * types for you. 73638fd1498Szrj */ 73738fd1498Szrj template<typename _Container, typename _Iterator> 73838fd1498Szrj inline insert_iterator<_Container> 73938fd1498Szrj inserter(_Container& __x, _Iterator __i) 74038fd1498Szrj { 74138fd1498Szrj return insert_iterator<_Container>(__x, 74238fd1498Szrj typename _Container::iterator(__i)); 74338fd1498Szrj } 74438fd1498Szrj 74538fd1498Szrj // @} group iterators 74638fd1498Szrj 74738fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 74838fd1498Szrj } // namespace 74938fd1498Szrj 75038fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 75138fd1498Szrj { 75238fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 75338fd1498Szrj 75438fd1498Szrj // This iterator adapter is @a normal in the sense that it does not 75538fd1498Szrj // change the semantics of any of the operators of its iterator 75638fd1498Szrj // parameter. Its primary purpose is to convert an iterator that is 75738fd1498Szrj // not a class, e.g. a pointer, into an iterator that is a class. 75838fd1498Szrj // The _Container parameter exists solely so that different containers 75938fd1498Szrj // using this template can instantiate different types, even if the 76038fd1498Szrj // _Iterator parameter is the same. 76138fd1498Szrj using std::iterator_traits; 76238fd1498Szrj using std::iterator; 76338fd1498Szrj template<typename _Iterator, typename _Container> 76438fd1498Szrj class __normal_iterator 76538fd1498Szrj { 76638fd1498Szrj protected: 76738fd1498Szrj _Iterator _M_current; 76838fd1498Szrj 76938fd1498Szrj typedef iterator_traits<_Iterator> __traits_type; 77038fd1498Szrj 77138fd1498Szrj public: 77238fd1498Szrj typedef _Iterator iterator_type; 77338fd1498Szrj typedef typename __traits_type::iterator_category iterator_category; 77438fd1498Szrj typedef typename __traits_type::value_type value_type; 77538fd1498Szrj typedef typename __traits_type::difference_type difference_type; 77638fd1498Szrj typedef typename __traits_type::reference reference; 77738fd1498Szrj typedef typename __traits_type::pointer pointer; 77838fd1498Szrj 77938fd1498Szrj _GLIBCXX_CONSTEXPR __normal_iterator() _GLIBCXX_NOEXCEPT 78038fd1498Szrj : _M_current(_Iterator()) { } 78138fd1498Szrj 78238fd1498Szrj explicit 78338fd1498Szrj __normal_iterator(const _Iterator& __i) _GLIBCXX_NOEXCEPT 78438fd1498Szrj : _M_current(__i) { } 78538fd1498Szrj 78638fd1498Szrj // Allow iterator to const_iterator conversion 78738fd1498Szrj template<typename _Iter> 78838fd1498Szrj __normal_iterator(const __normal_iterator<_Iter, 78938fd1498Szrj typename __enable_if< 79038fd1498Szrj (std::__are_same<_Iter, typename _Container::pointer>::__value), 79138fd1498Szrj _Container>::__type>& __i) _GLIBCXX_NOEXCEPT 79238fd1498Szrj : _M_current(__i.base()) { } 79338fd1498Szrj 79438fd1498Szrj // Forward iterator requirements 79538fd1498Szrj reference 79638fd1498Szrj operator*() const _GLIBCXX_NOEXCEPT 79738fd1498Szrj { return *_M_current; } 79838fd1498Szrj 79938fd1498Szrj pointer 80038fd1498Szrj operator->() const _GLIBCXX_NOEXCEPT 80138fd1498Szrj { return _M_current; } 80238fd1498Szrj 80338fd1498Szrj __normal_iterator& 80438fd1498Szrj operator++() _GLIBCXX_NOEXCEPT 80538fd1498Szrj { 80638fd1498Szrj ++_M_current; 80738fd1498Szrj return *this; 80838fd1498Szrj } 80938fd1498Szrj 81038fd1498Szrj __normal_iterator 81138fd1498Szrj operator++(int) _GLIBCXX_NOEXCEPT 81238fd1498Szrj { return __normal_iterator(_M_current++); } 81338fd1498Szrj 81438fd1498Szrj // Bidirectional iterator requirements 81538fd1498Szrj __normal_iterator& 81638fd1498Szrj operator--() _GLIBCXX_NOEXCEPT 81738fd1498Szrj { 81838fd1498Szrj --_M_current; 81938fd1498Szrj return *this; 82038fd1498Szrj } 82138fd1498Szrj 82238fd1498Szrj __normal_iterator 82338fd1498Szrj operator--(int) _GLIBCXX_NOEXCEPT 82438fd1498Szrj { return __normal_iterator(_M_current--); } 82538fd1498Szrj 82638fd1498Szrj // Random access iterator requirements 82738fd1498Szrj reference 82838fd1498Szrj operator[](difference_type __n) const _GLIBCXX_NOEXCEPT 82938fd1498Szrj { return _M_current[__n]; } 83038fd1498Szrj 83138fd1498Szrj __normal_iterator& 83238fd1498Szrj operator+=(difference_type __n) _GLIBCXX_NOEXCEPT 83338fd1498Szrj { _M_current += __n; return *this; } 83438fd1498Szrj 83538fd1498Szrj __normal_iterator 83638fd1498Szrj operator+(difference_type __n) const _GLIBCXX_NOEXCEPT 83738fd1498Szrj { return __normal_iterator(_M_current + __n); } 83838fd1498Szrj 83938fd1498Szrj __normal_iterator& 84038fd1498Szrj operator-=(difference_type __n) _GLIBCXX_NOEXCEPT 84138fd1498Szrj { _M_current -= __n; return *this; } 84238fd1498Szrj 84338fd1498Szrj __normal_iterator 84438fd1498Szrj operator-(difference_type __n) const _GLIBCXX_NOEXCEPT 84538fd1498Szrj { return __normal_iterator(_M_current - __n); } 84638fd1498Szrj 84738fd1498Szrj const _Iterator& 84838fd1498Szrj base() const _GLIBCXX_NOEXCEPT 84938fd1498Szrj { return _M_current; } 85038fd1498Szrj }; 85138fd1498Szrj 85238fd1498Szrj // Note: In what follows, the left- and right-hand-side iterators are 85338fd1498Szrj // allowed to vary in types (conceptually in cv-qualification) so that 85438fd1498Szrj // comparison between cv-qualified and non-cv-qualified iterators be 85538fd1498Szrj // valid. However, the greedy and unfriendly operators in std::rel_ops 85638fd1498Szrj // will make overload resolution ambiguous (when in scope) if we don't 85738fd1498Szrj // provide overloads whose operands are of the same type. Can someone 85838fd1498Szrj // remind me what generic programming is about? -- Gaby 85938fd1498Szrj 86038fd1498Szrj // Forward iterator requirements 86138fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 86238fd1498Szrj inline bool 86338fd1498Szrj operator==(const __normal_iterator<_IteratorL, _Container>& __lhs, 86438fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 86538fd1498Szrj _GLIBCXX_NOEXCEPT 86638fd1498Szrj { return __lhs.base() == __rhs.base(); } 86738fd1498Szrj 86838fd1498Szrj template<typename _Iterator, typename _Container> 86938fd1498Szrj inline bool 87038fd1498Szrj operator==(const __normal_iterator<_Iterator, _Container>& __lhs, 87138fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 87238fd1498Szrj _GLIBCXX_NOEXCEPT 87338fd1498Szrj { return __lhs.base() == __rhs.base(); } 87438fd1498Szrj 87538fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 87638fd1498Szrj inline bool 87738fd1498Szrj operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs, 87838fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 87938fd1498Szrj _GLIBCXX_NOEXCEPT 88038fd1498Szrj { return __lhs.base() != __rhs.base(); } 88138fd1498Szrj 88238fd1498Szrj template<typename _Iterator, typename _Container> 88338fd1498Szrj inline bool 88438fd1498Szrj operator!=(const __normal_iterator<_Iterator, _Container>& __lhs, 88538fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 88638fd1498Szrj _GLIBCXX_NOEXCEPT 88738fd1498Szrj { return __lhs.base() != __rhs.base(); } 88838fd1498Szrj 88938fd1498Szrj // Random access iterator requirements 89038fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 89138fd1498Szrj inline bool 89238fd1498Szrj operator<(const __normal_iterator<_IteratorL, _Container>& __lhs, 89338fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 89438fd1498Szrj _GLIBCXX_NOEXCEPT 89538fd1498Szrj { return __lhs.base() < __rhs.base(); } 89638fd1498Szrj 89738fd1498Szrj template<typename _Iterator, typename _Container> 89838fd1498Szrj inline bool 89938fd1498Szrj operator<(const __normal_iterator<_Iterator, _Container>& __lhs, 90038fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 90138fd1498Szrj _GLIBCXX_NOEXCEPT 90238fd1498Szrj { return __lhs.base() < __rhs.base(); } 90338fd1498Szrj 90438fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 90538fd1498Szrj inline bool 90638fd1498Szrj operator>(const __normal_iterator<_IteratorL, _Container>& __lhs, 90738fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 90838fd1498Szrj _GLIBCXX_NOEXCEPT 90938fd1498Szrj { return __lhs.base() > __rhs.base(); } 91038fd1498Szrj 91138fd1498Szrj template<typename _Iterator, typename _Container> 91238fd1498Szrj inline bool 91338fd1498Szrj operator>(const __normal_iterator<_Iterator, _Container>& __lhs, 91438fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 91538fd1498Szrj _GLIBCXX_NOEXCEPT 91638fd1498Szrj { return __lhs.base() > __rhs.base(); } 91738fd1498Szrj 91838fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 91938fd1498Szrj inline bool 92038fd1498Szrj operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs, 92138fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 92238fd1498Szrj _GLIBCXX_NOEXCEPT 92338fd1498Szrj { return __lhs.base() <= __rhs.base(); } 92438fd1498Szrj 92538fd1498Szrj template<typename _Iterator, typename _Container> 92638fd1498Szrj inline bool 92738fd1498Szrj operator<=(const __normal_iterator<_Iterator, _Container>& __lhs, 92838fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 92938fd1498Szrj _GLIBCXX_NOEXCEPT 93038fd1498Szrj { return __lhs.base() <= __rhs.base(); } 93138fd1498Szrj 93238fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 93338fd1498Szrj inline bool 93438fd1498Szrj operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs, 93538fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 93638fd1498Szrj _GLIBCXX_NOEXCEPT 93738fd1498Szrj { return __lhs.base() >= __rhs.base(); } 93838fd1498Szrj 93938fd1498Szrj template<typename _Iterator, typename _Container> 94038fd1498Szrj inline bool 94138fd1498Szrj operator>=(const __normal_iterator<_Iterator, _Container>& __lhs, 94238fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 94338fd1498Szrj _GLIBCXX_NOEXCEPT 94438fd1498Szrj { return __lhs.base() >= __rhs.base(); } 94538fd1498Szrj 94638fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 94738fd1498Szrj // According to the resolution of DR179 not only the various comparison 94838fd1498Szrj // operators but also operator- must accept mixed iterator/const_iterator 94938fd1498Szrj // parameters. 95038fd1498Szrj template<typename _IteratorL, typename _IteratorR, typename _Container> 95138fd1498Szrj #if __cplusplus >= 201103L 95238fd1498Szrj // DR 685. 95338fd1498Szrj inline auto 95438fd1498Szrj operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, 95538fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept 95638fd1498Szrj -> decltype(__lhs.base() - __rhs.base()) 95738fd1498Szrj #else 95838fd1498Szrj inline typename __normal_iterator<_IteratorL, _Container>::difference_type 95938fd1498Szrj operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, 96038fd1498Szrj const __normal_iterator<_IteratorR, _Container>& __rhs) 96138fd1498Szrj #endif 96238fd1498Szrj { return __lhs.base() - __rhs.base(); } 96338fd1498Szrj 96438fd1498Szrj template<typename _Iterator, typename _Container> 96538fd1498Szrj inline typename __normal_iterator<_Iterator, _Container>::difference_type 96638fd1498Szrj operator-(const __normal_iterator<_Iterator, _Container>& __lhs, 96738fd1498Szrj const __normal_iterator<_Iterator, _Container>& __rhs) 96838fd1498Szrj _GLIBCXX_NOEXCEPT 96938fd1498Szrj { return __lhs.base() - __rhs.base(); } 97038fd1498Szrj 97138fd1498Szrj template<typename _Iterator, typename _Container> 97238fd1498Szrj inline __normal_iterator<_Iterator, _Container> 97338fd1498Szrj operator+(typename __normal_iterator<_Iterator, _Container>::difference_type 97438fd1498Szrj __n, const __normal_iterator<_Iterator, _Container>& __i) 97538fd1498Szrj _GLIBCXX_NOEXCEPT 97638fd1498Szrj { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); } 97738fd1498Szrj 97838fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 97938fd1498Szrj } // namespace 98038fd1498Szrj 98138fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 98238fd1498Szrj { 98338fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 98438fd1498Szrj 98538fd1498Szrj template<typename _Iterator, typename _Container> 98638fd1498Szrj _Iterator 98738fd1498Szrj __niter_base(__gnu_cxx::__normal_iterator<_Iterator, _Container> __it) 98838fd1498Szrj { return __it.base(); } 98938fd1498Szrj 99038fd1498Szrj #if __cplusplus >= 201103L 99138fd1498Szrj 99238fd1498Szrj /** 99338fd1498Szrj * @addtogroup iterators 99438fd1498Szrj * @{ 99538fd1498Szrj */ 99638fd1498Szrj 99738fd1498Szrj // 24.4.3 Move iterators 99838fd1498Szrj /** 99938fd1498Szrj * Class template move_iterator is an iterator adapter with the same 100038fd1498Szrj * behavior as the underlying iterator except that its dereference 100138fd1498Szrj * operator implicitly converts the value returned by the underlying 100238fd1498Szrj * iterator's dereference operator to an rvalue reference. Some 100338fd1498Szrj * generic algorithms can be called with move iterators to replace 100438fd1498Szrj * copying with moving. 100538fd1498Szrj */ 100638fd1498Szrj template<typename _Iterator> 100738fd1498Szrj class move_iterator 100838fd1498Szrj { 100938fd1498Szrj protected: 101038fd1498Szrj _Iterator _M_current; 101138fd1498Szrj 101238fd1498Szrj typedef iterator_traits<_Iterator> __traits_type; 101338fd1498Szrj typedef typename __traits_type::reference __base_ref; 101438fd1498Szrj 101538fd1498Szrj public: 101638fd1498Szrj typedef _Iterator iterator_type; 101738fd1498Szrj typedef typename __traits_type::iterator_category iterator_category; 101838fd1498Szrj typedef typename __traits_type::value_type value_type; 101938fd1498Szrj typedef typename __traits_type::difference_type difference_type; 102038fd1498Szrj // NB: DR 680. 102138fd1498Szrj typedef _Iterator pointer; 102238fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 102338fd1498Szrj // 2106. move_iterator wrapping iterators returning prvalues 102438fd1498Szrj typedef typename conditional<is_reference<__base_ref>::value, 102538fd1498Szrj typename remove_reference<__base_ref>::type&&, 102638fd1498Szrj __base_ref>::type reference; 102738fd1498Szrj 102838fd1498Szrj _GLIBCXX17_CONSTEXPR 102938fd1498Szrj move_iterator() 103038fd1498Szrj : _M_current() { } 103138fd1498Szrj 103238fd1498Szrj explicit _GLIBCXX17_CONSTEXPR 103338fd1498Szrj move_iterator(iterator_type __i) 103438fd1498Szrj : _M_current(__i) { } 103538fd1498Szrj 103638fd1498Szrj template<typename _Iter> 103738fd1498Szrj _GLIBCXX17_CONSTEXPR 103838fd1498Szrj move_iterator(const move_iterator<_Iter>& __i) 103938fd1498Szrj : _M_current(__i.base()) { } 104038fd1498Szrj 104138fd1498Szrj _GLIBCXX17_CONSTEXPR iterator_type 104238fd1498Szrj base() const 104338fd1498Szrj { return _M_current; } 104438fd1498Szrj 104538fd1498Szrj _GLIBCXX17_CONSTEXPR reference 104638fd1498Szrj operator*() const 104738fd1498Szrj { return static_cast<reference>(*_M_current); } 104838fd1498Szrj 104938fd1498Szrj _GLIBCXX17_CONSTEXPR pointer 105038fd1498Szrj operator->() const 105138fd1498Szrj { return _M_current; } 105238fd1498Szrj 105338fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator& 105438fd1498Szrj operator++() 105538fd1498Szrj { 105638fd1498Szrj ++_M_current; 105738fd1498Szrj return *this; 105838fd1498Szrj } 105938fd1498Szrj 106038fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator 106138fd1498Szrj operator++(int) 106238fd1498Szrj { 106338fd1498Szrj move_iterator __tmp = *this; 106438fd1498Szrj ++_M_current; 106538fd1498Szrj return __tmp; 106638fd1498Szrj } 106738fd1498Szrj 106838fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator& 106938fd1498Szrj operator--() 107038fd1498Szrj { 107138fd1498Szrj --_M_current; 107238fd1498Szrj return *this; 107338fd1498Szrj } 107438fd1498Szrj 107538fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator 107638fd1498Szrj operator--(int) 107738fd1498Szrj { 107838fd1498Szrj move_iterator __tmp = *this; 107938fd1498Szrj --_M_current; 108038fd1498Szrj return __tmp; 108138fd1498Szrj } 108238fd1498Szrj 108338fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator 108438fd1498Szrj operator+(difference_type __n) const 108538fd1498Szrj { return move_iterator(_M_current + __n); } 108638fd1498Szrj 108738fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator& 108838fd1498Szrj operator+=(difference_type __n) 108938fd1498Szrj { 109038fd1498Szrj _M_current += __n; 109138fd1498Szrj return *this; 109238fd1498Szrj } 109338fd1498Szrj 109438fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator 109538fd1498Szrj operator-(difference_type __n) const 109638fd1498Szrj { return move_iterator(_M_current - __n); } 109738fd1498Szrj 109838fd1498Szrj _GLIBCXX17_CONSTEXPR move_iterator& 109938fd1498Szrj operator-=(difference_type __n) 110038fd1498Szrj { 110138fd1498Szrj _M_current -= __n; 110238fd1498Szrj return *this; 110338fd1498Szrj } 110438fd1498Szrj 110538fd1498Szrj _GLIBCXX17_CONSTEXPR reference 110638fd1498Szrj operator[](difference_type __n) const 110738fd1498Szrj { return std::move(_M_current[__n]); } 110838fd1498Szrj }; 110938fd1498Szrj 111038fd1498Szrj // Note: See __normal_iterator operators note from Gaby to understand 111138fd1498Szrj // why there are always 2 versions for most of the move_iterator 111238fd1498Szrj // operators. 111338fd1498Szrj template<typename _IteratorL, typename _IteratorR> 111438fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 111538fd1498Szrj operator==(const move_iterator<_IteratorL>& __x, 111638fd1498Szrj const move_iterator<_IteratorR>& __y) 111738fd1498Szrj { return __x.base() == __y.base(); } 111838fd1498Szrj 111938fd1498Szrj template<typename _Iterator> 112038fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 112138fd1498Szrj operator==(const move_iterator<_Iterator>& __x, 112238fd1498Szrj const move_iterator<_Iterator>& __y) 112338fd1498Szrj { return __x.base() == __y.base(); } 112438fd1498Szrj 112538fd1498Szrj template<typename _IteratorL, typename _IteratorR> 112638fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 112738fd1498Szrj operator!=(const move_iterator<_IteratorL>& __x, 112838fd1498Szrj const move_iterator<_IteratorR>& __y) 112938fd1498Szrj { return !(__x == __y); } 113038fd1498Szrj 113138fd1498Szrj template<typename _Iterator> 113238fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 113338fd1498Szrj operator!=(const move_iterator<_Iterator>& __x, 113438fd1498Szrj const move_iterator<_Iterator>& __y) 113538fd1498Szrj { return !(__x == __y); } 113638fd1498Szrj 113738fd1498Szrj template<typename _IteratorL, typename _IteratorR> 113838fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 113938fd1498Szrj operator<(const move_iterator<_IteratorL>& __x, 114038fd1498Szrj const move_iterator<_IteratorR>& __y) 114138fd1498Szrj { return __x.base() < __y.base(); } 114238fd1498Szrj 114338fd1498Szrj template<typename _Iterator> 114438fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 114538fd1498Szrj operator<(const move_iterator<_Iterator>& __x, 114638fd1498Szrj const move_iterator<_Iterator>& __y) 114738fd1498Szrj { return __x.base() < __y.base(); } 114838fd1498Szrj 114938fd1498Szrj template<typename _IteratorL, typename _IteratorR> 115038fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 115138fd1498Szrj operator<=(const move_iterator<_IteratorL>& __x, 115238fd1498Szrj const move_iterator<_IteratorR>& __y) 115338fd1498Szrj { return !(__y < __x); } 115438fd1498Szrj 115538fd1498Szrj template<typename _Iterator> 115638fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 115738fd1498Szrj operator<=(const move_iterator<_Iterator>& __x, 115838fd1498Szrj const move_iterator<_Iterator>& __y) 115938fd1498Szrj { return !(__y < __x); } 116038fd1498Szrj 116138fd1498Szrj template<typename _IteratorL, typename _IteratorR> 116238fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 116338fd1498Szrj operator>(const move_iterator<_IteratorL>& __x, 116438fd1498Szrj const move_iterator<_IteratorR>& __y) 116538fd1498Szrj { return __y < __x; } 116638fd1498Szrj 116738fd1498Szrj template<typename _Iterator> 116838fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 116938fd1498Szrj operator>(const move_iterator<_Iterator>& __x, 117038fd1498Szrj const move_iterator<_Iterator>& __y) 117138fd1498Szrj { return __y < __x; } 117238fd1498Szrj 117338fd1498Szrj template<typename _IteratorL, typename _IteratorR> 117438fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 117538fd1498Szrj operator>=(const move_iterator<_IteratorL>& __x, 117638fd1498Szrj const move_iterator<_IteratorR>& __y) 117738fd1498Szrj { return !(__x < __y); } 117838fd1498Szrj 117938fd1498Szrj template<typename _Iterator> 118038fd1498Szrj inline _GLIBCXX17_CONSTEXPR bool 118138fd1498Szrj operator>=(const move_iterator<_Iterator>& __x, 118238fd1498Szrj const move_iterator<_Iterator>& __y) 118338fd1498Szrj { return !(__x < __y); } 118438fd1498Szrj 118538fd1498Szrj // DR 685. 118638fd1498Szrj template<typename _IteratorL, typename _IteratorR> 118738fd1498Szrj inline _GLIBCXX17_CONSTEXPR auto 118838fd1498Szrj operator-(const move_iterator<_IteratorL>& __x, 118938fd1498Szrj const move_iterator<_IteratorR>& __y) 119038fd1498Szrj -> decltype(__x.base() - __y.base()) 119138fd1498Szrj { return __x.base() - __y.base(); } 119238fd1498Szrj 119338fd1498Szrj template<typename _Iterator> 119438fd1498Szrj inline _GLIBCXX17_CONSTEXPR move_iterator<_Iterator> 119538fd1498Szrj operator+(typename move_iterator<_Iterator>::difference_type __n, 119638fd1498Szrj const move_iterator<_Iterator>& __x) 119738fd1498Szrj { return __x + __n; } 119838fd1498Szrj 119938fd1498Szrj template<typename _Iterator> 120038fd1498Szrj inline _GLIBCXX17_CONSTEXPR move_iterator<_Iterator> 120138fd1498Szrj make_move_iterator(_Iterator __i) 120238fd1498Szrj { return move_iterator<_Iterator>(__i); } 120338fd1498Szrj 120438fd1498Szrj template<typename _Iterator, typename _ReturnType 120538fd1498Szrj = typename conditional<__move_if_noexcept_cond 120638fd1498Szrj <typename iterator_traits<_Iterator>::value_type>::value, 120738fd1498Szrj _Iterator, move_iterator<_Iterator>>::type> 120838fd1498Szrj inline _GLIBCXX17_CONSTEXPR _ReturnType 120938fd1498Szrj __make_move_if_noexcept_iterator(_Iterator __i) 121038fd1498Szrj { return _ReturnType(__i); } 121138fd1498Szrj 121238fd1498Szrj // Overload for pointers that matches std::move_if_noexcept more closely, 121338fd1498Szrj // returning a constant iterator when we don't want to move. 121438fd1498Szrj template<typename _Tp, typename _ReturnType 121538fd1498Szrj = typename conditional<__move_if_noexcept_cond<_Tp>::value, 121638fd1498Szrj const _Tp*, move_iterator<_Tp*>>::type> 121738fd1498Szrj inline _GLIBCXX17_CONSTEXPR _ReturnType 121838fd1498Szrj __make_move_if_noexcept_iterator(_Tp* __i) 121938fd1498Szrj { return _ReturnType(__i); } 122038fd1498Szrj 122138fd1498Szrj // @} group iterators 122238fd1498Szrj 122338fd1498Szrj template<typename _Iterator> 122438fd1498Szrj auto 122538fd1498Szrj __niter_base(move_iterator<_Iterator> __it) 122638fd1498Szrj -> decltype(make_move_iterator(__niter_base(__it.base()))) 122738fd1498Szrj { return make_move_iterator(__niter_base(__it.base())); } 122838fd1498Szrj 122938fd1498Szrj template<typename _Iterator> 123038fd1498Szrj struct __is_move_iterator<move_iterator<_Iterator> > 123138fd1498Szrj { 123238fd1498Szrj enum { __value = 1 }; 123338fd1498Szrj typedef __true_type __type; 123438fd1498Szrj }; 123538fd1498Szrj 123638fd1498Szrj template<typename _Iterator> 123738fd1498Szrj auto 123838fd1498Szrj __miter_base(move_iterator<_Iterator> __it) 123938fd1498Szrj -> decltype(__miter_base(__it.base())) 124038fd1498Szrj { return __miter_base(__it.base()); } 124138fd1498Szrj 124238fd1498Szrj #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter) 124338fd1498Szrj #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \ 124438fd1498Szrj std::__make_move_if_noexcept_iterator(_Iter) 124538fd1498Szrj #else 124638fd1498Szrj #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter) 124738fd1498Szrj #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter) 124838fd1498Szrj #endif // C++11 124938fd1498Szrj 125038fd1498Szrj #if __cpp_deduction_guides >= 201606 125138fd1498Szrj // These helper traits are used for deduction guides 125238fd1498Szrj // of associative containers. 125338fd1498Szrj template<typename _InputIterator> 125438fd1498Szrj using __iter_key_t = remove_const_t< 125538fd1498Szrj typename iterator_traits<_InputIterator>::value_type::first_type>; 125638fd1498Szrj 125738fd1498Szrj template<typename _InputIterator> 125838fd1498Szrj using __iter_val_t = 125938fd1498Szrj typename iterator_traits<_InputIterator>::value_type::second_type; 126038fd1498Szrj 126138fd1498Szrj template<typename _T1, typename _T2> 126238fd1498Szrj struct pair; 126338fd1498Szrj 126438fd1498Szrj template<typename _InputIterator> 126538fd1498Szrj using __iter_to_alloc_t = 126638fd1498Szrj pair<add_const_t<__iter_key_t<_InputIterator>>, 126738fd1498Szrj __iter_val_t<_InputIterator>>; 126838fd1498Szrj 126938fd1498Szrj #endif 127038fd1498Szrj 127138fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 127238fd1498Szrj } // namespace 127338fd1498Szrj 127438fd1498Szrj #ifdef _GLIBCXX_DEBUG 127538fd1498Szrj # include <debug/stl_iterator.h> 127638fd1498Szrj #endif 127738fd1498Szrj 127838fd1498Szrj #endif 1279