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