xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/stl_algobase.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Bits and pieces used in algorithms -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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_algobase.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 
62*404b540aSrobert #ifndef _ALGOBASE_H
63*404b540aSrobert #define _ALGOBASE_H 1
64*404b540aSrobert 
65*404b540aSrobert #include <bits/c++config.h>
66*404b540aSrobert #include <cstring>
67*404b540aSrobert #include <climits>
68*404b540aSrobert #include <cstdlib>
69*404b540aSrobert #include <cstddef>
70*404b540aSrobert #include <iosfwd>
71*404b540aSrobert #include <bits/stl_pair.h>
72*404b540aSrobert #include <bits/cpp_type_traits.h>
73*404b540aSrobert #include <ext/type_traits.h>
74*404b540aSrobert #include <bits/stl_iterator_base_types.h>
75*404b540aSrobert #include <bits/stl_iterator_base_funcs.h>
76*404b540aSrobert #include <bits/stl_iterator.h>
77*404b540aSrobert #include <bits/concept_check.h>
78*404b540aSrobert #include <debug/debug.h>
79*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(std)80*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
81*404b540aSrobert 
82*404b540aSrobert   /**
83*404b540aSrobert    *  @brief Swaps two values.
84*404b540aSrobert    *  @param  a  A thing of arbitrary type.
85*404b540aSrobert    *  @param  b  Another thing of arbitrary type.
86*404b540aSrobert    *  @return   Nothing.
87*404b540aSrobert    *
88*404b540aSrobert    *  This is the simple classic generic implementation.  It will work on
89*404b540aSrobert    *  any type which has a copy constructor and an assignment operator.
90*404b540aSrobert   */
91*404b540aSrobert   template<typename _Tp>
92*404b540aSrobert     inline void
93*404b540aSrobert     swap(_Tp& __a, _Tp& __b)
94*404b540aSrobert     {
95*404b540aSrobert       // concept requirements
96*404b540aSrobert       __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
97*404b540aSrobert 
98*404b540aSrobert       _Tp __tmp = __a;
99*404b540aSrobert       __a = __b;
100*404b540aSrobert       __b = __tmp;
101*404b540aSrobert     }
102*404b540aSrobert 
103*404b540aSrobert   // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
104*404b540aSrobert   // nutshell, we are partially implementing the resolution of DR 187,
105*404b540aSrobert   // when it's safe, i.e., the value_types are equal.
106*404b540aSrobert   template<bool _BoolType>
107*404b540aSrobert     struct __iter_swap
108*404b540aSrobert     {
109*404b540aSrobert       template<typename _ForwardIterator1, typename _ForwardIterator2>
110*404b540aSrobert         static void
iter_swap__iter_swap111*404b540aSrobert         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
112*404b540aSrobert         {
113*404b540aSrobert           typedef typename iterator_traits<_ForwardIterator1>::value_type
114*404b540aSrobert             _ValueType1;
115*404b540aSrobert           _ValueType1 __tmp = *__a;
116*404b540aSrobert           *__a = *__b;
117*404b540aSrobert           *__b = __tmp;
118*404b540aSrobert 	}
119*404b540aSrobert     };
120*404b540aSrobert 
121*404b540aSrobert   template<>
122*404b540aSrobert     struct __iter_swap<true>
123*404b540aSrobert     {
124*404b540aSrobert       template<typename _ForwardIterator1, typename _ForwardIterator2>
125*404b540aSrobert         static void
126*404b540aSrobert         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
127*404b540aSrobert         {
128*404b540aSrobert           swap(*__a, *__b);
129*404b540aSrobert         }
130*404b540aSrobert     };
131*404b540aSrobert 
132*404b540aSrobert   /**
133*404b540aSrobert    *  @brief Swaps the contents of two iterators.
134*404b540aSrobert    *  @param  a  An iterator.
135*404b540aSrobert    *  @param  b  Another iterator.
136*404b540aSrobert    *  @return   Nothing.
137*404b540aSrobert    *
138*404b540aSrobert    *  This function swaps the values pointed to by two iterators, not the
139*404b540aSrobert    *  iterators themselves.
140*404b540aSrobert   */
141*404b540aSrobert   template<typename _ForwardIterator1, typename _ForwardIterator2>
142*404b540aSrobert     inline void
143*404b540aSrobert     iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
144*404b540aSrobert     {
145*404b540aSrobert       typedef typename iterator_traits<_ForwardIterator1>::value_type
146*404b540aSrobert 	_ValueType1;
147*404b540aSrobert       typedef typename iterator_traits<_ForwardIterator2>::value_type
148*404b540aSrobert 	_ValueType2;
149*404b540aSrobert 
150*404b540aSrobert       // concept requirements
151*404b540aSrobert       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
152*404b540aSrobert 				  _ForwardIterator1>)
153*404b540aSrobert       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
154*404b540aSrobert 				  _ForwardIterator2>)
155*404b540aSrobert       __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
156*404b540aSrobert 				  _ValueType2>)
157*404b540aSrobert       __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
158*404b540aSrobert 				  _ValueType1>)
159*404b540aSrobert 
160*404b540aSrobert       typedef typename iterator_traits<_ForwardIterator1>::reference
161*404b540aSrobert 	_ReferenceType1;
162*404b540aSrobert       typedef typename iterator_traits<_ForwardIterator2>::reference
163*404b540aSrobert 	_ReferenceType2;
164*404b540aSrobert       std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value &&
165*404b540aSrobert 	__are_same<_ValueType1 &, _ReferenceType1>::__value &&
166*404b540aSrobert 	__are_same<_ValueType2 &, _ReferenceType2>::__value>::
167*404b540aSrobert 	iter_swap(__a, __b);
168*404b540aSrobert     }
169*404b540aSrobert 
170*404b540aSrobert   /**
171*404b540aSrobert    *  @brief This does what you think it does.
172*404b540aSrobert    *  @param  a  A thing of arbitrary type.
173*404b540aSrobert    *  @param  b  Another thing of arbitrary type.
174*404b540aSrobert    *  @return   The lesser of the parameters.
175*404b540aSrobert    *
176*404b540aSrobert    *  This is the simple classic generic implementation.  It will work on
177*404b540aSrobert    *  temporary expressions, since they are only evaluated once, unlike a
178*404b540aSrobert    *  preprocessor macro.
179*404b540aSrobert   */
180*404b540aSrobert   template<typename _Tp>
181*404b540aSrobert     inline const _Tp&
182*404b540aSrobert     min(const _Tp& __a, const _Tp& __b)
183*404b540aSrobert     {
184*404b540aSrobert       // concept requirements
185*404b540aSrobert       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
186*404b540aSrobert       //return __b < __a ? __b : __a;
187*404b540aSrobert       if (__b < __a)
188*404b540aSrobert 	return __b;
189*404b540aSrobert       return __a;
190*404b540aSrobert     }
191*404b540aSrobert 
192*404b540aSrobert   /**
193*404b540aSrobert    *  @brief This does what you think it does.
194*404b540aSrobert    *  @param  a  A thing of arbitrary type.
195*404b540aSrobert    *  @param  b  Another thing of arbitrary type.
196*404b540aSrobert    *  @return   The greater of the parameters.
197*404b540aSrobert    *
198*404b540aSrobert    *  This is the simple classic generic implementation.  It will work on
199*404b540aSrobert    *  temporary expressions, since they are only evaluated once, unlike a
200*404b540aSrobert    *  preprocessor macro.
201*404b540aSrobert   */
202*404b540aSrobert   template<typename _Tp>
203*404b540aSrobert     inline const _Tp&
204*404b540aSrobert     max(const _Tp& __a, const _Tp& __b)
205*404b540aSrobert     {
206*404b540aSrobert       // concept requirements
207*404b540aSrobert       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
208*404b540aSrobert       //return  __a < __b ? __b : __a;
209*404b540aSrobert       if (__a < __b)
210*404b540aSrobert 	return __b;
211*404b540aSrobert       return __a;
212*404b540aSrobert     }
213*404b540aSrobert 
214*404b540aSrobert   /**
215*404b540aSrobert    *  @brief This does what you think it does.
216*404b540aSrobert    *  @param  a  A thing of arbitrary type.
217*404b540aSrobert    *  @param  b  Another thing of arbitrary type.
218*404b540aSrobert    *  @param  comp  A @link s20_3_3_comparisons comparison functor@endlink.
219*404b540aSrobert    *  @return   The lesser of the parameters.
220*404b540aSrobert    *
221*404b540aSrobert    *  This will work on temporary expressions, since they are only evaluated
222*404b540aSrobert    *  once, unlike a preprocessor macro.
223*404b540aSrobert   */
224*404b540aSrobert   template<typename _Tp, typename _Compare>
225*404b540aSrobert     inline const _Tp&
226*404b540aSrobert     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
227*404b540aSrobert     {
228*404b540aSrobert       //return __comp(__b, __a) ? __b : __a;
229*404b540aSrobert       if (__comp(__b, __a))
230*404b540aSrobert 	return __b;
231*404b540aSrobert       return __a;
232*404b540aSrobert     }
233*404b540aSrobert 
234*404b540aSrobert   /**
235*404b540aSrobert    *  @brief This does what you think it does.
236*404b540aSrobert    *  @param  a  A thing of arbitrary type.
237*404b540aSrobert    *  @param  b  Another thing of arbitrary type.
238*404b540aSrobert    *  @param  comp  A @link s20_3_3_comparisons comparison functor@endlink.
239*404b540aSrobert    *  @return   The greater of the parameters.
240*404b540aSrobert    *
241*404b540aSrobert    *  This will work on temporary expressions, since they are only evaluated
242*404b540aSrobert    *  once, unlike a preprocessor macro.
243*404b540aSrobert   */
244*404b540aSrobert   template<typename _Tp, typename _Compare>
245*404b540aSrobert     inline const _Tp&
246*404b540aSrobert     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
247*404b540aSrobert     {
248*404b540aSrobert       //return __comp(__a, __b) ? __b : __a;
249*404b540aSrobert       if (__comp(__a, __b))
250*404b540aSrobert 	return __b;
251*404b540aSrobert       return __a;
252*404b540aSrobert     }
253*404b540aSrobert 
254*404b540aSrobert   // All of these auxiliary structs serve two purposes.  (1) Replace
255*404b540aSrobert   // calls to copy with memmove whenever possible.  (Memmove, not memcpy,
256*404b540aSrobert   // because the input and output ranges are permitted to overlap.)
257*404b540aSrobert   // (2) If we're using random access iterators, then write the loop as
258*404b540aSrobert   // a for loop with an explicit count.
259*404b540aSrobert 
260*404b540aSrobert   template<bool, typename>
261*404b540aSrobert     struct __copy
262*404b540aSrobert     {
263*404b540aSrobert       template<typename _II, typename _OI>
264*404b540aSrobert         static _OI
265*404b540aSrobert         copy(_II __first, _II __last, _OI __result)
266*404b540aSrobert         {
267*404b540aSrobert 	  for (; __first != __last; ++__result, ++__first)
268*404b540aSrobert 	    *__result = *__first;
269*404b540aSrobert 	  return __result;
270*404b540aSrobert 	}
271*404b540aSrobert     };
272*404b540aSrobert 
273*404b540aSrobert   template<bool _BoolType>
274*404b540aSrobert     struct __copy<_BoolType, random_access_iterator_tag>
275*404b540aSrobert     {
276*404b540aSrobert       template<typename _II, typename _OI>
277*404b540aSrobert         static _OI
278*404b540aSrobert         copy(_II __first, _II __last, _OI __result)
279*404b540aSrobert         {
280*404b540aSrobert 	  typedef typename iterator_traits<_II>::difference_type _Distance;
281*404b540aSrobert 	  for(_Distance __n = __last - __first; __n > 0; --__n)
282*404b540aSrobert 	    {
283*404b540aSrobert 	      *__result = *__first;
284*404b540aSrobert 	      ++__first;
285*404b540aSrobert 	      ++__result;
286*404b540aSrobert 	    }
287*404b540aSrobert 	  return __result;
288*404b540aSrobert 	}
289*404b540aSrobert     };
290*404b540aSrobert 
291*404b540aSrobert   template<>
292*404b540aSrobert     struct __copy<true, random_access_iterator_tag>
293*404b540aSrobert     {
294*404b540aSrobert       template<typename _Tp>
295*404b540aSrobert         static _Tp*
296*404b540aSrobert         copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
297*404b540aSrobert         {
298*404b540aSrobert 	  std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
299*404b540aSrobert 	  return __result + (__last - __first);
300*404b540aSrobert 	}
301*404b540aSrobert     };
302*404b540aSrobert 
303*404b540aSrobert   template<typename _II, typename _OI>
304*404b540aSrobert     inline _OI
305*404b540aSrobert     __copy_aux(_II __first, _II __last, _OI __result)
306*404b540aSrobert     {
307*404b540aSrobert       typedef typename iterator_traits<_II>::value_type _ValueTypeI;
308*404b540aSrobert       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
309*404b540aSrobert       typedef typename iterator_traits<_II>::iterator_category _Category;
310*404b540aSrobert       const bool __simple = (__is_scalar<_ValueTypeI>::__value
311*404b540aSrobert 	                     && __is_pointer<_II>::__value
312*404b540aSrobert 	                     && __is_pointer<_OI>::__value
313*404b540aSrobert 			     && __are_same<_ValueTypeI, _ValueTypeO>::__value);
314*404b540aSrobert 
315*404b540aSrobert       return std::__copy<__simple, _Category>::copy(__first, __last, __result);
316*404b540aSrobert     }
317*404b540aSrobert 
318*404b540aSrobert   // Helpers for streambuf iterators (either istream or ostream).
319*404b540aSrobert   template<typename _CharT>
320*404b540aSrobert   typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
321*404b540aSrobert 				  ostreambuf_iterator<_CharT> >::__type
322*404b540aSrobert     __copy_aux(_CharT*, _CharT*, ostreambuf_iterator<_CharT>);
323*404b540aSrobert 
324*404b540aSrobert   template<typename _CharT>
325*404b540aSrobert     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
326*404b540aSrobert 				    ostreambuf_iterator<_CharT> >::__type
327*404b540aSrobert     __copy_aux(const _CharT*, const _CharT*, ostreambuf_iterator<_CharT>);
328*404b540aSrobert 
329*404b540aSrobert   template<typename _CharT>
330*404b540aSrobert   typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, _CharT*>::__type
331*404b540aSrobert     __copy_aux(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
332*404b540aSrobert 	       _CharT*);
333*404b540aSrobert 
334*404b540aSrobert   template<bool, bool>
335*404b540aSrobert     struct __copy_normal
336*404b540aSrobert     {
337*404b540aSrobert       template<typename _II, typename _OI>
338*404b540aSrobert         static _OI
339*404b540aSrobert         __copy_n(_II __first, _II __last, _OI __result)
340*404b540aSrobert         { return std::__copy_aux(__first, __last, __result); }
341*404b540aSrobert     };
342*404b540aSrobert 
343*404b540aSrobert   template<>
344*404b540aSrobert     struct __copy_normal<true, false>
345*404b540aSrobert     {
346*404b540aSrobert       template<typename _II, typename _OI>
347*404b540aSrobert         static _OI
348*404b540aSrobert         __copy_n(_II __first, _II __last, _OI __result)
349*404b540aSrobert         { return std::__copy_aux(__first.base(), __last.base(), __result); }
350*404b540aSrobert     };
351*404b540aSrobert 
352*404b540aSrobert   template<>
353*404b540aSrobert     struct __copy_normal<false, true>
354*404b540aSrobert     {
355*404b540aSrobert       template<typename _II, typename _OI>
356*404b540aSrobert         static _OI
357*404b540aSrobert         __copy_n(_II __first, _II __last, _OI __result)
358*404b540aSrobert         { return _OI(std::__copy_aux(__first, __last, __result.base())); }
359*404b540aSrobert     };
360*404b540aSrobert 
361*404b540aSrobert   template<>
362*404b540aSrobert     struct __copy_normal<true, true>
363*404b540aSrobert     {
364*404b540aSrobert       template<typename _II, typename _OI>
365*404b540aSrobert         static _OI
366*404b540aSrobert         __copy_n(_II __first, _II __last, _OI __result)
367*404b540aSrobert         { return _OI(std::__copy_aux(__first.base(), __last.base(),
368*404b540aSrobert 				     __result.base())); }
369*404b540aSrobert     };
370*404b540aSrobert 
371*404b540aSrobert   /**
372*404b540aSrobert    *  @brief Copies the range [first,last) into result.
373*404b540aSrobert    *  @param  first  An input iterator.
374*404b540aSrobert    *  @param  last   An input iterator.
375*404b540aSrobert    *  @param  result An output iterator.
376*404b540aSrobert    *  @return   result + (first - last)
377*404b540aSrobert    *
378*404b540aSrobert    *  This inline function will boil down to a call to @c memmove whenever
379*404b540aSrobert    *  possible.  Failing that, if random access iterators are passed, then the
380*404b540aSrobert    *  loop count will be known (and therefore a candidate for compiler
381*404b540aSrobert    *  optimizations such as unrolling).  Result may not be contained within
382*404b540aSrobert    *  [first,last); the copy_backward function should be used instead.
383*404b540aSrobert    *
384*404b540aSrobert    *  Note that the end of the output range is permitted to be contained
385*404b540aSrobert    *  within [first,last).
386*404b540aSrobert   */
387*404b540aSrobert   template<typename _InputIterator, typename _OutputIterator>
388*404b540aSrobert     inline _OutputIterator
389*404b540aSrobert     copy(_InputIterator __first, _InputIterator __last,
390*404b540aSrobert 	 _OutputIterator __result)
391*404b540aSrobert     {
392*404b540aSrobert       // concept requirements
393*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
394*404b540aSrobert       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
395*404b540aSrobert 	    typename iterator_traits<_InputIterator>::value_type>)
396*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
397*404b540aSrobert 
398*404b540aSrobert        const bool __in = __is_normal_iterator<_InputIterator>::__value;
399*404b540aSrobert        const bool __out = __is_normal_iterator<_OutputIterator>::__value;
400*404b540aSrobert        return std::__copy_normal<__in, __out>::__copy_n(__first, __last,
401*404b540aSrobert 							__result);
402*404b540aSrobert     }
403*404b540aSrobert 
404*404b540aSrobert   // Overload for streambuf iterators.
405*404b540aSrobert   template<typename _CharT>
406*404b540aSrobert     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
407*404b540aSrobert   	       			    ostreambuf_iterator<_CharT> >::__type
408*404b540aSrobert     copy(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
409*404b540aSrobert 	 ostreambuf_iterator<_CharT>);
410*404b540aSrobert 
411*404b540aSrobert   template<bool, typename>
412*404b540aSrobert     struct __copy_backward
413*404b540aSrobert     {
414*404b540aSrobert       template<typename _BI1, typename _BI2>
415*404b540aSrobert         static _BI2
416*404b540aSrobert         __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
417*404b540aSrobert         {
418*404b540aSrobert 	  while (__first != __last)
419*404b540aSrobert 	    *--__result = *--__last;
420*404b540aSrobert 	  return __result;
421*404b540aSrobert 	}
422*404b540aSrobert     };
423*404b540aSrobert 
424*404b540aSrobert   template<bool _BoolType>
425*404b540aSrobert     struct __copy_backward<_BoolType, random_access_iterator_tag>
426*404b540aSrobert     {
427*404b540aSrobert       template<typename _BI1, typename _BI2>
428*404b540aSrobert         static _BI2
429*404b540aSrobert         __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
430*404b540aSrobert         {
431*404b540aSrobert 	  typename iterator_traits<_BI1>::difference_type __n;
432*404b540aSrobert 	  for (__n = __last - __first; __n > 0; --__n)
433*404b540aSrobert 	    *--__result = *--__last;
434*404b540aSrobert 	  return __result;
435*404b540aSrobert 	}
436*404b540aSrobert     };
437*404b540aSrobert 
438*404b540aSrobert   template<>
439*404b540aSrobert     struct __copy_backward<true, random_access_iterator_tag>
440*404b540aSrobert     {
441*404b540aSrobert       template<typename _Tp>
442*404b540aSrobert         static _Tp*
443*404b540aSrobert         __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
444*404b540aSrobert         {
445*404b540aSrobert 	  const ptrdiff_t _Num = __last - __first;
446*404b540aSrobert 	  std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
447*404b540aSrobert 	  return __result - _Num;
448*404b540aSrobert 	}
449*404b540aSrobert     };
450*404b540aSrobert 
451*404b540aSrobert   template<typename _BI1, typename _BI2>
452*404b540aSrobert     inline _BI2
453*404b540aSrobert     __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
454*404b540aSrobert     {
455*404b540aSrobert       typedef typename iterator_traits<_BI1>::value_type _ValueType1;
456*404b540aSrobert       typedef typename iterator_traits<_BI2>::value_type _ValueType2;
457*404b540aSrobert       typedef typename iterator_traits<_BI1>::iterator_category _Category;
458*404b540aSrobert       const bool __simple = (__is_scalar<_ValueType1>::__value
459*404b540aSrobert 	                     && __is_pointer<_BI1>::__value
460*404b540aSrobert 	                     && __is_pointer<_BI2>::__value
461*404b540aSrobert 			     && __are_same<_ValueType1, _ValueType2>::__value);
462*404b540aSrobert 
463*404b540aSrobert       return std::__copy_backward<__simple, _Category>::__copy_b(__first,
464*404b540aSrobert 								 __last,
465*404b540aSrobert 								 __result);
466*404b540aSrobert     }
467*404b540aSrobert 
468*404b540aSrobert   template<bool, bool>
469*404b540aSrobert     struct __copy_backward_normal
470*404b540aSrobert     {
471*404b540aSrobert       template<typename _BI1, typename _BI2>
472*404b540aSrobert         static _BI2
473*404b540aSrobert         __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
474*404b540aSrobert         { return std::__copy_backward_aux(__first, __last, __result); }
475*404b540aSrobert     };
476*404b540aSrobert 
477*404b540aSrobert   template<>
478*404b540aSrobert     struct __copy_backward_normal<true, false>
479*404b540aSrobert     {
480*404b540aSrobert       template<typename _BI1, typename _BI2>
481*404b540aSrobert         static _BI2
482*404b540aSrobert         __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
483*404b540aSrobert         { return std::__copy_backward_aux(__first.base(), __last.base(),
484*404b540aSrobert 					  __result); }
485*404b540aSrobert     };
486*404b540aSrobert 
487*404b540aSrobert   template<>
488*404b540aSrobert     struct __copy_backward_normal<false, true>
489*404b540aSrobert     {
490*404b540aSrobert       template<typename _BI1, typename _BI2>
491*404b540aSrobert         static _BI2
492*404b540aSrobert         __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
493*404b540aSrobert         { return _BI2(std::__copy_backward_aux(__first, __last,
494*404b540aSrobert 					       __result.base())); }
495*404b540aSrobert     };
496*404b540aSrobert 
497*404b540aSrobert   template<>
498*404b540aSrobert     struct __copy_backward_normal<true, true>
499*404b540aSrobert     {
500*404b540aSrobert       template<typename _BI1, typename _BI2>
501*404b540aSrobert         static _BI2
502*404b540aSrobert         __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
503*404b540aSrobert         { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
504*404b540aSrobert 					       __result.base())); }
505*404b540aSrobert     };
506*404b540aSrobert 
507*404b540aSrobert   /**
508*404b540aSrobert    *  @brief Copies the range [first,last) into result.
509*404b540aSrobert    *  @param  first  A bidirectional iterator.
510*404b540aSrobert    *  @param  last   A bidirectional iterator.
511*404b540aSrobert    *  @param  result A bidirectional iterator.
512*404b540aSrobert    *  @return   result - (first - last)
513*404b540aSrobert    *
514*404b540aSrobert    *  The function has the same effect as copy, but starts at the end of the
515*404b540aSrobert    *  range and works its way to the start, returning the start of the result.
516*404b540aSrobert    *  This inline function will boil down to a call to @c memmove whenever
517*404b540aSrobert    *  possible.  Failing that, if random access iterators are passed, then the
518*404b540aSrobert    *  loop count will be known (and therefore a candidate for compiler
519*404b540aSrobert    *  optimizations such as unrolling).
520*404b540aSrobert    *
521*404b540aSrobert    *  Result may not be in the range [first,last).  Use copy instead.  Note
522*404b540aSrobert    *  that the start of the output range may overlap [first,last).
523*404b540aSrobert   */
524*404b540aSrobert   template <typename _BI1, typename _BI2>
525*404b540aSrobert     inline _BI2
526*404b540aSrobert     copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
527*404b540aSrobert     {
528*404b540aSrobert       // concept requirements
529*404b540aSrobert       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
530*404b540aSrobert       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
531*404b540aSrobert       __glibcxx_function_requires(_ConvertibleConcept<
532*404b540aSrobert 	    typename iterator_traits<_BI1>::value_type,
533*404b540aSrobert 	    typename iterator_traits<_BI2>::value_type>)
534*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
535*404b540aSrobert 
536*404b540aSrobert       const bool __bi1 = __is_normal_iterator<_BI1>::__value;
537*404b540aSrobert       const bool __bi2 = __is_normal_iterator<_BI2>::__value;
538*404b540aSrobert       return std::__copy_backward_normal<__bi1, __bi2>::__copy_b_n(__first,
539*404b540aSrobert 								   __last,
540*404b540aSrobert 								   __result);
541*404b540aSrobert     }
542*404b540aSrobert 
543*404b540aSrobert   template<bool>
544*404b540aSrobert     struct __fill
545*404b540aSrobert     {
546*404b540aSrobert       template<typename _ForwardIterator, typename _Tp>
547*404b540aSrobert         static void
548*404b540aSrobert         fill(_ForwardIterator __first, _ForwardIterator __last,
549*404b540aSrobert 	     const _Tp& __value)
550*404b540aSrobert         {
551*404b540aSrobert 	  for (; __first != __last; ++__first)
552*404b540aSrobert 	    *__first = __value;
553*404b540aSrobert 	}
554*404b540aSrobert     };
555*404b540aSrobert 
556*404b540aSrobert   template<>
557*404b540aSrobert     struct __fill<true>
558*404b540aSrobert     {
559*404b540aSrobert       template<typename _ForwardIterator, typename _Tp>
560*404b540aSrobert         static void
561*404b540aSrobert         fill(_ForwardIterator __first, _ForwardIterator __last,
562*404b540aSrobert 	     const _Tp& __value)
563*404b540aSrobert         {
564*404b540aSrobert 	  const _Tp __tmp = __value;
565*404b540aSrobert 	  for (; __first != __last; ++__first)
566*404b540aSrobert 	    *__first = __tmp;
567*404b540aSrobert 	}
568*404b540aSrobert     };
569*404b540aSrobert 
570*404b540aSrobert   /**
571*404b540aSrobert    *  @brief Fills the range [first,last) with copies of value.
572*404b540aSrobert    *  @param  first  A forward iterator.
573*404b540aSrobert    *  @param  last   A forward iterator.
574*404b540aSrobert    *  @param  value  A reference-to-const of arbitrary type.
575*404b540aSrobert    *  @return   Nothing.
576*404b540aSrobert    *
577*404b540aSrobert    *  This function fills a range with copies of the same value.  For one-byte
578*404b540aSrobert    *  types filling contiguous areas of memory, this becomes an inline call to
579*404b540aSrobert    *  @c memset.
580*404b540aSrobert   */
581*404b540aSrobert   template<typename _ForwardIterator, typename _Tp>
582*404b540aSrobert     void
583*404b540aSrobert     fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
584*404b540aSrobert     {
585*404b540aSrobert       // concept requirements
586*404b540aSrobert       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
587*404b540aSrobert 				  _ForwardIterator>)
588*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
589*404b540aSrobert 
590*404b540aSrobert       const bool __scalar = __is_scalar<_Tp>::__value;
591*404b540aSrobert       std::__fill<__scalar>::fill(__first, __last, __value);
592*404b540aSrobert     }
593*404b540aSrobert 
594*404b540aSrobert   // Specialization: for one-byte types we can use memset.
595*404b540aSrobert   inline void
596*404b540aSrobert   fill(unsigned char* __first, unsigned char* __last, const unsigned char& __c)
597*404b540aSrobert   {
598*404b540aSrobert     __glibcxx_requires_valid_range(__first, __last);
599*404b540aSrobert     const unsigned char __tmp = __c;
600*404b540aSrobert     std::memset(__first, __tmp, __last - __first);
601*404b540aSrobert   }
602*404b540aSrobert 
603*404b540aSrobert   inline void
604*404b540aSrobert   fill(signed char* __first, signed char* __last, const signed char& __c)
605*404b540aSrobert   {
606*404b540aSrobert     __glibcxx_requires_valid_range(__first, __last);
607*404b540aSrobert     const signed char __tmp = __c;
608*404b540aSrobert     std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
609*404b540aSrobert   }
610*404b540aSrobert 
611*404b540aSrobert   inline void
612*404b540aSrobert   fill(char* __first, char* __last, const char& __c)
613*404b540aSrobert   {
614*404b540aSrobert     __glibcxx_requires_valid_range(__first, __last);
615*404b540aSrobert     const char __tmp = __c;
616*404b540aSrobert     std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
617*404b540aSrobert   }
618*404b540aSrobert 
619*404b540aSrobert   template<bool>
620*404b540aSrobert     struct __fill_n
621*404b540aSrobert     {
622*404b540aSrobert       template<typename _OutputIterator, typename _Size, typename _Tp>
623*404b540aSrobert         static _OutputIterator
624*404b540aSrobert         fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
625*404b540aSrobert         {
626*404b540aSrobert 	  for (; __n > 0; --__n, ++__first)
627*404b540aSrobert 	    *__first = __value;
628*404b540aSrobert 	  return __first;
629*404b540aSrobert 	}
630*404b540aSrobert     };
631*404b540aSrobert 
632*404b540aSrobert   template<>
633*404b540aSrobert     struct __fill_n<true>
634*404b540aSrobert     {
635*404b540aSrobert       template<typename _OutputIterator, typename _Size, typename _Tp>
636*404b540aSrobert         static _OutputIterator
637*404b540aSrobert         fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
638*404b540aSrobert         {
639*404b540aSrobert 	  const _Tp __tmp = __value;
640*404b540aSrobert 	  for (; __n > 0; --__n, ++__first)
641*404b540aSrobert 	    *__first = __tmp;
642*404b540aSrobert 	  return __first;
643*404b540aSrobert 	}
644*404b540aSrobert     };
645*404b540aSrobert 
646*404b540aSrobert   /**
647*404b540aSrobert    *  @brief Fills the range [first,first+n) with copies of value.
648*404b540aSrobert    *  @param  first  An output iterator.
649*404b540aSrobert    *  @param  n      The count of copies to perform.
650*404b540aSrobert    *  @param  value  A reference-to-const of arbitrary type.
651*404b540aSrobert    *  @return   The iterator at first+n.
652*404b540aSrobert    *
653*404b540aSrobert    *  This function fills a range with copies of the same value.  For one-byte
654*404b540aSrobert    *  types filling contiguous areas of memory, this becomes an inline call to
655*404b540aSrobert    *  @c memset.
656*404b540aSrobert   */
657*404b540aSrobert   template<typename _OutputIterator, typename _Size, typename _Tp>
658*404b540aSrobert     _OutputIterator
659*404b540aSrobert     fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
660*404b540aSrobert     {
661*404b540aSrobert       // concept requirements
662*404b540aSrobert       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
663*404b540aSrobert 
664*404b540aSrobert       const bool __scalar = __is_scalar<_Tp>::__value;
665*404b540aSrobert       return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
666*404b540aSrobert     }
667*404b540aSrobert 
668*404b540aSrobert   template<typename _Size>
669*404b540aSrobert     inline unsigned char*
670*404b540aSrobert     fill_n(unsigned char* __first, _Size __n, const unsigned char& __c)
671*404b540aSrobert     {
672*404b540aSrobert       std::fill(__first, __first + __n, __c);
673*404b540aSrobert       return __first + __n;
674*404b540aSrobert     }
675*404b540aSrobert 
676*404b540aSrobert   template<typename _Size>
677*404b540aSrobert     inline signed char*
678*404b540aSrobert     fill_n(signed char* __first, _Size __n, const signed char& __c)
679*404b540aSrobert     {
680*404b540aSrobert       std::fill(__first, __first + __n, __c);
681*404b540aSrobert       return __first + __n;
682*404b540aSrobert     }
683*404b540aSrobert 
684*404b540aSrobert   template<typename _Size>
685*404b540aSrobert     inline char*
686*404b540aSrobert     fill_n(char* __first, _Size __n, const char& __c)
687*404b540aSrobert     {
688*404b540aSrobert       std::fill(__first, __first + __n, __c);
689*404b540aSrobert       return __first + __n;
690*404b540aSrobert     }
691*404b540aSrobert 
692*404b540aSrobert   /**
693*404b540aSrobert    *  @brief Finds the places in ranges which don't match.
694*404b540aSrobert    *  @param  first1  An input iterator.
695*404b540aSrobert    *  @param  last1   An input iterator.
696*404b540aSrobert    *  @param  first2  An input iterator.
697*404b540aSrobert    *  @return   A pair of iterators pointing to the first mismatch.
698*404b540aSrobert    *
699*404b540aSrobert    *  This compares the elements of two ranges using @c == and returns a pair
700*404b540aSrobert    *  of iterators.  The first iterator points into the first range, the
701*404b540aSrobert    *  second iterator points into the second range, and the elements pointed
702*404b540aSrobert    *  to by the iterators are not equal.
703*404b540aSrobert   */
704*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2>
705*404b540aSrobert     pair<_InputIterator1, _InputIterator2>
706*404b540aSrobert     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
707*404b540aSrobert 	     _InputIterator2 __first2)
708*404b540aSrobert     {
709*404b540aSrobert       // concept requirements
710*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
711*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
712*404b540aSrobert       __glibcxx_function_requires(_EqualOpConcept<
713*404b540aSrobert 	    typename iterator_traits<_InputIterator1>::value_type,
714*404b540aSrobert 	    typename iterator_traits<_InputIterator2>::value_type>)
715*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
716*404b540aSrobert 
717*404b540aSrobert       while (__first1 != __last1 && *__first1 == *__first2)
718*404b540aSrobert         {
719*404b540aSrobert 	  ++__first1;
720*404b540aSrobert 	  ++__first2;
721*404b540aSrobert         }
722*404b540aSrobert       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
723*404b540aSrobert     }
724*404b540aSrobert 
725*404b540aSrobert   /**
726*404b540aSrobert    *  @brief Finds the places in ranges which don't match.
727*404b540aSrobert    *  @param  first1  An input iterator.
728*404b540aSrobert    *  @param  last1   An input iterator.
729*404b540aSrobert    *  @param  first2  An input iterator.
730*404b540aSrobert    *  @param  binary_pred  A binary predicate @link s20_3_1_base functor@endlink.
731*404b540aSrobert    *  @return   A pair of iterators pointing to the first mismatch.
732*404b540aSrobert    *
733*404b540aSrobert    *  This compares the elements of two ranges using the binary_pred
734*404b540aSrobert    *  parameter, and returns a pair
735*404b540aSrobert    *  of iterators.  The first iterator points into the first range, the
736*404b540aSrobert    *  second iterator points into the second range, and the elements pointed
737*404b540aSrobert    *  to by the iterators are not equal.
738*404b540aSrobert   */
739*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2,
740*404b540aSrobert 	   typename _BinaryPredicate>
741*404b540aSrobert     pair<_InputIterator1, _InputIterator2>
742*404b540aSrobert     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
743*404b540aSrobert 	     _InputIterator2 __first2, _BinaryPredicate __binary_pred)
744*404b540aSrobert     {
745*404b540aSrobert       // concept requirements
746*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
747*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
748*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
749*404b540aSrobert 
750*404b540aSrobert       while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
751*404b540aSrobert         {
752*404b540aSrobert 	  ++__first1;
753*404b540aSrobert 	  ++__first2;
754*404b540aSrobert         }
755*404b540aSrobert       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
756*404b540aSrobert     }
757*404b540aSrobert 
758*404b540aSrobert   /**
759*404b540aSrobert    *  @brief Tests a range for element-wise equality.
760*404b540aSrobert    *  @param  first1  An input iterator.
761*404b540aSrobert    *  @param  last1   An input iterator.
762*404b540aSrobert    *  @param  first2  An input iterator.
763*404b540aSrobert    *  @return   A boolean true or false.
764*404b540aSrobert    *
765*404b540aSrobert    *  This compares the elements of two ranges using @c == and returns true or
766*404b540aSrobert    *  false depending on whether all of the corresponding elements of the
767*404b540aSrobert    *  ranges are equal.
768*404b540aSrobert   */
769*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2>
770*404b540aSrobert     inline bool
771*404b540aSrobert     equal(_InputIterator1 __first1, _InputIterator1 __last1,
772*404b540aSrobert 	  _InputIterator2 __first2)
773*404b540aSrobert     {
774*404b540aSrobert       // concept requirements
775*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
776*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
777*404b540aSrobert       __glibcxx_function_requires(_EqualOpConcept<
778*404b540aSrobert 	    typename iterator_traits<_InputIterator1>::value_type,
779*404b540aSrobert 	    typename iterator_traits<_InputIterator2>::value_type>)
780*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
781*404b540aSrobert 
782*404b540aSrobert       for (; __first1 != __last1; ++__first1, ++__first2)
783*404b540aSrobert 	if (!(*__first1 == *__first2))
784*404b540aSrobert 	  return false;
785*404b540aSrobert       return true;
786*404b540aSrobert     }
787*404b540aSrobert 
788*404b540aSrobert   /**
789*404b540aSrobert    *  @brief Tests a range for element-wise equality.
790*404b540aSrobert    *  @param  first1  An input iterator.
791*404b540aSrobert    *  @param  last1   An input iterator.
792*404b540aSrobert    *  @param  first2  An input iterator.
793*404b540aSrobert    *  @param  binary_pred  A binary predicate @link s20_3_1_base functor@endlink.
794*404b540aSrobert    *  @return   A boolean true or false.
795*404b540aSrobert    *
796*404b540aSrobert    *  This compares the elements of two ranges using the binary_pred
797*404b540aSrobert    *  parameter, and returns true or
798*404b540aSrobert    *  false depending on whether all of the corresponding elements of the
799*404b540aSrobert    *  ranges are equal.
800*404b540aSrobert   */
801*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2,
802*404b540aSrobert 	   typename _BinaryPredicate>
803*404b540aSrobert     inline bool
804*404b540aSrobert     equal(_InputIterator1 __first1, _InputIterator1 __last1,
805*404b540aSrobert 	  _InputIterator2 __first2,
806*404b540aSrobert 	  _BinaryPredicate __binary_pred)
807*404b540aSrobert     {
808*404b540aSrobert       // concept requirements
809*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
810*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
811*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
812*404b540aSrobert 
813*404b540aSrobert       for (; __first1 != __last1; ++__first1, ++__first2)
814*404b540aSrobert 	if (!__binary_pred(*__first1, *__first2))
815*404b540aSrobert 	  return false;
816*404b540aSrobert       return true;
817*404b540aSrobert     }
818*404b540aSrobert 
819*404b540aSrobert   /**
820*404b540aSrobert    *  @brief Performs "dictionary" comparison on ranges.
821*404b540aSrobert    *  @param  first1  An input iterator.
822*404b540aSrobert    *  @param  last1   An input iterator.
823*404b540aSrobert    *  @param  first2  An input iterator.
824*404b540aSrobert    *  @param  last2   An input iterator.
825*404b540aSrobert    *  @return   A boolean true or false.
826*404b540aSrobert    *
827*404b540aSrobert    *  "Returns true if the sequence of elements defined by the range
828*404b540aSrobert    *  [first1,last1) is lexicographically less than the sequence of elements
829*404b540aSrobert    *  defined by the range [first2,last2).  Returns false otherwise."
830*404b540aSrobert    *  (Quoted from [25.3.8]/1.)  If the iterators are all character pointers,
831*404b540aSrobert    *  then this is an inline call to @c memcmp.
832*404b540aSrobert   */
833*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2>
834*404b540aSrobert     bool
835*404b540aSrobert     lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
836*404b540aSrobert 			    _InputIterator2 __first2, _InputIterator2 __last2)
837*404b540aSrobert     {
838*404b540aSrobert       // concept requirements
839*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
840*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
841*404b540aSrobert       __glibcxx_function_requires(_LessThanOpConcept<
842*404b540aSrobert 	    typename iterator_traits<_InputIterator1>::value_type,
843*404b540aSrobert 	    typename iterator_traits<_InputIterator2>::value_type>)
844*404b540aSrobert       __glibcxx_function_requires(_LessThanOpConcept<
845*404b540aSrobert 	    typename iterator_traits<_InputIterator2>::value_type,
846*404b540aSrobert 	    typename iterator_traits<_InputIterator1>::value_type>)
847*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
848*404b540aSrobert       __glibcxx_requires_valid_range(__first2, __last2);
849*404b540aSrobert 
850*404b540aSrobert       for (; __first1 != __last1 && __first2 != __last2;
851*404b540aSrobert 	   ++__first1, ++__first2)
852*404b540aSrobert 	{
853*404b540aSrobert 	  if (*__first1 < *__first2)
854*404b540aSrobert 	    return true;
855*404b540aSrobert 	  if (*__first2 < *__first1)
856*404b540aSrobert 	    return false;
857*404b540aSrobert 	}
858*404b540aSrobert       return __first1 == __last1 && __first2 != __last2;
859*404b540aSrobert     }
860*404b540aSrobert 
861*404b540aSrobert   /**
862*404b540aSrobert    *  @brief Performs "dictionary" comparison on ranges.
863*404b540aSrobert    *  @param  first1  An input iterator.
864*404b540aSrobert    *  @param  last1   An input iterator.
865*404b540aSrobert    *  @param  first2  An input iterator.
866*404b540aSrobert    *  @param  last2   An input iterator.
867*404b540aSrobert    *  @param  comp  A @link s20_3_3_comparisons comparison functor@endlink.
868*404b540aSrobert    *  @return   A boolean true or false.
869*404b540aSrobert    *
870*404b540aSrobert    *  The same as the four-parameter @c lexigraphical_compare, but uses the
871*404b540aSrobert    *  comp parameter instead of @c <.
872*404b540aSrobert   */
873*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2,
874*404b540aSrobert 	   typename _Compare>
875*404b540aSrobert     bool
876*404b540aSrobert     lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
877*404b540aSrobert 			    _InputIterator2 __first2, _InputIterator2 __last2,
878*404b540aSrobert 			    _Compare __comp)
879*404b540aSrobert     {
880*404b540aSrobert       // concept requirements
881*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
882*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
883*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
884*404b540aSrobert       __glibcxx_requires_valid_range(__first2, __last2);
885*404b540aSrobert 
886*404b540aSrobert       for (; __first1 != __last1 && __first2 != __last2;
887*404b540aSrobert 	   ++__first1, ++__first2)
888*404b540aSrobert 	{
889*404b540aSrobert 	  if (__comp(*__first1, *__first2))
890*404b540aSrobert 	    return true;
891*404b540aSrobert 	  if (__comp(*__first2, *__first1))
892*404b540aSrobert 	    return false;
893*404b540aSrobert 	}
894*404b540aSrobert       return __first1 == __last1 && __first2 != __last2;
895*404b540aSrobert     }
896*404b540aSrobert 
897*404b540aSrobert   inline bool
898*404b540aSrobert   lexicographical_compare(const unsigned char* __first1,
899*404b540aSrobert 			  const unsigned char* __last1,
900*404b540aSrobert 			  const unsigned char* __first2,
901*404b540aSrobert 			  const unsigned char* __last2)
902*404b540aSrobert   {
903*404b540aSrobert     __glibcxx_requires_valid_range(__first1, __last1);
904*404b540aSrobert     __glibcxx_requires_valid_range(__first2, __last2);
905*404b540aSrobert 
906*404b540aSrobert     const size_t __len1 = __last1 - __first1;
907*404b540aSrobert     const size_t __len2 = __last2 - __first2;
908*404b540aSrobert     const int __result = std::memcmp(__first1, __first2,
909*404b540aSrobert 				     std::min(__len1, __len2));
910*404b540aSrobert     return __result != 0 ? __result < 0 : __len1 < __len2;
911*404b540aSrobert   }
912*404b540aSrobert 
913*404b540aSrobert   inline bool
914*404b540aSrobert   lexicographical_compare(const char* __first1, const char* __last1,
915*404b540aSrobert 			  const char* __first2, const char* __last2)
916*404b540aSrobert   {
917*404b540aSrobert     __glibcxx_requires_valid_range(__first1, __last1);
918*404b540aSrobert     __glibcxx_requires_valid_range(__first2, __last2);
919*404b540aSrobert 
920*404b540aSrobert #if CHAR_MAX == SCHAR_MAX
921*404b540aSrobert     return std::lexicographical_compare((const signed char*) __first1,
922*404b540aSrobert 					(const signed char*) __last1,
923*404b540aSrobert 					(const signed char*) __first2,
924*404b540aSrobert 					(const signed char*) __last2);
925*404b540aSrobert #else /* CHAR_MAX == SCHAR_MAX */
926*404b540aSrobert     return std::lexicographical_compare((const unsigned char*) __first1,
927*404b540aSrobert 					(const unsigned char*) __last1,
928*404b540aSrobert 					(const unsigned char*) __first2,
929*404b540aSrobert 					(const unsigned char*) __last2);
930*404b540aSrobert #endif /* CHAR_MAX == SCHAR_MAX */
931*404b540aSrobert   }
932*404b540aSrobert 
933*404b540aSrobert _GLIBCXX_END_NAMESPACE
934*404b540aSrobert 
935*404b540aSrobert #endif
936