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