xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/stl_iterator.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Iterators -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert /*
32*404b540aSrobert  *
33*404b540aSrobert  * Copyright (c) 1994
34*404b540aSrobert  * Hewlett-Packard Company
35*404b540aSrobert  *
36*404b540aSrobert  * Permission to use, copy, modify, distribute and sell this software
37*404b540aSrobert  * and its documentation for any purpose is hereby granted without fee,
38*404b540aSrobert  * provided that the above copyright notice appear in all copies and
39*404b540aSrobert  * that both that copyright notice and this permission notice appear
40*404b540aSrobert  * in supporting documentation.  Hewlett-Packard Company makes no
41*404b540aSrobert  * representations about the suitability of this software for any
42*404b540aSrobert  * purpose.  It is provided "as is" without express or implied warranty.
43*404b540aSrobert  *
44*404b540aSrobert  *
45*404b540aSrobert  * Copyright (c) 1996-1998
46*404b540aSrobert  * Silicon Graphics Computer Systems, Inc.
47*404b540aSrobert  *
48*404b540aSrobert  * Permission to use, copy, modify, distribute and sell this software
49*404b540aSrobert  * and its documentation for any purpose is hereby granted without fee,
50*404b540aSrobert  * provided that the above copyright notice appear in all copies and
51*404b540aSrobert  * that both that copyright notice and this permission notice appear
52*404b540aSrobert  * in supporting documentation.  Silicon Graphics makes no
53*404b540aSrobert  * representations about the suitability of this software for any
54*404b540aSrobert  * purpose.  It is provided "as is" without express or implied warranty.
55*404b540aSrobert  */
56*404b540aSrobert 
57*404b540aSrobert /** @file stl_iterator.h
58*404b540aSrobert  *  This is an internal header file, included by other library headers.
59*404b540aSrobert  *  You should not attempt to use it directly.
60*404b540aSrobert  *
61*404b540aSrobert  *  This file implements reverse_iterator, back_insert_iterator,
62*404b540aSrobert  *  front_insert_iterator, insert_iterator, __normal_iterator, and their
63*404b540aSrobert  *  supporting functions and overloaded operators.
64*404b540aSrobert  */
65*404b540aSrobert 
66*404b540aSrobert #ifndef _ITERATOR_H
67*404b540aSrobert #define _ITERATOR_H 1
68*404b540aSrobert 
69*404b540aSrobert #include <bits/cpp_type_traits.h>
70*404b540aSrobert #include <ext/type_traits.h>
71*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(std)72*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
73*404b540aSrobert 
74*404b540aSrobert   // 24.4.1 Reverse iterators
75*404b540aSrobert   /**
76*404b540aSrobert    *  "Bidirectional and random access iterators have corresponding reverse
77*404b540aSrobert    *  %iterator adaptors that iterate through the data structure in the
78*404b540aSrobert    *  opposite direction.  They have the same signatures as the corresponding
79*404b540aSrobert    *  iterators.  The fundamental relation between a reverse %iterator and its
80*404b540aSrobert    *  corresponding %iterator @c i is established by the identity:
81*404b540aSrobert    *  @code
82*404b540aSrobert    *      &*(reverse_iterator(i)) == &*(i - 1)
83*404b540aSrobert    *  @endcode
84*404b540aSrobert    *
85*404b540aSrobert    *  This mapping is dictated by the fact that while there is always a
86*404b540aSrobert    *  pointer past the end of an array, there might not be a valid pointer
87*404b540aSrobert    *  before the beginning of an array." [24.4.1]/1,2
88*404b540aSrobert    *
89*404b540aSrobert    *  Reverse iterators can be tricky and surprising at first.  Their
90*404b540aSrobert    *  semantics make sense, however, and the trickiness is a side effect of
91*404b540aSrobert    *  the requirement that the iterators must be safe.
92*404b540aSrobert   */
93*404b540aSrobert   template<typename _Iterator>
94*404b540aSrobert     class reverse_iterator
95*404b540aSrobert     : public iterator<typename iterator_traits<_Iterator>::iterator_category,
96*404b540aSrobert 		      typename iterator_traits<_Iterator>::value_type,
97*404b540aSrobert 		      typename iterator_traits<_Iterator>::difference_type,
98*404b540aSrobert 		      typename iterator_traits<_Iterator>::pointer,
99*404b540aSrobert                       typename iterator_traits<_Iterator>::reference>
100*404b540aSrobert     {
101*404b540aSrobert     protected:
102*404b540aSrobert       _Iterator current;
103*404b540aSrobert 
104*404b540aSrobert     public:
105*404b540aSrobert       typedef _Iterator					       iterator_type;
106*404b540aSrobert       typedef typename iterator_traits<_Iterator>::difference_type
107*404b540aSrobert 							       difference_type;
108*404b540aSrobert       typedef typename iterator_traits<_Iterator>::reference   reference;
109*404b540aSrobert       typedef typename iterator_traits<_Iterator>::pointer     pointer;
110*404b540aSrobert 
111*404b540aSrobert     public:
112*404b540aSrobert       /**
113*404b540aSrobert        *  The default constructor default-initializes member @p current.
114*404b540aSrobert        *  If it is a pointer, that means it is zero-initialized.
115*404b540aSrobert       */
116*404b540aSrobert       // _GLIBCXX_RESOLVE_LIB_DEFECTS
117*404b540aSrobert       // 235 No specification of default ctor for reverse_iterator
118*404b540aSrobert       reverse_iterator() : current() { }
119*404b540aSrobert 
120*404b540aSrobert       /**
121*404b540aSrobert        *  This %iterator will move in the opposite direction that @p x does.
122*404b540aSrobert       */
123*404b540aSrobert       explicit
124*404b540aSrobert       reverse_iterator(iterator_type __x) : current(__x) { }
125*404b540aSrobert 
126*404b540aSrobert       /**
127*404b540aSrobert        *  The copy constructor is normal.
128*404b540aSrobert       */
129*404b540aSrobert       reverse_iterator(const reverse_iterator& __x)
130*404b540aSrobert       : current(__x.current) { }
131*404b540aSrobert 
132*404b540aSrobert       /**
133*404b540aSrobert        *  A reverse_iterator across other types can be copied in the normal
134*404b540aSrobert        *  fashion.
135*404b540aSrobert       */
136*404b540aSrobert       template<typename _Iter>
137*404b540aSrobert         reverse_iterator(const reverse_iterator<_Iter>& __x)
138*404b540aSrobert 	: current(__x.base()) { }
139*404b540aSrobert 
140*404b540aSrobert       /**
141*404b540aSrobert        *  @return  @c current, the %iterator used for underlying work.
142*404b540aSrobert       */
143*404b540aSrobert       iterator_type
144*404b540aSrobert       base() const
145*404b540aSrobert       { return current; }
146*404b540aSrobert 
147*404b540aSrobert       /**
148*404b540aSrobert        *  @return  TODO
149*404b540aSrobert        *
150*404b540aSrobert        *  @doctodo
151*404b540aSrobert       */
152*404b540aSrobert       reference
153*404b540aSrobert       operator*() const
154*404b540aSrobert       {
155*404b540aSrobert 	_Iterator __tmp = current;
156*404b540aSrobert 	return *--__tmp;
157*404b540aSrobert       }
158*404b540aSrobert 
159*404b540aSrobert       /**
160*404b540aSrobert        *  @return  TODO
161*404b540aSrobert        *
162*404b540aSrobert        *  @doctodo
163*404b540aSrobert       */
164*404b540aSrobert       pointer
165*404b540aSrobert       operator->() const
166*404b540aSrobert       { return &(operator*()); }
167*404b540aSrobert 
168*404b540aSrobert       /**
169*404b540aSrobert        *  @return  TODO
170*404b540aSrobert        *
171*404b540aSrobert        *  @doctodo
172*404b540aSrobert       */
173*404b540aSrobert       reverse_iterator&
174*404b540aSrobert       operator++()
175*404b540aSrobert       {
176*404b540aSrobert 	--current;
177*404b540aSrobert 	return *this;
178*404b540aSrobert       }
179*404b540aSrobert 
180*404b540aSrobert       /**
181*404b540aSrobert        *  @return  TODO
182*404b540aSrobert        *
183*404b540aSrobert        *  @doctodo
184*404b540aSrobert       */
185*404b540aSrobert       reverse_iterator
186*404b540aSrobert       operator++(int)
187*404b540aSrobert       {
188*404b540aSrobert 	reverse_iterator __tmp = *this;
189*404b540aSrobert 	--current;
190*404b540aSrobert 	return __tmp;
191*404b540aSrobert       }
192*404b540aSrobert 
193*404b540aSrobert       /**
194*404b540aSrobert        *  @return  TODO
195*404b540aSrobert        *
196*404b540aSrobert        *  @doctodo
197*404b540aSrobert       */
198*404b540aSrobert       reverse_iterator&
199*404b540aSrobert       operator--()
200*404b540aSrobert       {
201*404b540aSrobert 	++current;
202*404b540aSrobert 	return *this;
203*404b540aSrobert       }
204*404b540aSrobert 
205*404b540aSrobert       /**
206*404b540aSrobert        *  @return  TODO
207*404b540aSrobert        *
208*404b540aSrobert        *  @doctodo
209*404b540aSrobert       */
210*404b540aSrobert       reverse_iterator
211*404b540aSrobert       operator--(int)
212*404b540aSrobert       {
213*404b540aSrobert 	reverse_iterator __tmp = *this;
214*404b540aSrobert 	++current;
215*404b540aSrobert 	return __tmp;
216*404b540aSrobert       }
217*404b540aSrobert 
218*404b540aSrobert       /**
219*404b540aSrobert        *  @return  TODO
220*404b540aSrobert        *
221*404b540aSrobert        *  @doctodo
222*404b540aSrobert       */
223*404b540aSrobert       reverse_iterator
224*404b540aSrobert       operator+(difference_type __n) const
225*404b540aSrobert       { return reverse_iterator(current - __n); }
226*404b540aSrobert 
227*404b540aSrobert       /**
228*404b540aSrobert        *  @return  TODO
229*404b540aSrobert        *
230*404b540aSrobert        *  @doctodo
231*404b540aSrobert       */
232*404b540aSrobert       reverse_iterator&
233*404b540aSrobert       operator+=(difference_type __n)
234*404b540aSrobert       {
235*404b540aSrobert 	current -= __n;
236*404b540aSrobert 	return *this;
237*404b540aSrobert       }
238*404b540aSrobert 
239*404b540aSrobert       /**
240*404b540aSrobert        *  @return  TODO
241*404b540aSrobert        *
242*404b540aSrobert        *  @doctodo
243*404b540aSrobert       */
244*404b540aSrobert       reverse_iterator
245*404b540aSrobert       operator-(difference_type __n) const
246*404b540aSrobert       { return reverse_iterator(current + __n); }
247*404b540aSrobert 
248*404b540aSrobert       /**
249*404b540aSrobert        *  @return  TODO
250*404b540aSrobert        *
251*404b540aSrobert        *  @doctodo
252*404b540aSrobert       */
253*404b540aSrobert       reverse_iterator&
254*404b540aSrobert       operator-=(difference_type __n)
255*404b540aSrobert       {
256*404b540aSrobert 	current += __n;
257*404b540aSrobert 	return *this;
258*404b540aSrobert       }
259*404b540aSrobert 
260*404b540aSrobert       /**
261*404b540aSrobert        *  @return  TODO
262*404b540aSrobert        *
263*404b540aSrobert        *  @doctodo
264*404b540aSrobert       */
265*404b540aSrobert       reference
266*404b540aSrobert       operator[](difference_type __n) const
267*404b540aSrobert       { return *(*this + __n); }
268*404b540aSrobert     };
269*404b540aSrobert 
270*404b540aSrobert   //@{
271*404b540aSrobert   /**
272*404b540aSrobert    *  @param  x  A %reverse_iterator.
273*404b540aSrobert    *  @param  y  A %reverse_iterator.
274*404b540aSrobert    *  @return  A simple bool.
275*404b540aSrobert    *
276*404b540aSrobert    *  Reverse iterators forward many operations to their underlying base()
277*404b540aSrobert    *  iterators.  Others are implemented in terms of one another.
278*404b540aSrobert    *
279*404b540aSrobert   */
280*404b540aSrobert   template<typename _Iterator>
281*404b540aSrobert     inline bool
282*404b540aSrobert     operator==(const reverse_iterator<_Iterator>& __x,
283*404b540aSrobert 	       const reverse_iterator<_Iterator>& __y)
284*404b540aSrobert     { return __x.base() == __y.base(); }
285*404b540aSrobert 
286*404b540aSrobert   template<typename _Iterator>
287*404b540aSrobert     inline bool
288*404b540aSrobert     operator<(const reverse_iterator<_Iterator>& __x,
289*404b540aSrobert 	      const reverse_iterator<_Iterator>& __y)
290*404b540aSrobert     { return __y.base() < __x.base(); }
291*404b540aSrobert 
292*404b540aSrobert   template<typename _Iterator>
293*404b540aSrobert     inline bool
294*404b540aSrobert     operator!=(const reverse_iterator<_Iterator>& __x,
295*404b540aSrobert 	       const reverse_iterator<_Iterator>& __y)
296*404b540aSrobert     { return !(__x == __y); }
297*404b540aSrobert 
298*404b540aSrobert   template<typename _Iterator>
299*404b540aSrobert     inline bool
300*404b540aSrobert     operator>(const reverse_iterator<_Iterator>& __x,
301*404b540aSrobert 	      const reverse_iterator<_Iterator>& __y)
302*404b540aSrobert     { return __y < __x; }
303*404b540aSrobert 
304*404b540aSrobert   template<typename _Iterator>
305*404b540aSrobert     inline bool
306*404b540aSrobert     operator<=(const reverse_iterator<_Iterator>& __x,
307*404b540aSrobert 	       const reverse_iterator<_Iterator>& __y)
308*404b540aSrobert     { return !(__y < __x); }
309*404b540aSrobert 
310*404b540aSrobert   template<typename _Iterator>
311*404b540aSrobert     inline bool
312*404b540aSrobert     operator>=(const reverse_iterator<_Iterator>& __x,
313*404b540aSrobert 	       const reverse_iterator<_Iterator>& __y)
314*404b540aSrobert     { return !(__x < __y); }
315*404b540aSrobert 
316*404b540aSrobert   template<typename _Iterator>
317*404b540aSrobert     inline typename reverse_iterator<_Iterator>::difference_type
318*404b540aSrobert     operator-(const reverse_iterator<_Iterator>& __x,
319*404b540aSrobert 	      const reverse_iterator<_Iterator>& __y)
320*404b540aSrobert     { return __y.base() - __x.base(); }
321*404b540aSrobert 
322*404b540aSrobert   template<typename _Iterator>
323*404b540aSrobert     inline reverse_iterator<_Iterator>
324*404b540aSrobert     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
325*404b540aSrobert 	      const reverse_iterator<_Iterator>& __x)
326*404b540aSrobert     { return reverse_iterator<_Iterator>(__x.base() - __n); }
327*404b540aSrobert 
328*404b540aSrobert   // _GLIBCXX_RESOLVE_LIB_DEFECTS
329*404b540aSrobert   // DR 280. Comparison of reverse_iterator to const reverse_iterator.
330*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
331*404b540aSrobert     inline bool
332*404b540aSrobert     operator==(const reverse_iterator<_IteratorL>& __x,
333*404b540aSrobert 	       const reverse_iterator<_IteratorR>& __y)
334*404b540aSrobert     { return __x.base() == __y.base(); }
335*404b540aSrobert 
336*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
337*404b540aSrobert     inline bool
338*404b540aSrobert     operator<(const reverse_iterator<_IteratorL>& __x,
339*404b540aSrobert 	      const reverse_iterator<_IteratorR>& __y)
340*404b540aSrobert     { return __y.base() < __x.base(); }
341*404b540aSrobert 
342*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
343*404b540aSrobert     inline bool
344*404b540aSrobert     operator!=(const reverse_iterator<_IteratorL>& __x,
345*404b540aSrobert 	       const reverse_iterator<_IteratorR>& __y)
346*404b540aSrobert     { return !(__x == __y); }
347*404b540aSrobert 
348*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
349*404b540aSrobert     inline bool
350*404b540aSrobert     operator>(const reverse_iterator<_IteratorL>& __x,
351*404b540aSrobert 	      const reverse_iterator<_IteratorR>& __y)
352*404b540aSrobert     { return __y < __x; }
353*404b540aSrobert 
354*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
355*404b540aSrobert     inline bool
356*404b540aSrobert     operator<=(const reverse_iterator<_IteratorL>& __x,
357*404b540aSrobert 	       const reverse_iterator<_IteratorR>& __y)
358*404b540aSrobert     { return !(__y < __x); }
359*404b540aSrobert 
360*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
361*404b540aSrobert     inline bool
362*404b540aSrobert     operator>=(const reverse_iterator<_IteratorL>& __x,
363*404b540aSrobert 	       const reverse_iterator<_IteratorR>& __y)
364*404b540aSrobert     { return !(__x < __y); }
365*404b540aSrobert 
366*404b540aSrobert   template<typename _IteratorL, typename _IteratorR>
367*404b540aSrobert     inline typename reverse_iterator<_IteratorL>::difference_type
368*404b540aSrobert     operator-(const reverse_iterator<_IteratorL>& __x,
369*404b540aSrobert 	      const reverse_iterator<_IteratorR>& __y)
370*404b540aSrobert     { return __y.base() - __x.base(); }
371*404b540aSrobert   //@}
372*404b540aSrobert 
373*404b540aSrobert   // 24.4.2.2.1 back_insert_iterator
374*404b540aSrobert   /**
375*404b540aSrobert    *  @brief  Turns assignment into insertion.
376*404b540aSrobert    *
377*404b540aSrobert    *  These are output iterators, constructed from a container-of-T.
378*404b540aSrobert    *  Assigning a T to the iterator appends it to the container using
379*404b540aSrobert    *  push_back.
380*404b540aSrobert    *
381*404b540aSrobert    *  Tip:  Using the back_inserter function to create these iterators can
382*404b540aSrobert    *  save typing.
383*404b540aSrobert   */
384*404b540aSrobert   template<typename _Container>
385*404b540aSrobert     class back_insert_iterator
386*404b540aSrobert     : public iterator<output_iterator_tag, void, void, void, void>
387*404b540aSrobert     {
388*404b540aSrobert     protected:
389*404b540aSrobert       _Container* container;
390*404b540aSrobert 
391*404b540aSrobert     public:
392*404b540aSrobert       /// A nested typedef for the type of whatever container you used.
393*404b540aSrobert       typedef _Container          container_type;
394*404b540aSrobert 
395*404b540aSrobert       /// The only way to create this %iterator is with a container.
396*404b540aSrobert       explicit
back_insert_iterator(_Container & __x)397*404b540aSrobert       back_insert_iterator(_Container& __x) : container(&__x) { }
398*404b540aSrobert 
399*404b540aSrobert       /**
400*404b540aSrobert        *  @param  value  An instance of whatever type
401*404b540aSrobert        *                 container_type::const_reference is; presumably a
402*404b540aSrobert        *                 reference-to-const T for container<T>.
403*404b540aSrobert        *  @return  This %iterator, for chained operations.
404*404b540aSrobert        *
405*404b540aSrobert        *  This kind of %iterator doesn't really have a "position" in the
406*404b540aSrobert        *  container (you can think of the position as being permanently at
407*404b540aSrobert        *  the end, if you like).  Assigning a value to the %iterator will
408*404b540aSrobert        *  always append the value to the end of the container.
409*404b540aSrobert       */
410*404b540aSrobert       back_insert_iterator&
411*404b540aSrobert       operator=(typename _Container::const_reference __value)
412*404b540aSrobert       {
413*404b540aSrobert 	container->push_back(__value);
414*404b540aSrobert 	return *this;
415*404b540aSrobert       }
416*404b540aSrobert 
417*404b540aSrobert       /// Simply returns *this.
418*404b540aSrobert       back_insert_iterator&
419*404b540aSrobert       operator*()
420*404b540aSrobert       { return *this; }
421*404b540aSrobert 
422*404b540aSrobert       /// Simply returns *this.  (This %iterator does not "move".)
423*404b540aSrobert       back_insert_iterator&
424*404b540aSrobert       operator++()
425*404b540aSrobert       { return *this; }
426*404b540aSrobert 
427*404b540aSrobert       /// Simply returns *this.  (This %iterator does not "move".)
428*404b540aSrobert       back_insert_iterator
429*404b540aSrobert       operator++(int)
430*404b540aSrobert       { return *this; }
431*404b540aSrobert     };
432*404b540aSrobert 
433*404b540aSrobert   /**
434*404b540aSrobert    *  @param  x  A container of arbitrary type.
435*404b540aSrobert    *  @return  An instance of back_insert_iterator working on @p x.
436*404b540aSrobert    *
437*404b540aSrobert    *  This wrapper function helps in creating back_insert_iterator instances.
438*404b540aSrobert    *  Typing the name of the %iterator requires knowing the precise full
439*404b540aSrobert    *  type of the container, which can be tedious and impedes generic
440*404b540aSrobert    *  programming.  Using this function lets you take advantage of automatic
441*404b540aSrobert    *  template parameter deduction, making the compiler match the correct
442*404b540aSrobert    *  types for you.
443*404b540aSrobert   */
444*404b540aSrobert   template<typename _Container>
445*404b540aSrobert     inline back_insert_iterator<_Container>
back_inserter(_Container & __x)446*404b540aSrobert     back_inserter(_Container& __x)
447*404b540aSrobert     { return back_insert_iterator<_Container>(__x); }
448*404b540aSrobert 
449*404b540aSrobert   /**
450*404b540aSrobert    *  @brief  Turns assignment into insertion.
451*404b540aSrobert    *
452*404b540aSrobert    *  These are output iterators, constructed from a container-of-T.
453*404b540aSrobert    *  Assigning a T to the iterator prepends it to the container using
454*404b540aSrobert    *  push_front.
455*404b540aSrobert    *
456*404b540aSrobert    *  Tip:  Using the front_inserter function to create these iterators can
457*404b540aSrobert    *  save typing.
458*404b540aSrobert   */
459*404b540aSrobert   template<typename _Container>
460*404b540aSrobert     class front_insert_iterator
461*404b540aSrobert     : public iterator<output_iterator_tag, void, void, void, void>
462*404b540aSrobert     {
463*404b540aSrobert     protected:
464*404b540aSrobert       _Container* container;
465*404b540aSrobert 
466*404b540aSrobert     public:
467*404b540aSrobert       /// A nested typedef for the type of whatever container you used.
468*404b540aSrobert       typedef _Container          container_type;
469*404b540aSrobert 
470*404b540aSrobert       /// The only way to create this %iterator is with a container.
front_insert_iterator(_Container & __x)471*404b540aSrobert       explicit front_insert_iterator(_Container& __x) : container(&__x) { }
472*404b540aSrobert 
473*404b540aSrobert       /**
474*404b540aSrobert        *  @param  value  An instance of whatever type
475*404b540aSrobert        *                 container_type::const_reference is; presumably a
476*404b540aSrobert        *                 reference-to-const T for container<T>.
477*404b540aSrobert        *  @return  This %iterator, for chained operations.
478*404b540aSrobert        *
479*404b540aSrobert        *  This kind of %iterator doesn't really have a "position" in the
480*404b540aSrobert        *  container (you can think of the position as being permanently at
481*404b540aSrobert        *  the front, if you like).  Assigning a value to the %iterator will
482*404b540aSrobert        *  always prepend the value to the front of the container.
483*404b540aSrobert       */
484*404b540aSrobert       front_insert_iterator&
485*404b540aSrobert       operator=(typename _Container::const_reference __value)
486*404b540aSrobert       {
487*404b540aSrobert 	container->push_front(__value);
488*404b540aSrobert 	return *this;
489*404b540aSrobert       }
490*404b540aSrobert 
491*404b540aSrobert       /// Simply returns *this.
492*404b540aSrobert       front_insert_iterator&
493*404b540aSrobert       operator*()
494*404b540aSrobert       { return *this; }
495*404b540aSrobert 
496*404b540aSrobert       /// Simply returns *this.  (This %iterator does not "move".)
497*404b540aSrobert       front_insert_iterator&
498*404b540aSrobert       operator++()
499*404b540aSrobert       { return *this; }
500*404b540aSrobert 
501*404b540aSrobert       /// Simply returns *this.  (This %iterator does not "move".)
502*404b540aSrobert       front_insert_iterator
503*404b540aSrobert       operator++(int)
504*404b540aSrobert       { return *this; }
505*404b540aSrobert     };
506*404b540aSrobert 
507*404b540aSrobert   /**
508*404b540aSrobert    *  @param  x  A container of arbitrary type.
509*404b540aSrobert    *  @return  An instance of front_insert_iterator working on @p x.
510*404b540aSrobert    *
511*404b540aSrobert    *  This wrapper function helps in creating front_insert_iterator instances.
512*404b540aSrobert    *  Typing the name of the %iterator requires knowing the precise full
513*404b540aSrobert    *  type of the container, which can be tedious and impedes generic
514*404b540aSrobert    *  programming.  Using this function lets you take advantage of automatic
515*404b540aSrobert    *  template parameter deduction, making the compiler match the correct
516*404b540aSrobert    *  types for you.
517*404b540aSrobert   */
518*404b540aSrobert   template<typename _Container>
519*404b540aSrobert     inline front_insert_iterator<_Container>
front_inserter(_Container & __x)520*404b540aSrobert     front_inserter(_Container& __x)
521*404b540aSrobert     { return front_insert_iterator<_Container>(__x); }
522*404b540aSrobert 
523*404b540aSrobert   /**
524*404b540aSrobert    *  @brief  Turns assignment into insertion.
525*404b540aSrobert    *
526*404b540aSrobert    *  These are output iterators, constructed from a container-of-T.
527*404b540aSrobert    *  Assigning a T to the iterator inserts it in the container at the
528*404b540aSrobert    *  %iterator's position, rather than overwriting the value at that
529*404b540aSrobert    *  position.
530*404b540aSrobert    *
531*404b540aSrobert    *  (Sequences will actually insert a @e copy of the value before the
532*404b540aSrobert    *  %iterator's position.)
533*404b540aSrobert    *
534*404b540aSrobert    *  Tip:  Using the inserter function to create these iterators can
535*404b540aSrobert    *  save typing.
536*404b540aSrobert   */
537*404b540aSrobert   template<typename _Container>
538*404b540aSrobert     class insert_iterator
539*404b540aSrobert     : public iterator<output_iterator_tag, void, void, void, void>
540*404b540aSrobert     {
541*404b540aSrobert     protected:
542*404b540aSrobert       _Container* container;
543*404b540aSrobert       typename _Container::iterator iter;
544*404b540aSrobert 
545*404b540aSrobert     public:
546*404b540aSrobert       /// A nested typedef for the type of whatever container you used.
547*404b540aSrobert       typedef _Container          container_type;
548*404b540aSrobert 
549*404b540aSrobert       /**
550*404b540aSrobert        *  The only way to create this %iterator is with a container and an
551*404b540aSrobert        *  initial position (a normal %iterator into the container).
552*404b540aSrobert       */
insert_iterator(_Container & __x,typename _Container::iterator __i)553*404b540aSrobert       insert_iterator(_Container& __x, typename _Container::iterator __i)
554*404b540aSrobert       : container(&__x), iter(__i) {}
555*404b540aSrobert 
556*404b540aSrobert       /**
557*404b540aSrobert        *  @param  value  An instance of whatever type
558*404b540aSrobert        *                 container_type::const_reference is; presumably a
559*404b540aSrobert        *                 reference-to-const T for container<T>.
560*404b540aSrobert        *  @return  This %iterator, for chained operations.
561*404b540aSrobert        *
562*404b540aSrobert        *  This kind of %iterator maintains its own position in the
563*404b540aSrobert        *  container.  Assigning a value to the %iterator will insert the
564*404b540aSrobert        *  value into the container at the place before the %iterator.
565*404b540aSrobert        *
566*404b540aSrobert        *  The position is maintained such that subsequent assignments will
567*404b540aSrobert        *  insert values immediately after one another.  For example,
568*404b540aSrobert        *  @code
569*404b540aSrobert        *     // vector v contains A and Z
570*404b540aSrobert        *
571*404b540aSrobert        *     insert_iterator i (v, ++v.begin());
572*404b540aSrobert        *     i = 1;
573*404b540aSrobert        *     i = 2;
574*404b540aSrobert        *     i = 3;
575*404b540aSrobert        *
576*404b540aSrobert        *     // vector v contains A, 1, 2, 3, and Z
577*404b540aSrobert        *  @endcode
578*404b540aSrobert       */
579*404b540aSrobert       insert_iterator&
580*404b540aSrobert       operator=(const typename _Container::const_reference __value)
581*404b540aSrobert       {
582*404b540aSrobert 	iter = container->insert(iter, __value);
583*404b540aSrobert 	++iter;
584*404b540aSrobert 	return *this;
585*404b540aSrobert       }
586*404b540aSrobert 
587*404b540aSrobert       /// Simply returns *this.
588*404b540aSrobert       insert_iterator&
589*404b540aSrobert       operator*()
590*404b540aSrobert       { return *this; }
591*404b540aSrobert 
592*404b540aSrobert       /// Simply returns *this.  (This %iterator does not "move".)
593*404b540aSrobert       insert_iterator&
594*404b540aSrobert       operator++()
595*404b540aSrobert       { return *this; }
596*404b540aSrobert 
597*404b540aSrobert       /// Simply returns *this.  (This %iterator does not "move".)
598*404b540aSrobert       insert_iterator&
599*404b540aSrobert       operator++(int)
600*404b540aSrobert       { return *this; }
601*404b540aSrobert     };
602*404b540aSrobert 
603*404b540aSrobert   /**
604*404b540aSrobert    *  @param  x  A container of arbitrary type.
605*404b540aSrobert    *  @return  An instance of insert_iterator working on @p x.
606*404b540aSrobert    *
607*404b540aSrobert    *  This wrapper function helps in creating insert_iterator instances.
608*404b540aSrobert    *  Typing the name of the %iterator requires knowing the precise full
609*404b540aSrobert    *  type of the container, which can be tedious and impedes generic
610*404b540aSrobert    *  programming.  Using this function lets you take advantage of automatic
611*404b540aSrobert    *  template parameter deduction, making the compiler match the correct
612*404b540aSrobert    *  types for you.
613*404b540aSrobert   */
614*404b540aSrobert   template<typename _Container, typename _Iterator>
615*404b540aSrobert     inline insert_iterator<_Container>
inserter(_Container & __x,_Iterator __i)616*404b540aSrobert     inserter(_Container& __x, _Iterator __i)
617*404b540aSrobert     {
618*404b540aSrobert       return insert_iterator<_Container>(__x,
619*404b540aSrobert 					 typename _Container::iterator(__i));
620*404b540aSrobert     }
621*404b540aSrobert 
622*404b540aSrobert _GLIBCXX_END_NAMESPACE
623*404b540aSrobert 
624*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
625*404b540aSrobert 
626*404b540aSrobert   // This iterator adapter is 'normal' in the sense that it does not
627*404b540aSrobert   // change the semantics of any of the operators of its iterator
628*404b540aSrobert   // parameter.  Its primary purpose is to convert an iterator that is
629*404b540aSrobert   // not a class, e.g. a pointer, into an iterator that is a class.
630*404b540aSrobert   // The _Container parameter exists solely so that different containers
631*404b540aSrobert   // using this template can instantiate different types, even if the
632*404b540aSrobert   // _Iterator parameter is the same.
633*404b540aSrobert   using std::iterator_traits;
634*404b540aSrobert   using std::iterator;
635*404b540aSrobert   template<typename _Iterator, typename _Container>
636*404b540aSrobert     class __normal_iterator
637*404b540aSrobert     {
638*404b540aSrobert     protected:
639*404b540aSrobert       _Iterator _M_current;
640*404b540aSrobert 
641*404b540aSrobert     public:
642*404b540aSrobert       typedef typename iterator_traits<_Iterator>::iterator_category
643*404b540aSrobert                                                              iterator_category;
644*404b540aSrobert       typedef typename iterator_traits<_Iterator>::value_type  value_type;
645*404b540aSrobert       typedef typename iterator_traits<_Iterator>::difference_type
646*404b540aSrobert                                                              difference_type;
647*404b540aSrobert       typedef typename iterator_traits<_Iterator>::reference reference;
648*404b540aSrobert       typedef typename iterator_traits<_Iterator>::pointer   pointer;
649*404b540aSrobert 
__normal_iterator()650*404b540aSrobert       __normal_iterator() : _M_current(_Iterator()) { }
651*404b540aSrobert 
652*404b540aSrobert       explicit
__normal_iterator(const _Iterator & __i)653*404b540aSrobert       __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
654*404b540aSrobert 
655*404b540aSrobert       // Allow iterator to const_iterator conversion
656*404b540aSrobert       template<typename _Iter>
__normal_iterator(const __normal_iterator<_Iter,typename __enable_if<(std::__are_same<_Iter,typename _Container::pointer>::__value),_Container>::__type> & __i)657*404b540aSrobert         __normal_iterator(const __normal_iterator<_Iter,
658*404b540aSrobert 			  typename __enable_if<
659*404b540aSrobert       	       (std::__are_same<_Iter, typename _Container::pointer>::__value),
660*404b540aSrobert 		      _Container>::__type>& __i)
661*404b540aSrobert         : _M_current(__i.base()) { }
662*404b540aSrobert 
663*404b540aSrobert       // Forward iterator requirements
664*404b540aSrobert       reference
665*404b540aSrobert       operator*() const
666*404b540aSrobert       { return *_M_current; }
667*404b540aSrobert 
668*404b540aSrobert       pointer
669*404b540aSrobert       operator->() const
670*404b540aSrobert       { return _M_current; }
671*404b540aSrobert 
672*404b540aSrobert       __normal_iterator&
673*404b540aSrobert       operator++()
674*404b540aSrobert       {
675*404b540aSrobert 	++_M_current;
676*404b540aSrobert 	return *this;
677*404b540aSrobert       }
678*404b540aSrobert 
679*404b540aSrobert       __normal_iterator
680*404b540aSrobert       operator++(int)
681*404b540aSrobert       { return __normal_iterator(_M_current++); }
682*404b540aSrobert 
683*404b540aSrobert       // Bidirectional iterator requirements
684*404b540aSrobert       __normal_iterator&
685*404b540aSrobert       operator--()
686*404b540aSrobert       {
687*404b540aSrobert 	--_M_current;
688*404b540aSrobert 	return *this;
689*404b540aSrobert       }
690*404b540aSrobert 
691*404b540aSrobert       __normal_iterator
692*404b540aSrobert       operator--(int)
693*404b540aSrobert       { return __normal_iterator(_M_current--); }
694*404b540aSrobert 
695*404b540aSrobert       // Random access iterator requirements
696*404b540aSrobert       reference
697*404b540aSrobert       operator[](const difference_type& __n) const
698*404b540aSrobert       { return _M_current[__n]; }
699*404b540aSrobert 
700*404b540aSrobert       __normal_iterator&
701*404b540aSrobert       operator+=(const difference_type& __n)
702*404b540aSrobert       { _M_current += __n; return *this; }
703*404b540aSrobert 
704*404b540aSrobert       __normal_iterator
705*404b540aSrobert       operator+(const difference_type& __n) const
706*404b540aSrobert       { return __normal_iterator(_M_current + __n); }
707*404b540aSrobert 
708*404b540aSrobert       __normal_iterator&
709*404b540aSrobert       operator-=(const difference_type& __n)
710*404b540aSrobert       { _M_current -= __n; return *this; }
711*404b540aSrobert 
712*404b540aSrobert       __normal_iterator
713*404b540aSrobert       operator-(const difference_type& __n) const
714*404b540aSrobert       { return __normal_iterator(_M_current - __n); }
715*404b540aSrobert 
716*404b540aSrobert       const _Iterator&
base()717*404b540aSrobert       base() const
718*404b540aSrobert       { return _M_current; }
719*404b540aSrobert     };
720*404b540aSrobert 
721*404b540aSrobert   // Note: In what follows, the left- and right-hand-side iterators are
722*404b540aSrobert   // allowed to vary in types (conceptually in cv-qualification) so that
723*404b540aSrobert   // comparaison between cv-qualified and non-cv-qualified iterators be
724*404b540aSrobert   // valid.  However, the greedy and unfriendly operators in std::rel_ops
725*404b540aSrobert   // will make overload resolution ambiguous (when in scope) if we don't
726*404b540aSrobert   // provide overloads whose operands are of the same type.  Can someone
727*404b540aSrobert   // remind me what generic programming is about? -- Gaby
728*404b540aSrobert 
729*404b540aSrobert   // Forward iterator requirements
730*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
731*404b540aSrobert     inline bool
732*404b540aSrobert     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
733*404b540aSrobert 	       const __normal_iterator<_IteratorR, _Container>& __rhs)
734*404b540aSrobert     { return __lhs.base() == __rhs.base(); }
735*404b540aSrobert 
736*404b540aSrobert   template<typename _Iterator, typename _Container>
737*404b540aSrobert     inline bool
738*404b540aSrobert     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
739*404b540aSrobert 	       const __normal_iterator<_Iterator, _Container>& __rhs)
740*404b540aSrobert     { return __lhs.base() == __rhs.base(); }
741*404b540aSrobert 
742*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
743*404b540aSrobert     inline bool
744*404b540aSrobert     operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
745*404b540aSrobert 	       const __normal_iterator<_IteratorR, _Container>& __rhs)
746*404b540aSrobert     { return __lhs.base() != __rhs.base(); }
747*404b540aSrobert 
748*404b540aSrobert   template<typename _Iterator, typename _Container>
749*404b540aSrobert     inline bool
750*404b540aSrobert     operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
751*404b540aSrobert 	       const __normal_iterator<_Iterator, _Container>& __rhs)
752*404b540aSrobert     { return __lhs.base() != __rhs.base(); }
753*404b540aSrobert 
754*404b540aSrobert   // Random access iterator requirements
755*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
756*404b540aSrobert     inline bool
757*404b540aSrobert     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
758*404b540aSrobert 	      const __normal_iterator<_IteratorR, _Container>& __rhs)
759*404b540aSrobert     { return __lhs.base() < __rhs.base(); }
760*404b540aSrobert 
761*404b540aSrobert   template<typename _Iterator, typename _Container>
762*404b540aSrobert     inline bool
763*404b540aSrobert     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
764*404b540aSrobert 	      const __normal_iterator<_Iterator, _Container>& __rhs)
765*404b540aSrobert     { return __lhs.base() < __rhs.base(); }
766*404b540aSrobert 
767*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
768*404b540aSrobert     inline bool
769*404b540aSrobert     operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
770*404b540aSrobert 	      const __normal_iterator<_IteratorR, _Container>& __rhs)
771*404b540aSrobert     { return __lhs.base() > __rhs.base(); }
772*404b540aSrobert 
773*404b540aSrobert   template<typename _Iterator, typename _Container>
774*404b540aSrobert     inline bool
775*404b540aSrobert     operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
776*404b540aSrobert 	      const __normal_iterator<_Iterator, _Container>& __rhs)
777*404b540aSrobert     { return __lhs.base() > __rhs.base(); }
778*404b540aSrobert 
779*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
780*404b540aSrobert     inline bool
781*404b540aSrobert     operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
782*404b540aSrobert 	       const __normal_iterator<_IteratorR, _Container>& __rhs)
783*404b540aSrobert     { return __lhs.base() <= __rhs.base(); }
784*404b540aSrobert 
785*404b540aSrobert   template<typename _Iterator, typename _Container>
786*404b540aSrobert     inline bool
787*404b540aSrobert     operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
788*404b540aSrobert 	       const __normal_iterator<_Iterator, _Container>& __rhs)
789*404b540aSrobert     { return __lhs.base() <= __rhs.base(); }
790*404b540aSrobert 
791*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
792*404b540aSrobert     inline bool
793*404b540aSrobert     operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
794*404b540aSrobert 	       const __normal_iterator<_IteratorR, _Container>& __rhs)
795*404b540aSrobert     { return __lhs.base() >= __rhs.base(); }
796*404b540aSrobert 
797*404b540aSrobert   template<typename _Iterator, typename _Container>
798*404b540aSrobert     inline bool
799*404b540aSrobert     operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
800*404b540aSrobert 	       const __normal_iterator<_Iterator, _Container>& __rhs)
801*404b540aSrobert     { return __lhs.base() >= __rhs.base(); }
802*404b540aSrobert 
803*404b540aSrobert   // _GLIBCXX_RESOLVE_LIB_DEFECTS
804*404b540aSrobert   // According to the resolution of DR179 not only the various comparison
805*404b540aSrobert   // operators but also operator- must accept mixed iterator/const_iterator
806*404b540aSrobert   // parameters.
807*404b540aSrobert   template<typename _IteratorL, typename _IteratorR, typename _Container>
808*404b540aSrobert     inline typename __normal_iterator<_IteratorL, _Container>::difference_type
809*404b540aSrobert     operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
810*404b540aSrobert 	      const __normal_iterator<_IteratorR, _Container>& __rhs)
811*404b540aSrobert     { return __lhs.base() - __rhs.base(); }
812*404b540aSrobert 
813*404b540aSrobert   template<typename _Iterator, typename _Container>
814*404b540aSrobert     inline typename __normal_iterator<_Iterator, _Container>::difference_type
815*404b540aSrobert     operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
816*404b540aSrobert 	      const __normal_iterator<_Iterator, _Container>& __rhs)
817*404b540aSrobert     { return __lhs.base() - __rhs.base(); }
818*404b540aSrobert 
819*404b540aSrobert   template<typename _Iterator, typename _Container>
820*404b540aSrobert     inline __normal_iterator<_Iterator, _Container>
821*404b540aSrobert     operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
822*404b540aSrobert 	      __n, const __normal_iterator<_Iterator, _Container>& __i)
823*404b540aSrobert     { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
824*404b540aSrobert 
825*404b540aSrobert _GLIBCXX_END_NAMESPACE
826*404b540aSrobert 
827*404b540aSrobert #endif
828