1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001-2020 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 > 201703L
79 namespace std
_GLIBCXX_VISIBILITY(default)
81 _GLIBCXX_BEGIN_NAMESPACE_VERSION
84 * A constexpr wrapper for __builtin_memcmp.
85 * @param __num The number of elements of type _Tp (not bytes).
87 template<typename _Tp
, typename _Up
>
90 __memcmp(const _Tp
* __first1
, const _Up
* __first2
, size_t __num
)
92 #if __cplusplus >= 201103L
93 static_assert(sizeof(_Tp
) == sizeof(_Up
), "can be compared with memcmp");
95 #ifdef __cpp_lib_is_constant_evaluated
96 if (std::is_constant_evaluated())
98 for(; __num
> 0; ++__first1
, ++__first2
, --__num
)
99 if (*__first1
!= *__first2
)
100 return *__first1
< *__first2
? -1 : 1;
105 return __builtin_memcmp(__first1
, __first2
, sizeof(_Tp
) * __num
);
108 #if __cplusplus < 201103L
109 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
110 // nutshell, we are partially implementing the resolution of DR 187,
111 // when it's safe, i.e., the value_types are equal.
112 template<bool _BoolType
>
115 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
117 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
119 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
121 _ValueType1 __tmp
= *__a
;
128 struct __iter_swap
<true>
130 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
132 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
140 * @brief Swaps the contents of two iterators.
141 * @ingroup mutating_algorithms
142 * @param __a An iterator.
143 * @param __b Another iterator.
146 * This function swaps the values pointed to by two iterators, not the
147 * iterators themselves.
149 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
152 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
154 // concept requirements
155 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
157 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
160 #if __cplusplus < 201103L
161 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
163 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
166 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
168 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
171 typedef typename iterator_traits
<_ForwardIterator1
>::reference
173 typedef typename iterator_traits
<_ForwardIterator2
>::reference
175 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
176 && __are_same
<_ValueType1
&, _ReferenceType1
>::__value
177 && __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
180 // _GLIBCXX_RESOLVE_LIB_DEFECTS
181 // 187. iter_swap underspecified
187 * @brief Swap the elements of two sequences.
188 * @ingroup mutating_algorithms
189 * @param __first1 A forward iterator.
190 * @param __last1 A forward iterator.
191 * @param __first2 A forward iterator.
192 * @return An iterator equal to @p first2+(last1-first1).
194 * Swaps each element in the range @p [first1,last1) with the
195 * corresponding element in the range @p [first2,(last1-first1)).
196 * The ranges must not overlap.
198 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
201 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
202 _ForwardIterator2 __first2
)
204 // concept requirements
205 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
207 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
209 __glibcxx_requires_valid_range(__first1
, __last1
);
211 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
212 std::iter_swap(__first1
, __first2
);
217 * @brief This does what you think it does.
218 * @ingroup sorting_algorithms
219 * @param __a A thing of arbitrary type.
220 * @param __b Another thing of arbitrary type.
221 * @return The lesser of the parameters.
223 * This is the simple classic generic implementation. It will work on
224 * temporary expressions, since they are only evaluated once, unlike a
225 * preprocessor macro.
227 template<typename _Tp
>
230 min(const _Tp
& __a
, const _Tp
& __b
)
232 // concept requirements
233 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
234 //return __b < __a ? __b : __a;
241 * @brief This does what you think it does.
242 * @ingroup sorting_algorithms
243 * @param __a A thing of arbitrary type.
244 * @param __b Another thing of arbitrary type.
245 * @return The greater of the parameters.
247 * This is the simple classic generic implementation. It will work on
248 * temporary expressions, since they are only evaluated once, unlike a
249 * preprocessor macro.
251 template<typename _Tp
>
254 max(const _Tp
& __a
, const _Tp
& __b
)
256 // concept requirements
257 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
258 //return __a < __b ? __b : __a;
265 * @brief This does what you think it does.
266 * @ingroup sorting_algorithms
267 * @param __a A thing of arbitrary type.
268 * @param __b Another thing of arbitrary type.
269 * @param __comp A @link comparison_functors comparison functor@endlink.
270 * @return The lesser of the parameters.
272 * This will work on temporary expressions, since they are only evaluated
273 * once, unlike a preprocessor macro.
275 template<typename _Tp
, typename _Compare
>
278 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
280 //return __comp(__b, __a) ? __b : __a;
281 if (__comp(__b
, __a
))
287 * @brief This does what you think it does.
288 * @ingroup sorting_algorithms
289 * @param __a A thing of arbitrary type.
290 * @param __b Another thing of arbitrary type.
291 * @param __comp A @link comparison_functors comparison functor@endlink.
292 * @return The greater of the parameters.
294 * This will work on temporary expressions, since they are only evaluated
295 * once, unlike a preprocessor macro.
297 template<typename _Tp
, typename _Compare
>
300 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
302 //return __comp(__a, __b) ? __b : __a;
303 if (__comp(__a
, __b
))
308 // Fallback implementation of the function in bits/stl_iterator.h used to
309 // remove the __normal_iterator wrapper. See copy, fill, ...
310 template<typename _Iterator
>
313 __niter_base(_Iterator __it
)
314 _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible
<_Iterator
>::value
)
317 // Reverse the __niter_base transformation to get a
318 // __normal_iterator back again (this assumes that __normal_iterator
319 // is only used to wrap random access iterators, like pointers).
320 template<typename _From
, typename _To
>
323 __niter_wrap(_From __from
, _To __res
)
324 { return __from
+ (__res
- std::__niter_base(__from
)); }
326 // No need to wrap, iterator already has the right type.
327 template<typename _Iterator
>
330 __niter_wrap(const _Iterator
&, _Iterator __res
)
333 // All of these auxiliary structs serve two purposes. (1) Replace
334 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
335 // because the input and output ranges are permitted to overlap.)
336 // (2) If we're using random access iterators, then write the loop as
337 // a for loop with an explicit count.
339 template<bool _IsMove
, bool _IsSimple
, typename _Category
>
342 template<typename _II
, typename _OI
>
345 __copy_m(_II __first
, _II __last
, _OI __result
)
347 for (; __first
!= __last
; ++__result
, (void)++__first
)
348 *__result
= *__first
;
353 #if __cplusplus >= 201103L
354 template<typename _Category
>
355 struct __copy_move
<true, false, _Category
>
357 template<typename _II
, typename _OI
>
360 __copy_m(_II __first
, _II __last
, _OI __result
)
362 for (; __first
!= __last
; ++__result
, (void)++__first
)
363 *__result
= std::move(*__first
);
370 struct __copy_move
<false, false, random_access_iterator_tag
>
372 template<typename _II
, typename _OI
>
375 __copy_m(_II __first
, _II __last
, _OI __result
)
377 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
378 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
380 *__result
= *__first
;
388 #if __cplusplus >= 201103L
390 struct __copy_move
<true, false, random_access_iterator_tag
>
392 template<typename _II
, typename _OI
>
395 __copy_m(_II __first
, _II __last
, _OI __result
)
397 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
398 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
400 *__result
= std::move(*__first
);
409 template<bool _IsMove
>
410 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
412 template<typename _Tp
>
415 __copy_m(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
417 #if __cplusplus >= 201103L
418 using __assignable
= conditional
<_IsMove
,
419 is_move_assignable
<_Tp
>,
420 is_copy_assignable
<_Tp
>>;
421 // trivial types can have deleted assignment
422 static_assert( __assignable::type::value
, "type is not assignable" );
424 const ptrdiff_t _Num
= __last
- __first
;
426 __builtin_memmove(__result
, __first
, sizeof(_Tp
) * _Num
);
427 return __result
+ _Num
;
431 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
433 template<typename _Tp
, typename _Ref
, typename _Ptr
>
434 struct _Deque_iterator
;
436 struct _Bit_iterator
;
438 _GLIBCXX_END_NAMESPACE_CONTAINER
440 // Helpers for streambuf iterators (either istream or ostream).
441 // NB: avoid including <iosfwd>, relatively large.
442 template<typename _CharT
>
445 template<typename _CharT
, typename _Traits
>
446 class istreambuf_iterator
;
448 template<typename _CharT
, typename _Traits
>
449 class ostreambuf_iterator
;
451 template<bool _IsMove
, typename _CharT
>
452 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
453 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
454 __copy_move_a2(_CharT
*, _CharT
*,
455 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
457 template<bool _IsMove
, typename _CharT
>
458 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
459 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
460 __copy_move_a2(const _CharT
*, const _CharT
*,
461 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
463 template<bool _IsMove
, typename _CharT
>
464 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
466 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
467 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
469 template<bool _IsMove
, typename _II
, typename _OI
>
472 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
474 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
475 #ifdef __cpp_lib_is_constant_evaluated
476 if (std::is_constant_evaluated())
477 return std::__copy_move
<_IsMove
, false, _Category
>::
478 __copy_m(__first
, __last
, __result
);
480 return std::__copy_move
<_IsMove
, __memcpyable
<_OI
, _II
>::__value
,
481 _Category
>::__copy_m(__first
, __last
, __result
);
484 template<bool _IsMove
,
485 typename _Tp
, typename _Ref
, typename _Ptr
, typename _OI
>
487 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
488 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
491 template<bool _IsMove
,
492 typename _ITp
, typename _IRef
, typename _IPtr
, typename _OTp
>
493 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>
494 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
495 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
496 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>);
498 template<bool _IsMove
, typename _II
, typename _Tp
>
499 typename
__gnu_cxx::__enable_if
<
500 __is_random_access_iter
<_II
>::__value
,
501 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> >::__type
502 __copy_move_a1(_II
, _II
, _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
504 template<bool _IsMove
, typename _II
, typename _OI
>
507 __copy_move_a1(_II __first
, _II __last
, _OI __result
)
508 { return std::__copy_move_a2
<_IsMove
>(__first
, __last
, __result
); }
510 template<bool _IsMove
, typename _II
, typename _OI
>
513 __copy_move_a(_II __first
, _II __last
, _OI __result
)
515 return std::__niter_wrap(__result
,
516 std::__copy_move_a1
<_IsMove
>(std::__niter_base(__first
),
517 std::__niter_base(__last
),
518 std::__niter_base(__result
)));
521 template<bool _IsMove
,
522 typename _Ite
, typename _Seq
, typename _Cat
, typename _OI
>
524 __copy_move_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
525 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
528 template<bool _IsMove
,
529 typename _II
, typename _Ite
, typename _Seq
, typename _Cat
>
530 __gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
531 __copy_move_a(_II
, _II
,
532 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&);
534 template<bool _IsMove
,
535 typename _IIte
, typename _ISeq
, typename _ICat
,
536 typename _OIte
, typename _OSeq
, typename _OCat
>
537 ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>
538 __copy_move_a(const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
539 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
540 const ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>&);
543 * @brief Copies the range [first,last) into result.
544 * @ingroup mutating_algorithms
545 * @param __first An input iterator.
546 * @param __last An input iterator.
547 * @param __result An output iterator.
548 * @return result + (first - last)
550 * This inline function will boil down to a call to @c memmove whenever
551 * possible. Failing that, if random access iterators are passed, then the
552 * loop count will be known (and therefore a candidate for compiler
553 * optimizations such as unrolling). Result may not be contained within
554 * [first,last); the copy_backward function should be used instead.
556 * Note that the end of the output range is permitted to be contained
557 * within [first,last).
559 template<typename _II
, typename _OI
>
562 copy(_II __first
, _II __last
, _OI __result
)
564 // concept requirements
565 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
566 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
567 typename iterator_traits
<_II
>::value_type
>)
568 __glibcxx_requires_can_increment_range(__first
, __last
, __result
);
570 return std::__copy_move_a
<__is_move_iterator
<_II
>::__value
>
571 (std::__miter_base(__first
), std::__miter_base(__last
), __result
);
574 #if __cplusplus >= 201103L
576 * @brief Moves the range [first,last) into result.
577 * @ingroup mutating_algorithms
578 * @param __first An input iterator.
579 * @param __last An input iterator.
580 * @param __result An output iterator.
581 * @return result + (first - last)
583 * This inline function will boil down to a call to @c memmove whenever
584 * possible. Failing that, if random access iterators are passed, then the
585 * loop count will be known (and therefore a candidate for compiler
586 * optimizations such as unrolling). Result may not be contained within
587 * [first,last); the move_backward function should be used instead.
589 * Note that the end of the output range is permitted to be contained
590 * within [first,last).
592 template<typename _II
, typename _OI
>
595 move(_II __first
, _II __last
, _OI __result
)
597 // concept requirements
598 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
599 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
600 typename iterator_traits
<_II
>::value_type
>)
601 __glibcxx_requires_can_increment_range(__first
, __last
, __result
);
603 return std::__copy_move_a
<true>(std::__miter_base(__first
),
604 std::__miter_base(__last
), __result
);
607 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
609 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
612 template<bool _IsMove
, bool _IsSimple
, typename _Category
>
613 struct __copy_move_backward
615 template<typename _BI1
, typename _BI2
>
618 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
620 while (__first
!= __last
)
621 *--__result
= *--__last
;
626 #if __cplusplus >= 201103L
627 template<typename _Category
>
628 struct __copy_move_backward
<true, false, _Category
>
630 template<typename _BI1
, typename _BI2
>
633 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
635 while (__first
!= __last
)
636 *--__result
= std::move(*--__last
);
643 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
645 template<typename _BI1
, typename _BI2
>
648 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
650 typename iterator_traits
<_BI1
>::difference_type
651 __n
= __last
- __first
;
652 for (; __n
> 0; --__n
)
653 *--__result
= *--__last
;
658 #if __cplusplus >= 201103L
660 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
662 template<typename _BI1
, typename _BI2
>
665 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
667 typename iterator_traits
<_BI1
>::difference_type
668 __n
= __last
- __first
;
669 for (; __n
> 0; --__n
)
670 *--__result
= std::move(*--__last
);
676 template<bool _IsMove
>
677 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
679 template<typename _Tp
>
682 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
684 #if __cplusplus >= 201103L
685 using __assignable
= conditional
<_IsMove
,
686 is_move_assignable
<_Tp
>,
687 is_copy_assignable
<_Tp
>>;
688 // trivial types can have deleted assignment
689 static_assert( __assignable::type::value
, "type is not assignable" );
691 const ptrdiff_t _Num
= __last
- __first
;
693 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
694 return __result
- _Num
;
698 template<bool _IsMove
, typename _BI1
, typename _BI2
>
701 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
703 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
704 #ifdef __cpp_lib_is_constant_evaluated
705 if (std::is_constant_evaluated())
706 return std::__copy_move_backward
<_IsMove
, false, _Category
>::
707 __copy_move_b(__first
, __last
, __result
);
709 return std::__copy_move_backward
<_IsMove
,
710 __memcpyable
<_BI2
, _BI1
>::__value
,
711 _Category
>::__copy_move_b(__first
,
716 template<bool _IsMove
, typename _BI1
, typename _BI2
>
719 __copy_move_backward_a1(_BI1 __first
, _BI1 __last
, _BI2 __result
)
720 { return std::__copy_move_backward_a2
<_IsMove
>(__first
, __last
, __result
); }
722 template<bool _IsMove
,
723 typename _Tp
, typename _Ref
, typename _Ptr
, typename _OI
>
725 __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
726 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
729 template<bool _IsMove
,
730 typename _ITp
, typename _IRef
, typename _IPtr
, typename _OTp
>
731 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>
732 __copy_move_backward_a1(
733 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
734 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
735 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>);
737 template<bool _IsMove
, typename _II
, typename _Tp
>
738 typename
__gnu_cxx::__enable_if
<
739 __is_random_access_iter
<_II
>::__value
,
740 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> >::__type
741 __copy_move_backward_a1(_II
, _II
,
742 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
744 template<bool _IsMove
, typename _II
, typename _OI
>
747 __copy_move_backward_a(_II __first
, _II __last
, _OI __result
)
749 return std::__niter_wrap(__result
,
750 std::__copy_move_backward_a1
<_IsMove
>
751 (std::__niter_base(__first
), std::__niter_base(__last
),
752 std::__niter_base(__result
)));
755 template<bool _IsMove
,
756 typename _Ite
, typename _Seq
, typename _Cat
, typename _OI
>
758 __copy_move_backward_a(
759 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
760 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
763 template<bool _IsMove
,
764 typename _II
, typename _Ite
, typename _Seq
, typename _Cat
>
765 __gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
766 __copy_move_backward_a(_II
, _II
,
767 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&);
769 template<bool _IsMove
,
770 typename _IIte
, typename _ISeq
, typename _ICat
,
771 typename _OIte
, typename _OSeq
, typename _OCat
>
772 ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>
773 __copy_move_backward_a(
774 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
775 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
776 const ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>&);
779 * @brief Copies the range [first,last) into result.
780 * @ingroup mutating_algorithms
781 * @param __first A bidirectional iterator.
782 * @param __last A bidirectional iterator.
783 * @param __result A bidirectional iterator.
784 * @return result - (first - last)
786 * The function has the same effect as copy, but starts at the end of the
787 * range and works its way to the start, returning the start of the result.
788 * This inline function will boil down to a call to @c memmove whenever
789 * possible. Failing that, if random access iterators are passed, then the
790 * loop count will be known (and therefore a candidate for compiler
791 * optimizations such as unrolling).
793 * Result may not be in the range (first,last]. Use copy instead. Note
794 * that the start of the output range may overlap [first,last).
796 template<typename _BI1
, typename _BI2
>
799 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
801 // concept requirements
802 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
803 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
804 __glibcxx_function_requires(_ConvertibleConcept
<
805 typename iterator_traits
<_BI1
>::value_type
,
806 typename iterator_traits
<_BI2
>::value_type
>)
807 __glibcxx_requires_can_decrement_range(__first
, __last
, __result
);
809 return std::__copy_move_backward_a
<__is_move_iterator
<_BI1
>::__value
>
810 (std::__miter_base(__first
), std::__miter_base(__last
), __result
);
813 #if __cplusplus >= 201103L
815 * @brief Moves the range [first,last) into result.
816 * @ingroup mutating_algorithms
817 * @param __first A bidirectional iterator.
818 * @param __last A bidirectional iterator.
819 * @param __result A bidirectional iterator.
820 * @return result - (first - last)
822 * The function has the same effect as move, but starts at the end of the
823 * range and works its way to the start, returning the start of the result.
824 * This inline function will boil down to a call to @c memmove whenever
825 * possible. Failing that, if random access iterators are passed, then the
826 * loop count will be known (and therefore a candidate for compiler
827 * optimizations such as unrolling).
829 * Result may not be in the range (first,last]. Use move instead. Note
830 * that the start of the output range may overlap [first,last).
832 template<typename _BI1
, typename _BI2
>
835 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
837 // concept requirements
838 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
839 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
840 __glibcxx_function_requires(_ConvertibleConcept
<
841 typename iterator_traits
<_BI1
>::value_type
,
842 typename iterator_traits
<_BI2
>::value_type
>)
843 __glibcxx_requires_can_decrement_range(__first
, __last
, __result
);
845 return std::__copy_move_backward_a
<true>(std::__miter_base(__first
),
846 std::__miter_base(__last
),
850 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
852 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
855 template<typename _ForwardIterator
, typename _Tp
>
858 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
859 __fill_a1(_ForwardIterator __first
, _ForwardIterator __last
,
862 for (; __first
!= __last
; ++__first
)
866 template<typename _ForwardIterator
, typename _Tp
>
869 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
870 __fill_a1(_ForwardIterator __first
, _ForwardIterator __last
,
873 const _Tp __tmp
= __value
;
874 for (; __first
!= __last
; ++__first
)
878 // Specialization: for char types we can use memset.
879 template<typename _Tp
>
882 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
883 __fill_a1(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
885 const _Tp __tmp
= __c
;
886 #if __cpp_lib_is_constant_evaluated
887 if (std::is_constant_evaluated())
889 for (; __first
!= __last
; ++__first
)
894 if (const size_t __len
= __last
- __first
)
895 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
), __len
);
898 template<typename _Ite
, typename _Cont
, typename _Tp
>
901 __fill_a1(::__gnu_cxx::__normal_iterator
<_Ite
, _Cont
> __first
,
902 ::__gnu_cxx::__normal_iterator
<_Ite
, _Cont
> __last
,
904 { std::__fill_a1(__first
.base(), __last
.base(), __value
); }
906 template<typename _Tp
, typename _VTp
>
908 __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
909 const _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
913 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator
, _GLIBCXX_STD_C::_Bit_iterator
,
916 template<typename _FIte
, typename _Tp
>
919 __fill_a(_FIte __first
, _FIte __last
, const _Tp
& __value
)
920 { std::__fill_a1(__first
, __last
, __value
); }
922 template<typename _Ite
, typename _Seq
, typename _Cat
, typename _Tp
>
924 __fill_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
925 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
929 * @brief Fills the range [first,last) with copies of value.
930 * @ingroup mutating_algorithms
931 * @param __first A forward iterator.
932 * @param __last A forward iterator.
933 * @param __value A reference-to-const of arbitrary type.
936 * This function fills a range with copies of the same value. For char
937 * types filling contiguous areas of memory, this becomes an inline call
938 * to @c memset or @c wmemset.
940 template<typename _ForwardIterator
, typename _Tp
>
943 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
945 // concept requirements
946 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
948 __glibcxx_requires_valid_range(__first
, __last
);
950 std::__fill_a(__first
, __last
, __value
);
953 // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
954 inline _GLIBCXX_CONSTEXPR
int
955 __size_to_integer(int __n
) { return __n
; }
956 inline _GLIBCXX_CONSTEXPR
unsigned
957 __size_to_integer(unsigned __n
) { return __n
; }
958 inline _GLIBCXX_CONSTEXPR
long
959 __size_to_integer(long __n
) { return __n
; }
960 inline _GLIBCXX_CONSTEXPR
unsigned long
961 __size_to_integer(unsigned long __n
) { return __n
; }
962 inline _GLIBCXX_CONSTEXPR
long long
963 __size_to_integer(long long __n
) { return __n
; }
964 inline _GLIBCXX_CONSTEXPR
unsigned long long
965 __size_to_integer(unsigned long long __n
) { return __n
; }
967 #if defined(__GLIBCXX_TYPE_INT_N_0)
968 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
969 __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n
) { return __n
; }
970 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_0
971 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n
) { return __n
; }
973 #if defined(__GLIBCXX_TYPE_INT_N_1)
974 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
975 __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n
) { return __n
; }
976 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_1
977 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n
) { return __n
; }
979 #if defined(__GLIBCXX_TYPE_INT_N_2)
980 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
981 __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n
) { return __n
; }
982 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_2
983 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n
) { return __n
; }
985 #if defined(__GLIBCXX_TYPE_INT_N_3)
986 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_3
987 __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n
) { return __n
; }
988 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
989 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n
) { return __n
; }
992 inline _GLIBCXX_CONSTEXPR
long long
993 __size_to_integer(float __n
) { return __n
; }
994 inline _GLIBCXX_CONSTEXPR
long long
995 __size_to_integer(double __n
) { return __n
; }
996 inline _GLIBCXX_CONSTEXPR
long long
997 __size_to_integer(long double __n
) { return __n
; }
998 #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
999 inline _GLIBCXX_CONSTEXPR
long long
1000 __size_to_integer(__float128 __n
) { return __n
; }
1003 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1004 _GLIBCXX20_CONSTEXPR
1006 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
1007 __fill_n_a1(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
1009 for (; __n
> 0; --__n
, (void) ++__first
)
1014 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1015 _GLIBCXX20_CONSTEXPR
1017 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
1018 __fill_n_a1(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
1020 const _Tp __tmp
= __value
;
1021 for (; __n
> 0; --__n
, (void) ++__first
)
1026 template<typename _Ite
, typename _Seq
, typename _Cat
, typename _Size
,
1028 ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
1029 __fill_n_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>& __first
,
1030 _Size __n
, const _Tp
& __value
,
1031 std::input_iterator_tag
);
1033 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1034 _GLIBCXX20_CONSTEXPR
1035 inline _OutputIterator
1036 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1037 std::output_iterator_tag
)
1039 #if __cplusplus >= 201103L
1040 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1042 return __fill_n_a1(__first
, __n
, __value
);
1045 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1046 _GLIBCXX20_CONSTEXPR
1047 inline _OutputIterator
1048 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1049 std::input_iterator_tag
)
1051 #if __cplusplus >= 201103L
1052 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1054 return __fill_n_a1(__first
, __n
, __value
);
1057 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1058 _GLIBCXX20_CONSTEXPR
1059 inline _OutputIterator
1060 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1061 std::random_access_iterator_tag
)
1063 #if __cplusplus >= 201103L
1064 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1069 __glibcxx_requires_can_increment(__first
, __n
);
1071 std::__fill_a(__first
, __first
+ __n
, __value
);
1072 return __first
+ __n
;
1076 * @brief Fills the range [first,first+n) with copies of value.
1077 * @ingroup mutating_algorithms
1078 * @param __first An output iterator.
1079 * @param __n The count of copies to perform.
1080 * @param __value A reference-to-const of arbitrary type.
1081 * @return The iterator at first+n.
1083 * This function fills a range with copies of the same value. For char
1084 * types filling contiguous areas of memory, this becomes an inline call
1085 * to @c memset or @c wmemset.
1087 * If @p __n is negative, the function does nothing.
1089 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1090 // DR 865. More algorithms that throw away information
1091 // DR 426. search_n(), fill_n(), and generate_n() with negative n
1092 template<typename _OI
, typename _Size
, typename _Tp
>
1093 _GLIBCXX20_CONSTEXPR
1095 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
1097 // concept requirements
1098 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
1100 return std::__fill_n_a(__first
, std::__size_to_integer(__n
), __value
,
1101 std::__iterator_category(__first
));
1104 template<bool _BoolType
>
1107 template<typename _II1
, typename _II2
>
1108 _GLIBCXX20_CONSTEXPR
1110 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1112 for (; __first1
!= __last1
; ++__first1
, (void) ++__first2
)
1113 if (!(*__first1
== *__first2
))
1120 struct __equal
<true>
1122 template<typename _Tp
>
1123 _GLIBCXX20_CONSTEXPR
1125 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
1127 if (const size_t __len
= (__last1
- __first1
))
1128 return !std::__memcmp(__first1
, __first2
, __len
);
1133 template<typename _Tp
, typename _Ref
, typename _Ptr
, typename _II
>
1134 typename
__gnu_cxx::__enable_if
<
1135 __is_random_access_iter
<_II
>::__value
, bool>::__type
1136 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
1137 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
1140 template<typename _Tp1
, typename _Ref1
, typename _Ptr1
,
1141 typename _Tp2
, typename _Ref2
, typename _Ptr2
>
1143 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1144 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1145 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>);
1147 template<typename _II
, typename _Tp
, typename _Ref
, typename _Ptr
>
1148 typename
__gnu_cxx::__enable_if
<
1149 __is_random_access_iter
<_II
>::__value
, bool>::__type
1150 __equal_aux1(_II
, _II
,
1151 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>);
1153 template<typename _II1
, typename _II2
>
1154 _GLIBCXX20_CONSTEXPR
1156 __equal_aux1(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1158 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1159 const bool __simple
= ((__is_integer
<_ValueType1
>::__value
1160 || __is_pointer
<_ValueType1
>::__value
)
1161 && __memcmpable
<_II1
, _II2
>::__value
);
1162 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
1165 template<typename _II1
, typename _II2
>
1166 _GLIBCXX20_CONSTEXPR
1168 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1170 return std::__equal_aux1(std::__niter_base(__first1
),
1171 std::__niter_base(__last1
),
1172 std::__niter_base(__first2
));
1175 template<typename _II1
, typename _Seq1
, typename _Cat1
, typename _II2
>
1177 __equal_aux(const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1178 const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1181 template<typename _II1
, typename _II2
, typename _Seq2
, typename _Cat2
>
1183 __equal_aux(_II1
, _II1
,
1184 const ::__gnu_debug::_Safe_iterator
<_II2
, _Seq2
, _Cat2
>&);
1186 template<typename _II1
, typename _Seq1
, typename _Cat1
,
1187 typename _II2
, typename _Seq2
, typename _Cat2
>
1189 __equal_aux(const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1190 const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1191 const ::__gnu_debug::_Safe_iterator
<_II2
, _Seq2
, _Cat2
>&);
1193 template<typename
, typename
>
1196 template<typename _II1
, typename _II2
>
1197 _GLIBCXX20_CONSTEXPR
1199 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
1202 template<typename _II
>
1203 _GLIBCXX20_CONSTEXPR
1205 __cnd2(_II __first
, _II __last
)
1206 { return __first
!= __last
; }
1210 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
1212 template<typename _RAI1
, typename _RAI2
>
1213 _GLIBCXX20_CONSTEXPR
1215 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
1216 _RAI2 __first2
, _RAI2 __last2
)
1218 const typename iterator_traits
<_RAI1
>::difference_type
1219 __diff1
= __last1
- __first1
;
1220 const typename iterator_traits
<_RAI2
>::difference_type
1221 __diff2
= __last2
- __first2
;
1222 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
1225 template<typename _RAI
>
1226 static _GLIBCXX20_CONSTEXPR
bool
1231 template<typename _II1
, typename _II2
, typename _Compare
>
1232 _GLIBCXX20_CONSTEXPR
1234 __lexicographical_compare_impl(_II1 __first1
, _II1 __last1
,
1235 _II2 __first2
, _II2 __last2
,
1238 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1239 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1240 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1242 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1243 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1244 ++__first1
, (void)++__first2
)
1246 if (__comp(__first1
, __first2
))
1248 if (__comp(__first2
, __first1
))
1251 return __first1
== __last1
&& __first2
!= __last2
;
1254 template<bool _BoolType
>
1255 struct __lexicographical_compare
1257 template<typename _II1
, typename _II2
>
1258 _GLIBCXX20_CONSTEXPR
1260 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1262 using __gnu_cxx::__ops::__iter_less_iter
;
1263 return std::__lexicographical_compare_impl(__first1
, __last1
,
1265 __iter_less_iter());
1270 struct __lexicographical_compare
<true>
1272 template<typename _Tp
, typename _Up
>
1273 _GLIBCXX20_CONSTEXPR
1275 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
1276 const _Up
* __first2
, const _Up
* __last2
)
1278 const size_t __len1
= __last1
- __first1
;
1279 const size_t __len2
= __last2
- __first2
;
1280 if (const size_t __len
= std::min(__len1
, __len2
))
1281 if (int __result
= std::__memcmp(__first1
, __first2
, __len
))
1282 return __result
< 0;
1283 return __len1
< __len2
;
1287 template<typename _II1
, typename _II2
>
1288 _GLIBCXX20_CONSTEXPR
1290 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
1291 _II2 __first2
, _II2 __last2
)
1293 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1294 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1295 const bool __simple
=
1296 (__is_byte
<_ValueType1
>::__value
&& __is_byte
<_ValueType2
>::__value
1297 && !__gnu_cxx::__numeric_traits
<_ValueType1
>::__is_signed
1298 && !__gnu_cxx::__numeric_traits
<_ValueType2
>::__is_signed
1299 && __is_pointer
<_II1
>::__value
1300 && __is_pointer
<_II2
>::__value
1301 #if __cplusplus > 201703L && __cpp_lib_concepts
1302 // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
1303 // so __is_byte<T> could be true, but we can't use memcmp with
1305 && !is_volatile_v
<remove_reference_t
<iter_reference_t
<_II1
>>>
1306 && !is_volatile_v
<remove_reference_t
<iter_reference_t
<_II2
>>>
1310 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
1314 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1315 _GLIBCXX20_CONSTEXPR
1317 __lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1318 const _Tp
& __val
, _Compare __comp
)
1320 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1323 _DistanceType __len
= std::distance(__first
, __last
);
1327 _DistanceType __half
= __len
>> 1;
1328 _ForwardIterator __middle
= __first
;
1329 std::advance(__middle
, __half
);
1330 if (__comp(__middle
, __val
))
1334 __len
= __len
- __half
- 1;
1343 * @brief Finds the first position in which @a val could be inserted
1344 * without changing the ordering.
1345 * @param __first An iterator.
1346 * @param __last Another iterator.
1347 * @param __val The search term.
1348 * @return An iterator pointing to the first element <em>not less
1349 * than</em> @a val, or end() if every element is less than
1351 * @ingroup binary_search_algorithms
1353 template<typename _ForwardIterator
, typename _Tp
>
1354 _GLIBCXX20_CONSTEXPR
1355 inline _ForwardIterator
1356 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1359 // concept requirements
1360 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1361 __glibcxx_function_requires(_LessThanOpConcept
<
1362 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1363 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
1365 return std::__lower_bound(__first
, __last
, __val
,
1366 __gnu_cxx::__ops::__iter_less_val());
1369 /// This is a helper function for the sort routines and for random.tcc.
1370 // Precondition: __n > 0.
1371 inline _GLIBCXX_CONSTEXPR
int
1373 { return (int)sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1375 inline _GLIBCXX_CONSTEXPR
unsigned
1377 { return (int)sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1379 inline _GLIBCXX_CONSTEXPR
long
1381 { return (int)sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1383 inline _GLIBCXX_CONSTEXPR
unsigned long
1384 __lg(unsigned long __n
)
1385 { return (int)sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1387 inline _GLIBCXX_CONSTEXPR
long long
1389 { return (int)sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1391 inline _GLIBCXX_CONSTEXPR
unsigned long long
1392 __lg(unsigned long long __n
)
1393 { return (int)sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1395 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1398 * @brief Tests a range for element-wise equality.
1399 * @ingroup non_mutating_algorithms
1400 * @param __first1 An input iterator.
1401 * @param __last1 An input iterator.
1402 * @param __first2 An input iterator.
1403 * @return A boolean true or false.
1405 * This compares the elements of two ranges using @c == and returns true or
1406 * false depending on whether all of the corresponding elements of the
1409 template<typename _II1
, typename _II2
>
1410 _GLIBCXX20_CONSTEXPR
1412 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1414 // concept requirements
1415 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1416 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1417 __glibcxx_function_requires(_EqualOpConcept
<
1418 typename iterator_traits
<_II1
>::value_type
,
1419 typename iterator_traits
<_II2
>::value_type
>)
1420 __glibcxx_requires_can_increment_range(__first1
, __last1
, __first2
);
1422 return std::__equal_aux(__first1
, __last1
, __first2
);
1426 * @brief Tests a range for element-wise equality.
1427 * @ingroup non_mutating_algorithms
1428 * @param __first1 An input iterator.
1429 * @param __last1 An input iterator.
1430 * @param __first2 An input iterator.
1431 * @param __binary_pred A binary predicate @link functors
1433 * @return A boolean true or false.
1435 * This compares the elements of two ranges using the binary_pred
1436 * parameter, and returns true or
1437 * false depending on whether all of the corresponding elements of the
1440 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1441 _GLIBCXX20_CONSTEXPR
1443 equal(_IIter1 __first1
, _IIter1 __last1
,
1444 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
1446 // concept requirements
1447 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1448 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1449 __glibcxx_requires_valid_range(__first1
, __last1
);
1451 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
1452 if (!bool(__binary_pred(*__first1
, *__first2
)))
1457 #if __cplusplus >= 201103L
1458 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1459 template<typename _II1
, typename _II2
>
1460 _GLIBCXX20_CONSTEXPR
1462 __equal4(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1464 using _RATag
= random_access_iterator_tag
;
1465 using _Cat1
= typename iterator_traits
<_II1
>::iterator_category
;
1466 using _Cat2
= typename iterator_traits
<_II2
>::iterator_category
;
1467 using _RAIters
= __and_
<is_same
<_Cat1
, _RATag
>, is_same
<_Cat2
, _RATag
>>;
1470 auto __d1
= std::distance(__first1
, __last1
);
1471 auto __d2
= std::distance(__first2
, __last2
);
1474 return _GLIBCXX_STD_A::equal(__first1
, __last1
, __first2
);
1477 for (; __first1
!= __last1
&& __first2
!= __last2
;
1478 ++__first1
, (void)++__first2
)
1479 if (!(*__first1
== *__first2
))
1481 return __first1
== __last1
&& __first2
== __last2
;
1484 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1485 template<typename _II1
, typename _II2
, typename _BinaryPredicate
>
1486 _GLIBCXX20_CONSTEXPR
1488 __equal4(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
,
1489 _BinaryPredicate __binary_pred
)
1491 using _RATag
= random_access_iterator_tag
;
1492 using _Cat1
= typename iterator_traits
<_II1
>::iterator_category
;
1493 using _Cat2
= typename iterator_traits
<_II2
>::iterator_category
;
1494 using _RAIters
= __and_
<is_same
<_Cat1
, _RATag
>, is_same
<_Cat2
, _RATag
>>;
1497 auto __d1
= std::distance(__first1
, __last1
);
1498 auto __d2
= std::distance(__first2
, __last2
);
1501 return _GLIBCXX_STD_A::equal(__first1
, __last1
, __first2
,
1505 for (; __first1
!= __last1
&& __first2
!= __last2
;
1506 ++__first1
, (void)++__first2
)
1507 if (!bool(__binary_pred(*__first1
, *__first2
)))
1509 return __first1
== __last1
&& __first2
== __last2
;
1513 #if __cplusplus > 201103L
1515 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1518 * @brief Tests a range for element-wise equality.
1519 * @ingroup non_mutating_algorithms
1520 * @param __first1 An input iterator.
1521 * @param __last1 An input iterator.
1522 * @param __first2 An input iterator.
1523 * @param __last2 An input iterator.
1524 * @return A boolean true or false.
1526 * This compares the elements of two ranges using @c == and returns true or
1527 * false depending on whether all of the corresponding elements of the
1530 template<typename _II1
, typename _II2
>
1531 _GLIBCXX20_CONSTEXPR
1533 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1535 // concept requirements
1536 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1537 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1538 __glibcxx_function_requires(_EqualOpConcept
<
1539 typename iterator_traits
<_II1
>::value_type
,
1540 typename iterator_traits
<_II2
>::value_type
>)
1541 __glibcxx_requires_valid_range(__first1
, __last1
);
1542 __glibcxx_requires_valid_range(__first2
, __last2
);
1544 return _GLIBCXX_STD_A::__equal4(__first1
, __last1
, __first2
, __last2
);
1548 * @brief Tests a range for element-wise equality.
1549 * @ingroup non_mutating_algorithms
1550 * @param __first1 An input iterator.
1551 * @param __last1 An input iterator.
1552 * @param __first2 An input iterator.
1553 * @param __last2 An input iterator.
1554 * @param __binary_pred A binary predicate @link functors
1556 * @return A boolean true or false.
1558 * This compares the elements of two ranges using the binary_pred
1559 * parameter, and returns true or
1560 * false depending on whether all of the corresponding elements of the
1563 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1564 _GLIBCXX20_CONSTEXPR
1566 equal(_IIter1 __first1
, _IIter1 __last1
,
1567 _IIter2 __first2
, _IIter2 __last2
, _BinaryPredicate __binary_pred
)
1569 // concept requirements
1570 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1571 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1572 __glibcxx_requires_valid_range(__first1
, __last1
);
1573 __glibcxx_requires_valid_range(__first2
, __last2
);
1575 return _GLIBCXX_STD_A::__equal4(__first1
, __last1
, __first2
, __last2
,
1581 * @brief Performs @b dictionary comparison on ranges.
1582 * @ingroup sorting_algorithms
1583 * @param __first1 An input iterator.
1584 * @param __last1 An input iterator.
1585 * @param __first2 An input iterator.
1586 * @param __last2 An input iterator.
1587 * @return A boolean true or false.
1589 * <em>Returns true if the sequence of elements defined by the range
1590 * [first1,last1) is lexicographically less than the sequence of elements
1591 * defined by the range [first2,last2). Returns false otherwise.</em>
1592 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1593 * then this is an inline call to @c memcmp.
1595 template<typename _II1
, typename _II2
>
1596 _GLIBCXX20_CONSTEXPR
1598 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1599 _II2 __first2
, _II2 __last2
)
1601 #ifdef _GLIBCXX_CONCEPT_CHECKS
1602 // concept requirements
1603 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1604 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1606 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1607 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1608 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1609 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1610 __glibcxx_requires_valid_range(__first1
, __last1
);
1611 __glibcxx_requires_valid_range(__first2
, __last2
);
1613 return std::__lexicographical_compare_aux(std::__niter_base(__first1
),
1614 std::__niter_base(__last1
),
1615 std::__niter_base(__first2
),
1616 std::__niter_base(__last2
));
1620 * @brief Performs @b dictionary comparison on ranges.
1621 * @ingroup sorting_algorithms
1622 * @param __first1 An input iterator.
1623 * @param __last1 An input iterator.
1624 * @param __first2 An input iterator.
1625 * @param __last2 An input iterator.
1626 * @param __comp A @link comparison_functors comparison functor@endlink.
1627 * @return A boolean true or false.
1629 * The same as the four-parameter @c lexicographical_compare, but uses the
1630 * comp parameter instead of @c <.
1632 template<typename _II1
, typename _II2
, typename _Compare
>
1633 _GLIBCXX20_CONSTEXPR
1635 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1636 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1638 // concept requirements
1639 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1640 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1641 __glibcxx_requires_valid_range(__first1
, __last1
);
1642 __glibcxx_requires_valid_range(__first2
, __last2
);
1644 return std::__lexicographical_compare_impl
1645 (__first1
, __last1
, __first2
, __last2
,
1646 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1649 #if __cpp_lib_three_way_comparison
1650 // Iter points to a contiguous range of unsigned narrow character type
1651 // or std::byte, suitable for comparison by memcmp.
1652 template<typename _Iter
>
1653 concept __is_byte_iter
= contiguous_iterator
<_Iter
>
1654 && __is_byte
<iter_value_t
<_Iter
>>::__value
!= 0
1655 && !__gnu_cxx::__numeric_traits
<iter_value_t
<_Iter
>>::__is_signed
;
1657 // Return a struct with two members, initialized to the smaller of x and y
1658 // (or x if they compare equal) and the result of the comparison x <=> y.
1659 template<typename _Tp
>
1661 __min_cmp(_Tp __x
, _Tp __y
)
1665 decltype(__x
<=> __y
) _M_cmp
;
1667 auto __c
= __x
<=> __y
;
1669 return _Res
{__y
, __c
};
1670 return _Res
{__x
, __c
};
1674 * @brief Performs dictionary comparison on ranges.
1675 * @ingroup sorting_algorithms
1676 * @param __first1 An input iterator.
1677 * @param __last1 An input iterator.
1678 * @param __first2 An input iterator.
1679 * @param __last2 An input iterator.
1680 * @param __comp A @link comparison_functors comparison functor@endlink.
1681 * @return The comparison category that `__comp(*__first1, *__first2)`
1684 template<typename _InputIter1
, typename _InputIter2
, typename _Comp
>
1686 lexicographical_compare_three_way(_InputIter1 __first1
,
1687 _InputIter1 __last1
,
1688 _InputIter2 __first2
,
1689 _InputIter2 __last2
,
1691 -> decltype(__comp(*__first1
, *__first2
))
1693 // concept requirements
1694 __glibcxx_function_requires(_InputIteratorConcept
<_InputIter1
>)
1695 __glibcxx_function_requires(_InputIteratorConcept
<_InputIter2
>)
1696 __glibcxx_requires_valid_range(__first1
, __last1
);
1697 __glibcxx_requires_valid_range(__first2
, __last2
);
1699 #if __cpp_lib_is_constant_evaluated
1700 using _Cat
= decltype(__comp(*__first1
, *__first2
));
1701 static_assert(same_as
<common_comparison_category_t
<_Cat
>, _Cat
>);
1703 if (!std::is_constant_evaluated())
1704 if constexpr (same_as
<_Comp
, __detail::_Synth3way
>
1705 || same_as
<_Comp
, compare_three_way
>)
1706 if constexpr (__is_byte_iter
<_InputIter1
>)
1707 if constexpr (__is_byte_iter
<_InputIter2
>)
1709 const auto [__len
, __lencmp
]
1710 = std::__min_cmp(__last1
- __first1
, __last2
- __first2
);
1714 = __builtin_memcmp(&*__first1
, &*__first2
, __len
) <=> 0;
1720 #endif // is_constant_evaluated
1721 while (__first1
!= __last1
)
1723 if (__first2
== __last2
)
1724 return strong_ordering::greater
;
1725 if (auto __cmp
= __comp(*__first1
, *__first2
); __cmp
!= 0)
1730 return (__first2
== __last2
) <=> true; // See PR 94006
1733 template<typename _InputIter1
, typename _InputIter2
>
1735 lexicographical_compare_three_way(_InputIter1 __first1
,
1736 _InputIter1 __last1
,
1737 _InputIter2 __first2
,
1738 _InputIter2 __last2
)
1740 return std::lexicographical_compare_three_way(__first1
, __last1
,
1742 compare_three_way
{});
1744 #endif // three_way_comparison
1746 template<typename _InputIterator1
, typename _InputIterator2
,
1747 typename _BinaryPredicate
>
1748 _GLIBCXX20_CONSTEXPR
1749 pair
<_InputIterator1
, _InputIterator2
>
1750 __mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1751 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1753 while (__first1
!= __last1
&& __binary_pred(__first1
, __first2
))
1758 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1762 * @brief Finds the places in ranges which don't match.
1763 * @ingroup non_mutating_algorithms
1764 * @param __first1 An input iterator.
1765 * @param __last1 An input iterator.
1766 * @param __first2 An input iterator.
1767 * @return A pair of iterators pointing to the first mismatch.
1769 * This compares the elements of two ranges using @c == and returns a pair
1770 * of iterators. The first iterator points into the first range, the
1771 * second iterator points into the second range, and the elements pointed
1772 * to by the iterators are not equal.
1774 template<typename _InputIterator1
, typename _InputIterator2
>
1775 _GLIBCXX20_CONSTEXPR
1776 inline pair
<_InputIterator1
, _InputIterator2
>
1777 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1778 _InputIterator2 __first2
)
1780 // concept requirements
1781 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1783 __glibcxx_function_requires(_EqualOpConcept
<
1784 typename iterator_traits
<_InputIterator1
>::value_type
,
1785 typename iterator_traits
<_InputIterator2
>::value_type
>)
1786 __glibcxx_requires_valid_range(__first1
, __last1
);
1788 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
,
1789 __gnu_cxx::__ops::__iter_equal_to_iter());
1793 * @brief Finds the places in ranges which don't match.
1794 * @ingroup non_mutating_algorithms
1795 * @param __first1 An input iterator.
1796 * @param __last1 An input iterator.
1797 * @param __first2 An input iterator.
1798 * @param __binary_pred A binary predicate @link functors
1800 * @return A pair of iterators pointing to the first mismatch.
1802 * This compares the elements of two ranges using the binary_pred
1803 * parameter, and returns a pair
1804 * of iterators. The first iterator points into the first range, the
1805 * second iterator points into the second range, and the elements pointed
1806 * to by the iterators are not equal.
1808 template<typename _InputIterator1
, typename _InputIterator2
,
1809 typename _BinaryPredicate
>
1810 _GLIBCXX20_CONSTEXPR
1811 inline pair
<_InputIterator1
, _InputIterator2
>
1812 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1813 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1815 // concept requirements
1816 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1817 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1818 __glibcxx_requires_valid_range(__first1
, __last1
);
1820 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
,
1821 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1824 #if __cplusplus > 201103L
1826 template<typename _InputIterator1
, typename _InputIterator2
,
1827 typename _BinaryPredicate
>
1828 _GLIBCXX20_CONSTEXPR
1829 pair
<_InputIterator1
, _InputIterator2
>
1830 __mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1831 _InputIterator2 __first2
, _InputIterator2 __last2
,
1832 _BinaryPredicate __binary_pred
)
1834 while (__first1
!= __last1
&& __first2
!= __last2
1835 && __binary_pred(__first1
, __first2
))
1840 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1844 * @brief Finds the places in ranges which don't match.
1845 * @ingroup non_mutating_algorithms
1846 * @param __first1 An input iterator.
1847 * @param __last1 An input iterator.
1848 * @param __first2 An input iterator.
1849 * @param __last2 An input iterator.
1850 * @return A pair of iterators pointing to the first mismatch.
1852 * This compares the elements of two ranges using @c == and returns a pair
1853 * of iterators. The first iterator points into the first range, the
1854 * second iterator points into the second range, and the elements pointed
1855 * to by the iterators are not equal.
1857 template<typename _InputIterator1
, typename _InputIterator2
>
1858 _GLIBCXX20_CONSTEXPR
1859 inline pair
<_InputIterator1
, _InputIterator2
>
1860 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1861 _InputIterator2 __first2
, _InputIterator2 __last2
)
1863 // concept requirements
1864 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1865 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1866 __glibcxx_function_requires(_EqualOpConcept
<
1867 typename iterator_traits
<_InputIterator1
>::value_type
,
1868 typename iterator_traits
<_InputIterator2
>::value_type
>)
1869 __glibcxx_requires_valid_range(__first1
, __last1
);
1870 __glibcxx_requires_valid_range(__first2
, __last2
);
1872 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
, __last2
,
1873 __gnu_cxx::__ops::__iter_equal_to_iter());
1877 * @brief Finds the places in ranges which don't match.
1878 * @ingroup non_mutating_algorithms
1879 * @param __first1 An input iterator.
1880 * @param __last1 An input iterator.
1881 * @param __first2 An input iterator.
1882 * @param __last2 An input iterator.
1883 * @param __binary_pred A binary predicate @link functors
1885 * @return A pair of iterators pointing to the first mismatch.
1887 * This compares the elements of two ranges using the binary_pred
1888 * parameter, and returns a pair
1889 * of iterators. The first iterator points into the first range, the
1890 * second iterator points into the second range, and the elements pointed
1891 * to by the iterators are not equal.
1893 template<typename _InputIterator1
, typename _InputIterator2
,
1894 typename _BinaryPredicate
>
1895 _GLIBCXX20_CONSTEXPR
1896 inline pair
<_InputIterator1
, _InputIterator2
>
1897 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1898 _InputIterator2 __first2
, _InputIterator2 __last2
,
1899 _BinaryPredicate __binary_pred
)
1901 // concept requirements
1902 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1903 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1904 __glibcxx_requires_valid_range(__first1
, __last1
);
1905 __glibcxx_requires_valid_range(__first2
, __last2
);
1907 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
, __last2
,
1908 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1912 _GLIBCXX_END_NAMESPACE_ALGO
1914 /// This is an overload used by find algos for the Input Iterator case.
1915 template<typename _InputIterator
, typename _Predicate
>
1916 _GLIBCXX20_CONSTEXPR
1917 inline _InputIterator
1918 __find_if(_InputIterator __first
, _InputIterator __last
,
1919 _Predicate __pred
, input_iterator_tag
)
1921 while (__first
!= __last
&& !__pred(__first
))
1926 /// This is an overload used by find algos for the RAI case.
1927 template<typename _RandomAccessIterator
, typename _Predicate
>
1928 _GLIBCXX20_CONSTEXPR
1929 _RandomAccessIterator
1930 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1931 _Predicate __pred
, random_access_iterator_tag
)
1933 typename iterator_traits
<_RandomAccessIterator
>::difference_type
1934 __trip_count
= (__last
- __first
) >> 2;
1936 for (; __trip_count
> 0; --__trip_count
)
1938 if (__pred(__first
))
1942 if (__pred(__first
))
1946 if (__pred(__first
))
1950 if (__pred(__first
))
1955 switch (__last
- __first
)
1958 if (__pred(__first
))
1962 if (__pred(__first
))
1966 if (__pred(__first
))
1975 template<typename _Iterator
, typename _Predicate
>
1976 _GLIBCXX20_CONSTEXPR
1978 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
1980 return __find_if(__first
, __last
, __pred
,
1981 std::__iterator_category(__first
));
1984 template<typename _InputIterator
, typename _Predicate
>
1985 _GLIBCXX20_CONSTEXPR
1986 typename iterator_traits
<_InputIterator
>::difference_type
1987 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
1989 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
1990 for (; __first
!= __last
; ++__first
)
1991 if (__pred(__first
))
1996 #if __cplusplus >= 201103L
1997 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
1998 typename _BinaryPredicate
>
1999 _GLIBCXX20_CONSTEXPR
2001 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2002 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
2004 // Efficiently compare identical prefixes: O(N) if sequences
2005 // have the same elements in the same order.
2006 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
2007 if (!__pred(__first1
, __first2
))
2010 if (__first1
== __last1
)
2013 // Establish __last2 assuming equal ranges by iterating over the
2014 // rest of the list.
2015 _ForwardIterator2 __last2
= __first2
;
2016 std::advance(__last2
, std::distance(__first1
, __last1
));
2017 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
2019 if (__scan
!= std::__find_if(__first1
, __scan
,
2020 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
2021 continue; // We've seen this one before.
2024 = std::__count_if(__first2
, __last2
,
2025 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
2026 if (0 == __matches
||
2027 std::__count_if(__scan
, __last1
,
2028 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
2036 * @brief Checks whether a permutation of the second sequence is equal
2037 * to the first sequence.
2038 * @ingroup non_mutating_algorithms
2039 * @param __first1 Start of first range.
2040 * @param __last1 End of first range.
2041 * @param __first2 Start of second range.
2042 * @return true if there exists a permutation of the elements in the range
2043 * [__first2, __first2 + (__last1 - __first1)), beginning with
2044 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2045 * returns true; otherwise, returns false.
2047 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
2048 _GLIBCXX20_CONSTEXPR
2050 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2051 _ForwardIterator2 __first2
)
2053 // concept requirements
2054 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
2055 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
2056 __glibcxx_function_requires(_EqualOpConcept
<
2057 typename iterator_traits
<_ForwardIterator1
>::value_type
,
2058 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
2059 __glibcxx_requires_valid_range(__first1
, __last1
);
2061 return std::__is_permutation(__first1
, __last1
, __first2
,
2062 __gnu_cxx::__ops::__iter_equal_to_iter());
2066 _GLIBCXX_END_NAMESPACE_VERSION
2069 // NB: This file is included within many other C++ includes, as a way
2070 // of getting the base algorithms. So, make sure that parallel bits
2071 // come in too if requested.
2072 #ifdef _GLIBCXX_PARALLEL
2073 # include <parallel/algobase.h>