1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
56 #ifndef _STL_ALGOBASE_H
57 #define _STL_ALGOBASE_H 1
59 #include <bits/c++config.h>
60 #include <bits/functexcept.h>
61 #include <bits/cpp_type_traits.h>
62 #include <ext/type_traits.h>
63 #include <ext/numeric_traits.h>
64 #include <bits/stl_pair.h>
65 #include <bits/stl_iterator_base_types.h>
66 #include <bits/stl_iterator_base_funcs.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>
75 #if __cplusplus >= 201402L
76 # include <bit> // std::__bit_width
78 #if __cplusplus >= 202002L
82 namespace std
_GLIBCXX_VISIBILITY(default)
84 _GLIBCXX_BEGIN_NAMESPACE_VERSION
87 * A constexpr wrapper for __builtin_memcmp.
88 * @param __num The number of elements of type _Tp (not bytes).
90 template<typename _Tp
, typename _Up
>
93 __memcmp(const _Tp
* __first1
, const _Up
* __first2
, size_t __num
)
95 #if __cplusplus >= 201103L
96 static_assert(sizeof(_Tp
) == sizeof(_Up
), "can be compared with memcmp");
98 #ifdef __cpp_lib_is_constant_evaluated
99 if (std::is_constant_evaluated())
101 for(; __num
> 0; ++__first1
, ++__first2
, --__num
)
102 if (*__first1
!= *__first2
)
103 return *__first1
< *__first2
? -1 : 1;
108 return __builtin_memcmp(__first1
, __first2
, sizeof(_Tp
) * __num
);
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
>
118 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
120 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
122 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
124 _ValueType1 __tmp
= *__a
;
131 struct __iter_swap
<true>
133 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
135 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
143 * @brief Swaps the contents of two iterators.
144 * @ingroup mutating_algorithms
145 * @param __a An iterator.
146 * @param __b Another iterator.
149 * This function swaps the values pointed to by two iterators, not the
150 * iterators themselves.
152 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
155 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
157 // concept requirements
158 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
160 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
163 #if __cplusplus < 201103L
164 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
166 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
169 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
171 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
174 typedef typename iterator_traits
<_ForwardIterator1
>::reference
176 typedef typename iterator_traits
<_ForwardIterator2
>::reference
178 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
179 && __are_same
<_ValueType1
&, _ReferenceType1
>::__value
180 && __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
184 // 187. iter_swap underspecified
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).
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.
201 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
204 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
205 _ForwardIterator2 __first2
)
207 // concept requirements
208 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
210 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
212 __glibcxx_requires_valid_range(__first1
, __last1
);
214 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
215 std::iter_swap(__first1
, __first2
);
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.
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.
230 template<typename _Tp
>
233 min(const _Tp
& __a
, const _Tp
& __b
)
235 // concept requirements
236 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
237 //return __b < __a ? __b : __a;
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.
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.
254 template<typename _Tp
>
257 max(const _Tp
& __a
, const _Tp
& __b
)
259 // concept requirements
260 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
261 //return __a < __b ? __b : __a;
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.
275 * This will work on temporary expressions, since they are only evaluated
276 * once, unlike a preprocessor macro.
278 template<typename _Tp
, typename _Compare
>
281 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
283 //return __comp(__b, __a) ? __b : __a;
284 if (__comp(__b
, __a
))
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.
297 * This will work on temporary expressions, since they are only evaluated
298 * once, unlike a preprocessor macro.
300 template<typename _Tp
, typename _Compare
>
303 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
305 //return __comp(__a, __b) ? __b : __a;
306 if (__comp(__a
, __b
))
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
>
316 __niter_base(_Iterator __it
)
317 _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible
<_Iterator
>::value
)
320 template<typename _Ite
, typename _Seq
>
322 __niter_base(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
,
323 std::random_access_iterator_tag
>&);
325 // Reverse the __niter_base transformation to get a
326 // __normal_iterator back again (this assumes that __normal_iterator
327 // is only used to wrap random access iterators, like pointers).
328 template<typename _From
, typename _To
>
331 __niter_wrap(_From __from
, _To __res
)
332 { return __from
+ (__res
- std::__niter_base(__from
)); }
334 // No need to wrap, iterator already has the right type.
335 template<typename _Iterator
>
338 __niter_wrap(const _Iterator
&, _Iterator __res
)
341 // All of these auxiliary structs serve two purposes. (1) Replace
342 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
343 // because the input and output ranges are permitted to overlap.)
344 // (2) If we're using random access iterators, then write the loop as
345 // a for loop with an explicit count.
347 template<bool _IsMove
, bool _IsSimple
, typename _Category
>
350 template<typename _II
, typename _OI
>
353 __copy_m(_II __first
, _II __last
, _OI __result
)
355 for (; __first
!= __last
; ++__result
, (void)++__first
)
356 *__result
= *__first
;
361 #if __cplusplus >= 201103L
362 template<typename _Category
>
363 struct __copy_move
<true, false, _Category
>
365 template<typename _II
, typename _OI
>
368 __copy_m(_II __first
, _II __last
, _OI __result
)
370 for (; __first
!= __last
; ++__result
, (void)++__first
)
371 *__result
= std::move(*__first
);
378 struct __copy_move
<false, false, random_access_iterator_tag
>
380 template<typename _II
, typename _OI
>
383 __copy_m(_II __first
, _II __last
, _OI __result
)
385 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
386 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
388 *__result
= *__first
;
395 template<typename _Tp
, typename _Up
>
397 __assign_one(_Tp
* __to
, _Up
* __from
)
401 #if __cplusplus >= 201103L
403 struct __copy_move
<true, false, random_access_iterator_tag
>
405 template<typename _II
, typename _OI
>
408 __copy_m(_II __first
, _II __last
, _OI __result
)
410 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
411 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
413 *__result
= std::move(*__first
);
420 template<typename _Tp
, typename _Up
>
422 __assign_one(_Tp
* __to
, _Up
* __from
)
423 { *__to
= std::move(*__from
); }
427 template<bool _IsMove
>
428 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
430 template<typename _Tp
, typename _Up
>
433 __copy_m(_Tp
* __first
, _Tp
* __last
, _Up
* __result
)
435 const ptrdiff_t _Num
= __last
- __first
;
436 if (__builtin_expect(_Num
> 1, true))
437 __builtin_memmove(__result
, __first
, sizeof(_Tp
) * _Num
);
439 std::__copy_move
<_IsMove
, false, random_access_iterator_tag
>::
440 __assign_one(__result
, __first
);
441 return __result
+ _Num
;
445 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
447 template<typename _Tp
, typename _Ref
, typename _Ptr
>
448 struct _Deque_iterator
;
450 struct _Bit_iterator
;
452 _GLIBCXX_END_NAMESPACE_CONTAINER
455 // Helpers for streambuf iterators (either istream or ostream).
456 // NB: avoid including <iosfwd>, relatively large.
457 template<typename _CharT
>
460 template<typename _CharT
, typename _Traits
>
461 class istreambuf_iterator
;
463 template<typename _CharT
, typename _Traits
>
464 class ostreambuf_iterator
;
466 template<bool _IsMove
, typename _CharT
>
467 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
468 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
469 __copy_move_a2(_CharT
*, _CharT
*,
470 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
472 template<bool _IsMove
, typename _CharT
>
473 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
474 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
475 __copy_move_a2(const _CharT
*, const _CharT
*,
476 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
478 template<bool _IsMove
, typename _CharT
>
479 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
481 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
482 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
484 template<bool _IsMove
, typename _CharT
>
485 typename
__gnu_cxx::__enable_if
<
486 __is_char
<_CharT
>::__value
,
487 _GLIBCXX_STD_C::_Deque_iterator
<_CharT
, _CharT
&, _CharT
*> >::__type
489 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
490 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
491 _GLIBCXX_STD_C::_Deque_iterator
<_CharT
, _CharT
&, _CharT
*>);
494 template<bool _IsMove
, typename _II
, typename _OI
>
497 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
499 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
500 #ifdef __cpp_lib_is_constant_evaluated
501 if (std::is_constant_evaluated())
502 return std::__copy_move
<_IsMove
, false, _Category
>::
503 __copy_m(__first
, __last
, __result
);
505 return std::__copy_move
<_IsMove
, __memcpyable
<_OI
, _II
>::__value
,
506 _Category
>::__copy_m(__first
, __last
, __result
);
509 template<bool _IsMove
,
510 typename _Tp
, typename _Ref
, typename _Ptr
, typename _OI
>
512 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
513 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
516 template<bool _IsMove
,
517 typename _ITp
, typename _IRef
, typename _IPtr
, typename _OTp
>
518 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>
519 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
520 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
521 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>);
523 template<bool _IsMove
, typename _II
, typename _Tp
>
524 typename
__gnu_cxx::__enable_if
<
525 __is_random_access_iter
<_II
>::__value
,
526 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> >::__type
527 __copy_move_a1(_II
, _II
, _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
529 template<bool _IsMove
, typename _II
, typename _OI
>
532 __copy_move_a1(_II __first
, _II __last
, _OI __result
)
533 { return std::__copy_move_a2
<_IsMove
>(__first
, __last
, __result
); }
535 template<bool _IsMove
, typename _II
, typename _OI
>
538 __copy_move_a(_II __first
, _II __last
, _OI __result
)
540 return std::__niter_wrap(__result
,
541 std::__copy_move_a1
<_IsMove
>(std::__niter_base(__first
),
542 std::__niter_base(__last
),
543 std::__niter_base(__result
)));
546 template<bool _IsMove
,
547 typename _Ite
, typename _Seq
, typename _Cat
, typename _OI
>
549 __copy_move_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
550 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
553 template<bool _IsMove
,
554 typename _II
, typename _Ite
, typename _Seq
, typename _Cat
>
555 __gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
556 __copy_move_a(_II
, _II
,
557 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&);
559 template<bool _IsMove
,
560 typename _IIte
, typename _ISeq
, typename _ICat
,
561 typename _OIte
, typename _OSeq
, typename _OCat
>
562 ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>
563 __copy_move_a(const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
564 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
565 const ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>&);
567 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
570 __copy_n_a(_InputIterator __first
, _Size __n
, _OutputIterator __result
,
577 *__result
= *__first
;
589 template<typename _CharT
, typename _Size
>
590 typename
__gnu_cxx::__enable_if
<
591 __is_char
<_CharT
>::__value
, _CharT
*>::__type
592 __copy_n_a(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
593 _Size
, _CharT
*, bool);
595 template<typename _CharT
, typename _Size
>
596 typename
__gnu_cxx::__enable_if
<
597 __is_char
<_CharT
>::__value
,
598 _GLIBCXX_STD_C::_Deque_iterator
<_CharT
, _CharT
&, _CharT
*> >::__type
599 __copy_n_a(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _Size
,
600 _GLIBCXX_STD_C::_Deque_iterator
<_CharT
, _CharT
&, _CharT
*>,
605 * @brief Copies the range [first,last) into result.
606 * @ingroup mutating_algorithms
607 * @param __first An input iterator.
608 * @param __last An input iterator.
609 * @param __result An output iterator.
610 * @return result + (last - first)
612 * This inline function will boil down to a call to @c memmove whenever
613 * possible. Failing that, if random access iterators are passed, then the
614 * loop count will be known (and therefore a candidate for compiler
615 * optimizations such as unrolling). Result may not be contained within
616 * [first,last); the copy_backward function should be used instead.
618 * Note that the end of the output range is permitted to be contained
619 * within [first,last).
621 template<typename _II
, typename _OI
>
624 copy(_II __first
, _II __last
, _OI __result
)
626 // concept requirements
627 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
628 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
629 typename iterator_traits
<_II
>::reference
>)
630 __glibcxx_requires_can_increment_range(__first
, __last
, __result
);
632 return std::__copy_move_a
<__is_move_iterator
<_II
>::__value
>
633 (std::__miter_base(__first
), std::__miter_base(__last
), __result
);
636 #if __cplusplus >= 201103L
638 * @brief Moves the range [first,last) into result.
639 * @ingroup mutating_algorithms
640 * @param __first An input iterator.
641 * @param __last An input iterator.
642 * @param __result An output iterator.
643 * @return result + (last - first)
645 * This inline function will boil down to a call to @c memmove whenever
646 * possible. Failing that, if random access iterators are passed, then the
647 * loop count will be known (and therefore a candidate for compiler
648 * optimizations such as unrolling). Result may not be contained within
649 * [first,last); the move_backward function should be used instead.
651 * Note that the end of the output range is permitted to be contained
652 * within [first,last).
654 template<typename _II
, typename _OI
>
657 move(_II __first
, _II __last
, _OI __result
)
659 // concept requirements
660 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
661 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
662 typename iterator_traits
<_II
>::value_type
&&>)
663 __glibcxx_requires_can_increment_range(__first
, __last
, __result
);
665 return std::__copy_move_a
<true>(std::__miter_base(__first
),
666 std::__miter_base(__last
), __result
);
669 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
671 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
674 template<bool _IsMove
, bool _IsSimple
, typename _Category
>
675 struct __copy_move_backward
677 template<typename _BI1
, typename _BI2
>
680 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
682 while (__first
!= __last
)
683 *--__result
= *--__last
;
688 #if __cplusplus >= 201103L
689 template<typename _Category
>
690 struct __copy_move_backward
<true, false, _Category
>
692 template<typename _BI1
, typename _BI2
>
695 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
697 while (__first
!= __last
)
698 *--__result
= std::move(*--__last
);
705 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
707 template<typename _BI1
, typename _BI2
>
710 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
712 typename iterator_traits
<_BI1
>::difference_type
713 __n
= __last
- __first
;
714 for (; __n
> 0; --__n
)
715 *--__result
= *--__last
;
720 #if __cplusplus >= 201103L
722 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
724 template<typename _BI1
, typename _BI2
>
727 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
729 typename iterator_traits
<_BI1
>::difference_type
730 __n
= __last
- __first
;
731 for (; __n
> 0; --__n
)
732 *--__result
= std::move(*--__last
);
738 template<bool _IsMove
>
739 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
741 template<typename _Tp
, typename _Up
>
744 __copy_move_b(_Tp
* __first
, _Tp
* __last
, _Up
* __result
)
746 const ptrdiff_t _Num
= __last
- __first
;
747 if (__builtin_expect(_Num
> 1, true))
748 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
750 std::__copy_move
<_IsMove
, false, random_access_iterator_tag
>::
751 __assign_one(__result
- 1, __first
);
752 return __result
- _Num
;
756 template<bool _IsMove
, typename _BI1
, typename _BI2
>
759 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
761 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
762 #ifdef __cpp_lib_is_constant_evaluated
763 if (std::is_constant_evaluated())
764 return std::__copy_move_backward
<_IsMove
, false, _Category
>::
765 __copy_move_b(__first
, __last
, __result
);
767 return std::__copy_move_backward
<_IsMove
,
768 __memcpyable
<_BI2
, _BI1
>::__value
,
769 _Category
>::__copy_move_b(__first
,
774 template<bool _IsMove
, typename _BI1
, typename _BI2
>
777 __copy_move_backward_a1(_BI1 __first
, _BI1 __last
, _BI2 __result
)
778 { return std::__copy_move_backward_a2
<_IsMove
>(__first
, __last
, __result
); }
780 template<bool _IsMove
,
781 typename _Tp
, typename _Ref
, typename _Ptr
, typename _OI
>
783 __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
784 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
787 template<bool _IsMove
,
788 typename _ITp
, typename _IRef
, typename _IPtr
, typename _OTp
>
789 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>
790 __copy_move_backward_a1(
791 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
792 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
793 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>);
795 template<bool _IsMove
, typename _II
, typename _Tp
>
796 typename
__gnu_cxx::__enable_if
<
797 __is_random_access_iter
<_II
>::__value
,
798 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> >::__type
799 __copy_move_backward_a1(_II
, _II
,
800 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
802 template<bool _IsMove
, typename _II
, typename _OI
>
805 __copy_move_backward_a(_II __first
, _II __last
, _OI __result
)
807 return std::__niter_wrap(__result
,
808 std::__copy_move_backward_a1
<_IsMove
>
809 (std::__niter_base(__first
), std::__niter_base(__last
),
810 std::__niter_base(__result
)));
813 template<bool _IsMove
,
814 typename _Ite
, typename _Seq
, typename _Cat
, typename _OI
>
816 __copy_move_backward_a(
817 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
818 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
821 template<bool _IsMove
,
822 typename _II
, typename _Ite
, typename _Seq
, typename _Cat
>
823 __gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
824 __copy_move_backward_a(_II
, _II
,
825 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&);
827 template<bool _IsMove
,
828 typename _IIte
, typename _ISeq
, typename _ICat
,
829 typename _OIte
, typename _OSeq
, typename _OCat
>
830 ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>
831 __copy_move_backward_a(
832 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
833 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
834 const ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>&);
837 * @brief Copies the range [first,last) into result.
838 * @ingroup mutating_algorithms
839 * @param __first A bidirectional iterator.
840 * @param __last A bidirectional iterator.
841 * @param __result A bidirectional iterator.
842 * @return result - (last - first)
844 * The function has the same effect as copy, but starts at the end of the
845 * range and works its way to the start, returning the start of the result.
846 * This inline function will boil down to a call to @c memmove whenever
847 * possible. Failing that, if random access iterators are passed, then the
848 * loop count will be known (and therefore a candidate for compiler
849 * optimizations such as unrolling).
851 * Result may not be in the range (first,last]. Use copy instead. Note
852 * that the start of the output range may overlap [first,last).
854 template<typename _BI1
, typename _BI2
>
857 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
859 // concept requirements
860 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
861 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
862 __glibcxx_function_requires(_OutputIteratorConcept
<_BI2
,
863 typename iterator_traits
<_BI1
>::reference
>)
864 __glibcxx_requires_can_decrement_range(__first
, __last
, __result
);
866 return std::__copy_move_backward_a
<__is_move_iterator
<_BI1
>::__value
>
867 (std::__miter_base(__first
), std::__miter_base(__last
), __result
);
870 #if __cplusplus >= 201103L
872 * @brief Moves the range [first,last) into result.
873 * @ingroup mutating_algorithms
874 * @param __first A bidirectional iterator.
875 * @param __last A bidirectional iterator.
876 * @param __result A bidirectional iterator.
877 * @return result - (last - first)
879 * The function has the same effect as move, but starts at the end of the
880 * range and works its way to the start, returning the start of the result.
881 * This inline function will boil down to a call to @c memmove whenever
882 * possible. Failing that, if random access iterators are passed, then the
883 * loop count will be known (and therefore a candidate for compiler
884 * optimizations such as unrolling).
886 * Result may not be in the range (first,last]. Use move instead. Note
887 * that the start of the output range may overlap [first,last).
889 template<typename _BI1
, typename _BI2
>
892 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
894 // concept requirements
895 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
896 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
897 __glibcxx_function_requires(_OutputIteratorConcept
<_BI2
,
898 typename iterator_traits
<_BI1
>::value_type
&&>)
899 __glibcxx_requires_can_decrement_range(__first
, __last
, __result
);
901 return std::__copy_move_backward_a
<true>(std::__miter_base(__first
),
902 std::__miter_base(__last
),
906 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
908 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
911 template<typename _ForwardIterator
, typename _Tp
>
914 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
915 __fill_a1(_ForwardIterator __first
, _ForwardIterator __last
,
918 for (; __first
!= __last
; ++__first
)
922 template<typename _ForwardIterator
, typename _Tp
>
925 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
926 __fill_a1(_ForwardIterator __first
, _ForwardIterator __last
,
929 const _Tp __tmp
= __value
;
930 for (; __first
!= __last
; ++__first
)
934 // Specialization: for char types we can use memset.
935 template<typename _Tp
>
938 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
939 __fill_a1(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
941 const _Tp __tmp
= __c
;
942 #if __cpp_lib_is_constant_evaluated
943 if (std::is_constant_evaluated())
945 for (; __first
!= __last
; ++__first
)
950 if (const size_t __len
= __last
- __first
)
951 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
), __len
);
954 template<typename _Ite
, typename _Cont
, typename _Tp
>
957 __fill_a1(::__gnu_cxx::__normal_iterator
<_Ite
, _Cont
> __first
,
958 ::__gnu_cxx::__normal_iterator
<_Ite
, _Cont
> __last
,
960 { std::__fill_a1(__first
.base(), __last
.base(), __value
); }
962 template<typename _Tp
, typename _VTp
>
964 __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
965 const _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
970 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator
, _GLIBCXX_STD_C::_Bit_iterator
,
973 template<typename _FIte
, typename _Tp
>
976 __fill_a(_FIte __first
, _FIte __last
, const _Tp
& __value
)
977 { std::__fill_a1(__first
, __last
, __value
); }
979 template<typename _Ite
, typename _Seq
, typename _Cat
, typename _Tp
>
981 __fill_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
982 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
986 * @brief Fills the range [first,last) with copies of value.
987 * @ingroup mutating_algorithms
988 * @param __first A forward iterator.
989 * @param __last A forward iterator.
990 * @param __value A reference-to-const of arbitrary type.
993 * This function fills a range with copies of the same value. For char
994 * types filling contiguous areas of memory, this becomes an inline call
995 * to @c memset or @c wmemset.
997 template<typename _ForwardIterator
, typename _Tp
>
1000 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
1002 // concept requirements
1003 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1005 __glibcxx_requires_valid_range(__first
, __last
);
1007 std::__fill_a(__first
, __last
, __value
);
1010 // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
1011 inline _GLIBCXX_CONSTEXPR
int
1012 __size_to_integer(int __n
) { return __n
; }
1013 inline _GLIBCXX_CONSTEXPR
unsigned
1014 __size_to_integer(unsigned __n
) { return __n
; }
1015 inline _GLIBCXX_CONSTEXPR
long
1016 __size_to_integer(long __n
) { return __n
; }
1017 inline _GLIBCXX_CONSTEXPR
unsigned long
1018 __size_to_integer(unsigned long __n
) { return __n
; }
1019 inline _GLIBCXX_CONSTEXPR
long long
1020 __size_to_integer(long long __n
) { return __n
; }
1021 inline _GLIBCXX_CONSTEXPR
unsigned long long
1022 __size_to_integer(unsigned long long __n
) { return __n
; }
1024 #if defined(__GLIBCXX_TYPE_INT_N_0)
1025 __extension__
inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
1026 __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n
) { return __n
; }
1027 __extension__
inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_0
1028 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n
) { return __n
; }
1030 #if defined(__GLIBCXX_TYPE_INT_N_1)
1031 __extension__
inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
1032 __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n
) { return __n
; }
1033 __extension__
inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_1
1034 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n
) { return __n
; }
1036 #if defined(__GLIBCXX_TYPE_INT_N_2)
1037 __extension__
inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
1038 __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n
) { return __n
; }
1039 __extension__
inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_2
1040 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n
) { return __n
; }
1042 #if defined(__GLIBCXX_TYPE_INT_N_3)
1043 __extension__
inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_3
1044 __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n
) { return __n
; }
1045 __extension__
inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
1046 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n
) { return __n
; }
1049 inline _GLIBCXX_CONSTEXPR
long long
1050 __size_to_integer(float __n
) { return (long long)__n
; }
1051 inline _GLIBCXX_CONSTEXPR
long long
1052 __size_to_integer(double __n
) { return (long long)__n
; }
1053 inline _GLIBCXX_CONSTEXPR
long long
1054 __size_to_integer(long double __n
) { return (long long)__n
; }
1055 #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
1056 __extension__
inline _GLIBCXX_CONSTEXPR
long long
1057 __size_to_integer(__float128 __n
) { return (long long)__n
; }
1060 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1061 _GLIBCXX20_CONSTEXPR
1063 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
1064 __fill_n_a1(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
1066 for (; __n
> 0; --__n
, (void) ++__first
)
1071 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1072 _GLIBCXX20_CONSTEXPR
1074 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
1075 __fill_n_a1(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
1077 const _Tp __tmp
= __value
;
1078 for (; __n
> 0; --__n
, (void) ++__first
)
1083 template<typename _Ite
, typename _Seq
, typename _Cat
, typename _Size
,
1085 ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
1086 __fill_n_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>& __first
,
1087 _Size __n
, const _Tp
& __value
,
1088 std::input_iterator_tag
);
1090 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1091 _GLIBCXX20_CONSTEXPR
1092 inline _OutputIterator
1093 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1094 std::output_iterator_tag
)
1096 #if __cplusplus >= 201103L
1097 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1099 return __fill_n_a1(__first
, __n
, __value
);
1102 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1103 _GLIBCXX20_CONSTEXPR
1104 inline _OutputIterator
1105 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1106 std::input_iterator_tag
)
1108 #if __cplusplus >= 201103L
1109 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1111 return __fill_n_a1(__first
, __n
, __value
);
1114 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1115 _GLIBCXX20_CONSTEXPR
1116 inline _OutputIterator
1117 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1118 std::random_access_iterator_tag
)
1120 #if __cplusplus >= 201103L
1121 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1126 __glibcxx_requires_can_increment(__first
, __n
);
1128 std::__fill_a(__first
, __first
+ __n
, __value
);
1129 return __first
+ __n
;
1133 * @brief Fills the range [first,first+n) with copies of value.
1134 * @ingroup mutating_algorithms
1135 * @param __first An output iterator.
1136 * @param __n The count of copies to perform.
1137 * @param __value A reference-to-const of arbitrary type.
1138 * @return The iterator at first+n.
1140 * This function fills a range with copies of the same value. For char
1141 * types filling contiguous areas of memory, this becomes an inline call
1142 * to @c memset or @c wmemset.
1144 * If @p __n is negative, the function does nothing.
1146 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1147 // DR 865. More algorithms that throw away information
1148 // DR 426. search_n(), fill_n(), and generate_n() with negative n
1149 template<typename _OI
, typename _Size
, typename _Tp
>
1150 _GLIBCXX20_CONSTEXPR
1152 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
1154 // concept requirements
1155 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, const _Tp
&>)
1157 return std::__fill_n_a(__first
, std::__size_to_integer(__n
), __value
,
1158 std::__iterator_category(__first
));
1161 template<bool _BoolType
>
1164 template<typename _II1
, typename _II2
>
1165 _GLIBCXX20_CONSTEXPR
1167 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1169 for (; __first1
!= __last1
; ++__first1
, (void) ++__first2
)
1170 if (!(*__first1
== *__first2
))
1177 struct __equal
<true>
1179 template<typename _Tp
>
1180 _GLIBCXX20_CONSTEXPR
1182 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
1184 if (const size_t __len
= (__last1
- __first1
))
1185 return !std::__memcmp(__first1
, __first2
, __len
);
1190 template<typename _Tp
, typename _Ref
, typename _Ptr
, typename _II
>
1191 typename
__gnu_cxx::__enable_if
<
1192 __is_random_access_iter
<_II
>::__value
, bool>::__type
1193 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
1194 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
1197 template<typename _Tp1
, typename _Ref1
, typename _Ptr1
,
1198 typename _Tp2
, typename _Ref2
, typename _Ptr2
>
1200 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1201 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1202 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>);
1204 template<typename _II
, typename _Tp
, typename _Ref
, typename _Ptr
>
1205 typename
__gnu_cxx::__enable_if
<
1206 __is_random_access_iter
<_II
>::__value
, bool>::__type
1207 __equal_aux1(_II
, _II
,
1208 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>);
1210 template<typename _II1
, typename _II2
>
1211 _GLIBCXX20_CONSTEXPR
1213 __equal_aux1(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1215 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1216 const bool __simple
= ((__is_integer
<_ValueType1
>::__value
1217 || __is_pointer
<_ValueType1
>::__value
)
1218 && __memcmpable
<_II1
, _II2
>::__value
);
1219 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
1222 template<typename _II1
, typename _II2
>
1223 _GLIBCXX20_CONSTEXPR
1225 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1227 return std::__equal_aux1(std::__niter_base(__first1
),
1228 std::__niter_base(__last1
),
1229 std::__niter_base(__first2
));
1232 template<typename _II1
, typename _Seq1
, typename _Cat1
, typename _II2
>
1234 __equal_aux(const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1235 const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1238 template<typename _II1
, typename _II2
, typename _Seq2
, typename _Cat2
>
1240 __equal_aux(_II1
, _II1
,
1241 const ::__gnu_debug::_Safe_iterator
<_II2
, _Seq2
, _Cat2
>&);
1243 template<typename _II1
, typename _Seq1
, typename _Cat1
,
1244 typename _II2
, typename _Seq2
, typename _Cat2
>
1246 __equal_aux(const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1247 const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1248 const ::__gnu_debug::_Safe_iterator
<_II2
, _Seq2
, _Cat2
>&);
1250 template<typename
, typename
>
1253 template<typename _II1
, typename _II2
>
1254 _GLIBCXX20_CONSTEXPR
1256 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
1259 template<typename _II
>
1260 _GLIBCXX20_CONSTEXPR
1262 __cnd2(_II __first
, _II __last
)
1263 { return __first
!= __last
; }
1267 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
1269 template<typename _RAI1
, typename _RAI2
>
1270 _GLIBCXX20_CONSTEXPR
1272 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
1273 _RAI2 __first2
, _RAI2 __last2
)
1275 const typename iterator_traits
<_RAI1
>::difference_type
1276 __diff1
= __last1
- __first1
;
1277 const typename iterator_traits
<_RAI2
>::difference_type
1278 __diff2
= __last2
- __first2
;
1279 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
1282 template<typename _RAI
>
1283 static _GLIBCXX20_CONSTEXPR
bool
1288 template<typename _II1
, typename _II2
, typename _Compare
>
1289 _GLIBCXX20_CONSTEXPR
1291 __lexicographical_compare_impl(_II1 __first1
, _II1 __last1
,
1292 _II2 __first2
, _II2 __last2
,
1295 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1296 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1297 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1299 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1300 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1301 ++__first1
, (void)++__first2
)
1303 if (__comp(__first1
, __first2
))
1305 if (__comp(__first2
, __first1
))
1308 return __first1
== __last1
&& __first2
!= __last2
;
1311 template<bool _BoolType
>
1312 struct __lexicographical_compare
1314 template<typename _II1
, typename _II2
>
1315 _GLIBCXX20_CONSTEXPR
1317 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1319 using __gnu_cxx::__ops::__iter_less_iter
;
1320 return std::__lexicographical_compare_impl(__first1
, __last1
,
1322 __iter_less_iter());
1325 template<typename _II1
, typename _II2
>
1326 _GLIBCXX20_CONSTEXPR
1328 __3way(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1330 while (__first1
!= __last1
)
1332 if (__first2
== __last2
)
1334 if (*__first1
< *__first2
)
1336 if (*__first2
< *__first1
)
1341 return int(__first2
== __last2
) - 1;
1346 struct __lexicographical_compare
<true>
1348 template<typename _Tp
, typename _Up
>
1349 _GLIBCXX20_CONSTEXPR
1351 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
1352 const _Up
* __first2
, const _Up
* __last2
)
1353 { return __3way(__first1
, __last1
, __first2
, __last2
) < 0; }
1355 template<typename _Tp
, typename _Up
>
1356 _GLIBCXX20_CONSTEXPR
1358 __3way(const _Tp
* __first1
, const _Tp
* __last1
,
1359 const _Up
* __first2
, const _Up
* __last2
)
1361 const size_t __len1
= __last1
- __first1
;
1362 const size_t __len2
= __last2
- __first2
;
1363 if (const size_t __len
= std::min(__len1
, __len2
))
1364 if (int __result
= std::__memcmp(__first1
, __first2
, __len
))
1366 return ptrdiff_t(__len1
- __len2
);
1370 template<typename _II1
, typename _II2
>
1371 _GLIBCXX20_CONSTEXPR
1373 __lexicographical_compare_aux1(_II1 __first1
, _II1 __last1
,
1374 _II2 __first2
, _II2 __last2
)
1376 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1377 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1378 const bool __simple
=
1379 (__is_memcmp_ordered_with
<_ValueType1
, _ValueType2
>::__value
1380 && __is_pointer
<_II1
>::__value
1381 && __is_pointer
<_II2
>::__value
1382 #if __cplusplus > 201703L && __glibcxx_concepts
1383 // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
1384 // so __is_byte<T> could be true, but we can't use memcmp with
1386 && !is_volatile_v
<remove_reference_t
<iter_reference_t
<_II1
>>>
1387 && !is_volatile_v
<remove_reference_t
<iter_reference_t
<_II2
>>>
1391 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
1395 template<typename _Tp1
, typename _Ref1
, typename _Ptr1
,
1398 __lexicographical_compare_aux1(
1399 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1400 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1403 template<typename _Tp1
,
1404 typename _Tp2
, typename _Ref2
, typename _Ptr2
>
1406 __lexicographical_compare_aux1(_Tp1
*, _Tp1
*,
1407 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>,
1408 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>);
1410 template<typename _Tp1
, typename _Ref1
, typename _Ptr1
,
1411 typename _Tp2
, typename _Ref2
, typename _Ptr2
>
1413 __lexicographical_compare_aux1(
1414 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1415 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1416 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>,
1417 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>);
1419 template<typename _II1
, typename _II2
>
1420 _GLIBCXX20_CONSTEXPR
1422 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
1423 _II2 __first2
, _II2 __last2
)
1425 return std::__lexicographical_compare_aux1(std::__niter_base(__first1
),
1426 std::__niter_base(__last1
),
1427 std::__niter_base(__first2
),
1428 std::__niter_base(__last2
));
1431 template<typename _Iter1
, typename _Seq1
, typename _Cat1
,
1434 __lexicographical_compare_aux(
1435 const ::__gnu_debug::_Safe_iterator
<_Iter1
, _Seq1
, _Cat1
>&,
1436 const ::__gnu_debug::_Safe_iterator
<_Iter1
, _Seq1
, _Cat1
>&,
1439 template<typename _II1
,
1440 typename _Iter2
, typename _Seq2
, typename _Cat2
>
1442 __lexicographical_compare_aux(
1444 const ::__gnu_debug::_Safe_iterator
<_Iter2
, _Seq2
, _Cat2
>&,
1445 const ::__gnu_debug::_Safe_iterator
<_Iter2
, _Seq2
, _Cat2
>&);
1447 template<typename _Iter1
, typename _Seq1
, typename _Cat1
,
1448 typename _Iter2
, typename _Seq2
, typename _Cat2
>
1450 __lexicographical_compare_aux(
1451 const ::__gnu_debug::_Safe_iterator
<_Iter1
, _Seq1
, _Cat1
>&,
1452 const ::__gnu_debug::_Safe_iterator
<_Iter1
, _Seq1
, _Cat1
>&,
1453 const ::__gnu_debug::_Safe_iterator
<_Iter2
, _Seq2
, _Cat2
>&,
1454 const ::__gnu_debug::_Safe_iterator
<_Iter2
, _Seq2
, _Cat2
>&);
1456 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1457 _GLIBCXX20_CONSTEXPR
1459 __lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1460 const _Tp
& __val
, _Compare __comp
)
1462 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1465 _DistanceType __len
= std::distance(__first
, __last
);
1469 _DistanceType __half
= __len
>> 1;
1470 _ForwardIterator __middle
= __first
;
1471 std::advance(__middle
, __half
);
1472 if (__comp(__middle
, __val
))
1476 __len
= __len
- __half
- 1;
1485 * @brief Finds the first position in which @a val could be inserted
1486 * without changing the ordering.
1487 * @param __first An iterator.
1488 * @param __last Another iterator.
1489 * @param __val The search term.
1490 * @return An iterator pointing to the first element <em>not less
1491 * than</em> @a val, or end() if every element is less than
1493 * @ingroup binary_search_algorithms
1495 template<typename _ForwardIterator
, typename _Tp
>
1496 _GLIBCXX20_CONSTEXPR
1497 inline _ForwardIterator
1498 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1501 // concept requirements
1502 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1503 __glibcxx_function_requires(_LessThanOpConcept
<
1504 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1505 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
1507 return std::__lower_bound(__first
, __last
, __val
,
1508 __gnu_cxx::__ops::__iter_less_val());
1511 /// This is a helper function for the sort routines and for random.tcc.
1512 // Precondition: __n > 0.
1513 template<typename _Tp
>
1514 inline _GLIBCXX_CONSTEXPR _Tp
1517 #if __cplusplus >= 201402L
1518 return std::__bit_width(make_unsigned_t
<_Tp
>(__n
)) - 1;
1520 // Use +__n so it promotes to at least int.
1521 return (sizeof(+__n
) * __CHAR_BIT__
- 1)
1522 - (sizeof(+__n
) == sizeof(long long)
1523 ? __builtin_clzll(+__n
)
1524 : (sizeof(+__n
) == sizeof(long)
1525 ? __builtin_clzl(+__n
)
1526 : __builtin_clz(+__n
)));
1530 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1533 * @brief Tests a range for element-wise equality.
1534 * @ingroup non_mutating_algorithms
1535 * @param __first1 An input iterator.
1536 * @param __last1 An input iterator.
1537 * @param __first2 An input iterator.
1538 * @return A boolean true or false.
1540 * This compares the elements of two ranges using @c == and returns true or
1541 * false depending on whether all of the corresponding elements of the
1544 template<typename _II1
, typename _II2
>
1545 _GLIBCXX20_CONSTEXPR
1547 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1549 // concept requirements
1550 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1551 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1552 __glibcxx_function_requires(_EqualOpConcept
<
1553 typename iterator_traits
<_II1
>::value_type
,
1554 typename iterator_traits
<_II2
>::value_type
>)
1555 __glibcxx_requires_can_increment_range(__first1
, __last1
, __first2
);
1557 return std::__equal_aux(__first1
, __last1
, __first2
);
1561 * @brief Tests a range for element-wise equality.
1562 * @ingroup non_mutating_algorithms
1563 * @param __first1 An input iterator.
1564 * @param __last1 An input iterator.
1565 * @param __first2 An input iterator.
1566 * @param __binary_pred A binary predicate @link functors
1568 * @return A boolean true or false.
1570 * This compares the elements of two ranges using the binary_pred
1571 * parameter, and returns true or
1572 * false depending on whether all of the corresponding elements of the
1575 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1576 _GLIBCXX20_CONSTEXPR
1578 equal(_IIter1 __first1
, _IIter1 __last1
,
1579 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
1581 // concept requirements
1582 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1583 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1584 __glibcxx_requires_valid_range(__first1
, __last1
);
1586 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
1587 if (!bool(__binary_pred(*__first1
, *__first2
)))
1592 #if __cplusplus >= 201103L
1593 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1594 template<typename _II1
, typename _II2
>
1595 _GLIBCXX20_CONSTEXPR
1597 __equal4(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1599 using _RATag
= random_access_iterator_tag
;
1600 using _Cat1
= typename iterator_traits
<_II1
>::iterator_category
;
1601 using _Cat2
= typename iterator_traits
<_II2
>::iterator_category
;
1602 using _RAIters
= __and_
<is_same
<_Cat1
, _RATag
>, is_same
<_Cat2
, _RATag
>>;
1605 auto __d1
= std::distance(__first1
, __last1
);
1606 auto __d2
= std::distance(__first2
, __last2
);
1609 return _GLIBCXX_STD_A::equal(__first1
, __last1
, __first2
);
1612 for (; __first1
!= __last1
&& __first2
!= __last2
;
1613 ++__first1
, (void)++__first2
)
1614 if (!(*__first1
== *__first2
))
1616 return __first1
== __last1
&& __first2
== __last2
;
1619 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1620 template<typename _II1
, typename _II2
, typename _BinaryPredicate
>
1621 _GLIBCXX20_CONSTEXPR
1623 __equal4(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
,
1624 _BinaryPredicate __binary_pred
)
1626 using _RATag
= random_access_iterator_tag
;
1627 using _Cat1
= typename iterator_traits
<_II1
>::iterator_category
;
1628 using _Cat2
= typename iterator_traits
<_II2
>::iterator_category
;
1629 using _RAIters
= __and_
<is_same
<_Cat1
, _RATag
>, is_same
<_Cat2
, _RATag
>>;
1632 auto __d1
= std::distance(__first1
, __last1
);
1633 auto __d2
= std::distance(__first2
, __last2
);
1636 return _GLIBCXX_STD_A::equal(__first1
, __last1
, __first2
,
1640 for (; __first1
!= __last1
&& __first2
!= __last2
;
1641 ++__first1
, (void)++__first2
)
1642 if (!bool(__binary_pred(*__first1
, *__first2
)))
1644 return __first1
== __last1
&& __first2
== __last2
;
1648 #ifdef __glibcxx_robust_nonmodifying_seq_ops // C++ >= 14
1650 * @brief Tests a range for element-wise equality.
1651 * @ingroup non_mutating_algorithms
1652 * @param __first1 An input iterator.
1653 * @param __last1 An input iterator.
1654 * @param __first2 An input iterator.
1655 * @param __last2 An input iterator.
1656 * @return A boolean true or false.
1658 * This compares the elements of two ranges using @c == and returns true or
1659 * false depending on whether all of the corresponding elements of the
1662 template<typename _II1
, typename _II2
>
1663 _GLIBCXX20_CONSTEXPR
1665 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1667 // concept requirements
1668 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1669 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1670 __glibcxx_function_requires(_EqualOpConcept
<
1671 typename iterator_traits
<_II1
>::value_type
,
1672 typename iterator_traits
<_II2
>::value_type
>)
1673 __glibcxx_requires_valid_range(__first1
, __last1
);
1674 __glibcxx_requires_valid_range(__first2
, __last2
);
1676 return _GLIBCXX_STD_A::__equal4(__first1
, __last1
, __first2
, __last2
);
1680 * @brief Tests a range for element-wise equality.
1681 * @ingroup non_mutating_algorithms
1682 * @param __first1 An input iterator.
1683 * @param __last1 An input iterator.
1684 * @param __first2 An input iterator.
1685 * @param __last2 An input iterator.
1686 * @param __binary_pred A binary predicate @link functors
1688 * @return A boolean true or false.
1690 * This compares the elements of two ranges using the binary_pred
1691 * parameter, and returns true or
1692 * false depending on whether all of the corresponding elements of the
1695 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1696 _GLIBCXX20_CONSTEXPR
1698 equal(_IIter1 __first1
, _IIter1 __last1
,
1699 _IIter2 __first2
, _IIter2 __last2
, _BinaryPredicate __binary_pred
)
1701 // concept requirements
1702 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1703 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1704 __glibcxx_requires_valid_range(__first1
, __last1
);
1705 __glibcxx_requires_valid_range(__first2
, __last2
);
1707 return _GLIBCXX_STD_A::__equal4(__first1
, __last1
, __first2
, __last2
,
1710 #endif // __glibcxx_robust_nonmodifying_seq_ops
1713 * @brief Performs @b dictionary comparison on ranges.
1714 * @ingroup sorting_algorithms
1715 * @param __first1 An input iterator.
1716 * @param __last1 An input iterator.
1717 * @param __first2 An input iterator.
1718 * @param __last2 An input iterator.
1719 * @return A boolean true or false.
1721 * <em>Returns true if the sequence of elements defined by the range
1722 * [first1,last1) is lexicographically less than the sequence of elements
1723 * defined by the range [first2,last2). Returns false otherwise.</em>
1724 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1725 * then this is an inline call to @c memcmp.
1727 template<typename _II1
, typename _II2
>
1728 _GLIBCXX20_CONSTEXPR
1730 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1731 _II2 __first2
, _II2 __last2
)
1733 #ifdef _GLIBCXX_CONCEPT_CHECKS
1734 // concept requirements
1735 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1736 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1738 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1739 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1740 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1741 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1742 __glibcxx_requires_valid_range(__first1
, __last1
);
1743 __glibcxx_requires_valid_range(__first2
, __last2
);
1745 return std::__lexicographical_compare_aux(__first1
, __last1
,
1750 * @brief Performs @b dictionary comparison on ranges.
1751 * @ingroup sorting_algorithms
1752 * @param __first1 An input iterator.
1753 * @param __last1 An input iterator.
1754 * @param __first2 An input iterator.
1755 * @param __last2 An input iterator.
1756 * @param __comp A @link comparison_functors comparison functor@endlink.
1757 * @return A boolean true or false.
1759 * The same as the four-parameter @c lexicographical_compare, but uses the
1760 * comp parameter instead of @c <.
1762 template<typename _II1
, typename _II2
, typename _Compare
>
1763 _GLIBCXX20_CONSTEXPR
1765 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1766 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1768 // concept requirements
1769 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1770 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1771 __glibcxx_requires_valid_range(__first1
, __last1
);
1772 __glibcxx_requires_valid_range(__first2
, __last2
);
1774 return std::__lexicographical_compare_impl
1775 (__first1
, __last1
, __first2
, __last2
,
1776 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1779 #if __cpp_lib_three_way_comparison
1780 // Iter points to a contiguous range of unsigned narrow character type
1781 // or std::byte, suitable for comparison by memcmp.
1782 template<typename _Iter
>
1783 concept __is_byte_iter
= contiguous_iterator
<_Iter
>
1784 && __is_memcmp_ordered
<iter_value_t
<_Iter
>>::__value
;
1786 // Return a struct with two members, initialized to the smaller of x and y
1787 // (or x if they compare equal) and the result of the comparison x <=> y.
1788 template<typename _Tp
>
1790 __min_cmp(_Tp __x
, _Tp __y
)
1794 decltype(__x
<=> __y
) _M_cmp
;
1796 auto __c
= __x
<=> __y
;
1798 return _Res
{__y
, __c
};
1799 return _Res
{__x
, __c
};
1803 * @brief Performs dictionary comparison on ranges.
1804 * @ingroup sorting_algorithms
1805 * @param __first1 An input iterator.
1806 * @param __last1 An input iterator.
1807 * @param __first2 An input iterator.
1808 * @param __last2 An input iterator.
1809 * @param __comp A @link comparison_functors comparison functor@endlink.
1810 * @return The comparison category that `__comp(*__first1, *__first2)`
1813 template<typename _InputIter1
, typename _InputIter2
, typename _Comp
>
1815 lexicographical_compare_three_way(_InputIter1 __first1
,
1816 _InputIter1 __last1
,
1817 _InputIter2 __first2
,
1818 _InputIter2 __last2
,
1820 -> decltype(__comp(*__first1
, *__first2
))
1822 // concept requirements
1823 __glibcxx_function_requires(_InputIteratorConcept
<_InputIter1
>)
1824 __glibcxx_function_requires(_InputIteratorConcept
<_InputIter2
>)
1825 __glibcxx_requires_valid_range(__first1
, __last1
);
1826 __glibcxx_requires_valid_range(__first2
, __last2
);
1828 using _Cat
= decltype(__comp(*__first1
, *__first2
));
1829 static_assert(same_as
<common_comparison_category_t
<_Cat
>, _Cat
>);
1831 if (!std::__is_constant_evaluated())
1832 if constexpr (same_as
<_Comp
, __detail::_Synth3way
>
1833 || same_as
<_Comp
, compare_three_way
>)
1834 if constexpr (__is_byte_iter
<_InputIter1
>)
1835 if constexpr (__is_byte_iter
<_InputIter2
>)
1837 const auto [__len
, __lencmp
] = _GLIBCXX_STD_A::
1838 __min_cmp(__last1
- __first1
, __last2
- __first2
);
1842 = __builtin_memcmp(&*__first1
, &*__first2
, __len
) <=> 0;
1849 while (__first1
!= __last1
)
1851 if (__first2
== __last2
)
1852 return strong_ordering::greater
;
1853 if (auto __cmp
= __comp(*__first1
, *__first2
); __cmp
!= 0)
1858 return (__first2
== __last2
) <=> true; // See PR 94006
1861 template<typename _InputIter1
, typename _InputIter2
>
1863 lexicographical_compare_three_way(_InputIter1 __first1
,
1864 _InputIter1 __last1
,
1865 _InputIter2 __first2
,
1866 _InputIter2 __last2
)
1868 return _GLIBCXX_STD_A::
1869 lexicographical_compare_three_way(__first1
, __last1
, __first2
, __last2
,
1870 compare_three_way
{});
1872 #endif // three_way_comparison
1874 template<typename _InputIterator1
, typename _InputIterator2
,
1875 typename _BinaryPredicate
>
1876 _GLIBCXX20_CONSTEXPR
1877 pair
<_InputIterator1
, _InputIterator2
>
1878 __mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1879 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1881 while (__first1
!= __last1
&& __binary_pred(__first1
, __first2
))
1886 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1890 * @brief Finds the places in ranges which don't match.
1891 * @ingroup non_mutating_algorithms
1892 * @param __first1 An input iterator.
1893 * @param __last1 An input iterator.
1894 * @param __first2 An input iterator.
1895 * @return A pair of iterators pointing to the first mismatch.
1897 * This compares the elements of two ranges using @c == and returns a pair
1898 * of iterators. The first iterator points into the first range, the
1899 * second iterator points into the second range, and the elements pointed
1900 * to by the iterators are not equal.
1902 template<typename _InputIterator1
, typename _InputIterator2
>
1903 _GLIBCXX20_CONSTEXPR
1904 inline pair
<_InputIterator1
, _InputIterator2
>
1905 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1906 _InputIterator2 __first2
)
1908 // concept requirements
1909 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1910 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1911 __glibcxx_function_requires(_EqualOpConcept
<
1912 typename iterator_traits
<_InputIterator1
>::value_type
,
1913 typename iterator_traits
<_InputIterator2
>::value_type
>)
1914 __glibcxx_requires_valid_range(__first1
, __last1
);
1916 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
,
1917 __gnu_cxx::__ops::__iter_equal_to_iter());
1921 * @brief Finds the places in ranges which don't match.
1922 * @ingroup non_mutating_algorithms
1923 * @param __first1 An input iterator.
1924 * @param __last1 An input iterator.
1925 * @param __first2 An input iterator.
1926 * @param __binary_pred A binary predicate @link functors
1928 * @return A pair of iterators pointing to the first mismatch.
1930 * This compares the elements of two ranges using the binary_pred
1931 * parameter, and returns a pair
1932 * of iterators. The first iterator points into the first range, the
1933 * second iterator points into the second range, and the elements pointed
1934 * to by the iterators are not equal.
1936 template<typename _InputIterator1
, typename _InputIterator2
,
1937 typename _BinaryPredicate
>
1938 _GLIBCXX20_CONSTEXPR
1939 inline pair
<_InputIterator1
, _InputIterator2
>
1940 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1941 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1943 // concept requirements
1944 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1945 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1946 __glibcxx_requires_valid_range(__first1
, __last1
);
1948 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
,
1949 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1952 #if __glibcxx_robust_nonmodifying_seq_ops // C++ >= 14
1953 template<typename _InputIterator1
, typename _InputIterator2
,
1954 typename _BinaryPredicate
>
1955 _GLIBCXX20_CONSTEXPR
1956 pair
<_InputIterator1
, _InputIterator2
>
1957 __mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1958 _InputIterator2 __first2
, _InputIterator2 __last2
,
1959 _BinaryPredicate __binary_pred
)
1961 while (__first1
!= __last1
&& __first2
!= __last2
1962 && __binary_pred(__first1
, __first2
))
1967 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1971 * @brief Finds the places in ranges which don't match.
1972 * @ingroup non_mutating_algorithms
1973 * @param __first1 An input iterator.
1974 * @param __last1 An input iterator.
1975 * @param __first2 An input iterator.
1976 * @param __last2 An input iterator.
1977 * @return A pair of iterators pointing to the first mismatch.
1979 * This compares the elements of two ranges using @c == and returns a pair
1980 * of iterators. The first iterator points into the first range, the
1981 * second iterator points into the second range, and the elements pointed
1982 * to by the iterators are not equal.
1984 template<typename _InputIterator1
, typename _InputIterator2
>
1985 _GLIBCXX20_CONSTEXPR
1986 inline pair
<_InputIterator1
, _InputIterator2
>
1987 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1988 _InputIterator2 __first2
, _InputIterator2 __last2
)
1990 // concept requirements
1991 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1992 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1993 __glibcxx_function_requires(_EqualOpConcept
<
1994 typename iterator_traits
<_InputIterator1
>::value_type
,
1995 typename iterator_traits
<_InputIterator2
>::value_type
>)
1996 __glibcxx_requires_valid_range(__first1
, __last1
);
1997 __glibcxx_requires_valid_range(__first2
, __last2
);
1999 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
, __last2
,
2000 __gnu_cxx::__ops::__iter_equal_to_iter());
2004 * @brief Finds the places in ranges which don't match.
2005 * @ingroup non_mutating_algorithms
2006 * @param __first1 An input iterator.
2007 * @param __last1 An input iterator.
2008 * @param __first2 An input iterator.
2009 * @param __last2 An input iterator.
2010 * @param __binary_pred A binary predicate @link functors
2012 * @return A pair of iterators pointing to the first mismatch.
2014 * This compares the elements of two ranges using the binary_pred
2015 * parameter, and returns a pair
2016 * of iterators. The first iterator points into the first range, the
2017 * second iterator points into the second range, and the elements pointed
2018 * to by the iterators are not equal.
2020 template<typename _InputIterator1
, typename _InputIterator2
,
2021 typename _BinaryPredicate
>
2022 _GLIBCXX20_CONSTEXPR
2023 inline pair
<_InputIterator1
, _InputIterator2
>
2024 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
2025 _InputIterator2 __first2
, _InputIterator2 __last2
,
2026 _BinaryPredicate __binary_pred
)
2028 // concept requirements
2029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2030 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2031 __glibcxx_requires_valid_range(__first1
, __last1
);
2032 __glibcxx_requires_valid_range(__first2
, __last2
);
2034 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
, __last2
,
2035 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
2039 _GLIBCXX_END_NAMESPACE_ALGO
2041 /// This is an overload used by find algos for the Input Iterator case.
2042 template<typename _InputIterator
, typename _Predicate
>
2043 _GLIBCXX20_CONSTEXPR
2044 inline _InputIterator
2045 __find_if(_InputIterator __first
, _InputIterator __last
,
2046 _Predicate __pred
, input_iterator_tag
)
2048 while (__first
!= __last
&& !__pred(__first
))
2053 /// This is an overload used by find algos for the RAI case.
2054 template<typename _RandomAccessIterator
, typename _Predicate
>
2055 _GLIBCXX20_CONSTEXPR
2056 _RandomAccessIterator
2057 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2058 _Predicate __pred
, random_access_iterator_tag
)
2060 typename iterator_traits
<_RandomAccessIterator
>::difference_type
2061 __trip_count
= (__last
- __first
) >> 2;
2063 for (; __trip_count
> 0; --__trip_count
)
2065 if (__pred(__first
))
2069 if (__pred(__first
))
2073 if (__pred(__first
))
2077 if (__pred(__first
))
2082 switch (__last
- __first
)
2085 if (__pred(__first
))
2090 if (__pred(__first
))
2095 if (__pred(__first
))
2105 template<typename _Iterator
, typename _Predicate
>
2106 _GLIBCXX20_CONSTEXPR
2108 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
2110 return __find_if(__first
, __last
, __pred
,
2111 std::__iterator_category(__first
));
2114 template<typename _InputIterator
, typename _Predicate
>
2115 _GLIBCXX20_CONSTEXPR
2116 typename iterator_traits
<_InputIterator
>::difference_type
2117 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
2119 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
2120 for (; __first
!= __last
; ++__first
)
2121 if (__pred(__first
))
2126 template<typename _ForwardIterator
, typename _Predicate
>
2127 _GLIBCXX20_CONSTEXPR
2129 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
2132 __first
= std::__find_if(__first
, __last
, __pred
);
2133 if (__first
== __last
)
2135 _ForwardIterator __result
= __first
;
2137 for (; __first
!= __last
; ++__first
)
2138 if (!__pred(__first
))
2140 *__result
= _GLIBCXX_MOVE(*__first
);
2146 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
2147 typename _BinaryPredicate
>
2148 _GLIBCXX20_CONSTEXPR
2150 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2151 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
2152 _BinaryPredicate __predicate
)
2154 // Test for empty ranges
2155 if (__first1
== __last1
|| __first2
== __last2
)
2158 // Test for a pattern of length 1.
2159 _ForwardIterator2
__p1(__first2
);
2160 if (++__p1
== __last2
)
2161 return std::__find_if(__first1
, __last1
,
2162 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
2165 _ForwardIterator1 __current
= __first1
;
2170 std::__find_if(__first1
, __last1
,
2171 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
2173 if (__first1
== __last1
)
2176 _ForwardIterator2 __p
= __p1
;
2177 __current
= __first1
;
2178 if (++__current
== __last1
)
2181 while (__predicate(__current
, __p
))
2183 if (++__p
== __last2
)
2185 if (++__current
== __last1
)
2193 #if __cplusplus >= 201103L
2194 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
2195 typename _BinaryPredicate
>
2196 _GLIBCXX20_CONSTEXPR
2198 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2199 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
2201 // Efficiently compare identical prefixes: O(N) if sequences
2202 // have the same elements in the same order.
2203 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
2204 if (!__pred(__first1
, __first2
))
2207 if (__first1
== __last1
)
2210 // Establish __last2 assuming equal ranges by iterating over the
2211 // rest of the list.
2212 _ForwardIterator2 __last2
= __first2
;
2213 std::advance(__last2
, std::distance(__first1
, __last1
));
2214 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
2216 if (__scan
!= std::__find_if(__first1
, __scan
,
2217 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
2218 continue; // We've seen this one before.
2221 = std::__count_if(__first2
, __last2
,
2222 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
2223 if (0 == __matches
||
2224 std::__count_if(__scan
, __last1
,
2225 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
2233 * @brief Checks whether a permutation of the second sequence is equal
2234 * to the first sequence.
2235 * @ingroup non_mutating_algorithms
2236 * @param __first1 Start of first range.
2237 * @param __last1 End of first range.
2238 * @param __first2 Start of second range.
2239 * @return true if there exists a permutation of the elements in the range
2240 * [__first2, __first2 + (__last1 - __first1)), beginning with
2241 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2242 * returns true; otherwise, returns false.
2244 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
2245 _GLIBCXX20_CONSTEXPR
2247 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2248 _ForwardIterator2 __first2
)
2250 // concept requirements
2251 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
2252 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
2253 __glibcxx_function_requires(_EqualOpConcept
<
2254 typename iterator_traits
<_ForwardIterator1
>::value_type
,
2255 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
2256 __glibcxx_requires_valid_range(__first1
, __last1
);
2258 return std::__is_permutation(__first1
, __last1
, __first2
,
2259 __gnu_cxx::__ops::__iter_equal_to_iter());
2263 _GLIBCXX_BEGIN_NAMESPACE_ALGO
2266 * @brief Search a sequence for a matching sub-sequence using a predicate.
2267 * @ingroup non_mutating_algorithms
2268 * @param __first1 A forward iterator.
2269 * @param __last1 A forward iterator.
2270 * @param __first2 A forward iterator.
2271 * @param __last2 A forward iterator.
2272 * @param __predicate A binary predicate.
2273 * @return The first iterator @c i in the range
2274 * @p [__first1,__last1-(__last2-__first2)) such that
2275 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
2276 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
2278 * Searches the range @p [__first1,__last1) for a sub-sequence that
2279 * compares equal value-by-value with the sequence given by @p
2280 * [__first2,__last2), using @p __predicate to determine equality,
2281 * and returns an iterator to the first element of the
2282 * sub-sequence, or @p __last1 if no such iterator exists.
2284 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
2286 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
2287 typename _BinaryPredicate
>
2288 _GLIBCXX20_CONSTEXPR
2289 inline _ForwardIterator1
2290 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2291 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
2292 _BinaryPredicate __predicate
)
2294 // concept requirements
2295 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
2296 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
2297 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
2298 typename iterator_traits
<_ForwardIterator1
>::value_type
,
2299 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
2300 __glibcxx_requires_valid_range(__first1
, __last1
);
2301 __glibcxx_requires_valid_range(__first2
, __last2
);
2303 return std::__search(__first1
, __last1
, __first2
, __last2
,
2304 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
2307 _GLIBCXX_END_NAMESPACE_ALGO
2308 _GLIBCXX_END_NAMESPACE_VERSION
2311 // NB: This file is included within many other C++ includes, as a way
2312 // of getting the base algorithms. So, make sure that parallel bits
2313 // come in too if requested.
2314 #ifdef _GLIBCXX_PARALLEL
2315 # include <parallel/algobase.h>