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