libstdc++
stl_algobase.h
Go to the documentation of this file.
1// Core algorithmic facilities -*- C++ -*-
2
3// Copyright (C) 2001-2023 Free Software Foundation, Inc.
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
8// Free Software Foundation; either version 3, or (at your option)
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
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.
19
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/>.
24
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
51/** @file bits/stl_algobase.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
54 */
55
56#ifndef _STL_ALGOBASE_H
57#define _STL_ALGOBASE_H 1
58
59#include <bits/c++config.h>
60#include <bits/functexcept.h>
62#include <ext/type_traits.h>
63#include <ext/numeric_traits.h>
64#include <bits/stl_pair.h>
67#include <bits/stl_iterator.h>
68#include <bits/concept_check.h>
69#include <debug/debug.h>
70#include <bits/move.h> // For std::swap
71#include <bits/predefined_ops.h>
72#if __cplusplus >= 201103L
73# include <type_traits>
74#endif
75#if __cplusplus >= 201402L
76# include <bit> // std::__bit_width
77#endif
78#if __cplusplus >= 202002L
79# include <compare>
80#endif
81
82namespace std _GLIBCXX_VISIBILITY(default)
83{
84_GLIBCXX_BEGIN_NAMESPACE_VERSION
85
86 /*
87 * A constexpr wrapper for __builtin_memcmp.
88 * @param __num The number of elements of type _Tp (not bytes).
89 */
90 template<typename _Tp, typename _Up>
91 _GLIBCXX14_CONSTEXPR
92 inline int
93 __memcmp(const _Tp* __first1, const _Up* __first2, size_t __num)
94 {
95#if __cplusplus >= 201103L
96 static_assert(sizeof(_Tp) == sizeof(_Up), "can be compared with memcmp");
97#endif
98#ifdef __cpp_lib_is_constant_evaluated
100 {
101 for(; __num > 0; ++__first1, ++__first2, --__num)
102 if (*__first1 != *__first2)
103 return *__first1 < *__first2 ? -1 : 1;
104 return 0;
105 }
106 else
107#endif
108 return __builtin_memcmp(__first1, __first2, sizeof(_Tp) * __num);
109 }
110
111#if __cplusplus < 201103L
112 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
113 // nutshell, we are partially implementing the resolution of DR 187,
114 // when it's safe, i.e., the value_types are equal.
115 template<bool _BoolType>
116 struct __iter_swap
117 {
118 template<typename _ForwardIterator1, typename _ForwardIterator2>
119 static void
120 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
121 {
122 typedef typename iterator_traits<_ForwardIterator1>::value_type
123 _ValueType1;
124 _ValueType1 __tmp = *__a;
125 *__a = *__b;
126 *__b = __tmp;
127 }
128 };
129
130 template<>
131 struct __iter_swap<true>
132 {
133 template<typename _ForwardIterator1, typename _ForwardIterator2>
134 static void
135 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
136 {
137 swap(*__a, *__b);
138 }
139 };
140#endif // C++03
141
142 /**
143 * @brief Swaps the contents of two iterators.
144 * @ingroup mutating_algorithms
145 * @param __a An iterator.
146 * @param __b Another iterator.
147 * @return Nothing.
148 *
149 * This function swaps the values pointed to by two iterators, not the
150 * iterators themselves.
151 */
152 template<typename _ForwardIterator1, typename _ForwardIterator2>
153 _GLIBCXX20_CONSTEXPR
154 inline void
155 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
156 {
157 // concept requirements
158 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
159 _ForwardIterator1>)
160 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
161 _ForwardIterator2>)
162
163#if __cplusplus < 201103L
165 _ValueType1;
167 _ValueType2;
168
169 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
170 _ValueType2>)
171 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
172 _ValueType1>)
173
175 _ReferenceType1;
177 _ReferenceType2;
178 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
179 && __are_same<_ValueType1&, _ReferenceType1>::__value
180 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
181 iter_swap(__a, __b);
182#else
183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
184 // 187. iter_swap underspecified
185 swap(*__a, *__b);
186#endif
187 }
188
189 /**
190 * @brief Swap the elements of two sequences.
191 * @ingroup mutating_algorithms
192 * @param __first1 A forward iterator.
193 * @param __last1 A forward iterator.
194 * @param __first2 A forward iterator.
195 * @return An iterator equal to @p first2+(last1-first1).
196 *
197 * Swaps each element in the range @p [first1,last1) with the
198 * corresponding element in the range @p [first2,(last1-first1)).
199 * The ranges must not overlap.
200 */
201 template<typename _ForwardIterator1, typename _ForwardIterator2>
202 _GLIBCXX20_CONSTEXPR
203 _ForwardIterator2
204 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
205 _ForwardIterator2 __first2)
206 {
207 // concept requirements
208 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
209 _ForwardIterator1>)
210 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
211 _ForwardIterator2>)
212 __glibcxx_requires_valid_range(__first1, __last1);
213
214 for (; __first1 != __last1; ++__first1, (void)++__first2)
215 std::iter_swap(__first1, __first2);
216 return __first2;
217 }
218
219 /**
220 * @brief This does what you think it does.
221 * @ingroup sorting_algorithms
222 * @param __a A thing of arbitrary type.
223 * @param __b Another thing of arbitrary type.
224 * @return The lesser of the parameters.
225 *
226 * This is the simple classic generic implementation. It will work on
227 * temporary expressions, since they are only evaluated once, unlike a
228 * preprocessor macro.
229 */
230 template<typename _Tp>
231 _GLIBCXX14_CONSTEXPR
232 inline const _Tp&
233 min(const _Tp& __a, const _Tp& __b)
234 {
235 // concept requirements
236 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
237 //return __b < __a ? __b : __a;
238 if (__b < __a)
239 return __b;
240 return __a;
241 }
242
243 /**
244 * @brief This does what you think it does.
245 * @ingroup sorting_algorithms
246 * @param __a A thing of arbitrary type.
247 * @param __b Another thing of arbitrary type.
248 * @return The greater of the parameters.
249 *
250 * This is the simple classic generic implementation. It will work on
251 * temporary expressions, since they are only evaluated once, unlike a
252 * preprocessor macro.
253 */
254 template<typename _Tp>
255 _GLIBCXX14_CONSTEXPR
256 inline const _Tp&
257 max(const _Tp& __a, const _Tp& __b)
258 {
259 // concept requirements
260 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
261 //return __a < __b ? __b : __a;
262 if (__a < __b)
263 return __b;
264 return __a;
265 }
266
267 /**
268 * @brief This does what you think it does.
269 * @ingroup sorting_algorithms
270 * @param __a A thing of arbitrary type.
271 * @param __b Another thing of arbitrary type.
272 * @param __comp A @link comparison_functors comparison functor@endlink.
273 * @return The lesser of the parameters.
274 *
275 * This will work on temporary expressions, since they are only evaluated
276 * once, unlike a preprocessor macro.
277 */
278 template<typename _Tp, typename _Compare>
279 _GLIBCXX14_CONSTEXPR
280 inline const _Tp&
281 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
282 {
283 //return __comp(__b, __a) ? __b : __a;
284 if (__comp(__b, __a))
285 return __b;
286 return __a;
287 }
288
289 /**
290 * @brief This does what you think it does.
291 * @ingroup sorting_algorithms
292 * @param __a A thing of arbitrary type.
293 * @param __b Another thing of arbitrary type.
294 * @param __comp A @link comparison_functors comparison functor@endlink.
295 * @return The greater of the parameters.
296 *
297 * This will work on temporary expressions, since they are only evaluated
298 * once, unlike a preprocessor macro.
299 */
300 template<typename _Tp, typename _Compare>
301 _GLIBCXX14_CONSTEXPR
302 inline const _Tp&
303 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
304 {
305 //return __comp(__a, __b) ? __b : __a;
306 if (__comp(__a, __b))
307 return __b;
308 return __a;
309 }
310
311 // Fallback implementation of the function in bits/stl_iterator.h used to
312 // remove the __normal_iterator wrapper. See copy, fill, ...
313 template<typename _Iterator>
314 _GLIBCXX20_CONSTEXPR
315 inline _Iterator
316 __niter_base(_Iterator __it)
318 { return __it; }
319
320 template<typename _Ite, typename _Seq>
321 _Ite
322 __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
324
325 // Reverse the __niter_base transformation to get a
326 // __normal_iterator back again (this assumes that __normal_iterator
327 // is only used to wrap random access iterators, like pointers).
328 template<typename _From, typename _To>
329 _GLIBCXX20_CONSTEXPR
330 inline _From
331 __niter_wrap(_From __from, _To __res)
332 { return __from + (__res - std::__niter_base(__from)); }
333
334 // No need to wrap, iterator already has the right type.
335 template<typename _Iterator>
336 _GLIBCXX20_CONSTEXPR
337 inline _Iterator
338 __niter_wrap(const _Iterator&, _Iterator __res)
339 { return __res; }
340
341 // All of these auxiliary structs serve two purposes. (1) Replace
342 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
343 // because the input and output ranges are permitted to overlap.)
344 // (2) If we're using random access iterators, then write the loop as
345 // a for loop with an explicit count.
346
347 template<bool _IsMove, bool _IsSimple, typename _Category>
348 struct __copy_move
349 {
350 template<typename _II, typename _OI>
351 _GLIBCXX20_CONSTEXPR
352 static _OI
353 __copy_m(_II __first, _II __last, _OI __result)
354 {
355 for (; __first != __last; ++__result, (void)++__first)
356 *__result = *__first;
357 return __result;
358 }
359 };
360
361#if __cplusplus >= 201103L
362 template<typename _Category>
363 struct __copy_move<true, false, _Category>
364 {
365 template<typename _II, typename _OI>
366 _GLIBCXX20_CONSTEXPR
367 static _OI
368 __copy_m(_II __first, _II __last, _OI __result)
369 {
370 for (; __first != __last; ++__result, (void)++__first)
371 *__result = std::move(*__first);
372 return __result;
373 }
374 };
375#endif
376
377 template<>
378 struct __copy_move<false, false, random_access_iterator_tag>
379 {
380 template<typename _II, typename _OI>
381 _GLIBCXX20_CONSTEXPR
382 static _OI
383 __copy_m(_II __first, _II __last, _OI __result)
384 {
385 typedef typename iterator_traits<_II>::difference_type _Distance;
386 for(_Distance __n = __last - __first; __n > 0; --__n)
387 {
388 *__result = *__first;
389 ++__first;
390 ++__result;
391 }
392 return __result;
393 }
394 };
395
396#if __cplusplus >= 201103L
397 template<>
398 struct __copy_move<true, false, random_access_iterator_tag>
399 {
400 template<typename _II, typename _OI>
401 _GLIBCXX20_CONSTEXPR
402 static _OI
403 __copy_m(_II __first, _II __last, _OI __result)
404 {
405 typedef typename iterator_traits<_II>::difference_type _Distance;
406 for(_Distance __n = __last - __first; __n > 0; --__n)
407 {
408 *__result = std::move(*__first);
409 ++__first;
410 ++__result;
411 }
412 return __result;
413 }
414 };
415#endif
416
417 template<bool _IsMove>
418 struct __copy_move<_IsMove, true, random_access_iterator_tag>
419 {
420 template<typename _Tp>
421 _GLIBCXX20_CONSTEXPR
422 static _Tp*
423 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
424 {
425#if __cplusplus >= 201103L
426 using __assignable = __conditional_t<_IsMove,
427 is_move_assignable<_Tp>,
428 is_copy_assignable<_Tp>>;
429 // trivial types can have deleted assignment
430 static_assert( __assignable::value, "type must be assignable" );
431#endif
432 const ptrdiff_t _Num = __last - __first;
433 if (_Num)
434 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
435 return __result + _Num;
436 }
437 };
438
439_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
440
441 template<typename _Tp, typename _Ref, typename _Ptr>
442 struct _Deque_iterator;
443
444 struct _Bit_iterator;
445
446_GLIBCXX_END_NAMESPACE_CONTAINER
447
448#if _GLIBCXX_HOSTED
449 // Helpers for streambuf iterators (either istream or ostream).
450 // NB: avoid including <iosfwd>, relatively large.
451 template<typename _CharT>
452 struct char_traits;
453
454 template<typename _CharT, typename _Traits>
455 class istreambuf_iterator;
456
457 template<typename _CharT, typename _Traits>
458 class ostreambuf_iterator;
459
460 template<bool _IsMove, typename _CharT>
461 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
462 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
463 __copy_move_a2(_CharT*, _CharT*,
464 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
465
466 template<bool _IsMove, typename _CharT>
467 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
468 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
469 __copy_move_a2(const _CharT*, const _CharT*,
470 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
471
472 template<bool _IsMove, typename _CharT>
473 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
474 _CharT*>::__type
475 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
476 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
477
478 template<bool _IsMove, typename _CharT>
479 typename __gnu_cxx::__enable_if<
480 __is_char<_CharT>::__value,
481 _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> >::__type
482 __copy_move_a2(
483 istreambuf_iterator<_CharT, char_traits<_CharT> >,
484 istreambuf_iterator<_CharT, char_traits<_CharT> >,
485 _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*>);
486#endif // HOSTED
487
488 template<bool _IsMove, typename _II, typename _OI>
489 _GLIBCXX20_CONSTEXPR
490 inline _OI
491 __copy_move_a2(_II __first, _II __last, _OI __result)
492 {
493 typedef typename iterator_traits<_II>::iterator_category _Category;
494#ifdef __cpp_lib_is_constant_evaluated
496 return std::__copy_move<_IsMove, false, _Category>::
497 __copy_m(__first, __last, __result);
498#endif
499 return std::__copy_move<_IsMove, __memcpyable<_OI, _II>::__value,
500 _Category>::__copy_m(__first, __last, __result);
501 }
502
503 template<bool _IsMove,
504 typename _Tp, typename _Ref, typename _Ptr, typename _OI>
505 _OI
506 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
507 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
508 _OI);
509
510 template<bool _IsMove,
511 typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
512 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
513 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
514 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
515 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
516
517 template<bool _IsMove, typename _II, typename _Tp>
518 typename __gnu_cxx::__enable_if<
519 __is_random_access_iter<_II>::__value,
520 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
521 __copy_move_a1(_II, _II, _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
522
523 template<bool _IsMove, typename _II, typename _OI>
524 _GLIBCXX20_CONSTEXPR
525 inline _OI
526 __copy_move_a1(_II __first, _II __last, _OI __result)
527 { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
528
529 template<bool _IsMove, typename _II, typename _OI>
530 _GLIBCXX20_CONSTEXPR
531 inline _OI
532 __copy_move_a(_II __first, _II __last, _OI __result)
533 {
534 return std::__niter_wrap(__result,
535 std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
536 std::__niter_base(__last),
537 std::__niter_base(__result)));
538 }
539
540 template<bool _IsMove,
541 typename _Ite, typename _Seq, typename _Cat, typename _OI>
542 _OI
543 __copy_move_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
544 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
545 _OI);
546
547 template<bool _IsMove,
548 typename _II, typename _Ite, typename _Seq, typename _Cat>
550 __copy_move_a(_II, _II,
551 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
552
553 template<bool _IsMove,
554 typename _IIte, typename _ISeq, typename _ICat,
555 typename _OIte, typename _OSeq, typename _OCat>
557 __copy_move_a(const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
558 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
559 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
560
561 template<typename _InputIterator, typename _Size, typename _OutputIterator>
562 _GLIBCXX20_CONSTEXPR
563 _OutputIterator
564 __copy_n_a(_InputIterator __first, _Size __n, _OutputIterator __result,
565 bool)
566 {
567 if (__n > 0)
568 {
569 while (true)
570 {
571 *__result = *__first;
572 ++__result;
573 if (--__n > 0)
574 ++__first;
575 else
576 break;
577 }
578 }
579 return __result;
580 }
581
582#if _GLIBCXX_HOSTED
583 template<typename _CharT, typename _Size>
584 typename __gnu_cxx::__enable_if<
585 __is_char<_CharT>::__value, _CharT*>::__type
586 __copy_n_a(istreambuf_iterator<_CharT, char_traits<_CharT> >,
587 _Size, _CharT*, bool);
588
589 template<typename _CharT, typename _Size>
590 typename __gnu_cxx::__enable_if<
591 __is_char<_CharT>::__value,
592 _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> >::__type
593 __copy_n_a(istreambuf_iterator<_CharT, char_traits<_CharT> >, _Size,
594 _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*>,
595 bool);
596#endif
597
598 /**
599 * @brief Copies the range [first,last) into result.
600 * @ingroup mutating_algorithms
601 * @param __first An input iterator.
602 * @param __last An input iterator.
603 * @param __result An output iterator.
604 * @return result + (last - first)
605 *
606 * This inline function will boil down to a call to @c memmove whenever
607 * possible. Failing that, if random access iterators are passed, then the
608 * loop count will be known (and therefore a candidate for compiler
609 * optimizations such as unrolling). Result may not be contained within
610 * [first,last); the copy_backward function should be used instead.
611 *
612 * Note that the end of the output range is permitted to be contained
613 * within [first,last).
614 */
615 template<typename _II, typename _OI>
616 _GLIBCXX20_CONSTEXPR
617 inline _OI
618 copy(_II __first, _II __last, _OI __result)
619 {
620 // concept requirements
621 __glibcxx_function_requires(_InputIteratorConcept<_II>)
622 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
624 __glibcxx_requires_can_increment_range(__first, __last, __result);
625
626 return std::__copy_move_a<__is_move_iterator<_II>::__value>
627 (std::__miter_base(__first), std::__miter_base(__last), __result);
628 }
629
630#if __cplusplus >= 201103L
631 /**
632 * @brief Moves the range [first,last) into result.
633 * @ingroup mutating_algorithms
634 * @param __first An input iterator.
635 * @param __last An input iterator.
636 * @param __result An output iterator.
637 * @return result + (last - first)
638 *
639 * This inline function will boil down to a call to @c memmove whenever
640 * possible. Failing that, if random access iterators are passed, then the
641 * loop count will be known (and therefore a candidate for compiler
642 * optimizations such as unrolling). Result may not be contained within
643 * [first,last); the move_backward function should be used instead.
644 *
645 * Note that the end of the output range is permitted to be contained
646 * within [first,last).
647 */
648 template<typename _II, typename _OI>
649 _GLIBCXX20_CONSTEXPR
650 inline _OI
651 move(_II __first, _II __last, _OI __result)
652 {
653 // concept requirements
654 __glibcxx_function_requires(_InputIteratorConcept<_II>)
655 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
657 __glibcxx_requires_can_increment_range(__first, __last, __result);
658
659 return std::__copy_move_a<true>(std::__miter_base(__first),
660 std::__miter_base(__last), __result);
661 }
662
663#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
664#else
665#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
666#endif
667
668 template<bool _IsMove, bool _IsSimple, typename _Category>
669 struct __copy_move_backward
670 {
671 template<typename _BI1, typename _BI2>
672 _GLIBCXX20_CONSTEXPR
673 static _BI2
674 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
675 {
676 while (__first != __last)
677 *--__result = *--__last;
678 return __result;
679 }
680 };
681
682#if __cplusplus >= 201103L
683 template<typename _Category>
684 struct __copy_move_backward<true, false, _Category>
685 {
686 template<typename _BI1, typename _BI2>
687 _GLIBCXX20_CONSTEXPR
688 static _BI2
689 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
690 {
691 while (__first != __last)
692 *--__result = std::move(*--__last);
693 return __result;
694 }
695 };
696#endif
697
698 template<>
699 struct __copy_move_backward<false, false, random_access_iterator_tag>
700 {
701 template<typename _BI1, typename _BI2>
702 _GLIBCXX20_CONSTEXPR
703 static _BI2
704 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
705 {
706 typename iterator_traits<_BI1>::difference_type
707 __n = __last - __first;
708 for (; __n > 0; --__n)
709 *--__result = *--__last;
710 return __result;
711 }
712 };
713
714#if __cplusplus >= 201103L
715 template<>
716 struct __copy_move_backward<true, false, random_access_iterator_tag>
717 {
718 template<typename _BI1, typename _BI2>
719 _GLIBCXX20_CONSTEXPR
720 static _BI2
721 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
722 {
723 typename iterator_traits<_BI1>::difference_type
724 __n = __last - __first;
725 for (; __n > 0; --__n)
726 *--__result = std::move(*--__last);
727 return __result;
728 }
729 };
730#endif
731
732 template<bool _IsMove>
733 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
734 {
735 template<typename _Tp>
736 _GLIBCXX20_CONSTEXPR
737 static _Tp*
738 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
739 {
740#if __cplusplus >= 201103L
741 using __assignable = __conditional_t<_IsMove,
742 is_move_assignable<_Tp>,
743 is_copy_assignable<_Tp>>;
744 // trivial types can have deleted assignment
745 static_assert( __assignable::value, "type must be assignable" );
746#endif
747 const ptrdiff_t _Num = __last - __first;
748 if (_Num)
749 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
750 return __result - _Num;
751 }
752 };
753
754 template<bool _IsMove, typename _BI1, typename _BI2>
755 _GLIBCXX20_CONSTEXPR
756 inline _BI2
757 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
758 {
759 typedef typename iterator_traits<_BI1>::iterator_category _Category;
760#ifdef __cpp_lib_is_constant_evaluated
762 return std::__copy_move_backward<_IsMove, false, _Category>::
763 __copy_move_b(__first, __last, __result);
764#endif
765 return std::__copy_move_backward<_IsMove,
766 __memcpyable<_BI2, _BI1>::__value,
767 _Category>::__copy_move_b(__first,
768 __last,
769 __result);
770 }
771
772 template<bool _IsMove, typename _BI1, typename _BI2>
773 _GLIBCXX20_CONSTEXPR
774 inline _BI2
775 __copy_move_backward_a1(_BI1 __first, _BI1 __last, _BI2 __result)
776 { return std::__copy_move_backward_a2<_IsMove>(__first, __last, __result); }
777
778 template<bool _IsMove,
779 typename _Tp, typename _Ref, typename _Ptr, typename _OI>
780 _OI
781 __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
782 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
783 _OI);
784
785 template<bool _IsMove,
786 typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
787 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
788 __copy_move_backward_a1(
789 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
790 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
791 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
792
793 template<bool _IsMove, typename _II, typename _Tp>
794 typename __gnu_cxx::__enable_if<
795 __is_random_access_iter<_II>::__value,
796 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
797 __copy_move_backward_a1(_II, _II,
798 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
799
800 template<bool _IsMove, typename _II, typename _OI>
801 _GLIBCXX20_CONSTEXPR
802 inline _OI
803 __copy_move_backward_a(_II __first, _II __last, _OI __result)
804 {
805 return std::__niter_wrap(__result,
806 std::__copy_move_backward_a1<_IsMove>
807 (std::__niter_base(__first), std::__niter_base(__last),
808 std::__niter_base(__result)));
809 }
810
811 template<bool _IsMove,
812 typename _Ite, typename _Seq, typename _Cat, typename _OI>
813 _OI
814 __copy_move_backward_a(
815 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
816 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
817 _OI);
818
819 template<bool _IsMove,
820 typename _II, typename _Ite, typename _Seq, typename _Cat>
822 __copy_move_backward_a(_II, _II,
823 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
824
825 template<bool _IsMove,
826 typename _IIte, typename _ISeq, typename _ICat,
827 typename _OIte, typename _OSeq, typename _OCat>
829 __copy_move_backward_a(
830 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
831 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
832 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
833
834 /**
835 * @brief Copies the range [first,last) into result.
836 * @ingroup mutating_algorithms
837 * @param __first A bidirectional iterator.
838 * @param __last A bidirectional iterator.
839 * @param __result A bidirectional iterator.
840 * @return result - (last - first)
841 *
842 * The function has the same effect as copy, but starts at the end of the
843 * range and works its way to the start, returning the start of the result.
844 * This inline function will boil down to a call to @c memmove whenever
845 * possible. Failing that, if random access iterators are passed, then the
846 * loop count will be known (and therefore a candidate for compiler
847 * optimizations such as unrolling).
848 *
849 * Result may not be in the range (first,last]. Use copy instead. Note
850 * that the start of the output range may overlap [first,last).
851 */
852 template<typename _BI1, typename _BI2>
853 _GLIBCXX20_CONSTEXPR
854 inline _BI2
855 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
856 {
857 // concept requirements
858 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
859 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
860 __glibcxx_function_requires(_OutputIteratorConcept<_BI2,
862 __glibcxx_requires_can_decrement_range(__first, __last, __result);
863
864 return std::__copy_move_backward_a<__is_move_iterator<_BI1>::__value>
865 (std::__miter_base(__first), std::__miter_base(__last), __result);
866 }
867
868#if __cplusplus >= 201103L
869 /**
870 * @brief Moves the range [first,last) into result.
871 * @ingroup mutating_algorithms
872 * @param __first A bidirectional iterator.
873 * @param __last A bidirectional iterator.
874 * @param __result A bidirectional iterator.
875 * @return result - (last - first)
876 *
877 * The function has the same effect as move, but starts at the end of the
878 * range and works its way to the start, returning the start of the result.
879 * This inline function will boil down to a call to @c memmove whenever
880 * possible. Failing that, if random access iterators are passed, then the
881 * loop count will be known (and therefore a candidate for compiler
882 * optimizations such as unrolling).
883 *
884 * Result may not be in the range (first,last]. Use move instead. Note
885 * that the start of the output range may overlap [first,last).
886 */
887 template<typename _BI1, typename _BI2>
888 _GLIBCXX20_CONSTEXPR
889 inline _BI2
890 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
891 {
892 // concept requirements
893 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
894 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
895 __glibcxx_function_requires(_OutputIteratorConcept<_BI2,
897 __glibcxx_requires_can_decrement_range(__first, __last, __result);
898
899 return std::__copy_move_backward_a<true>(std::__miter_base(__first),
900 std::__miter_base(__last),
901 __result);
902 }
903
904#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
905#else
906#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
907#endif
908
909 template<typename _ForwardIterator, typename _Tp>
910 _GLIBCXX20_CONSTEXPR
911 inline typename
912 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
913 __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
914 const _Tp& __value)
915 {
916 for (; __first != __last; ++__first)
917 *__first = __value;
918 }
919
920 template<typename _ForwardIterator, typename _Tp>
921 _GLIBCXX20_CONSTEXPR
922 inline typename
923 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
924 __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
925 const _Tp& __value)
926 {
927 const _Tp __tmp = __value;
928 for (; __first != __last; ++__first)
929 *__first = __tmp;
930 }
931
932 // Specialization: for char types we can use memset.
933 template<typename _Tp>
934 _GLIBCXX20_CONSTEXPR
935 inline typename
936 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
937 __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c)
938 {
939 const _Tp __tmp = __c;
940#if __cpp_lib_is_constant_evaluated
942 {
943 for (; __first != __last; ++__first)
944 *__first = __tmp;
945 return;
946 }
947#endif
948 if (const size_t __len = __last - __first)
949 __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
950 }
951
952 template<typename _Ite, typename _Cont, typename _Tp>
953 _GLIBCXX20_CONSTEXPR
954 inline void
955 __fill_a1(::__gnu_cxx::__normal_iterator<_Ite, _Cont> __first,
956 ::__gnu_cxx::__normal_iterator<_Ite, _Cont> __last,
957 const _Tp& __value)
958 { std::__fill_a1(__first.base(), __last.base(), __value); }
959
960 template<typename _Tp, typename _VTp>
961 void
962 __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
963 const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
964 const _VTp&);
965
966 _GLIBCXX20_CONSTEXPR
967 void
968 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator, _GLIBCXX_STD_C::_Bit_iterator,
969 const bool&);
970
971 template<typename _FIte, typename _Tp>
972 _GLIBCXX20_CONSTEXPR
973 inline void
974 __fill_a(_FIte __first, _FIte __last, const _Tp& __value)
975 { std::__fill_a1(__first, __last, __value); }
976
977 template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
978 void
979 __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
980 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
981 const _Tp&);
982
983 /**
984 * @brief Fills the range [first,last) with copies of value.
985 * @ingroup mutating_algorithms
986 * @param __first A forward iterator.
987 * @param __last A forward iterator.
988 * @param __value A reference-to-const of arbitrary type.
989 * @return Nothing.
990 *
991 * This function fills a range with copies of the same value. For char
992 * types filling contiguous areas of memory, this becomes an inline call
993 * to @c memset or @c wmemset.
994 */
995 template<typename _ForwardIterator, typename _Tp>
996 _GLIBCXX20_CONSTEXPR
997 inline void
998 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
999 {
1000 // concept requirements
1001 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1002 _ForwardIterator>)
1003 __glibcxx_requires_valid_range(__first, __last);
1004
1005 std::__fill_a(__first, __last, __value);
1006 }
1007
1008 // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
1009 inline _GLIBCXX_CONSTEXPR int
1010 __size_to_integer(int __n) { return __n; }
1011 inline _GLIBCXX_CONSTEXPR unsigned
1012 __size_to_integer(unsigned __n) { return __n; }
1013 inline _GLIBCXX_CONSTEXPR long
1014 __size_to_integer(long __n) { return __n; }
1015 inline _GLIBCXX_CONSTEXPR unsigned long
1016 __size_to_integer(unsigned long __n) { return __n; }
1017 inline _GLIBCXX_CONSTEXPR long long
1018 __size_to_integer(long long __n) { return __n; }
1019 inline _GLIBCXX_CONSTEXPR unsigned long long
1020 __size_to_integer(unsigned long long __n) { return __n; }
1021
1022#if defined(__GLIBCXX_TYPE_INT_N_0)
1023 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
1024 __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
1025 __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_0
1026 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
1027#endif
1028#if defined(__GLIBCXX_TYPE_INT_N_1)
1029 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
1030 __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
1031 __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_1
1032 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
1033#endif
1034#if defined(__GLIBCXX_TYPE_INT_N_2)
1035 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
1036 __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
1037 __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_2
1038 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
1039#endif
1040#if defined(__GLIBCXX_TYPE_INT_N_3)
1041 __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_3
1042 __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
1043 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
1044 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
1045#endif
1046
1047 inline _GLIBCXX_CONSTEXPR long long
1048 __size_to_integer(float __n) { return (long long)__n; }
1049 inline _GLIBCXX_CONSTEXPR long long
1050 __size_to_integer(double __n) { return (long long)__n; }
1051 inline _GLIBCXX_CONSTEXPR long long
1052 __size_to_integer(long double __n) { return (long long)__n; }
1053#if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
1054 __extension__ inline _GLIBCXX_CONSTEXPR long long
1055 __size_to_integer(__float128 __n) { return (long long)__n; }
1056#endif
1057
1058 template<typename _OutputIterator, typename _Size, typename _Tp>
1059 _GLIBCXX20_CONSTEXPR
1060 inline typename
1061 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
1062 __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
1063 {
1064 for (; __n > 0; --__n, (void) ++__first)
1065 *__first = __value;
1066 return __first;
1067 }
1068
1069 template<typename _OutputIterator, typename _Size, typename _Tp>
1070 _GLIBCXX20_CONSTEXPR
1071 inline typename
1072 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
1073 __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
1074 {
1075 const _Tp __tmp = __value;
1076 for (; __n > 0; --__n, (void) ++__first)
1077 *__first = __tmp;
1078 return __first;
1079 }
1080
1081 template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
1082 typename _Tp>
1084 __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
1085 _Size __n, const _Tp& __value,
1087
1088 template<typename _OutputIterator, typename _Size, typename _Tp>
1089 _GLIBCXX20_CONSTEXPR
1090 inline _OutputIterator
1091 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1093 {
1094#if __cplusplus >= 201103L
1095 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1096#endif
1097 return __fill_n_a1(__first, __n, __value);
1098 }
1099
1100 template<typename _OutputIterator, typename _Size, typename _Tp>
1101 _GLIBCXX20_CONSTEXPR
1102 inline _OutputIterator
1103 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1105 {
1106#if __cplusplus >= 201103L
1107 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1108#endif
1109 return __fill_n_a1(__first, __n, __value);
1110 }
1111
1112 template<typename _OutputIterator, typename _Size, typename _Tp>
1113 _GLIBCXX20_CONSTEXPR
1114 inline _OutputIterator
1115 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1117 {
1118#if __cplusplus >= 201103L
1119 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1120#endif
1121 if (__n <= 0)
1122 return __first;
1123
1124 __glibcxx_requires_can_increment(__first, __n);
1125
1126 std::__fill_a(__first, __first + __n, __value);
1127 return __first + __n;
1128 }
1129
1130 /**
1131 * @brief Fills the range [first,first+n) with copies of value.
1132 * @ingroup mutating_algorithms
1133 * @param __first An output iterator.
1134 * @param __n The count of copies to perform.
1135 * @param __value A reference-to-const of arbitrary type.
1136 * @return The iterator at first+n.
1137 *
1138 * This function fills a range with copies of the same value. For char
1139 * types filling contiguous areas of memory, this becomes an inline call
1140 * to @c memset or @c wmemset.
1141 *
1142 * If @p __n is negative, the function does nothing.
1143 */
1144 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1145 // DR 865. More algorithms that throw away information
1146 // DR 426. search_n(), fill_n(), and generate_n() with negative n
1147 template<typename _OI, typename _Size, typename _Tp>
1148 _GLIBCXX20_CONSTEXPR
1149 inline _OI
1150 fill_n(_OI __first, _Size __n, const _Tp& __value)
1151 {
1152 // concept requirements
1153 __glibcxx_function_requires(_OutputIteratorConcept<_OI, const _Tp&>)
1154
1155 return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
1156 std::__iterator_category(__first));
1157 }
1158
1159 template<bool _BoolType>
1160 struct __equal
1161 {
1162 template<typename _II1, typename _II2>
1163 _GLIBCXX20_CONSTEXPR
1164 static bool
1165 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1166 {
1167 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1168 if (!(*__first1 == *__first2))
1169 return false;
1170 return true;
1171 }
1172 };
1173
1174 template<>
1175 struct __equal<true>
1176 {
1177 template<typename _Tp>
1178 _GLIBCXX20_CONSTEXPR
1179 static bool
1180 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
1181 {
1182 if (const size_t __len = (__last1 - __first1))
1183 return !std::__memcmp(__first1, __first2, __len);
1184 return true;
1185 }
1186 };
1187
1188 template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
1189 typename __gnu_cxx::__enable_if<
1190 __is_random_access_iter<_II>::__value, bool>::__type
1191 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1192 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1193 _II);
1194
1195 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1196 typename _Tp2, typename _Ref2, typename _Ptr2>
1197 bool
1198 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1199 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1200 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1201
1202 template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
1203 typename __gnu_cxx::__enable_if<
1204 __is_random_access_iter<_II>::__value, bool>::__type
1205 __equal_aux1(_II, _II,
1206 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
1207
1208 template<typename _II1, typename _II2>
1209 _GLIBCXX20_CONSTEXPR
1210 inline bool
1211 __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
1212 {
1213 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1214 const bool __simple = ((__is_integer<_ValueType1>::__value
1215 || __is_pointer<_ValueType1>::__value)
1216 && __memcmpable<_II1, _II2>::__value);
1217 return std::__equal<__simple>::equal(__first1, __last1, __first2);
1218 }
1219
1220 template<typename _II1, typename _II2>
1221 _GLIBCXX20_CONSTEXPR
1222 inline bool
1223 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
1224 {
1225 return std::__equal_aux1(std::__niter_base(__first1),
1226 std::__niter_base(__last1),
1227 std::__niter_base(__first2));
1228 }
1229
1230 template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
1231 bool
1232 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1233 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1234 _II2);
1235
1236 template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
1237 bool
1238 __equal_aux(_II1, _II1,
1239 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1240
1241 template<typename _II1, typename _Seq1, typename _Cat1,
1242 typename _II2, typename _Seq2, typename _Cat2>
1243 bool
1244 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1245 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1246 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1247
1248 template<typename, typename>
1249 struct __lc_rai
1250 {
1251 template<typename _II1, typename _II2>
1252 _GLIBCXX20_CONSTEXPR
1253 static _II1
1254 __newlast1(_II1, _II1 __last1, _II2, _II2)
1255 { return __last1; }
1256
1257 template<typename _II>
1258 _GLIBCXX20_CONSTEXPR
1259 static bool
1260 __cnd2(_II __first, _II __last)
1261 { return __first != __last; }
1262 };
1263
1264 template<>
1265 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
1266 {
1267 template<typename _RAI1, typename _RAI2>
1268 _GLIBCXX20_CONSTEXPR
1269 static _RAI1
1270 __newlast1(_RAI1 __first1, _RAI1 __last1,
1271 _RAI2 __first2, _RAI2 __last2)
1272 {
1273 const typename iterator_traits<_RAI1>::difference_type
1274 __diff1 = __last1 - __first1;
1275 const typename iterator_traits<_RAI2>::difference_type
1276 __diff2 = __last2 - __first2;
1277 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
1278 }
1279
1280 template<typename _RAI>
1281 static _GLIBCXX20_CONSTEXPR bool
1282 __cnd2(_RAI, _RAI)
1283 { return true; }
1284 };
1285
1286 template<typename _II1, typename _II2, typename _Compare>
1287 _GLIBCXX20_CONSTEXPR
1288 bool
1289 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
1290 _II2 __first2, _II2 __last2,
1291 _Compare __comp)
1292 {
1293 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1294 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1295 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1296
1297 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1298 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1299 ++__first1, (void)++__first2)
1300 {
1301 if (__comp(__first1, __first2))
1302 return true;
1303 if (__comp(__first2, __first1))
1304 return false;
1305 }
1306 return __first1 == __last1 && __first2 != __last2;
1307 }
1308
1309 template<bool _BoolType>
1310 struct __lexicographical_compare
1311 {
1312 template<typename _II1, typename _II2>
1313 _GLIBCXX20_CONSTEXPR
1314 static bool
1315 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1316 {
1317 using __gnu_cxx::__ops::__iter_less_iter;
1318 return std::__lexicographical_compare_impl(__first1, __last1,
1319 __first2, __last2,
1320 __iter_less_iter());
1321 }
1322
1323 template<typename _II1, typename _II2>
1324 _GLIBCXX20_CONSTEXPR
1325 static int
1326 __3way(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1327 {
1328 while (__first1 != __last1)
1329 {
1330 if (__first2 == __last2)
1331 return +1;
1332 if (*__first1 < *__first2)
1333 return -1;
1334 if (*__first2 < *__first1)
1335 return +1;
1336 ++__first1;
1337 ++__first2;
1338 }
1339 return int(__first2 == __last2) - 1;
1340 }
1341 };
1342
1343 template<>
1344 struct __lexicographical_compare<true>
1345 {
1346 template<typename _Tp, typename _Up>
1347 _GLIBCXX20_CONSTEXPR
1348 static bool
1349 __lc(const _Tp* __first1, const _Tp* __last1,
1350 const _Up* __first2, const _Up* __last2)
1351 { return __3way(__first1, __last1, __first2, __last2) < 0; }
1352
1353 template<typename _Tp, typename _Up>
1354 _GLIBCXX20_CONSTEXPR
1355 static ptrdiff_t
1356 __3way(const _Tp* __first1, const _Tp* __last1,
1357 const _Up* __first2, const _Up* __last2)
1358 {
1359 const size_t __len1 = __last1 - __first1;
1360 const size_t __len2 = __last2 - __first2;
1361 if (const size_t __len = std::min(__len1, __len2))
1362 if (int __result = std::__memcmp(__first1, __first2, __len))
1363 return __result;
1364 return ptrdiff_t(__len1 - __len2);
1365 }
1366 };
1367
1368 template<typename _II1, typename _II2>
1369 _GLIBCXX20_CONSTEXPR
1370 inline bool
1371 __lexicographical_compare_aux1(_II1 __first1, _II1 __last1,
1372 _II2 __first2, _II2 __last2)
1373 {
1374 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1375 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1376 const bool __simple =
1377 (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
1378 && __is_pointer<_II1>::__value
1379 && __is_pointer<_II2>::__value
1380#if __cplusplus > 201703L && __cpp_lib_concepts
1381 // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
1382 // so __is_byte<T> could be true, but we can't use memcmp with
1383 // volatile data.
1384 && !is_volatile_v<remove_reference_t<iter_reference_t<_II1>>>
1385 && !is_volatile_v<remove_reference_t<iter_reference_t<_II2>>>
1386#endif
1387 );
1388
1389 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
1390 __first2, __last2);
1391 }
1392
1393 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1394 typename _Tp2>
1395 bool
1396 __lexicographical_compare_aux1(
1397 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1398 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1399 _Tp2*, _Tp2*);
1400
1401 template<typename _Tp1,
1402 typename _Tp2, typename _Ref2, typename _Ptr2>
1403 bool
1404 __lexicographical_compare_aux1(_Tp1*, _Tp1*,
1405 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
1406 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1407
1408 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1409 typename _Tp2, typename _Ref2, typename _Ptr2>
1410 bool
1411 __lexicographical_compare_aux1(
1412 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1413 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1414 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
1415 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1416
1417 template<typename _II1, typename _II2>
1418 _GLIBCXX20_CONSTEXPR
1419 inline bool
1420 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
1421 _II2 __first2, _II2 __last2)
1422 {
1423 return std::__lexicographical_compare_aux1(std::__niter_base(__first1),
1424 std::__niter_base(__last1),
1425 std::__niter_base(__first2),
1426 std::__niter_base(__last2));
1427 }
1428
1429 template<typename _Iter1, typename _Seq1, typename _Cat1,
1430 typename _II2>
1431 bool
1432 __lexicographical_compare_aux(
1433 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1434 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1435 _II2, _II2);
1436
1437 template<typename _II1,
1438 typename _Iter2, typename _Seq2, typename _Cat2>
1439 bool
1440 __lexicographical_compare_aux(
1441 _II1, _II1,
1442 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
1443 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
1444
1445 template<typename _Iter1, typename _Seq1, typename _Cat1,
1446 typename _Iter2, typename _Seq2, typename _Cat2>
1447 bool
1448 __lexicographical_compare_aux(
1449 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1450 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1451 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
1452 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
1453
1454 template<typename _ForwardIterator, typename _Tp, typename _Compare>
1455 _GLIBCXX20_CONSTEXPR
1456 _ForwardIterator
1457 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1458 const _Tp& __val, _Compare __comp)
1459 {
1460 typedef typename iterator_traits<_ForwardIterator>::difference_type
1461 _DistanceType;
1462
1463 _DistanceType __len = std::distance(__first, __last);
1464
1465 while (__len > 0)
1466 {
1467 _DistanceType __half = __len >> 1;
1468 _ForwardIterator __middle = __first;
1469 std::advance(__middle, __half);
1470 if (__comp(__middle, __val))
1471 {
1472 __first = __middle;
1473 ++__first;
1474 __len = __len - __half - 1;
1475 }
1476 else
1477 __len = __half;
1478 }
1479 return __first;
1480 }
1481
1482 /**
1483 * @brief Finds the first position in which @a val could be inserted
1484 * without changing the ordering.
1485 * @param __first An iterator.
1486 * @param __last Another iterator.
1487 * @param __val The search term.
1488 * @return An iterator pointing to the first element <em>not less
1489 * than</em> @a val, or end() if every element is less than
1490 * @a val.
1491 * @ingroup binary_search_algorithms
1492 */
1493 template<typename _ForwardIterator, typename _Tp>
1494 _GLIBCXX20_CONSTEXPR
1495 inline _ForwardIterator
1496 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1497 const _Tp& __val)
1498 {
1499 // concept requirements
1500 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1501 __glibcxx_function_requires(_LessThanOpConcept<
1503 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1504
1505 return std::__lower_bound(__first, __last, __val,
1506 __gnu_cxx::__ops::__iter_less_val());
1507 }
1508
1509 /// This is a helper function for the sort routines and for random.tcc.
1510 // Precondition: __n > 0.
1511 template<typename _Tp>
1512 inline _GLIBCXX_CONSTEXPR _Tp
1513 __lg(_Tp __n)
1514 {
1515#if __cplusplus >= 201402L
1516 return std::__bit_width(make_unsigned_t<_Tp>(__n)) - 1;
1517#else
1518 // Use +__n so it promotes to at least int.
1519 const int __sz = sizeof(+__n);
1520 int __w = __sz * __CHAR_BIT__ - 1;
1521 if (__sz == sizeof(long long))
1522 __w -= __builtin_clzll(+__n);
1523 else if (__sz == sizeof(long))
1524 __w -= __builtin_clzl(+__n);
1525 else if (__sz == sizeof(int))
1526 __w -= __builtin_clz(+__n);
1527 return __w;
1528#endif
1529 }
1530
1531_GLIBCXX_BEGIN_NAMESPACE_ALGO
1532
1533 /**
1534 * @brief Tests a range for element-wise equality.
1535 * @ingroup non_mutating_algorithms
1536 * @param __first1 An input iterator.
1537 * @param __last1 An input iterator.
1538 * @param __first2 An input iterator.
1539 * @return A boolean true or false.
1540 *
1541 * This compares the elements of two ranges using @c == and returns true or
1542 * false depending on whether all of the corresponding elements of the
1543 * ranges are equal.
1544 */
1545 template<typename _II1, typename _II2>
1546 _GLIBCXX20_CONSTEXPR
1547 inline bool
1548 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1549 {
1550 // concept requirements
1551 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1552 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1553 __glibcxx_function_requires(_EqualOpConcept<
1556 __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
1557
1558 return std::__equal_aux(__first1, __last1, __first2);
1559 }
1560
1561 /**
1562 * @brief Tests a range for element-wise equality.
1563 * @ingroup non_mutating_algorithms
1564 * @param __first1 An input iterator.
1565 * @param __last1 An input iterator.
1566 * @param __first2 An input iterator.
1567 * @param __binary_pred A binary predicate @link functors
1568 * functor@endlink.
1569 * @return A boolean true or false.
1570 *
1571 * This compares the elements of two ranges using the binary_pred
1572 * parameter, and returns true or
1573 * false depending on whether all of the corresponding elements of the
1574 * ranges are equal.
1575 */
1576 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1577 _GLIBCXX20_CONSTEXPR
1578 inline bool
1579 equal(_IIter1 __first1, _IIter1 __last1,
1580 _IIter2 __first2, _BinaryPredicate __binary_pred)
1581 {
1582 // concept requirements
1583 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1584 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1585 __glibcxx_requires_valid_range(__first1, __last1);
1586
1587 for (; __first1 != __last1; ++__first1, (void)++__first2)
1588 if (!bool(__binary_pred(*__first1, *__first2)))
1589 return false;
1590 return true;
1591 }
1592
1593#if __cplusplus >= 201103L
1594 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1595 template<typename _II1, typename _II2>
1596 _GLIBCXX20_CONSTEXPR
1597 inline bool
1598 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1599 {
1600 using _RATag = random_access_iterator_tag;
1601 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1602 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1603 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1604 if (_RAIters())
1605 {
1606 auto __d1 = std::distance(__first1, __last1);
1607 auto __d2 = std::distance(__first2, __last2);
1608 if (__d1 != __d2)
1609 return false;
1610 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1611 }
1612
1613 for (; __first1 != __last1 && __first2 != __last2;
1614 ++__first1, (void)++__first2)
1615 if (!(*__first1 == *__first2))
1616 return false;
1617 return __first1 == __last1 && __first2 == __last2;
1618 }
1619
1620 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1621 template<typename _II1, typename _II2, typename _BinaryPredicate>
1622 _GLIBCXX20_CONSTEXPR
1623 inline bool
1624 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
1625 _BinaryPredicate __binary_pred)
1626 {
1627 using _RATag = random_access_iterator_tag;
1628 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1629 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1630 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1631 if (_RAIters())
1632 {
1633 auto __d1 = std::distance(__first1, __last1);
1634 auto __d2 = std::distance(__first2, __last2);
1635 if (__d1 != __d2)
1636 return false;
1637 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1638 __binary_pred);
1639 }
1640
1641 for (; __first1 != __last1 && __first2 != __last2;
1642 ++__first1, (void)++__first2)
1643 if (!bool(__binary_pred(*__first1, *__first2)))
1644 return false;
1645 return __first1 == __last1 && __first2 == __last2;
1646 }
1647#endif // C++11
1648
1649#if __cplusplus > 201103L
1650
1651#define __cpp_lib_robust_nonmodifying_seq_ops 201304L
1652
1653 /**
1654 * @brief Tests a range for element-wise equality.
1655 * @ingroup non_mutating_algorithms
1656 * @param __first1 An input iterator.
1657 * @param __last1 An input iterator.
1658 * @param __first2 An input iterator.
1659 * @param __last2 An input iterator.
1660 * @return A boolean true or false.
1661 *
1662 * This compares the elements of two ranges using @c == and returns true or
1663 * false depending on whether all of the corresponding elements of the
1664 * ranges are equal.
1665 */
1666 template<typename _II1, typename _II2>
1667 _GLIBCXX20_CONSTEXPR
1668 inline bool
1669 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1670 {
1671 // concept requirements
1672 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1673 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1674 __glibcxx_function_requires(_EqualOpConcept<
1677 __glibcxx_requires_valid_range(__first1, __last1);
1678 __glibcxx_requires_valid_range(__first2, __last2);
1679
1680 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
1681 }
1682
1683 /**
1684 * @brief Tests a range for element-wise equality.
1685 * @ingroup non_mutating_algorithms
1686 * @param __first1 An input iterator.
1687 * @param __last1 An input iterator.
1688 * @param __first2 An input iterator.
1689 * @param __last2 An input iterator.
1690 * @param __binary_pred A binary predicate @link functors
1691 * functor@endlink.
1692 * @return A boolean true or false.
1693 *
1694 * This compares the elements of two ranges using the binary_pred
1695 * parameter, and returns true or
1696 * false depending on whether all of the corresponding elements of the
1697 * ranges are equal.
1698 */
1699 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1700 _GLIBCXX20_CONSTEXPR
1701 inline bool
1702 equal(_IIter1 __first1, _IIter1 __last1,
1703 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1704 {
1705 // concept requirements
1706 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1707 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1708 __glibcxx_requires_valid_range(__first1, __last1);
1709 __glibcxx_requires_valid_range(__first2, __last2);
1710
1711 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
1712 __binary_pred);
1713 }
1714#endif // C++14
1715
1716 /**
1717 * @brief Performs @b dictionary comparison on ranges.
1718 * @ingroup sorting_algorithms
1719 * @param __first1 An input iterator.
1720 * @param __last1 An input iterator.
1721 * @param __first2 An input iterator.
1722 * @param __last2 An input iterator.
1723 * @return A boolean true or false.
1724 *
1725 * <em>Returns true if the sequence of elements defined by the range
1726 * [first1,last1) is lexicographically less than the sequence of elements
1727 * defined by the range [first2,last2). Returns false otherwise.</em>
1728 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1729 * then this is an inline call to @c memcmp.
1730 */
1731 template<typename _II1, typename _II2>
1732 _GLIBCXX20_CONSTEXPR
1733 inline bool
1734 lexicographical_compare(_II1 __first1, _II1 __last1,
1735 _II2 __first2, _II2 __last2)
1736 {
1737#ifdef _GLIBCXX_CONCEPT_CHECKS
1738 // concept requirements
1739 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1740 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1741#endif
1742 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1743 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1744 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1745 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1746 __glibcxx_requires_valid_range(__first1, __last1);
1747 __glibcxx_requires_valid_range(__first2, __last2);
1748
1749 return std::__lexicographical_compare_aux(__first1, __last1,
1750 __first2, __last2);
1751 }
1752
1753 /**
1754 * @brief Performs @b dictionary comparison on ranges.
1755 * @ingroup sorting_algorithms
1756 * @param __first1 An input iterator.
1757 * @param __last1 An input iterator.
1758 * @param __first2 An input iterator.
1759 * @param __last2 An input iterator.
1760 * @param __comp A @link comparison_functors comparison functor@endlink.
1761 * @return A boolean true or false.
1762 *
1763 * The same as the four-parameter @c lexicographical_compare, but uses the
1764 * comp parameter instead of @c <.
1765 */
1766 template<typename _II1, typename _II2, typename _Compare>
1767 _GLIBCXX20_CONSTEXPR
1768 inline bool
1769 lexicographical_compare(_II1 __first1, _II1 __last1,
1770 _II2 __first2, _II2 __last2, _Compare __comp)
1771 {
1772 // concept requirements
1773 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1774 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1775 __glibcxx_requires_valid_range(__first1, __last1);
1776 __glibcxx_requires_valid_range(__first2, __last2);
1777
1778 return std::__lexicographical_compare_impl
1779 (__first1, __last1, __first2, __last2,
1780 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1781 }
1782
1783#if __cpp_lib_three_way_comparison
1784 // Iter points to a contiguous range of unsigned narrow character type
1785 // or std::byte, suitable for comparison by memcmp.
1786 template<typename _Iter>
1787 concept __is_byte_iter = contiguous_iterator<_Iter>
1788 && __is_memcmp_ordered<iter_value_t<_Iter>>::__value;
1789
1790 // Return a struct with two members, initialized to the smaller of x and y
1791 // (or x if they compare equal) and the result of the comparison x <=> y.
1792 template<typename _Tp>
1793 constexpr auto
1794 __min_cmp(_Tp __x, _Tp __y)
1795 {
1796 struct _Res {
1797 _Tp _M_min;
1798 decltype(__x <=> __y) _M_cmp;
1799 };
1800 auto __c = __x <=> __y;
1801 if (__c > 0)
1802 return _Res{__y, __c};
1803 return _Res{__x, __c};
1804 }
1805
1806 /**
1807 * @brief Performs dictionary comparison on ranges.
1808 * @ingroup sorting_algorithms
1809 * @param __first1 An input iterator.
1810 * @param __last1 An input iterator.
1811 * @param __first2 An input iterator.
1812 * @param __last2 An input iterator.
1813 * @param __comp A @link comparison_functors comparison functor@endlink.
1814 * @return The comparison category that `__comp(*__first1, *__first2)`
1815 * returns.
1816 */
1817 template<typename _InputIter1, typename _InputIter2, typename _Comp>
1818 constexpr auto
1819 lexicographical_compare_three_way(_InputIter1 __first1,
1820 _InputIter1 __last1,
1821 _InputIter2 __first2,
1822 _InputIter2 __last2,
1823 _Comp __comp)
1824 -> decltype(__comp(*__first1, *__first2))
1825 {
1826 // concept requirements
1827 __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
1828 __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
1829 __glibcxx_requires_valid_range(__first1, __last1);
1830 __glibcxx_requires_valid_range(__first2, __last2);
1831
1832 using _Cat = decltype(__comp(*__first1, *__first2));
1833 static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
1834
1835 if (!std::__is_constant_evaluated())
1836 if constexpr (same_as<_Comp, __detail::_Synth3way>
1837 || same_as<_Comp, compare_three_way>)
1838 if constexpr (__is_byte_iter<_InputIter1>)
1839 if constexpr (__is_byte_iter<_InputIter2>)
1840 {
1841 const auto [__len, __lencmp] = _GLIBCXX_STD_A::
1842 __min_cmp(__last1 - __first1, __last2 - __first2);
1843 if (__len)
1844 {
1845 const auto __c
1846 = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0;
1847 if (__c != 0)
1848 return __c;
1849 }
1850 return __lencmp;
1851 }
1852
1853 while (__first1 != __last1)
1854 {
1855 if (__first2 == __last2)
1856 return strong_ordering::greater;
1857 if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
1858 return __cmp;
1859 ++__first1;
1860 ++__first2;
1861 }
1862 return (__first2 == __last2) <=> true; // See PR 94006
1863 }
1864
1865 template<typename _InputIter1, typename _InputIter2>
1866 constexpr auto
1867 lexicographical_compare_three_way(_InputIter1 __first1,
1868 _InputIter1 __last1,
1869 _InputIter2 __first2,
1870 _InputIter2 __last2)
1871 {
1872 return _GLIBCXX_STD_A::
1873 lexicographical_compare_three_way(__first1, __last1, __first2, __last2,
1874 compare_three_way{});
1875 }
1876#endif // three_way_comparison
1877
1878 template<typename _InputIterator1, typename _InputIterator2,
1879 typename _BinaryPredicate>
1880 _GLIBCXX20_CONSTEXPR
1881 pair<_InputIterator1, _InputIterator2>
1882 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1883 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1884 {
1885 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1886 {
1887 ++__first1;
1888 ++__first2;
1889 }
1890 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1891 }
1892
1893 /**
1894 * @brief Finds the places in ranges which don't match.
1895 * @ingroup non_mutating_algorithms
1896 * @param __first1 An input iterator.
1897 * @param __last1 An input iterator.
1898 * @param __first2 An input iterator.
1899 * @return A pair of iterators pointing to the first mismatch.
1900 *
1901 * This compares the elements of two ranges using @c == and returns a pair
1902 * of iterators. The first iterator points into the first range, the
1903 * second iterator points into the second range, and the elements pointed
1904 * to by the iterators are not equal.
1905 */
1906 template<typename _InputIterator1, typename _InputIterator2>
1907 _GLIBCXX20_CONSTEXPR
1908 inline pair<_InputIterator1, _InputIterator2>
1909 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1910 _InputIterator2 __first2)
1911 {
1912 // concept requirements
1913 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1914 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1915 __glibcxx_function_requires(_EqualOpConcept<
1918 __glibcxx_requires_valid_range(__first1, __last1);
1919
1920 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1921 __gnu_cxx::__ops::__iter_equal_to_iter());
1922 }
1923
1924 /**
1925 * @brief Finds the places in ranges which don't match.
1926 * @ingroup non_mutating_algorithms
1927 * @param __first1 An input iterator.
1928 * @param __last1 An input iterator.
1929 * @param __first2 An input iterator.
1930 * @param __binary_pred A binary predicate @link functors
1931 * functor@endlink.
1932 * @return A pair of iterators pointing to the first mismatch.
1933 *
1934 * This compares the elements of two ranges using the binary_pred
1935 * parameter, and returns a pair
1936 * of iterators. The first iterator points into the first range, the
1937 * second iterator points into the second range, and the elements pointed
1938 * to by the iterators are not equal.
1939 */
1940 template<typename _InputIterator1, typename _InputIterator2,
1941 typename _BinaryPredicate>
1942 _GLIBCXX20_CONSTEXPR
1943 inline pair<_InputIterator1, _InputIterator2>
1944 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1945 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1946 {
1947 // concept requirements
1948 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1949 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1950 __glibcxx_requires_valid_range(__first1, __last1);
1951
1952 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1953 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1954 }
1955
1956#if __cplusplus > 201103L
1957
1958 template<typename _InputIterator1, typename _InputIterator2,
1959 typename _BinaryPredicate>
1960 _GLIBCXX20_CONSTEXPR
1961 pair<_InputIterator1, _InputIterator2>
1962 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1963 _InputIterator2 __first2, _InputIterator2 __last2,
1964 _BinaryPredicate __binary_pred)
1965 {
1966 while (__first1 != __last1 && __first2 != __last2
1967 && __binary_pred(__first1, __first2))
1968 {
1969 ++__first1;
1970 ++__first2;
1971 }
1972 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1973 }
1974
1975 /**
1976 * @brief Finds the places in ranges which don't match.
1977 * @ingroup non_mutating_algorithms
1978 * @param __first1 An input iterator.
1979 * @param __last1 An input iterator.
1980 * @param __first2 An input iterator.
1981 * @param __last2 An input iterator.
1982 * @return A pair of iterators pointing to the first mismatch.
1983 *
1984 * This compares the elements of two ranges using @c == and returns a pair
1985 * of iterators. The first iterator points into the first range, the
1986 * second iterator points into the second range, and the elements pointed
1987 * to by the iterators are not equal.
1988 */
1989 template<typename _InputIterator1, typename _InputIterator2>
1990 _GLIBCXX20_CONSTEXPR
1991 inline pair<_InputIterator1, _InputIterator2>
1992 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1993 _InputIterator2 __first2, _InputIterator2 __last2)
1994 {
1995 // concept requirements
1996 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1997 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1998 __glibcxx_function_requires(_EqualOpConcept<
2001 __glibcxx_requires_valid_range(__first1, __last1);
2002 __glibcxx_requires_valid_range(__first2, __last2);
2003
2004 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
2005 __gnu_cxx::__ops::__iter_equal_to_iter());
2006 }
2007
2008 /**
2009 * @brief Finds the places in ranges which don't match.
2010 * @ingroup non_mutating_algorithms
2011 * @param __first1 An input iterator.
2012 * @param __last1 An input iterator.
2013 * @param __first2 An input iterator.
2014 * @param __last2 An input iterator.
2015 * @param __binary_pred A binary predicate @link functors
2016 * functor@endlink.
2017 * @return A pair of iterators pointing to the first mismatch.
2018 *
2019 * This compares the elements of two ranges using the binary_pred
2020 * parameter, and returns a pair
2021 * of iterators. The first iterator points into the first range, the
2022 * second iterator points into the second range, and the elements pointed
2023 * to by the iterators are not equal.
2024 */
2025 template<typename _InputIterator1, typename _InputIterator2,
2026 typename _BinaryPredicate>
2027 _GLIBCXX20_CONSTEXPR
2028 inline pair<_InputIterator1, _InputIterator2>
2029 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
2030 _InputIterator2 __first2, _InputIterator2 __last2,
2031 _BinaryPredicate __binary_pred)
2032 {
2033 // concept requirements
2034 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2035 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2036 __glibcxx_requires_valid_range(__first1, __last1);
2037 __glibcxx_requires_valid_range(__first2, __last2);
2038
2039 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
2040 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
2041 }
2042#endif
2043
2044_GLIBCXX_END_NAMESPACE_ALGO
2045
2046 /// This is an overload used by find algos for the Input Iterator case.
2047 template<typename _InputIterator, typename _Predicate>
2048 _GLIBCXX20_CONSTEXPR
2049 inline _InputIterator
2050 __find_if(_InputIterator __first, _InputIterator __last,
2051 _Predicate __pred, input_iterator_tag)
2052 {
2053 while (__first != __last && !__pred(__first))
2054 ++__first;
2055 return __first;
2056 }
2057
2058 /// This is an overload used by find algos for the RAI case.
2059 template<typename _RandomAccessIterator, typename _Predicate>
2060 _GLIBCXX20_CONSTEXPR
2061 _RandomAccessIterator
2062 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
2063 _Predicate __pred, random_access_iterator_tag)
2064 {
2066 __trip_count = (__last - __first) >> 2;
2067
2068 for (; __trip_count > 0; --__trip_count)
2069 {
2070 if (__pred(__first))
2071 return __first;
2072 ++__first;
2073
2074 if (__pred(__first))
2075 return __first;
2076 ++__first;
2077
2078 if (__pred(__first))
2079 return __first;
2080 ++__first;
2081
2082 if (__pred(__first))
2083 return __first;
2084 ++__first;
2085 }
2086
2087 switch (__last - __first)
2088 {
2089 case 3:
2090 if (__pred(__first))
2091 return __first;
2092 ++__first;
2093 // FALLTHRU
2094 case 2:
2095 if (__pred(__first))
2096 return __first;
2097 ++__first;
2098 // FALLTHRU
2099 case 1:
2100 if (__pred(__first))
2101 return __first;
2102 ++__first;
2103 // FALLTHRU
2104 case 0:
2105 default:
2106 return __last;
2107 }
2108 }
2109
2110 template<typename _Iterator, typename _Predicate>
2111 _GLIBCXX20_CONSTEXPR
2112 inline _Iterator
2113 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
2114 {
2115 return __find_if(__first, __last, __pred,
2116 std::__iterator_category(__first));
2117 }
2118
2119 template<typename _InputIterator, typename _Predicate>
2120 _GLIBCXX20_CONSTEXPR
2121 typename iterator_traits<_InputIterator>::difference_type
2122 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
2123 {
2124 typename iterator_traits<_InputIterator>::difference_type __n = 0;
2125 for (; __first != __last; ++__first)
2126 if (__pred(__first))
2127 ++__n;
2128 return __n;
2129 }
2130
2131 template<typename _ForwardIterator, typename _Predicate>
2132 _GLIBCXX20_CONSTEXPR
2133 _ForwardIterator
2134 __remove_if(_ForwardIterator __first, _ForwardIterator __last,
2135 _Predicate __pred)
2136 {
2137 __first = std::__find_if(__first, __last, __pred);
2138 if (__first == __last)
2139 return __first;
2140 _ForwardIterator __result = __first;
2141 ++__first;
2142 for (; __first != __last; ++__first)
2143 if (!__pred(__first))
2144 {
2145 *__result = _GLIBCXX_MOVE(*__first);
2146 ++__result;
2147 }
2148 return __result;
2149 }
2150
2151#if __cplusplus >= 201103L
2152 template<typename _ForwardIterator1, typename _ForwardIterator2,
2153 typename _BinaryPredicate>
2154 _GLIBCXX20_CONSTEXPR
2155 bool
2156 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2157 _ForwardIterator2 __first2, _BinaryPredicate __pred)
2158 {
2159 // Efficiently compare identical prefixes: O(N) if sequences
2160 // have the same elements in the same order.
2161 for (; __first1 != __last1; ++__first1, (void)++__first2)
2162 if (!__pred(__first1, __first2))
2163 break;
2164
2165 if (__first1 == __last1)
2166 return true;
2167
2168 // Establish __last2 assuming equal ranges by iterating over the
2169 // rest of the list.
2170 _ForwardIterator2 __last2 = __first2;
2171 std::advance(__last2, std::distance(__first1, __last1));
2172 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
2173 {
2174 if (__scan != std::__find_if(__first1, __scan,
2175 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
2176 continue; // We've seen this one before.
2177
2178 auto __matches
2179 = std::__count_if(__first2, __last2,
2180 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
2181 if (0 == __matches ||
2182 std::__count_if(__scan, __last1,
2183 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
2184 != __matches)
2185 return false;
2186 }
2187 return true;
2188 }
2189
2190 /**
2191 * @brief Checks whether a permutation of the second sequence is equal
2192 * to the first sequence.
2193 * @ingroup non_mutating_algorithms
2194 * @param __first1 Start of first range.
2195 * @param __last1 End of first range.
2196 * @param __first2 Start of second range.
2197 * @return true if there exists a permutation of the elements in the range
2198 * [__first2, __first2 + (__last1 - __first1)), beginning with
2199 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2200 * returns true; otherwise, returns false.
2201 */
2202 template<typename _ForwardIterator1, typename _ForwardIterator2>
2203 _GLIBCXX20_CONSTEXPR
2204 inline bool
2205 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2206 _ForwardIterator2 __first2)
2207 {
2208 // concept requirements
2209 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
2210 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
2211 __glibcxx_function_requires(_EqualOpConcept<
2214 __glibcxx_requires_valid_range(__first1, __last1);
2215
2216 return std::__is_permutation(__first1, __last1, __first2,
2217 __gnu_cxx::__ops::__iter_equal_to_iter());
2218 }
2219#endif // C++11
2220
2221_GLIBCXX_END_NAMESPACE_VERSION
2222} // namespace std
2223
2224// NB: This file is included within many other C++ includes, as a way
2225// of getting the base algorithms. So, make sure that parallel bits
2226// come in too if requested.
2227#ifdef _GLIBCXX_PARALLEL
2228# include <parallel/algobase.h>
2229#endif
2230
2231#endif
Parallel STL function calls corresponding to the stl_algobase.h header. The functions defined here ma...
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition: type_traits:1983
constexpr bool is_constant_evaluated() noexcept
Returns true only when called during constant evaluation.
Definition: type_traits:3628
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
Definition: stl_algobase.h:890
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:257
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:233
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr _Tp __lg(_Tp __n)
This is a helper function for the sort routines and for random.tcc.
constexpr _InputIterator __find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred, input_iterator_tag)
This is an overload used by find algos for the Input Iterator case.
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
is_nothrow_copy_constructible
Definition: type_traits:1133
Safe iterator wrapper.
Traits class for iterators.
Marking input iterators.
Marking output iterators.
Random-access iterators support a superset of bidirectional iterator operations.