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
1153 __glibcxx_requires_can_increment(__first, __n);
1154
1155 std::__fill_a(__first, __first + __n, __value);
1156 return __first + __n;
1157 }
1158
1159 /**
1160 * @brief Fills the range [first,first+n) with copies of value.
1161 * @ingroup mutating_algorithms
1162 * @param __first An output iterator.
1163 * @param __n The count of copies to perform.
1164 * @param __value A reference-to-const of arbitrary type.
1165 * @return The iterator at first+n.
1166 *
1167 * This function fills a range with copies of the same value. For char
1168 * types filling contiguous areas of memory, this becomes an inline call
1169 * to @c memset or @c wmemset.
1170 *
1171 * If @p __n is negative, the function does nothing.
1172 */
1173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1174 // DR 865. More algorithms that throw away information
1175 // DR 426. search_n(), fill_n(), and generate_n() with negative n
1176 template<typename _OI, typename _Size, typename _Tp>
1177 __attribute__((__always_inline__))
1178 _GLIBCXX20_CONSTEXPR
1179 inline _OI
1180 fill_n(_OI __first, _Size __n, const _Tp& __value)
1181 {
1182 // concept requirements
1183 __glibcxx_function_requires(_OutputIteratorConcept<_OI, const _Tp&>)
1184
1185 return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
1186 std::__iterator_category(__first));
1187 }
1188
1189 template<bool _BoolType>
1190 struct __equal
1191 {
1192 template<typename _II1, typename _II2>
1193 _GLIBCXX20_CONSTEXPR
1194 static bool
1195 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1196 {
1197 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1198 if (!(*__first1 == *__first2))
1199 return false;
1200 return true;
1201 }
1202 };
1203
1204 template<>
1205 struct __equal<true>
1206 {
1207 template<typename _Tp>
1208 _GLIBCXX20_CONSTEXPR
1209 static bool
1210 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
1211 {
1212 if (const size_t __len = (__last1 - __first1))
1213 return !std::__memcmp(__first1, __first2, __len);
1214 return true;
1215 }
1216 };
1217
1218 template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
1219 typename __gnu_cxx::__enable_if<
1220 __is_random_access_iter<_II>::__value, bool>::__type
1221 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1222 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1223 _II);
1224
1225 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1226 typename _Tp2, typename _Ref2, typename _Ptr2>
1227 bool
1228 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1229 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1230 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1231
1232 template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
1233 typename __gnu_cxx::__enable_if<
1234 __is_random_access_iter<_II>::__value, bool>::__type
1235 __equal_aux1(_II, _II,
1236 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
1237
1238 template<typename _II1, typename _II2>
1239 _GLIBCXX20_CONSTEXPR
1240 inline bool
1241 __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
1242 {
1243 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1244 const bool __simple = ((__is_integer<_ValueType1>::__value
1245#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
1246 || __is_pointer(_ValueType1)
1247#endif
1248#if __glibcxx_byte && __glibcxx_type_trait_variable_templates
1249 // bits/cpp_type_traits.h declares std::byte
1250 || is_same_v<_ValueType1, byte>
1251#endif
1252 ) && __memcmpable<_II1, _II2>::__value);
1253 return std::__equal<__simple>::equal(__first1, __last1, __first2);
1254 }
1255
1256 template<typename _II1, typename _II2>
1257 __attribute__((__always_inline__))
1258 _GLIBCXX20_CONSTEXPR
1259 inline bool
1260 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
1261 {
1262 return std::__equal_aux1(std::__niter_base(__first1),
1263 std::__niter_base(__last1),
1264 std::__niter_base(__first2));
1265 }
1266
1267 template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
1268 _GLIBCXX20_CONSTEXPR
1269 bool
1270 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1271 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1272 _II2);
1273
1274 template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
1275 _GLIBCXX20_CONSTEXPR
1276 bool
1277 __equal_aux(_II1, _II1,
1278 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1279
1280 template<typename _II1, typename _Seq1, typename _Cat1,
1281 typename _II2, typename _Seq2, typename _Cat2>
1282 _GLIBCXX20_CONSTEXPR
1283 bool
1284 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1285 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1286 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1287
1288 template<typename, typename>
1289 struct __lc_rai
1290 {
1291 template<typename _II1, typename _II2>
1292 _GLIBCXX20_CONSTEXPR
1293 static _II1
1294 __newlast1(_II1, _II1 __last1, _II2, _II2)
1295 { return __last1; }
1296
1297 template<typename _II>
1298 _GLIBCXX20_CONSTEXPR
1299 static bool
1300 __cnd2(_II __first, _II __last)
1301 { return __first != __last; }
1302 };
1303
1304 template<>
1306 {
1307 template<typename _RAI1, typename _RAI2>
1308 _GLIBCXX20_CONSTEXPR
1309 static _RAI1
1310 __newlast1(_RAI1 __first1, _RAI1 __last1,
1311 _RAI2 __first2, _RAI2 __last2)
1312 {
1313 const typename iterator_traits<_RAI1>::difference_type
1314 __diff1 = __last1 - __first1;
1315 const typename iterator_traits<_RAI2>::difference_type
1316 __diff2 = __last2 - __first2;
1317 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
1318 }
1319
1320 template<typename _RAI>
1321 static _GLIBCXX20_CONSTEXPR bool
1322 __cnd2(_RAI, _RAI)
1323 { return true; }
1324 };
1325
1326 template<typename _II1, typename _II2, typename _Compare>
1327 _GLIBCXX20_CONSTEXPR
1328 bool
1329 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
1330 _II2 __first2, _II2 __last2,
1331 _Compare __comp)
1332 {
1333 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1334 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1335 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1336
1337 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1338 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1339 ++__first1, (void)++__first2)
1340 {
1341 if (__comp(__first1, __first2))
1342 return true;
1343 if (__comp(__first2, __first1))
1344 return false;
1345 }
1346 return __first1 == __last1 && __first2 != __last2;
1347 }
1348
1349 template<bool _BoolType>
1350 struct __lexicographical_compare
1351 {
1352 template<typename _II1, typename _II2>
1353 _GLIBCXX20_CONSTEXPR
1354 static bool
1355 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1356 {
1357 using __gnu_cxx::__ops::__iter_less_iter;
1358 return std::__lexicographical_compare_impl(__first1, __last1,
1359 __first2, __last2,
1360 __iter_less_iter());
1361 }
1362
1363 template<typename _II1, typename _II2>
1364 _GLIBCXX20_CONSTEXPR
1365 static int
1366 __3way(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1367 {
1368 while (__first1 != __last1)
1369 {
1370 if (__first2 == __last2)
1371 return +1;
1372 if (*__first1 < *__first2)
1373 return -1;
1374 if (*__first2 < *__first1)
1375 return +1;
1376 ++__first1;
1377 ++__first2;
1378 }
1379 return int(__first2 == __last2) - 1;
1380 }
1381 };
1382
1383 template<>
1384 struct __lexicographical_compare<true>
1385 {
1386 template<typename _Tp, typename _Up>
1387 _GLIBCXX20_CONSTEXPR
1388 static bool
1389 __lc(const _Tp* __first1, const _Tp* __last1,
1390 const _Up* __first2, const _Up* __last2)
1391 { return __3way(__first1, __last1, __first2, __last2) < 0; }
1392
1393 template<typename _Tp, typename _Up>
1394 _GLIBCXX20_CONSTEXPR
1395 static ptrdiff_t
1396 __3way(const _Tp* __first1, const _Tp* __last1,
1397 const _Up* __first2, const _Up* __last2)
1398 {
1399 const size_t __len1 = __last1 - __first1;
1400 const size_t __len2 = __last2 - __first2;
1401 if (const size_t __len = std::min(__len1, __len2))
1402 if (int __result = std::__memcmp(__first1, __first2, __len))
1403 return __result;
1404 return ptrdiff_t(__len1 - __len2);
1405 }
1406 };
1407
1408 template<typename _II1, typename _II2>
1409 _GLIBCXX20_CONSTEXPR
1410 inline bool
1411 __lexicographical_compare_aux1(_II1 __first1, _II1 __last1,
1412 _II2 __first2, _II2 __last2)
1413 {
1414 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1415 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1416#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
1417 const bool __simple =
1418 (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
1419 && __is_pointer(_II1) && __is_pointer(_II2)
1420#if __cplusplus > 201703L && __glibcxx_concepts
1421 // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
1422 // so __is_byte<T> could be true, but we can't use memcmp with
1423 // volatile data.
1424 && !is_volatile_v<remove_reference_t<iter_reference_t<_II1>>>
1425 && !is_volatile_v<remove_reference_t<iter_reference_t<_II2>>>
1426#endif
1427 );
1428#else
1429 const bool __simple = false;
1430#endif
1431
1432 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
1433 __first2, __last2);
1434 }
1435
1436 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1437 typename _Tp2>
1438 bool
1439 __lexicographical_compare_aux1(
1440 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1441 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1442 _Tp2*, _Tp2*);
1443
1444 template<typename _Tp1,
1445 typename _Tp2, typename _Ref2, typename _Ptr2>
1446 bool
1447 __lexicographical_compare_aux1(_Tp1*, _Tp1*,
1448 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
1449 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1450
1451 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1452 typename _Tp2, typename _Ref2, typename _Ptr2>
1453 bool
1454 __lexicographical_compare_aux1(
1455 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1456 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1457 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
1458 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1459
1460 template<typename _II1, typename _II2>
1461 _GLIBCXX20_CONSTEXPR
1462 inline bool
1463 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
1464 _II2 __first2, _II2 __last2)
1465 {
1466 return std::__lexicographical_compare_aux1(std::__niter_base(__first1),
1467 std::__niter_base(__last1),
1468 std::__niter_base(__first2),
1469 std::__niter_base(__last2));
1470 }
1471
1472 template<typename _Iter1, typename _Seq1, typename _Cat1,
1473 typename _II2>
1474 _GLIBCXX20_CONSTEXPR
1475 bool
1476 __lexicographical_compare_aux(
1477 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1478 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1479 _II2, _II2);
1480
1481 template<typename _II1,
1482 typename _Iter2, typename _Seq2, typename _Cat2>
1483 _GLIBCXX20_CONSTEXPR
1484 bool
1485 __lexicographical_compare_aux(
1486 _II1, _II1,
1487 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
1488 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
1489
1490 template<typename _Iter1, typename _Seq1, typename _Cat1,
1491 typename _Iter2, typename _Seq2, typename _Cat2>
1492 _GLIBCXX20_CONSTEXPR
1493 bool
1494 __lexicographical_compare_aux(
1495 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1496 const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
1497 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
1498 const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
1499
1500 template<typename _ForwardIterator, typename _Tp, typename _Compare>
1501 _GLIBCXX20_CONSTEXPR
1502 _ForwardIterator
1503 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1504 const _Tp& __val, _Compare __comp)
1505 {
1507 _DistanceType;
1508
1509 _DistanceType __len = std::distance(__first, __last);
1510
1511 while (__len > 0)
1512 {
1513 _DistanceType __half = __len >> 1;
1514 _ForwardIterator __middle = __first;
1515 std::advance(__middle, __half);
1516 if (__comp(__middle, __val))
1517 {
1518 __first = __middle;
1519 ++__first;
1520 __len = __len - __half - 1;
1521 }
1522 else
1523 __len = __half;
1524 }
1525 return __first;
1526 }
1527
1528 /**
1529 * @brief Finds the first position in which @a val could be inserted
1530 * without changing the ordering.
1531 * @param __first An iterator.
1532 * @param __last Another iterator.
1533 * @param __val The search term.
1534 * @return An iterator pointing to the first element <em>not less
1535 * than</em> @a val, or end() if every element is less than
1536 * @a val.
1537 * @ingroup binary_search_algorithms
1538 */
1539 template<typename _ForwardIterator, typename _Tp>
1540 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1541 inline _ForwardIterator
1542 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1543 const _Tp& __val)
1544 {
1545 // concept requirements
1546 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1547 __glibcxx_function_requires(_LessThanOpConcept<
1549 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1550
1551 return std::__lower_bound(__first, __last, __val,
1552 __gnu_cxx::__ops::__iter_less_val());
1553 }
1554
1555 /// This is a helper function for the sort routines and for random.tcc.
1556 // Precondition: __n > 0.
1557 template<typename _Tp>
1558 inline _GLIBCXX_CONSTEXPR _Tp
1559 __lg(_Tp __n)
1560 {
1561#if __cplusplus >= 201402L
1562 return std::__bit_width(make_unsigned_t<_Tp>(__n)) - 1;
1563#else
1564#pragma GCC diagnostic push
1565#pragma GCC diagnostic ignored "-Wlong-long"
1566 // Use +__n so it promotes to at least int.
1567 return (sizeof(+__n) * __CHAR_BIT__ - 1)
1568 - (sizeof(+__n) == sizeof(long long)
1569 ? __builtin_clzll(+__n)
1570 : (sizeof(+__n) == sizeof(long)
1571 ? __builtin_clzl(+__n)
1572 : __builtin_clz(+__n)));
1573#pragma GCC diagnostic pop
1574#endif
1575 }
1576
1577_GLIBCXX_BEGIN_NAMESPACE_ALGO
1578
1579 /**
1580 * @brief Tests a range for element-wise equality.
1581 * @ingroup non_mutating_algorithms
1582 * @param __first1 An input iterator.
1583 * @param __last1 An input iterator.
1584 * @param __first2 An input iterator.
1585 * @return A boolean true or false.
1586 *
1587 * This compares the elements of two ranges using @c == and returns true or
1588 * false depending on whether all of the corresponding elements of the
1589 * ranges are equal.
1590 */
1591 template<typename _II1, typename _II2>
1592 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1593 inline bool
1594 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1595 {
1596 // concept requirements
1597 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1598 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1599 __glibcxx_function_requires(_EqualOpConcept<
1602 __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
1603
1604 return std::__equal_aux(__first1, __last1, __first2);
1605 }
1606
1607 /**
1608 * @brief Tests a range for element-wise equality.
1609 * @ingroup non_mutating_algorithms
1610 * @param __first1 An input iterator.
1611 * @param __last1 An input iterator.
1612 * @param __first2 An input iterator.
1613 * @param __binary_pred A binary predicate @link functors
1614 * functor@endlink.
1615 * @return A boolean true or false.
1616 *
1617 * This compares the elements of two ranges using the binary_pred
1618 * parameter, and returns true or
1619 * false depending on whether all of the corresponding elements of the
1620 * ranges are equal.
1621 */
1622 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1623 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1624 inline bool
1625 equal(_IIter1 __first1, _IIter1 __last1,
1626 _IIter2 __first2, _BinaryPredicate __binary_pred)
1627 {
1628 // concept requirements
1629 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1630 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1631 __glibcxx_requires_valid_range(__first1, __last1);
1632
1633 for (; __first1 != __last1; ++__first1, (void)++__first2)
1634 if (!bool(__binary_pred(*__first1, *__first2)))
1635 return false;
1636 return true;
1637 }
1638
1639#if __cplusplus >= 201103L
1640#pragma GCC diagnostic push
1641#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1642
1643 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1644 template<typename _II1, typename _II2>
1645 _GLIBCXX20_CONSTEXPR
1646 inline bool
1647 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1648 {
1649 using _RATag = random_access_iterator_tag;
1650 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1651 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1652 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1653 if constexpr (_RAIters::value)
1654 {
1655 if ((__last1 - __first1) != (__last2 - __first2))
1656 return false;
1657 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1658 }
1659 else
1660 {
1661 for (; __first1 != __last1 && __first2 != __last2;
1662 ++__first1, (void)++__first2)
1663 if (!(*__first1 == *__first2))
1664 return false;
1665 return __first1 == __last1 && __first2 == __last2;
1666 }
1667 }
1668
1669 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1670 template<typename _II1, typename _II2, typename _BinaryPredicate>
1671 _GLIBCXX20_CONSTEXPR
1672 inline bool
1673 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
1674 _BinaryPredicate __binary_pred)
1675 {
1676 using _RATag = random_access_iterator_tag;
1677 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1678 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1679 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1680 if constexpr (_RAIters::value)
1681 {
1682 if ((__last1 - __first1) != (__last2 - __first2))
1683 return false;
1684 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1685 __binary_pred);
1686 }
1687 else
1688 {
1689 for (; __first1 != __last1 && __first2 != __last2;
1690 ++__first1, (void)++__first2)
1691 if (!bool(__binary_pred(*__first1, *__first2)))
1692 return false;
1693 return __first1 == __last1 && __first2 == __last2;
1694 }
1695 }
1696#pragma GCC diagnostic pop
1697#endif // C++11
1698
1699#ifdef __glibcxx_robust_nonmodifying_seq_ops // C++ >= 14
1700 /**
1701 * @brief Tests a range for element-wise equality.
1702 * @ingroup non_mutating_algorithms
1703 * @param __first1 An input iterator.
1704 * @param __last1 An input iterator.
1705 * @param __first2 An input iterator.
1706 * @param __last2 An input iterator.
1707 * @return A boolean true or false.
1708 *
1709 * This compares the elements of two ranges using @c == and returns true or
1710 * false depending on whether all of the corresponding elements of the
1711 * ranges are equal.
1712 */
1713 template<typename _II1, typename _II2>
1714 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1715 inline bool
1716 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1717 {
1718 // concept requirements
1719 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1720 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1721 __glibcxx_function_requires(_EqualOpConcept<
1724 __glibcxx_requires_valid_range(__first1, __last1);
1725 __glibcxx_requires_valid_range(__first2, __last2);
1726
1727 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
1728 }
1729
1730 /**
1731 * @brief Tests a range for element-wise equality.
1732 * @ingroup non_mutating_algorithms
1733 * @param __first1 An input iterator.
1734 * @param __last1 An input iterator.
1735 * @param __first2 An input iterator.
1736 * @param __last2 An input iterator.
1737 * @param __binary_pred A binary predicate @link functors
1738 * functor@endlink.
1739 * @return A boolean true or false.
1740 *
1741 * This compares the elements of two ranges using the binary_pred
1742 * parameter, and returns true or
1743 * false depending on whether all of the corresponding elements of the
1744 * ranges are equal.
1745 */
1746 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1747 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1748 inline bool
1749 equal(_IIter1 __first1, _IIter1 __last1,
1750 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1751 {
1752 // concept requirements
1753 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1754 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1755 __glibcxx_requires_valid_range(__first1, __last1);
1756 __glibcxx_requires_valid_range(__first2, __last2);
1757
1758 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
1759 __binary_pred);
1760 }
1761#endif // __glibcxx_robust_nonmodifying_seq_ops
1762
1763 /**
1764 * @brief Performs @b dictionary comparison on ranges.
1765 * @ingroup sorting_algorithms
1766 * @param __first1 An input iterator.
1767 * @param __last1 An input iterator.
1768 * @param __first2 An input iterator.
1769 * @param __last2 An input iterator.
1770 * @return A boolean true or false.
1771 *
1772 * <em>Returns true if the sequence of elements defined by the range
1773 * [first1,last1) is lexicographically less than the sequence of elements
1774 * defined by the range [first2,last2). Returns false otherwise.</em>
1775 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1776 * then this is an inline call to @c memcmp.
1777 */
1778 template<typename _II1, typename _II2>
1779 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1780 inline bool
1781 lexicographical_compare(_II1 __first1, _II1 __last1,
1782 _II2 __first2, _II2 __last2)
1783 {
1784#ifdef _GLIBCXX_CONCEPT_CHECKS
1785 // concept requirements
1786 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1787 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1788#endif
1789 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1790 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1791 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1792 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1793 __glibcxx_requires_valid_range(__first1, __last1);
1794 __glibcxx_requires_valid_range(__first2, __last2);
1795
1796 return std::__lexicographical_compare_aux(__first1, __last1,
1797 __first2, __last2);
1798 }
1799
1800 /**
1801 * @brief Performs @b dictionary comparison on ranges.
1802 * @ingroup sorting_algorithms
1803 * @param __first1 An input iterator.
1804 * @param __last1 An input iterator.
1805 * @param __first2 An input iterator.
1806 * @param __last2 An input iterator.
1807 * @param __comp A @link comparison_functors comparison functor@endlink.
1808 * @return A boolean true or false.
1809 *
1810 * The same as the four-parameter @c lexicographical_compare, but uses the
1811 * comp parameter instead of @c <.
1812 */
1813 template<typename _II1, typename _II2, typename _Compare>
1814 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1815 inline bool
1816 lexicographical_compare(_II1 __first1, _II1 __last1,
1817 _II2 __first2, _II2 __last2, _Compare __comp)
1818 {
1819 // concept requirements
1820 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1821 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1822 __glibcxx_requires_valid_range(__first1, __last1);
1823 __glibcxx_requires_valid_range(__first2, __last2);
1824
1825 return std::__lexicographical_compare_impl
1826 (__first1, __last1, __first2, __last2,
1827 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1828 }
1829
1830#if __cpp_lib_three_way_comparison
1831 // Both iterators refer to contiguous ranges of unsigned narrow characters,
1832 // or std::byte, or big-endian unsigned integers, suitable for comparison
1833 // using memcmp.
1834 template<typename _Iter1, typename _Iter2>
1835 concept __memcmp_ordered_with
1836 = (__is_memcmp_ordered_with<iter_value_t<_Iter1>,
1837 iter_value_t<_Iter2>>::__value)
1838 && contiguous_iterator<_Iter1> && contiguous_iterator<_Iter2>;
1839
1840 // Return a struct with two members, initialized to the smaller of x and y
1841 // (or x if they compare equal) and the result of the comparison x <=> y.
1842 template<typename _Tp>
1843 constexpr auto
1844 __min_cmp(_Tp __x, _Tp __y)
1845 {
1846 struct _Res {
1847 _Tp _M_min;
1848 decltype(__x <=> __y) _M_cmp;
1849 };
1850 auto __c = __x <=> __y;
1851 if (__c > 0)
1852 return _Res{__y, __c};
1853 return _Res{__x, __c};
1854 }
1855
1856 /**
1857 * @brief Performs dictionary comparison on ranges.
1858 * @ingroup sorting_algorithms
1859 * @param __first1 An input iterator.
1860 * @param __last1 An input iterator.
1861 * @param __first2 An input iterator.
1862 * @param __last2 An input iterator.
1863 * @param __comp A @link comparison_functors comparison functor@endlink.
1864 * @return The comparison category that `__comp(*__first1, *__first2)`
1865 * returns.
1866 */
1867 template<typename _InputIter1, typename _InputIter2, typename _Comp>
1868 [[nodiscard]] constexpr auto
1870 _InputIter1 __last1,
1871 _InputIter2 __first2,
1872 _InputIter2 __last2,
1873 _Comp __comp)
1874 -> decltype(__comp(*__first1, *__first2))
1875 {
1876 // concept requirements
1877 __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
1878 __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
1879 __glibcxx_requires_valid_range(__first1, __last1);
1880 __glibcxx_requires_valid_range(__first2, __last2);
1881
1882 using _Cat = decltype(__comp(*__first1, *__first2));
1883 static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
1884
1885 if (!std::__is_constant_evaluated())
1886 if constexpr (same_as<_Comp, __detail::_Synth3way>
1887 || same_as<_Comp, compare_three_way>)
1888 if constexpr (__memcmp_ordered_with<_InputIter1, _InputIter2>)
1889 {
1890 const auto [__len, __lencmp] = _GLIBCXX_STD_A::
1891 __min_cmp(__last1 - __first1, __last2 - __first2);
1892 if (__len)
1893 {
1894 const auto __blen = __len * sizeof(*__first1);
1895 const auto __c
1896 = __builtin_memcmp(&*__first1, &*__first2, __blen) <=> 0;
1897 if (__c != 0)
1898 return __c;
1899 }
1900 return __lencmp;
1901 }
1902
1903 while (__first1 != __last1)
1904 {
1905 if (__first2 == __last2)
1906 return strong_ordering::greater;
1907 if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
1908 return __cmp;
1909 ++__first1;
1910 ++__first2;
1911 }
1912 return (__first2 == __last2) <=> true; // See PR 94006
1913 }
1914
1915 template<typename _InputIter1, typename _InputIter2>
1916 constexpr auto
1917 lexicographical_compare_three_way(_InputIter1 __first1,
1918 _InputIter1 __last1,
1919 _InputIter2 __first2,
1920 _InputIter2 __last2)
1921 {
1922 return _GLIBCXX_STD_A::
1923 lexicographical_compare_three_way(__first1, __last1, __first2, __last2,
1924 compare_three_way{});
1925 }
1926#endif // three_way_comparison
1927
1928 template<typename _InputIterator1, typename _InputIterator2,
1929 typename _BinaryPredicate>
1930 _GLIBCXX20_CONSTEXPR
1932 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1933 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1934 {
1935 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1936 {
1937 ++__first1;
1938 ++__first2;
1939 }
1940 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1941 }
1942
1943 /**
1944 * @brief Finds the places in ranges which don't match.
1945 * @ingroup non_mutating_algorithms
1946 * @param __first1 An input iterator.
1947 * @param __last1 An input iterator.
1948 * @param __first2 An input iterator.
1949 * @return A pair of iterators pointing to the first mismatch.
1950 *
1951 * This compares the elements of two ranges using @c == and returns a pair
1952 * of iterators. The first iterator points into the first range, the
1953 * second iterator points into the second range, and the elements pointed
1954 * to by the iterators are not equal.
1955 */
1956 template<typename _InputIterator1, typename _InputIterator2>
1957 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1959 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1960 _InputIterator2 __first2)
1961 {
1962 // concept requirements
1963 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1964 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1965 __glibcxx_function_requires(_EqualOpConcept<
1968 __glibcxx_requires_valid_range(__first1, __last1);
1969
1970 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1971 __gnu_cxx::__ops::__iter_equal_to_iter());
1972 }
1973
1974 /**
1975 * @brief Finds the places in ranges which don't match.
1976 * @ingroup non_mutating_algorithms
1977 * @param __first1 An input iterator.
1978 * @param __last1 An input iterator.
1979 * @param __first2 An input iterator.
1980 * @param __binary_pred A binary predicate @link functors
1981 * functor@endlink.
1982 * @return A pair of iterators pointing to the first mismatch.
1983 *
1984 * This compares the elements of two ranges using the binary_pred
1985 * parameter, and returns a pair
1986 * of iterators. The first iterator points into the first range, the
1987 * second iterator points into the second range, and the elements pointed
1988 * to by the iterators are not equal.
1989 */
1990 template<typename _InputIterator1, typename _InputIterator2,
1991 typename _BinaryPredicate>
1992 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1994 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1995 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1996 {
1997 // concept requirements
1998 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1999 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2000 __glibcxx_requires_valid_range(__first1, __last1);
2001
2002 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
2003 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
2004 }
2005
2006#if __glibcxx_robust_nonmodifying_seq_ops // C++ >= 14
2007 template<typename _InputIterator1, typename _InputIterator2,
2008 typename _BinaryPredicate>
2009 _GLIBCXX20_CONSTEXPR
2011 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
2012 _InputIterator2 __first2, _InputIterator2 __last2,
2013 _BinaryPredicate __binary_pred)
2014 {
2015 while (__first1 != __last1 && __first2 != __last2
2016 && __binary_pred(__first1, __first2))
2017 {
2018 ++__first1;
2019 ++__first2;
2020 }
2021 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
2022 }
2023
2024 /**
2025 * @brief Finds the places in ranges which don't match.
2026 * @ingroup non_mutating_algorithms
2027 * @param __first1 An input iterator.
2028 * @param __last1 An input iterator.
2029 * @param __first2 An input iterator.
2030 * @param __last2 An input iterator.
2031 * @return A pair of iterators pointing to the first mismatch.
2032 *
2033 * This compares the elements of two ranges using @c == and returns a pair
2034 * of iterators. The first iterator points into the first range, the
2035 * second iterator points into the second range, and the elements pointed
2036 * to by the iterators are not equal.
2037 */
2038 template<typename _InputIterator1, typename _InputIterator2>
2039 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2041 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
2042 _InputIterator2 __first2, _InputIterator2 __last2)
2043 {
2044 // concept requirements
2045 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2046 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2047 __glibcxx_function_requires(_EqualOpConcept<
2050 __glibcxx_requires_valid_range(__first1, __last1);
2051 __glibcxx_requires_valid_range(__first2, __last2);
2052
2053 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
2054 __gnu_cxx::__ops::__iter_equal_to_iter());
2055 }
2056
2057 /**
2058 * @brief Finds the places in ranges which don't match.
2059 * @ingroup non_mutating_algorithms
2060 * @param __first1 An input iterator.
2061 * @param __last1 An input iterator.
2062 * @param __first2 An input iterator.
2063 * @param __last2 An input iterator.
2064 * @param __binary_pred A binary predicate @link functors
2065 * functor@endlink.
2066 * @return A pair of iterators pointing to the first mismatch.
2067 *
2068 * This compares the elements of two ranges using the binary_pred
2069 * parameter, and returns a pair
2070 * of iterators. The first iterator points into the first range, the
2071 * second iterator points into the second range, and the elements pointed
2072 * to by the iterators are not equal.
2073 */
2074 template<typename _InputIterator1, typename _InputIterator2,
2075 typename _BinaryPredicate>
2076 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2078 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
2079 _InputIterator2 __first2, _InputIterator2 __last2,
2080 _BinaryPredicate __binary_pred)
2081 {
2082 // concept requirements
2083 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2084 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2085 __glibcxx_requires_valid_range(__first1, __last1);
2086 __glibcxx_requires_valid_range(__first2, __last2);
2087
2088 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
2089 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
2090 }
2091#endif
2092
2093_GLIBCXX_END_NAMESPACE_ALGO
2094
2095 // Implementation of std::find_if, also used in std::remove_if and others.
2096 template<typename _Iterator, typename _Predicate>
2097 _GLIBCXX20_CONSTEXPR
2098 inline _Iterator
2099 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
2100 {
2101#pragma GCC unroll 4
2102 while (__first != __last && !__pred(__first))
2103 ++__first;
2104 return __first;
2105 }
2106
2107 template<typename _InputIterator, typename _Predicate>
2108 _GLIBCXX20_CONSTEXPR
2110 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
2111 {
2113 for (; __first != __last; ++__first)
2114 if (__pred(__first))
2115 ++__n;
2116 return __n;
2117 }
2118
2119 template<typename _ForwardIterator, typename _Predicate>
2120 _GLIBCXX20_CONSTEXPR
2121 _ForwardIterator
2122 __remove_if(_ForwardIterator __first, _ForwardIterator __last,
2123 _Predicate __pred)
2124 {
2125 __first = std::__find_if(__first, __last, __pred);
2126 if (__first == __last)
2127 return __first;
2128 _ForwardIterator __result = __first;
2129 ++__first;
2130 for (; __first != __last; ++__first)
2131 if (!__pred(__first))
2132 {
2133 *__result = _GLIBCXX_MOVE(*__first);
2134 ++__result;
2135 }
2136 return __result;
2137 }
2138
2139 template<typename _ForwardIterator1, typename _ForwardIterator2,
2140 typename _BinaryPredicate>
2141 _GLIBCXX20_CONSTEXPR
2142 _ForwardIterator1
2143 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2144 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
2145 _BinaryPredicate __predicate)
2146 {
2147 // Test for empty ranges
2148 if (__first1 == __last1 || __first2 == __last2)
2149 return __first1;
2150
2151 // Test for a pattern of length 1.
2152 _ForwardIterator2 __p1(__first2);
2153 if (++__p1 == __last2)
2154 return std::__find_if(__first1, __last1,
2155 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
2156
2157 // General case.
2158 _ForwardIterator1 __current = __first1;
2159
2160 for (;;)
2161 {
2162 __first1 =
2163 std::__find_if(__first1, __last1,
2164 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
2165
2166 if (__first1 == __last1)
2167 return __last1;
2168
2169 _ForwardIterator2 __p = __p1;
2170 __current = __first1;
2171 if (++__current == __last1)
2172 return __last1;
2173
2174 while (__predicate(__current, __p))
2175 {
2176 if (++__p == __last2)
2177 return __first1;
2178 if (++__current == __last1)
2179 return __last1;
2180 }
2181 ++__first1;
2182 }
2183 return __first1;
2184 }
2185
2186#if __cplusplus >= 201103L
2187 template<typename _ForwardIterator1, typename _ForwardIterator2,
2188 typename _BinaryPredicate>
2189 _GLIBCXX20_CONSTEXPR
2190 bool
2191 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2192 _ForwardIterator2 __first2, _BinaryPredicate __pred)
2193 {
2194 // Efficiently compare identical prefixes: O(N) if sequences
2195 // have the same elements in the same order.
2196 for (; __first1 != __last1; ++__first1, (void)++__first2)
2197 if (!__pred(__first1, __first2))
2198 break;
2199
2200 if (__first1 == __last1)
2201 return true;
2202
2203 // Establish __last2 assuming equal ranges by iterating over the
2204 // rest of the list.
2205 _ForwardIterator2 __last2 = __first2;
2206 std::advance(__last2, std::distance(__first1, __last1));
2207 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
2208 {
2209 if (__scan != std::__find_if(__first1, __scan,
2210 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
2211 continue; // We've seen this one before.
2212
2213 auto __matches
2214 = std::__count_if(__first2, __last2,
2215 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
2216 if (0 == __matches ||
2217 std::__count_if(__scan, __last1,
2218 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
2219 != __matches)
2220 return false;
2221 }
2222 return true;
2223 }
2224
2225 /**
2226 * @brief Checks whether a permutation of the second sequence is equal
2227 * to the first sequence.
2228 * @ingroup non_mutating_algorithms
2229 * @param __first1 Start of first range.
2230 * @param __last1 End of first range.
2231 * @param __first2 Start of second range.
2232 * @return true if there exists a permutation of the elements in the range
2233 * [__first2, __first2 + (__last1 - __first1)), beginning with
2234 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2235 * returns true; otherwise, returns false.
2236 */
2237 template<typename _ForwardIterator1, typename _ForwardIterator2>
2238 _GLIBCXX20_CONSTEXPR
2239 inline bool
2240 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2241 _ForwardIterator2 __first2)
2242 {
2243 // concept requirements
2244 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
2245 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
2246 __glibcxx_function_requires(_EqualOpConcept<
2249 __glibcxx_requires_valid_range(__first1, __last1);
2250
2251 return std::__is_permutation(__first1, __last1, __first2,
2252 __gnu_cxx::__ops::__iter_equal_to_iter());
2253 }
2254#endif // C++11
2255
2256_GLIBCXX_BEGIN_NAMESPACE_ALGO
2257
2258 /**
2259 * @brief Search a sequence for a matching sub-sequence using a predicate.
2260 * @ingroup non_mutating_algorithms
2261 * @param __first1 A forward iterator.
2262 * @param __last1 A forward iterator.
2263 * @param __first2 A forward iterator.
2264 * @param __last2 A forward iterator.
2265 * @param __predicate A binary predicate.
2266 * @return The first iterator @c i in the range
2267 * @p [__first1,__last1-(__last2-__first2)) such that
2268 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
2269 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
2270 *
2271 * Searches the range @p [__first1,__last1) for a sub-sequence that
2272 * compares equal value-by-value with the sequence given by @p
2273 * [__first2,__last2), using @p __predicate to determine equality,
2274 * and returns an iterator to the first element of the
2275 * sub-sequence, or @p __last1 if no such iterator exists.
2276 *
2277 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
2278 */
2279 template<typename _ForwardIterator1, typename _ForwardIterator2,
2280 typename _BinaryPredicate>
2281 _GLIBCXX20_CONSTEXPR
2282 inline _ForwardIterator1
2283 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2284 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
2285 _BinaryPredicate __predicate)
2286 {
2287 // concept requirements
2288 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
2289 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
2290 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
2293 __glibcxx_requires_valid_range(__first1, __last1);
2294 __glibcxx_requires_valid_range(__first2, __last2);
2295
2296 return std::__search(__first1, __last1, __first2, __last2,
2297 __gnu_cxx::__ops::__iter_comp_iter(__predicate));
2298 }
2299
2300_GLIBCXX_END_NAMESPACE_ALGO
2301_GLIBCXX_END_NAMESPACE_VERSION
2302} // namespace std
2303
2304// NB: This file is included within many other C++ includes, as a way
2305// of getting the base algorithms. So, make sure that parallel bits
2306// come in too if requested.
2307#ifdef _GLIBCXX_PARALLEL
2308# include <parallel/algobase.h>
2309#endif
2310
2311#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:2202
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:485
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.