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