]> gcc.gnu.org Git - gcc.git/blame - libstdc++-v3/include/bits/stl_algobase.h
libstdc++: Only use std::compare_three_way when concepts are supported
[gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
CommitLineData
c60cd1dc 1// Core algorithmic facilities -*- C++ -*-
42526146 2
8d9254fc 3// Copyright (C) 2001-2020 Free Software Foundation, Inc.
42526146
PE
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
42526146
PE
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
42526146 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
42526146 24
725dc051
BK
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
f910786b 51/** @file bits/stl_algobase.h
729e3d3f 52 * This is an internal header file, included by other library headers.
f910786b 53 * Do not attempt to use it directly. @headername{algorithm}
725dc051
BK
54 */
55
046d30f4
PC
56#ifndef _STL_ALGOBASE_H
57#define _STL_ALGOBASE_H 1
725dc051 58
9cfeea6e 59#include <bits/c++config.h>
39b8cd70 60#include <bits/functexcept.h>
badd64ad 61#include <bits/cpp_type_traits.h>
105c6331 62#include <ext/type_traits.h>
6725add5 63#include <ext/numeric_traits.h>
8ad7097c 64#include <bits/stl_pair.h>
4f39bf5c 65#include <bits/stl_iterator_base_types.h>
30a20a1e 66#include <bits/stl_iterator_base_funcs.h>
725dc051 67#include <bits/stl_iterator.h>
30a20a1e 68#include <bits/concept_check.h>
285b36d6 69#include <debug/debug.h>
e5795ce4 70#include <bits/move.h> // For std::swap
ea89b248 71#include <bits/predefined_ops.h>
ff2e7f19
MG
72#if __cplusplus >= 201103L
73# include <type_traits>
74#endif
f1355c8d
JW
75#if __cplusplus > 201703L
76# include <compare>
77#endif
3a6b0f54 78
12ffa228
BK
79namespace std _GLIBCXX_VISIBILITY(default)
80{
81_GLIBCXX_BEGIN_NAMESPACE_VERSION
575665ff 82
3a66e68a
ESR
83 /*
84 * A constexpr wrapper for __builtin_memmove.
85 * @param __num The number of elements of type _Tp (not bytes).
86 */
87 template<bool _IsMove, typename _Tp>
88 _GLIBCXX14_CONSTEXPR
89 inline void*
90 __memmove(_Tp* __dst, const _Tp* __src, size_t __num)
91 {
92#ifdef __cpp_lib_is_constant_evaluated
93 if (std::is_constant_evaluated())
94 {
95 for(; __num > 0; --__num)
96 {
97 if constexpr (_IsMove)
98 *__dst = std::move(*__src);
99 else
100 *__dst = *__src;
101 ++__src;
102 ++__dst;
103 }
104 return __dst;
105 }
106 else
107#endif
108 return __builtin_memmove(__dst, __src, sizeof(_Tp) * __num);
109 return __dst;
110 }
111
112 /*
113 * A constexpr wrapper for __builtin_memcmp.
114 * @param __num The number of elements of type _Tp (not bytes).
115 */
116 template<typename _Tp>
117 _GLIBCXX14_CONSTEXPR
118 inline int
119 __memcmp(const _Tp* __first1, const _Tp* __first2, size_t __num)
120 {
121#ifdef __cpp_lib_is_constant_evaluated
122 if (std::is_constant_evaluated())
123 {
124 for(; __num > 0; ++__first1, ++__first2, --__num)
125 if (*__first1 != *__first2)
126 return *__first1 < *__first2 ? -1 : 1;
127 return 0;
128 }
129 else
130#endif
131 return __builtin_memcmp(__first1, __first2, sizeof(_Tp) * __num);
132 }
133
734f5023 134#if __cplusplus < 201103L
575665ff
CJ
135 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
136 // nutshell, we are partially implementing the resolution of DR 187,
137 // when it's safe, i.e., the value_types are equal.
138 template<bool _BoolType>
139 struct __iter_swap
140 {
141 template<typename _ForwardIterator1, typename _ForwardIterator2>
7a91c710 142 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
143 static void
144 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
145 {
146 typedef typename iterator_traits<_ForwardIterator1>::value_type
147 _ValueType1;
148 _ValueType1 __tmp = *__a;
149 *__a = *__b;
150 *__b = __tmp;
575665ff
CJ
151 }
152 };
153
154 template<>
155 struct __iter_swap<true>
156 {
157 template<typename _ForwardIterator1, typename _ForwardIterator2>
7a91c710 158 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
159 static void
160 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
161 {
162 swap(*__a, *__b);
163 }
575665ff 164 };
93d9a365 165#endif
575665ff 166
729e3d3f
PE
167 /**
168 * @brief Swaps the contents of two iterators.
5b9daa7e 169 * @ingroup mutating_algorithms
93c66bc6
BK
170 * @param __a An iterator.
171 * @param __b Another iterator.
729e3d3f
PE
172 * @return Nothing.
173 *
174 * This function swaps the values pointed to by two iterators, not the
175 * iterators themselves.
176 */
08addde6 177 template<typename _ForwardIterator1, typename _ForwardIterator2>
7a91c710 178 _GLIBCXX20_CONSTEXPR
02d92e3b 179 inline void
08addde6 180 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
02d92e3b 181 {
02d92e3b 182 // concept requirements
ffa67767
PC
183 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
184 _ForwardIterator1>)
185 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
186 _ForwardIterator2>)
93d9a365 187
734f5023 188#if __cplusplus < 201103L
93d9a365
PC
189 typedef typename iterator_traits<_ForwardIterator1>::value_type
190 _ValueType1;
191 typedef typename iterator_traits<_ForwardIterator2>::value_type
192 _ValueType2;
193
ffa67767
PC
194 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
195 _ValueType2>)
196 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
197 _ValueType1>)
aed63147
CJ
198
199 typedef typename iterator_traits<_ForwardIterator1>::reference
200 _ReferenceType1;
201 typedef typename iterator_traits<_ForwardIterator2>::reference
202 _ReferenceType2;
d22a3166
PC
203 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
204 && __are_same<_ValueType1&, _ReferenceType1>::__value
205 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
575665ff 206 iter_swap(__a, __b);
93d9a365
PC
207#else
208 swap(*__a, *__b);
209#endif
02d92e3b
SW
210 }
211
91b0b94a
PC
212 /**
213 * @brief Swap the elements of two sequences.
5b9daa7e 214 * @ingroup mutating_algorithms
93c66bc6
BK
215 * @param __first1 A forward iterator.
216 * @param __last1 A forward iterator.
217 * @param __first2 A forward iterator.
91b0b94a
PC
218 * @return An iterator equal to @p first2+(last1-first1).
219 *
220 * Swaps each element in the range @p [first1,last1) with the
221 * corresponding element in the range @p [first2,(last1-first1)).
222 * The ranges must not overlap.
223 */
224 template<typename _ForwardIterator1, typename _ForwardIterator2>
7a91c710 225 _GLIBCXX20_CONSTEXPR
91b0b94a
PC
226 _ForwardIterator2
227 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
228 _ForwardIterator2 __first2)
229 {
230 // concept requirements
231 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
232 _ForwardIterator1>)
233 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
234 _ForwardIterator2>)
91b0b94a
PC
235 __glibcxx_requires_valid_range(__first1, __last1);
236
f970a17d 237 for (; __first1 != __last1; ++__first1, (void)++__first2)
91b0b94a
PC
238 std::iter_swap(__first1, __first2);
239 return __first2;
240 }
241
729e3d3f
PE
242 /**
243 * @brief This does what you think it does.
5b9daa7e 244 * @ingroup sorting_algorithms
93c66bc6
BK
245 * @param __a A thing of arbitrary type.
246 * @param __b Another thing of arbitrary type.
729e3d3f
PE
247 * @return The lesser of the parameters.
248 *
249 * This is the simple classic generic implementation. It will work on
250 * temporary expressions, since they are only evaluated once, unlike a
251 * preprocessor macro.
252 */
02d92e3b 253 template<typename _Tp>
8dff34fe 254 _GLIBCXX14_CONSTEXPR
02d92e3b
SW
255 inline const _Tp&
256 min(const _Tp& __a, const _Tp& __b)
257 {
258 // concept requirements
3d7c150e 259 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
4fd97a63
PC
260 //return __b < __a ? __b : __a;
261 if (__b < __a)
262 return __b;
263 return __a;
02d92e3b
SW
264 }
265
1b4a6975
PE
266 /**
267 * @brief This does what you think it does.
5b9daa7e 268 * @ingroup sorting_algorithms
93c66bc6
BK
269 * @param __a A thing of arbitrary type.
270 * @param __b Another thing of arbitrary type.
1b4a6975
PE
271 * @return The greater of the parameters.
272 *
273 * This is the simple classic generic implementation. It will work on
274 * temporary expressions, since they are only evaluated once, unlike a
275 * preprocessor macro.
276 */
02d92e3b 277 template<typename _Tp>
8dff34fe 278 _GLIBCXX14_CONSTEXPR
02d92e3b 279 inline const _Tp&
ed6814f7 280 max(const _Tp& __a, const _Tp& __b)
02d92e3b
SW
281 {
282 // concept requirements
3d7c150e 283 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
4fd97a63
PC
284 //return __a < __b ? __b : __a;
285 if (__a < __b)
286 return __b;
287 return __a;
02d92e3b
SW
288 }
289
1b4a6975
PE
290 /**
291 * @brief This does what you think it does.
5b9daa7e 292 * @ingroup sorting_algorithms
93c66bc6
BK
293 * @param __a A thing of arbitrary type.
294 * @param __b Another thing of arbitrary type.
295 * @param __comp A @link comparison_functors comparison functor@endlink.
1b4a6975
PE
296 * @return The lesser of the parameters.
297 *
298 * This will work on temporary expressions, since they are only evaluated
299 * once, unlike a preprocessor macro.
300 */
02d92e3b 301 template<typename _Tp, typename _Compare>
8dff34fe 302 _GLIBCXX14_CONSTEXPR
02d92e3b
SW
303 inline const _Tp&
304 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
4fd97a63
PC
305 {
306 //return __comp(__b, __a) ? __b : __a;
307 if (__comp(__b, __a))
308 return __b;
309 return __a;
310 }
02d92e3b 311
1b4a6975
PE
312 /**
313 * @brief This does what you think it does.
5b9daa7e 314 * @ingroup sorting_algorithms
93c66bc6
BK
315 * @param __a A thing of arbitrary type.
316 * @param __b Another thing of arbitrary type.
317 * @param __comp A @link comparison_functors comparison functor@endlink.
1b4a6975
PE
318 * @return The greater of the parameters.
319 *
320 * This will work on temporary expressions, since they are only evaluated
321 * once, unlike a preprocessor macro.
322 */
02d92e3b 323 template<typename _Tp, typename _Compare>
8dff34fe 324 _GLIBCXX14_CONSTEXPR
02d92e3b
SW
325 inline const _Tp&
326 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
4fd97a63
PC
327 {
328 //return __comp(__a, __b) ? __b : __a;
329 if (__comp(__a, __b))
330 return __b;
331 return __a;
332 }
02d92e3b 333
e1c444fe
FD
334 // Fallback implementation of the function in bits/stl_iterator.h used to
335 // remove the __normal_iterator wrapper. See copy, fill, ...
a2fe9203 336 template<typename _Iterator>
3a66e68a 337 _GLIBCXX20_CONSTEXPR
e1c444fe 338 inline _Iterator
6989b63f 339 __niter_base(_Iterator __it)
ff2e7f19 340 _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
e1c444fe 341 { return __it; }
6989b63f 342
315aadc8
FD
343 // Reverse the __niter_base transformation to get a
344 // __normal_iterator back again (this assumes that __normal_iterator
345 // is only used to wrap random access iterators, like pointers).
346 template<typename _From, typename _To>
3a66e68a 347 _GLIBCXX20_CONSTEXPR
315aadc8
FD
348 inline _From
349 __niter_wrap(_From __from, _To __res)
350 { return __from + (__res - std::__niter_base(__from)); }
351
352 // No need to wrap, iterator already has the right type.
353 template<typename _Iterator>
3a66e68a 354 _GLIBCXX20_CONSTEXPR
315aadc8 355 inline _Iterator
e874029d 356 __niter_wrap(const _Iterator&, _Iterator __res)
315aadc8
FD
357 { return __res; }
358
5e91e92e 359 // All of these auxiliary structs serve two purposes. (1) Replace
02d92e3b
SW
360 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
361 // because the input and output ranges are permitted to overlap.)
362 // (2) If we're using random access iterators, then write the loop as
363 // a for loop with an explicit count.
364
61f5cb23 365 template<bool _IsMove, bool _IsSimple, typename _Category>
3c167a8b 366 struct __copy_move
02d92e3b 367 {
695e0fbf 368 template<typename _II, typename _OI>
3a66e68a 369 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
370 static _OI
371 __copy_m(_II __first, _II __last, _OI __result)
372 {
f970a17d 373 for (; __first != __last; ++__result, (void)++__first)
5f6d5f0a
PC
374 *__result = *__first;
375 return __result;
376 }
377 };
378
734f5023 379#if __cplusplus >= 201103L
5f6d5f0a
PC
380 template<typename _Category>
381 struct __copy_move<true, false, _Category>
382 {
383 template<typename _II, typename _OI>
3a66e68a 384 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
385 static _OI
386 __copy_m(_II __first, _II __last, _OI __result)
387 {
f970a17d 388 for (; __first != __last; ++__result, (void)++__first)
5f6d5f0a
PC
389 *__result = std::move(*__first);
390 return __result;
391 }
392 };
393#endif
394
395 template<>
396 struct __copy_move<false, false, random_access_iterator_tag>
397 {
398 template<typename _II, typename _OI>
3a66e68a 399 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
400 static _OI
401 __copy_m(_II __first, _II __last, _OI __result)
402 {
5f6d5f0a
PC
403 typedef typename iterator_traits<_II>::difference_type _Distance;
404 for(_Distance __n = __last - __first; __n > 0; --__n)
405 {
406 *__result = *__first;
407 ++__first;
408 ++__result;
409 }
695e0fbf
PC
410 return __result;
411 }
412 };
02d92e3b 413
734f5023 414#if __cplusplus >= 201103L
5f6d5f0a
PC
415 template<>
416 struct __copy_move<true, false, random_access_iterator_tag>
02d92e3b 417 {
695e0fbf 418 template<typename _II, typename _OI>
3a66e68a 419 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
420 static _OI
421 __copy_m(_II __first, _II __last, _OI __result)
422 {
695e0fbf
PC
423 typedef typename iterator_traits<_II>::difference_type _Distance;
424 for(_Distance __n = __last - __first; __n > 0; --__n)
425 {
5f6d5f0a 426 *__result = std::move(*__first);
695e0fbf
PC
427 ++__first;
428 ++__result;
429 }
430 return __result;
46c4e5d6 431 }
695e0fbf 432 };
5f6d5f0a 433#endif
02d92e3b 434
f0112db9
PC
435 template<bool _IsMove>
436 struct __copy_move<_IsMove, true, random_access_iterator_tag>
02d92e3b 437 {
695e0fbf 438 template<typename _Tp>
3a66e68a 439 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
440 static _Tp*
441 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
442 {
5275f3e5 443#if __cplusplus >= 201103L
f1d591e8
JW
444 using __assignable = conditional<_IsMove,
445 is_move_assignable<_Tp>,
446 is_copy_assignable<_Tp>>;
5275f3e5 447 // trivial types can have deleted assignment
f1d591e8 448 static_assert( __assignable::type::value, "type is not assignable" );
5275f3e5 449#endif
f7d601a5
PC
450 const ptrdiff_t _Num = __last - __first;
451 if (_Num)
3a66e68a 452 std::__memmove<_IsMove>(__result, __first, _Num);
f7d601a5 453 return __result + _Num;
695e0fbf
PC
454 }
455 };
02d92e3b 456
0002d5d2 457 // Helpers for streambuf iterators (either istream or ostream).
39b8cd70
PC
458 // NB: avoid including <iosfwd>, relatively large.
459 template<typename _CharT>
460 struct char_traits;
461
462 template<typename _CharT, typename _Traits>
463 class istreambuf_iterator;
464
465 template<typename _CharT, typename _Traits>
466 class ostreambuf_iterator;
467
f0112db9 468 template<bool _IsMove, typename _CharT>
e5795ce4 469 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
39b8cd70 470 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
f0112db9
PC
471 __copy_move_a2(_CharT*, _CharT*,
472 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
0002d5d2 473
f0112db9 474 template<bool _IsMove, typename _CharT>
e5795ce4 475 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
39b8cd70 476 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
f0112db9
PC
477 __copy_move_a2(const _CharT*, const _CharT*,
478 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
0002d5d2 479
f0112db9 480 template<bool _IsMove, typename _CharT>
f56fe8ff
PC
481 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
482 _CharT*>::__type
f0112db9
PC
483 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
484 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
485
486 template<bool _IsMove, typename _II, typename _OI>
3a66e68a 487 _GLIBCXX20_CONSTEXPR
f0112db9
PC
488 inline _OI
489 __copy_move_a2(_II __first, _II __last, _OI __result)
6004c17b
FD
490 {
491 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
492 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
493 typedef typename iterator_traits<_II>::iterator_category _Category;
494 const bool __simple = (__is_trivially_copyable(_ValueTypeI)
495 && __is_pointer<_II>::__value
496 && __is_pointer<_OI>::__value
497 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
498 return std::__copy_move<_IsMove, __simple,
499 _Category>::__copy_m(__first, __last, __result);
500 }
501
502_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
503
504 template<typename _Tp, typename _Ref, typename _Ptr>
505 struct _Deque_iterator;
506
507_GLIBCXX_END_NAMESPACE_CONTAINER
508
509 template<bool _IsMove,
510 typename _Tp, typename _Ref, typename _Ptr, typename _OI>
511 _OI
512 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
513 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
514 _OI);
515
516 template<bool _IsMove,
517 typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
518 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
519 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
520 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
521 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
522
523 template<bool _IsMove, typename _II, typename _Tp>
524 typename __gnu_cxx::__enable_if<
525 __is_random_access_iter<_II>::__value,
526 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
527 __copy_move_a1(_II, _II, _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
528
529 template<bool _IsMove, typename _II, typename _OI>
530 _GLIBCXX20_CONSTEXPR
531 inline _OI
532 __copy_move_a1(_II __first, _II __last, _OI __result)
533 { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
534
535 template<bool _IsMove, typename _II, typename _OI>
536 _GLIBCXX20_CONSTEXPR
537 inline _OI
538 __copy_move_a(_II __first, _II __last, _OI __result)
f0112db9 539 {
315aadc8 540 return std::__niter_wrap(__result,
6004c17b
FD
541 std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
542 std::__niter_base(__last),
543 std::__niter_base(__result)));
f0112db9 544 }
0002d5d2 545
6004c17b
FD
546 template<bool _IsMove,
547 typename _Ite, typename _Seq, typename _Cat, typename _OI>
548 _OI
549 __copy_move_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
550 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
551 _OI);
552
553 template<bool _IsMove,
554 typename _II, typename _Ite, typename _Seq, typename _Cat>
555 __gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
556 __copy_move_a(_II, _II,
557 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
558
559 template<bool _IsMove,
560 typename _IIte, typename _ISeq, typename _ICat,
561 typename _OIte, typename _OSeq, typename _OCat>
562 ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>
563 __copy_move_a(const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
564 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
565 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
566
1b4a6975
PE
567 /**
568 * @brief Copies the range [first,last) into result.
5b9daa7e 569 * @ingroup mutating_algorithms
93c66bc6
BK
570 * @param __first An input iterator.
571 * @param __last An input iterator.
572 * @param __result An output iterator.
1b4a6975
PE
573 * @return result + (first - last)
574 *
575 * This inline function will boil down to a call to @c memmove whenever
576 * possible. Failing that, if random access iterators are passed, then the
577 * loop count will be known (and therefore a candidate for compiler
119dbb1f
JQ
578 * optimizations such as unrolling). Result may not be contained within
579 * [first,last); the copy_backward function should be used instead.
580 *
581 * Note that the end of the output range is permitted to be contained
582 * within [first,last).
1b4a6975 583 */
d22a3166 584 template<typename _II, typename _OI>
3a66e68a 585 _GLIBCXX20_CONSTEXPR
d22a3166
PC
586 inline _OI
587 copy(_II __first, _II __last, _OI __result)
02d92e3b
SW
588 {
589 // concept requirements
d22a3166
PC
590 __glibcxx_function_requires(_InputIteratorConcept<_II>)
591 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
592 typename iterator_traits<_II>::value_type>)
84a9d3b6 593 __glibcxx_requires_can_increment_range(__first, __last, __result);
02d92e3b 594
6004c17b 595 return std::__copy_move_a<__is_move_iterator<_II>::__value>
84a9d3b6 596 (std::__miter_base(__first), std::__miter_base(__last), __result);
02d92e3b 597 }
0002d5d2 598
734f5023 599#if __cplusplus >= 201103L
3c167a8b
PC
600 /**
601 * @brief Moves the range [first,last) into result.
5b9daa7e 602 * @ingroup mutating_algorithms
93c66bc6
BK
603 * @param __first An input iterator.
604 * @param __last An input iterator.
605 * @param __result An output iterator.
3c167a8b
PC
606 * @return result + (first - last)
607 *
608 * This inline function will boil down to a call to @c memmove whenever
609 * possible. Failing that, if random access iterators are passed, then the
610 * loop count will be known (and therefore a candidate for compiler
611 * optimizations such as unrolling). Result may not be contained within
612 * [first,last); the move_backward function should be used instead.
613 *
614 * Note that the end of the output range is permitted to be contained
615 * within [first,last).
616 */
617 template<typename _II, typename _OI>
3a66e68a 618 _GLIBCXX20_CONSTEXPR
3c167a8b
PC
619 inline _OI
620 move(_II __first, _II __last, _OI __result)
621 {
622 // concept requirements
623 __glibcxx_function_requires(_InputIteratorConcept<_II>)
624 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
625 typename iterator_traits<_II>::value_type>)
84a9d3b6 626 __glibcxx_requires_can_increment_range(__first, __last, __result);
3c167a8b 627
6004c17b
FD
628 return std::__copy_move_a<true>(std::__miter_base(__first),
629 std::__miter_base(__last), __result);
3c167a8b 630 }
245a5fe5
PC
631
632#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
633#else
634#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
3c167a8b 635#endif
d22a3166 636
6004c17b 637 template<bool _IsMove, bool _IsSimple, typename _Category>
3c167a8b 638 struct __copy_move_backward
badd64ad
PC
639 {
640 template<typename _BI1, typename _BI2>
3a66e68a 641 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
642 static _BI2
643 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
644 {
badd64ad 645 while (__first != __last)
5f6d5f0a 646 *--__result = *--__last;
badd64ad
PC
647 return __result;
648 }
02d92e3b
SW
649 };
650
734f5023 651#if __cplusplus >= 201103L
5f6d5f0a
PC
652 template<typename _Category>
653 struct __copy_move_backward<true, false, _Category>
654 {
655 template<typename _BI1, typename _BI2>
3a66e68a 656 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
657 static _BI2
658 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
659 {
5f6d5f0a
PC
660 while (__first != __last)
661 *--__result = std::move(*--__last);
662 return __result;
663 }
664 };
665#endif
666
667 template<>
668 struct __copy_move_backward<false, false, random_access_iterator_tag>
badd64ad
PC
669 {
670 template<typename _BI1, typename _BI2>
3a66e68a 671 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
672 static _BI2
673 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
674 {
3a66e68a
ESR
675 typename iterator_traits<_BI1>::difference_type
676 __n = __last - __first;
677 for (; __n > 0; --__n)
5f6d5f0a 678 *--__result = *--__last;
badd64ad
PC
679 return __result;
680 }
02d92e3b
SW
681 };
682
734f5023 683#if __cplusplus >= 201103L
5f6d5f0a
PC
684 template<>
685 struct __copy_move_backward<true, false, random_access_iterator_tag>
686 {
687 template<typename _BI1, typename _BI2>
3a66e68a 688 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
689 static _BI2
690 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
691 {
3a66e68a
ESR
692 typename iterator_traits<_BI1>::difference_type
693 __n = __last - __first;
694 for (; __n > 0; --__n)
5f6d5f0a
PC
695 *--__result = std::move(*--__last);
696 return __result;
697 }
698 };
699#endif
700
f0112db9
PC
701 template<bool _IsMove>
702 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
badd64ad
PC
703 {
704 template<typename _Tp>
3a66e68a 705 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
706 static _Tp*
707 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
708 {
5275f3e5 709#if __cplusplus >= 201103L
f1d591e8
JW
710 using __assignable = conditional<_IsMove,
711 is_move_assignable<_Tp>,
712 is_copy_assignable<_Tp>>;
5275f3e5 713 // trivial types can have deleted assignment
f1d591e8 714 static_assert( __assignable::type::value, "type is not assignable" );
5275f3e5 715#endif
badd64ad 716 const ptrdiff_t _Num = __last - __first;
f7d601a5 717 if (_Num)
3a66e68a 718 std::__memmove<_IsMove>(__result - _Num, __first, _Num);
badd64ad
PC
719 return __result - _Num;
720 }
02d92e3b
SW
721 };
722
f0112db9 723 template<bool _IsMove, typename _BI1, typename _BI2>
3a66e68a 724 _GLIBCXX20_CONSTEXPR
02d92e3b 725 inline _BI2
6004c17b 726 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
02d92e3b 727 {
695e0fbf
PC
728 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
729 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
badd64ad 730 typedef typename iterator_traits<_BI1>::iterator_category _Category;
20a0c4e3 731 const bool __simple = (__is_trivially_copyable(_ValueType1)
e5795ce4
FD
732 && __is_pointer<_BI1>::__value
733 && __is_pointer<_BI2>::__value
4d73fac9 734 && __are_same<_ValueType1, _ValueType2>::__value);
badd64ad 735
3a66e68a
ESR
736#ifdef __cpp_lib_is_constant_evaluated
737 if (std::is_constant_evaluated())
738 return std::__copy_move_backward<true, false,
739 _Category>::__copy_move_b(__first, __last,
740 __result);
741#endif
f0112db9 742 return std::__copy_move_backward<_IsMove, __simple,
e5795ce4 743 _Category>::__copy_move_b(__first,
e75ea710
PC
744 __last,
745 __result);
02d92e3b
SW
746 }
747
f0112db9 748 template<bool _IsMove, typename _BI1, typename _BI2>
3a66e68a 749 _GLIBCXX20_CONSTEXPR
f0112db9 750 inline _BI2
6004c17b
FD
751 __copy_move_backward_a1(_BI1 __first, _BI1 __last, _BI2 __result)
752 { return std::__copy_move_backward_a2<_IsMove>(__first, __last, __result); }
753
754 template<bool _IsMove,
755 typename _Tp, typename _Ref, typename _Ptr, typename _OI>
756 _OI
757 __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
758 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
759 _OI);
760
761 template<bool _IsMove,
762 typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
763 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
764 __copy_move_backward_a1(
765 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
766 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
767 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
768
769 template<bool _IsMove, typename _II, typename _Tp>
770 typename __gnu_cxx::__enable_if<
771 __is_random_access_iter<_II>::__value,
772 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
773 __copy_move_backward_a1(_II, _II,
774 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
775
776 template<bool _IsMove, typename _II, typename _OI>
777 _GLIBCXX20_CONSTEXPR
778 inline _OI
779 __copy_move_backward_a(_II __first, _II __last, _OI __result)
f0112db9 780 {
315aadc8 781 return std::__niter_wrap(__result,
6004c17b 782 std::__copy_move_backward_a1<_IsMove>
6989b63f
PC
783 (std::__niter_base(__first), std::__niter_base(__last),
784 std::__niter_base(__result)));
f0112db9
PC
785 }
786
6004c17b
FD
787 template<bool _IsMove,
788 typename _Ite, typename _Seq, typename _Cat, typename _OI>
789 _OI
790 __copy_move_backward_a(
791 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
792 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
793 _OI);
794
795 template<bool _IsMove,
796 typename _II, typename _Ite, typename _Seq, typename _Cat>
797 __gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
798 __copy_move_backward_a(_II, _II,
799 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
800
801 template<bool _IsMove,
802 typename _IIte, typename _ISeq, typename _ICat,
803 typename _OIte, typename _OSeq, typename _OCat>
804 ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>
805 __copy_move_backward_a(
806 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
807 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
808 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
809
1b4a6975
PE
810 /**
811 * @brief Copies the range [first,last) into result.
5b9daa7e 812 * @ingroup mutating_algorithms
93c66bc6
BK
813 * @param __first A bidirectional iterator.
814 * @param __last A bidirectional iterator.
815 * @param __result A bidirectional iterator.
1b4a6975
PE
816 * @return result - (first - last)
817 *
818 * The function has the same effect as copy, but starts at the end of the
819 * range and works its way to the start, returning the start of the result.
820 * This inline function will boil down to a call to @c memmove whenever
821 * possible. Failing that, if random access iterators are passed, then the
822 * loop count will be known (and therefore a candidate for compiler
823 * optimizations such as unrolling).
119dbb1f 824 *
9a7fb488 825 * Result may not be in the range (first,last]. Use copy instead. Note
119dbb1f 826 * that the start of the output range may overlap [first,last).
1b4a6975 827 */
f0112db9 828 template<typename _BI1, typename _BI2>
3a66e68a 829 _GLIBCXX20_CONSTEXPR
02d92e3b
SW
830 inline _BI2
831 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
832 {
833 // concept requirements
3d7c150e
BK
834 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
835 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
836 __glibcxx_function_requires(_ConvertibleConcept<
02d92e3b 837 typename iterator_traits<_BI1>::value_type,
4d16bdbb 838 typename iterator_traits<_BI2>::value_type>)
84a9d3b6 839 __glibcxx_requires_can_decrement_range(__first, __last, __result);
02d92e3b 840
6004c17b 841 return std::__copy_move_backward_a<__is_move_iterator<_BI1>::__value>
84a9d3b6 842 (std::__miter_base(__first), std::__miter_base(__last), __result);
02d92e3b
SW
843 }
844
734f5023 845#if __cplusplus >= 201103L
3c167a8b
PC
846 /**
847 * @brief Moves the range [first,last) into result.
5b9daa7e 848 * @ingroup mutating_algorithms
93c66bc6
BK
849 * @param __first A bidirectional iterator.
850 * @param __last A bidirectional iterator.
851 * @param __result A bidirectional iterator.
3c167a8b
PC
852 * @return result - (first - last)
853 *
854 * The function has the same effect as move, but starts at the end of the
855 * range and works its way to the start, returning the start of the result.
856 * This inline function will boil down to a call to @c memmove whenever
857 * possible. Failing that, if random access iterators are passed, then the
858 * loop count will be known (and therefore a candidate for compiler
859 * optimizations such as unrolling).
860 *
848ca96f 861 * Result may not be in the range (first,last]. Use move instead. Note
3c167a8b
PC
862 * that the start of the output range may overlap [first,last).
863 */
864 template<typename _BI1, typename _BI2>
3a66e68a 865 _GLIBCXX20_CONSTEXPR
3c167a8b
PC
866 inline _BI2
867 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
868 {
869 // concept requirements
870 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
871 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
872 __glibcxx_function_requires(_ConvertibleConcept<
873 typename iterator_traits<_BI1>::value_type,
874 typename iterator_traits<_BI2>::value_type>)
84a9d3b6 875 __glibcxx_requires_can_decrement_range(__first, __last, __result);
3c167a8b 876
6004c17b
FD
877 return std::__copy_move_backward_a<true>(std::__miter_base(__first),
878 std::__miter_base(__last),
879 __result);
3c167a8b 880 }
245a5fe5
PC
881
882#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
883#else
884#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
3c167a8b 885#endif
e9e90c1f 886
394033f8 887 template<typename _ForwardIterator, typename _Tp>
3a66e68a 888 _GLIBCXX20_CONSTEXPR
394033f8
PC
889 inline typename
890 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
6004c17b
FD
891 __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
892 const _Tp& __value)
6e539e23 893 {
394033f8
PC
894 for (; __first != __last; ++__first)
895 *__first = __value;
896 }
e5795ce4 897
08addde6 898 template<typename _ForwardIterator, typename _Tp>
3a66e68a 899 _GLIBCXX20_CONSTEXPR
394033f8
PC
900 inline typename
901 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
6004c17b
FD
902 __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
903 const _Tp& __value)
02d92e3b 904 {
b14f95a8 905 const _Tp __tmp = __value;
394033f8 906 for (; __first != __last; ++__first)
b14f95a8 907 *__first = __tmp;
02d92e3b
SW
908 }
909
d22a3166 910 // Specialization: for char types we can use memset.
394033f8
PC
911 template<typename _Tp>
912 inline typename
913 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
6004c17b 914 __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c)
b14f95a8
PC
915 {
916 const _Tp __tmp = __c;
5d946f42
JW
917 if (const size_t __len = __last - __first)
918 __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
b14f95a8 919 }
725dc051 920
6004c17b
FD
921 template<typename _Ite, typename _Cont, typename _Tp>
922 _GLIBCXX20_CONSTEXPR
923 inline void
924 __fill_a1(::__gnu_cxx::__normal_iterator<_Ite, _Cont> __first,
925 ::__gnu_cxx::__normal_iterator<_Ite, _Cont> __last,
926 const _Tp& __value)
927 { std::__fill_a1(__first.base(), __last.base(), __value); }
928
929 template<typename _Tp, typename _VTp>
930 void
931 __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
932 const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
933 const _VTp&);
934
935 template<typename _FIte, typename _Tp>
936 _GLIBCXX20_CONSTEXPR
937 inline void
938 __fill_a(_FIte __first, _FIte __last, const _Tp& __value)
939 { std::__fill_a1(__first, __last, __value); }
940
941 template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
942 void
943 __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
944 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
945 const _Tp&);
946
e9e90c1f
PC
947 /**
948 * @brief Fills the range [first,last) with copies of value.
5b9daa7e 949 * @ingroup mutating_algorithms
93c66bc6
BK
950 * @param __first A forward iterator.
951 * @param __last A forward iterator.
952 * @param __value A reference-to-const of arbitrary type.
e9e90c1f
PC
953 * @return Nothing.
954 *
955 * This function fills a range with copies of the same value. For char
956 * types filling contiguous areas of memory, this becomes an inline call
957 * to @c memset or @c wmemset.
958 */
959 template<typename _ForwardIterator, typename _Tp>
3a66e68a 960 _GLIBCXX20_CONSTEXPR
e9e90c1f
PC
961 inline void
962 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
963 {
964 // concept requirements
965 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
966 _ForwardIterator>)
967 __glibcxx_requires_valid_range(__first, __last);
968
6004c17b 969 std::__fill_a(__first, __last, __value);
e9e90c1f
PC
970 }
971
846541dd
JW
972 // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
973 inline _GLIBCXX_CONSTEXPR int
974 __size_to_integer(int __n) { return __n; }
975 inline _GLIBCXX_CONSTEXPR unsigned
976 __size_to_integer(unsigned __n) { return __n; }
977 inline _GLIBCXX_CONSTEXPR long
978 __size_to_integer(long __n) { return __n; }
979 inline _GLIBCXX_CONSTEXPR unsigned long
980 __size_to_integer(unsigned long __n) { return __n; }
981 inline _GLIBCXX_CONSTEXPR long long
982 __size_to_integer(long long __n) { return __n; }
983 inline _GLIBCXX_CONSTEXPR unsigned long long
984 __size_to_integer(unsigned long long __n) { return __n; }
985
986#if defined(__GLIBCXX_TYPE_INT_N_0)
987 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
988 __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
989 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_0
990 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
991#endif
992#if defined(__GLIBCXX_TYPE_INT_N_1)
993 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
994 __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
995 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_1
996 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
997#endif
998#if defined(__GLIBCXX_TYPE_INT_N_2)
999 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
1000 __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
1001 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_2
1002 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
1003#endif
1004#if defined(__GLIBCXX_TYPE_INT_N_3)
1005 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_3
1006 __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
1007 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
1008 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
1009#endif
1010
1011 inline _GLIBCXX_CONSTEXPR long long
1012 __size_to_integer(float __n) { return __n; }
1013 inline _GLIBCXX_CONSTEXPR long long
1014 __size_to_integer(double __n) { return __n; }
1015 inline _GLIBCXX_CONSTEXPR long long
1016 __size_to_integer(long double __n) { return __n; }
1017#if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
1018 inline _GLIBCXX_CONSTEXPR long long
1019 __size_to_integer(__float128 __n) { return __n; }
1020#endif
1021
6e539e23 1022 template<typename _OutputIterator, typename _Size, typename _Tp>
3a66e68a 1023 _GLIBCXX20_CONSTEXPR
394033f8
PC
1024 inline typename
1025 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
6004c17b 1026 __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
02d92e3b 1027 {
846541dd 1028 for (; __n > 0; --__n, (void) ++__first)
394033f8
PC
1029 *__first = __value;
1030 return __first;
02d92e3b
SW
1031 }
1032
394033f8 1033 template<typename _OutputIterator, typename _Size, typename _Tp>
3a66e68a 1034 _GLIBCXX20_CONSTEXPR
394033f8
PC
1035 inline typename
1036 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
6004c17b 1037 __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
02d92e3b 1038 {
b14f95a8 1039 const _Tp __tmp = __value;
846541dd 1040 for (; __n > 0; --__n, (void) ++__first)
b14f95a8 1041 *__first = __tmp;
394033f8 1042 return __first;
02d92e3b
SW
1043 }
1044
6004c17b
FD
1045 template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
1046 typename _Tp>
1047 ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
1048 __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
1049 _Size __n, const _Tp& __value,
1050 std::input_iterator_tag);
1051
1052 template<typename _OutputIterator, typename _Size, typename _Tp>
3a66e68a 1053 _GLIBCXX20_CONSTEXPR
6004c17b
FD
1054 inline _OutputIterator
1055 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1056 std::output_iterator_tag)
1057 {
1058#if __cplusplus >= 201103L
1059 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1060#endif
1061 return __fill_n_a1(__first, __n, __value);
1062 }
1063
1064 template<typename _OutputIterator, typename _Size, typename _Tp>
1065 _GLIBCXX20_CONSTEXPR
1066 inline _OutputIterator
1067 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1068 std::input_iterator_tag)
1069 {
1070#if __cplusplus >= 201103L
1071 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1072#endif
1073 return __fill_n_a1(__first, __n, __value);
1074 }
1075
1076 template<typename _OutputIterator, typename _Size, typename _Tp>
1077 _GLIBCXX20_CONSTEXPR
1078 inline _OutputIterator
1079 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1080 std::random_access_iterator_tag)
e9e90c1f 1081 {
6004c17b
FD
1082#if __cplusplus >= 201103L
1083 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1084#endif
1085 if (__n <= 0)
1086 return __first;
1087
1088 __glibcxx_requires_can_increment(__first, __n);
1089
1090 std::__fill_a(__first, __first + __n, __value);
e9e90c1f
PC
1091 return __first + __n;
1092 }
1093
e9e90c1f
PC
1094 /**
1095 * @brief Fills the range [first,first+n) with copies of value.
5b9daa7e 1096 * @ingroup mutating_algorithms
93c66bc6
BK
1097 * @param __first An output iterator.
1098 * @param __n The count of copies to perform.
1099 * @param __value A reference-to-const of arbitrary type.
e9e90c1f
PC
1100 * @return The iterator at first+n.
1101 *
1102 * This function fills a range with copies of the same value. For char
1103 * types filling contiguous areas of memory, this becomes an inline call
846541dd 1104 * to @c memset or @c wmemset.
82ab4b64 1105 *
846541dd 1106 * If @p __n is negative, the function does nothing.
e9e90c1f 1107 */
846541dd
JW
1108 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1109 // DR 865. More algorithms that throw away information
1110 // DR 426. search_n(), fill_n(), and generate_n() with negative n
d22a3166 1111 template<typename _OI, typename _Size, typename _Tp>
3a66e68a 1112 _GLIBCXX20_CONSTEXPR
d22a3166
PC
1113 inline _OI
1114 fill_n(_OI __first, _Size __n, const _Tp& __value)
e9e90c1f
PC
1115 {
1116 // concept requirements
d22a3166 1117 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
e9e90c1f 1118
6004c17b
FD
1119 return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
1120 std::__iterator_category(__first));
e9e90c1f 1121 }
02d92e3b 1122
d22a3166
PC
1123 template<bool _BoolType>
1124 struct __equal
1125 {
1126 template<typename _II1, typename _II2>
3a66e68a 1127 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
1128 static bool
1129 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1130 {
d67be443 1131 for (; __first1 != __last1; ++__first1, (void) ++__first2)
d22a3166
PC
1132 if (!(*__first1 == *__first2))
1133 return false;
1134 return true;
1135 }
1136 };
1137
1138 template<>
1139 struct __equal<true>
1140 {
1141 template<typename _Tp>
3a66e68a 1142 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
1143 static bool
1144 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
1145 {
12fc64ac 1146 if (const size_t __len = (__last1 - __first1))
3a66e68a 1147 return !std::__memcmp(__first1, __first2, __len);
12fc64ac 1148 return true;
d22a3166
PC
1149 }
1150 };
1151
6004c17b
FD
1152 template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
1153 typename __gnu_cxx::__enable_if<
1154 __is_random_access_iter<_II>::__value, bool>::__type
1155 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1156 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1157 _II);
1158
1159 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1160 typename _Tp2, typename _Ref2, typename _Ptr2>
1161 bool
1162 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1163 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1164 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1165
1166 template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
1167 typename __gnu_cxx::__enable_if<
1168 __is_random_access_iter<_II>::__value, bool>::__type
1169 __equal_aux1(_II, _II,
1170 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
1171
d22a3166 1172 template<typename _II1, typename _II2>
3a66e68a 1173 _GLIBCXX20_CONSTEXPR
d22a3166 1174 inline bool
6004c17b 1175 __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
d22a3166
PC
1176 {
1177 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1178 typedef typename iterator_traits<_II2>::value_type _ValueType2;
92b2342a
EW
1179 const bool __simple = ((__is_integer<_ValueType1>::__value
1180 || __is_pointer<_ValueType1>::__value)
e5795ce4
FD
1181 && __is_pointer<_II1>::__value
1182 && __is_pointer<_II2>::__value
d22a3166
PC
1183 && __are_same<_ValueType1, _ValueType2>::__value);
1184
1185 return std::__equal<__simple>::equal(__first1, __last1, __first2);
1186 }
1187
6004c17b
FD
1188 template<typename _II1, typename _II2>
1189 _GLIBCXX20_CONSTEXPR
1190 inline bool
1191 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
1192 {
1193 return std::__equal_aux1(std::__niter_base(__first1),
1194 std::__niter_base(__last1),
1195 std::__niter_base(__first2));
1196 }
1197
1198 template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
1199 bool
1200 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1201 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1202 _II2);
1203
1204 template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
1205 bool
1206 __equal_aux(_II1, _II1,
1207 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1208
1209 template<typename _II1, typename _Seq1, typename _Cat1,
1210 typename _II2, typename _Seq2, typename _Cat2>
1211 bool
1212 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1213 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1214 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1215
c2ba9709
JS
1216 template<typename, typename>
1217 struct __lc_rai
1218 {
1219 template<typename _II1, typename _II2>
3a66e68a 1220 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
1221 static _II1
1222 __newlast1(_II1, _II1 __last1, _II2, _II2)
1223 { return __last1; }
c2ba9709
JS
1224
1225 template<typename _II>
3a66e68a 1226 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
1227 static bool
1228 __cnd2(_II __first, _II __last)
1229 { return __first != __last; }
c2ba9709
JS
1230 };
1231
1232 template<>
1233 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
1234 {
1235 template<typename _RAI1, typename _RAI2>
3a66e68a 1236 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
1237 static _RAI1
1238 __newlast1(_RAI1 __first1, _RAI1 __last1,
c2ba9709 1239 _RAI2 __first2, _RAI2 __last2)
e5795ce4 1240 {
c2ba9709
JS
1241 const typename iterator_traits<_RAI1>::difference_type
1242 __diff1 = __last1 - __first1;
1243 const typename iterator_traits<_RAI2>::difference_type
1244 __diff2 = __last2 - __first2;
1245 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
1246 }
1247
1248 template<typename _RAI>
3a66e68a 1249 static _GLIBCXX20_CONSTEXPR bool
e5795ce4
FD
1250 __cnd2(_RAI, _RAI)
1251 { return true; }
c2ba9709
JS
1252 };
1253
ea89b248 1254 template<typename _II1, typename _II2, typename _Compare>
3a66e68a 1255 _GLIBCXX20_CONSTEXPR
ea89b248
FD
1256 bool
1257 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
1258 _II2 __first2, _II2 __last2,
1259 _Compare __comp)
1260 {
1261 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1262 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1263 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1264
1265 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1266 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
f970a17d 1267 ++__first1, (void)++__first2)
ea89b248
FD
1268 {
1269 if (__comp(__first1, __first2))
1270 return true;
1271 if (__comp(__first2, __first1))
1272 return false;
1273 }
1274 return __first1 == __last1 && __first2 != __last2;
1275 }
1276
478b2b9c
PC
1277 template<bool _BoolType>
1278 struct __lexicographical_compare
1279 {
1280 template<typename _II1, typename _II2>
3a66e68a 1281 _GLIBCXX20_CONSTEXPR
e5795ce4 1282 static bool __lc(_II1, _II1, _II2, _II2);
478b2b9c
PC
1283 };
1284
ba940b7c
PC
1285 template<bool _BoolType>
1286 template<typename _II1, typename _II2>
3a66e68a 1287 _GLIBCXX20_CONSTEXPR
ba940b7c
PC
1288 bool
1289 __lexicographical_compare<_BoolType>::
1290 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1291 {
3bd2644c
FD
1292 return std::__lexicographical_compare_impl(__first1, __last1,
1293 __first2, __last2,
1294 __gnu_cxx::__ops::__iter_less_iter());
ba940b7c
PC
1295 }
1296
478b2b9c
PC
1297 template<>
1298 struct __lexicographical_compare<true>
1299 {
1300 template<typename _Tp, typename _Up>
3a66e68a 1301 _GLIBCXX20_CONSTEXPR
e5795ce4
FD
1302 static bool
1303 __lc(const _Tp* __first1, const _Tp* __last1,
478b2b9c
PC
1304 const _Up* __first2, const _Up* __last2)
1305 {
1306 const size_t __len1 = __last1 - __first1;
1307 const size_t __len2 = __last2 - __first2;
12fc64ac 1308 if (const size_t __len = std::min(__len1, __len2))
3a66e68a 1309 if (int __result = std::__memcmp(__first1, __first2, __len))
12fc64ac 1310 return __result < 0;
6759edde 1311 return __len1 < __len2;
478b2b9c
PC
1312 }
1313 };
1314
1315 template<typename _II1, typename _II2>
3a66e68a 1316 _GLIBCXX20_CONSTEXPR
478b2b9c
PC
1317 inline bool
1318 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
1319 _II2 __first2, _II2 __last2)
1320 {
1321 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1322 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1323 const bool __simple =
1324 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
1325 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
1326 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
1327 && __is_pointer<_II1>::__value
1328 && __is_pointer<_II2>::__value);
1329
1330 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
1331 __first2, __last2);
1332 }
1333
ea89b248 1334 template<typename _ForwardIterator, typename _Tp, typename _Compare>
3a66e68a 1335 _GLIBCXX20_CONSTEXPR
247d8075 1336 _ForwardIterator
ea89b248
FD
1337 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1338 const _Tp& __val, _Compare __comp)
247d8075 1339 {
247d8075
PC
1340 typedef typename iterator_traits<_ForwardIterator>::difference_type
1341 _DistanceType;
1342
247d8075 1343 _DistanceType __len = std::distance(__first, __last);
247d8075
PC
1344
1345 while (__len > 0)
1346 {
1ed78d6c
CY
1347 _DistanceType __half = __len >> 1;
1348 _ForwardIterator __middle = __first;
247d8075 1349 std::advance(__middle, __half);
ea89b248 1350 if (__comp(__middle, __val))
247d8075
PC
1351 {
1352 __first = __middle;
1353 ++__first;
1354 __len = __len - __half - 1;
1355 }
1356 else
1357 __len = __half;
1358 }
1359 return __first;
1360 }
1361
ea89b248
FD
1362 /**
1363 * @brief Finds the first position in which @a val could be inserted
1364 * without changing the ordering.
1365 * @param __first An iterator.
1366 * @param __last Another iterator.
1367 * @param __val The search term.
1368 * @return An iterator pointing to the first element <em>not less
e5795ce4 1369 * than</em> @a val, or end() if every element is less than
ea89b248
FD
1370 * @a val.
1371 * @ingroup binary_search_algorithms
1372 */
1373 template<typename _ForwardIterator, typename _Tp>
3a66e68a 1374 _GLIBCXX20_CONSTEXPR
3bd2644c 1375 inline _ForwardIterator
ea89b248
FD
1376 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1377 const _Tp& __val)
1378 {
1379 // concept requirements
1380 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1381 __glibcxx_function_requires(_LessThanOpConcept<
1382 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1383 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1384
1385 return std::__lower_bound(__first, __last, __val,
1386 __gnu_cxx::__ops::__iter_less_val());
1387 }
1388
247d8075
PC
1389 /// This is a helper function for the sort routines and for random.tcc.
1390 // Precondition: __n > 0.
cf48c255 1391 inline _GLIBCXX_CONSTEXPR int
247d8075 1392 __lg(int __n)
eca5f925 1393 { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
247d8075 1394
cf48c255 1395 inline _GLIBCXX_CONSTEXPR unsigned
f84ca6e7 1396 __lg(unsigned __n)
eca5f925 1397 { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
f84ca6e7 1398
cf48c255 1399 inline _GLIBCXX_CONSTEXPR long
247d8075 1400 __lg(long __n)
eca5f925 1401 { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
247d8075 1402
cf48c255 1403 inline _GLIBCXX_CONSTEXPR unsigned long
f84ca6e7 1404 __lg(unsigned long __n)
eca5f925 1405 { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
f84ca6e7 1406
cf48c255 1407 inline _GLIBCXX_CONSTEXPR long long
247d8075 1408 __lg(long long __n)
eca5f925 1409 { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
247d8075 1410
cf48c255 1411 inline _GLIBCXX_CONSTEXPR unsigned long long
f84ca6e7 1412 __lg(unsigned long long __n)
eca5f925 1413 { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
f84ca6e7 1414
12ffa228 1415_GLIBCXX_BEGIN_NAMESPACE_ALGO
c2ba9709 1416
1b4a6975
PE
1417 /**
1418 * @brief Tests a range for element-wise equality.
5b9daa7e 1419 * @ingroup non_mutating_algorithms
93c66bc6
BK
1420 * @param __first1 An input iterator.
1421 * @param __last1 An input iterator.
1422 * @param __first2 An input iterator.
1b4a6975
PE
1423 * @return A boolean true or false.
1424 *
1425 * This compares the elements of two ranges using @c == and returns true or
1426 * false depending on whether all of the corresponding elements of the
1427 * ranges are equal.
1428 */
d22a3166 1429 template<typename _II1, typename _II2>
3a66e68a 1430 _GLIBCXX20_CONSTEXPR
02d92e3b 1431 inline bool
d22a3166 1432 equal(_II1 __first1, _II1 __last1, _II2 __first2)
02d92e3b
SW
1433 {
1434 // concept requirements
d22a3166
PC
1435 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1436 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
3d7c150e 1437 __glibcxx_function_requires(_EqualOpConcept<
d22a3166
PC
1438 typename iterator_traits<_II1>::value_type,
1439 typename iterator_traits<_II2>::value_type>)
315aadc8 1440 __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
d22a3166 1441
6004c17b 1442 return std::__equal_aux(__first1, __last1, __first2);
02d92e3b
SW
1443 }
1444
1b4a6975
PE
1445 /**
1446 * @brief Tests a range for element-wise equality.
5b9daa7e 1447 * @ingroup non_mutating_algorithms
93c66bc6
BK
1448 * @param __first1 An input iterator.
1449 * @param __last1 An input iterator.
1450 * @param __first2 An input iterator.
1451 * @param __binary_pred A binary predicate @link functors
c2ba9709
JS
1452 * functor@endlink.
1453 * @return A boolean true or false.
1b4a6975
PE
1454 *
1455 * This compares the elements of two ranges using the binary_pred
1456 * parameter, and returns true or
1457 * false depending on whether all of the corresponding elements of the
1458 * ranges are equal.
1459 */
c2ba9709 1460 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
3a66e68a 1461 _GLIBCXX20_CONSTEXPR
02d92e3b 1462 inline bool
c2ba9709
JS
1463 equal(_IIter1 __first1, _IIter1 __last1,
1464 _IIter2 __first2, _BinaryPredicate __binary_pred)
02d92e3b
SW
1465 {
1466 // concept requirements
c2ba9709
JS
1467 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1468 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
285b36d6 1469 __glibcxx_requires_valid_range(__first1, __last1);
02d92e3b 1470
f970a17d 1471 for (; __first1 != __last1; ++__first1, (void)++__first2)
dded9d2c 1472 if (!bool(__binary_pred(*__first1, *__first2)))
02d92e3b 1473 return false;
725dc051 1474 return true;
02d92e3b
SW
1475 }
1476
23b49089
JW
1477#if __cplusplus >= 201103L
1478 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1479 template<typename _II1, typename _II2>
3a66e68a 1480 _GLIBCXX20_CONSTEXPR
23b49089
JW
1481 inline bool
1482 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1483 {
1484 using _RATag = random_access_iterator_tag;
1485 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1486 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1487 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1488 if (_RAIters())
1489 {
1490 auto __d1 = std::distance(__first1, __last1);
1491 auto __d2 = std::distance(__first2, __last2);
1492 if (__d1 != __d2)
1493 return false;
1494 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1495 }
1496
1497 for (; __first1 != __last1 && __first2 != __last2;
1498 ++__first1, (void)++__first2)
1499 if (!(*__first1 == *__first2))
1500 return false;
1501 return __first1 == __last1 && __first2 == __last2;
1502 }
1503
1504 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1505 template<typename _II1, typename _II2, typename _BinaryPredicate>
3a66e68a 1506 _GLIBCXX20_CONSTEXPR
23b49089
JW
1507 inline bool
1508 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
1509 _BinaryPredicate __binary_pred)
1510 {
1511 using _RATag = random_access_iterator_tag;
1512 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1513 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1514 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1515 if (_RAIters())
1516 {
1517 auto __d1 = std::distance(__first1, __last1);
1518 auto __d2 = std::distance(__first2, __last2);
1519 if (__d1 != __d2)
1520 return false;
1521 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1522 __binary_pred);
1523 }
1524
1525 for (; __first1 != __last1 && __first2 != __last2;
1526 ++__first1, (void)++__first2)
1527 if (!bool(__binary_pred(*__first1, *__first2)))
1528 return false;
1529 return __first1 == __last1 && __first2 == __last2;
1530 }
1531#endif // C++11
1532
f7fbb003 1533#if __cplusplus > 201103L
a15f7cb8
ESR
1534
1535#define __cpp_lib_robust_nonmodifying_seq_ops 201304
1536
f7fbb003
JW
1537 /**
1538 * @brief Tests a range for element-wise equality.
1539 * @ingroup non_mutating_algorithms
1540 * @param __first1 An input iterator.
1541 * @param __last1 An input iterator.
1542 * @param __first2 An input iterator.
1543 * @param __last2 An input iterator.
1544 * @return A boolean true or false.
1545 *
1546 * This compares the elements of two ranges using @c == and returns true or
1547 * false depending on whether all of the corresponding elements of the
1548 * ranges are equal.
1549 */
1550 template<typename _II1, typename _II2>
3a66e68a 1551 _GLIBCXX20_CONSTEXPR
f7fbb003
JW
1552 inline bool
1553 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1554 {
1555 // concept requirements
1556 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1557 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1558 __glibcxx_function_requires(_EqualOpConcept<
1559 typename iterator_traits<_II1>::value_type,
1560 typename iterator_traits<_II2>::value_type>)
1561 __glibcxx_requires_valid_range(__first1, __last1);
1562 __glibcxx_requires_valid_range(__first2, __last2);
1563
23b49089 1564 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
f7fbb003
JW
1565 }
1566
1567 /**
1568 * @brief Tests a range for element-wise equality.
1569 * @ingroup non_mutating_algorithms
1570 * @param __first1 An input iterator.
1571 * @param __last1 An input iterator.
1572 * @param __first2 An input iterator.
1573 * @param __last2 An input iterator.
1574 * @param __binary_pred A binary predicate @link functors
1575 * functor@endlink.
1576 * @return A boolean true or false.
1577 *
1578 * This compares the elements of two ranges using the binary_pred
1579 * parameter, and returns true or
1580 * false depending on whether all of the corresponding elements of the
1581 * ranges are equal.
1582 */
1583 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
3a66e68a 1584 _GLIBCXX20_CONSTEXPR
f7fbb003
JW
1585 inline bool
1586 equal(_IIter1 __first1, _IIter1 __last1,
1587 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1588 {
1589 // concept requirements
1590 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1591 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1592 __glibcxx_requires_valid_range(__first1, __last1);
1593 __glibcxx_requires_valid_range(__first2, __last2);
1594
23b49089
JW
1595 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
1596 __binary_pred);
f7fbb003 1597 }
23b49089 1598#endif // C++14
f7fbb003 1599
1b4a6975 1600 /**
2a60a9f6 1601 * @brief Performs @b dictionary comparison on ranges.
5b9daa7e 1602 * @ingroup sorting_algorithms
93c66bc6
BK
1603 * @param __first1 An input iterator.
1604 * @param __last1 An input iterator.
1605 * @param __first2 An input iterator.
1606 * @param __last2 An input iterator.
1b4a6975
PE
1607 * @return A boolean true or false.
1608 *
2a60a9f6 1609 * <em>Returns true if the sequence of elements defined by the range
1b4a6975 1610 * [first1,last1) is lexicographically less than the sequence of elements
2a60a9f6 1611 * defined by the range [first2,last2). Returns false otherwise.</em>
1b4a6975
PE
1612 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1613 * then this is an inline call to @c memcmp.
1614 */
c2fe93f7 1615 template<typename _II1, typename _II2>
3a66e68a 1616 _GLIBCXX20_CONSTEXPR
de03de64
PC
1617 inline bool
1618 lexicographical_compare(_II1 __first1, _II1 __last1,
c2fe93f7 1619 _II2 __first2, _II2 __last2)
02d92e3b 1620 {
650dc14a 1621#ifdef _GLIBCXX_CONCEPT_CHECKS
02d92e3b 1622 // concept requirements
c2fe93f7
PC
1623 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1624 typedef typename iterator_traits<_II2>::value_type _ValueType2;
650dc14a 1625#endif
c2fe93f7
PC
1626 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1627 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1628 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1629 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
285b36d6
BK
1630 __glibcxx_requires_valid_range(__first1, __last1);
1631 __glibcxx_requires_valid_range(__first2, __last2);
02d92e3b 1632
6989b63f
PC
1633 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1634 std::__niter_base(__last1),
1635 std::__niter_base(__first2),
1636 std::__niter_base(__last2));
de03de64 1637 }
4f39bf5c 1638
1b4a6975 1639 /**
2a60a9f6 1640 * @brief Performs @b dictionary comparison on ranges.
5b9daa7e 1641 * @ingroup sorting_algorithms
93c66bc6
BK
1642 * @param __first1 An input iterator.
1643 * @param __last1 An input iterator.
1644 * @param __first2 An input iterator.
1645 * @param __last2 An input iterator.
1646 * @param __comp A @link comparison_functors comparison functor@endlink.
1b4a6975
PE
1647 * @return A boolean true or false.
1648 *
c2fe93f7 1649 * The same as the four-parameter @c lexicographical_compare, but uses the
1b4a6975
PE
1650 * comp parameter instead of @c <.
1651 */
c2fe93f7 1652 template<typename _II1, typename _II2, typename _Compare>
3a66e68a 1653 _GLIBCXX20_CONSTEXPR
3bd2644c 1654 inline bool
c2fe93f7
PC
1655 lexicographical_compare(_II1 __first1, _II1 __last1,
1656 _II2 __first2, _II2 __last2, _Compare __comp)
02d92e3b
SW
1657 {
1658 // concept requirements
c2fe93f7
PC
1659 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1660 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
285b36d6
BK
1661 __glibcxx_requires_valid_range(__first1, __last1);
1662 __glibcxx_requires_valid_range(__first2, __last2);
02d92e3b 1663
ea89b248
FD
1664 return std::__lexicographical_compare_impl
1665 (__first1, __last1, __first2, __last2,
1666 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1667 }
1668
f1355c8d
JW
1669#if __cpp_lib_three_way_comparison
1670#if __cpp_lib_concepts
1671 // Iter points to a contiguous range of unsigned narrow character type
1672 // or std::byte, suitable for comparison by memcmp.
1673 template<typename _Iter>
1674 concept __is_byte_iter = contiguous_iterator<_Iter>
1675 && __is_byte<iter_value_t<_Iter>>::__value != 0
1676 && !__gnu_cxx::__numeric_traits<iter_value_t<_Iter>>::__is_signed;
1677
1678 // Return a struct with two members, initialized to the smaller of x and y
1679 // (or x if they compare equal) and the result of the comparison x <=> y.
1680 template<typename _Tp>
1681 constexpr auto
1682 __min_cmp(_Tp __x, _Tp __y)
1683 {
1684 struct _Res {
1685 _Tp _M_min;
1686 decltype(__x <=> __y) _M_cmp;
1687 };
1688 auto __c = __x <=> __y;
1689 if (__c > 0)
1690 return _Res{__y, __c};
1691 return _Res{__x, __c};
1692 }
1693#endif
1694
1695 /**
1696 * @brief Performs dictionary comparison on ranges.
1697 * @ingroup sorting_algorithms
1698 * @param __first1 An input iterator.
1699 * @param __last1 An input iterator.
1700 * @param __first2 An input iterator.
1701 * @param __last2 An input iterator.
1702 * @param __comp A @link comparison_functors comparison functor@endlink.
1703 * @return The comparison category that `__comp(*__first1, *__first2)`
1704 * returns.
1705 */
1706 template<typename _InputIter1, typename _InputIter2, typename _Comp>
1707 constexpr auto
1708 lexicographical_compare_three_way(_InputIter1 __first1,
1709 _InputIter1 __last1,
1710 _InputIter2 __first2,
1711 _InputIter2 __last2,
1712 _Comp __comp)
1713 -> decltype(__comp(*__first1, *__first2))
1714 {
1715 // concept requirements
1716 __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
1717 __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
1718 __glibcxx_requires_valid_range(__first1, __last1);
1719 __glibcxx_requires_valid_range(__first2, __last2);
1720
1721#if __cpp_lib_concepts && __cpp_lib_is_constant_evaluated
1722 using _Cat = decltype(__comp(*__first1, *__first2));
1723 static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
1724
1725 if (!std::is_constant_evaluated())
1726 if constexpr (same_as<_Comp, __detail::_Synth3way>
1727 || same_as<_Comp, compare_three_way>)
1728 if constexpr (__is_byte_iter<_InputIter1>)
1729 if constexpr (__is_byte_iter<_InputIter2>)
1730 {
1731 const auto [__len, __lencmp]
1732 = std::__min_cmp(__last1 - __first1, __last2 - __first2);
1733 if (__len)
1734 {
1735 const auto __c
1736 = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0;
1737 if (__c != 0)
1738 return __c;
1739 }
1740 return __lencmp;
1741 }
1742#endif // concepts && is_constant_evaluated
1743 while (__first1 != __last1 && __first2 != __last2)
1744 {
1745 if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
1746 return __cmp;
1747 ++__first1;
1748 ++__first2;
1749 }
1750 return __first1 != __last1 ? strong_ordering::greater
1751 : __first2 != __last2 ? strong_ordering::less : strong_ordering::equal;
1752 }
1753
b4e70137 1754#if __cpp_lib_concepts
f1355c8d
JW
1755 template<typename _InputIter1, typename _InputIter2>
1756 constexpr auto
1757 lexicographical_compare_three_way(_InputIter1 __first1,
1758 _InputIter1 __last1,
1759 _InputIter2 __first2,
1760 _InputIter2 __last2)
1761 {
1762 return std::lexicographical_compare_three_way(__first1, __last1,
1763 __first2, __last2,
1764 compare_three_way{});
1765 }
b4e70137 1766#endif // concepts
f1355c8d
JW
1767#endif // three_way_comparison
1768
ea89b248
FD
1769 template<typename _InputIterator1, typename _InputIterator2,
1770 typename _BinaryPredicate>
3a66e68a 1771 _GLIBCXX20_CONSTEXPR
ea89b248
FD
1772 pair<_InputIterator1, _InputIterator2>
1773 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1774 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1775 {
1776 while (__first1 != __last1 && __binary_pred(__first1, __first2))
e5795ce4 1777 {
ea89b248
FD
1778 ++__first1;
1779 ++__first2;
e5795ce4 1780 }
ea89b248 1781 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
02d92e3b
SW
1782 }
1783
c2ba9709
JS
1784 /**
1785 * @brief Finds the places in ranges which don't match.
5b9daa7e 1786 * @ingroup non_mutating_algorithms
93c66bc6
BK
1787 * @param __first1 An input iterator.
1788 * @param __last1 An input iterator.
1789 * @param __first2 An input iterator.
c2ba9709
JS
1790 * @return A pair of iterators pointing to the first mismatch.
1791 *
1792 * This compares the elements of two ranges using @c == and returns a pair
1793 * of iterators. The first iterator points into the first range, the
1794 * second iterator points into the second range, and the elements pointed
1795 * to by the iterators are not equal.
1796 */
1797 template<typename _InputIterator1, typename _InputIterator2>
3a66e68a 1798 _GLIBCXX20_CONSTEXPR
3bd2644c 1799 inline pair<_InputIterator1, _InputIterator2>
c2ba9709
JS
1800 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1801 _InputIterator2 __first2)
1802 {
1803 // concept requirements
1804 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1805 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1806 __glibcxx_function_requires(_EqualOpConcept<
1807 typename iterator_traits<_InputIterator1>::value_type,
1808 typename iterator_traits<_InputIterator2>::value_type>)
1809 __glibcxx_requires_valid_range(__first1, __last1);
02d92e3b 1810
ea89b248
FD
1811 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1812 __gnu_cxx::__ops::__iter_equal_to_iter());
c2ba9709 1813 }
285b36d6 1814
c2ba9709
JS
1815 /**
1816 * @brief Finds the places in ranges which don't match.
5b9daa7e 1817 * @ingroup non_mutating_algorithms
93c66bc6
BK
1818 * @param __first1 An input iterator.
1819 * @param __last1 An input iterator.
1820 * @param __first2 An input iterator.
1821 * @param __binary_pred A binary predicate @link functors
c2ba9709
JS
1822 * functor@endlink.
1823 * @return A pair of iterators pointing to the first mismatch.
1824 *
1825 * This compares the elements of two ranges using the binary_pred
1826 * parameter, and returns a pair
1827 * of iterators. The first iterator points into the first range, the
1828 * second iterator points into the second range, and the elements pointed
1829 * to by the iterators are not equal.
1830 */
1831 template<typename _InputIterator1, typename _InputIterator2,
1832 typename _BinaryPredicate>
3a66e68a 1833 _GLIBCXX20_CONSTEXPR
3bd2644c 1834 inline pair<_InputIterator1, _InputIterator2>
c2ba9709
JS
1835 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1836 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1837 {
1838 // concept requirements
1839 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1840 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1841 __glibcxx_requires_valid_range(__first1, __last1);
02d92e3b 1842
ea89b248
FD
1843 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1844 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1845 }
1846
1847#if __cplusplus > 201103L
1848
1849 template<typename _InputIterator1, typename _InputIterator2,
1850 typename _BinaryPredicate>
3a66e68a 1851 _GLIBCXX20_CONSTEXPR
ea89b248
FD
1852 pair<_InputIterator1, _InputIterator2>
1853 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1854 _InputIterator2 __first2, _InputIterator2 __last2,
1855 _BinaryPredicate __binary_pred)
1856 {
1857 while (__first1 != __last1 && __first2 != __last2
1858 && __binary_pred(__first1, __first2))
e5795ce4 1859 {
c2ba9709
JS
1860 ++__first1;
1861 ++__first2;
e5795ce4 1862 }
c2ba9709
JS
1863 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1864 }
1865
f7fbb003
JW
1866 /**
1867 * @brief Finds the places in ranges which don't match.
1868 * @ingroup non_mutating_algorithms
1869 * @param __first1 An input iterator.
1870 * @param __last1 An input iterator.
1871 * @param __first2 An input iterator.
1872 * @param __last2 An input iterator.
1873 * @return A pair of iterators pointing to the first mismatch.
1874 *
1875 * This compares the elements of two ranges using @c == and returns a pair
1876 * of iterators. The first iterator points into the first range, the
1877 * second iterator points into the second range, and the elements pointed
1878 * to by the iterators are not equal.
1879 */
1880 template<typename _InputIterator1, typename _InputIterator2>
3a66e68a 1881 _GLIBCXX20_CONSTEXPR
3bd2644c 1882 inline pair<_InputIterator1, _InputIterator2>
f7fbb003
JW
1883 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1884 _InputIterator2 __first2, _InputIterator2 __last2)
1885 {
1886 // concept requirements
1887 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1888 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1889 __glibcxx_function_requires(_EqualOpConcept<
1890 typename iterator_traits<_InputIterator1>::value_type,
1891 typename iterator_traits<_InputIterator2>::value_type>)
1892 __glibcxx_requires_valid_range(__first1, __last1);
1893 __glibcxx_requires_valid_range(__first2, __last2);
1894
ea89b248
FD
1895 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1896 __gnu_cxx::__ops::__iter_equal_to_iter());
f7fbb003
JW
1897 }
1898
1899 /**
1900 * @brief Finds the places in ranges which don't match.
1901 * @ingroup non_mutating_algorithms
1902 * @param __first1 An input iterator.
1903 * @param __last1 An input iterator.
1904 * @param __first2 An input iterator.
1905 * @param __last2 An input iterator.
1906 * @param __binary_pred A binary predicate @link functors
1907 * functor@endlink.
1908 * @return A pair of iterators pointing to the first mismatch.
1909 *
1910 * This compares the elements of two ranges using the binary_pred
1911 * parameter, and returns a pair
1912 * of iterators. The first iterator points into the first range, the
1913 * second iterator points into the second range, and the elements pointed
1914 * to by the iterators are not equal.
1915 */
1916 template<typename _InputIterator1, typename _InputIterator2,
1917 typename _BinaryPredicate>
3a66e68a 1918 _GLIBCXX20_CONSTEXPR
3bd2644c 1919 inline pair<_InputIterator1, _InputIterator2>
f7fbb003
JW
1920 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1921 _InputIterator2 __first2, _InputIterator2 __last2,
1922 _BinaryPredicate __binary_pred)
1923 {
1924 // concept requirements
1925 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1926 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1927 __glibcxx_requires_valid_range(__first1, __last1);
1928 __glibcxx_requires_valid_range(__first2, __last2);
1929
ea89b248
FD
1930 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1931 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
f7fbb003
JW
1932 }
1933#endif
1934
12ffa228 1935_GLIBCXX_END_NAMESPACE_ALGO
4a15d842 1936_GLIBCXX_END_NAMESPACE_VERSION
12ffa228 1937} // namespace std
c2ba9709
JS
1938
1939// NB: This file is included within many other C++ includes, as a way
1940// of getting the base algorithms. So, make sure that parallel bits
e5795ce4 1941// come in too if requested.
c2ba9709 1942#ifdef _GLIBCXX_PARALLEL
c2ba9709
JS
1943# include <parallel/algobase.h>
1944#endif
725dc051 1945
ed6814f7 1946#endif
This page took 2.003824 seconds and 5 git commands to generate.