1*e4b17023SJohn Marino // Iterators -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4*e4b17023SJohn Marino // 2010, 2011, 2012
5*e4b17023SJohn Marino // Free Software Foundation, Inc.
6*e4b17023SJohn Marino //
7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino // any later version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*e4b17023SJohn Marino // GNU General Public License for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /*
28*e4b17023SJohn Marino *
29*e4b17023SJohn Marino * Copyright (c) 1994
30*e4b17023SJohn Marino * Hewlett-Packard Company
31*e4b17023SJohn Marino *
32*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
33*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
34*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
35*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
36*e4b17023SJohn Marino * in supporting documentation. Hewlett-Packard Company makes no
37*e4b17023SJohn Marino * representations about the suitability of this software for any
38*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
39*e4b17023SJohn Marino *
40*e4b17023SJohn Marino *
41*e4b17023SJohn Marino * Copyright (c) 1996-1998
42*e4b17023SJohn Marino * Silicon Graphics Computer Systems, Inc.
43*e4b17023SJohn Marino *
44*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
45*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
46*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
47*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
48*e4b17023SJohn Marino * in supporting documentation. Silicon Graphics makes no
49*e4b17023SJohn Marino * representations about the suitability of this software for any
50*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
51*e4b17023SJohn Marino */
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino /** @file bits/stl_iterator.h
54*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
55*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{iterator}
56*e4b17023SJohn Marino *
57*e4b17023SJohn Marino * This file implements reverse_iterator, back_insert_iterator,
58*e4b17023SJohn Marino * front_insert_iterator, insert_iterator, __normal_iterator, and their
59*e4b17023SJohn Marino * supporting functions and overloaded operators.
60*e4b17023SJohn Marino */
61*e4b17023SJohn Marino
62*e4b17023SJohn Marino #ifndef _STL_ITERATOR_H
63*e4b17023SJohn Marino #define _STL_ITERATOR_H 1
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino #include <bits/cpp_type_traits.h>
66*e4b17023SJohn Marino #include <ext/type_traits.h>
67*e4b17023SJohn Marino #include <bits/move.h>
68*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)69*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
70*e4b17023SJohn Marino {
71*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino /**
74*e4b17023SJohn Marino * @addtogroup iterators
75*e4b17023SJohn Marino * @{
76*e4b17023SJohn Marino */
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino // 24.4.1 Reverse iterators
79*e4b17023SJohn Marino /**
80*e4b17023SJohn Marino * Bidirectional and random access iterators have corresponding reverse
81*e4b17023SJohn Marino * %iterator adaptors that iterate through the data structure in the
82*e4b17023SJohn Marino * opposite direction. They have the same signatures as the corresponding
83*e4b17023SJohn Marino * iterators. The fundamental relation between a reverse %iterator and its
84*e4b17023SJohn Marino * corresponding %iterator @c i is established by the identity:
85*e4b17023SJohn Marino * @code
86*e4b17023SJohn Marino * &*(reverse_iterator(i)) == &*(i - 1)
87*e4b17023SJohn Marino * @endcode
88*e4b17023SJohn Marino *
89*e4b17023SJohn Marino * <em>This mapping is dictated by the fact that while there is always a
90*e4b17023SJohn Marino * pointer past the end of an array, there might not be a valid pointer
91*e4b17023SJohn Marino * before the beginning of an array.</em> [24.4.1]/1,2
92*e4b17023SJohn Marino *
93*e4b17023SJohn Marino * Reverse iterators can be tricky and surprising at first. Their
94*e4b17023SJohn Marino * semantics make sense, however, and the trickiness is a side effect of
95*e4b17023SJohn Marino * the requirement that the iterators must be safe.
96*e4b17023SJohn Marino */
97*e4b17023SJohn Marino template<typename _Iterator>
98*e4b17023SJohn Marino class reverse_iterator
99*e4b17023SJohn Marino : public iterator<typename iterator_traits<_Iterator>::iterator_category,
100*e4b17023SJohn Marino typename iterator_traits<_Iterator>::value_type,
101*e4b17023SJohn Marino typename iterator_traits<_Iterator>::difference_type,
102*e4b17023SJohn Marino typename iterator_traits<_Iterator>::pointer,
103*e4b17023SJohn Marino typename iterator_traits<_Iterator>::reference>
104*e4b17023SJohn Marino {
105*e4b17023SJohn Marino protected:
106*e4b17023SJohn Marino _Iterator current;
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino typedef iterator_traits<_Iterator> __traits_type;
109*e4b17023SJohn Marino
110*e4b17023SJohn Marino public:
111*e4b17023SJohn Marino typedef _Iterator iterator_type;
112*e4b17023SJohn Marino typedef typename __traits_type::difference_type difference_type;
113*e4b17023SJohn Marino typedef typename __traits_type::pointer pointer;
114*e4b17023SJohn Marino typedef typename __traits_type::reference reference;
115*e4b17023SJohn Marino
116*e4b17023SJohn Marino /**
117*e4b17023SJohn Marino * The default constructor value-initializes member @p current.
118*e4b17023SJohn Marino * If it is a pointer, that means it is zero-initialized.
119*e4b17023SJohn Marino */
120*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
121*e4b17023SJohn Marino // 235 No specification of default ctor for reverse_iterator
122*e4b17023SJohn Marino reverse_iterator() : current() { }
123*e4b17023SJohn Marino
124*e4b17023SJohn Marino /**
125*e4b17023SJohn Marino * This %iterator will move in the opposite direction that @p x does.
126*e4b17023SJohn Marino */
127*e4b17023SJohn Marino explicit
128*e4b17023SJohn Marino reverse_iterator(iterator_type __x) : current(__x) { }
129*e4b17023SJohn Marino
130*e4b17023SJohn Marino /**
131*e4b17023SJohn Marino * The copy constructor is normal.
132*e4b17023SJohn Marino */
133*e4b17023SJohn Marino reverse_iterator(const reverse_iterator& __x)
134*e4b17023SJohn Marino : current(__x.current) { }
135*e4b17023SJohn Marino
136*e4b17023SJohn Marino /**
137*e4b17023SJohn Marino * A %reverse_iterator across other types can be copied if the
138*e4b17023SJohn Marino * underlying %iterator can be converted to the type of @c current.
139*e4b17023SJohn Marino */
140*e4b17023SJohn Marino template<typename _Iter>
141*e4b17023SJohn Marino reverse_iterator(const reverse_iterator<_Iter>& __x)
142*e4b17023SJohn Marino : current(__x.base()) { }
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino /**
145*e4b17023SJohn Marino * @return @c current, the %iterator used for underlying work.
146*e4b17023SJohn Marino */
147*e4b17023SJohn Marino iterator_type
148*e4b17023SJohn Marino base() const
149*e4b17023SJohn Marino { return current; }
150*e4b17023SJohn Marino
151*e4b17023SJohn Marino /**
152*e4b17023SJohn Marino * @return A reference to the value at @c --current
153*e4b17023SJohn Marino *
154*e4b17023SJohn Marino * This requires that @c --current is dereferenceable.
155*e4b17023SJohn Marino *
156*e4b17023SJohn Marino * @warning This implementation requires that for an iterator of the
157*e4b17023SJohn Marino * underlying iterator type, @c x, a reference obtained by
158*e4b17023SJohn Marino * @c *x remains valid after @c x has been modified or
159*e4b17023SJohn Marino * destroyed. This is a bug: http://gcc.gnu.org/PR51823
160*e4b17023SJohn Marino */
161*e4b17023SJohn Marino reference
162*e4b17023SJohn Marino operator*() const
163*e4b17023SJohn Marino {
164*e4b17023SJohn Marino _Iterator __tmp = current;
165*e4b17023SJohn Marino return *--__tmp;
166*e4b17023SJohn Marino }
167*e4b17023SJohn Marino
168*e4b17023SJohn Marino /**
169*e4b17023SJohn Marino * @return A pointer to the value at @c --current
170*e4b17023SJohn Marino *
171*e4b17023SJohn Marino * This requires that @c --current is dereferenceable.
172*e4b17023SJohn Marino */
173*e4b17023SJohn Marino pointer
174*e4b17023SJohn Marino operator->() const
175*e4b17023SJohn Marino { return &(operator*()); }
176*e4b17023SJohn Marino
177*e4b17023SJohn Marino /**
178*e4b17023SJohn Marino * @return @c *this
179*e4b17023SJohn Marino *
180*e4b17023SJohn Marino * Decrements the underlying iterator.
181*e4b17023SJohn Marino */
182*e4b17023SJohn Marino reverse_iterator&
183*e4b17023SJohn Marino operator++()
184*e4b17023SJohn Marino {
185*e4b17023SJohn Marino --current;
186*e4b17023SJohn Marino return *this;
187*e4b17023SJohn Marino }
188*e4b17023SJohn Marino
189*e4b17023SJohn Marino /**
190*e4b17023SJohn Marino * @return The original value of @c *this
191*e4b17023SJohn Marino *
192*e4b17023SJohn Marino * Decrements the underlying iterator.
193*e4b17023SJohn Marino */
194*e4b17023SJohn Marino reverse_iterator
195*e4b17023SJohn Marino operator++(int)
196*e4b17023SJohn Marino {
197*e4b17023SJohn Marino reverse_iterator __tmp = *this;
198*e4b17023SJohn Marino --current;
199*e4b17023SJohn Marino return __tmp;
200*e4b17023SJohn Marino }
201*e4b17023SJohn Marino
202*e4b17023SJohn Marino /**
203*e4b17023SJohn Marino * @return @c *this
204*e4b17023SJohn Marino *
205*e4b17023SJohn Marino * Increments the underlying iterator.
206*e4b17023SJohn Marino */
207*e4b17023SJohn Marino reverse_iterator&
208*e4b17023SJohn Marino operator--()
209*e4b17023SJohn Marino {
210*e4b17023SJohn Marino ++current;
211*e4b17023SJohn Marino return *this;
212*e4b17023SJohn Marino }
213*e4b17023SJohn Marino
214*e4b17023SJohn Marino /**
215*e4b17023SJohn Marino * @return A reverse_iterator with the previous value of @c *this
216*e4b17023SJohn Marino *
217*e4b17023SJohn Marino * Increments the underlying iterator.
218*e4b17023SJohn Marino */
219*e4b17023SJohn Marino reverse_iterator
220*e4b17023SJohn Marino operator--(int)
221*e4b17023SJohn Marino {
222*e4b17023SJohn Marino reverse_iterator __tmp = *this;
223*e4b17023SJohn Marino ++current;
224*e4b17023SJohn Marino return __tmp;
225*e4b17023SJohn Marino }
226*e4b17023SJohn Marino
227*e4b17023SJohn Marino /**
228*e4b17023SJohn Marino * @return A reverse_iterator that refers to @c current - @a __n
229*e4b17023SJohn Marino *
230*e4b17023SJohn Marino * The underlying iterator must be a Random Access Iterator.
231*e4b17023SJohn Marino */
232*e4b17023SJohn Marino reverse_iterator
233*e4b17023SJohn Marino operator+(difference_type __n) const
234*e4b17023SJohn Marino { return reverse_iterator(current - __n); }
235*e4b17023SJohn Marino
236*e4b17023SJohn Marino /**
237*e4b17023SJohn Marino * @return *this
238*e4b17023SJohn Marino *
239*e4b17023SJohn Marino * Moves the underlying iterator backwards @a __n steps.
240*e4b17023SJohn Marino * The underlying iterator must be a Random Access Iterator.
241*e4b17023SJohn Marino */
242*e4b17023SJohn Marino reverse_iterator&
243*e4b17023SJohn Marino operator+=(difference_type __n)
244*e4b17023SJohn Marino {
245*e4b17023SJohn Marino current -= __n;
246*e4b17023SJohn Marino return *this;
247*e4b17023SJohn Marino }
248*e4b17023SJohn Marino
249*e4b17023SJohn Marino /**
250*e4b17023SJohn Marino * @return A reverse_iterator that refers to @c current - @a __n
251*e4b17023SJohn Marino *
252*e4b17023SJohn Marino * The underlying iterator must be a Random Access Iterator.
253*e4b17023SJohn Marino */
254*e4b17023SJohn Marino reverse_iterator
255*e4b17023SJohn Marino operator-(difference_type __n) const
256*e4b17023SJohn Marino { return reverse_iterator(current + __n); }
257*e4b17023SJohn Marino
258*e4b17023SJohn Marino /**
259*e4b17023SJohn Marino * @return *this
260*e4b17023SJohn Marino *
261*e4b17023SJohn Marino * Moves the underlying iterator forwards @a __n steps.
262*e4b17023SJohn Marino * The underlying iterator must be a Random Access Iterator.
263*e4b17023SJohn Marino */
264*e4b17023SJohn Marino reverse_iterator&
265*e4b17023SJohn Marino operator-=(difference_type __n)
266*e4b17023SJohn Marino {
267*e4b17023SJohn Marino current += __n;
268*e4b17023SJohn Marino return *this;
269*e4b17023SJohn Marino }
270*e4b17023SJohn Marino
271*e4b17023SJohn Marino /**
272*e4b17023SJohn Marino * @return The value at @c current - @a __n - 1
273*e4b17023SJohn Marino *
274*e4b17023SJohn Marino * The underlying iterator must be a Random Access Iterator.
275*e4b17023SJohn Marino */
276*e4b17023SJohn Marino reference
277*e4b17023SJohn Marino operator[](difference_type __n) const
278*e4b17023SJohn Marino { return *(*this + __n); }
279*e4b17023SJohn Marino };
280*e4b17023SJohn Marino
281*e4b17023SJohn Marino //@{
282*e4b17023SJohn Marino /**
283*e4b17023SJohn Marino * @param __x A %reverse_iterator.
284*e4b17023SJohn Marino * @param __y A %reverse_iterator.
285*e4b17023SJohn Marino * @return A simple bool.
286*e4b17023SJohn Marino *
287*e4b17023SJohn Marino * Reverse iterators forward many operations to their underlying base()
288*e4b17023SJohn Marino * iterators. Others are implemented in terms of one another.
289*e4b17023SJohn Marino *
290*e4b17023SJohn Marino */
291*e4b17023SJohn Marino template<typename _Iterator>
292*e4b17023SJohn Marino inline bool
293*e4b17023SJohn Marino operator==(const reverse_iterator<_Iterator>& __x,
294*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
295*e4b17023SJohn Marino { return __x.base() == __y.base(); }
296*e4b17023SJohn Marino
297*e4b17023SJohn Marino template<typename _Iterator>
298*e4b17023SJohn Marino inline bool
299*e4b17023SJohn Marino operator<(const reverse_iterator<_Iterator>& __x,
300*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
301*e4b17023SJohn Marino { return __y.base() < __x.base(); }
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino template<typename _Iterator>
304*e4b17023SJohn Marino inline bool
305*e4b17023SJohn Marino operator!=(const reverse_iterator<_Iterator>& __x,
306*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
307*e4b17023SJohn Marino { return !(__x == __y); }
308*e4b17023SJohn Marino
309*e4b17023SJohn Marino template<typename _Iterator>
310*e4b17023SJohn Marino inline bool
311*e4b17023SJohn Marino operator>(const reverse_iterator<_Iterator>& __x,
312*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
313*e4b17023SJohn Marino { return __y < __x; }
314*e4b17023SJohn Marino
315*e4b17023SJohn Marino template<typename _Iterator>
316*e4b17023SJohn Marino inline bool
317*e4b17023SJohn Marino operator<=(const reverse_iterator<_Iterator>& __x,
318*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
319*e4b17023SJohn Marino { return !(__y < __x); }
320*e4b17023SJohn Marino
321*e4b17023SJohn Marino template<typename _Iterator>
322*e4b17023SJohn Marino inline bool
323*e4b17023SJohn Marino operator>=(const reverse_iterator<_Iterator>& __x,
324*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
325*e4b17023SJohn Marino { return !(__x < __y); }
326*e4b17023SJohn Marino
327*e4b17023SJohn Marino template<typename _Iterator>
328*e4b17023SJohn Marino inline typename reverse_iterator<_Iterator>::difference_type
329*e4b17023SJohn Marino operator-(const reverse_iterator<_Iterator>& __x,
330*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __y)
331*e4b17023SJohn Marino { return __y.base() - __x.base(); }
332*e4b17023SJohn Marino
333*e4b17023SJohn Marino template<typename _Iterator>
334*e4b17023SJohn Marino inline reverse_iterator<_Iterator>
335*e4b17023SJohn Marino operator+(typename reverse_iterator<_Iterator>::difference_type __n,
336*e4b17023SJohn Marino const reverse_iterator<_Iterator>& __x)
337*e4b17023SJohn Marino { return reverse_iterator<_Iterator>(__x.base() - __n); }
338*e4b17023SJohn Marino
339*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
340*e4b17023SJohn Marino // DR 280. Comparison of reverse_iterator to const reverse_iterator.
341*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
342*e4b17023SJohn Marino inline bool
343*e4b17023SJohn Marino operator==(const reverse_iterator<_IteratorL>& __x,
344*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
345*e4b17023SJohn Marino { return __x.base() == __y.base(); }
346*e4b17023SJohn Marino
347*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
348*e4b17023SJohn Marino inline bool
349*e4b17023SJohn Marino operator<(const reverse_iterator<_IteratorL>& __x,
350*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
351*e4b17023SJohn Marino { return __y.base() < __x.base(); }
352*e4b17023SJohn Marino
353*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
354*e4b17023SJohn Marino inline bool
355*e4b17023SJohn Marino operator!=(const reverse_iterator<_IteratorL>& __x,
356*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
357*e4b17023SJohn Marino { return !(__x == __y); }
358*e4b17023SJohn Marino
359*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
360*e4b17023SJohn Marino inline bool
361*e4b17023SJohn Marino operator>(const reverse_iterator<_IteratorL>& __x,
362*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
363*e4b17023SJohn Marino { return __y < __x; }
364*e4b17023SJohn Marino
365*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
366*e4b17023SJohn Marino inline bool
367*e4b17023SJohn Marino operator<=(const reverse_iterator<_IteratorL>& __x,
368*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
369*e4b17023SJohn Marino { return !(__y < __x); }
370*e4b17023SJohn Marino
371*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
372*e4b17023SJohn Marino inline bool
373*e4b17023SJohn Marino operator>=(const reverse_iterator<_IteratorL>& __x,
374*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
375*e4b17023SJohn Marino { return !(__x < __y); }
376*e4b17023SJohn Marino
377*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
378*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
379*e4b17023SJohn Marino // DR 685.
380*e4b17023SJohn Marino inline auto
381*e4b17023SJohn Marino operator-(const reverse_iterator<_IteratorL>& __x,
382*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
383*e4b17023SJohn Marino -> decltype(__y.base() - __x.base())
384*e4b17023SJohn Marino #else
385*e4b17023SJohn Marino inline typename reverse_iterator<_IteratorL>::difference_type
386*e4b17023SJohn Marino operator-(const reverse_iterator<_IteratorL>& __x,
387*e4b17023SJohn Marino const reverse_iterator<_IteratorR>& __y)
388*e4b17023SJohn Marino #endif
389*e4b17023SJohn Marino { return __y.base() - __x.base(); }
390*e4b17023SJohn Marino //@}
391*e4b17023SJohn Marino
392*e4b17023SJohn Marino // 24.4.2.2.1 back_insert_iterator
393*e4b17023SJohn Marino /**
394*e4b17023SJohn Marino * @brief Turns assignment into insertion.
395*e4b17023SJohn Marino *
396*e4b17023SJohn Marino * These are output iterators, constructed from a container-of-T.
397*e4b17023SJohn Marino * Assigning a T to the iterator appends it to the container using
398*e4b17023SJohn Marino * push_back.
399*e4b17023SJohn Marino *
400*e4b17023SJohn Marino * Tip: Using the back_inserter function to create these iterators can
401*e4b17023SJohn Marino * save typing.
402*e4b17023SJohn Marino */
403*e4b17023SJohn Marino template<typename _Container>
404*e4b17023SJohn Marino class back_insert_iterator
405*e4b17023SJohn Marino : public iterator<output_iterator_tag, void, void, void, void>
406*e4b17023SJohn Marino {
407*e4b17023SJohn Marino protected:
408*e4b17023SJohn Marino _Container* container;
409*e4b17023SJohn Marino
410*e4b17023SJohn Marino public:
411*e4b17023SJohn Marino /// A nested typedef for the type of whatever container you used.
412*e4b17023SJohn Marino typedef _Container container_type;
413*e4b17023SJohn Marino
414*e4b17023SJohn Marino /// The only way to create this %iterator is with a container.
415*e4b17023SJohn Marino explicit
416*e4b17023SJohn Marino back_insert_iterator(_Container& __x) : container(&__x) { }
417*e4b17023SJohn Marino
418*e4b17023SJohn Marino /**
419*e4b17023SJohn Marino * @param __value An instance of whatever type
420*e4b17023SJohn Marino * container_type::const_reference is; presumably a
421*e4b17023SJohn Marino * reference-to-const T for container<T>.
422*e4b17023SJohn Marino * @return This %iterator, for chained operations.
423*e4b17023SJohn Marino *
424*e4b17023SJohn Marino * This kind of %iterator doesn't really have a @a position in the
425*e4b17023SJohn Marino * container (you can think of the position as being permanently at
426*e4b17023SJohn Marino * the end, if you like). Assigning a value to the %iterator will
427*e4b17023SJohn Marino * always append the value to the end of the container.
428*e4b17023SJohn Marino */
429*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
430*e4b17023SJohn Marino back_insert_iterator&
431*e4b17023SJohn Marino operator=(typename _Container::const_reference __value)
432*e4b17023SJohn Marino {
433*e4b17023SJohn Marino container->push_back(__value);
434*e4b17023SJohn Marino return *this;
435*e4b17023SJohn Marino }
436*e4b17023SJohn Marino #else
437*e4b17023SJohn Marino back_insert_iterator&
438*e4b17023SJohn Marino operator=(const typename _Container::value_type& __value)
439*e4b17023SJohn Marino {
440*e4b17023SJohn Marino container->push_back(__value);
441*e4b17023SJohn Marino return *this;
442*e4b17023SJohn Marino }
443*e4b17023SJohn Marino
444*e4b17023SJohn Marino back_insert_iterator&
445*e4b17023SJohn Marino operator=(typename _Container::value_type&& __value)
446*e4b17023SJohn Marino {
447*e4b17023SJohn Marino container->push_back(std::move(__value));
448*e4b17023SJohn Marino return *this;
449*e4b17023SJohn Marino }
450*e4b17023SJohn Marino #endif
451*e4b17023SJohn Marino
452*e4b17023SJohn Marino /// Simply returns *this.
453*e4b17023SJohn Marino back_insert_iterator&
454*e4b17023SJohn Marino operator*()
455*e4b17023SJohn Marino { return *this; }
456*e4b17023SJohn Marino
457*e4b17023SJohn Marino /// Simply returns *this. (This %iterator does not @a move.)
458*e4b17023SJohn Marino back_insert_iterator&
459*e4b17023SJohn Marino operator++()
460*e4b17023SJohn Marino { return *this; }
461*e4b17023SJohn Marino
462*e4b17023SJohn Marino /// Simply returns *this. (This %iterator does not @a move.)
463*e4b17023SJohn Marino back_insert_iterator
464*e4b17023SJohn Marino operator++(int)
465*e4b17023SJohn Marino { return *this; }
466*e4b17023SJohn Marino };
467*e4b17023SJohn Marino
468*e4b17023SJohn Marino /**
469*e4b17023SJohn Marino * @param __x A container of arbitrary type.
470*e4b17023SJohn Marino * @return An instance of back_insert_iterator working on @p __x.
471*e4b17023SJohn Marino *
472*e4b17023SJohn Marino * This wrapper function helps in creating back_insert_iterator instances.
473*e4b17023SJohn Marino * Typing the name of the %iterator requires knowing the precise full
474*e4b17023SJohn Marino * type of the container, which can be tedious and impedes generic
475*e4b17023SJohn Marino * programming. Using this function lets you take advantage of automatic
476*e4b17023SJohn Marino * template parameter deduction, making the compiler match the correct
477*e4b17023SJohn Marino * types for you.
478*e4b17023SJohn Marino */
479*e4b17023SJohn Marino template<typename _Container>
480*e4b17023SJohn Marino inline back_insert_iterator<_Container>
481*e4b17023SJohn Marino back_inserter(_Container& __x)
482*e4b17023SJohn Marino { return back_insert_iterator<_Container>(__x); }
483*e4b17023SJohn Marino
484*e4b17023SJohn Marino /**
485*e4b17023SJohn Marino * @brief Turns assignment into insertion.
486*e4b17023SJohn Marino *
487*e4b17023SJohn Marino * These are output iterators, constructed from a container-of-T.
488*e4b17023SJohn Marino * Assigning a T to the iterator prepends it to the container using
489*e4b17023SJohn Marino * push_front.
490*e4b17023SJohn Marino *
491*e4b17023SJohn Marino * Tip: Using the front_inserter function to create these iterators can
492*e4b17023SJohn Marino * save typing.
493*e4b17023SJohn Marino */
494*e4b17023SJohn Marino template<typename _Container>
495*e4b17023SJohn Marino class front_insert_iterator
496*e4b17023SJohn Marino : public iterator<output_iterator_tag, void, void, void, void>
497*e4b17023SJohn Marino {
498*e4b17023SJohn Marino protected:
499*e4b17023SJohn Marino _Container* container;
500*e4b17023SJohn Marino
501*e4b17023SJohn Marino public:
502*e4b17023SJohn Marino /// A nested typedef for the type of whatever container you used.
503*e4b17023SJohn Marino typedef _Container container_type;
504*e4b17023SJohn Marino
505*e4b17023SJohn Marino /// The only way to create this %iterator is with a container.
506*e4b17023SJohn Marino explicit front_insert_iterator(_Container& __x) : container(&__x) { }
507*e4b17023SJohn Marino
508*e4b17023SJohn Marino /**
509*e4b17023SJohn Marino * @param __value An instance of whatever type
510*e4b17023SJohn Marino * container_type::const_reference is; presumably a
511*e4b17023SJohn Marino * reference-to-const T for container<T>.
512*e4b17023SJohn Marino * @return This %iterator, for chained operations.
513*e4b17023SJohn Marino *
514*e4b17023SJohn Marino * This kind of %iterator doesn't really have a @a position in the
515*e4b17023SJohn Marino * container (you can think of the position as being permanently at
516*e4b17023SJohn Marino * the front, if you like). Assigning a value to the %iterator will
517*e4b17023SJohn Marino * always prepend the value to the front of the container.
518*e4b17023SJohn Marino */
519*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
520*e4b17023SJohn Marino front_insert_iterator&
521*e4b17023SJohn Marino operator=(typename _Container::const_reference __value)
522*e4b17023SJohn Marino {
523*e4b17023SJohn Marino container->push_front(__value);
524*e4b17023SJohn Marino return *this;
525*e4b17023SJohn Marino }
526*e4b17023SJohn Marino #else
527*e4b17023SJohn Marino front_insert_iterator&
528*e4b17023SJohn Marino operator=(const typename _Container::value_type& __value)
529*e4b17023SJohn Marino {
530*e4b17023SJohn Marino container->push_front(__value);
531*e4b17023SJohn Marino return *this;
532*e4b17023SJohn Marino }
533*e4b17023SJohn Marino
534*e4b17023SJohn Marino front_insert_iterator&
535*e4b17023SJohn Marino operator=(typename _Container::value_type&& __value)
536*e4b17023SJohn Marino {
537*e4b17023SJohn Marino container->push_front(std::move(__value));
538*e4b17023SJohn Marino return *this;
539*e4b17023SJohn Marino }
540*e4b17023SJohn Marino #endif
541*e4b17023SJohn Marino
542*e4b17023SJohn Marino /// Simply returns *this.
543*e4b17023SJohn Marino front_insert_iterator&
544*e4b17023SJohn Marino operator*()
545*e4b17023SJohn Marino { return *this; }
546*e4b17023SJohn Marino
547*e4b17023SJohn Marino /// Simply returns *this. (This %iterator does not @a move.)
548*e4b17023SJohn Marino front_insert_iterator&
549*e4b17023SJohn Marino operator++()
550*e4b17023SJohn Marino { return *this; }
551*e4b17023SJohn Marino
552*e4b17023SJohn Marino /// Simply returns *this. (This %iterator does not @a move.)
553*e4b17023SJohn Marino front_insert_iterator
554*e4b17023SJohn Marino operator++(int)
555*e4b17023SJohn Marino { return *this; }
556*e4b17023SJohn Marino };
557*e4b17023SJohn Marino
558*e4b17023SJohn Marino /**
559*e4b17023SJohn Marino * @param __x A container of arbitrary type.
560*e4b17023SJohn Marino * @return An instance of front_insert_iterator working on @p x.
561*e4b17023SJohn Marino *
562*e4b17023SJohn Marino * This wrapper function helps in creating front_insert_iterator instances.
563*e4b17023SJohn Marino * Typing the name of the %iterator requires knowing the precise full
564*e4b17023SJohn Marino * type of the container, which can be tedious and impedes generic
565*e4b17023SJohn Marino * programming. Using this function lets you take advantage of automatic
566*e4b17023SJohn Marino * template parameter deduction, making the compiler match the correct
567*e4b17023SJohn Marino * types for you.
568*e4b17023SJohn Marino */
569*e4b17023SJohn Marino template<typename _Container>
570*e4b17023SJohn Marino inline front_insert_iterator<_Container>
571*e4b17023SJohn Marino front_inserter(_Container& __x)
572*e4b17023SJohn Marino { return front_insert_iterator<_Container>(__x); }
573*e4b17023SJohn Marino
574*e4b17023SJohn Marino /**
575*e4b17023SJohn Marino * @brief Turns assignment into insertion.
576*e4b17023SJohn Marino *
577*e4b17023SJohn Marino * These are output iterators, constructed from a container-of-T.
578*e4b17023SJohn Marino * Assigning a T to the iterator inserts it in the container at the
579*e4b17023SJohn Marino * %iterator's position, rather than overwriting the value at that
580*e4b17023SJohn Marino * position.
581*e4b17023SJohn Marino *
582*e4b17023SJohn Marino * (Sequences will actually insert a @e copy of the value before the
583*e4b17023SJohn Marino * %iterator's position.)
584*e4b17023SJohn Marino *
585*e4b17023SJohn Marino * Tip: Using the inserter function to create these iterators can
586*e4b17023SJohn Marino * save typing.
587*e4b17023SJohn Marino */
588*e4b17023SJohn Marino template<typename _Container>
589*e4b17023SJohn Marino class insert_iterator
590*e4b17023SJohn Marino : public iterator<output_iterator_tag, void, void, void, void>
591*e4b17023SJohn Marino {
592*e4b17023SJohn Marino protected:
593*e4b17023SJohn Marino _Container* container;
594*e4b17023SJohn Marino typename _Container::iterator iter;
595*e4b17023SJohn Marino
596*e4b17023SJohn Marino public:
597*e4b17023SJohn Marino /// A nested typedef for the type of whatever container you used.
598*e4b17023SJohn Marino typedef _Container container_type;
599*e4b17023SJohn Marino
600*e4b17023SJohn Marino /**
601*e4b17023SJohn Marino * The only way to create this %iterator is with a container and an
602*e4b17023SJohn Marino * initial position (a normal %iterator into the container).
603*e4b17023SJohn Marino */
604*e4b17023SJohn Marino insert_iterator(_Container& __x, typename _Container::iterator __i)
605*e4b17023SJohn Marino : container(&__x), iter(__i) {}
606*e4b17023SJohn Marino
607*e4b17023SJohn Marino /**
608*e4b17023SJohn Marino * @param __value An instance of whatever type
609*e4b17023SJohn Marino * container_type::const_reference is; presumably a
610*e4b17023SJohn Marino * reference-to-const T for container<T>.
611*e4b17023SJohn Marino * @return This %iterator, for chained operations.
612*e4b17023SJohn Marino *
613*e4b17023SJohn Marino * This kind of %iterator maintains its own position in the
614*e4b17023SJohn Marino * container. Assigning a value to the %iterator will insert the
615*e4b17023SJohn Marino * value into the container at the place before the %iterator.
616*e4b17023SJohn Marino *
617*e4b17023SJohn Marino * The position is maintained such that subsequent assignments will
618*e4b17023SJohn Marino * insert values immediately after one another. For example,
619*e4b17023SJohn Marino * @code
620*e4b17023SJohn Marino * // vector v contains A and Z
621*e4b17023SJohn Marino *
622*e4b17023SJohn Marino * insert_iterator i (v, ++v.begin());
623*e4b17023SJohn Marino * i = 1;
624*e4b17023SJohn Marino * i = 2;
625*e4b17023SJohn Marino * i = 3;
626*e4b17023SJohn Marino *
627*e4b17023SJohn Marino * // vector v contains A, 1, 2, 3, and Z
628*e4b17023SJohn Marino * @endcode
629*e4b17023SJohn Marino */
630*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
631*e4b17023SJohn Marino insert_iterator&
632*e4b17023SJohn Marino operator=(typename _Container::const_reference __value)
633*e4b17023SJohn Marino {
634*e4b17023SJohn Marino iter = container->insert(iter, __value);
635*e4b17023SJohn Marino ++iter;
636*e4b17023SJohn Marino return *this;
637*e4b17023SJohn Marino }
638*e4b17023SJohn Marino #else
639*e4b17023SJohn Marino insert_iterator&
640*e4b17023SJohn Marino operator=(const typename _Container::value_type& __value)
641*e4b17023SJohn Marino {
642*e4b17023SJohn Marino iter = container->insert(iter, __value);
643*e4b17023SJohn Marino ++iter;
644*e4b17023SJohn Marino return *this;
645*e4b17023SJohn Marino }
646*e4b17023SJohn Marino
647*e4b17023SJohn Marino insert_iterator&
648*e4b17023SJohn Marino operator=(typename _Container::value_type&& __value)
649*e4b17023SJohn Marino {
650*e4b17023SJohn Marino iter = container->insert(iter, std::move(__value));
651*e4b17023SJohn Marino ++iter;
652*e4b17023SJohn Marino return *this;
653*e4b17023SJohn Marino }
654*e4b17023SJohn Marino #endif
655*e4b17023SJohn Marino
656*e4b17023SJohn Marino /// Simply returns *this.
657*e4b17023SJohn Marino insert_iterator&
658*e4b17023SJohn Marino operator*()
659*e4b17023SJohn Marino { return *this; }
660*e4b17023SJohn Marino
661*e4b17023SJohn Marino /// Simply returns *this. (This %iterator does not @a move.)
662*e4b17023SJohn Marino insert_iterator&
663*e4b17023SJohn Marino operator++()
664*e4b17023SJohn Marino { return *this; }
665*e4b17023SJohn Marino
666*e4b17023SJohn Marino /// Simply returns *this. (This %iterator does not @a move.)
667*e4b17023SJohn Marino insert_iterator&
668*e4b17023SJohn Marino operator++(int)
669*e4b17023SJohn Marino { return *this; }
670*e4b17023SJohn Marino };
671*e4b17023SJohn Marino
672*e4b17023SJohn Marino /**
673*e4b17023SJohn Marino * @param __x A container of arbitrary type.
674*e4b17023SJohn Marino * @return An instance of insert_iterator working on @p __x.
675*e4b17023SJohn Marino *
676*e4b17023SJohn Marino * This wrapper function helps in creating insert_iterator instances.
677*e4b17023SJohn Marino * Typing the name of the %iterator requires knowing the precise full
678*e4b17023SJohn Marino * type of the container, which can be tedious and impedes generic
679*e4b17023SJohn Marino * programming. Using this function lets you take advantage of automatic
680*e4b17023SJohn Marino * template parameter deduction, making the compiler match the correct
681*e4b17023SJohn Marino * types for you.
682*e4b17023SJohn Marino */
683*e4b17023SJohn Marino template<typename _Container, typename _Iterator>
684*e4b17023SJohn Marino inline insert_iterator<_Container>
685*e4b17023SJohn Marino inserter(_Container& __x, _Iterator __i)
686*e4b17023SJohn Marino {
687*e4b17023SJohn Marino return insert_iterator<_Container>(__x,
688*e4b17023SJohn Marino typename _Container::iterator(__i));
689*e4b17023SJohn Marino }
690*e4b17023SJohn Marino
691*e4b17023SJohn Marino // @} group iterators
692*e4b17023SJohn Marino
693*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
694*e4b17023SJohn Marino } // namespace
695*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)696*e4b17023SJohn Marino namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
697*e4b17023SJohn Marino {
698*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
699*e4b17023SJohn Marino
700*e4b17023SJohn Marino // This iterator adapter is @a normal in the sense that it does not
701*e4b17023SJohn Marino // change the semantics of any of the operators of its iterator
702*e4b17023SJohn Marino // parameter. Its primary purpose is to convert an iterator that is
703*e4b17023SJohn Marino // not a class, e.g. a pointer, into an iterator that is a class.
704*e4b17023SJohn Marino // The _Container parameter exists solely so that different containers
705*e4b17023SJohn Marino // using this template can instantiate different types, even if the
706*e4b17023SJohn Marino // _Iterator parameter is the same.
707*e4b17023SJohn Marino using std::iterator_traits;
708*e4b17023SJohn Marino using std::iterator;
709*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
710*e4b17023SJohn Marino class __normal_iterator
711*e4b17023SJohn Marino {
712*e4b17023SJohn Marino protected:
713*e4b17023SJohn Marino _Iterator _M_current;
714*e4b17023SJohn Marino
715*e4b17023SJohn Marino typedef iterator_traits<_Iterator> __traits_type;
716*e4b17023SJohn Marino
717*e4b17023SJohn Marino public:
718*e4b17023SJohn Marino typedef _Iterator iterator_type;
719*e4b17023SJohn Marino typedef typename __traits_type::iterator_category iterator_category;
720*e4b17023SJohn Marino typedef typename __traits_type::value_type value_type;
721*e4b17023SJohn Marino typedef typename __traits_type::difference_type difference_type;
722*e4b17023SJohn Marino typedef typename __traits_type::reference reference;
723*e4b17023SJohn Marino typedef typename __traits_type::pointer pointer;
724*e4b17023SJohn Marino
725*e4b17023SJohn Marino _GLIBCXX_CONSTEXPR __normal_iterator() : _M_current(_Iterator()) { }
726*e4b17023SJohn Marino
727*e4b17023SJohn Marino explicit
728*e4b17023SJohn Marino __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
729*e4b17023SJohn Marino
730*e4b17023SJohn Marino // Allow iterator to const_iterator conversion
731*e4b17023SJohn Marino template<typename _Iter>
732*e4b17023SJohn Marino __normal_iterator(const __normal_iterator<_Iter,
733*e4b17023SJohn Marino typename __enable_if<
734*e4b17023SJohn Marino (std::__are_same<_Iter, typename _Container::pointer>::__value),
735*e4b17023SJohn Marino _Container>::__type>& __i)
736*e4b17023SJohn Marino : _M_current(__i.base()) { }
737*e4b17023SJohn Marino
738*e4b17023SJohn Marino // Forward iterator requirements
739*e4b17023SJohn Marino reference
740*e4b17023SJohn Marino operator*() const
741*e4b17023SJohn Marino { return *_M_current; }
742*e4b17023SJohn Marino
743*e4b17023SJohn Marino pointer
744*e4b17023SJohn Marino operator->() const
745*e4b17023SJohn Marino { return _M_current; }
746*e4b17023SJohn Marino
747*e4b17023SJohn Marino __normal_iterator&
748*e4b17023SJohn Marino operator++()
749*e4b17023SJohn Marino {
750*e4b17023SJohn Marino ++_M_current;
751*e4b17023SJohn Marino return *this;
752*e4b17023SJohn Marino }
753*e4b17023SJohn Marino
754*e4b17023SJohn Marino __normal_iterator
755*e4b17023SJohn Marino operator++(int)
756*e4b17023SJohn Marino { return __normal_iterator(_M_current++); }
757*e4b17023SJohn Marino
758*e4b17023SJohn Marino // Bidirectional iterator requirements
759*e4b17023SJohn Marino __normal_iterator&
760*e4b17023SJohn Marino operator--()
761*e4b17023SJohn Marino {
762*e4b17023SJohn Marino --_M_current;
763*e4b17023SJohn Marino return *this;
764*e4b17023SJohn Marino }
765*e4b17023SJohn Marino
766*e4b17023SJohn Marino __normal_iterator
767*e4b17023SJohn Marino operator--(int)
768*e4b17023SJohn Marino { return __normal_iterator(_M_current--); }
769*e4b17023SJohn Marino
770*e4b17023SJohn Marino // Random access iterator requirements
771*e4b17023SJohn Marino reference
772*e4b17023SJohn Marino operator[](const difference_type& __n) const
773*e4b17023SJohn Marino { return _M_current[__n]; }
774*e4b17023SJohn Marino
775*e4b17023SJohn Marino __normal_iterator&
776*e4b17023SJohn Marino operator+=(const difference_type& __n)
777*e4b17023SJohn Marino { _M_current += __n; return *this; }
778*e4b17023SJohn Marino
779*e4b17023SJohn Marino __normal_iterator
780*e4b17023SJohn Marino operator+(const difference_type& __n) const
781*e4b17023SJohn Marino { return __normal_iterator(_M_current + __n); }
782*e4b17023SJohn Marino
783*e4b17023SJohn Marino __normal_iterator&
784*e4b17023SJohn Marino operator-=(const difference_type& __n)
785*e4b17023SJohn Marino { _M_current -= __n; return *this; }
786*e4b17023SJohn Marino
787*e4b17023SJohn Marino __normal_iterator
788*e4b17023SJohn Marino operator-(const difference_type& __n) const
789*e4b17023SJohn Marino { return __normal_iterator(_M_current - __n); }
790*e4b17023SJohn Marino
791*e4b17023SJohn Marino const _Iterator&
792*e4b17023SJohn Marino base() const
793*e4b17023SJohn Marino { return _M_current; }
794*e4b17023SJohn Marino };
795*e4b17023SJohn Marino
796*e4b17023SJohn Marino // Note: In what follows, the left- and right-hand-side iterators are
797*e4b17023SJohn Marino // allowed to vary in types (conceptually in cv-qualification) so that
798*e4b17023SJohn Marino // comparison between cv-qualified and non-cv-qualified iterators be
799*e4b17023SJohn Marino // valid. However, the greedy and unfriendly operators in std::rel_ops
800*e4b17023SJohn Marino // will make overload resolution ambiguous (when in scope) if we don't
801*e4b17023SJohn Marino // provide overloads whose operands are of the same type. Can someone
802*e4b17023SJohn Marino // remind me what generic programming is about? -- Gaby
803*e4b17023SJohn Marino
804*e4b17023SJohn Marino // Forward iterator requirements
805*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
806*e4b17023SJohn Marino inline bool
807*e4b17023SJohn Marino operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
808*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
809*e4b17023SJohn Marino { return __lhs.base() == __rhs.base(); }
810*e4b17023SJohn Marino
811*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
812*e4b17023SJohn Marino inline bool
813*e4b17023SJohn Marino operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
814*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
815*e4b17023SJohn Marino { return __lhs.base() == __rhs.base(); }
816*e4b17023SJohn Marino
817*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
818*e4b17023SJohn Marino inline bool
819*e4b17023SJohn Marino operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
820*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
821*e4b17023SJohn Marino { return __lhs.base() != __rhs.base(); }
822*e4b17023SJohn Marino
823*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
824*e4b17023SJohn Marino inline bool
825*e4b17023SJohn Marino operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
826*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
827*e4b17023SJohn Marino { return __lhs.base() != __rhs.base(); }
828*e4b17023SJohn Marino
829*e4b17023SJohn Marino // Random access iterator requirements
830*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
831*e4b17023SJohn Marino inline bool
832*e4b17023SJohn Marino operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
833*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
834*e4b17023SJohn Marino { return __lhs.base() < __rhs.base(); }
835*e4b17023SJohn Marino
836*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
837*e4b17023SJohn Marino inline bool
838*e4b17023SJohn Marino operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
839*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
840*e4b17023SJohn Marino { return __lhs.base() < __rhs.base(); }
841*e4b17023SJohn Marino
842*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
843*e4b17023SJohn Marino inline bool
844*e4b17023SJohn Marino operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
845*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
846*e4b17023SJohn Marino { return __lhs.base() > __rhs.base(); }
847*e4b17023SJohn Marino
848*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
849*e4b17023SJohn Marino inline bool
850*e4b17023SJohn Marino operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
851*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
852*e4b17023SJohn Marino { return __lhs.base() > __rhs.base(); }
853*e4b17023SJohn Marino
854*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
855*e4b17023SJohn Marino inline bool
856*e4b17023SJohn Marino operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
857*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
858*e4b17023SJohn Marino { return __lhs.base() <= __rhs.base(); }
859*e4b17023SJohn Marino
860*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
861*e4b17023SJohn Marino inline bool
862*e4b17023SJohn Marino operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
863*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
864*e4b17023SJohn Marino { return __lhs.base() <= __rhs.base(); }
865*e4b17023SJohn Marino
866*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
867*e4b17023SJohn Marino inline bool
868*e4b17023SJohn Marino operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
869*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
870*e4b17023SJohn Marino { return __lhs.base() >= __rhs.base(); }
871*e4b17023SJohn Marino
872*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
873*e4b17023SJohn Marino inline bool
874*e4b17023SJohn Marino operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
875*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
876*e4b17023SJohn Marino { return __lhs.base() >= __rhs.base(); }
877*e4b17023SJohn Marino
878*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
879*e4b17023SJohn Marino // According to the resolution of DR179 not only the various comparison
880*e4b17023SJohn Marino // operators but also operator- must accept mixed iterator/const_iterator
881*e4b17023SJohn Marino // parameters.
882*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR, typename _Container>
883*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
884*e4b17023SJohn Marino // DR 685.
885*e4b17023SJohn Marino inline auto
886*e4b17023SJohn Marino operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
887*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
888*e4b17023SJohn Marino -> decltype(__lhs.base() - __rhs.base())
889*e4b17023SJohn Marino #else
890*e4b17023SJohn Marino inline typename __normal_iterator<_IteratorL, _Container>::difference_type
891*e4b17023SJohn Marino operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
892*e4b17023SJohn Marino const __normal_iterator<_IteratorR, _Container>& __rhs)
893*e4b17023SJohn Marino #endif
894*e4b17023SJohn Marino { return __lhs.base() - __rhs.base(); }
895*e4b17023SJohn Marino
896*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
897*e4b17023SJohn Marino inline typename __normal_iterator<_Iterator, _Container>::difference_type
898*e4b17023SJohn Marino operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
899*e4b17023SJohn Marino const __normal_iterator<_Iterator, _Container>& __rhs)
900*e4b17023SJohn Marino { return __lhs.base() - __rhs.base(); }
901*e4b17023SJohn Marino
902*e4b17023SJohn Marino template<typename _Iterator, typename _Container>
903*e4b17023SJohn Marino inline __normal_iterator<_Iterator, _Container>
904*e4b17023SJohn Marino operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
905*e4b17023SJohn Marino __n, const __normal_iterator<_Iterator, _Container>& __i)
906*e4b17023SJohn Marino { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
907*e4b17023SJohn Marino
908*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
909*e4b17023SJohn Marino } // namespace
910*e4b17023SJohn Marino
911*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
912*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)913*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
914*e4b17023SJohn Marino {
915*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
916*e4b17023SJohn Marino
917*e4b17023SJohn Marino /**
918*e4b17023SJohn Marino * @addtogroup iterators
919*e4b17023SJohn Marino * @{
920*e4b17023SJohn Marino */
921*e4b17023SJohn Marino
922*e4b17023SJohn Marino // 24.4.3 Move iterators
923*e4b17023SJohn Marino /**
924*e4b17023SJohn Marino * Class template move_iterator is an iterator adapter with the same
925*e4b17023SJohn Marino * behavior as the underlying iterator except that its dereference
926*e4b17023SJohn Marino * operator implicitly converts the value returned by the underlying
927*e4b17023SJohn Marino * iterator's dereference operator to an rvalue reference. Some
928*e4b17023SJohn Marino * generic algorithms can be called with move iterators to replace
929*e4b17023SJohn Marino * copying with moving.
930*e4b17023SJohn Marino */
931*e4b17023SJohn Marino template<typename _Iterator>
932*e4b17023SJohn Marino class move_iterator
933*e4b17023SJohn Marino {
934*e4b17023SJohn Marino protected:
935*e4b17023SJohn Marino _Iterator _M_current;
936*e4b17023SJohn Marino
937*e4b17023SJohn Marino typedef iterator_traits<_Iterator> __traits_type;
938*e4b17023SJohn Marino
939*e4b17023SJohn Marino public:
940*e4b17023SJohn Marino typedef _Iterator iterator_type;
941*e4b17023SJohn Marino typedef typename __traits_type::iterator_category iterator_category;
942*e4b17023SJohn Marino typedef typename __traits_type::value_type value_type;
943*e4b17023SJohn Marino typedef typename __traits_type::difference_type difference_type;
944*e4b17023SJohn Marino // NB: DR 680.
945*e4b17023SJohn Marino typedef _Iterator pointer;
946*e4b17023SJohn Marino typedef value_type&& reference;
947*e4b17023SJohn Marino
948*e4b17023SJohn Marino move_iterator()
949*e4b17023SJohn Marino : _M_current() { }
950*e4b17023SJohn Marino
951*e4b17023SJohn Marino explicit
952*e4b17023SJohn Marino move_iterator(iterator_type __i)
953*e4b17023SJohn Marino : _M_current(__i) { }
954*e4b17023SJohn Marino
955*e4b17023SJohn Marino template<typename _Iter>
956*e4b17023SJohn Marino move_iterator(const move_iterator<_Iter>& __i)
957*e4b17023SJohn Marino : _M_current(__i.base()) { }
958*e4b17023SJohn Marino
959*e4b17023SJohn Marino iterator_type
960*e4b17023SJohn Marino base() const
961*e4b17023SJohn Marino { return _M_current; }
962*e4b17023SJohn Marino
963*e4b17023SJohn Marino reference
964*e4b17023SJohn Marino operator*() const
965*e4b17023SJohn Marino { return std::move(*_M_current); }
966*e4b17023SJohn Marino
967*e4b17023SJohn Marino pointer
968*e4b17023SJohn Marino operator->() const
969*e4b17023SJohn Marino { return _M_current; }
970*e4b17023SJohn Marino
971*e4b17023SJohn Marino move_iterator&
972*e4b17023SJohn Marino operator++()
973*e4b17023SJohn Marino {
974*e4b17023SJohn Marino ++_M_current;
975*e4b17023SJohn Marino return *this;
976*e4b17023SJohn Marino }
977*e4b17023SJohn Marino
978*e4b17023SJohn Marino move_iterator
979*e4b17023SJohn Marino operator++(int)
980*e4b17023SJohn Marino {
981*e4b17023SJohn Marino move_iterator __tmp = *this;
982*e4b17023SJohn Marino ++_M_current;
983*e4b17023SJohn Marino return __tmp;
984*e4b17023SJohn Marino }
985*e4b17023SJohn Marino
986*e4b17023SJohn Marino move_iterator&
987*e4b17023SJohn Marino operator--()
988*e4b17023SJohn Marino {
989*e4b17023SJohn Marino --_M_current;
990*e4b17023SJohn Marino return *this;
991*e4b17023SJohn Marino }
992*e4b17023SJohn Marino
993*e4b17023SJohn Marino move_iterator
994*e4b17023SJohn Marino operator--(int)
995*e4b17023SJohn Marino {
996*e4b17023SJohn Marino move_iterator __tmp = *this;
997*e4b17023SJohn Marino --_M_current;
998*e4b17023SJohn Marino return __tmp;
999*e4b17023SJohn Marino }
1000*e4b17023SJohn Marino
1001*e4b17023SJohn Marino move_iterator
1002*e4b17023SJohn Marino operator+(difference_type __n) const
1003*e4b17023SJohn Marino { return move_iterator(_M_current + __n); }
1004*e4b17023SJohn Marino
1005*e4b17023SJohn Marino move_iterator&
1006*e4b17023SJohn Marino operator+=(difference_type __n)
1007*e4b17023SJohn Marino {
1008*e4b17023SJohn Marino _M_current += __n;
1009*e4b17023SJohn Marino return *this;
1010*e4b17023SJohn Marino }
1011*e4b17023SJohn Marino
1012*e4b17023SJohn Marino move_iterator
1013*e4b17023SJohn Marino operator-(difference_type __n) const
1014*e4b17023SJohn Marino { return move_iterator(_M_current - __n); }
1015*e4b17023SJohn Marino
1016*e4b17023SJohn Marino move_iterator&
1017*e4b17023SJohn Marino operator-=(difference_type __n)
1018*e4b17023SJohn Marino {
1019*e4b17023SJohn Marino _M_current -= __n;
1020*e4b17023SJohn Marino return *this;
1021*e4b17023SJohn Marino }
1022*e4b17023SJohn Marino
1023*e4b17023SJohn Marino reference
1024*e4b17023SJohn Marino operator[](difference_type __n) const
1025*e4b17023SJohn Marino { return std::move(_M_current[__n]); }
1026*e4b17023SJohn Marino };
1027*e4b17023SJohn Marino
1028*e4b17023SJohn Marino // Note: See __normal_iterator operators note from Gaby to understand
1029*e4b17023SJohn Marino // why there are always 2 versions for most of the move_iterator
1030*e4b17023SJohn Marino // operators.
1031*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1032*e4b17023SJohn Marino inline bool
1033*e4b17023SJohn Marino operator==(const move_iterator<_IteratorL>& __x,
1034*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1035*e4b17023SJohn Marino { return __x.base() == __y.base(); }
1036*e4b17023SJohn Marino
1037*e4b17023SJohn Marino template<typename _Iterator>
1038*e4b17023SJohn Marino inline bool
1039*e4b17023SJohn Marino operator==(const move_iterator<_Iterator>& __x,
1040*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1041*e4b17023SJohn Marino { return __x.base() == __y.base(); }
1042*e4b17023SJohn Marino
1043*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1044*e4b17023SJohn Marino inline bool
1045*e4b17023SJohn Marino operator!=(const move_iterator<_IteratorL>& __x,
1046*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1047*e4b17023SJohn Marino { return !(__x == __y); }
1048*e4b17023SJohn Marino
1049*e4b17023SJohn Marino template<typename _Iterator>
1050*e4b17023SJohn Marino inline bool
1051*e4b17023SJohn Marino operator!=(const move_iterator<_Iterator>& __x,
1052*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1053*e4b17023SJohn Marino { return !(__x == __y); }
1054*e4b17023SJohn Marino
1055*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1056*e4b17023SJohn Marino inline bool
1057*e4b17023SJohn Marino operator<(const move_iterator<_IteratorL>& __x,
1058*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1059*e4b17023SJohn Marino { return __x.base() < __y.base(); }
1060*e4b17023SJohn Marino
1061*e4b17023SJohn Marino template<typename _Iterator>
1062*e4b17023SJohn Marino inline bool
1063*e4b17023SJohn Marino operator<(const move_iterator<_Iterator>& __x,
1064*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1065*e4b17023SJohn Marino { return __x.base() < __y.base(); }
1066*e4b17023SJohn Marino
1067*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1068*e4b17023SJohn Marino inline bool
1069*e4b17023SJohn Marino operator<=(const move_iterator<_IteratorL>& __x,
1070*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1071*e4b17023SJohn Marino { return !(__y < __x); }
1072*e4b17023SJohn Marino
1073*e4b17023SJohn Marino template<typename _Iterator>
1074*e4b17023SJohn Marino inline bool
1075*e4b17023SJohn Marino operator<=(const move_iterator<_Iterator>& __x,
1076*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1077*e4b17023SJohn Marino { return !(__y < __x); }
1078*e4b17023SJohn Marino
1079*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1080*e4b17023SJohn Marino inline bool
1081*e4b17023SJohn Marino operator>(const move_iterator<_IteratorL>& __x,
1082*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1083*e4b17023SJohn Marino { return __y < __x; }
1084*e4b17023SJohn Marino
1085*e4b17023SJohn Marino template<typename _Iterator>
1086*e4b17023SJohn Marino inline bool
1087*e4b17023SJohn Marino operator>(const move_iterator<_Iterator>& __x,
1088*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1089*e4b17023SJohn Marino { return __y < __x; }
1090*e4b17023SJohn Marino
1091*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1092*e4b17023SJohn Marino inline bool
1093*e4b17023SJohn Marino operator>=(const move_iterator<_IteratorL>& __x,
1094*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1095*e4b17023SJohn Marino { return !(__x < __y); }
1096*e4b17023SJohn Marino
1097*e4b17023SJohn Marino template<typename _Iterator>
1098*e4b17023SJohn Marino inline bool
1099*e4b17023SJohn Marino operator>=(const move_iterator<_Iterator>& __x,
1100*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1101*e4b17023SJohn Marino { return !(__x < __y); }
1102*e4b17023SJohn Marino
1103*e4b17023SJohn Marino // DR 685.
1104*e4b17023SJohn Marino template<typename _IteratorL, typename _IteratorR>
1105*e4b17023SJohn Marino inline auto
1106*e4b17023SJohn Marino operator-(const move_iterator<_IteratorL>& __x,
1107*e4b17023SJohn Marino const move_iterator<_IteratorR>& __y)
1108*e4b17023SJohn Marino -> decltype(__x.base() - __y.base())
1109*e4b17023SJohn Marino { return __x.base() - __y.base(); }
1110*e4b17023SJohn Marino
1111*e4b17023SJohn Marino template<typename _Iterator>
1112*e4b17023SJohn Marino inline auto
1113*e4b17023SJohn Marino operator-(const move_iterator<_Iterator>& __x,
1114*e4b17023SJohn Marino const move_iterator<_Iterator>& __y)
1115*e4b17023SJohn Marino -> decltype(__x.base() - __y.base())
1116*e4b17023SJohn Marino { return __x.base() - __y.base(); }
1117*e4b17023SJohn Marino
1118*e4b17023SJohn Marino template<typename _Iterator>
1119*e4b17023SJohn Marino inline move_iterator<_Iterator>
1120*e4b17023SJohn Marino operator+(typename move_iterator<_Iterator>::difference_type __n,
1121*e4b17023SJohn Marino const move_iterator<_Iterator>& __x)
1122*e4b17023SJohn Marino { return __x + __n; }
1123*e4b17023SJohn Marino
1124*e4b17023SJohn Marino template<typename _Iterator>
1125*e4b17023SJohn Marino inline move_iterator<_Iterator>
1126*e4b17023SJohn Marino make_move_iterator(_Iterator __i)
1127*e4b17023SJohn Marino { return move_iterator<_Iterator>(__i); }
1128*e4b17023SJohn Marino
1129*e4b17023SJohn Marino template<typename _Iterator, typename _ReturnType
1130*e4b17023SJohn Marino = typename conditional<__move_if_noexcept_cond
1131*e4b17023SJohn Marino <typename iterator_traits<_Iterator>::value_type>::value,
1132*e4b17023SJohn Marino _Iterator, move_iterator<_Iterator>>::type>
1133*e4b17023SJohn Marino inline _ReturnType
1134*e4b17023SJohn Marino __make_move_if_noexcept_iterator(_Iterator __i)
1135*e4b17023SJohn Marino { return _ReturnType(__i); }
1136*e4b17023SJohn Marino
1137*e4b17023SJohn Marino // @} group iterators
1138*e4b17023SJohn Marino
1139*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
1140*e4b17023SJohn Marino } // namespace
1141*e4b17023SJohn Marino
1142*e4b17023SJohn Marino #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1143*e4b17023SJohn Marino #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1144*e4b17023SJohn Marino std::__make_move_if_noexcept_iterator(_Iter)
1145*e4b17023SJohn Marino #else
1146*e4b17023SJohn Marino #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1147*e4b17023SJohn Marino #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1148*e4b17023SJohn Marino #endif // __GXX_EXPERIMENTAL_CXX0X__
1149*e4b17023SJohn Marino
1150*e4b17023SJohn Marino #endif
1151