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
>
90 __memcmp(const _Tp
* __first1
, const _Tp
* __first2
, size_t __num
)
92 #ifdef __cpp_lib_is_constant_evaluated
93 if (std::is_constant_evaluated())
95 for(; __num
> 0; ++__first1
, ++__first2
, --__num
)
96 if (*__first1
!= *__first2
)
97 return *__first1
< *__first2
? -1 : 1;
102 return __builtin_memcmp(__first1
, __first2
, sizeof(_Tp
) * __num
);
105 #if __cplusplus < 201103L
106 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
107 // nutshell, we are partially implementing the resolution of DR 187,
108 // when it's safe, i.e., the value_types are equal.
109 template<bool _BoolType
>
112 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
114 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
116 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
118 _ValueType1 __tmp
= *__a
;
125 struct __iter_swap
<true>
127 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
129 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
137 * @brief Swaps the contents of two iterators.
138 * @ingroup mutating_algorithms
139 * @param __a An iterator.
140 * @param __b Another iterator.
143 * This function swaps the values pointed to by two iterators, not the
144 * iterators themselves.
146 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
149 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
151 // concept requirements
152 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
154 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
157 #if __cplusplus < 201103L
158 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
160 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
163 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
165 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
168 typedef typename iterator_traits
<_ForwardIterator1
>::reference
170 typedef typename iterator_traits
<_ForwardIterator2
>::reference
172 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
173 && __are_same
<_ValueType1
&, _ReferenceType1
>::__value
174 && __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
177 // _GLIBCXX_RESOLVE_LIB_DEFECTS
178 // 187. iter_swap underspecified
184 * @brief Swap the elements of two sequences.
185 * @ingroup mutating_algorithms
186 * @param __first1 A forward iterator.
187 * @param __last1 A forward iterator.
188 * @param __first2 A forward iterator.
189 * @return An iterator equal to @p first2+(last1-first1).
191 * Swaps each element in the range @p [first1,last1) with the
192 * corresponding element in the range @p [first2,(last1-first1)).
193 * The ranges must not overlap.
195 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
198 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
199 _ForwardIterator2 __first2
)
201 // concept requirements
202 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
204 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
206 __glibcxx_requires_valid_range(__first1
, __last1
);
208 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
209 std::iter_swap(__first1
, __first2
);
214 * @brief This does what you think it does.
215 * @ingroup sorting_algorithms
216 * @param __a A thing of arbitrary type.
217 * @param __b Another thing of arbitrary type.
218 * @return The lesser of the parameters.
220 * This is the simple classic generic implementation. It will work on
221 * temporary expressions, since they are only evaluated once, unlike a
222 * preprocessor macro.
224 template<typename _Tp
>
227 min(const _Tp
& __a
, const _Tp
& __b
)
229 // concept requirements
230 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
231 //return __b < __a ? __b : __a;
238 * @brief This does what you think it does.
239 * @ingroup sorting_algorithms
240 * @param __a A thing of arbitrary type.
241 * @param __b Another thing of arbitrary type.
242 * @return The greater of the parameters.
244 * This is the simple classic generic implementation. It will work on
245 * temporary expressions, since they are only evaluated once, unlike a
246 * preprocessor macro.
248 template<typename _Tp
>
251 max(const _Tp
& __a
, const _Tp
& __b
)
253 // concept requirements
254 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
255 //return __a < __b ? __b : __a;
262 * @brief This does what you think it does.
263 * @ingroup sorting_algorithms
264 * @param __a A thing of arbitrary type.
265 * @param __b Another thing of arbitrary type.
266 * @param __comp A @link comparison_functors comparison functor@endlink.
267 * @return The lesser of the parameters.
269 * This will work on temporary expressions, since they are only evaluated
270 * once, unlike a preprocessor macro.
272 template<typename _Tp
, typename _Compare
>
275 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
277 //return __comp(__b, __a) ? __b : __a;
278 if (__comp(__b
, __a
))
284 * @brief This does what you think it does.
285 * @ingroup sorting_algorithms
286 * @param __a A thing of arbitrary type.
287 * @param __b Another thing of arbitrary type.
288 * @param __comp A @link comparison_functors comparison functor@endlink.
289 * @return The greater of the parameters.
291 * This will work on temporary expressions, since they are only evaluated
292 * once, unlike a preprocessor macro.
294 template<typename _Tp
, typename _Compare
>
297 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
299 //return __comp(__a, __b) ? __b : __a;
300 if (__comp(__a
, __b
))
305 // Fallback implementation of the function in bits/stl_iterator.h used to
306 // remove the __normal_iterator wrapper. See copy, fill, ...
307 template<typename _Iterator
>
310 __niter_base(_Iterator __it
)
311 _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible
<_Iterator
>::value
)
314 // Reverse the __niter_base transformation to get a
315 // __normal_iterator back again (this assumes that __normal_iterator
316 // is only used to wrap random access iterators, like pointers).
317 template<typename _From
, typename _To
>
320 __niter_wrap(_From __from
, _To __res
)
321 { return __from
+ (__res
- std::__niter_base(__from
)); }
323 // No need to wrap, iterator already has the right type.
324 template<typename _Iterator
>
327 __niter_wrap(const _Iterator
&, _Iterator __res
)
330 // All of these auxiliary structs serve two purposes. (1) Replace
331 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
332 // because the input and output ranges are permitted to overlap.)
333 // (2) If we're using random access iterators, then write the loop as
334 // a for loop with an explicit count.
336 template<bool _IsMove
, bool _IsSimple
, typename _Category
>
339 template<typename _II
, typename _OI
>
342 __copy_m(_II __first
, _II __last
, _OI __result
)
344 for (; __first
!= __last
; ++__result
, (void)++__first
)
345 *__result
= *__first
;
350 #if __cplusplus >= 201103L
351 template<typename _Category
>
352 struct __copy_move
<true, false, _Category
>
354 template<typename _II
, typename _OI
>
357 __copy_m(_II __first
, _II __last
, _OI __result
)
359 for (; __first
!= __last
; ++__result
, (void)++__first
)
360 *__result
= std::move(*__first
);
367 struct __copy_move
<false, false, random_access_iterator_tag
>
369 template<typename _II
, typename _OI
>
372 __copy_m(_II __first
, _II __last
, _OI __result
)
374 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
375 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
377 *__result
= *__first
;
385 #if __cplusplus >= 201103L
387 struct __copy_move
<true, false, random_access_iterator_tag
>
389 template<typename _II
, typename _OI
>
392 __copy_m(_II __first
, _II __last
, _OI __result
)
394 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
395 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
397 *__result
= std::move(*__first
);
406 template<bool _IsMove
>
407 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
409 template<typename _Tp
>
412 __copy_m(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
414 #if __cplusplus >= 201103L
415 using __assignable
= conditional
<_IsMove
,
416 is_move_assignable
<_Tp
>,
417 is_copy_assignable
<_Tp
>>;
418 // trivial types can have deleted assignment
419 static_assert( __assignable::type::value
, "type is not assignable" );
421 const ptrdiff_t _Num
= __last
- __first
;
423 __builtin_memmove(__result
, __first
, sizeof(_Tp
) * _Num
);
424 return __result
+ _Num
;
428 // Helpers for streambuf iterators (either istream or ostream).
429 // NB: avoid including <iosfwd>, relatively large.
430 template<typename _CharT
>
433 template<typename _CharT
, typename _Traits
>
434 class istreambuf_iterator
;
436 template<typename _CharT
, typename _Traits
>
437 class ostreambuf_iterator
;
439 template<bool _IsMove
, typename _CharT
>
440 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
441 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
442 __copy_move_a2(_CharT
*, _CharT
*,
443 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
445 template<bool _IsMove
, typename _CharT
>
446 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
447 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
448 __copy_move_a2(const _CharT
*, const _CharT
*,
449 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
451 template<bool _IsMove
, typename _CharT
>
452 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
454 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
455 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
457 template<bool _IsMove
, typename _II
, typename _OI
>
460 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
462 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
463 #ifdef __cpp_lib_is_constant_evaluated
464 if (std::is_constant_evaluated())
465 return std::__copy_move
<_IsMove
, false, _Category
>::
466 __copy_m(__first
, __last
, __result
);
468 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
469 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
470 const bool __simple
= (__is_trivially_copyable(_ValueTypeI
)
471 && __is_pointer
<_II
>::__value
472 && __is_pointer
<_OI
>::__value
473 && __are_same
<_ValueTypeI
, _ValueTypeO
>::__value
);
474 return std::__copy_move
<_IsMove
, __simple
,
475 _Category
>::__copy_m(__first
, __last
, __result
);
478 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
480 template<typename _Tp
, typename _Ref
, typename _Ptr
>
481 struct _Deque_iterator
;
483 _GLIBCXX_END_NAMESPACE_CONTAINER
485 template<bool _IsMove
,
486 typename _Tp
, typename _Ref
, typename _Ptr
, typename _OI
>
488 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
489 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
492 template<bool _IsMove
,
493 typename _ITp
, typename _IRef
, typename _IPtr
, typename _OTp
>
494 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>
495 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
496 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
497 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>);
499 template<bool _IsMove
, typename _II
, typename _Tp
>
500 typename
__gnu_cxx::__enable_if
<
501 __is_random_access_iter
<_II
>::__value
,
502 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> >::__type
503 __copy_move_a1(_II
, _II
, _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
505 template<bool _IsMove
, typename _II
, typename _OI
>
508 __copy_move_a1(_II __first
, _II __last
, _OI __result
)
509 { return std::__copy_move_a2
<_IsMove
>(__first
, __last
, __result
); }
511 template<bool _IsMove
, typename _II
, typename _OI
>
514 __copy_move_a(_II __first
, _II __last
, _OI __result
)
516 return std::__niter_wrap(__result
,
517 std::__copy_move_a1
<_IsMove
>(std::__niter_base(__first
),
518 std::__niter_base(__last
),
519 std::__niter_base(__result
)));
522 template<bool _IsMove
,
523 typename _Ite
, typename _Seq
, typename _Cat
, typename _OI
>
525 __copy_move_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
526 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
529 template<bool _IsMove
,
530 typename _II
, typename _Ite
, typename _Seq
, typename _Cat
>
531 __gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
532 __copy_move_a(_II
, _II
,
533 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&);
535 template<bool _IsMove
,
536 typename _IIte
, typename _ISeq
, typename _ICat
,
537 typename _OIte
, typename _OSeq
, typename _OCat
>
538 ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>
539 __copy_move_a(const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
540 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
541 const ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>&);
544 * @brief Copies the range [first,last) into result.
545 * @ingroup mutating_algorithms
546 * @param __first An input iterator.
547 * @param __last An input iterator.
548 * @param __result An output iterator.
549 * @return result + (first - last)
551 * This inline function will boil down to a call to @c memmove whenever
552 * possible. Failing that, if random access iterators are passed, then the
553 * loop count will be known (and therefore a candidate for compiler
554 * optimizations such as unrolling). Result may not be contained within
555 * [first,last); the copy_backward function should be used instead.
557 * Note that the end of the output range is permitted to be contained
558 * within [first,last).
560 template<typename _II
, typename _OI
>
563 copy(_II __first
, _II __last
, _OI __result
)
565 // concept requirements
566 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
567 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
568 typename iterator_traits
<_II
>::value_type
>)
569 __glibcxx_requires_can_increment_range(__first
, __last
, __result
);
571 return std::__copy_move_a
<__is_move_iterator
<_II
>::__value
>
572 (std::__miter_base(__first
), std::__miter_base(__last
), __result
);
575 #if __cplusplus >= 201103L
577 * @brief Moves the range [first,last) into result.
578 * @ingroup mutating_algorithms
579 * @param __first An input iterator.
580 * @param __last An input iterator.
581 * @param __result An output iterator.
582 * @return result + (first - last)
584 * This inline function will boil down to a call to @c memmove whenever
585 * possible. Failing that, if random access iterators are passed, then the
586 * loop count will be known (and therefore a candidate for compiler
587 * optimizations such as unrolling). Result may not be contained within
588 * [first,last); the move_backward function should be used instead.
590 * Note that the end of the output range is permitted to be contained
591 * within [first,last).
593 template<typename _II
, typename _OI
>
596 move(_II __first
, _II __last
, _OI __result
)
598 // concept requirements
599 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
600 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
601 typename iterator_traits
<_II
>::value_type
>)
602 __glibcxx_requires_can_increment_range(__first
, __last
, __result
);
604 return std::__copy_move_a
<true>(std::__miter_base(__first
),
605 std::__miter_base(__last
), __result
);
608 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
610 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
613 template<bool _IsMove
, bool _IsSimple
, typename _Category
>
614 struct __copy_move_backward
616 template<typename _BI1
, typename _BI2
>
619 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
621 while (__first
!= __last
)
622 *--__result
= *--__last
;
627 #if __cplusplus >= 201103L
628 template<typename _Category
>
629 struct __copy_move_backward
<true, false, _Category
>
631 template<typename _BI1
, typename _BI2
>
634 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
636 while (__first
!= __last
)
637 *--__result
= std::move(*--__last
);
644 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
646 template<typename _BI1
, typename _BI2
>
649 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
651 typename iterator_traits
<_BI1
>::difference_type
652 __n
= __last
- __first
;
653 for (; __n
> 0; --__n
)
654 *--__result
= *--__last
;
659 #if __cplusplus >= 201103L
661 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
663 template<typename _BI1
, typename _BI2
>
666 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
668 typename iterator_traits
<_BI1
>::difference_type
669 __n
= __last
- __first
;
670 for (; __n
> 0; --__n
)
671 *--__result
= std::move(*--__last
);
677 template<bool _IsMove
>
678 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
680 template<typename _Tp
>
683 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
685 #if __cplusplus >= 201103L
686 using __assignable
= conditional
<_IsMove
,
687 is_move_assignable
<_Tp
>,
688 is_copy_assignable
<_Tp
>>;
689 // trivial types can have deleted assignment
690 static_assert( __assignable::type::value
, "type is not assignable" );
692 const ptrdiff_t _Num
= __last
- __first
;
694 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
695 return __result
- _Num
;
699 template<bool _IsMove
, typename _BI1
, typename _BI2
>
702 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
704 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
705 #ifdef __cpp_lib_is_constant_evaluated
706 if (std::is_constant_evaluated())
707 return std::__copy_move_backward
<_IsMove
, false, _Category
>::
708 __copy_move_b(__first
, __last
, __result
);
710 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
711 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
712 const bool __simple
= (__is_trivially_copyable(_ValueType1
)
713 && __is_pointer
<_BI1
>::__value
714 && __is_pointer
<_BI2
>::__value
715 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
717 return std::__copy_move_backward
<_IsMove
, __simple
,
718 _Category
>::__copy_move_b(__first
,
723 template<bool _IsMove
, typename _BI1
, typename _BI2
>
726 __copy_move_backward_a1(_BI1 __first
, _BI1 __last
, _BI2 __result
)
727 { return std::__copy_move_backward_a2
<_IsMove
>(__first
, __last
, __result
); }
729 template<bool _IsMove
,
730 typename _Tp
, typename _Ref
, typename _Ptr
, typename _OI
>
732 __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
733 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
736 template<bool _IsMove
,
737 typename _ITp
, typename _IRef
, typename _IPtr
, typename _OTp
>
738 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>
739 __copy_move_backward_a1(
740 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
741 _GLIBCXX_STD_C::_Deque_iterator
<_ITp
, _IRef
, _IPtr
>,
742 _GLIBCXX_STD_C::_Deque_iterator
<_OTp
, _OTp
&, _OTp
*>);
744 template<bool _IsMove
, typename _II
, typename _Tp
>
745 typename
__gnu_cxx::__enable_if
<
746 __is_random_access_iter
<_II
>::__value
,
747 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> >::__type
748 __copy_move_backward_a1(_II
, _II
,
749 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
751 template<bool _IsMove
, typename _II
, typename _OI
>
754 __copy_move_backward_a(_II __first
, _II __last
, _OI __result
)
756 return std::__niter_wrap(__result
,
757 std::__copy_move_backward_a1
<_IsMove
>
758 (std::__niter_base(__first
), std::__niter_base(__last
),
759 std::__niter_base(__result
)));
762 template<bool _IsMove
,
763 typename _Ite
, typename _Seq
, typename _Cat
, typename _OI
>
765 __copy_move_backward_a(
766 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
767 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
770 template<bool _IsMove
,
771 typename _II
, typename _Ite
, typename _Seq
, typename _Cat
>
772 __gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
773 __copy_move_backward_a(_II
, _II
,
774 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&);
776 template<bool _IsMove
,
777 typename _IIte
, typename _ISeq
, typename _ICat
,
778 typename _OIte
, typename _OSeq
, typename _OCat
>
779 ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>
780 __copy_move_backward_a(
781 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
782 const ::__gnu_debug::_Safe_iterator
<_IIte
, _ISeq
, _ICat
>&,
783 const ::__gnu_debug::_Safe_iterator
<_OIte
, _OSeq
, _OCat
>&);
786 * @brief Copies the range [first,last) into result.
787 * @ingroup mutating_algorithms
788 * @param __first A bidirectional iterator.
789 * @param __last A bidirectional iterator.
790 * @param __result A bidirectional iterator.
791 * @return result - (first - last)
793 * The function has the same effect as copy, but starts at the end of the
794 * range and works its way to the start, returning the start of the result.
795 * This inline function will boil down to a call to @c memmove whenever
796 * possible. Failing that, if random access iterators are passed, then the
797 * loop count will be known (and therefore a candidate for compiler
798 * optimizations such as unrolling).
800 * Result may not be in the range (first,last]. Use copy instead. Note
801 * that the start of the output range may overlap [first,last).
803 template<typename _BI1
, typename _BI2
>
806 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
808 // concept requirements
809 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
810 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
811 __glibcxx_function_requires(_ConvertibleConcept
<
812 typename iterator_traits
<_BI1
>::value_type
,
813 typename iterator_traits
<_BI2
>::value_type
>)
814 __glibcxx_requires_can_decrement_range(__first
, __last
, __result
);
816 return std::__copy_move_backward_a
<__is_move_iterator
<_BI1
>::__value
>
817 (std::__miter_base(__first
), std::__miter_base(__last
), __result
);
820 #if __cplusplus >= 201103L
822 * @brief Moves the range [first,last) into result.
823 * @ingroup mutating_algorithms
824 * @param __first A bidirectional iterator.
825 * @param __last A bidirectional iterator.
826 * @param __result A bidirectional iterator.
827 * @return result - (first - last)
829 * The function has the same effect as move, but starts at the end of the
830 * range and works its way to the start, returning the start of the result.
831 * This inline function will boil down to a call to @c memmove whenever
832 * possible. Failing that, if random access iterators are passed, then the
833 * loop count will be known (and therefore a candidate for compiler
834 * optimizations such as unrolling).
836 * Result may not be in the range (first,last]. Use move instead. Note
837 * that the start of the output range may overlap [first,last).
839 template<typename _BI1
, typename _BI2
>
842 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
844 // concept requirements
845 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
846 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
847 __glibcxx_function_requires(_ConvertibleConcept
<
848 typename iterator_traits
<_BI1
>::value_type
,
849 typename iterator_traits
<_BI2
>::value_type
>)
850 __glibcxx_requires_can_decrement_range(__first
, __last
, __result
);
852 return std::__copy_move_backward_a
<true>(std::__miter_base(__first
),
853 std::__miter_base(__last
),
857 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
859 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
862 template<typename _ForwardIterator
, typename _Tp
>
865 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
866 __fill_a1(_ForwardIterator __first
, _ForwardIterator __last
,
869 for (; __first
!= __last
; ++__first
)
873 template<typename _ForwardIterator
, typename _Tp
>
876 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
877 __fill_a1(_ForwardIterator __first
, _ForwardIterator __last
,
880 const _Tp __tmp
= __value
;
881 for (; __first
!= __last
; ++__first
)
885 // Specialization: for char types we can use memset.
886 template<typename _Tp
>
888 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
889 __fill_a1(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
891 const _Tp __tmp
= __c
;
892 if (const size_t __len
= __last
- __first
)
893 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
), __len
);
896 template<typename _Ite
, typename _Cont
, typename _Tp
>
899 __fill_a1(::__gnu_cxx::__normal_iterator
<_Ite
, _Cont
> __first
,
900 ::__gnu_cxx::__normal_iterator
<_Ite
, _Cont
> __last
,
902 { std::__fill_a1(__first
.base(), __last
.base(), __value
); }
904 template<typename _Tp
, typename _VTp
>
906 __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
907 const _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
910 template<typename _FIte
, typename _Tp
>
913 __fill_a(_FIte __first
, _FIte __last
, const _Tp
& __value
)
914 { std::__fill_a1(__first
, __last
, __value
); }
916 template<typename _Ite
, typename _Seq
, typename _Cat
, typename _Tp
>
918 __fill_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
919 const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>&,
923 * @brief Fills the range [first,last) with copies of value.
924 * @ingroup mutating_algorithms
925 * @param __first A forward iterator.
926 * @param __last A forward iterator.
927 * @param __value A reference-to-const of arbitrary type.
930 * This function fills a range with copies of the same value. For char
931 * types filling contiguous areas of memory, this becomes an inline call
932 * to @c memset or @c wmemset.
934 template<typename _ForwardIterator
, typename _Tp
>
937 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
939 // concept requirements
940 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
942 __glibcxx_requires_valid_range(__first
, __last
);
944 std::__fill_a(__first
, __last
, __value
);
947 // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
948 inline _GLIBCXX_CONSTEXPR
int
949 __size_to_integer(int __n
) { return __n
; }
950 inline _GLIBCXX_CONSTEXPR
unsigned
951 __size_to_integer(unsigned __n
) { return __n
; }
952 inline _GLIBCXX_CONSTEXPR
long
953 __size_to_integer(long __n
) { return __n
; }
954 inline _GLIBCXX_CONSTEXPR
unsigned long
955 __size_to_integer(unsigned long __n
) { return __n
; }
956 inline _GLIBCXX_CONSTEXPR
long long
957 __size_to_integer(long long __n
) { return __n
; }
958 inline _GLIBCXX_CONSTEXPR
unsigned long long
959 __size_to_integer(unsigned long long __n
) { return __n
; }
961 #if defined(__GLIBCXX_TYPE_INT_N_0)
962 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
963 __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n
) { return __n
; }
964 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_0
965 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n
) { return __n
; }
967 #if defined(__GLIBCXX_TYPE_INT_N_1)
968 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
969 __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n
) { return __n
; }
970 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_1
971 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n
) { return __n
; }
973 #if defined(__GLIBCXX_TYPE_INT_N_2)
974 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
975 __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n
) { return __n
; }
976 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_2
977 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n
) { return __n
; }
979 #if defined(__GLIBCXX_TYPE_INT_N_3)
980 inline _GLIBCXX_CONSTEXPR
unsigned __GLIBCXX_TYPE_INT_N_3
981 __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n
) { return __n
; }
982 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
983 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n
) { return __n
; }
986 inline _GLIBCXX_CONSTEXPR
long long
987 __size_to_integer(float __n
) { return __n
; }
988 inline _GLIBCXX_CONSTEXPR
long long
989 __size_to_integer(double __n
) { return __n
; }
990 inline _GLIBCXX_CONSTEXPR
long long
991 __size_to_integer(long double __n
) { return __n
; }
992 #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
993 inline _GLIBCXX_CONSTEXPR
long long
994 __size_to_integer(__float128 __n
) { return __n
; }
997 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1000 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
1001 __fill_n_a1(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
1003 for (; __n
> 0; --__n
, (void) ++__first
)
1008 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1009 _GLIBCXX20_CONSTEXPR
1011 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
1012 __fill_n_a1(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
1014 const _Tp __tmp
= __value
;
1015 for (; __n
> 0; --__n
, (void) ++__first
)
1020 template<typename _Ite
, typename _Seq
, typename _Cat
, typename _Size
,
1022 ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>
1023 __fill_n_a(const ::__gnu_debug::_Safe_iterator
<_Ite
, _Seq
, _Cat
>& __first
,
1024 _Size __n
, const _Tp
& __value
,
1025 std::input_iterator_tag
);
1027 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1028 _GLIBCXX20_CONSTEXPR
1029 inline _OutputIterator
1030 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1031 std::output_iterator_tag
)
1033 #if __cplusplus >= 201103L
1034 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1036 return __fill_n_a1(__first
, __n
, __value
);
1039 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1040 _GLIBCXX20_CONSTEXPR
1041 inline _OutputIterator
1042 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1043 std::input_iterator_tag
)
1045 #if __cplusplus >= 201103L
1046 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1048 return __fill_n_a1(__first
, __n
, __value
);
1051 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
1052 _GLIBCXX20_CONSTEXPR
1053 inline _OutputIterator
1054 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
,
1055 std::random_access_iterator_tag
)
1057 #if __cplusplus >= 201103L
1058 static_assert(is_integral
<_Size
>{}, "fill_n must pass integral size");
1063 __glibcxx_requires_can_increment(__first
, __n
);
1065 std::__fill_a(__first
, __first
+ __n
, __value
);
1066 return __first
+ __n
;
1070 * @brief Fills the range [first,first+n) with copies of value.
1071 * @ingroup mutating_algorithms
1072 * @param __first An output iterator.
1073 * @param __n The count of copies to perform.
1074 * @param __value A reference-to-const of arbitrary type.
1075 * @return The iterator at first+n.
1077 * This function fills a range with copies of the same value. For char
1078 * types filling contiguous areas of memory, this becomes an inline call
1079 * to @c memset or @c wmemset.
1081 * If @p __n is negative, the function does nothing.
1083 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1084 // DR 865. More algorithms that throw away information
1085 // DR 426. search_n(), fill_n(), and generate_n() with negative n
1086 template<typename _OI
, typename _Size
, typename _Tp
>
1087 _GLIBCXX20_CONSTEXPR
1089 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
1091 // concept requirements
1092 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
1094 return std::__fill_n_a(__first
, std::__size_to_integer(__n
), __value
,
1095 std::__iterator_category(__first
));
1098 template<bool _BoolType
>
1101 template<typename _II1
, typename _II2
>
1102 _GLIBCXX20_CONSTEXPR
1104 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1106 for (; __first1
!= __last1
; ++__first1
, (void) ++__first2
)
1107 if (!(*__first1
== *__first2
))
1114 struct __equal
<true>
1116 template<typename _Tp
>
1117 _GLIBCXX20_CONSTEXPR
1119 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
1121 if (const size_t __len
= (__last1
- __first1
))
1122 return !std::__memcmp(__first1
, __first2
, __len
);
1127 template<typename _Tp
, typename _Ref
, typename _Ptr
, typename _II
>
1128 typename
__gnu_cxx::__enable_if
<
1129 __is_random_access_iter
<_II
>::__value
, bool>::__type
1130 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
1131 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>,
1134 template<typename _Tp1
, typename _Ref1
, typename _Ptr1
,
1135 typename _Tp2
, typename _Ref2
, typename _Ptr2
>
1137 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1138 _GLIBCXX_STD_C::_Deque_iterator
<_Tp1
, _Ref1
, _Ptr1
>,
1139 _GLIBCXX_STD_C::_Deque_iterator
<_Tp2
, _Ref2
, _Ptr2
>);
1141 template<typename _II
, typename _Tp
, typename _Ref
, typename _Ptr
>
1142 typename
__gnu_cxx::__enable_if
<
1143 __is_random_access_iter
<_II
>::__value
, bool>::__type
1144 __equal_aux1(_II
, _II
,
1145 _GLIBCXX_STD_C::_Deque_iterator
<_Tp
, _Ref
, _Ptr
>);
1147 template<typename _II1
, typename _II2
>
1148 _GLIBCXX20_CONSTEXPR
1150 __equal_aux1(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1152 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1153 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1154 const bool __simple
= ((__is_integer
<_ValueType1
>::__value
1155 || __is_pointer
<_ValueType1
>::__value
)
1156 && __is_pointer
<_II1
>::__value
1157 && __is_pointer
<_II2
>::__value
1158 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
1160 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
1163 template<typename _II1
, typename _II2
>
1164 _GLIBCXX20_CONSTEXPR
1166 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1168 return std::__equal_aux1(std::__niter_base(__first1
),
1169 std::__niter_base(__last1
),
1170 std::__niter_base(__first2
));
1173 template<typename _II1
, typename _Seq1
, typename _Cat1
, typename _II2
>
1175 __equal_aux(const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1176 const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1179 template<typename _II1
, typename _II2
, typename _Seq2
, typename _Cat2
>
1181 __equal_aux(_II1
, _II1
,
1182 const ::__gnu_debug::_Safe_iterator
<_II2
, _Seq2
, _Cat2
>&);
1184 template<typename _II1
, typename _Seq1
, typename _Cat1
,
1185 typename _II2
, typename _Seq2
, typename _Cat2
>
1187 __equal_aux(const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1188 const ::__gnu_debug::_Safe_iterator
<_II1
, _Seq1
, _Cat1
>&,
1189 const ::__gnu_debug::_Safe_iterator
<_II2
, _Seq2
, _Cat2
>&);
1191 template<typename
, typename
>
1194 template<typename _II1
, typename _II2
>
1195 _GLIBCXX20_CONSTEXPR
1197 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
1200 template<typename _II
>
1201 _GLIBCXX20_CONSTEXPR
1203 __cnd2(_II __first
, _II __last
)
1204 { return __first
!= __last
; }
1208 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
1210 template<typename _RAI1
, typename _RAI2
>
1211 _GLIBCXX20_CONSTEXPR
1213 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
1214 _RAI2 __first2
, _RAI2 __last2
)
1216 const typename iterator_traits
<_RAI1
>::difference_type
1217 __diff1
= __last1
- __first1
;
1218 const typename iterator_traits
<_RAI2
>::difference_type
1219 __diff2
= __last2
- __first2
;
1220 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
1223 template<typename _RAI
>
1224 static _GLIBCXX20_CONSTEXPR
bool
1229 template<typename _II1
, typename _II2
, typename _Compare
>
1230 _GLIBCXX20_CONSTEXPR
1232 __lexicographical_compare_impl(_II1 __first1
, _II1 __last1
,
1233 _II2 __first2
, _II2 __last2
,
1236 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1237 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1238 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1240 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1241 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1242 ++__first1
, (void)++__first2
)
1244 if (__comp(__first1
, __first2
))
1246 if (__comp(__first2
, __first1
))
1249 return __first1
== __last1
&& __first2
!= __last2
;
1252 template<bool _BoolType
>
1253 struct __lexicographical_compare
1255 template<typename _II1
, typename _II2
>
1256 _GLIBCXX20_CONSTEXPR
1258 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1260 using __gnu_cxx::__ops::__iter_less_iter
;
1261 return std::__lexicographical_compare_impl(__first1
, __last1
,
1263 __iter_less_iter());
1268 struct __lexicographical_compare
<true>
1270 template<typename _Tp
, typename _Up
>
1271 _GLIBCXX20_CONSTEXPR
1273 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
1274 const _Up
* __first2
, const _Up
* __last2
)
1276 const size_t __len1
= __last1
- __first1
;
1277 const size_t __len2
= __last2
- __first2
;
1278 if (const size_t __len
= std::min(__len1
, __len2
))
1279 if (int __result
= std::__memcmp(__first1
, __first2
, __len
))
1280 return __result
< 0;
1281 return __len1
< __len2
;
1285 template<typename _II1
, typename _II2
>
1286 _GLIBCXX20_CONSTEXPR
1288 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
1289 _II2 __first2
, _II2 __last2
)
1291 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1292 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1293 const bool __simple
=
1294 (__is_byte
<_ValueType1
>::__value
&& __is_byte
<_ValueType2
>::__value
1295 && !__gnu_cxx::__numeric_traits
<_ValueType1
>::__is_signed
1296 && !__gnu_cxx::__numeric_traits
<_ValueType2
>::__is_signed
1297 && __is_pointer
<_II1
>::__value
1298 && __is_pointer
<_II2
>::__value
);
1300 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
1304 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1305 _GLIBCXX20_CONSTEXPR
1307 __lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1308 const _Tp
& __val
, _Compare __comp
)
1310 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1313 _DistanceType __len
= std::distance(__first
, __last
);
1317 _DistanceType __half
= __len
>> 1;
1318 _ForwardIterator __middle
= __first
;
1319 std::advance(__middle
, __half
);
1320 if (__comp(__middle
, __val
))
1324 __len
= __len
- __half
- 1;
1333 * @brief Finds the first position in which @a val could be inserted
1334 * without changing the ordering.
1335 * @param __first An iterator.
1336 * @param __last Another iterator.
1337 * @param __val The search term.
1338 * @return An iterator pointing to the first element <em>not less
1339 * than</em> @a val, or end() if every element is less than
1341 * @ingroup binary_search_algorithms
1343 template<typename _ForwardIterator
, typename _Tp
>
1344 _GLIBCXX20_CONSTEXPR
1345 inline _ForwardIterator
1346 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1349 // concept requirements
1350 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1351 __glibcxx_function_requires(_LessThanOpConcept
<
1352 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1353 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
1355 return std::__lower_bound(__first
, __last
, __val
,
1356 __gnu_cxx::__ops::__iter_less_val());
1359 /// This is a helper function for the sort routines and for random.tcc.
1360 // Precondition: __n > 0.
1361 inline _GLIBCXX_CONSTEXPR
int
1363 { return (int)sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1365 inline _GLIBCXX_CONSTEXPR
unsigned
1367 { return (int)sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1369 inline _GLIBCXX_CONSTEXPR
long
1371 { return (int)sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1373 inline _GLIBCXX_CONSTEXPR
unsigned long
1374 __lg(unsigned long __n
)
1375 { return (int)sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1377 inline _GLIBCXX_CONSTEXPR
long long
1379 { return (int)sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1381 inline _GLIBCXX_CONSTEXPR
unsigned long long
1382 __lg(unsigned long long __n
)
1383 { return (int)sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1385 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1388 * @brief Tests a range for element-wise equality.
1389 * @ingroup non_mutating_algorithms
1390 * @param __first1 An input iterator.
1391 * @param __last1 An input iterator.
1392 * @param __first2 An input iterator.
1393 * @return A boolean true or false.
1395 * This compares the elements of two ranges using @c == and returns true or
1396 * false depending on whether all of the corresponding elements of the
1399 template<typename _II1
, typename _II2
>
1400 _GLIBCXX20_CONSTEXPR
1402 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1404 // concept requirements
1405 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1406 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1407 __glibcxx_function_requires(_EqualOpConcept
<
1408 typename iterator_traits
<_II1
>::value_type
,
1409 typename iterator_traits
<_II2
>::value_type
>)
1410 __glibcxx_requires_can_increment_range(__first1
, __last1
, __first2
);
1412 return std::__equal_aux(__first1
, __last1
, __first2
);
1416 * @brief Tests a range for element-wise equality.
1417 * @ingroup non_mutating_algorithms
1418 * @param __first1 An input iterator.
1419 * @param __last1 An input iterator.
1420 * @param __first2 An input iterator.
1421 * @param __binary_pred A binary predicate @link functors
1423 * @return A boolean true or false.
1425 * This compares the elements of two ranges using the binary_pred
1426 * parameter, and returns true or
1427 * false depending on whether all of the corresponding elements of the
1430 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1431 _GLIBCXX20_CONSTEXPR
1433 equal(_IIter1 __first1
, _IIter1 __last1
,
1434 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
1436 // concept requirements
1437 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1438 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1439 __glibcxx_requires_valid_range(__first1
, __last1
);
1441 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
1442 if (!bool(__binary_pred(*__first1
, *__first2
)))
1447 #if __cplusplus >= 201103L
1448 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1449 template<typename _II1
, typename _II2
>
1450 _GLIBCXX20_CONSTEXPR
1452 __equal4(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1454 using _RATag
= random_access_iterator_tag
;
1455 using _Cat1
= typename iterator_traits
<_II1
>::iterator_category
;
1456 using _Cat2
= typename iterator_traits
<_II2
>::iterator_category
;
1457 using _RAIters
= __and_
<is_same
<_Cat1
, _RATag
>, is_same
<_Cat2
, _RATag
>>;
1460 auto __d1
= std::distance(__first1
, __last1
);
1461 auto __d2
= std::distance(__first2
, __last2
);
1464 return _GLIBCXX_STD_A::equal(__first1
, __last1
, __first2
);
1467 for (; __first1
!= __last1
&& __first2
!= __last2
;
1468 ++__first1
, (void)++__first2
)
1469 if (!(*__first1
== *__first2
))
1471 return __first1
== __last1
&& __first2
== __last2
;
1474 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1475 template<typename _II1
, typename _II2
, typename _BinaryPredicate
>
1476 _GLIBCXX20_CONSTEXPR
1478 __equal4(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
,
1479 _BinaryPredicate __binary_pred
)
1481 using _RATag
= random_access_iterator_tag
;
1482 using _Cat1
= typename iterator_traits
<_II1
>::iterator_category
;
1483 using _Cat2
= typename iterator_traits
<_II2
>::iterator_category
;
1484 using _RAIters
= __and_
<is_same
<_Cat1
, _RATag
>, is_same
<_Cat2
, _RATag
>>;
1487 auto __d1
= std::distance(__first1
, __last1
);
1488 auto __d2
= std::distance(__first2
, __last2
);
1491 return _GLIBCXX_STD_A::equal(__first1
, __last1
, __first2
,
1495 for (; __first1
!= __last1
&& __first2
!= __last2
;
1496 ++__first1
, (void)++__first2
)
1497 if (!bool(__binary_pred(*__first1
, *__first2
)))
1499 return __first1
== __last1
&& __first2
== __last2
;
1503 #if __cplusplus > 201103L
1505 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1508 * @brief Tests a range for element-wise equality.
1509 * @ingroup non_mutating_algorithms
1510 * @param __first1 An input iterator.
1511 * @param __last1 An input iterator.
1512 * @param __first2 An input iterator.
1513 * @param __last2 An input iterator.
1514 * @return A boolean true or false.
1516 * This compares the elements of two ranges using @c == and returns true or
1517 * false depending on whether all of the corresponding elements of the
1520 template<typename _II1
, typename _II2
>
1521 _GLIBCXX20_CONSTEXPR
1523 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
1525 // concept requirements
1526 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1527 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1528 __glibcxx_function_requires(_EqualOpConcept
<
1529 typename iterator_traits
<_II1
>::value_type
,
1530 typename iterator_traits
<_II2
>::value_type
>)
1531 __glibcxx_requires_valid_range(__first1
, __last1
);
1532 __glibcxx_requires_valid_range(__first2
, __last2
);
1534 return _GLIBCXX_STD_A::__equal4(__first1
, __last1
, __first2
, __last2
);
1538 * @brief Tests a range for element-wise equality.
1539 * @ingroup non_mutating_algorithms
1540 * @param __first1 An input iterator.
1541 * @param __last1 An input iterator.
1542 * @param __first2 An input iterator.
1543 * @param __last2 An input iterator.
1544 * @param __binary_pred A binary predicate @link functors
1546 * @return A boolean true or false.
1548 * This compares the elements of two ranges using the binary_pred
1549 * parameter, and returns true or
1550 * false depending on whether all of the corresponding elements of the
1553 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1554 _GLIBCXX20_CONSTEXPR
1556 equal(_IIter1 __first1
, _IIter1 __last1
,
1557 _IIter2 __first2
, _IIter2 __last2
, _BinaryPredicate __binary_pred
)
1559 // concept requirements
1560 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1561 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1562 __glibcxx_requires_valid_range(__first1
, __last1
);
1563 __glibcxx_requires_valid_range(__first2
, __last2
);
1565 return _GLIBCXX_STD_A::__equal4(__first1
, __last1
, __first2
, __last2
,
1571 * @brief Performs @b dictionary comparison on ranges.
1572 * @ingroup sorting_algorithms
1573 * @param __first1 An input iterator.
1574 * @param __last1 An input iterator.
1575 * @param __first2 An input iterator.
1576 * @param __last2 An input iterator.
1577 * @return A boolean true or false.
1579 * <em>Returns true if the sequence of elements defined by the range
1580 * [first1,last1) is lexicographically less than the sequence of elements
1581 * defined by the range [first2,last2). Returns false otherwise.</em>
1582 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1583 * then this is an inline call to @c memcmp.
1585 template<typename _II1
, typename _II2
>
1586 _GLIBCXX20_CONSTEXPR
1588 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1589 _II2 __first2
, _II2 __last2
)
1591 #ifdef _GLIBCXX_CONCEPT_CHECKS
1592 // concept requirements
1593 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1594 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1596 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1597 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1598 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1599 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1600 __glibcxx_requires_valid_range(__first1
, __last1
);
1601 __glibcxx_requires_valid_range(__first2
, __last2
);
1603 return std::__lexicographical_compare_aux(std::__niter_base(__first1
),
1604 std::__niter_base(__last1
),
1605 std::__niter_base(__first2
),
1606 std::__niter_base(__last2
));
1610 * @brief Performs @b dictionary comparison on ranges.
1611 * @ingroup sorting_algorithms
1612 * @param __first1 An input iterator.
1613 * @param __last1 An input iterator.
1614 * @param __first2 An input iterator.
1615 * @param __last2 An input iterator.
1616 * @param __comp A @link comparison_functors comparison functor@endlink.
1617 * @return A boolean true or false.
1619 * The same as the four-parameter @c lexicographical_compare, but uses the
1620 * comp parameter instead of @c <.
1622 template<typename _II1
, typename _II2
, typename _Compare
>
1623 _GLIBCXX20_CONSTEXPR
1625 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1626 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1628 // concept requirements
1629 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1630 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1631 __glibcxx_requires_valid_range(__first1
, __last1
);
1632 __glibcxx_requires_valid_range(__first2
, __last2
);
1634 return std::__lexicographical_compare_impl
1635 (__first1
, __last1
, __first2
, __last2
,
1636 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1639 #if __cpp_lib_three_way_comparison
1640 // Iter points to a contiguous range of unsigned narrow character type
1641 // or std::byte, suitable for comparison by memcmp.
1642 template<typename _Iter
>
1643 concept __is_byte_iter
= contiguous_iterator
<_Iter
>
1644 && __is_byte
<iter_value_t
<_Iter
>>::__value
!= 0
1645 && !__gnu_cxx::__numeric_traits
<iter_value_t
<_Iter
>>::__is_signed
;
1647 // Return a struct with two members, initialized to the smaller of x and y
1648 // (or x if they compare equal) and the result of the comparison x <=> y.
1649 template<typename _Tp
>
1651 __min_cmp(_Tp __x
, _Tp __y
)
1655 decltype(__x
<=> __y
) _M_cmp
;
1657 auto __c
= __x
<=> __y
;
1659 return _Res
{__y
, __c
};
1660 return _Res
{__x
, __c
};
1664 * @brief Performs dictionary comparison on ranges.
1665 * @ingroup sorting_algorithms
1666 * @param __first1 An input iterator.
1667 * @param __last1 An input iterator.
1668 * @param __first2 An input iterator.
1669 * @param __last2 An input iterator.
1670 * @param __comp A @link comparison_functors comparison functor@endlink.
1671 * @return The comparison category that `__comp(*__first1, *__first2)`
1674 template<typename _InputIter1
, typename _InputIter2
, typename _Comp
>
1676 lexicographical_compare_three_way(_InputIter1 __first1
,
1677 _InputIter1 __last1
,
1678 _InputIter2 __first2
,
1679 _InputIter2 __last2
,
1681 -> decltype(__comp(*__first1
, *__first2
))
1683 // concept requirements
1684 __glibcxx_function_requires(_InputIteratorConcept
<_InputIter1
>)
1685 __glibcxx_function_requires(_InputIteratorConcept
<_InputIter2
>)
1686 __glibcxx_requires_valid_range(__first1
, __last1
);
1687 __glibcxx_requires_valid_range(__first2
, __last2
);
1689 #if __cpp_lib_is_constant_evaluated
1690 using _Cat
= decltype(__comp(*__first1
, *__first2
));
1691 static_assert(same_as
<common_comparison_category_t
<_Cat
>, _Cat
>);
1693 if (!std::is_constant_evaluated())
1694 if constexpr (same_as
<_Comp
, __detail::_Synth3way
>
1695 || same_as
<_Comp
, compare_three_way
>)
1696 if constexpr (__is_byte_iter
<_InputIter1
>)
1697 if constexpr (__is_byte_iter
<_InputIter2
>)
1699 const auto [__len
, __lencmp
]
1700 = std::__min_cmp(__last1
- __first1
, __last2
- __first2
);
1704 = __builtin_memcmp(&*__first1
, &*__first2
, __len
) <=> 0;
1710 #endif // is_constant_evaluated
1711 while (__first1
!= __last1
&& __first2
!= __last2
)
1713 if (auto __cmp
= __comp(*__first1
, *__first2
); __cmp
!= 0)
1718 return __first1
!= __last1
? strong_ordering::greater
1719 : __first2
!= __last2
? strong_ordering::less
: strong_ordering::equal
;
1722 template<typename _InputIter1
, typename _InputIter2
>
1724 lexicographical_compare_three_way(_InputIter1 __first1
,
1725 _InputIter1 __last1
,
1726 _InputIter2 __first2
,
1727 _InputIter2 __last2
)
1729 return std::lexicographical_compare_three_way(__first1
, __last1
,
1731 compare_three_way
{});
1733 #endif // three_way_comparison
1735 template<typename _InputIterator1
, typename _InputIterator2
,
1736 typename _BinaryPredicate
>
1737 _GLIBCXX20_CONSTEXPR
1738 pair
<_InputIterator1
, _InputIterator2
>
1739 __mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1740 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1742 while (__first1
!= __last1
&& __binary_pred(__first1
, __first2
))
1747 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1751 * @brief Finds the places in ranges which don't match.
1752 * @ingroup non_mutating_algorithms
1753 * @param __first1 An input iterator.
1754 * @param __last1 An input iterator.
1755 * @param __first2 An input iterator.
1756 * @return A pair of iterators pointing to the first mismatch.
1758 * This compares the elements of two ranges using @c == and returns a pair
1759 * of iterators. The first iterator points into the first range, the
1760 * second iterator points into the second range, and the elements pointed
1761 * to by the iterators are not equal.
1763 template<typename _InputIterator1
, typename _InputIterator2
>
1764 _GLIBCXX20_CONSTEXPR
1765 inline pair
<_InputIterator1
, _InputIterator2
>
1766 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1767 _InputIterator2 __first2
)
1769 // concept requirements
1770 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1771 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1772 __glibcxx_function_requires(_EqualOpConcept
<
1773 typename iterator_traits
<_InputIterator1
>::value_type
,
1774 typename iterator_traits
<_InputIterator2
>::value_type
>)
1775 __glibcxx_requires_valid_range(__first1
, __last1
);
1777 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
,
1778 __gnu_cxx::__ops::__iter_equal_to_iter());
1782 * @brief Finds the places in ranges which don't match.
1783 * @ingroup non_mutating_algorithms
1784 * @param __first1 An input iterator.
1785 * @param __last1 An input iterator.
1786 * @param __first2 An input iterator.
1787 * @param __binary_pred A binary predicate @link functors
1789 * @return A pair of iterators pointing to the first mismatch.
1791 * This compares the elements of two ranges using the binary_pred
1792 * parameter, and returns a pair
1793 * of iterators. The first iterator points into the first range, the
1794 * second iterator points into the second range, and the elements pointed
1795 * to by the iterators are not equal.
1797 template<typename _InputIterator1
, typename _InputIterator2
,
1798 typename _BinaryPredicate
>
1799 _GLIBCXX20_CONSTEXPR
1800 inline pair
<_InputIterator1
, _InputIterator2
>
1801 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1802 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1804 // concept requirements
1805 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1806 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1807 __glibcxx_requires_valid_range(__first1
, __last1
);
1809 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
,
1810 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1813 #if __cplusplus > 201103L
1815 template<typename _InputIterator1
, typename _InputIterator2
,
1816 typename _BinaryPredicate
>
1817 _GLIBCXX20_CONSTEXPR
1818 pair
<_InputIterator1
, _InputIterator2
>
1819 __mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1820 _InputIterator2 __first2
, _InputIterator2 __last2
,
1821 _BinaryPredicate __binary_pred
)
1823 while (__first1
!= __last1
&& __first2
!= __last2
1824 && __binary_pred(__first1
, __first2
))
1829 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1833 * @brief Finds the places in ranges which don't match.
1834 * @ingroup non_mutating_algorithms
1835 * @param __first1 An input iterator.
1836 * @param __last1 An input iterator.
1837 * @param __first2 An input iterator.
1838 * @param __last2 An input iterator.
1839 * @return A pair of iterators pointing to the first mismatch.
1841 * This compares the elements of two ranges using @c == and returns a pair
1842 * of iterators. The first iterator points into the first range, the
1843 * second iterator points into the second range, and the elements pointed
1844 * to by the iterators are not equal.
1846 template<typename _InputIterator1
, typename _InputIterator2
>
1847 _GLIBCXX20_CONSTEXPR
1848 inline pair
<_InputIterator1
, _InputIterator2
>
1849 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1850 _InputIterator2 __first2
, _InputIterator2 __last2
)
1852 // concept requirements
1853 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1854 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1855 __glibcxx_function_requires(_EqualOpConcept
<
1856 typename iterator_traits
<_InputIterator1
>::value_type
,
1857 typename iterator_traits
<_InputIterator2
>::value_type
>)
1858 __glibcxx_requires_valid_range(__first1
, __last1
);
1859 __glibcxx_requires_valid_range(__first2
, __last2
);
1861 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
, __last2
,
1862 __gnu_cxx::__ops::__iter_equal_to_iter());
1866 * @brief Finds the places in ranges which don't match.
1867 * @ingroup non_mutating_algorithms
1868 * @param __first1 An input iterator.
1869 * @param __last1 An input iterator.
1870 * @param __first2 An input iterator.
1871 * @param __last2 An input iterator.
1872 * @param __binary_pred A binary predicate @link functors
1874 * @return A pair of iterators pointing to the first mismatch.
1876 * This compares the elements of two ranges using the binary_pred
1877 * parameter, and returns a pair
1878 * of iterators. The first iterator points into the first range, the
1879 * second iterator points into the second range, and the elements pointed
1880 * to by the iterators are not equal.
1882 template<typename _InputIterator1
, typename _InputIterator2
,
1883 typename _BinaryPredicate
>
1884 _GLIBCXX20_CONSTEXPR
1885 inline pair
<_InputIterator1
, _InputIterator2
>
1886 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1887 _InputIterator2 __first2
, _InputIterator2 __last2
,
1888 _BinaryPredicate __binary_pred
)
1890 // concept requirements
1891 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1893 __glibcxx_requires_valid_range(__first1
, __last1
);
1894 __glibcxx_requires_valid_range(__first2
, __last2
);
1896 return _GLIBCXX_STD_A::__mismatch(__first1
, __last1
, __first2
, __last2
,
1897 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1901 _GLIBCXX_END_NAMESPACE_ALGO
1903 /// This is an overload used by find algos for the Input Iterator case.
1904 template<typename _InputIterator
, typename _Predicate
>
1905 _GLIBCXX20_CONSTEXPR
1906 inline _InputIterator
1907 __find_if(_InputIterator __first
, _InputIterator __last
,
1908 _Predicate __pred
, input_iterator_tag
)
1910 while (__first
!= __last
&& !__pred(__first
))
1915 /// This is an overload used by find algos for the RAI case.
1916 template<typename _RandomAccessIterator
, typename _Predicate
>
1917 _GLIBCXX20_CONSTEXPR
1918 _RandomAccessIterator
1919 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1920 _Predicate __pred
, random_access_iterator_tag
)
1922 typename iterator_traits
<_RandomAccessIterator
>::difference_type
1923 __trip_count
= (__last
- __first
) >> 2;
1925 for (; __trip_count
> 0; --__trip_count
)
1927 if (__pred(__first
))
1931 if (__pred(__first
))
1935 if (__pred(__first
))
1939 if (__pred(__first
))
1944 switch (__last
- __first
)
1947 if (__pred(__first
))
1951 if (__pred(__first
))
1955 if (__pred(__first
))
1964 template<typename _Iterator
, typename _Predicate
>
1965 _GLIBCXX20_CONSTEXPR
1967 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
1969 return __find_if(__first
, __last
, __pred
,
1970 std::__iterator_category(__first
));
1973 template<typename _InputIterator
, typename _Predicate
>
1974 _GLIBCXX20_CONSTEXPR
1975 typename iterator_traits
<_InputIterator
>::difference_type
1976 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
1978 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
1979 for (; __first
!= __last
; ++__first
)
1980 if (__pred(__first
))
1985 #if __cplusplus >= 201103L
1986 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
1987 typename _BinaryPredicate
>
1988 _GLIBCXX20_CONSTEXPR
1990 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1991 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
1993 // Efficiently compare identical prefixes: O(N) if sequences
1994 // have the same elements in the same order.
1995 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
1996 if (!__pred(__first1
, __first2
))
1999 if (__first1
== __last1
)
2002 // Establish __last2 assuming equal ranges by iterating over the
2003 // rest of the list.
2004 _ForwardIterator2 __last2
= __first2
;
2005 std::advance(__last2
, std::distance(__first1
, __last1
));
2006 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
2008 if (__scan
!= std::__find_if(__first1
, __scan
,
2009 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
2010 continue; // We've seen this one before.
2013 = std::__count_if(__first2
, __last2
,
2014 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
2015 if (0 == __matches
||
2016 std::__count_if(__scan
, __last1
,
2017 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
2025 * @brief Checks whether a permutation of the second sequence is equal
2026 * to the first sequence.
2027 * @ingroup non_mutating_algorithms
2028 * @param __first1 Start of first range.
2029 * @param __last1 End of first range.
2030 * @param __first2 Start of second range.
2031 * @return true if there exists a permutation of the elements in the range
2032 * [__first2, __first2 + (__last1 - __first1)), beginning with
2033 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2034 * returns true; otherwise, returns false.
2036 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
2037 _GLIBCXX20_CONSTEXPR
2039 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
2040 _ForwardIterator2 __first2
)
2042 // concept requirements
2043 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
2044 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
2045 __glibcxx_function_requires(_EqualOpConcept
<
2046 typename iterator_traits
<_ForwardIterator1
>::value_type
,
2047 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
2048 __glibcxx_requires_valid_range(__first1
, __last1
);
2050 return std::__is_permutation(__first1
, __last1
, __first2
,
2051 __gnu_cxx::__ops::__iter_equal_to_iter());
2055 _GLIBCXX_END_NAMESPACE_VERSION
2058 // NB: This file is included within many other C++ includes, as a way
2059 // of getting the base algorithms. So, make sure that parallel bits
2060 // come in too if requested.
2061 #ifdef _GLIBCXX_PARALLEL
2062 # include <parallel/algobase.h>