xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/bits/stl_algobase.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino // Core algorithmic facilities -*- C++ -*-
2*e4b17023SJohn Marino 
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4*e4b17023SJohn Marino // 2011 Free Software Foundation, Inc.
5*e4b17023SJohn Marino //
6*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library.  This library is free
7*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
8*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
9*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino // any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*e4b17023SJohn Marino // GNU General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
18*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
19*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
22*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
23*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
25*e4b17023SJohn Marino 
26*e4b17023SJohn Marino /*
27*e4b17023SJohn Marino  *
28*e4b17023SJohn Marino  * Copyright (c) 1994
29*e4b17023SJohn Marino  * Hewlett-Packard Company
30*e4b17023SJohn Marino  *
31*e4b17023SJohn Marino  * Permission to use, copy, modify, distribute and sell this software
32*e4b17023SJohn Marino  * and its documentation for any purpose is hereby granted without fee,
33*e4b17023SJohn Marino  * provided that the above copyright notice appear in all copies and
34*e4b17023SJohn Marino  * that both that copyright notice and this permission notice appear
35*e4b17023SJohn Marino  * in supporting documentation.  Hewlett-Packard Company makes no
36*e4b17023SJohn Marino  * representations about the suitability of this software for any
37*e4b17023SJohn Marino  * purpose.  It is provided "as is" without express or implied warranty.
38*e4b17023SJohn Marino  *
39*e4b17023SJohn Marino  *
40*e4b17023SJohn Marino  * Copyright (c) 1996-1998
41*e4b17023SJohn Marino  * Silicon Graphics Computer Systems, Inc.
42*e4b17023SJohn Marino  *
43*e4b17023SJohn Marino  * Permission to use, copy, modify, distribute and sell this software
44*e4b17023SJohn Marino  * and its documentation for any purpose is hereby granted without fee,
45*e4b17023SJohn Marino  * provided that the above copyright notice appear in all copies and
46*e4b17023SJohn Marino  * that both that copyright notice and this permission notice appear
47*e4b17023SJohn Marino  * in supporting documentation.  Silicon Graphics makes no
48*e4b17023SJohn Marino  * representations about the suitability of this software for any
49*e4b17023SJohn Marino  * purpose.  It is provided "as is" without express or implied warranty.
50*e4b17023SJohn Marino  */
51*e4b17023SJohn Marino 
52*e4b17023SJohn Marino /** @file bits/stl_algobase.h
53*e4b17023SJohn Marino  *  This is an internal header file, included by other library headers.
54*e4b17023SJohn Marino  *  Do not attempt to use it directly. @headername{algorithm}
55*e4b17023SJohn Marino  */
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino #ifndef _STL_ALGOBASE_H
58*e4b17023SJohn Marino #define _STL_ALGOBASE_H 1
59*e4b17023SJohn Marino 
60*e4b17023SJohn Marino #include <bits/c++config.h>
61*e4b17023SJohn Marino #include <bits/functexcept.h>
62*e4b17023SJohn Marino #include <bits/cpp_type_traits.h>
63*e4b17023SJohn Marino #include <ext/type_traits.h>
64*e4b17023SJohn Marino #include <ext/numeric_traits.h>
65*e4b17023SJohn Marino #include <bits/stl_pair.h>
66*e4b17023SJohn Marino #include <bits/stl_iterator_base_types.h>
67*e4b17023SJohn Marino #include <bits/stl_iterator_base_funcs.h>
68*e4b17023SJohn Marino #include <bits/stl_iterator.h>
69*e4b17023SJohn Marino #include <bits/concept_check.h>
70*e4b17023SJohn Marino #include <debug/debug.h>
71*e4b17023SJohn Marino #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
72*e4b17023SJohn Marino 
_GLIBCXX_VISIBILITY(default)73*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
74*e4b17023SJohn Marino {
75*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
76*e4b17023SJohn Marino 
77*e4b17023SJohn Marino   // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
78*e4b17023SJohn Marino   // nutshell, we are partially implementing the resolution of DR 187,
79*e4b17023SJohn Marino   // when it's safe, i.e., the value_types are equal.
80*e4b17023SJohn Marino   template<bool _BoolType>
81*e4b17023SJohn Marino     struct __iter_swap
82*e4b17023SJohn Marino     {
83*e4b17023SJohn Marino       template<typename _ForwardIterator1, typename _ForwardIterator2>
84*e4b17023SJohn Marino         static void
85*e4b17023SJohn Marino         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
86*e4b17023SJohn Marino         {
87*e4b17023SJohn Marino           typedef typename iterator_traits<_ForwardIterator1>::value_type
88*e4b17023SJohn Marino             _ValueType1;
89*e4b17023SJohn Marino           _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
90*e4b17023SJohn Marino           *__a = _GLIBCXX_MOVE(*__b);
91*e4b17023SJohn Marino           *__b = _GLIBCXX_MOVE(__tmp);
92*e4b17023SJohn Marino 	}
93*e4b17023SJohn Marino     };
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino   template<>
96*e4b17023SJohn Marino     struct __iter_swap<true>
97*e4b17023SJohn Marino     {
98*e4b17023SJohn Marino       template<typename _ForwardIterator1, typename _ForwardIterator2>
99*e4b17023SJohn Marino         static void
100*e4b17023SJohn Marino         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
101*e4b17023SJohn Marino         {
102*e4b17023SJohn Marino           swap(*__a, *__b);
103*e4b17023SJohn Marino         }
104*e4b17023SJohn Marino     };
105*e4b17023SJohn Marino 
106*e4b17023SJohn Marino   /**
107*e4b17023SJohn Marino    *  @brief Swaps the contents of two iterators.
108*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
109*e4b17023SJohn Marino    *  @param  __a  An iterator.
110*e4b17023SJohn Marino    *  @param  __b  Another iterator.
111*e4b17023SJohn Marino    *  @return   Nothing.
112*e4b17023SJohn Marino    *
113*e4b17023SJohn Marino    *  This function swaps the values pointed to by two iterators, not the
114*e4b17023SJohn Marino    *  iterators themselves.
115*e4b17023SJohn Marino   */
116*e4b17023SJohn Marino   template<typename _ForwardIterator1, typename _ForwardIterator2>
117*e4b17023SJohn Marino     inline void
118*e4b17023SJohn Marino     iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
119*e4b17023SJohn Marino     {
120*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator1>::value_type
121*e4b17023SJohn Marino 	_ValueType1;
122*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator2>::value_type
123*e4b17023SJohn Marino 	_ValueType2;
124*e4b17023SJohn Marino 
125*e4b17023SJohn Marino       // concept requirements
126*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
127*e4b17023SJohn Marino 				  _ForwardIterator1>)
128*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
129*e4b17023SJohn Marino 				  _ForwardIterator2>)
130*e4b17023SJohn Marino       __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
131*e4b17023SJohn Marino 				  _ValueType2>)
132*e4b17023SJohn Marino       __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
133*e4b17023SJohn Marino 				  _ValueType1>)
134*e4b17023SJohn Marino 
135*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator1>::reference
136*e4b17023SJohn Marino 	_ReferenceType1;
137*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator2>::reference
138*e4b17023SJohn Marino 	_ReferenceType2;
139*e4b17023SJohn Marino       std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
140*e4b17023SJohn Marino 	&& __are_same<_ValueType1&, _ReferenceType1>::__value
141*e4b17023SJohn Marino 	&& __are_same<_ValueType2&, _ReferenceType2>::__value>::
142*e4b17023SJohn Marino 	iter_swap(__a, __b);
143*e4b17023SJohn Marino     }
144*e4b17023SJohn Marino 
145*e4b17023SJohn Marino   /**
146*e4b17023SJohn Marino    *  @brief Swap the elements of two sequences.
147*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
148*e4b17023SJohn Marino    *  @param  __first1  A forward iterator.
149*e4b17023SJohn Marino    *  @param  __last1   A forward iterator.
150*e4b17023SJohn Marino    *  @param  __first2  A forward iterator.
151*e4b17023SJohn Marino    *  @return   An iterator equal to @p first2+(last1-first1).
152*e4b17023SJohn Marino    *
153*e4b17023SJohn Marino    *  Swaps each element in the range @p [first1,last1) with the
154*e4b17023SJohn Marino    *  corresponding element in the range @p [first2,(last1-first1)).
155*e4b17023SJohn Marino    *  The ranges must not overlap.
156*e4b17023SJohn Marino   */
157*e4b17023SJohn Marino   template<typename _ForwardIterator1, typename _ForwardIterator2>
158*e4b17023SJohn Marino     _ForwardIterator2
159*e4b17023SJohn Marino     swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
160*e4b17023SJohn Marino 		_ForwardIterator2 __first2)
161*e4b17023SJohn Marino     {
162*e4b17023SJohn Marino       // concept requirements
163*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
164*e4b17023SJohn Marino 				  _ForwardIterator1>)
165*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
166*e4b17023SJohn Marino 				  _ForwardIterator2>)
167*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
168*e4b17023SJohn Marino 
169*e4b17023SJohn Marino       for (; __first1 != __last1; ++__first1, ++__first2)
170*e4b17023SJohn Marino 	std::iter_swap(__first1, __first2);
171*e4b17023SJohn Marino       return __first2;
172*e4b17023SJohn Marino     }
173*e4b17023SJohn Marino 
174*e4b17023SJohn Marino   /**
175*e4b17023SJohn Marino    *  @brief This does what you think it does.
176*e4b17023SJohn Marino    *  @ingroup sorting_algorithms
177*e4b17023SJohn Marino    *  @param  __a  A thing of arbitrary type.
178*e4b17023SJohn Marino    *  @param  __b  Another thing of arbitrary type.
179*e4b17023SJohn Marino    *  @return   The lesser of the parameters.
180*e4b17023SJohn Marino    *
181*e4b17023SJohn Marino    *  This is the simple classic generic implementation.  It will work on
182*e4b17023SJohn Marino    *  temporary expressions, since they are only evaluated once, unlike a
183*e4b17023SJohn Marino    *  preprocessor macro.
184*e4b17023SJohn Marino   */
185*e4b17023SJohn Marino   template<typename _Tp>
186*e4b17023SJohn Marino     inline const _Tp&
187*e4b17023SJohn Marino     min(const _Tp& __a, const _Tp& __b)
188*e4b17023SJohn Marino     {
189*e4b17023SJohn Marino       // concept requirements
190*e4b17023SJohn Marino       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
191*e4b17023SJohn Marino       //return __b < __a ? __b : __a;
192*e4b17023SJohn Marino       if (__b < __a)
193*e4b17023SJohn Marino 	return __b;
194*e4b17023SJohn Marino       return __a;
195*e4b17023SJohn Marino     }
196*e4b17023SJohn Marino 
197*e4b17023SJohn Marino   /**
198*e4b17023SJohn Marino    *  @brief This does what you think it does.
199*e4b17023SJohn Marino    *  @ingroup sorting_algorithms
200*e4b17023SJohn Marino    *  @param  __a  A thing of arbitrary type.
201*e4b17023SJohn Marino    *  @param  __b  Another thing of arbitrary type.
202*e4b17023SJohn Marino    *  @return   The greater of the parameters.
203*e4b17023SJohn Marino    *
204*e4b17023SJohn Marino    *  This is the simple classic generic implementation.  It will work on
205*e4b17023SJohn Marino    *  temporary expressions, since they are only evaluated once, unlike a
206*e4b17023SJohn Marino    *  preprocessor macro.
207*e4b17023SJohn Marino   */
208*e4b17023SJohn Marino   template<typename _Tp>
209*e4b17023SJohn Marino     inline const _Tp&
210*e4b17023SJohn Marino     max(const _Tp& __a, const _Tp& __b)
211*e4b17023SJohn Marino     {
212*e4b17023SJohn Marino       // concept requirements
213*e4b17023SJohn Marino       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
214*e4b17023SJohn Marino       //return  __a < __b ? __b : __a;
215*e4b17023SJohn Marino       if (__a < __b)
216*e4b17023SJohn Marino 	return __b;
217*e4b17023SJohn Marino       return __a;
218*e4b17023SJohn Marino     }
219*e4b17023SJohn Marino 
220*e4b17023SJohn Marino   /**
221*e4b17023SJohn Marino    *  @brief This does what you think it does.
222*e4b17023SJohn Marino    *  @ingroup sorting_algorithms
223*e4b17023SJohn Marino    *  @param  __a  A thing of arbitrary type.
224*e4b17023SJohn Marino    *  @param  __b  Another thing of arbitrary type.
225*e4b17023SJohn Marino    *  @param  __comp  A @link comparison_functors comparison functor@endlink.
226*e4b17023SJohn Marino    *  @return   The lesser of the parameters.
227*e4b17023SJohn Marino    *
228*e4b17023SJohn Marino    *  This will work on temporary expressions, since they are only evaluated
229*e4b17023SJohn Marino    *  once, unlike a preprocessor macro.
230*e4b17023SJohn Marino   */
231*e4b17023SJohn Marino   template<typename _Tp, typename _Compare>
232*e4b17023SJohn Marino     inline const _Tp&
233*e4b17023SJohn Marino     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
234*e4b17023SJohn Marino     {
235*e4b17023SJohn Marino       //return __comp(__b, __a) ? __b : __a;
236*e4b17023SJohn Marino       if (__comp(__b, __a))
237*e4b17023SJohn Marino 	return __b;
238*e4b17023SJohn Marino       return __a;
239*e4b17023SJohn Marino     }
240*e4b17023SJohn Marino 
241*e4b17023SJohn Marino   /**
242*e4b17023SJohn Marino    *  @brief This does what you think it does.
243*e4b17023SJohn Marino    *  @ingroup sorting_algorithms
244*e4b17023SJohn Marino    *  @param  __a  A thing of arbitrary type.
245*e4b17023SJohn Marino    *  @param  __b  Another thing of arbitrary type.
246*e4b17023SJohn Marino    *  @param  __comp  A @link comparison_functors comparison functor@endlink.
247*e4b17023SJohn Marino    *  @return   The greater of the parameters.
248*e4b17023SJohn Marino    *
249*e4b17023SJohn Marino    *  This will work on temporary expressions, since they are only evaluated
250*e4b17023SJohn Marino    *  once, unlike a preprocessor macro.
251*e4b17023SJohn Marino   */
252*e4b17023SJohn Marino   template<typename _Tp, typename _Compare>
253*e4b17023SJohn Marino     inline const _Tp&
254*e4b17023SJohn Marino     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
255*e4b17023SJohn Marino     {
256*e4b17023SJohn Marino       //return __comp(__a, __b) ? __b : __a;
257*e4b17023SJohn Marino       if (__comp(__a, __b))
258*e4b17023SJohn Marino 	return __b;
259*e4b17023SJohn Marino       return __a;
260*e4b17023SJohn Marino     }
261*e4b17023SJohn Marino 
262*e4b17023SJohn Marino   // If _Iterator is a __normal_iterator return its base (a plain pointer,
263*e4b17023SJohn Marino   // normally) otherwise return it untouched.  See copy, fill, ...
264*e4b17023SJohn Marino   template<typename _Iterator>
265*e4b17023SJohn Marino     struct _Niter_base
266*e4b17023SJohn Marino     : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
267*e4b17023SJohn Marino     { };
268*e4b17023SJohn Marino 
269*e4b17023SJohn Marino   template<typename _Iterator>
270*e4b17023SJohn Marino     inline typename _Niter_base<_Iterator>::iterator_type
271*e4b17023SJohn Marino     __niter_base(_Iterator __it)
272*e4b17023SJohn Marino     { return std::_Niter_base<_Iterator>::_S_base(__it); }
273*e4b17023SJohn Marino 
274*e4b17023SJohn Marino   // Likewise, for move_iterator.
275*e4b17023SJohn Marino   template<typename _Iterator>
276*e4b17023SJohn Marino     struct _Miter_base
277*e4b17023SJohn Marino     : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
278*e4b17023SJohn Marino     { };
279*e4b17023SJohn Marino 
280*e4b17023SJohn Marino   template<typename _Iterator>
281*e4b17023SJohn Marino     inline typename _Miter_base<_Iterator>::iterator_type
282*e4b17023SJohn Marino     __miter_base(_Iterator __it)
283*e4b17023SJohn Marino     { return std::_Miter_base<_Iterator>::_S_base(__it); }
284*e4b17023SJohn Marino 
285*e4b17023SJohn Marino   // All of these auxiliary structs serve two purposes.  (1) Replace
286*e4b17023SJohn Marino   // calls to copy with memmove whenever possible.  (Memmove, not memcpy,
287*e4b17023SJohn Marino   // because the input and output ranges are permitted to overlap.)
288*e4b17023SJohn Marino   // (2) If we're using random access iterators, then write the loop as
289*e4b17023SJohn Marino   // a for loop with an explicit count.
290*e4b17023SJohn Marino 
291*e4b17023SJohn Marino   template<bool, bool, typename>
292*e4b17023SJohn Marino     struct __copy_move
293*e4b17023SJohn Marino     {
294*e4b17023SJohn Marino       template<typename _II, typename _OI>
295*e4b17023SJohn Marino         static _OI
296*e4b17023SJohn Marino         __copy_m(_II __first, _II __last, _OI __result)
297*e4b17023SJohn Marino         {
298*e4b17023SJohn Marino 	  for (; __first != __last; ++__result, ++__first)
299*e4b17023SJohn Marino 	    *__result = *__first;
300*e4b17023SJohn Marino 	  return __result;
301*e4b17023SJohn Marino 	}
302*e4b17023SJohn Marino     };
303*e4b17023SJohn Marino 
304*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
305*e4b17023SJohn Marino   template<typename _Category>
306*e4b17023SJohn Marino     struct __copy_move<true, false, _Category>
307*e4b17023SJohn Marino     {
308*e4b17023SJohn Marino       template<typename _II, typename _OI>
309*e4b17023SJohn Marino         static _OI
310*e4b17023SJohn Marino         __copy_m(_II __first, _II __last, _OI __result)
311*e4b17023SJohn Marino         {
312*e4b17023SJohn Marino 	  for (; __first != __last; ++__result, ++__first)
313*e4b17023SJohn Marino 	    *__result = std::move(*__first);
314*e4b17023SJohn Marino 	  return __result;
315*e4b17023SJohn Marino 	}
316*e4b17023SJohn Marino     };
317*e4b17023SJohn Marino #endif
318*e4b17023SJohn Marino 
319*e4b17023SJohn Marino   template<>
320*e4b17023SJohn Marino     struct __copy_move<false, false, random_access_iterator_tag>
321*e4b17023SJohn Marino     {
322*e4b17023SJohn Marino       template<typename _II, typename _OI>
323*e4b17023SJohn Marino         static _OI
324*e4b17023SJohn Marino         __copy_m(_II __first, _II __last, _OI __result)
325*e4b17023SJohn Marino         {
326*e4b17023SJohn Marino 	  typedef typename iterator_traits<_II>::difference_type _Distance;
327*e4b17023SJohn Marino 	  for(_Distance __n = __last - __first; __n > 0; --__n)
328*e4b17023SJohn Marino 	    {
329*e4b17023SJohn Marino 	      *__result = *__first;
330*e4b17023SJohn Marino 	      ++__first;
331*e4b17023SJohn Marino 	      ++__result;
332*e4b17023SJohn Marino 	    }
333*e4b17023SJohn Marino 	  return __result;
334*e4b17023SJohn Marino 	}
335*e4b17023SJohn Marino     };
336*e4b17023SJohn Marino 
337*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
338*e4b17023SJohn Marino   template<>
339*e4b17023SJohn Marino     struct __copy_move<true, false, random_access_iterator_tag>
340*e4b17023SJohn Marino     {
341*e4b17023SJohn Marino       template<typename _II, typename _OI>
342*e4b17023SJohn Marino         static _OI
343*e4b17023SJohn Marino         __copy_m(_II __first, _II __last, _OI __result)
344*e4b17023SJohn Marino         {
345*e4b17023SJohn Marino 	  typedef typename iterator_traits<_II>::difference_type _Distance;
346*e4b17023SJohn Marino 	  for(_Distance __n = __last - __first; __n > 0; --__n)
347*e4b17023SJohn Marino 	    {
348*e4b17023SJohn Marino 	      *__result = std::move(*__first);
349*e4b17023SJohn Marino 	      ++__first;
350*e4b17023SJohn Marino 	      ++__result;
351*e4b17023SJohn Marino 	    }
352*e4b17023SJohn Marino 	  return __result;
353*e4b17023SJohn Marino 	}
354*e4b17023SJohn Marino     };
355*e4b17023SJohn Marino #endif
356*e4b17023SJohn Marino 
357*e4b17023SJohn Marino   template<bool _IsMove>
358*e4b17023SJohn Marino     struct __copy_move<_IsMove, true, random_access_iterator_tag>
359*e4b17023SJohn Marino     {
360*e4b17023SJohn Marino       template<typename _Tp>
361*e4b17023SJohn Marino         static _Tp*
362*e4b17023SJohn Marino         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
363*e4b17023SJohn Marino         {
364*e4b17023SJohn Marino 	  const ptrdiff_t _Num = __last - __first;
365*e4b17023SJohn Marino 	  if (_Num)
366*e4b17023SJohn Marino 	    __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
367*e4b17023SJohn Marino 	  return __result + _Num;
368*e4b17023SJohn Marino 	}
369*e4b17023SJohn Marino     };
370*e4b17023SJohn Marino 
371*e4b17023SJohn Marino   template<bool _IsMove, typename _II, typename _OI>
372*e4b17023SJohn Marino     inline _OI
373*e4b17023SJohn Marino     __copy_move_a(_II __first, _II __last, _OI __result)
374*e4b17023SJohn Marino     {
375*e4b17023SJohn Marino       typedef typename iterator_traits<_II>::value_type _ValueTypeI;
376*e4b17023SJohn Marino       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
377*e4b17023SJohn Marino       typedef typename iterator_traits<_II>::iterator_category _Category;
378*e4b17023SJohn Marino       const bool __simple = (__is_trivial(_ValueTypeI)
379*e4b17023SJohn Marino 	                     && __is_pointer<_II>::__value
380*e4b17023SJohn Marino 	                     && __is_pointer<_OI>::__value
381*e4b17023SJohn Marino 			     && __are_same<_ValueTypeI, _ValueTypeO>::__value);
382*e4b17023SJohn Marino 
383*e4b17023SJohn Marino       return std::__copy_move<_IsMove, __simple,
384*e4b17023SJohn Marino 	                      _Category>::__copy_m(__first, __last, __result);
385*e4b17023SJohn Marino     }
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino   // Helpers for streambuf iterators (either istream or ostream).
388*e4b17023SJohn Marino   // NB: avoid including <iosfwd>, relatively large.
389*e4b17023SJohn Marino   template<typename _CharT>
390*e4b17023SJohn Marino     struct char_traits;
391*e4b17023SJohn Marino 
392*e4b17023SJohn Marino   template<typename _CharT, typename _Traits>
393*e4b17023SJohn Marino     class istreambuf_iterator;
394*e4b17023SJohn Marino 
395*e4b17023SJohn Marino   template<typename _CharT, typename _Traits>
396*e4b17023SJohn Marino     class ostreambuf_iterator;
397*e4b17023SJohn Marino 
398*e4b17023SJohn Marino   template<bool _IsMove, typename _CharT>
399*e4b17023SJohn Marino     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
400*e4b17023SJohn Marino 	     ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
401*e4b17023SJohn Marino     __copy_move_a2(_CharT*, _CharT*,
402*e4b17023SJohn Marino 		   ostreambuf_iterator<_CharT, char_traits<_CharT> >);
403*e4b17023SJohn Marino 
404*e4b17023SJohn Marino   template<bool _IsMove, typename _CharT>
405*e4b17023SJohn Marino     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
406*e4b17023SJohn Marino 	     ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
407*e4b17023SJohn Marino     __copy_move_a2(const _CharT*, const _CharT*,
408*e4b17023SJohn Marino 		   ostreambuf_iterator<_CharT, char_traits<_CharT> >);
409*e4b17023SJohn Marino 
410*e4b17023SJohn Marino   template<bool _IsMove, typename _CharT>
411*e4b17023SJohn Marino     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
412*e4b17023SJohn Marino 				    _CharT*>::__type
413*e4b17023SJohn Marino     __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
414*e4b17023SJohn Marino 		   istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
415*e4b17023SJohn Marino 
416*e4b17023SJohn Marino   template<bool _IsMove, typename _II, typename _OI>
417*e4b17023SJohn Marino     inline _OI
418*e4b17023SJohn Marino     __copy_move_a2(_II __first, _II __last, _OI __result)
419*e4b17023SJohn Marino     {
420*e4b17023SJohn Marino       return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
421*e4b17023SJohn Marino 					     std::__niter_base(__last),
422*e4b17023SJohn Marino 					     std::__niter_base(__result)));
423*e4b17023SJohn Marino     }
424*e4b17023SJohn Marino 
425*e4b17023SJohn Marino   /**
426*e4b17023SJohn Marino    *  @brief Copies the range [first,last) into result.
427*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
428*e4b17023SJohn Marino    *  @param  __first  An input iterator.
429*e4b17023SJohn Marino    *  @param  __last   An input iterator.
430*e4b17023SJohn Marino    *  @param  __result An output iterator.
431*e4b17023SJohn Marino    *  @return   result + (first - last)
432*e4b17023SJohn Marino    *
433*e4b17023SJohn Marino    *  This inline function will boil down to a call to @c memmove whenever
434*e4b17023SJohn Marino    *  possible.  Failing that, if random access iterators are passed, then the
435*e4b17023SJohn Marino    *  loop count will be known (and therefore a candidate for compiler
436*e4b17023SJohn Marino    *  optimizations such as unrolling).  Result may not be contained within
437*e4b17023SJohn Marino    *  [first,last); the copy_backward function should be used instead.
438*e4b17023SJohn Marino    *
439*e4b17023SJohn Marino    *  Note that the end of the output range is permitted to be contained
440*e4b17023SJohn Marino    *  within [first,last).
441*e4b17023SJohn Marino   */
442*e4b17023SJohn Marino   template<typename _II, typename _OI>
443*e4b17023SJohn Marino     inline _OI
444*e4b17023SJohn Marino     copy(_II __first, _II __last, _OI __result)
445*e4b17023SJohn Marino     {
446*e4b17023SJohn Marino       // concept requirements
447*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II>)
448*e4b17023SJohn Marino       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
449*e4b17023SJohn Marino 	    typename iterator_traits<_II>::value_type>)
450*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first, __last);
451*e4b17023SJohn Marino 
452*e4b17023SJohn Marino       return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
453*e4b17023SJohn Marino 	      (std::__miter_base(__first), std::__miter_base(__last),
454*e4b17023SJohn Marino 	       __result));
455*e4b17023SJohn Marino     }
456*e4b17023SJohn Marino 
457*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
458*e4b17023SJohn Marino   /**
459*e4b17023SJohn Marino    *  @brief Moves the range [first,last) into result.
460*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
461*e4b17023SJohn Marino    *  @param  __first  An input iterator.
462*e4b17023SJohn Marino    *  @param  __last   An input iterator.
463*e4b17023SJohn Marino    *  @param  __result An output iterator.
464*e4b17023SJohn Marino    *  @return   result + (first - last)
465*e4b17023SJohn Marino    *
466*e4b17023SJohn Marino    *  This inline function will boil down to a call to @c memmove whenever
467*e4b17023SJohn Marino    *  possible.  Failing that, if random access iterators are passed, then the
468*e4b17023SJohn Marino    *  loop count will be known (and therefore a candidate for compiler
469*e4b17023SJohn Marino    *  optimizations such as unrolling).  Result may not be contained within
470*e4b17023SJohn Marino    *  [first,last); the move_backward function should be used instead.
471*e4b17023SJohn Marino    *
472*e4b17023SJohn Marino    *  Note that the end of the output range is permitted to be contained
473*e4b17023SJohn Marino    *  within [first,last).
474*e4b17023SJohn Marino   */
475*e4b17023SJohn Marino   template<typename _II, typename _OI>
476*e4b17023SJohn Marino     inline _OI
477*e4b17023SJohn Marino     move(_II __first, _II __last, _OI __result)
478*e4b17023SJohn Marino     {
479*e4b17023SJohn Marino       // concept requirements
480*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II>)
481*e4b17023SJohn Marino       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
482*e4b17023SJohn Marino 	    typename iterator_traits<_II>::value_type>)
483*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first, __last);
484*e4b17023SJohn Marino 
485*e4b17023SJohn Marino       return std::__copy_move_a2<true>(std::__miter_base(__first),
486*e4b17023SJohn Marino 				       std::__miter_base(__last), __result);
487*e4b17023SJohn Marino     }
488*e4b17023SJohn Marino 
489*e4b17023SJohn Marino #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
490*e4b17023SJohn Marino #else
491*e4b17023SJohn Marino #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
492*e4b17023SJohn Marino #endif
493*e4b17023SJohn Marino 
494*e4b17023SJohn Marino   template<bool, bool, typename>
495*e4b17023SJohn Marino     struct __copy_move_backward
496*e4b17023SJohn Marino     {
497*e4b17023SJohn Marino       template<typename _BI1, typename _BI2>
498*e4b17023SJohn Marino         static _BI2
499*e4b17023SJohn Marino         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
500*e4b17023SJohn Marino         {
501*e4b17023SJohn Marino 	  while (__first != __last)
502*e4b17023SJohn Marino 	    *--__result = *--__last;
503*e4b17023SJohn Marino 	  return __result;
504*e4b17023SJohn Marino 	}
505*e4b17023SJohn Marino     };
506*e4b17023SJohn Marino 
507*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
508*e4b17023SJohn Marino   template<typename _Category>
509*e4b17023SJohn Marino     struct __copy_move_backward<true, false, _Category>
510*e4b17023SJohn Marino     {
511*e4b17023SJohn Marino       template<typename _BI1, typename _BI2>
512*e4b17023SJohn Marino         static _BI2
513*e4b17023SJohn Marino         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
514*e4b17023SJohn Marino         {
515*e4b17023SJohn Marino 	  while (__first != __last)
516*e4b17023SJohn Marino 	    *--__result = std::move(*--__last);
517*e4b17023SJohn Marino 	  return __result;
518*e4b17023SJohn Marino 	}
519*e4b17023SJohn Marino     };
520*e4b17023SJohn Marino #endif
521*e4b17023SJohn Marino 
522*e4b17023SJohn Marino   template<>
523*e4b17023SJohn Marino     struct __copy_move_backward<false, false, random_access_iterator_tag>
524*e4b17023SJohn Marino     {
525*e4b17023SJohn Marino       template<typename _BI1, typename _BI2>
526*e4b17023SJohn Marino         static _BI2
527*e4b17023SJohn Marino         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
528*e4b17023SJohn Marino         {
529*e4b17023SJohn Marino 	  typename iterator_traits<_BI1>::difference_type __n;
530*e4b17023SJohn Marino 	  for (__n = __last - __first; __n > 0; --__n)
531*e4b17023SJohn Marino 	    *--__result = *--__last;
532*e4b17023SJohn Marino 	  return __result;
533*e4b17023SJohn Marino 	}
534*e4b17023SJohn Marino     };
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
537*e4b17023SJohn Marino   template<>
538*e4b17023SJohn Marino     struct __copy_move_backward<true, false, random_access_iterator_tag>
539*e4b17023SJohn Marino     {
540*e4b17023SJohn Marino       template<typename _BI1, typename _BI2>
541*e4b17023SJohn Marino         static _BI2
542*e4b17023SJohn Marino         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
543*e4b17023SJohn Marino         {
544*e4b17023SJohn Marino 	  typename iterator_traits<_BI1>::difference_type __n;
545*e4b17023SJohn Marino 	  for (__n = __last - __first; __n > 0; --__n)
546*e4b17023SJohn Marino 	    *--__result = std::move(*--__last);
547*e4b17023SJohn Marino 	  return __result;
548*e4b17023SJohn Marino 	}
549*e4b17023SJohn Marino     };
550*e4b17023SJohn Marino #endif
551*e4b17023SJohn Marino 
552*e4b17023SJohn Marino   template<bool _IsMove>
553*e4b17023SJohn Marino     struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
554*e4b17023SJohn Marino     {
555*e4b17023SJohn Marino       template<typename _Tp>
556*e4b17023SJohn Marino         static _Tp*
557*e4b17023SJohn Marino         __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
558*e4b17023SJohn Marino         {
559*e4b17023SJohn Marino 	  const ptrdiff_t _Num = __last - __first;
560*e4b17023SJohn Marino 	  if (_Num)
561*e4b17023SJohn Marino 	    __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
562*e4b17023SJohn Marino 	  return __result - _Num;
563*e4b17023SJohn Marino 	}
564*e4b17023SJohn Marino     };
565*e4b17023SJohn Marino 
566*e4b17023SJohn Marino   template<bool _IsMove, typename _BI1, typename _BI2>
567*e4b17023SJohn Marino     inline _BI2
568*e4b17023SJohn Marino     __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
569*e4b17023SJohn Marino     {
570*e4b17023SJohn Marino       typedef typename iterator_traits<_BI1>::value_type _ValueType1;
571*e4b17023SJohn Marino       typedef typename iterator_traits<_BI2>::value_type _ValueType2;
572*e4b17023SJohn Marino       typedef typename iterator_traits<_BI1>::iterator_category _Category;
573*e4b17023SJohn Marino       const bool __simple = (__is_trivial(_ValueType1)
574*e4b17023SJohn Marino 	                     && __is_pointer<_BI1>::__value
575*e4b17023SJohn Marino 	                     && __is_pointer<_BI2>::__value
576*e4b17023SJohn Marino 			     && __are_same<_ValueType1, _ValueType2>::__value);
577*e4b17023SJohn Marino 
578*e4b17023SJohn Marino       return std::__copy_move_backward<_IsMove, __simple,
579*e4b17023SJohn Marino 	                               _Category>::__copy_move_b(__first,
580*e4b17023SJohn Marino 								 __last,
581*e4b17023SJohn Marino 								 __result);
582*e4b17023SJohn Marino     }
583*e4b17023SJohn Marino 
584*e4b17023SJohn Marino   template<bool _IsMove, typename _BI1, typename _BI2>
585*e4b17023SJohn Marino     inline _BI2
586*e4b17023SJohn Marino     __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
587*e4b17023SJohn Marino     {
588*e4b17023SJohn Marino       return _BI2(std::__copy_move_backward_a<_IsMove>
589*e4b17023SJohn Marino 		  (std::__niter_base(__first), std::__niter_base(__last),
590*e4b17023SJohn Marino 		   std::__niter_base(__result)));
591*e4b17023SJohn Marino     }
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino   /**
594*e4b17023SJohn Marino    *  @brief Copies the range [first,last) into result.
595*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
596*e4b17023SJohn Marino    *  @param  __first  A bidirectional iterator.
597*e4b17023SJohn Marino    *  @param  __last   A bidirectional iterator.
598*e4b17023SJohn Marino    *  @param  __result A bidirectional iterator.
599*e4b17023SJohn Marino    *  @return   result - (first - last)
600*e4b17023SJohn Marino    *
601*e4b17023SJohn Marino    *  The function has the same effect as copy, but starts at the end of the
602*e4b17023SJohn Marino    *  range and works its way to the start, returning the start of the result.
603*e4b17023SJohn Marino    *  This inline function will boil down to a call to @c memmove whenever
604*e4b17023SJohn Marino    *  possible.  Failing that, if random access iterators are passed, then the
605*e4b17023SJohn Marino    *  loop count will be known (and therefore a candidate for compiler
606*e4b17023SJohn Marino    *  optimizations such as unrolling).
607*e4b17023SJohn Marino    *
608*e4b17023SJohn Marino    *  Result may not be in the range [first,last).  Use copy instead.  Note
609*e4b17023SJohn Marino    *  that the start of the output range may overlap [first,last).
610*e4b17023SJohn Marino   */
611*e4b17023SJohn Marino   template<typename _BI1, typename _BI2>
612*e4b17023SJohn Marino     inline _BI2
613*e4b17023SJohn Marino     copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
614*e4b17023SJohn Marino     {
615*e4b17023SJohn Marino       // concept requirements
616*e4b17023SJohn Marino       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
617*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
618*e4b17023SJohn Marino       __glibcxx_function_requires(_ConvertibleConcept<
619*e4b17023SJohn Marino 	    typename iterator_traits<_BI1>::value_type,
620*e4b17023SJohn Marino 	    typename iterator_traits<_BI2>::value_type>)
621*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first, __last);
622*e4b17023SJohn Marino 
623*e4b17023SJohn Marino       return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
624*e4b17023SJohn Marino 	      (std::__miter_base(__first), std::__miter_base(__last),
625*e4b17023SJohn Marino 	       __result));
626*e4b17023SJohn Marino     }
627*e4b17023SJohn Marino 
628*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
629*e4b17023SJohn Marino   /**
630*e4b17023SJohn Marino    *  @brief Moves the range [first,last) into result.
631*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
632*e4b17023SJohn Marino    *  @param  __first  A bidirectional iterator.
633*e4b17023SJohn Marino    *  @param  __last   A bidirectional iterator.
634*e4b17023SJohn Marino    *  @param  __result A bidirectional iterator.
635*e4b17023SJohn Marino    *  @return   result - (first - last)
636*e4b17023SJohn Marino    *
637*e4b17023SJohn Marino    *  The function has the same effect as move, but starts at the end of the
638*e4b17023SJohn Marino    *  range and works its way to the start, returning the start of the result.
639*e4b17023SJohn Marino    *  This inline function will boil down to a call to @c memmove whenever
640*e4b17023SJohn Marino    *  possible.  Failing that, if random access iterators are passed, then the
641*e4b17023SJohn Marino    *  loop count will be known (and therefore a candidate for compiler
642*e4b17023SJohn Marino    *  optimizations such as unrolling).
643*e4b17023SJohn Marino    *
644*e4b17023SJohn Marino    *  Result may not be in the range (first,last].  Use move instead.  Note
645*e4b17023SJohn Marino    *  that the start of the output range may overlap [first,last).
646*e4b17023SJohn Marino   */
647*e4b17023SJohn Marino   template<typename _BI1, typename _BI2>
648*e4b17023SJohn Marino     inline _BI2
649*e4b17023SJohn Marino     move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
650*e4b17023SJohn Marino     {
651*e4b17023SJohn Marino       // concept requirements
652*e4b17023SJohn Marino       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
653*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
654*e4b17023SJohn Marino       __glibcxx_function_requires(_ConvertibleConcept<
655*e4b17023SJohn Marino 	    typename iterator_traits<_BI1>::value_type,
656*e4b17023SJohn Marino 	    typename iterator_traits<_BI2>::value_type>)
657*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first, __last);
658*e4b17023SJohn Marino 
659*e4b17023SJohn Marino       return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
660*e4b17023SJohn Marino 						std::__miter_base(__last),
661*e4b17023SJohn Marino 						__result);
662*e4b17023SJohn Marino     }
663*e4b17023SJohn Marino 
664*e4b17023SJohn Marino #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
665*e4b17023SJohn Marino #else
666*e4b17023SJohn Marino #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
667*e4b17023SJohn Marino #endif
668*e4b17023SJohn Marino 
669*e4b17023SJohn Marino   template<typename _ForwardIterator, typename _Tp>
670*e4b17023SJohn Marino     inline typename
671*e4b17023SJohn Marino     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
672*e4b17023SJohn Marino     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
673*e4b17023SJohn Marino  	     const _Tp& __value)
674*e4b17023SJohn Marino     {
675*e4b17023SJohn Marino       for (; __first != __last; ++__first)
676*e4b17023SJohn Marino 	*__first = __value;
677*e4b17023SJohn Marino     }
678*e4b17023SJohn Marino 
679*e4b17023SJohn Marino   template<typename _ForwardIterator, typename _Tp>
680*e4b17023SJohn Marino     inline typename
681*e4b17023SJohn Marino     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
682*e4b17023SJohn Marino     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
683*e4b17023SJohn Marino 	     const _Tp& __value)
684*e4b17023SJohn Marino     {
685*e4b17023SJohn Marino       const _Tp __tmp = __value;
686*e4b17023SJohn Marino       for (; __first != __last; ++__first)
687*e4b17023SJohn Marino 	*__first = __tmp;
688*e4b17023SJohn Marino     }
689*e4b17023SJohn Marino 
690*e4b17023SJohn Marino   // Specialization: for char types we can use memset.
691*e4b17023SJohn Marino   template<typename _Tp>
692*e4b17023SJohn Marino     inline typename
693*e4b17023SJohn Marino     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
694*e4b17023SJohn Marino     __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
695*e4b17023SJohn Marino     {
696*e4b17023SJohn Marino       const _Tp __tmp = __c;
697*e4b17023SJohn Marino       __builtin_memset(__first, static_cast<unsigned char>(__tmp),
698*e4b17023SJohn Marino 		       __last - __first);
699*e4b17023SJohn Marino     }
700*e4b17023SJohn Marino 
701*e4b17023SJohn Marino   /**
702*e4b17023SJohn Marino    *  @brief Fills the range [first,last) with copies of value.
703*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
704*e4b17023SJohn Marino    *  @param  __first  A forward iterator.
705*e4b17023SJohn Marino    *  @param  __last   A forward iterator.
706*e4b17023SJohn Marino    *  @param  __value  A reference-to-const of arbitrary type.
707*e4b17023SJohn Marino    *  @return   Nothing.
708*e4b17023SJohn Marino    *
709*e4b17023SJohn Marino    *  This function fills a range with copies of the same value.  For char
710*e4b17023SJohn Marino    *  types filling contiguous areas of memory, this becomes an inline call
711*e4b17023SJohn Marino    *  to @c memset or @c wmemset.
712*e4b17023SJohn Marino   */
713*e4b17023SJohn Marino   template<typename _ForwardIterator, typename _Tp>
714*e4b17023SJohn Marino     inline void
715*e4b17023SJohn Marino     fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
716*e4b17023SJohn Marino     {
717*e4b17023SJohn Marino       // concept requirements
718*e4b17023SJohn Marino       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
719*e4b17023SJohn Marino 				  _ForwardIterator>)
720*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first, __last);
721*e4b17023SJohn Marino 
722*e4b17023SJohn Marino       std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
723*e4b17023SJohn Marino 		    __value);
724*e4b17023SJohn Marino     }
725*e4b17023SJohn Marino 
726*e4b17023SJohn Marino   template<typename _OutputIterator, typename _Size, typename _Tp>
727*e4b17023SJohn Marino     inline typename
728*e4b17023SJohn Marino     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
729*e4b17023SJohn Marino     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
730*e4b17023SJohn Marino     {
731*e4b17023SJohn Marino       for (__decltype(__n + 0) __niter = __n;
732*e4b17023SJohn Marino 	   __niter > 0; --__niter, ++__first)
733*e4b17023SJohn Marino 	*__first = __value;
734*e4b17023SJohn Marino       return __first;
735*e4b17023SJohn Marino     }
736*e4b17023SJohn Marino 
737*e4b17023SJohn Marino   template<typename _OutputIterator, typename _Size, typename _Tp>
738*e4b17023SJohn Marino     inline typename
739*e4b17023SJohn Marino     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
740*e4b17023SJohn Marino     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
741*e4b17023SJohn Marino     {
742*e4b17023SJohn Marino       const _Tp __tmp = __value;
743*e4b17023SJohn Marino       for (__decltype(__n + 0) __niter = __n;
744*e4b17023SJohn Marino 	   __niter > 0; --__niter, ++__first)
745*e4b17023SJohn Marino 	*__first = __tmp;
746*e4b17023SJohn Marino       return __first;
747*e4b17023SJohn Marino     }
748*e4b17023SJohn Marino 
749*e4b17023SJohn Marino   template<typename _Size, typename _Tp>
750*e4b17023SJohn Marino     inline typename
751*e4b17023SJohn Marino     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
752*e4b17023SJohn Marino     __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
753*e4b17023SJohn Marino     {
754*e4b17023SJohn Marino       std::__fill_a(__first, __first + __n, __c);
755*e4b17023SJohn Marino       return __first + __n;
756*e4b17023SJohn Marino     }
757*e4b17023SJohn Marino 
758*e4b17023SJohn Marino   /**
759*e4b17023SJohn Marino    *  @brief Fills the range [first,first+n) with copies of value.
760*e4b17023SJohn Marino    *  @ingroup mutating_algorithms
761*e4b17023SJohn Marino    *  @param  __first  An output iterator.
762*e4b17023SJohn Marino    *  @param  __n      The count of copies to perform.
763*e4b17023SJohn Marino    *  @param  __value  A reference-to-const of arbitrary type.
764*e4b17023SJohn Marino    *  @return   The iterator at first+n.
765*e4b17023SJohn Marino    *
766*e4b17023SJohn Marino    *  This function fills a range with copies of the same value.  For char
767*e4b17023SJohn Marino    *  types filling contiguous areas of memory, this becomes an inline call
768*e4b17023SJohn Marino    *  to @c memset or @ wmemset.
769*e4b17023SJohn Marino    *
770*e4b17023SJohn Marino    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
771*e4b17023SJohn Marino    *  DR 865. More algorithms that throw away information
772*e4b17023SJohn Marino   */
773*e4b17023SJohn Marino   template<typename _OI, typename _Size, typename _Tp>
774*e4b17023SJohn Marino     inline _OI
775*e4b17023SJohn Marino     fill_n(_OI __first, _Size __n, const _Tp& __value)
776*e4b17023SJohn Marino     {
777*e4b17023SJohn Marino       // concept requirements
778*e4b17023SJohn Marino       __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
779*e4b17023SJohn Marino 
780*e4b17023SJohn Marino       return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
781*e4b17023SJohn Marino     }
782*e4b17023SJohn Marino 
783*e4b17023SJohn Marino   template<bool _BoolType>
784*e4b17023SJohn Marino     struct __equal
785*e4b17023SJohn Marino     {
786*e4b17023SJohn Marino       template<typename _II1, typename _II2>
787*e4b17023SJohn Marino         static bool
788*e4b17023SJohn Marino         equal(_II1 __first1, _II1 __last1, _II2 __first2)
789*e4b17023SJohn Marino         {
790*e4b17023SJohn Marino 	  for (; __first1 != __last1; ++__first1, ++__first2)
791*e4b17023SJohn Marino 	    if (!(*__first1 == *__first2))
792*e4b17023SJohn Marino 	      return false;
793*e4b17023SJohn Marino 	  return true;
794*e4b17023SJohn Marino 	}
795*e4b17023SJohn Marino     };
796*e4b17023SJohn Marino 
797*e4b17023SJohn Marino   template<>
798*e4b17023SJohn Marino     struct __equal<true>
799*e4b17023SJohn Marino     {
800*e4b17023SJohn Marino       template<typename _Tp>
801*e4b17023SJohn Marino         static bool
802*e4b17023SJohn Marino         equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
803*e4b17023SJohn Marino         {
804*e4b17023SJohn Marino 	  return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
805*e4b17023SJohn Marino 				   * (__last1 - __first1));
806*e4b17023SJohn Marino 	}
807*e4b17023SJohn Marino     };
808*e4b17023SJohn Marino 
809*e4b17023SJohn Marino   template<typename _II1, typename _II2>
810*e4b17023SJohn Marino     inline bool
811*e4b17023SJohn Marino     __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
812*e4b17023SJohn Marino     {
813*e4b17023SJohn Marino       typedef typename iterator_traits<_II1>::value_type _ValueType1;
814*e4b17023SJohn Marino       typedef typename iterator_traits<_II2>::value_type _ValueType2;
815*e4b17023SJohn Marino       const bool __simple = ((__is_integer<_ValueType1>::__value
816*e4b17023SJohn Marino 			      || __is_pointer<_ValueType1>::__value)
817*e4b17023SJohn Marino 	                     && __is_pointer<_II1>::__value
818*e4b17023SJohn Marino 	                     && __is_pointer<_II2>::__value
819*e4b17023SJohn Marino 			     && __are_same<_ValueType1, _ValueType2>::__value);
820*e4b17023SJohn Marino 
821*e4b17023SJohn Marino       return std::__equal<__simple>::equal(__first1, __last1, __first2);
822*e4b17023SJohn Marino     }
823*e4b17023SJohn Marino 
824*e4b17023SJohn Marino 
825*e4b17023SJohn Marino   template<typename, typename>
826*e4b17023SJohn Marino     struct __lc_rai
827*e4b17023SJohn Marino     {
828*e4b17023SJohn Marino       template<typename _II1, typename _II2>
829*e4b17023SJohn Marino         static _II1
830*e4b17023SJohn Marino         __newlast1(_II1, _II1 __last1, _II2, _II2)
831*e4b17023SJohn Marino         { return __last1; }
832*e4b17023SJohn Marino 
833*e4b17023SJohn Marino       template<typename _II>
834*e4b17023SJohn Marino         static bool
835*e4b17023SJohn Marino         __cnd2(_II __first, _II __last)
836*e4b17023SJohn Marino         { return __first != __last; }
837*e4b17023SJohn Marino     };
838*e4b17023SJohn Marino 
839*e4b17023SJohn Marino   template<>
840*e4b17023SJohn Marino     struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
841*e4b17023SJohn Marino     {
842*e4b17023SJohn Marino       template<typename _RAI1, typename _RAI2>
843*e4b17023SJohn Marino         static _RAI1
844*e4b17023SJohn Marino         __newlast1(_RAI1 __first1, _RAI1 __last1,
845*e4b17023SJohn Marino 		   _RAI2 __first2, _RAI2 __last2)
846*e4b17023SJohn Marino         {
847*e4b17023SJohn Marino 	  const typename iterator_traits<_RAI1>::difference_type
848*e4b17023SJohn Marino 	    __diff1 = __last1 - __first1;
849*e4b17023SJohn Marino 	  const typename iterator_traits<_RAI2>::difference_type
850*e4b17023SJohn Marino 	    __diff2 = __last2 - __first2;
851*e4b17023SJohn Marino 	  return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
852*e4b17023SJohn Marino 	}
853*e4b17023SJohn Marino 
854*e4b17023SJohn Marino       template<typename _RAI>
855*e4b17023SJohn Marino         static bool
856*e4b17023SJohn Marino         __cnd2(_RAI, _RAI)
857*e4b17023SJohn Marino         { return true; }
858*e4b17023SJohn Marino     };
859*e4b17023SJohn Marino 
860*e4b17023SJohn Marino   template<bool _BoolType>
861*e4b17023SJohn Marino     struct __lexicographical_compare
862*e4b17023SJohn Marino     {
863*e4b17023SJohn Marino       template<typename _II1, typename _II2>
864*e4b17023SJohn Marino         static bool __lc(_II1, _II1, _II2, _II2);
865*e4b17023SJohn Marino     };
866*e4b17023SJohn Marino 
867*e4b17023SJohn Marino   template<bool _BoolType>
868*e4b17023SJohn Marino     template<typename _II1, typename _II2>
869*e4b17023SJohn Marino       bool
870*e4b17023SJohn Marino       __lexicographical_compare<_BoolType>::
871*e4b17023SJohn Marino       __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
872*e4b17023SJohn Marino       {
873*e4b17023SJohn Marino 	typedef typename iterator_traits<_II1>::iterator_category _Category1;
874*e4b17023SJohn Marino 	typedef typename iterator_traits<_II2>::iterator_category _Category2;
875*e4b17023SJohn Marino 	typedef std::__lc_rai<_Category1, _Category2> 	__rai_type;
876*e4b17023SJohn Marino 
877*e4b17023SJohn Marino 	__last1 = __rai_type::__newlast1(__first1, __last1,
878*e4b17023SJohn Marino 					 __first2, __last2);
879*e4b17023SJohn Marino 	for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
880*e4b17023SJohn Marino 	     ++__first1, ++__first2)
881*e4b17023SJohn Marino 	  {
882*e4b17023SJohn Marino 	    if (*__first1 < *__first2)
883*e4b17023SJohn Marino 	      return true;
884*e4b17023SJohn Marino 	    if (*__first2 < *__first1)
885*e4b17023SJohn Marino 	      return false;
886*e4b17023SJohn Marino 	  }
887*e4b17023SJohn Marino 	return __first1 == __last1 && __first2 != __last2;
888*e4b17023SJohn Marino       }
889*e4b17023SJohn Marino 
890*e4b17023SJohn Marino   template<>
891*e4b17023SJohn Marino     struct __lexicographical_compare<true>
892*e4b17023SJohn Marino     {
893*e4b17023SJohn Marino       template<typename _Tp, typename _Up>
894*e4b17023SJohn Marino         static bool
895*e4b17023SJohn Marino         __lc(const _Tp* __first1, const _Tp* __last1,
896*e4b17023SJohn Marino 	     const _Up* __first2, const _Up* __last2)
897*e4b17023SJohn Marino 	{
898*e4b17023SJohn Marino 	  const size_t __len1 = __last1 - __first1;
899*e4b17023SJohn Marino 	  const size_t __len2 = __last2 - __first2;
900*e4b17023SJohn Marino 	  const int __result = __builtin_memcmp(__first1, __first2,
901*e4b17023SJohn Marino 						std::min(__len1, __len2));
902*e4b17023SJohn Marino 	  return __result != 0 ? __result < 0 : __len1 < __len2;
903*e4b17023SJohn Marino 	}
904*e4b17023SJohn Marino     };
905*e4b17023SJohn Marino 
906*e4b17023SJohn Marino   template<typename _II1, typename _II2>
907*e4b17023SJohn Marino     inline bool
908*e4b17023SJohn Marino     __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
909*e4b17023SJohn Marino 				  _II2 __first2, _II2 __last2)
910*e4b17023SJohn Marino     {
911*e4b17023SJohn Marino       typedef typename iterator_traits<_II1>::value_type _ValueType1;
912*e4b17023SJohn Marino       typedef typename iterator_traits<_II2>::value_type _ValueType2;
913*e4b17023SJohn Marino       const bool __simple =
914*e4b17023SJohn Marino 	(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
915*e4b17023SJohn Marino 	 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
916*e4b17023SJohn Marino 	 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
917*e4b17023SJohn Marino 	 && __is_pointer<_II1>::__value
918*e4b17023SJohn Marino 	 && __is_pointer<_II2>::__value);
919*e4b17023SJohn Marino 
920*e4b17023SJohn Marino       return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
921*e4b17023SJohn Marino 							    __first2, __last2);
922*e4b17023SJohn Marino     }
923*e4b17023SJohn Marino 
924*e4b17023SJohn Marino   /**
925*e4b17023SJohn Marino    *  @brief Finds the first position in which @a val could be inserted
926*e4b17023SJohn Marino    *         without changing the ordering.
927*e4b17023SJohn Marino    *  @param  __first   An iterator.
928*e4b17023SJohn Marino    *  @param  __last    Another iterator.
929*e4b17023SJohn Marino    *  @param  __val     The search term.
930*e4b17023SJohn Marino    *  @return         An iterator pointing to the first element <em>not less
931*e4b17023SJohn Marino    *                  than</em> @a val, or end() if every element is less than
932*e4b17023SJohn Marino    *                  @a val.
933*e4b17023SJohn Marino    *  @ingroup binary_search_algorithms
934*e4b17023SJohn Marino   */
935*e4b17023SJohn Marino   template<typename _ForwardIterator, typename _Tp>
936*e4b17023SJohn Marino     _ForwardIterator
937*e4b17023SJohn Marino     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
938*e4b17023SJohn Marino 		const _Tp& __val)
939*e4b17023SJohn Marino     {
940*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator>::value_type
941*e4b17023SJohn Marino 	_ValueType;
942*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator>::difference_type
943*e4b17023SJohn Marino 	_DistanceType;
944*e4b17023SJohn Marino 
945*e4b17023SJohn Marino       // concept requirements
946*e4b17023SJohn Marino       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
947*e4b17023SJohn Marino       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
948*e4b17023SJohn Marino       __glibcxx_requires_partitioned_lower(__first, __last, __val);
949*e4b17023SJohn Marino 
950*e4b17023SJohn Marino       _DistanceType __len = std::distance(__first, __last);
951*e4b17023SJohn Marino 
952*e4b17023SJohn Marino       while (__len > 0)
953*e4b17023SJohn Marino 	{
954*e4b17023SJohn Marino 	  _DistanceType __half = __len >> 1;
955*e4b17023SJohn Marino 	  _ForwardIterator __middle = __first;
956*e4b17023SJohn Marino 	  std::advance(__middle, __half);
957*e4b17023SJohn Marino 	  if (*__middle < __val)
958*e4b17023SJohn Marino 	    {
959*e4b17023SJohn Marino 	      __first = __middle;
960*e4b17023SJohn Marino 	      ++__first;
961*e4b17023SJohn Marino 	      __len = __len - __half - 1;
962*e4b17023SJohn Marino 	    }
963*e4b17023SJohn Marino 	  else
964*e4b17023SJohn Marino 	    __len = __half;
965*e4b17023SJohn Marino 	}
966*e4b17023SJohn Marino       return __first;
967*e4b17023SJohn Marino     }
968*e4b17023SJohn Marino 
969*e4b17023SJohn Marino   /// This is a helper function for the sort routines and for random.tcc.
970*e4b17023SJohn Marino   //  Precondition: __n > 0.
971*e4b17023SJohn Marino   template<typename _Size>
972*e4b17023SJohn Marino     inline _Size
973*e4b17023SJohn Marino     __lg(_Size __n)
974*e4b17023SJohn Marino     {
975*e4b17023SJohn Marino       _Size __k;
976*e4b17023SJohn Marino       for (__k = 0; __n != 0; __n >>= 1)
977*e4b17023SJohn Marino 	++__k;
978*e4b17023SJohn Marino       return __k - 1;
979*e4b17023SJohn Marino     }
980*e4b17023SJohn Marino 
981*e4b17023SJohn Marino   inline int
982*e4b17023SJohn Marino   __lg(int __n)
983*e4b17023SJohn Marino   { return sizeof(int) * __CHAR_BIT__  - 1 - __builtin_clz(__n); }
984*e4b17023SJohn Marino 
985*e4b17023SJohn Marino   inline unsigned
986*e4b17023SJohn Marino   __lg(unsigned __n)
987*e4b17023SJohn Marino   { return sizeof(int) * __CHAR_BIT__  - 1 - __builtin_clz(__n); }
988*e4b17023SJohn Marino 
989*e4b17023SJohn Marino   inline long
990*e4b17023SJohn Marino   __lg(long __n)
991*e4b17023SJohn Marino   { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
992*e4b17023SJohn Marino 
993*e4b17023SJohn Marino   inline unsigned long
994*e4b17023SJohn Marino   __lg(unsigned long __n)
995*e4b17023SJohn Marino   { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
996*e4b17023SJohn Marino 
997*e4b17023SJohn Marino   inline long long
998*e4b17023SJohn Marino   __lg(long long __n)
999*e4b17023SJohn Marino   { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1000*e4b17023SJohn Marino 
1001*e4b17023SJohn Marino   inline unsigned long long
1002*e4b17023SJohn Marino   __lg(unsigned long long __n)
1003*e4b17023SJohn Marino   { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1004*e4b17023SJohn Marino 
1005*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
1006*e4b17023SJohn Marino 
1007*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_ALGO
1008*e4b17023SJohn Marino 
1009*e4b17023SJohn Marino   /**
1010*e4b17023SJohn Marino    *  @brief Tests a range for element-wise equality.
1011*e4b17023SJohn Marino    *  @ingroup non_mutating_algorithms
1012*e4b17023SJohn Marino    *  @param  __first1  An input iterator.
1013*e4b17023SJohn Marino    *  @param  __last1   An input iterator.
1014*e4b17023SJohn Marino    *  @param  __first2  An input iterator.
1015*e4b17023SJohn Marino    *  @return   A boolean true or false.
1016*e4b17023SJohn Marino    *
1017*e4b17023SJohn Marino    *  This compares the elements of two ranges using @c == and returns true or
1018*e4b17023SJohn Marino    *  false depending on whether all of the corresponding elements of the
1019*e4b17023SJohn Marino    *  ranges are equal.
1020*e4b17023SJohn Marino   */
1021*e4b17023SJohn Marino   template<typename _II1, typename _II2>
1022*e4b17023SJohn Marino     inline bool
1023*e4b17023SJohn Marino     equal(_II1 __first1, _II1 __last1, _II2 __first2)
1024*e4b17023SJohn Marino     {
1025*e4b17023SJohn Marino       // concept requirements
1026*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1027*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1028*e4b17023SJohn Marino       __glibcxx_function_requires(_EqualOpConcept<
1029*e4b17023SJohn Marino 	    typename iterator_traits<_II1>::value_type,
1030*e4b17023SJohn Marino 	    typename iterator_traits<_II2>::value_type>)
1031*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
1032*e4b17023SJohn Marino 
1033*e4b17023SJohn Marino       return std::__equal_aux(std::__niter_base(__first1),
1034*e4b17023SJohn Marino 			      std::__niter_base(__last1),
1035*e4b17023SJohn Marino 			      std::__niter_base(__first2));
1036*e4b17023SJohn Marino     }
1037*e4b17023SJohn Marino 
1038*e4b17023SJohn Marino   /**
1039*e4b17023SJohn Marino    *  @brief Tests a range for element-wise equality.
1040*e4b17023SJohn Marino    *  @ingroup non_mutating_algorithms
1041*e4b17023SJohn Marino    *  @param  __first1  An input iterator.
1042*e4b17023SJohn Marino    *  @param  __last1   An input iterator.
1043*e4b17023SJohn Marino    *  @param  __first2  An input iterator.
1044*e4b17023SJohn Marino    *  @param __binary_pred A binary predicate @link functors
1045*e4b17023SJohn Marino    *                  functor@endlink.
1046*e4b17023SJohn Marino    *  @return         A boolean true or false.
1047*e4b17023SJohn Marino    *
1048*e4b17023SJohn Marino    *  This compares the elements of two ranges using the binary_pred
1049*e4b17023SJohn Marino    *  parameter, and returns true or
1050*e4b17023SJohn Marino    *  false depending on whether all of the corresponding elements of the
1051*e4b17023SJohn Marino    *  ranges are equal.
1052*e4b17023SJohn Marino   */
1053*e4b17023SJohn Marino   template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1054*e4b17023SJohn Marino     inline bool
1055*e4b17023SJohn Marino     equal(_IIter1 __first1, _IIter1 __last1,
1056*e4b17023SJohn Marino 	  _IIter2 __first2, _BinaryPredicate __binary_pred)
1057*e4b17023SJohn Marino     {
1058*e4b17023SJohn Marino       // concept requirements
1059*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1060*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1061*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
1062*e4b17023SJohn Marino 
1063*e4b17023SJohn Marino       for (; __first1 != __last1; ++__first1, ++__first2)
1064*e4b17023SJohn Marino 	if (!bool(__binary_pred(*__first1, *__first2)))
1065*e4b17023SJohn Marino 	  return false;
1066*e4b17023SJohn Marino       return true;
1067*e4b17023SJohn Marino     }
1068*e4b17023SJohn Marino 
1069*e4b17023SJohn Marino   /**
1070*e4b17023SJohn Marino    *  @brief Performs @b dictionary comparison on ranges.
1071*e4b17023SJohn Marino    *  @ingroup sorting_algorithms
1072*e4b17023SJohn Marino    *  @param  __first1  An input iterator.
1073*e4b17023SJohn Marino    *  @param  __last1   An input iterator.
1074*e4b17023SJohn Marino    *  @param  __first2  An input iterator.
1075*e4b17023SJohn Marino    *  @param  __last2   An input iterator.
1076*e4b17023SJohn Marino    *  @return   A boolean true or false.
1077*e4b17023SJohn Marino    *
1078*e4b17023SJohn Marino    *  <em>Returns true if the sequence of elements defined by the range
1079*e4b17023SJohn Marino    *  [first1,last1) is lexicographically less than the sequence of elements
1080*e4b17023SJohn Marino    *  defined by the range [first2,last2).  Returns false otherwise.</em>
1081*e4b17023SJohn Marino    *  (Quoted from [25.3.8]/1.)  If the iterators are all character pointers,
1082*e4b17023SJohn Marino    *  then this is an inline call to @c memcmp.
1083*e4b17023SJohn Marino   */
1084*e4b17023SJohn Marino   template<typename _II1, typename _II2>
1085*e4b17023SJohn Marino     inline bool
1086*e4b17023SJohn Marino     lexicographical_compare(_II1 __first1, _II1 __last1,
1087*e4b17023SJohn Marino 			    _II2 __first2, _II2 __last2)
1088*e4b17023SJohn Marino     {
1089*e4b17023SJohn Marino       // concept requirements
1090*e4b17023SJohn Marino       typedef typename iterator_traits<_II1>::value_type _ValueType1;
1091*e4b17023SJohn Marino       typedef typename iterator_traits<_II2>::value_type _ValueType2;
1092*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1093*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1094*e4b17023SJohn Marino       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1095*e4b17023SJohn Marino       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1096*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
1097*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first2, __last2);
1098*e4b17023SJohn Marino 
1099*e4b17023SJohn Marino       return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1100*e4b17023SJohn Marino 						std::__niter_base(__last1),
1101*e4b17023SJohn Marino 						std::__niter_base(__first2),
1102*e4b17023SJohn Marino 						std::__niter_base(__last2));
1103*e4b17023SJohn Marino     }
1104*e4b17023SJohn Marino 
1105*e4b17023SJohn Marino   /**
1106*e4b17023SJohn Marino    *  @brief Performs @b dictionary comparison on ranges.
1107*e4b17023SJohn Marino    *  @ingroup sorting_algorithms
1108*e4b17023SJohn Marino    *  @param  __first1  An input iterator.
1109*e4b17023SJohn Marino    *  @param  __last1   An input iterator.
1110*e4b17023SJohn Marino    *  @param  __first2  An input iterator.
1111*e4b17023SJohn Marino    *  @param  __last2   An input iterator.
1112*e4b17023SJohn Marino    *  @param  __comp  A @link comparison_functors comparison functor@endlink.
1113*e4b17023SJohn Marino    *  @return   A boolean true or false.
1114*e4b17023SJohn Marino    *
1115*e4b17023SJohn Marino    *  The same as the four-parameter @c lexicographical_compare, but uses the
1116*e4b17023SJohn Marino    *  comp parameter instead of @c <.
1117*e4b17023SJohn Marino   */
1118*e4b17023SJohn Marino   template<typename _II1, typename _II2, typename _Compare>
1119*e4b17023SJohn Marino     bool
1120*e4b17023SJohn Marino     lexicographical_compare(_II1 __first1, _II1 __last1,
1121*e4b17023SJohn Marino 			    _II2 __first2, _II2 __last2, _Compare __comp)
1122*e4b17023SJohn Marino     {
1123*e4b17023SJohn Marino       typedef typename iterator_traits<_II1>::iterator_category _Category1;
1124*e4b17023SJohn Marino       typedef typename iterator_traits<_II2>::iterator_category _Category2;
1125*e4b17023SJohn Marino       typedef std::__lc_rai<_Category1, _Category2> 	__rai_type;
1126*e4b17023SJohn Marino 
1127*e4b17023SJohn Marino       // concept requirements
1128*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1129*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1130*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
1131*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first2, __last2);
1132*e4b17023SJohn Marino 
1133*e4b17023SJohn Marino       __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1134*e4b17023SJohn Marino       for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1135*e4b17023SJohn Marino 	   ++__first1, ++__first2)
1136*e4b17023SJohn Marino 	{
1137*e4b17023SJohn Marino 	  if (__comp(*__first1, *__first2))
1138*e4b17023SJohn Marino 	    return true;
1139*e4b17023SJohn Marino 	  if (__comp(*__first2, *__first1))
1140*e4b17023SJohn Marino 	    return false;
1141*e4b17023SJohn Marino 	}
1142*e4b17023SJohn Marino       return __first1 == __last1 && __first2 != __last2;
1143*e4b17023SJohn Marino     }
1144*e4b17023SJohn Marino 
1145*e4b17023SJohn Marino   /**
1146*e4b17023SJohn Marino    *  @brief Finds the places in ranges which don't match.
1147*e4b17023SJohn Marino    *  @ingroup non_mutating_algorithms
1148*e4b17023SJohn Marino    *  @param  __first1  An input iterator.
1149*e4b17023SJohn Marino    *  @param  __last1   An input iterator.
1150*e4b17023SJohn Marino    *  @param  __first2  An input iterator.
1151*e4b17023SJohn Marino    *  @return   A pair of iterators pointing to the first mismatch.
1152*e4b17023SJohn Marino    *
1153*e4b17023SJohn Marino    *  This compares the elements of two ranges using @c == and returns a pair
1154*e4b17023SJohn Marino    *  of iterators.  The first iterator points into the first range, the
1155*e4b17023SJohn Marino    *  second iterator points into the second range, and the elements pointed
1156*e4b17023SJohn Marino    *  to by the iterators are not equal.
1157*e4b17023SJohn Marino   */
1158*e4b17023SJohn Marino   template<typename _InputIterator1, typename _InputIterator2>
1159*e4b17023SJohn Marino     pair<_InputIterator1, _InputIterator2>
1160*e4b17023SJohn Marino     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1161*e4b17023SJohn Marino 	     _InputIterator2 __first2)
1162*e4b17023SJohn Marino     {
1163*e4b17023SJohn Marino       // concept requirements
1164*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1165*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1166*e4b17023SJohn Marino       __glibcxx_function_requires(_EqualOpConcept<
1167*e4b17023SJohn Marino 	    typename iterator_traits<_InputIterator1>::value_type,
1168*e4b17023SJohn Marino 	    typename iterator_traits<_InputIterator2>::value_type>)
1169*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
1170*e4b17023SJohn Marino 
1171*e4b17023SJohn Marino       while (__first1 != __last1 && *__first1 == *__first2)
1172*e4b17023SJohn Marino         {
1173*e4b17023SJohn Marino 	  ++__first1;
1174*e4b17023SJohn Marino 	  ++__first2;
1175*e4b17023SJohn Marino         }
1176*e4b17023SJohn Marino       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1177*e4b17023SJohn Marino     }
1178*e4b17023SJohn Marino 
1179*e4b17023SJohn Marino   /**
1180*e4b17023SJohn Marino    *  @brief Finds the places in ranges which don't match.
1181*e4b17023SJohn Marino    *  @ingroup non_mutating_algorithms
1182*e4b17023SJohn Marino    *  @param  __first1  An input iterator.
1183*e4b17023SJohn Marino    *  @param  __last1   An input iterator.
1184*e4b17023SJohn Marino    *  @param  __first2  An input iterator.
1185*e4b17023SJohn Marino    *  @param __binary_pred A binary predicate @link functors
1186*e4b17023SJohn Marino    *         functor@endlink.
1187*e4b17023SJohn Marino    *  @return   A pair of iterators pointing to the first mismatch.
1188*e4b17023SJohn Marino    *
1189*e4b17023SJohn Marino    *  This compares the elements of two ranges using the binary_pred
1190*e4b17023SJohn Marino    *  parameter, and returns a pair
1191*e4b17023SJohn Marino    *  of iterators.  The first iterator points into the first range, the
1192*e4b17023SJohn Marino    *  second iterator points into the second range, and the elements pointed
1193*e4b17023SJohn Marino    *  to by the iterators are not equal.
1194*e4b17023SJohn Marino   */
1195*e4b17023SJohn Marino   template<typename _InputIterator1, typename _InputIterator2,
1196*e4b17023SJohn Marino 	   typename _BinaryPredicate>
1197*e4b17023SJohn Marino     pair<_InputIterator1, _InputIterator2>
1198*e4b17023SJohn Marino     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1199*e4b17023SJohn Marino 	     _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1200*e4b17023SJohn Marino     {
1201*e4b17023SJohn Marino       // concept requirements
1202*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1203*e4b17023SJohn Marino       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1204*e4b17023SJohn Marino       __glibcxx_requires_valid_range(__first1, __last1);
1205*e4b17023SJohn Marino 
1206*e4b17023SJohn Marino       while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1207*e4b17023SJohn Marino         {
1208*e4b17023SJohn Marino 	  ++__first1;
1209*e4b17023SJohn Marino 	  ++__first2;
1210*e4b17023SJohn Marino         }
1211*e4b17023SJohn Marino       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1212*e4b17023SJohn Marino     }
1213*e4b17023SJohn Marino 
1214*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_ALGO
1215*e4b17023SJohn Marino } // namespace std
1216*e4b17023SJohn Marino 
1217*e4b17023SJohn Marino // NB: This file is included within many other C++ includes, as a way
1218*e4b17023SJohn Marino // of getting the base algorithms. So, make sure that parallel bits
1219*e4b17023SJohn Marino // come in too if requested.
1220*e4b17023SJohn Marino #ifdef _GLIBCXX_PARALLEL
1221*e4b17023SJohn Marino # include <parallel/algobase.h>
1222*e4b17023SJohn Marino #endif
1223*e4b17023SJohn Marino 
1224*e4b17023SJohn Marino #endif
1225