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