1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2019 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.
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_algo.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
59 #include <cstdlib> // for rand
60 #include <bits/algorithmfwd.h>
61 #include <bits/stl_heap.h>
62 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
63 #include <bits/predefined_ops.h>
65 #if __cplusplus >= 201103L
66 #include <bits/uniform_int_dist.h>
69 // See concept_check.h for the __glibcxx_*_requires macros.
71 namespace std
_GLIBCXX_VISIBILITY(default)
73 _GLIBCXX_BEGIN_NAMESPACE_VERSION
75 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
76 template<typename _Iterator
, typename _Compare
>
78 __move_median_to_first(_Iterator __result
,_Iterator __a
, _Iterator __b
,
79 _Iterator __c
, _Compare __comp
)
84 std::iter_swap(__result
, __b
);
85 else if (__comp(__a
, __c
))
86 std::iter_swap(__result
, __c
);
88 std::iter_swap(__result
, __a
);
90 else if (__comp(__a
, __c
))
91 std::iter_swap(__result
, __a
);
92 else if (__comp(__b
, __c
))
93 std::iter_swap(__result
, __c
);
95 std::iter_swap(__result
, __b
);
98 /// This is an overload used by find algos for the Input Iterator case.
99 template<typename _InputIterator
, typename _Predicate
>
101 inline _InputIterator
102 __find_if(_InputIterator __first
, _InputIterator __last
,
103 _Predicate __pred
, input_iterator_tag
)
105 while (__first
!= __last
&& !__pred(__first
))
110 /// This is an overload used by find algos for the RAI case.
111 template<typename _RandomAccessIterator
, typename _Predicate
>
113 _RandomAccessIterator
114 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
115 _Predicate __pred
, random_access_iterator_tag
)
117 typename iterator_traits
<_RandomAccessIterator
>::difference_type
118 __trip_count
= (__last
- __first
) >> 2;
120 for (; __trip_count
> 0; --__trip_count
)
139 switch (__last
- __first
)
159 template<typename _Iterator
, typename _Predicate
>
162 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
164 return __find_if(__first
, __last
, __pred
,
165 std::__iterator_category(__first
));
168 /// Provided for stable_partition to use.
169 template<typename _InputIterator
, typename _Predicate
>
171 inline _InputIterator
172 __find_if_not(_InputIterator __first
, _InputIterator __last
,
175 return std::__find_if(__first
, __last
,
176 __gnu_cxx::__ops::__negate(__pred
),
177 std::__iterator_category(__first
));
180 /// Like find_if_not(), but uses and updates a count of the
181 /// remaining range length instead of comparing against an end
183 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
186 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
188 for (; __len
; --__len
, (void) ++__first
)
189 if (!__pred(__first
))
196 // set_symmetric_difference
207 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
208 typename _BinaryPredicate
>
211 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
212 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
213 _BinaryPredicate __predicate
)
215 // Test for empty ranges
216 if (__first1
== __last1
|| __first2
== __last2
)
219 // Test for a pattern of length 1.
220 _ForwardIterator2
__p1(__first2
);
221 if (++__p1
== __last2
)
222 return std::__find_if(__first1
, __last1
,
223 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
226 _ForwardIterator1 __current
= __first1
;
231 std::__find_if(__first1
, __last1
,
232 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
234 if (__first1
== __last1
)
237 _ForwardIterator2 __p
= __p1
;
238 __current
= __first1
;
239 if (++__current
== __last1
)
242 while (__predicate(__current
, __p
))
244 if (++__p
== __last2
)
246 if (++__current
== __last1
)
257 * This is an helper function for search_n overloaded for forward iterators.
259 template<typename _ForwardIterator
, typename _Integer
,
260 typename _UnaryPredicate
>
263 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
264 _Integer __count
, _UnaryPredicate __unary_pred
,
265 std::forward_iterator_tag
)
267 __first
= std::__find_if(__first
, __last
, __unary_pred
);
268 while (__first
!= __last
)
270 typename iterator_traits
<_ForwardIterator
>::difference_type
272 _ForwardIterator __i
= __first
;
274 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
283 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
289 * This is an helper function for search_n overloaded for random access
292 template<typename _RandomAccessIter
, typename _Integer
,
293 typename _UnaryPredicate
>
296 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
297 _Integer __count
, _UnaryPredicate __unary_pred
,
298 std::random_access_iterator_tag
)
300 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
303 _DistanceType __tailSize
= __last
- __first
;
304 _DistanceType __remainder
= __count
;
306 while (__remainder
<= __tailSize
) // the main loop...
308 __first
+= __remainder
;
309 __tailSize
-= __remainder
;
310 // __first here is always pointing to one past the last element of
311 // next possible match.
312 _RandomAccessIter __backTrack
= __first
;
313 while (__unary_pred(--__backTrack
))
315 if (--__remainder
== 0)
316 return (__first
- __count
); // Success
318 __remainder
= __count
+ 1 - (__first
- __backTrack
);
320 return __last
; // Failure
323 template<typename _ForwardIterator
, typename _Integer
,
324 typename _UnaryPredicate
>
327 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
329 _UnaryPredicate __unary_pred
)
335 return std::__find_if(__first
, __last
, __unary_pred
);
337 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
338 std::__iterator_category(__first
));
341 // find_end for forward iterators.
342 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
343 typename _BinaryPredicate
>
346 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
347 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
348 forward_iterator_tag
, forward_iterator_tag
,
349 _BinaryPredicate __comp
)
351 if (__first2
== __last2
)
354 _ForwardIterator1 __result
= __last1
;
357 _ForwardIterator1 __new_result
358 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
359 if (__new_result
== __last1
)
363 __result
= __new_result
;
364 __first1
= __new_result
;
370 // find_end for bidirectional iterators (much faster).
371 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
372 typename _BinaryPredicate
>
374 _BidirectionalIterator1
375 __find_end(_BidirectionalIterator1 __first1
,
376 _BidirectionalIterator1 __last1
,
377 _BidirectionalIterator2 __first2
,
378 _BidirectionalIterator2 __last2
,
379 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
380 _BinaryPredicate __comp
)
382 // concept requirements
383 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
384 _BidirectionalIterator1
>)
385 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
386 _BidirectionalIterator2
>)
388 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
389 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
391 _RevIterator1
__rlast1(__first1
);
392 _RevIterator2
__rlast2(__first2
);
393 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
394 _RevIterator2(__last2
), __rlast2
,
397 if (__rresult
== __rlast1
)
401 _BidirectionalIterator1 __result
= __rresult
.base();
402 std::advance(__result
, -std::distance(__first2
, __last2
));
408 * @brief Find last matching subsequence in a sequence.
409 * @ingroup non_mutating_algorithms
410 * @param __first1 Start of range to search.
411 * @param __last1 End of range to search.
412 * @param __first2 Start of sequence to match.
413 * @param __last2 End of sequence to match.
414 * @return The last iterator @c i in the range
415 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
416 * @p *(__first2+N) for each @c N in the range @p
417 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
419 * Searches the range @p [__first1,__last1) for a sub-sequence that
420 * compares equal value-by-value with the sequence given by @p
421 * [__first2,__last2) and returns an iterator to the __first
422 * element of the sub-sequence, or @p __last1 if the sub-sequence
423 * is not found. The sub-sequence will be the last such
424 * subsequence contained in [__first1,__last1).
426 * Because the sub-sequence must lie completely within the range @p
427 * [__first1,__last1) it must start at a position less than @p
428 * __last1-(__last2-__first2) where @p __last2-__first2 is the
429 * length of the sub-sequence. This means that the returned
430 * iterator @c i will be in the range @p
431 * [__first1,__last1-(__last2-__first2))
433 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
435 inline _ForwardIterator1
436 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
437 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
439 // concept requirements
440 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
441 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
442 __glibcxx_function_requires(_EqualOpConcept
<
443 typename iterator_traits
<_ForwardIterator1
>::value_type
,
444 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
445 __glibcxx_requires_valid_range(__first1
, __last1
);
446 __glibcxx_requires_valid_range(__first2
, __last2
);
448 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
449 std::__iterator_category(__first1
),
450 std::__iterator_category(__first2
),
451 __gnu_cxx::__ops::__iter_equal_to_iter());
455 * @brief Find last matching subsequence in a sequence using a predicate.
456 * @ingroup non_mutating_algorithms
457 * @param __first1 Start of range to search.
458 * @param __last1 End of range to search.
459 * @param __first2 Start of sequence to match.
460 * @param __last2 End of sequence to match.
461 * @param __comp The predicate to use.
462 * @return The last iterator @c i in the range @p
463 * [__first1,__last1-(__last2-__first2)) such that @c
464 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
465 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
468 * Searches the range @p [__first1,__last1) for a sub-sequence that
469 * compares equal value-by-value with the sequence given by @p
470 * [__first2,__last2) using comp as a predicate and returns an
471 * iterator to the first element of the sub-sequence, or @p __last1
472 * if the sub-sequence is not found. The sub-sequence will be the
473 * last such subsequence contained in [__first,__last1).
475 * Because the sub-sequence must lie completely within the range @p
476 * [__first1,__last1) it must start at a position less than @p
477 * __last1-(__last2-__first2) where @p __last2-__first2 is the
478 * length of the sub-sequence. This means that the returned
479 * iterator @c i will be in the range @p
480 * [__first1,__last1-(__last2-__first2))
482 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
483 typename _BinaryPredicate
>
485 inline _ForwardIterator1
486 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
487 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
488 _BinaryPredicate __comp
)
490 // concept requirements
491 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
492 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
493 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
494 typename iterator_traits
<_ForwardIterator1
>::value_type
,
495 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
496 __glibcxx_requires_valid_range(__first1
, __last1
);
497 __glibcxx_requires_valid_range(__first2
, __last2
);
499 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
500 std::__iterator_category(__first1
),
501 std::__iterator_category(__first2
),
502 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
505 #if __cplusplus >= 201103L
507 * @brief Checks that a predicate is true for all the elements
509 * @ingroup non_mutating_algorithms
510 * @param __first An input iterator.
511 * @param __last An input iterator.
512 * @param __pred A predicate.
513 * @return True if the check is true, false otherwise.
515 * Returns true if @p __pred is true for each element in the range
516 * @p [__first,__last), and false otherwise.
518 template<typename _InputIterator
, typename _Predicate
>
521 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
522 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
525 * @brief Checks that a predicate is false for all the elements
527 * @ingroup non_mutating_algorithms
528 * @param __first An input iterator.
529 * @param __last An input iterator.
530 * @param __pred A predicate.
531 * @return True if the check is true, false otherwise.
533 * Returns true if @p __pred is false for each element in the range
534 * @p [__first,__last), and false otherwise.
536 template<typename _InputIterator
, typename _Predicate
>
539 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
540 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
543 * @brief Checks that a predicate is false for at least an element
545 * @ingroup non_mutating_algorithms
546 * @param __first An input iterator.
547 * @param __last An input iterator.
548 * @param __pred A predicate.
549 * @return True if the check is true, false otherwise.
551 * Returns true if an element exists in the range @p
552 * [__first,__last) such that @p __pred is true, and false
555 template<typename _InputIterator
, typename _Predicate
>
558 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
559 { return !std::none_of(__first
, __last
, __pred
); }
562 * @brief Find the first element in a sequence for which a
563 * predicate is false.
564 * @ingroup non_mutating_algorithms
565 * @param __first An input iterator.
566 * @param __last An input iterator.
567 * @param __pred A predicate.
568 * @return The first iterator @c i in the range @p [__first,__last)
569 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
571 template<typename _InputIterator
, typename _Predicate
>
573 inline _InputIterator
574 find_if_not(_InputIterator __first
, _InputIterator __last
,
577 // concept requirements
578 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
579 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
580 typename iterator_traits
<_InputIterator
>::value_type
>)
581 __glibcxx_requires_valid_range(__first
, __last
);
582 return std::__find_if_not(__first
, __last
,
583 __gnu_cxx::__ops::__pred_iter(__pred
));
587 * @brief Checks whether the sequence is partitioned.
588 * @ingroup mutating_algorithms
589 * @param __first An input iterator.
590 * @param __last An input iterator.
591 * @param __pred A predicate.
592 * @return True if the range @p [__first,__last) is partioned by @p __pred,
593 * i.e. if all elements that satisfy @p __pred appear before those that
596 template<typename _InputIterator
, typename _Predicate
>
599 is_partitioned(_InputIterator __first
, _InputIterator __last
,
602 __first
= std::find_if_not(__first
, __last
, __pred
);
603 if (__first
== __last
)
606 return std::none_of(__first
, __last
, __pred
);
610 * @brief Find the partition point of a partitioned range.
611 * @ingroup mutating_algorithms
612 * @param __first An iterator.
613 * @param __last Another iterator.
614 * @param __pred A predicate.
615 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
616 * and @p none_of(mid, __last, __pred) are both true.
618 template<typename _ForwardIterator
, typename _Predicate
>
621 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
624 // concept requirements
625 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
626 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
627 typename iterator_traits
<_ForwardIterator
>::value_type
>)
629 // A specific debug-mode test will be necessary...
630 __glibcxx_requires_valid_range(__first
, __last
);
632 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
635 _DistanceType __len
= std::distance(__first
, __last
);
639 _DistanceType __half
= __len
>> 1;
640 _ForwardIterator __middle
= __first
;
641 std::advance(__middle
, __half
);
642 if (__pred(*__middle
))
646 __len
= __len
- __half
- 1;
655 template<typename _InputIterator
, typename _OutputIterator
,
659 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
660 _OutputIterator __result
, _Predicate __pred
)
662 for (; __first
!= __last
; ++__first
)
663 if (!__pred(__first
))
665 *__result
= *__first
;
672 * @brief Copy a sequence, removing elements of a given value.
673 * @ingroup mutating_algorithms
674 * @param __first An input iterator.
675 * @param __last An input iterator.
676 * @param __result An output iterator.
677 * @param __value The value to be removed.
678 * @return An iterator designating the end of the resulting sequence.
680 * Copies each element in the range @p [__first,__last) not equal
681 * to @p __value to the range beginning at @p __result.
682 * remove_copy() is stable, so the relative order of elements that
683 * are copied is unchanged.
685 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
687 inline _OutputIterator
688 remove_copy(_InputIterator __first
, _InputIterator __last
,
689 _OutputIterator __result
, const _Tp
& __value
)
691 // concept requirements
692 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
693 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
694 typename iterator_traits
<_InputIterator
>::value_type
>)
695 __glibcxx_function_requires(_EqualOpConcept
<
696 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
697 __glibcxx_requires_valid_range(__first
, __last
);
699 return std::__remove_copy_if(__first
, __last
, __result
,
700 __gnu_cxx::__ops::__iter_equals_val(__value
));
704 * @brief Copy a sequence, removing elements for which a predicate is true.
705 * @ingroup mutating_algorithms
706 * @param __first An input iterator.
707 * @param __last An input iterator.
708 * @param __result An output iterator.
709 * @param __pred A predicate.
710 * @return An iterator designating the end of the resulting sequence.
712 * Copies each element in the range @p [__first,__last) for which
713 * @p __pred returns false to the range beginning at @p __result.
715 * remove_copy_if() is stable, so the relative order of elements that are
716 * copied is unchanged.
718 template<typename _InputIterator
, typename _OutputIterator
,
721 inline _OutputIterator
722 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
723 _OutputIterator __result
, _Predicate __pred
)
725 // concept requirements
726 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
727 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
728 typename iterator_traits
<_InputIterator
>::value_type
>)
729 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
730 typename iterator_traits
<_InputIterator
>::value_type
>)
731 __glibcxx_requires_valid_range(__first
, __last
);
733 return std::__remove_copy_if(__first
, __last
, __result
,
734 __gnu_cxx::__ops::__pred_iter(__pred
));
737 #if __cplusplus >= 201103L
739 * @brief Copy the elements of a sequence for which a predicate is true.
740 * @ingroup mutating_algorithms
741 * @param __first An input iterator.
742 * @param __last An input iterator.
743 * @param __result An output iterator.
744 * @param __pred A predicate.
745 * @return An iterator designating the end of the resulting sequence.
747 * Copies each element in the range @p [__first,__last) for which
748 * @p __pred returns true to the range beginning at @p __result.
750 * copy_if() is stable, so the relative order of elements that are
751 * copied is unchanged.
753 template<typename _InputIterator
, typename _OutputIterator
,
757 copy_if(_InputIterator __first
, _InputIterator __last
,
758 _OutputIterator __result
, _Predicate __pred
)
760 // concept requirements
761 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
762 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
763 typename iterator_traits
<_InputIterator
>::value_type
>)
764 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
765 typename iterator_traits
<_InputIterator
>::value_type
>)
766 __glibcxx_requires_valid_range(__first
, __last
);
768 for (; __first
!= __last
; ++__first
)
769 if (__pred(*__first
))
771 *__result
= *__first
;
777 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
780 __copy_n(_InputIterator __first
, _Size __n
,
781 _OutputIterator __result
, input_iterator_tag
)
787 *__result
= *__first
;
798 template<typename _RandomAccessIterator
, typename _Size
,
799 typename _OutputIterator
>
801 inline _OutputIterator
802 __copy_n(_RandomAccessIterator __first
, _Size __n
,
803 _OutputIterator __result
, random_access_iterator_tag
)
804 { return std::copy(__first
, __first
+ __n
, __result
); }
807 * @brief Copies the range [first,first+n) into [result,result+n).
808 * @ingroup mutating_algorithms
809 * @param __first An input iterator.
810 * @param __n The number of elements to copy.
811 * @param __result An output iterator.
814 * This inline function will boil down to a call to @c memmove whenever
815 * possible. Failing that, if random access iterators are passed, then the
816 * loop count will be known (and therefore a candidate for compiler
817 * optimizations such as unrolling).
819 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
821 inline _OutputIterator
822 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
824 // concept requirements
825 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
826 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
827 typename iterator_traits
<_InputIterator
>::value_type
>)
829 return std::__copy_n(__first
, __n
, __result
,
830 std::__iterator_category(__first
));
834 * @brief Copy the elements of a sequence to separate output sequences
835 * depending on the truth value of a predicate.
836 * @ingroup mutating_algorithms
837 * @param __first An input iterator.
838 * @param __last An input iterator.
839 * @param __out_true An output iterator.
840 * @param __out_false An output iterator.
841 * @param __pred A predicate.
842 * @return A pair designating the ends of the resulting sequences.
844 * Copies each element in the range @p [__first,__last) for which
845 * @p __pred returns true to the range beginning at @p out_true
846 * and each element for which @p __pred returns false to @p __out_false.
848 template<typename _InputIterator
, typename _OutputIterator1
,
849 typename _OutputIterator2
, typename _Predicate
>
851 pair
<_OutputIterator1
, _OutputIterator2
>
852 partition_copy(_InputIterator __first
, _InputIterator __last
,
853 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
856 // concept requirements
857 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
858 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
859 typename iterator_traits
<_InputIterator
>::value_type
>)
860 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
861 typename iterator_traits
<_InputIterator
>::value_type
>)
862 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
863 typename iterator_traits
<_InputIterator
>::value_type
>)
864 __glibcxx_requires_valid_range(__first
, __last
);
866 for (; __first
!= __last
; ++__first
)
867 if (__pred(*__first
))
869 *__out_true
= *__first
;
874 *__out_false
= *__first
;
878 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
882 template<typename _ForwardIterator
, typename _Predicate
>
885 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
888 __first
= std::__find_if(__first
, __last
, __pred
);
889 if (__first
== __last
)
891 _ForwardIterator __result
= __first
;
893 for (; __first
!= __last
; ++__first
)
894 if (!__pred(__first
))
896 *__result
= _GLIBCXX_MOVE(*__first
);
903 * @brief Remove elements from a sequence.
904 * @ingroup mutating_algorithms
905 * @param __first An input iterator.
906 * @param __last An input iterator.
907 * @param __value The value to be removed.
908 * @return An iterator designating the end of the resulting sequence.
910 * All elements equal to @p __value are removed from the range
911 * @p [__first,__last).
913 * remove() is stable, so the relative order of elements that are
914 * not removed is unchanged.
916 * Elements between the end of the resulting sequence and @p __last
917 * are still present, but their value is unspecified.
919 template<typename _ForwardIterator
, typename _Tp
>
921 inline _ForwardIterator
922 remove(_ForwardIterator __first
, _ForwardIterator __last
,
925 // concept requirements
926 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
928 __glibcxx_function_requires(_EqualOpConcept
<
929 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
930 __glibcxx_requires_valid_range(__first
, __last
);
932 return std::__remove_if(__first
, __last
,
933 __gnu_cxx::__ops::__iter_equals_val(__value
));
937 * @brief Remove elements from a sequence using a predicate.
938 * @ingroup mutating_algorithms
939 * @param __first A forward iterator.
940 * @param __last A forward iterator.
941 * @param __pred A predicate.
942 * @return An iterator designating the end of the resulting sequence.
944 * All elements for which @p __pred returns true are removed from the range
945 * @p [__first,__last).
947 * remove_if() is stable, so the relative order of elements that are
948 * not removed is unchanged.
950 * Elements between the end of the resulting sequence and @p __last
951 * are still present, but their value is unspecified.
953 template<typename _ForwardIterator
, typename _Predicate
>
955 inline _ForwardIterator
956 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
959 // concept requirements
960 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
962 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
963 typename iterator_traits
<_ForwardIterator
>::value_type
>)
964 __glibcxx_requires_valid_range(__first
, __last
);
966 return std::__remove_if(__first
, __last
,
967 __gnu_cxx::__ops::__pred_iter(__pred
));
970 template<typename _ForwardIterator
, typename _BinaryPredicate
>
973 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
974 _BinaryPredicate __binary_pred
)
976 if (__first
== __last
)
978 _ForwardIterator __next
= __first
;
979 while (++__next
!= __last
)
981 if (__binary_pred(__first
, __next
))
988 template<typename _ForwardIterator
, typename _BinaryPredicate
>
991 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
992 _BinaryPredicate __binary_pred
)
994 // Skip the beginning, if already unique.
995 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
996 if (__first
== __last
)
999 // Do the real copy work.
1000 _ForwardIterator __dest
= __first
;
1002 while (++__first
!= __last
)
1003 if (!__binary_pred(__dest
, __first
))
1004 *++__dest
= _GLIBCXX_MOVE(*__first
);
1009 * @brief Remove consecutive duplicate values from a sequence.
1010 * @ingroup mutating_algorithms
1011 * @param __first A forward iterator.
1012 * @param __last A forward iterator.
1013 * @return An iterator designating the end of the resulting sequence.
1015 * Removes all but the first element from each group of consecutive
1016 * values that compare equal.
1017 * unique() is stable, so the relative order of elements that are
1018 * not removed is unchanged.
1019 * Elements between the end of the resulting sequence and @p __last
1020 * are still present, but their value is unspecified.
1022 template<typename _ForwardIterator
>
1023 _GLIBCXX20_CONSTEXPR
1024 inline _ForwardIterator
1025 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1027 // concept requirements
1028 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1030 __glibcxx_function_requires(_EqualityComparableConcept
<
1031 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1032 __glibcxx_requires_valid_range(__first
, __last
);
1034 return std::__unique(__first
, __last
,
1035 __gnu_cxx::__ops::__iter_equal_to_iter());
1039 * @brief Remove consecutive values from a sequence using a predicate.
1040 * @ingroup mutating_algorithms
1041 * @param __first A forward iterator.
1042 * @param __last A forward iterator.
1043 * @param __binary_pred A binary predicate.
1044 * @return An iterator designating the end of the resulting sequence.
1046 * Removes all but the first element from each group of consecutive
1047 * values for which @p __binary_pred returns true.
1048 * unique() is stable, so the relative order of elements that are
1049 * not removed is unchanged.
1050 * Elements between the end of the resulting sequence and @p __last
1051 * are still present, but their value is unspecified.
1053 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1054 _GLIBCXX20_CONSTEXPR
1055 inline _ForwardIterator
1056 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1057 _BinaryPredicate __binary_pred
)
1059 // concept requirements
1060 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1062 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1063 typename iterator_traits
<_ForwardIterator
>::value_type
,
1064 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1065 __glibcxx_requires_valid_range(__first
, __last
);
1067 return std::__unique(__first
, __last
,
1068 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1072 * This is an uglified
1073 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1075 * overloaded for forward iterators and output iterator as result.
1077 template<typename _ForwardIterator
, typename _OutputIterator
,
1078 typename _BinaryPredicate
>
1079 _GLIBCXX20_CONSTEXPR
1081 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1082 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1083 forward_iterator_tag
, output_iterator_tag
)
1085 // concept requirements -- iterators already checked
1086 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1087 typename iterator_traits
<_ForwardIterator
>::value_type
,
1088 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1090 _ForwardIterator __next
= __first
;
1091 *__result
= *__first
;
1092 while (++__next
!= __last
)
1093 if (!__binary_pred(__first
, __next
))
1096 *++__result
= *__first
;
1102 * This is an uglified
1103 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1105 * overloaded for input iterators and output iterator as result.
1107 template<typename _InputIterator
, typename _OutputIterator
,
1108 typename _BinaryPredicate
>
1109 _GLIBCXX20_CONSTEXPR
1111 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1112 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1113 input_iterator_tag
, output_iterator_tag
)
1115 // concept requirements -- iterators already checked
1116 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1117 typename iterator_traits
<_InputIterator
>::value_type
,
1118 typename iterator_traits
<_InputIterator
>::value_type
>)
1120 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1121 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1123 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1124 *__result
= __value
;
1125 while (++__first
!= __last
)
1126 if (!__rebound_pred(__first
, __value
))
1129 *++__result
= __value
;
1135 * This is an uglified
1136 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1138 * overloaded for input iterators and forward iterator as result.
1140 template<typename _InputIterator
, typename _ForwardIterator
,
1141 typename _BinaryPredicate
>
1142 _GLIBCXX20_CONSTEXPR
1144 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1145 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1146 input_iterator_tag
, forward_iterator_tag
)
1148 // concept requirements -- iterators already checked
1149 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1150 typename iterator_traits
<_ForwardIterator
>::value_type
,
1151 typename iterator_traits
<_InputIterator
>::value_type
>)
1152 *__result
= *__first
;
1153 while (++__first
!= __last
)
1154 if (!__binary_pred(__result
, __first
))
1155 *++__result
= *__first
;
1160 * This is an uglified reverse(_BidirectionalIterator,
1161 * _BidirectionalIterator)
1162 * overloaded for bidirectional iterators.
1164 template<typename _BidirectionalIterator
>
1166 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1167 bidirectional_iterator_tag
)
1170 if (__first
== __last
|| __first
== --__last
)
1174 std::iter_swap(__first
, __last
);
1180 * This is an uglified reverse(_BidirectionalIterator,
1181 * _BidirectionalIterator)
1182 * overloaded for random access iterators.
1184 template<typename _RandomAccessIterator
>
1186 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1187 random_access_iterator_tag
)
1189 if (__first
== __last
)
1192 while (__first
< __last
)
1194 std::iter_swap(__first
, __last
);
1201 * @brief Reverse a sequence.
1202 * @ingroup mutating_algorithms
1203 * @param __first A bidirectional iterator.
1204 * @param __last A bidirectional iterator.
1205 * @return reverse() returns no value.
1207 * Reverses the order of the elements in the range @p [__first,__last),
1208 * so that the first element becomes the last etc.
1209 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1210 * swaps @p *(__first+i) and @p *(__last-(i+1))
1212 template<typename _BidirectionalIterator
>
1214 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1216 // concept requirements
1217 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1218 _BidirectionalIterator
>)
1219 __glibcxx_requires_valid_range(__first
, __last
);
1220 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1224 * @brief Copy a sequence, reversing its elements.
1225 * @ingroup mutating_algorithms
1226 * @param __first A bidirectional iterator.
1227 * @param __last A bidirectional iterator.
1228 * @param __result An output iterator.
1229 * @return An iterator designating the end of the resulting sequence.
1231 * Copies the elements in the range @p [__first,__last) to the
1232 * range @p [__result,__result+(__last-__first)) such that the
1233 * order of the elements is reversed. For every @c i such that @p
1234 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1235 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1236 * The ranges @p [__first,__last) and @p
1237 * [__result,__result+(__last-__first)) must not overlap.
1239 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1240 _GLIBCXX20_CONSTEXPR
1242 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1243 _OutputIterator __result
)
1245 // concept requirements
1246 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1247 _BidirectionalIterator
>)
1248 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1249 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1250 __glibcxx_requires_valid_range(__first
, __last
);
1252 while (__first
!= __last
)
1255 *__result
= *__last
;
1262 * This is a helper function for the rotate algorithm specialized on RAIs.
1263 * It returns the greatest common divisor of two integer values.
1265 template<typename _EuclideanRingElement
>
1266 _EuclideanRingElement
1267 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1271 _EuclideanRingElement __t
= __m
% __n
;
1278 inline namespace _V2
1281 /// This is a helper function for the rotate algorithm.
1282 template<typename _ForwardIterator
>
1284 __rotate(_ForwardIterator __first
,
1285 _ForwardIterator __middle
,
1286 _ForwardIterator __last
,
1287 forward_iterator_tag
)
1289 if (__first
== __middle
)
1291 else if (__last
== __middle
)
1294 _ForwardIterator __first2
= __middle
;
1297 std::iter_swap(__first
, __first2
);
1300 if (__first
== __middle
)
1301 __middle
= __first2
;
1303 while (__first2
!= __last
);
1305 _ForwardIterator __ret
= __first
;
1307 __first2
= __middle
;
1309 while (__first2
!= __last
)
1311 std::iter_swap(__first
, __first2
);
1314 if (__first
== __middle
)
1315 __middle
= __first2
;
1316 else if (__first2
== __last
)
1317 __first2
= __middle
;
1322 /// This is a helper function for the rotate algorithm.
1323 template<typename _BidirectionalIterator
>
1324 _BidirectionalIterator
1325 __rotate(_BidirectionalIterator __first
,
1326 _BidirectionalIterator __middle
,
1327 _BidirectionalIterator __last
,
1328 bidirectional_iterator_tag
)
1330 // concept requirements
1331 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1332 _BidirectionalIterator
>)
1334 if (__first
== __middle
)
1336 else if (__last
== __middle
)
1339 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1340 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1342 while (__first
!= __middle
&& __middle
!= __last
)
1344 std::iter_swap(__first
, --__last
);
1348 if (__first
== __middle
)
1350 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1355 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1360 /// This is a helper function for the rotate algorithm.
1361 template<typename _RandomAccessIterator
>
1362 _RandomAccessIterator
1363 __rotate(_RandomAccessIterator __first
,
1364 _RandomAccessIterator __middle
,
1365 _RandomAccessIterator __last
,
1366 random_access_iterator_tag
)
1368 // concept requirements
1369 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1370 _RandomAccessIterator
>)
1372 if (__first
== __middle
)
1374 else if (__last
== __middle
)
1377 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1379 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1382 _Distance __n
= __last
- __first
;
1383 _Distance __k
= __middle
- __first
;
1385 if (__k
== __n
- __k
)
1387 std::swap_ranges(__first
, __middle
, __middle
);
1391 _RandomAccessIterator __p
= __first
;
1392 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1396 if (__k
< __n
- __k
)
1398 if (__is_pod(_ValueType
) && __k
== 1)
1400 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1401 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1402 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1405 _RandomAccessIterator __q
= __p
+ __k
;
1406 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1408 std::iter_swap(__p
, __q
);
1415 std::swap(__n
, __k
);
1421 if (__is_pod(_ValueType
) && __k
== 1)
1423 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1424 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1425 *__p
= _GLIBCXX_MOVE(__t
);
1428 _RandomAccessIterator __q
= __p
+ __n
;
1430 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1434 std::iter_swap(__p
, __q
);
1439 std::swap(__n
, __k
);
1444 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1445 // DR 488. rotate throws away useful information
1447 * @brief Rotate the elements of a sequence.
1448 * @ingroup mutating_algorithms
1449 * @param __first A forward iterator.
1450 * @param __middle A forward iterator.
1451 * @param __last A forward iterator.
1452 * @return first + (last - middle).
1454 * Rotates the elements of the range @p [__first,__last) by
1455 * @p (__middle - __first) positions so that the element at @p __middle
1456 * is moved to @p __first, the element at @p __middle+1 is moved to
1457 * @p __first+1 and so on for each element in the range
1458 * @p [__first,__last).
1460 * This effectively swaps the ranges @p [__first,__middle) and
1461 * @p [__middle,__last).
1464 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1465 * for each @p n in the range @p [0,__last-__first).
1467 template<typename _ForwardIterator
>
1468 inline _ForwardIterator
1469 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1470 _ForwardIterator __last
)
1472 // concept requirements
1473 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1475 __glibcxx_requires_valid_range(__first
, __middle
);
1476 __glibcxx_requires_valid_range(__middle
, __last
);
1478 return std::__rotate(__first
, __middle
, __last
,
1479 std::__iterator_category(__first
));
1485 * @brief Copy a sequence, rotating its elements.
1486 * @ingroup mutating_algorithms
1487 * @param __first A forward iterator.
1488 * @param __middle A forward iterator.
1489 * @param __last A forward iterator.
1490 * @param __result An output iterator.
1491 * @return An iterator designating the end of the resulting sequence.
1493 * Copies the elements of the range @p [__first,__last) to the
1494 * range beginning at @result, rotating the copied elements by
1495 * @p (__middle-__first) positions so that the element at @p __middle
1496 * is moved to @p __result, the element at @p __middle+1 is moved
1497 * to @p __result+1 and so on for each element in the range @p
1501 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1502 * for each @p n in the range @p [0,__last-__first).
1504 template<typename _ForwardIterator
, typename _OutputIterator
>
1505 _GLIBCXX20_CONSTEXPR
1506 inline _OutputIterator
1507 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1508 _ForwardIterator __last
, _OutputIterator __result
)
1510 // concept requirements
1511 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1512 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1513 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1514 __glibcxx_requires_valid_range(__first
, __middle
);
1515 __glibcxx_requires_valid_range(__middle
, __last
);
1517 return std::copy(__first
, __middle
,
1518 std::copy(__middle
, __last
, __result
));
1521 /// This is a helper function...
1522 template<typename _ForwardIterator
, typename _Predicate
>
1524 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1525 _Predicate __pred
, forward_iterator_tag
)
1527 if (__first
== __last
)
1530 while (__pred(*__first
))
1531 if (++__first
== __last
)
1534 _ForwardIterator __next
= __first
;
1536 while (++__next
!= __last
)
1537 if (__pred(*__next
))
1539 std::iter_swap(__first
, __next
);
1546 /// This is a helper function...
1547 template<typename _BidirectionalIterator
, typename _Predicate
>
1548 _BidirectionalIterator
1549 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1550 _Predicate __pred
, bidirectional_iterator_tag
)
1555 if (__first
== __last
)
1557 else if (__pred(*__first
))
1563 if (__first
== __last
)
1565 else if (!bool(__pred(*__last
)))
1569 std::iter_swap(__first
, __last
);
1576 /// This is a helper function...
1577 /// Requires __first != __last and !__pred(__first)
1578 /// and __len == distance(__first, __last).
1580 /// !__pred(__first) allows us to guarantee that we don't
1581 /// move-assign an element onto itself.
1582 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1585 __stable_partition_adaptive(_ForwardIterator __first
,
1586 _ForwardIterator __last
,
1587 _Predicate __pred
, _Distance __len
,
1589 _Distance __buffer_size
)
1594 if (__len
<= __buffer_size
)
1596 _ForwardIterator __result1
= __first
;
1597 _Pointer __result2
= __buffer
;
1599 // The precondition guarantees that !__pred(__first), so
1600 // move that element to the buffer before starting the loop.
1601 // This ensures that we only call __pred once per element.
1602 *__result2
= _GLIBCXX_MOVE(*__first
);
1605 for (; __first
!= __last
; ++__first
)
1606 if (__pred(__first
))
1608 *__result1
= _GLIBCXX_MOVE(*__first
);
1613 *__result2
= _GLIBCXX_MOVE(*__first
);
1617 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1621 _ForwardIterator __middle
= __first
;
1622 std::advance(__middle
, __len
/ 2);
1623 _ForwardIterator __left_split
=
1624 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1625 __len
/ 2, __buffer
,
1628 // Advance past true-predicate values to satisfy this
1629 // function's preconditions.
1630 _Distance __right_len
= __len
- __len
/ 2;
1631 _ForwardIterator __right_split
=
1632 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1636 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1638 __buffer
, __buffer_size
);
1640 return std::rotate(__left_split
, __middle
, __right_split
);
1643 template<typename _ForwardIterator
, typename _Predicate
>
1645 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1648 __first
= std::__find_if_not(__first
, __last
, __pred
);
1650 if (__first
== __last
)
1653 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1655 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1658 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1659 __buf(__first
, std::distance(__first
, __last
));
1661 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1662 _DistanceType(__buf
.requested_size()),
1664 _DistanceType(__buf
.size()));
1668 * @brief Move elements for which a predicate is true to the beginning
1669 * of a sequence, preserving relative ordering.
1670 * @ingroup mutating_algorithms
1671 * @param __first A forward iterator.
1672 * @param __last A forward iterator.
1673 * @param __pred A predicate functor.
1674 * @return An iterator @p middle such that @p __pred(i) is true for each
1675 * iterator @p i in the range @p [first,middle) and false for each @p i
1676 * in the range @p [middle,last).
1678 * Performs the same function as @p partition() with the additional
1679 * guarantee that the relative ordering of elements in each group is
1680 * preserved, so any two elements @p x and @p y in the range
1681 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1682 * relative ordering after calling @p stable_partition().
1684 template<typename _ForwardIterator
, typename _Predicate
>
1685 inline _ForwardIterator
1686 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1689 // concept requirements
1690 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1692 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1693 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1694 __glibcxx_requires_valid_range(__first
, __last
);
1696 return std::__stable_partition(__first
, __last
,
1697 __gnu_cxx::__ops::__pred_iter(__pred
));
1700 /// This is a helper function for the sort routines.
1701 template<typename _RandomAccessIterator
, typename _Compare
>
1703 __heap_select(_RandomAccessIterator __first
,
1704 _RandomAccessIterator __middle
,
1705 _RandomAccessIterator __last
, _Compare __comp
)
1707 std::__make_heap(__first
, __middle
, __comp
);
1708 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1709 if (__comp(__i
, __first
))
1710 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1715 template<typename _InputIterator
, typename _RandomAccessIterator
,
1717 _RandomAccessIterator
1718 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1719 _RandomAccessIterator __result_first
,
1720 _RandomAccessIterator __result_last
,
1723 typedef typename iterator_traits
<_InputIterator
>::value_type
1725 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1726 typedef typename
_RItTraits::difference_type _DistanceType
;
1728 if (__result_first
== __result_last
)
1729 return __result_last
;
1730 _RandomAccessIterator __result_real_last
= __result_first
;
1731 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1733 *__result_real_last
= *__first
;
1734 ++__result_real_last
;
1738 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1739 while (__first
!= __last
)
1741 if (__comp(__first
, __result_first
))
1742 std::__adjust_heap(__result_first
, _DistanceType(0),
1743 _DistanceType(__result_real_last
1745 _InputValueType(*__first
), __comp
);
1748 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1749 return __result_real_last
;
1753 * @brief Copy the smallest elements of a sequence.
1754 * @ingroup sorting_algorithms
1755 * @param __first An iterator.
1756 * @param __last Another iterator.
1757 * @param __result_first A random-access iterator.
1758 * @param __result_last Another random-access iterator.
1759 * @return An iterator indicating the end of the resulting sequence.
1761 * Copies and sorts the smallest N values from the range @p [__first,__last)
1762 * to the range beginning at @p __result_first, where the number of
1763 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1764 * @p (__result_last-__result_first).
1765 * After the sort if @e i and @e j are iterators in the range
1766 * @p [__result_first,__result_first+N) such that i precedes j then
1768 * The value returned is @p __result_first+N.
1770 template<typename _InputIterator
, typename _RandomAccessIterator
>
1771 inline _RandomAccessIterator
1772 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1773 _RandomAccessIterator __result_first
,
1774 _RandomAccessIterator __result_last
)
1776 #ifdef _GLIBCXX_CONCEPT_CHECKS
1777 typedef typename iterator_traits
<_InputIterator
>::value_type
1779 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1783 // concept requirements
1784 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1785 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1787 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1789 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1790 __glibcxx_requires_valid_range(__first
, __last
);
1791 __glibcxx_requires_irreflexive(__first
, __last
);
1792 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1794 return std::__partial_sort_copy(__first
, __last
,
1795 __result_first
, __result_last
,
1796 __gnu_cxx::__ops::__iter_less_iter());
1800 * @brief Copy the smallest elements of a sequence using a predicate for
1802 * @ingroup sorting_algorithms
1803 * @param __first An input iterator.
1804 * @param __last Another input iterator.
1805 * @param __result_first A random-access iterator.
1806 * @param __result_last Another random-access iterator.
1807 * @param __comp A comparison functor.
1808 * @return An iterator indicating the end of the resulting sequence.
1810 * Copies and sorts the smallest N values from the range @p [__first,__last)
1811 * to the range beginning at @p result_first, where the number of
1812 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1813 * @p (__result_last-__result_first).
1814 * After the sort if @e i and @e j are iterators in the range
1815 * @p [__result_first,__result_first+N) such that i precedes j then
1816 * @p __comp(*j,*i) is false.
1817 * The value returned is @p __result_first+N.
1819 template<typename _InputIterator
, typename _RandomAccessIterator
,
1821 inline _RandomAccessIterator
1822 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1823 _RandomAccessIterator __result_first
,
1824 _RandomAccessIterator __result_last
,
1827 #ifdef _GLIBCXX_CONCEPT_CHECKS
1828 typedef typename iterator_traits
<_InputIterator
>::value_type
1830 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1834 // concept requirements
1835 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1836 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1837 _RandomAccessIterator
>)
1838 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1840 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1841 _InputValueType
, _OutputValueType
>)
1842 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1843 _OutputValueType
, _OutputValueType
>)
1844 __glibcxx_requires_valid_range(__first
, __last
);
1845 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1846 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1848 return std::__partial_sort_copy(__first
, __last
,
1849 __result_first
, __result_last
,
1850 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1853 /// This is a helper function for the sort routine.
1854 template<typename _RandomAccessIterator
, typename _Compare
>
1855 _GLIBCXX20_CONSTEXPR
1857 __unguarded_linear_insert(_RandomAccessIterator __last
,
1860 typename iterator_traits
<_RandomAccessIterator
>::value_type
1861 __val
= _GLIBCXX_MOVE(*__last
);
1862 _RandomAccessIterator __next
= __last
;
1864 while (__comp(__val
, __next
))
1866 *__last
= _GLIBCXX_MOVE(*__next
);
1870 *__last
= _GLIBCXX_MOVE(__val
);
1873 /// This is a helper function for the sort routine.
1874 template<typename _RandomAccessIterator
, typename _Compare
>
1875 _GLIBCXX20_CONSTEXPR
1877 __insertion_sort(_RandomAccessIterator __first
,
1878 _RandomAccessIterator __last
, _Compare __comp
)
1880 if (__first
== __last
) return;
1882 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1884 if (__comp(__i
, __first
))
1886 typename iterator_traits
<_RandomAccessIterator
>::value_type
1887 __val
= _GLIBCXX_MOVE(*__i
);
1888 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1889 *__first
= _GLIBCXX_MOVE(__val
);
1892 std::__unguarded_linear_insert(__i
,
1893 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1897 /// This is a helper function for the sort routine.
1898 template<typename _RandomAccessIterator
, typename _Compare
>
1899 _GLIBCXX20_CONSTEXPR
1901 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1902 _RandomAccessIterator __last
, _Compare __comp
)
1904 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1905 std::__unguarded_linear_insert(__i
,
1906 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1911 * This controls some aspect of the sort routines.
1913 enum { _S_threshold
= 16 };
1915 /// This is a helper function for the sort routine.
1916 template<typename _RandomAccessIterator
, typename _Compare
>
1917 _GLIBCXX20_CONSTEXPR
1919 __final_insertion_sort(_RandomAccessIterator __first
,
1920 _RandomAccessIterator __last
, _Compare __comp
)
1922 if (__last
- __first
> int(_S_threshold
))
1924 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1925 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1929 std::__insertion_sort(__first
, __last
, __comp
);
1932 /// This is a helper function...
1933 template<typename _RandomAccessIterator
, typename _Compare
>
1934 _RandomAccessIterator
1935 __unguarded_partition(_RandomAccessIterator __first
,
1936 _RandomAccessIterator __last
,
1937 _RandomAccessIterator __pivot
, _Compare __comp
)
1941 while (__comp(__first
, __pivot
))
1944 while (__comp(__pivot
, __last
))
1946 if (!(__first
< __last
))
1948 std::iter_swap(__first
, __last
);
1953 /// This is a helper function...
1954 template<typename _RandomAccessIterator
, typename _Compare
>
1955 inline _RandomAccessIterator
1956 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1957 _RandomAccessIterator __last
, _Compare __comp
)
1959 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1960 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1962 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1965 template<typename _RandomAccessIterator
, typename _Compare
>
1967 __partial_sort(_RandomAccessIterator __first
,
1968 _RandomAccessIterator __middle
,
1969 _RandomAccessIterator __last
,
1972 std::__heap_select(__first
, __middle
, __last
, __comp
);
1973 std::__sort_heap(__first
, __middle
, __comp
);
1976 /// This is a helper function for the sort routine.
1977 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1979 __introsort_loop(_RandomAccessIterator __first
,
1980 _RandomAccessIterator __last
,
1981 _Size __depth_limit
, _Compare __comp
)
1983 while (__last
- __first
> int(_S_threshold
))
1985 if (__depth_limit
== 0)
1987 std::__partial_sort(__first
, __last
, __last
, __comp
);
1991 _RandomAccessIterator __cut
=
1992 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1993 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2000 template<typename _RandomAccessIterator
, typename _Compare
>
2002 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2005 if (__first
!= __last
)
2007 std::__introsort_loop(__first
, __last
,
2008 std::__lg(__last
- __first
) * 2,
2010 std::__final_insertion_sort(__first
, __last
, __comp
);
2014 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2016 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2017 _RandomAccessIterator __last
, _Size __depth_limit
,
2020 while (__last
- __first
> 3)
2022 if (__depth_limit
== 0)
2024 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2025 // Place the nth largest element in its final position.
2026 std::iter_swap(__first
, __nth
);
2030 _RandomAccessIterator __cut
=
2031 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2037 std::__insertion_sort(__first
, __last
, __comp
);
2042 // lower_bound moved to stl_algobase.h
2045 * @brief Finds the first position in which @p __val could be inserted
2046 * without changing the ordering.
2047 * @ingroup binary_search_algorithms
2048 * @param __first An iterator.
2049 * @param __last Another iterator.
2050 * @param __val The search term.
2051 * @param __comp A functor to use for comparisons.
2052 * @return An iterator pointing to the first element <em>not less
2053 * than</em> @p __val, or end() if every element is less
2055 * @ingroup binary_search_algorithms
2057 * The comparison function should have the same effects on ordering as
2058 * the function used for the initial sort.
2060 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2061 _GLIBCXX20_CONSTEXPR
2062 inline _ForwardIterator
2063 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2064 const _Tp
& __val
, _Compare __comp
)
2066 // concept requirements
2067 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2068 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2069 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2070 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2073 return std::__lower_bound(__first
, __last
, __val
,
2074 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2077 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2078 _GLIBCXX20_CONSTEXPR
2080 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2081 const _Tp
& __val
, _Compare __comp
)
2083 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2086 _DistanceType __len
= std::distance(__first
, __last
);
2090 _DistanceType __half
= __len
>> 1;
2091 _ForwardIterator __middle
= __first
;
2092 std::advance(__middle
, __half
);
2093 if (__comp(__val
, __middle
))
2099 __len
= __len
- __half
- 1;
2106 * @brief Finds the last position in which @p __val could be inserted
2107 * without changing the ordering.
2108 * @ingroup binary_search_algorithms
2109 * @param __first An iterator.
2110 * @param __last Another iterator.
2111 * @param __val The search term.
2112 * @return An iterator pointing to the first element greater than @p __val,
2113 * or end() if no elements are greater than @p __val.
2114 * @ingroup binary_search_algorithms
2116 template<typename _ForwardIterator
, typename _Tp
>
2117 _GLIBCXX20_CONSTEXPR
2118 inline _ForwardIterator
2119 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2122 // concept requirements
2123 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2124 __glibcxx_function_requires(_LessThanOpConcept
<
2125 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2126 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2128 return std::__upper_bound(__first
, __last
, __val
,
2129 __gnu_cxx::__ops::__val_less_iter());
2133 * @brief Finds the last position in which @p __val could be inserted
2134 * without changing the ordering.
2135 * @ingroup binary_search_algorithms
2136 * @param __first An iterator.
2137 * @param __last Another iterator.
2138 * @param __val The search term.
2139 * @param __comp A functor to use for comparisons.
2140 * @return An iterator pointing to the first element greater than @p __val,
2141 * or end() if no elements are greater than @p __val.
2142 * @ingroup binary_search_algorithms
2144 * The comparison function should have the same effects on ordering as
2145 * the function used for the initial sort.
2147 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2148 _GLIBCXX20_CONSTEXPR
2149 inline _ForwardIterator
2150 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2151 const _Tp
& __val
, _Compare __comp
)
2153 // concept requirements
2154 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2155 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2156 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2157 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2160 return std::__upper_bound(__first
, __last
, __val
,
2161 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2164 template<typename _ForwardIterator
, typename _Tp
,
2165 typename _CompareItTp
, typename _CompareTpIt
>
2166 _GLIBCXX20_CONSTEXPR
2167 pair
<_ForwardIterator
, _ForwardIterator
>
2168 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2170 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2172 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2175 _DistanceType __len
= std::distance(__first
, __last
);
2179 _DistanceType __half
= __len
>> 1;
2180 _ForwardIterator __middle
= __first
;
2181 std::advance(__middle
, __half
);
2182 if (__comp_it_val(__middle
, __val
))
2186 __len
= __len
- __half
- 1;
2188 else if (__comp_val_it(__val
, __middle
))
2192 _ForwardIterator __left
2193 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2194 std::advance(__first
, __len
);
2195 _ForwardIterator __right
2196 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2197 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2200 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2204 * @brief Finds the largest subrange in which @p __val could be inserted
2205 * at any place in it without changing the ordering.
2206 * @ingroup binary_search_algorithms
2207 * @param __first An iterator.
2208 * @param __last Another iterator.
2209 * @param __val The search term.
2210 * @return An pair of iterators defining the subrange.
2211 * @ingroup binary_search_algorithms
2213 * This is equivalent to
2215 * std::make_pair(lower_bound(__first, __last, __val),
2216 * upper_bound(__first, __last, __val))
2218 * but does not actually call those functions.
2220 template<typename _ForwardIterator
, typename _Tp
>
2221 _GLIBCXX20_CONSTEXPR
2222 inline pair
<_ForwardIterator
, _ForwardIterator
>
2223 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2226 // concept requirements
2227 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2228 __glibcxx_function_requires(_LessThanOpConcept
<
2229 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2230 __glibcxx_function_requires(_LessThanOpConcept
<
2231 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2232 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2233 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2235 return std::__equal_range(__first
, __last
, __val
,
2236 __gnu_cxx::__ops::__iter_less_val(),
2237 __gnu_cxx::__ops::__val_less_iter());
2241 * @brief Finds the largest subrange in which @p __val could be inserted
2242 * at any place in it without changing the ordering.
2243 * @param __first An iterator.
2244 * @param __last Another iterator.
2245 * @param __val The search term.
2246 * @param __comp A functor to use for comparisons.
2247 * @return An pair of iterators defining the subrange.
2248 * @ingroup binary_search_algorithms
2250 * This is equivalent to
2252 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2253 * upper_bound(__first, __last, __val, __comp))
2255 * but does not actually call those functions.
2257 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2258 _GLIBCXX20_CONSTEXPR
2259 inline pair
<_ForwardIterator
, _ForwardIterator
>
2260 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2261 const _Tp
& __val
, _Compare __comp
)
2263 // concept requirements
2264 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2265 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2266 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2267 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2268 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2269 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2271 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2274 return std::__equal_range(__first
, __last
, __val
,
2275 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2276 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2280 * @brief Determines whether an element exists in a range.
2281 * @ingroup binary_search_algorithms
2282 * @param __first An iterator.
2283 * @param __last Another iterator.
2284 * @param __val The search term.
2285 * @return True if @p __val (or its equivalent) is in [@p
2286 * __first,@p __last ].
2288 * Note that this does not actually return an iterator to @p __val. For
2289 * that, use std::find or a container's specialized find member functions.
2291 template<typename _ForwardIterator
, typename _Tp
>
2292 _GLIBCXX20_CONSTEXPR
2294 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2297 // concept requirements
2298 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2299 __glibcxx_function_requires(_LessThanOpConcept
<
2300 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2301 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2302 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2304 _ForwardIterator __i
2305 = std::__lower_bound(__first
, __last
, __val
,
2306 __gnu_cxx::__ops::__iter_less_val());
2307 return __i
!= __last
&& !(__val
< *__i
);
2311 * @brief Determines whether an element exists in a range.
2312 * @ingroup binary_search_algorithms
2313 * @param __first An iterator.
2314 * @param __last Another iterator.
2315 * @param __val The search term.
2316 * @param __comp A functor to use for comparisons.
2317 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2319 * Note that this does not actually return an iterator to @p __val. For
2320 * that, use std::find or a container's specialized find member functions.
2322 * The comparison function should have the same effects on ordering as
2323 * the function used for the initial sort.
2325 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2326 _GLIBCXX20_CONSTEXPR
2328 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2329 const _Tp
& __val
, _Compare __comp
)
2331 // concept requirements
2332 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2333 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2334 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2335 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2337 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2340 _ForwardIterator __i
2341 = std::__lower_bound(__first
, __last
, __val
,
2342 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2343 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2348 /// This is a helper function for the __merge_adaptive routines.
2349 template<typename _InputIterator1
, typename _InputIterator2
,
2350 typename _OutputIterator
, typename _Compare
>
2352 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2353 _InputIterator2 __first2
, _InputIterator2 __last2
,
2354 _OutputIterator __result
, _Compare __comp
)
2356 while (__first1
!= __last1
&& __first2
!= __last2
)
2358 if (__comp(__first2
, __first1
))
2360 *__result
= _GLIBCXX_MOVE(*__first2
);
2365 *__result
= _GLIBCXX_MOVE(*__first1
);
2370 if (__first1
!= __last1
)
2371 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2374 /// This is a helper function for the __merge_adaptive routines.
2375 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2376 typename _BidirectionalIterator3
, typename _Compare
>
2378 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2379 _BidirectionalIterator1 __last1
,
2380 _BidirectionalIterator2 __first2
,
2381 _BidirectionalIterator2 __last2
,
2382 _BidirectionalIterator3 __result
,
2385 if (__first1
== __last1
)
2387 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2390 else if (__first2
== __last2
)
2397 if (__comp(__last2
, __last1
))
2399 *--__result
= _GLIBCXX_MOVE(*__last1
);
2400 if (__first1
== __last1
)
2402 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2409 *--__result
= _GLIBCXX_MOVE(*__last2
);
2410 if (__first2
== __last2
)
2417 /// This is a helper function for the merge routines.
2418 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2420 _BidirectionalIterator1
2421 __rotate_adaptive(_BidirectionalIterator1 __first
,
2422 _BidirectionalIterator1 __middle
,
2423 _BidirectionalIterator1 __last
,
2424 _Distance __len1
, _Distance __len2
,
2425 _BidirectionalIterator2 __buffer
,
2426 _Distance __buffer_size
)
2428 _BidirectionalIterator2 __buffer_end
;
2429 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2433 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2434 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2435 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2440 else if (__len1
<= __buffer_size
)
2444 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2445 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2446 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2452 return std::rotate(__first
, __middle
, __last
);
2455 /// This is a helper function for the merge routines.
2456 template<typename _BidirectionalIterator
, typename _Distance
,
2457 typename _Pointer
, typename _Compare
>
2459 __merge_adaptive(_BidirectionalIterator __first
,
2460 _BidirectionalIterator __middle
,
2461 _BidirectionalIterator __last
,
2462 _Distance __len1
, _Distance __len2
,
2463 _Pointer __buffer
, _Distance __buffer_size
,
2466 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2468 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2469 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2472 else if (__len2
<= __buffer_size
)
2474 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2475 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2476 __buffer_end
, __last
, __comp
);
2480 _BidirectionalIterator __first_cut
= __first
;
2481 _BidirectionalIterator __second_cut
= __middle
;
2482 _Distance __len11
= 0;
2483 _Distance __len22
= 0;
2484 if (__len1
> __len2
)
2486 __len11
= __len1
/ 2;
2487 std::advance(__first_cut
, __len11
);
2489 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2490 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2491 __len22
= std::distance(__middle
, __second_cut
);
2495 __len22
= __len2
/ 2;
2496 std::advance(__second_cut
, __len22
);
2498 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2499 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2500 __len11
= std::distance(__first
, __first_cut
);
2503 _BidirectionalIterator __new_middle
2504 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2505 __len1
- __len11
, __len22
, __buffer
,
2507 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2508 __len22
, __buffer
, __buffer_size
, __comp
);
2509 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2511 __len2
- __len22
, __buffer
,
2512 __buffer_size
, __comp
);
2516 /// This is a helper function for the merge routines.
2517 template<typename _BidirectionalIterator
, typename _Distance
,
2520 __merge_without_buffer(_BidirectionalIterator __first
,
2521 _BidirectionalIterator __middle
,
2522 _BidirectionalIterator __last
,
2523 _Distance __len1
, _Distance __len2
,
2526 if (__len1
== 0 || __len2
== 0)
2529 if (__len1
+ __len2
== 2)
2531 if (__comp(__middle
, __first
))
2532 std::iter_swap(__first
, __middle
);
2536 _BidirectionalIterator __first_cut
= __first
;
2537 _BidirectionalIterator __second_cut
= __middle
;
2538 _Distance __len11
= 0;
2539 _Distance __len22
= 0;
2540 if (__len1
> __len2
)
2542 __len11
= __len1
/ 2;
2543 std::advance(__first_cut
, __len11
);
2545 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2546 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2547 __len22
= std::distance(__middle
, __second_cut
);
2551 __len22
= __len2
/ 2;
2552 std::advance(__second_cut
, __len22
);
2554 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2555 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2556 __len11
= std::distance(__first
, __first_cut
);
2559 _BidirectionalIterator __new_middle
2560 = std::rotate(__first_cut
, __middle
, __second_cut
);
2561 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2562 __len11
, __len22
, __comp
);
2563 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2564 __len1
- __len11
, __len2
- __len22
, __comp
);
2567 template<typename _BidirectionalIterator
, typename _Compare
>
2569 __inplace_merge(_BidirectionalIterator __first
,
2570 _BidirectionalIterator __middle
,
2571 _BidirectionalIterator __last
,
2574 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2576 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2579 if (__first
== __middle
|| __middle
== __last
)
2582 const _DistanceType __len1
= std::distance(__first
, __middle
);
2583 const _DistanceType __len2
= std::distance(__middle
, __last
);
2585 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2586 _TmpBuf
__buf(__first
, __len1
+ __len2
);
2588 if (__buf
.begin() == 0)
2589 std::__merge_without_buffer
2590 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2592 std::__merge_adaptive
2593 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2594 _DistanceType(__buf
.size()), __comp
);
2598 * @brief Merges two sorted ranges in place.
2599 * @ingroup sorting_algorithms
2600 * @param __first An iterator.
2601 * @param __middle Another iterator.
2602 * @param __last Another iterator.
2605 * Merges two sorted and consecutive ranges, [__first,__middle) and
2606 * [__middle,__last), and puts the result in [__first,__last). The
2607 * output will be sorted. The sort is @e stable, that is, for
2608 * equivalent elements in the two ranges, elements from the first
2609 * range will always come before elements from the second.
2611 * If enough additional memory is available, this takes (__last-__first)-1
2612 * comparisons. Otherwise an NlogN algorithm is used, where N is
2613 * distance(__first,__last).
2615 template<typename _BidirectionalIterator
>
2617 inplace_merge(_BidirectionalIterator __first
,
2618 _BidirectionalIterator __middle
,
2619 _BidirectionalIterator __last
)
2621 // concept requirements
2622 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2623 _BidirectionalIterator
>)
2624 __glibcxx_function_requires(_LessThanComparableConcept
<
2625 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2626 __glibcxx_requires_sorted(__first
, __middle
);
2627 __glibcxx_requires_sorted(__middle
, __last
);
2628 __glibcxx_requires_irreflexive(__first
, __last
);
2630 std::__inplace_merge(__first
, __middle
, __last
,
2631 __gnu_cxx::__ops::__iter_less_iter());
2635 * @brief Merges two sorted ranges in place.
2636 * @ingroup sorting_algorithms
2637 * @param __first An iterator.
2638 * @param __middle Another iterator.
2639 * @param __last Another iterator.
2640 * @param __comp A functor to use for comparisons.
2643 * Merges two sorted and consecutive ranges, [__first,__middle) and
2644 * [middle,last), and puts the result in [__first,__last). The output will
2645 * be sorted. The sort is @e stable, that is, for equivalent
2646 * elements in the two ranges, elements from the first range will always
2647 * come before elements from the second.
2649 * If enough additional memory is available, this takes (__last-__first)-1
2650 * comparisons. Otherwise an NlogN algorithm is used, where N is
2651 * distance(__first,__last).
2653 * The comparison function should have the same effects on ordering as
2654 * the function used for the initial sort.
2656 template<typename _BidirectionalIterator
, typename _Compare
>
2658 inplace_merge(_BidirectionalIterator __first
,
2659 _BidirectionalIterator __middle
,
2660 _BidirectionalIterator __last
,
2663 // concept requirements
2664 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2665 _BidirectionalIterator
>)
2666 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2667 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2668 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2669 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2670 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2671 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2673 std::__inplace_merge(__first
, __middle
, __last
,
2674 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2678 /// This is a helper function for the __merge_sort_loop routines.
2679 template<typename _InputIterator
, typename _OutputIterator
,
2682 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2683 _InputIterator __first2
, _InputIterator __last2
,
2684 _OutputIterator __result
, _Compare __comp
)
2686 while (__first1
!= __last1
&& __first2
!= __last2
)
2688 if (__comp(__first2
, __first1
))
2690 *__result
= _GLIBCXX_MOVE(*__first2
);
2695 *__result
= _GLIBCXX_MOVE(*__first1
);
2700 return _GLIBCXX_MOVE3(__first2
, __last2
,
2701 _GLIBCXX_MOVE3(__first1
, __last1
,
2705 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2706 typename _Distance
, typename _Compare
>
2708 __merge_sort_loop(_RandomAccessIterator1 __first
,
2709 _RandomAccessIterator1 __last
,
2710 _RandomAccessIterator2 __result
, _Distance __step_size
,
2713 const _Distance __two_step
= 2 * __step_size
;
2715 while (__last
- __first
>= __two_step
)
2717 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2718 __first
+ __step_size
,
2719 __first
+ __two_step
,
2721 __first
+= __two_step
;
2723 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2725 std::__move_merge(__first
, __first
+ __step_size
,
2726 __first
+ __step_size
, __last
, __result
, __comp
);
2729 template<typename _RandomAccessIterator
, typename _Distance
,
2732 __chunk_insertion_sort(_RandomAccessIterator __first
,
2733 _RandomAccessIterator __last
,
2734 _Distance __chunk_size
, _Compare __comp
)
2736 while (__last
- __first
>= __chunk_size
)
2738 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2739 __first
+= __chunk_size
;
2741 std::__insertion_sort(__first
, __last
, __comp
);
2744 enum { _S_chunk_size
= 7 };
2746 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2748 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2749 _RandomAccessIterator __last
,
2750 _Pointer __buffer
, _Compare __comp
)
2752 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2755 const _Distance __len
= __last
- __first
;
2756 const _Pointer __buffer_last
= __buffer
+ __len
;
2758 _Distance __step_size
= _S_chunk_size
;
2759 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2761 while (__step_size
< __len
)
2763 std::__merge_sort_loop(__first
, __last
, __buffer
,
2764 __step_size
, __comp
);
2766 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2767 __step_size
, __comp
);
2772 template<typename _RandomAccessIterator
, typename _Pointer
,
2773 typename _Distance
, typename _Compare
>
2775 __stable_sort_adaptive(_RandomAccessIterator __first
,
2776 _RandomAccessIterator __last
,
2777 _Pointer __buffer
, _Distance __buffer_size
,
2780 const _Distance __len
= (__last
- __first
+ 1) / 2;
2781 const _RandomAccessIterator __middle
= __first
+ __len
;
2782 if (__len
> __buffer_size
)
2784 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2785 __buffer_size
, __comp
);
2786 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2787 __buffer_size
, __comp
);
2791 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2792 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2794 std::__merge_adaptive(__first
, __middle
, __last
,
2795 _Distance(__middle
- __first
),
2796 _Distance(__last
- __middle
),
2797 __buffer
, __buffer_size
,
2801 /// This is a helper function for the stable sorting routines.
2802 template<typename _RandomAccessIterator
, typename _Compare
>
2804 __inplace_stable_sort(_RandomAccessIterator __first
,
2805 _RandomAccessIterator __last
, _Compare __comp
)
2807 if (__last
- __first
< 15)
2809 std::__insertion_sort(__first
, __last
, __comp
);
2812 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2813 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2814 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2815 std::__merge_without_buffer(__first
, __middle
, __last
,
2823 // Set algorithms: includes, set_union, set_intersection, set_difference,
2824 // set_symmetric_difference. All of these algorithms have the precondition
2825 // that their input ranges are sorted and the postcondition that their output
2826 // ranges are sorted.
2828 template<typename _InputIterator1
, typename _InputIterator2
,
2830 _GLIBCXX20_CONSTEXPR
2832 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2833 _InputIterator2 __first2
, _InputIterator2 __last2
,
2836 while (__first1
!= __last1
&& __first2
!= __last2
)
2837 if (__comp(__first2
, __first1
))
2839 else if (__comp(__first1
, __first2
))
2847 return __first2
== __last2
;
2851 * @brief Determines whether all elements of a sequence exists in a range.
2852 * @param __first1 Start of search range.
2853 * @param __last1 End of search range.
2854 * @param __first2 Start of sequence
2855 * @param __last2 End of sequence.
2856 * @return True if each element in [__first2,__last2) is contained in order
2857 * within [__first1,__last1). False otherwise.
2858 * @ingroup set_algorithms
2860 * This operation expects both [__first1,__last1) and
2861 * [__first2,__last2) to be sorted. Searches for the presence of
2862 * each element in [__first2,__last2) within [__first1,__last1).
2863 * The iterators over each range only move forward, so this is a
2864 * linear algorithm. If an element in [__first2,__last2) is not
2865 * found before the search iterator reaches @p __last2, false is
2868 template<typename _InputIterator1
, typename _InputIterator2
>
2869 _GLIBCXX20_CONSTEXPR
2871 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2872 _InputIterator2 __first2
, _InputIterator2 __last2
)
2874 // concept requirements
2875 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2876 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2877 __glibcxx_function_requires(_LessThanOpConcept
<
2878 typename iterator_traits
<_InputIterator1
>::value_type
,
2879 typename iterator_traits
<_InputIterator2
>::value_type
>)
2880 __glibcxx_function_requires(_LessThanOpConcept
<
2881 typename iterator_traits
<_InputIterator2
>::value_type
,
2882 typename iterator_traits
<_InputIterator1
>::value_type
>)
2883 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2884 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2885 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2886 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2888 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2889 __gnu_cxx::__ops::__iter_less_iter());
2893 * @brief Determines whether all elements of a sequence exists in a range
2895 * @ingroup set_algorithms
2896 * @param __first1 Start of search range.
2897 * @param __last1 End of search range.
2898 * @param __first2 Start of sequence
2899 * @param __last2 End of sequence.
2900 * @param __comp Comparison function to use.
2901 * @return True if each element in [__first2,__last2) is contained
2902 * in order within [__first1,__last1) according to comp. False
2903 * otherwise. @ingroup set_algorithms
2905 * This operation expects both [__first1,__last1) and
2906 * [__first2,__last2) to be sorted. Searches for the presence of
2907 * each element in [__first2,__last2) within [__first1,__last1),
2908 * using comp to decide. The iterators over each range only move
2909 * forward, so this is a linear algorithm. If an element in
2910 * [__first2,__last2) is not found before the search iterator
2911 * reaches @p __last2, false is returned.
2913 template<typename _InputIterator1
, typename _InputIterator2
,
2915 _GLIBCXX20_CONSTEXPR
2917 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2918 _InputIterator2 __first2
, _InputIterator2 __last2
,
2921 // concept requirements
2922 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2923 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2924 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2925 typename iterator_traits
<_InputIterator1
>::value_type
,
2926 typename iterator_traits
<_InputIterator2
>::value_type
>)
2927 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2928 typename iterator_traits
<_InputIterator2
>::value_type
,
2929 typename iterator_traits
<_InputIterator1
>::value_type
>)
2930 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2931 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2932 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2933 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2935 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2936 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2945 // set_symmetric_difference
2949 template<typename _BidirectionalIterator
, typename _Compare
>
2950 _GLIBCXX20_CONSTEXPR
2952 __next_permutation(_BidirectionalIterator __first
,
2953 _BidirectionalIterator __last
, _Compare __comp
)
2955 if (__first
== __last
)
2957 _BidirectionalIterator __i
= __first
;
2966 _BidirectionalIterator __ii
= __i
;
2968 if (__comp(__i
, __ii
))
2970 _BidirectionalIterator __j
= __last
;
2971 while (!__comp(__i
, --__j
))
2973 std::iter_swap(__i
, __j
);
2974 std::__reverse(__ii
, __last
,
2975 std::__iterator_category(__first
));
2980 std::__reverse(__first
, __last
,
2981 std::__iterator_category(__first
));
2988 * @brief Permute range into the next @e dictionary ordering.
2989 * @ingroup sorting_algorithms
2990 * @param __first Start of range.
2991 * @param __last End of range.
2992 * @return False if wrapped to first permutation, true otherwise.
2994 * Treats all permutations of the range as a set of @e dictionary sorted
2995 * sequences. Permutes the current sequence into the next one of this set.
2996 * Returns true if there are more sequences to generate. If the sequence
2997 * is the largest of the set, the smallest is generated and false returned.
2999 template<typename _BidirectionalIterator
>
3001 next_permutation(_BidirectionalIterator __first
,
3002 _BidirectionalIterator __last
)
3004 // concept requirements
3005 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3006 _BidirectionalIterator
>)
3007 __glibcxx_function_requires(_LessThanComparableConcept
<
3008 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3009 __glibcxx_requires_valid_range(__first
, __last
);
3010 __glibcxx_requires_irreflexive(__first
, __last
);
3012 return std::__next_permutation
3013 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
3017 * @brief Permute range into the next @e dictionary ordering using
3018 * comparison functor.
3019 * @ingroup sorting_algorithms
3020 * @param __first Start of range.
3021 * @param __last End of range.
3022 * @param __comp A comparison functor.
3023 * @return False if wrapped to first permutation, true otherwise.
3025 * Treats all permutations of the range [__first,__last) as a set of
3026 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3027 * sequence into the next one of this set. Returns true if there are more
3028 * sequences to generate. If the sequence is the largest of the set, the
3029 * smallest is generated and false returned.
3031 template<typename _BidirectionalIterator
, typename _Compare
>
3033 next_permutation(_BidirectionalIterator __first
,
3034 _BidirectionalIterator __last
, _Compare __comp
)
3036 // concept requirements
3037 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3038 _BidirectionalIterator
>)
3039 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3040 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3041 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3042 __glibcxx_requires_valid_range(__first
, __last
);
3043 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3045 return std::__next_permutation
3046 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3049 template<typename _BidirectionalIterator
, typename _Compare
>
3050 _GLIBCXX20_CONSTEXPR
3052 __prev_permutation(_BidirectionalIterator __first
,
3053 _BidirectionalIterator __last
, _Compare __comp
)
3055 if (__first
== __last
)
3057 _BidirectionalIterator __i
= __first
;
3066 _BidirectionalIterator __ii
= __i
;
3068 if (__comp(__ii
, __i
))
3070 _BidirectionalIterator __j
= __last
;
3071 while (!__comp(--__j
, __i
))
3073 std::iter_swap(__i
, __j
);
3074 std::__reverse(__ii
, __last
,
3075 std::__iterator_category(__first
));
3080 std::__reverse(__first
, __last
,
3081 std::__iterator_category(__first
));
3088 * @brief Permute range into the previous @e dictionary ordering.
3089 * @ingroup sorting_algorithms
3090 * @param __first Start of range.
3091 * @param __last End of range.
3092 * @return False if wrapped to last permutation, true otherwise.
3094 * Treats all permutations of the range as a set of @e dictionary sorted
3095 * sequences. Permutes the current sequence into the previous one of this
3096 * set. Returns true if there are more sequences to generate. If the
3097 * sequence is the smallest of the set, the largest is generated and false
3100 template<typename _BidirectionalIterator
>
3102 prev_permutation(_BidirectionalIterator __first
,
3103 _BidirectionalIterator __last
)
3105 // concept requirements
3106 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3107 _BidirectionalIterator
>)
3108 __glibcxx_function_requires(_LessThanComparableConcept
<
3109 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3110 __glibcxx_requires_valid_range(__first
, __last
);
3111 __glibcxx_requires_irreflexive(__first
, __last
);
3113 return std::__prev_permutation(__first
, __last
,
3114 __gnu_cxx::__ops::__iter_less_iter());
3118 * @brief Permute range into the previous @e dictionary ordering using
3119 * comparison functor.
3120 * @ingroup sorting_algorithms
3121 * @param __first Start of range.
3122 * @param __last End of range.
3123 * @param __comp A comparison functor.
3124 * @return False if wrapped to last permutation, true otherwise.
3126 * Treats all permutations of the range [__first,__last) as a set of
3127 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3128 * sequence into the previous one of this set. Returns true if there are
3129 * more sequences to generate. If the sequence is the smallest of the set,
3130 * the largest is generated and false returned.
3132 template<typename _BidirectionalIterator
, typename _Compare
>
3134 prev_permutation(_BidirectionalIterator __first
,
3135 _BidirectionalIterator __last
, _Compare __comp
)
3137 // concept requirements
3138 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3139 _BidirectionalIterator
>)
3140 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3141 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3142 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3143 __glibcxx_requires_valid_range(__first
, __last
);
3144 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3146 return std::__prev_permutation(__first
, __last
,
3147 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3153 template<typename _InputIterator
, typename _OutputIterator
,
3154 typename _Predicate
, typename _Tp
>
3155 _GLIBCXX20_CONSTEXPR
3157 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3158 _OutputIterator __result
,
3159 _Predicate __pred
, const _Tp
& __new_value
)
3161 for (; __first
!= __last
; ++__first
, (void)++__result
)
3162 if (__pred(__first
))
3163 *__result
= __new_value
;
3165 *__result
= *__first
;
3170 * @brief Copy a sequence, replacing each element of one value with another
3172 * @param __first An input iterator.
3173 * @param __last An input iterator.
3174 * @param __result An output iterator.
3175 * @param __old_value The value to be replaced.
3176 * @param __new_value The replacement value.
3177 * @return The end of the output sequence, @p result+(last-first).
3179 * Copies each element in the input range @p [__first,__last) to the
3180 * output range @p [__result,__result+(__last-__first)) replacing elements
3181 * equal to @p __old_value with @p __new_value.
3183 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3184 _GLIBCXX20_CONSTEXPR
3185 inline _OutputIterator
3186 replace_copy(_InputIterator __first
, _InputIterator __last
,
3187 _OutputIterator __result
,
3188 const _Tp
& __old_value
, const _Tp
& __new_value
)
3190 // concept requirements
3191 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3192 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3193 typename iterator_traits
<_InputIterator
>::value_type
>)
3194 __glibcxx_function_requires(_EqualOpConcept
<
3195 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3196 __glibcxx_requires_valid_range(__first
, __last
);
3198 return std::__replace_copy_if(__first
, __last
, __result
,
3199 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3204 * @brief Copy a sequence, replacing each value for which a predicate
3205 * returns true with another value.
3206 * @ingroup mutating_algorithms
3207 * @param __first An input iterator.
3208 * @param __last An input iterator.
3209 * @param __result An output iterator.
3210 * @param __pred A predicate.
3211 * @param __new_value The replacement value.
3212 * @return The end of the output sequence, @p __result+(__last-__first).
3214 * Copies each element in the range @p [__first,__last) to the range
3215 * @p [__result,__result+(__last-__first)) replacing elements for which
3216 * @p __pred returns true with @p __new_value.
3218 template<typename _InputIterator
, typename _OutputIterator
,
3219 typename _Predicate
, typename _Tp
>
3220 _GLIBCXX20_CONSTEXPR
3221 inline _OutputIterator
3222 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3223 _OutputIterator __result
,
3224 _Predicate __pred
, const _Tp
& __new_value
)
3226 // concept requirements
3227 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3228 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3229 typename iterator_traits
<_InputIterator
>::value_type
>)
3230 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3231 typename iterator_traits
<_InputIterator
>::value_type
>)
3232 __glibcxx_requires_valid_range(__first
, __last
);
3234 return std::__replace_copy_if(__first
, __last
, __result
,
3235 __gnu_cxx::__ops::__pred_iter(__pred
),
3239 template<typename _InputIterator
, typename _Predicate
>
3240 _GLIBCXX20_CONSTEXPR
3241 typename iterator_traits
<_InputIterator
>::difference_type
3242 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3244 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3245 for (; __first
!= __last
; ++__first
)
3246 if (__pred(__first
))
3251 #if __cplusplus >= 201103L
3253 * @brief Determines whether the elements of a sequence are sorted.
3254 * @ingroup sorting_algorithms
3255 * @param __first An iterator.
3256 * @param __last Another iterator.
3257 * @return True if the elements are sorted, false otherwise.
3259 template<typename _ForwardIterator
>
3260 _GLIBCXX20_CONSTEXPR
3262 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3263 { return std::is_sorted_until(__first
, __last
) == __last
; }
3266 * @brief Determines whether the elements of a sequence are sorted
3267 * according to a comparison functor.
3268 * @ingroup sorting_algorithms
3269 * @param __first An iterator.
3270 * @param __last Another iterator.
3271 * @param __comp A comparison functor.
3272 * @return True if the elements are sorted, false otherwise.
3274 template<typename _ForwardIterator
, typename _Compare
>
3275 _GLIBCXX20_CONSTEXPR
3277 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3279 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3281 template<typename _ForwardIterator
, typename _Compare
>
3282 _GLIBCXX20_CONSTEXPR
3284 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3287 if (__first
== __last
)
3290 _ForwardIterator __next
= __first
;
3291 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3292 if (__comp(__next
, __first
))
3298 * @brief Determines the end of a sorted sequence.
3299 * @ingroup sorting_algorithms
3300 * @param __first An iterator.
3301 * @param __last Another iterator.
3302 * @return An iterator pointing to the last iterator i in [__first, __last)
3303 * for which the range [__first, i) is sorted.
3305 template<typename _ForwardIterator
>
3306 _GLIBCXX20_CONSTEXPR
3307 inline _ForwardIterator
3308 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3310 // concept requirements
3311 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3312 __glibcxx_function_requires(_LessThanComparableConcept
<
3313 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3314 __glibcxx_requires_valid_range(__first
, __last
);
3315 __glibcxx_requires_irreflexive(__first
, __last
);
3317 return std::__is_sorted_until(__first
, __last
,
3318 __gnu_cxx::__ops::__iter_less_iter());
3322 * @brief Determines the end of a sorted sequence using comparison functor.
3323 * @ingroup sorting_algorithms
3324 * @param __first An iterator.
3325 * @param __last Another iterator.
3326 * @param __comp A comparison functor.
3327 * @return An iterator pointing to the last iterator i in [__first, __last)
3328 * for which the range [__first, i) is sorted.
3330 template<typename _ForwardIterator
, typename _Compare
>
3331 _GLIBCXX20_CONSTEXPR
3332 inline _ForwardIterator
3333 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3336 // concept requirements
3337 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3338 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3339 typename iterator_traits
<_ForwardIterator
>::value_type
,
3340 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3341 __glibcxx_requires_valid_range(__first
, __last
);
3342 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3344 return std::__is_sorted_until(__first
, __last
,
3345 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3349 * @brief Determines min and max at once as an ordered pair.
3350 * @ingroup sorting_algorithms
3351 * @param __a A thing of arbitrary type.
3352 * @param __b Another thing of arbitrary type.
3353 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3356 template<typename _Tp
>
3357 _GLIBCXX14_CONSTEXPR
3358 inline pair
<const _Tp
&, const _Tp
&>
3359 minmax(const _Tp
& __a
, const _Tp
& __b
)
3361 // concept requirements
3362 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3364 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3365 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3369 * @brief Determines min and max at once as an ordered pair.
3370 * @ingroup sorting_algorithms
3371 * @param __a A thing of arbitrary type.
3372 * @param __b Another thing of arbitrary type.
3373 * @param __comp A @link comparison_functors comparison functor @endlink.
3374 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3377 template<typename _Tp
, typename _Compare
>
3378 _GLIBCXX14_CONSTEXPR
3379 inline pair
<const _Tp
&, const _Tp
&>
3380 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3382 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3383 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3386 template<typename _ForwardIterator
, typename _Compare
>
3387 _GLIBCXX14_CONSTEXPR
3388 pair
<_ForwardIterator
, _ForwardIterator
>
3389 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3392 _ForwardIterator __next
= __first
;
3393 if (__first
== __last
3394 || ++__next
== __last
)
3395 return std::make_pair(__first
, __first
);
3397 _ForwardIterator __min
{}, __max
{};
3398 if (__comp(__next
, __first
))
3412 while (__first
!= __last
)
3415 if (++__next
== __last
)
3417 if (__comp(__first
, __min
))
3419 else if (!__comp(__first
, __max
))
3424 if (__comp(__next
, __first
))
3426 if (__comp(__next
, __min
))
3428 if (!__comp(__first
, __max
))
3433 if (__comp(__first
, __min
))
3435 if (!__comp(__next
, __max
))
3443 return std::make_pair(__min
, __max
);
3447 * @brief Return a pair of iterators pointing to the minimum and maximum
3448 * elements in a range.
3449 * @ingroup sorting_algorithms
3450 * @param __first Start of range.
3451 * @param __last End of range.
3452 * @return make_pair(m, M), where m is the first iterator i in
3453 * [__first, __last) such that no other element in the range is
3454 * smaller, and where M is the last iterator i in [__first, __last)
3455 * such that no other element in the range is larger.
3457 template<typename _ForwardIterator
>
3458 _GLIBCXX14_CONSTEXPR
3459 inline pair
<_ForwardIterator
, _ForwardIterator
>
3460 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3462 // concept requirements
3463 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3464 __glibcxx_function_requires(_LessThanComparableConcept
<
3465 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3466 __glibcxx_requires_valid_range(__first
, __last
);
3467 __glibcxx_requires_irreflexive(__first
, __last
);
3469 return std::__minmax_element(__first
, __last
,
3470 __gnu_cxx::__ops::__iter_less_iter());
3474 * @brief Return a pair of iterators pointing to the minimum and maximum
3475 * elements in a range.
3476 * @ingroup sorting_algorithms
3477 * @param __first Start of range.
3478 * @param __last End of range.
3479 * @param __comp Comparison functor.
3480 * @return make_pair(m, M), where m is the first iterator i in
3481 * [__first, __last) such that no other element in the range is
3482 * smaller, and where M is the last iterator i in [__first, __last)
3483 * such that no other element in the range is larger.
3485 template<typename _ForwardIterator
, typename _Compare
>
3486 _GLIBCXX14_CONSTEXPR
3487 inline pair
<_ForwardIterator
, _ForwardIterator
>
3488 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3491 // concept requirements
3492 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3493 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3494 typename iterator_traits
<_ForwardIterator
>::value_type
,
3495 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3496 __glibcxx_requires_valid_range(__first
, __last
);
3497 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3499 return std::__minmax_element(__first
, __last
,
3500 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3504 template<typename _Tp
>
3505 _GLIBCXX14_CONSTEXPR
3507 min(initializer_list
<_Tp
> __l
)
3508 { return *std::min_element(__l
.begin(), __l
.end()); }
3510 template<typename _Tp
, typename _Compare
>
3511 _GLIBCXX14_CONSTEXPR
3513 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3514 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3516 template<typename _Tp
>
3517 _GLIBCXX14_CONSTEXPR
3519 max(initializer_list
<_Tp
> __l
)
3520 { return *std::max_element(__l
.begin(), __l
.end()); }
3522 template<typename _Tp
, typename _Compare
>
3523 _GLIBCXX14_CONSTEXPR
3525 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3526 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3528 template<typename _Tp
>
3529 _GLIBCXX14_CONSTEXPR
3530 inline pair
<_Tp
, _Tp
>
3531 minmax(initializer_list
<_Tp
> __l
)
3533 pair
<const _Tp
*, const _Tp
*> __p
=
3534 std::minmax_element(__l
.begin(), __l
.end());
3535 return std::make_pair(*__p
.first
, *__p
.second
);
3538 template<typename _Tp
, typename _Compare
>
3539 _GLIBCXX14_CONSTEXPR
3540 inline pair
<_Tp
, _Tp
>
3541 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3543 pair
<const _Tp
*, const _Tp
*> __p
=
3544 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3545 return std::make_pair(*__p
.first
, *__p
.second
);
3548 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3549 typename _BinaryPredicate
>
3550 _GLIBCXX20_CONSTEXPR
3552 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3553 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3555 // Efficiently compare identical prefixes: O(N) if sequences
3556 // have the same elements in the same order.
3557 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
3558 if (!__pred(__first1
, __first2
))
3561 if (__first1
== __last1
)
3564 // Establish __last2 assuming equal ranges by iterating over the
3565 // rest of the list.
3566 _ForwardIterator2 __last2
= __first2
;
3567 std::advance(__last2
, std::distance(__first1
, __last1
));
3568 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3570 if (__scan
!= std::__find_if(__first1
, __scan
,
3571 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3572 continue; // We've seen this one before.
3575 = std::__count_if(__first2
, __last2
,
3576 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3577 if (0 == __matches
||
3578 std::__count_if(__scan
, __last1
,
3579 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3587 * @brief Checks whether a permutation of the second sequence is equal
3588 * to the first sequence.
3589 * @ingroup non_mutating_algorithms
3590 * @param __first1 Start of first range.
3591 * @param __last1 End of first range.
3592 * @param __first2 Start of second range.
3593 * @return true if there exists a permutation of the elements in the range
3594 * [__first2, __first2 + (__last1 - __first1)), beginning with
3595 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3596 * returns true; otherwise, returns false.
3598 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3599 _GLIBCXX20_CONSTEXPR
3601 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3602 _ForwardIterator2 __first2
)
3604 // concept requirements
3605 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3606 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3607 __glibcxx_function_requires(_EqualOpConcept
<
3608 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3609 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3610 __glibcxx_requires_valid_range(__first1
, __last1
);
3612 return std::__is_permutation(__first1
, __last1
, __first2
,
3613 __gnu_cxx::__ops::__iter_equal_to_iter());
3617 * @brief Checks whether a permutation of the second sequence is equal
3618 * to the first sequence.
3619 * @ingroup non_mutating_algorithms
3620 * @param __first1 Start of first range.
3621 * @param __last1 End of first range.
3622 * @param __first2 Start of second range.
3623 * @param __pred A binary predicate.
3624 * @return true if there exists a permutation of the elements in
3625 * the range [__first2, __first2 + (__last1 - __first1)),
3626 * beginning with ForwardIterator2 begin, such that
3627 * equal(__first1, __last1, __begin, __pred) returns true;
3628 * otherwise, returns false.
3630 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3631 typename _BinaryPredicate
>
3632 _GLIBCXX20_CONSTEXPR
3634 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3635 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3637 // concept requirements
3638 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3639 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3640 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3641 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3642 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3643 __glibcxx_requires_valid_range(__first1
, __last1
);
3645 return std::__is_permutation(__first1
, __last1
, __first2
,
3646 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3649 #if __cplusplus > 201103L
3650 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3651 typename _BinaryPredicate
>
3652 _GLIBCXX20_CONSTEXPR
3654 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3655 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3656 _BinaryPredicate __pred
)
3659 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3661 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3662 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3663 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3664 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3667 auto __d1
= std::distance(__first1
, __last1
);
3668 auto __d2
= std::distance(__first2
, __last2
);
3673 // Efficiently compare identical prefixes: O(N) if sequences
3674 // have the same elements in the same order.
3675 for (; __first1
!= __last1
&& __first2
!= __last2
;
3676 ++__first1
, (void)++__first2
)
3677 if (!__pred(__first1
, __first2
))
3682 if (__first1
== __last1
)
3687 auto __d1
= std::distance(__first1
, __last1
);
3688 auto __d2
= std::distance(__first2
, __last2
);
3689 if (__d1
== 0 && __d2
== 0)
3695 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3697 if (__scan
!= std::__find_if(__first1
, __scan
,
3698 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3699 continue; // We've seen this one before.
3701 auto __matches
= std::__count_if(__first2
, __last2
,
3702 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3704 || std::__count_if(__scan
, __last1
,
3705 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3713 * @brief Checks whether a permutaion of the second sequence is equal
3714 * to the first sequence.
3715 * @ingroup non_mutating_algorithms
3716 * @param __first1 Start of first range.
3717 * @param __last1 End of first range.
3718 * @param __first2 Start of second range.
3719 * @param __last2 End of first range.
3720 * @return true if there exists a permutation of the elements in the range
3721 * [__first2, __last2), beginning with ForwardIterator2 begin,
3722 * such that equal(__first1, __last1, begin) returns true;
3723 * otherwise, returns false.
3725 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3726 _GLIBCXX20_CONSTEXPR
3728 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3729 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3731 __glibcxx_requires_valid_range(__first1
, __last1
);
3732 __glibcxx_requires_valid_range(__first2
, __last2
);
3735 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3736 __gnu_cxx::__ops::__iter_equal_to_iter());
3740 * @brief Checks whether a permutation of the second sequence is equal
3741 * to the first sequence.
3742 * @ingroup non_mutating_algorithms
3743 * @param __first1 Start of first range.
3744 * @param __last1 End of first range.
3745 * @param __first2 Start of second range.
3746 * @param __last2 End of first range.
3747 * @param __pred A binary predicate.
3748 * @return true if there exists a permutation of the elements in the range
3749 * [__first2, __last2), beginning with ForwardIterator2 begin,
3750 * such that equal(__first1, __last1, __begin, __pred) returns true;
3751 * otherwise, returns false.
3753 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3754 typename _BinaryPredicate
>
3755 _GLIBCXX20_CONSTEXPR
3757 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3758 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3759 _BinaryPredicate __pred
)
3761 __glibcxx_requires_valid_range(__first1
, __last1
);
3762 __glibcxx_requires_valid_range(__first2
, __last2
);
3764 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3765 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3768 #if __cplusplus > 201402L
3770 #define __cpp_lib_clamp 201603
3773 * @brief Returns the value clamped between lo and hi.
3774 * @ingroup sorting_algorithms
3775 * @param __val A value of arbitrary type.
3776 * @param __lo A lower limit of arbitrary type.
3777 * @param __hi An upper limit of arbitrary type.
3778 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
3780 template<typename _Tp
>
3781 constexpr const _Tp
&
3782 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3784 __glibcxx_assert(!(__hi
< __lo
));
3785 return (__val
< __lo
) ? __lo
: (__hi
< __val
) ? __hi
: __val
;
3789 * @brief Returns the value clamped between lo and hi.
3790 * @ingroup sorting_algorithms
3791 * @param __val A value of arbitrary type.
3792 * @param __lo A lower limit of arbitrary type.
3793 * @param __hi An upper limit of arbitrary type.
3794 * @param __comp A comparison functor.
3795 * @return max(__val, __lo, __comp) if __comp(__val, __hi)
3796 * or min(__val, __hi, __comp) otherwise.
3798 template<typename _Tp
, typename _Compare
>
3799 constexpr const _Tp
&
3800 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3802 __glibcxx_assert(!__comp(__hi
, __lo
));
3803 return __comp(__val
, __lo
) ? __lo
: __comp(__hi
, __val
) ? __hi
: __val
;
3808 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3810 * @brief Generate two uniformly distributed integers using a
3811 * single distribution invocation.
3812 * @param __b0 The upper bound for the first integer.
3813 * @param __b1 The upper bound for the second integer.
3814 * @param __g A UniformRandomBitGenerator.
3815 * @return A pair (i, j) with i and j uniformly distributed
3816 * over [0, __b0) and [0, __b1), respectively.
3818 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3820 * Using uniform_int_distribution with a range that is very
3821 * small relative to the range of the generator ends up wasting
3822 * potentially expensively generated randomness, since
3823 * uniform_int_distribution does not store leftover randomness
3824 * between invocations.
3826 * If we know we want two integers in ranges that are sufficiently
3827 * small, we can compose the ranges, use a single distribution
3828 * invocation, and significantly reduce the waste.
3830 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3831 pair
<_IntType
, _IntType
>
3832 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3833 _UniformRandomBitGenerator
&& __g
)
3836 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3837 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3841 * @brief Shuffle the elements of a sequence using a uniform random
3843 * @ingroup mutating_algorithms
3844 * @param __first A forward iterator.
3845 * @param __last A forward iterator.
3846 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3849 * Reorders the elements in the range @p [__first,__last) using @p __g to
3850 * provide random numbers.
3852 template<typename _RandomAccessIterator
,
3853 typename _UniformRandomNumberGenerator
>
3855 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3856 _UniformRandomNumberGenerator
&& __g
)
3858 // concept requirements
3859 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3860 _RandomAccessIterator
>)
3861 __glibcxx_requires_valid_range(__first
, __last
);
3863 if (__first
== __last
)
3866 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3869 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3870 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3871 typedef typename
__distr_type::param_type __p_type
;
3873 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3875 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3878 const __uc_type __urngrange
= __g
.max() - __g
.min();
3879 const __uc_type __urange
= __uc_type(__last
- __first
);
3881 if (__urngrange
/ __urange
>= __urange
)
3882 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3884 _RandomAccessIterator __i
= __first
+ 1;
3886 // Since we know the range isn't empty, an even number of elements
3887 // means an uneven number of elements /to swap/, in which case we
3888 // do the first one up front:
3890 if ((__urange
% 2) == 0)
3892 __distr_type __d
{0, 1};
3893 std::iter_swap(__i
++, __first
+ __d(__g
));
3896 // Now we know that __last - __i is even, so we do the rest in pairs,
3897 // using a single distribution invocation to produce swap positions
3898 // for two successive elements at a time:
3900 while (__i
!= __last
)
3902 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3904 const pair
<__uc_type
, __uc_type
> __pospos
=
3905 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3907 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3908 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3916 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3917 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3923 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3926 * @brief Apply a function to every element of a sequence.
3927 * @ingroup non_mutating_algorithms
3928 * @param __first An input iterator.
3929 * @param __last An input iterator.
3930 * @param __f A unary function object.
3933 * Applies the function object @p __f to each element in the range
3934 * @p [first,last). @p __f must not modify the order of the sequence.
3935 * If @p __f has a return value it is ignored.
3937 template<typename _InputIterator
, typename _Function
>
3938 _GLIBCXX20_CONSTEXPR
3940 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3942 // concept requirements
3943 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3944 __glibcxx_requires_valid_range(__first
, __last
);
3945 for (; __first
!= __last
; ++__first
)
3947 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3950 #if __cplusplus >= 201703L
3952 * @brief Apply a function to every element of a sequence.
3953 * @ingroup non_mutating_algorithms
3954 * @param __first An input iterator.
3955 * @param __n A value convertible to an integer.
3956 * @param __f A unary function object.
3957 * @return `__first+__n`
3959 * Applies the function object `__f` to each element in the range
3960 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3961 * If `__f` has a return value it is ignored.
3963 template<typename _InputIterator
, typename _Size
, typename _Function
>
3965 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3967 auto __n2
= std::__size_to_integer(__n
);
3968 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3969 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3970 return std::for_each(__first
, __first
+ __n2
, __f
);
3984 * @brief Find the first occurrence of a value in a sequence.
3985 * @ingroup non_mutating_algorithms
3986 * @param __first An input iterator.
3987 * @param __last An input iterator.
3988 * @param __val The value to find.
3989 * @return The first iterator @c i in the range @p [__first,__last)
3990 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3992 template<typename _InputIterator
, typename _Tp
>
3993 _GLIBCXX20_CONSTEXPR
3994 inline _InputIterator
3995 find(_InputIterator __first
, _InputIterator __last
,
3998 // concept requirements
3999 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4000 __glibcxx_function_requires(_EqualOpConcept
<
4001 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4002 __glibcxx_requires_valid_range(__first
, __last
);
4003 return std::__find_if(__first
, __last
,
4004 __gnu_cxx::__ops::__iter_equals_val(__val
));
4008 * @brief Find the first element in a sequence for which a
4009 * predicate is true.
4010 * @ingroup non_mutating_algorithms
4011 * @param __first An input iterator.
4012 * @param __last An input iterator.
4013 * @param __pred A predicate.
4014 * @return The first iterator @c i in the range @p [__first,__last)
4015 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
4017 template<typename _InputIterator
, typename _Predicate
>
4018 _GLIBCXX20_CONSTEXPR
4019 inline _InputIterator
4020 find_if(_InputIterator __first
, _InputIterator __last
,
4023 // concept requirements
4024 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4025 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4026 typename iterator_traits
<_InputIterator
>::value_type
>)
4027 __glibcxx_requires_valid_range(__first
, __last
);
4029 return std::__find_if(__first
, __last
,
4030 __gnu_cxx::__ops::__pred_iter(__pred
));
4034 * @brief Find element from a set in a sequence.
4035 * @ingroup non_mutating_algorithms
4036 * @param __first1 Start of range to search.
4037 * @param __last1 End of range to search.
4038 * @param __first2 Start of match candidates.
4039 * @param __last2 End of match candidates.
4040 * @return The first iterator @c i in the range
4041 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
4042 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
4044 * Searches the range @p [__first1,__last1) for an element that is
4045 * equal to some element in the range [__first2,__last2). If
4046 * found, returns an iterator in the range [__first1,__last1),
4047 * otherwise returns @p __last1.
4049 template<typename _InputIterator
, typename _ForwardIterator
>
4050 _GLIBCXX20_CONSTEXPR
4052 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4053 _ForwardIterator __first2
, _ForwardIterator __last2
)
4055 // concept requirements
4056 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4057 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4058 __glibcxx_function_requires(_EqualOpConcept
<
4059 typename iterator_traits
<_InputIterator
>::value_type
,
4060 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4061 __glibcxx_requires_valid_range(__first1
, __last1
);
4062 __glibcxx_requires_valid_range(__first2
, __last2
);
4064 for (; __first1
!= __last1
; ++__first1
)
4065 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4066 if (*__first1
== *__iter
)
4072 * @brief Find element from a set in a sequence using a predicate.
4073 * @ingroup non_mutating_algorithms
4074 * @param __first1 Start of range to search.
4075 * @param __last1 End of range to search.
4076 * @param __first2 Start of match candidates.
4077 * @param __last2 End of match candidates.
4078 * @param __comp Predicate to use.
4079 * @return The first iterator @c i in the range
4080 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4081 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4082 * such iterator exists.
4085 * Searches the range @p [__first1,__last1) for an element that is
4086 * equal to some element in the range [__first2,__last2). If
4087 * found, returns an iterator in the range [__first1,__last1),
4088 * otherwise returns @p __last1.
4090 template<typename _InputIterator
, typename _ForwardIterator
,
4091 typename _BinaryPredicate
>
4092 _GLIBCXX20_CONSTEXPR
4094 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4095 _ForwardIterator __first2
, _ForwardIterator __last2
,
4096 _BinaryPredicate __comp
)
4098 // concept requirements
4099 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4100 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4101 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4102 typename iterator_traits
<_InputIterator
>::value_type
,
4103 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4104 __glibcxx_requires_valid_range(__first1
, __last1
);
4105 __glibcxx_requires_valid_range(__first2
, __last2
);
4107 for (; __first1
!= __last1
; ++__first1
)
4108 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4109 if (__comp(*__first1
, *__iter
))
4115 * @brief Find two adjacent values in a sequence that are equal.
4116 * @ingroup non_mutating_algorithms
4117 * @param __first A forward iterator.
4118 * @param __last A forward iterator.
4119 * @return The first iterator @c i such that @c i and @c i+1 are both
4120 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4121 * or @p __last if no such iterator exists.
4123 template<typename _ForwardIterator
>
4124 _GLIBCXX20_CONSTEXPR
4125 inline _ForwardIterator
4126 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4128 // concept requirements
4129 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4130 __glibcxx_function_requires(_EqualityComparableConcept
<
4131 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4132 __glibcxx_requires_valid_range(__first
, __last
);
4134 return std::__adjacent_find(__first
, __last
,
4135 __gnu_cxx::__ops::__iter_equal_to_iter());
4139 * @brief Find two adjacent values in a sequence using a predicate.
4140 * @ingroup non_mutating_algorithms
4141 * @param __first A forward iterator.
4142 * @param __last A forward iterator.
4143 * @param __binary_pred A binary predicate.
4144 * @return The first iterator @c i such that @c i and @c i+1 are both
4145 * valid iterators in @p [__first,__last) and such that
4146 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4149 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4150 _GLIBCXX20_CONSTEXPR
4151 inline _ForwardIterator
4152 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4153 _BinaryPredicate __binary_pred
)
4155 // concept requirements
4156 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4157 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4158 typename iterator_traits
<_ForwardIterator
>::value_type
,
4159 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4160 __glibcxx_requires_valid_range(__first
, __last
);
4162 return std::__adjacent_find(__first
, __last
,
4163 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4167 * @brief Count the number of copies of a value in a sequence.
4168 * @ingroup non_mutating_algorithms
4169 * @param __first An input iterator.
4170 * @param __last An input iterator.
4171 * @param __value The value to be counted.
4172 * @return The number of iterators @c i in the range @p [__first,__last)
4173 * for which @c *i == @p __value
4175 template<typename _InputIterator
, typename _Tp
>
4176 _GLIBCXX20_CONSTEXPR
4177 inline typename iterator_traits
<_InputIterator
>::difference_type
4178 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4180 // concept requirements
4181 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4182 __glibcxx_function_requires(_EqualOpConcept
<
4183 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4184 __glibcxx_requires_valid_range(__first
, __last
);
4186 return std::__count_if(__first
, __last
,
4187 __gnu_cxx::__ops::__iter_equals_val(__value
));
4191 * @brief Count the elements of a sequence for which a predicate is true.
4192 * @ingroup non_mutating_algorithms
4193 * @param __first An input iterator.
4194 * @param __last An input iterator.
4195 * @param __pred A predicate.
4196 * @return The number of iterators @c i in the range @p [__first,__last)
4197 * for which @p __pred(*i) is true.
4199 template<typename _InputIterator
, typename _Predicate
>
4200 _GLIBCXX20_CONSTEXPR
4201 inline typename iterator_traits
<_InputIterator
>::difference_type
4202 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4204 // concept requirements
4205 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4206 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4207 typename iterator_traits
<_InputIterator
>::value_type
>)
4208 __glibcxx_requires_valid_range(__first
, __last
);
4210 return std::__count_if(__first
, __last
,
4211 __gnu_cxx::__ops::__pred_iter(__pred
));
4215 * @brief Search a sequence for a matching sub-sequence.
4216 * @ingroup non_mutating_algorithms
4217 * @param __first1 A forward iterator.
4218 * @param __last1 A forward iterator.
4219 * @param __first2 A forward iterator.
4220 * @param __last2 A forward iterator.
4221 * @return The first iterator @c i in the range @p
4222 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4223 * *(__first2+N) for each @c N in the range @p
4224 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4226 * Searches the range @p [__first1,__last1) for a sub-sequence that
4227 * compares equal value-by-value with the sequence given by @p
4228 * [__first2,__last2) and returns an iterator to the first element
4229 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4232 * Because the sub-sequence must lie completely within the range @p
4233 * [__first1,__last1) it must start at a position less than @p
4234 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4235 * length of the sub-sequence.
4237 * This means that the returned iterator @c i will be in the range
4238 * @p [__first1,__last1-(__last2-__first2))
4240 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4241 _GLIBCXX20_CONSTEXPR
4242 inline _ForwardIterator1
4243 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4244 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4246 // concept requirements
4247 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4248 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4249 __glibcxx_function_requires(_EqualOpConcept
<
4250 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4251 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4252 __glibcxx_requires_valid_range(__first1
, __last1
);
4253 __glibcxx_requires_valid_range(__first2
, __last2
);
4255 return std::__search(__first1
, __last1
, __first2
, __last2
,
4256 __gnu_cxx::__ops::__iter_equal_to_iter());
4260 * @brief Search a sequence for a matching sub-sequence using a predicate.
4261 * @ingroup non_mutating_algorithms
4262 * @param __first1 A forward iterator.
4263 * @param __last1 A forward iterator.
4264 * @param __first2 A forward iterator.
4265 * @param __last2 A forward iterator.
4266 * @param __predicate A binary predicate.
4267 * @return The first iterator @c i in the range
4268 * @p [__first1,__last1-(__last2-__first2)) such that
4269 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4270 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4272 * Searches the range @p [__first1,__last1) for a sub-sequence that
4273 * compares equal value-by-value with the sequence given by @p
4274 * [__first2,__last2), using @p __predicate to determine equality,
4275 * and returns an iterator to the first element of the
4276 * sub-sequence, or @p __last1 if no such iterator exists.
4278 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4280 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4281 typename _BinaryPredicate
>
4282 _GLIBCXX20_CONSTEXPR
4283 inline _ForwardIterator1
4284 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4285 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4286 _BinaryPredicate __predicate
)
4288 // concept requirements
4289 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4290 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4291 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4292 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4293 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4294 __glibcxx_requires_valid_range(__first1
, __last1
);
4295 __glibcxx_requires_valid_range(__first2
, __last2
);
4297 return std::__search(__first1
, __last1
, __first2
, __last2
,
4298 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4302 * @brief Search a sequence for a number of consecutive values.
4303 * @ingroup non_mutating_algorithms
4304 * @param __first A forward iterator.
4305 * @param __last A forward iterator.
4306 * @param __count The number of consecutive values.
4307 * @param __val The value to find.
4308 * @return The first iterator @c i in the range @p
4309 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4310 * each @c N in the range @p [0,__count), or @p __last if no such
4313 * Searches the range @p [__first,__last) for @p count consecutive elements
4314 * equal to @p __val.
4316 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4317 _GLIBCXX20_CONSTEXPR
4318 inline _ForwardIterator
4319 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4320 _Integer __count
, const _Tp
& __val
)
4322 // concept requirements
4323 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4324 __glibcxx_function_requires(_EqualOpConcept
<
4325 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4326 __glibcxx_requires_valid_range(__first
, __last
);
4328 return std::__search_n(__first
, __last
, __count
,
4329 __gnu_cxx::__ops::__iter_equals_val(__val
));
4334 * @brief Search a sequence for a number of consecutive values using a
4336 * @ingroup non_mutating_algorithms
4337 * @param __first A forward iterator.
4338 * @param __last A forward iterator.
4339 * @param __count The number of consecutive values.
4340 * @param __val The value to find.
4341 * @param __binary_pred A binary predicate.
4342 * @return The first iterator @c i in the range @p
4343 * [__first,__last-__count) such that @p
4344 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4345 * @p [0,__count), or @p __last if no such iterator exists.
4347 * Searches the range @p [__first,__last) for @p __count
4348 * consecutive elements for which the predicate returns true.
4350 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4351 typename _BinaryPredicate
>
4352 _GLIBCXX20_CONSTEXPR
4353 inline _ForwardIterator
4354 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4355 _Integer __count
, const _Tp
& __val
,
4356 _BinaryPredicate __binary_pred
)
4358 // concept requirements
4359 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4360 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4361 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4362 __glibcxx_requires_valid_range(__first
, __last
);
4364 return std::__search_n(__first
, __last
, __count
,
4365 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4368 #if __cplusplus > 201402L
4369 /** @brief Search a sequence using a Searcher object.
4371 * @param __first A forward iterator.
4372 * @param __last A forward iterator.
4373 * @param __searcher A callable object.
4374 * @return @p __searcher(__first,__last).first
4376 template<typename _ForwardIterator
, typename _Searcher
>
4377 inline _ForwardIterator
4378 search(_ForwardIterator __first
, _ForwardIterator __last
,
4379 const _Searcher
& __searcher
)
4380 { return __searcher(__first
, __last
).first
; }
4384 * @brief Perform an operation on a sequence.
4385 * @ingroup mutating_algorithms
4386 * @param __first An input iterator.
4387 * @param __last An input iterator.
4388 * @param __result An output iterator.
4389 * @param __unary_op A unary operator.
4390 * @return An output iterator equal to @p __result+(__last-__first).
4392 * Applies the operator to each element in the input range and assigns
4393 * the results to successive elements of the output sequence.
4394 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4395 * range @p [0,__last-__first).
4397 * @p unary_op must not alter its argument.
4399 template<typename _InputIterator
, typename _OutputIterator
,
4400 typename _UnaryOperation
>
4401 _GLIBCXX20_CONSTEXPR
4403 transform(_InputIterator __first
, _InputIterator __last
,
4404 _OutputIterator __result
, _UnaryOperation __unary_op
)
4406 // concept requirements
4407 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4408 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4409 // "the type returned by a _UnaryOperation"
4410 __typeof__(__unary_op(*__first
))>)
4411 __glibcxx_requires_valid_range(__first
, __last
);
4413 for (; __first
!= __last
; ++__first
, (void)++__result
)
4414 *__result
= __unary_op(*__first
);
4419 * @brief Perform an operation on corresponding elements of two sequences.
4420 * @ingroup mutating_algorithms
4421 * @param __first1 An input iterator.
4422 * @param __last1 An input iterator.
4423 * @param __first2 An input iterator.
4424 * @param __result An output iterator.
4425 * @param __binary_op A binary operator.
4426 * @return An output iterator equal to @p result+(last-first).
4428 * Applies the operator to the corresponding elements in the two
4429 * input ranges and assigns the results to successive elements of the
4432 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4433 * @c N in the range @p [0,__last1-__first1).
4435 * @p binary_op must not alter either of its arguments.
4437 template<typename _InputIterator1
, typename _InputIterator2
,
4438 typename _OutputIterator
, typename _BinaryOperation
>
4439 _GLIBCXX20_CONSTEXPR
4441 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4442 _InputIterator2 __first2
, _OutputIterator __result
,
4443 _BinaryOperation __binary_op
)
4445 // concept requirements
4446 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4447 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4448 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4449 // "the type returned by a _BinaryOperation"
4450 __typeof__(__binary_op(*__first1
,*__first2
))>)
4451 __glibcxx_requires_valid_range(__first1
, __last1
);
4453 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4454 *__result
= __binary_op(*__first1
, *__first2
);
4459 * @brief Replace each occurrence of one value in a sequence with another
4461 * @ingroup mutating_algorithms
4462 * @param __first A forward iterator.
4463 * @param __last A forward iterator.
4464 * @param __old_value The value to be replaced.
4465 * @param __new_value The replacement value.
4466 * @return replace() returns no value.
4468 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4469 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4471 template<typename _ForwardIterator
, typename _Tp
>
4472 _GLIBCXX20_CONSTEXPR
4474 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4475 const _Tp
& __old_value
, const _Tp
& __new_value
)
4477 // concept requirements
4478 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4480 __glibcxx_function_requires(_EqualOpConcept
<
4481 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4482 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4483 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4484 __glibcxx_requires_valid_range(__first
, __last
);
4486 for (; __first
!= __last
; ++__first
)
4487 if (*__first
== __old_value
)
4488 *__first
= __new_value
;
4492 * @brief Replace each value in a sequence for which a predicate returns
4493 * true with another value.
4494 * @ingroup mutating_algorithms
4495 * @param __first A forward iterator.
4496 * @param __last A forward iterator.
4497 * @param __pred A predicate.
4498 * @param __new_value The replacement value.
4499 * @return replace_if() returns no value.
4501 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4502 * is true then the assignment @c *i = @p __new_value is performed.
4504 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4505 _GLIBCXX20_CONSTEXPR
4507 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4508 _Predicate __pred
, const _Tp
& __new_value
)
4510 // concept requirements
4511 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4513 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4514 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4515 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4516 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4517 __glibcxx_requires_valid_range(__first
, __last
);
4519 for (; __first
!= __last
; ++__first
)
4520 if (__pred(*__first
))
4521 *__first
= __new_value
;
4525 * @brief Assign the result of a function object to each value in a
4527 * @ingroup mutating_algorithms
4528 * @param __first A forward iterator.
4529 * @param __last A forward iterator.
4530 * @param __gen A function object taking no arguments and returning
4531 * std::iterator_traits<_ForwardIterator>::value_type
4532 * @return generate() returns no value.
4534 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4535 * @p [__first,__last).
4537 template<typename _ForwardIterator
, typename _Generator
>
4538 _GLIBCXX20_CONSTEXPR
4540 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4543 // concept requirements
4544 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4545 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4546 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4547 __glibcxx_requires_valid_range(__first
, __last
);
4549 for (; __first
!= __last
; ++__first
)
4554 * @brief Assign the result of a function object to each value in a
4556 * @ingroup mutating_algorithms
4557 * @param __first A forward iterator.
4558 * @param __n The length of the sequence.
4559 * @param __gen A function object taking no arguments and returning
4560 * std::iterator_traits<_ForwardIterator>::value_type
4561 * @return The end of the sequence, @p __first+__n
4563 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4564 * @p [__first,__first+__n).
4566 * If @p __n is negative, the function does nothing and returns @p __first.
4568 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4569 // DR 865. More algorithms that throw away information
4570 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4571 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4572 _GLIBCXX20_CONSTEXPR
4574 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4576 // concept requirements
4577 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4578 // "the type returned by a _Generator"
4579 __typeof__(__gen())>)
4581 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4582 for (_IntSize __niter
= std::__size_to_integer(__n
);
4583 __niter
> 0; --__niter
, (void) ++__first
)
4589 * @brief Copy a sequence, removing consecutive duplicate values.
4590 * @ingroup mutating_algorithms
4591 * @param __first An input iterator.
4592 * @param __last An input iterator.
4593 * @param __result An output iterator.
4594 * @return An iterator designating the end of the resulting sequence.
4596 * Copies each element in the range @p [__first,__last) to the range
4597 * beginning at @p __result, except that only the first element is copied
4598 * from groups of consecutive elements that compare equal.
4599 * unique_copy() is stable, so the relative order of elements that are
4600 * copied is unchanged.
4602 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4603 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4605 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4606 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4609 template<typename _InputIterator
, typename _OutputIterator
>
4610 _GLIBCXX20_CONSTEXPR
4611 inline _OutputIterator
4612 unique_copy(_InputIterator __first
, _InputIterator __last
,
4613 _OutputIterator __result
)
4615 // concept requirements
4616 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4617 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4618 typename iterator_traits
<_InputIterator
>::value_type
>)
4619 __glibcxx_function_requires(_EqualityComparableConcept
<
4620 typename iterator_traits
<_InputIterator
>::value_type
>)
4621 __glibcxx_requires_valid_range(__first
, __last
);
4623 if (__first
== __last
)
4625 return std::__unique_copy(__first
, __last
, __result
,
4626 __gnu_cxx::__ops::__iter_equal_to_iter(),
4627 std::__iterator_category(__first
),
4628 std::__iterator_category(__result
));
4632 * @brief Copy a sequence, removing consecutive values using a predicate.
4633 * @ingroup mutating_algorithms
4634 * @param __first An input iterator.
4635 * @param __last An input iterator.
4636 * @param __result An output iterator.
4637 * @param __binary_pred A binary predicate.
4638 * @return An iterator designating the end of the resulting sequence.
4640 * Copies each element in the range @p [__first,__last) to the range
4641 * beginning at @p __result, except that only the first element is copied
4642 * from groups of consecutive elements for which @p __binary_pred returns
4644 * unique_copy() is stable, so the relative order of elements that are
4645 * copied is unchanged.
4647 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4648 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4650 template<typename _InputIterator
, typename _OutputIterator
,
4651 typename _BinaryPredicate
>
4652 _GLIBCXX20_CONSTEXPR
4653 inline _OutputIterator
4654 unique_copy(_InputIterator __first
, _InputIterator __last
,
4655 _OutputIterator __result
,
4656 _BinaryPredicate __binary_pred
)
4658 // concept requirements -- predicates checked later
4659 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4660 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4661 typename iterator_traits
<_InputIterator
>::value_type
>)
4662 __glibcxx_requires_valid_range(__first
, __last
);
4664 if (__first
== __last
)
4666 return std::__unique_copy(__first
, __last
, __result
,
4667 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4668 std::__iterator_category(__first
),
4669 std::__iterator_category(__result
));
4674 * @brief Randomly shuffle the elements of a sequence.
4675 * @ingroup mutating_algorithms
4676 * @param __first A forward iterator.
4677 * @param __last A forward iterator.
4680 * Reorder the elements in the range @p [__first,__last) using a random
4681 * distribution, so that every possible ordering of the sequence is
4684 template<typename _RandomAccessIterator
>
4686 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4688 // concept requirements
4689 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4690 _RandomAccessIterator
>)
4691 __glibcxx_requires_valid_range(__first
, __last
);
4693 if (__first
!= __last
)
4694 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4696 // XXX rand() % N is not uniformly distributed
4697 _RandomAccessIterator __j
= __first
4698 + std::rand() % ((__i
- __first
) + 1);
4700 std::iter_swap(__i
, __j
);
4706 * @brief Shuffle the elements of a sequence using a random number
4708 * @ingroup mutating_algorithms
4709 * @param __first A forward iterator.
4710 * @param __last A forward iterator.
4711 * @param __rand The RNG functor or function.
4714 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4715 * provide a random distribution. Calling @p __rand(N) for a positive
4716 * integer @p N should return a randomly chosen integer from the
4719 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4721 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4722 #if __cplusplus >= 201103L
4723 _RandomNumberGenerator
&& __rand
)
4725 _RandomNumberGenerator
& __rand
)
4728 // concept requirements
4729 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4730 _RandomAccessIterator
>)
4731 __glibcxx_requires_valid_range(__first
, __last
);
4733 if (__first
== __last
)
4735 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4737 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4739 std::iter_swap(__i
, __j
);
4745 * @brief Move elements for which a predicate is true to the beginning
4747 * @ingroup mutating_algorithms
4748 * @param __first A forward iterator.
4749 * @param __last A forward iterator.
4750 * @param __pred A predicate functor.
4751 * @return An iterator @p middle such that @p __pred(i) is true for each
4752 * iterator @p i in the range @p [__first,middle) and false for each @p i
4753 * in the range @p [middle,__last).
4755 * @p __pred must not modify its operand. @p partition() does not preserve
4756 * the relative ordering of elements in each group, use
4757 * @p stable_partition() if this is needed.
4759 template<typename _ForwardIterator
, typename _Predicate
>
4760 inline _ForwardIterator
4761 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4764 // concept requirements
4765 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4767 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4768 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4769 __glibcxx_requires_valid_range(__first
, __last
);
4771 return std::__partition(__first
, __last
, __pred
,
4772 std::__iterator_category(__first
));
4777 * @brief Sort the smallest elements of a sequence.
4778 * @ingroup sorting_algorithms
4779 * @param __first An iterator.
4780 * @param __middle Another iterator.
4781 * @param __last Another iterator.
4784 * Sorts the smallest @p (__middle-__first) elements in the range
4785 * @p [first,last) and moves them to the range @p [__first,__middle). The
4786 * order of the remaining elements in the range @p [__middle,__last) is
4788 * After the sort if @e i and @e j are iterators in the range
4789 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4790 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4792 template<typename _RandomAccessIterator
>
4794 partial_sort(_RandomAccessIterator __first
,
4795 _RandomAccessIterator __middle
,
4796 _RandomAccessIterator __last
)
4798 // concept requirements
4799 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4800 _RandomAccessIterator
>)
4801 __glibcxx_function_requires(_LessThanComparableConcept
<
4802 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4803 __glibcxx_requires_valid_range(__first
, __middle
);
4804 __glibcxx_requires_valid_range(__middle
, __last
);
4805 __glibcxx_requires_irreflexive(__first
, __last
);
4807 std::__partial_sort(__first
, __middle
, __last
,
4808 __gnu_cxx::__ops::__iter_less_iter());
4812 * @brief Sort the smallest elements of a sequence using a predicate
4814 * @ingroup sorting_algorithms
4815 * @param __first An iterator.
4816 * @param __middle Another iterator.
4817 * @param __last Another iterator.
4818 * @param __comp A comparison functor.
4821 * Sorts the smallest @p (__middle-__first) elements in the range
4822 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4823 * order of the remaining elements in the range @p [__middle,__last) is
4825 * After the sort if @e i and @e j are iterators in the range
4826 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4827 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4830 template<typename _RandomAccessIterator
, typename _Compare
>
4832 partial_sort(_RandomAccessIterator __first
,
4833 _RandomAccessIterator __middle
,
4834 _RandomAccessIterator __last
,
4837 // concept requirements
4838 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4839 _RandomAccessIterator
>)
4840 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4841 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4842 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4843 __glibcxx_requires_valid_range(__first
, __middle
);
4844 __glibcxx_requires_valid_range(__middle
, __last
);
4845 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4847 std::__partial_sort(__first
, __middle
, __last
,
4848 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4852 * @brief Sort a sequence just enough to find a particular position.
4853 * @ingroup sorting_algorithms
4854 * @param __first An iterator.
4855 * @param __nth Another iterator.
4856 * @param __last Another iterator.
4859 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4860 * is the same element that would have been in that position had the
4861 * whole sequence been sorted. The elements either side of @p *__nth are
4862 * not completely sorted, but for any iterator @e i in the range
4863 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4864 * holds that *j < *i is false.
4866 template<typename _RandomAccessIterator
>
4868 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4869 _RandomAccessIterator __last
)
4871 // concept requirements
4872 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4873 _RandomAccessIterator
>)
4874 __glibcxx_function_requires(_LessThanComparableConcept
<
4875 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4876 __glibcxx_requires_valid_range(__first
, __nth
);
4877 __glibcxx_requires_valid_range(__nth
, __last
);
4878 __glibcxx_requires_irreflexive(__first
, __last
);
4880 if (__first
== __last
|| __nth
== __last
)
4883 std::__introselect(__first
, __nth
, __last
,
4884 std::__lg(__last
- __first
) * 2,
4885 __gnu_cxx::__ops::__iter_less_iter());
4889 * @brief Sort a sequence just enough to find a particular position
4890 * using a predicate for comparison.
4891 * @ingroup sorting_algorithms
4892 * @param __first An iterator.
4893 * @param __nth Another iterator.
4894 * @param __last Another iterator.
4895 * @param __comp A comparison functor.
4898 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4899 * is the same element that would have been in that position had the
4900 * whole sequence been sorted. The elements either side of @p *__nth are
4901 * not completely sorted, but for any iterator @e i in the range
4902 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4903 * holds that @p __comp(*j,*i) is false.
4905 template<typename _RandomAccessIterator
, typename _Compare
>
4907 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4908 _RandomAccessIterator __last
, _Compare __comp
)
4910 // concept requirements
4911 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4912 _RandomAccessIterator
>)
4913 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4914 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4915 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4916 __glibcxx_requires_valid_range(__first
, __nth
);
4917 __glibcxx_requires_valid_range(__nth
, __last
);
4918 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4920 if (__first
== __last
|| __nth
== __last
)
4923 std::__introselect(__first
, __nth
, __last
,
4924 std::__lg(__last
- __first
) * 2,
4925 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4929 * @brief Sort the elements of a sequence.
4930 * @ingroup sorting_algorithms
4931 * @param __first An iterator.
4932 * @param __last Another iterator.
4935 * Sorts the elements in the range @p [__first,__last) in ascending order,
4936 * such that for each iterator @e i in the range @p [__first,__last-1),
4937 * *(i+1)<*i is false.
4939 * The relative ordering of equivalent elements is not preserved, use
4940 * @p stable_sort() if this is needed.
4942 template<typename _RandomAccessIterator
>
4944 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4946 // concept requirements
4947 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4948 _RandomAccessIterator
>)
4949 __glibcxx_function_requires(_LessThanComparableConcept
<
4950 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4951 __glibcxx_requires_valid_range(__first
, __last
);
4952 __glibcxx_requires_irreflexive(__first
, __last
);
4954 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4958 * @brief Sort the elements of a sequence using a predicate for comparison.
4959 * @ingroup sorting_algorithms
4960 * @param __first An iterator.
4961 * @param __last Another iterator.
4962 * @param __comp A comparison functor.
4965 * Sorts the elements in the range @p [__first,__last) in ascending order,
4966 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4967 * range @p [__first,__last-1).
4969 * The relative ordering of equivalent elements is not preserved, use
4970 * @p stable_sort() if this is needed.
4972 template<typename _RandomAccessIterator
, typename _Compare
>
4974 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4977 // concept requirements
4978 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4979 _RandomAccessIterator
>)
4980 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4981 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4982 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4983 __glibcxx_requires_valid_range(__first
, __last
);
4984 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4986 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4989 template<typename _InputIterator1
, typename _InputIterator2
,
4990 typename _OutputIterator
, typename _Compare
>
4991 _GLIBCXX20_CONSTEXPR
4993 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4994 _InputIterator2 __first2
, _InputIterator2 __last2
,
4995 _OutputIterator __result
, _Compare __comp
)
4997 while (__first1
!= __last1
&& __first2
!= __last2
)
4999 if (__comp(__first2
, __first1
))
5001 *__result
= *__first2
;
5006 *__result
= *__first1
;
5011 return std::copy(__first2
, __last2
,
5012 std::copy(__first1
, __last1
, __result
));
5016 * @brief Merges two sorted ranges.
5017 * @ingroup sorting_algorithms
5018 * @param __first1 An iterator.
5019 * @param __first2 Another iterator.
5020 * @param __last1 Another iterator.
5021 * @param __last2 Another iterator.
5022 * @param __result An iterator pointing to the end of the merged range.
5023 * @return An iterator pointing to the first element <em>not less
5026 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5027 * the sorted range @p [__result, __result + (__last1-__first1) +
5028 * (__last2-__first2)). Both input ranges must be sorted, and the
5029 * output range must not overlap with either of the input ranges.
5030 * The sort is @e stable, that is, for equivalent elements in the
5031 * two ranges, elements from the first range will always come
5032 * before elements from the second.
5034 template<typename _InputIterator1
, typename _InputIterator2
,
5035 typename _OutputIterator
>
5036 _GLIBCXX20_CONSTEXPR
5037 inline _OutputIterator
5038 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5039 _InputIterator2 __first2
, _InputIterator2 __last2
,
5040 _OutputIterator __result
)
5042 // concept requirements
5043 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5044 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5045 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5046 typename iterator_traits
<_InputIterator1
>::value_type
>)
5047 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5048 typename iterator_traits
<_InputIterator2
>::value_type
>)
5049 __glibcxx_function_requires(_LessThanOpConcept
<
5050 typename iterator_traits
<_InputIterator2
>::value_type
,
5051 typename iterator_traits
<_InputIterator1
>::value_type
>)
5052 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5053 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5054 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5055 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5057 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5058 __first2
, __last2
, __result
,
5059 __gnu_cxx::__ops::__iter_less_iter());
5063 * @brief Merges two sorted ranges.
5064 * @ingroup sorting_algorithms
5065 * @param __first1 An iterator.
5066 * @param __first2 Another iterator.
5067 * @param __last1 Another iterator.
5068 * @param __last2 Another iterator.
5069 * @param __result An iterator pointing to the end of the merged range.
5070 * @param __comp A functor to use for comparisons.
5071 * @return An iterator pointing to the first element "not less
5074 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5075 * the sorted range @p [__result, __result + (__last1-__first1) +
5076 * (__last2-__first2)). Both input ranges must be sorted, and the
5077 * output range must not overlap with either of the input ranges.
5078 * The sort is @e stable, that is, for equivalent elements in the
5079 * two ranges, elements from the first range will always come
5080 * before elements from the second.
5082 * The comparison function should have the same effects on ordering as
5083 * the function used for the initial sort.
5085 template<typename _InputIterator1
, typename _InputIterator2
,
5086 typename _OutputIterator
, typename _Compare
>
5087 _GLIBCXX20_CONSTEXPR
5088 inline _OutputIterator
5089 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5090 _InputIterator2 __first2
, _InputIterator2 __last2
,
5091 _OutputIterator __result
, _Compare __comp
)
5093 // concept requirements
5094 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5095 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5096 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5097 typename iterator_traits
<_InputIterator1
>::value_type
>)
5098 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5099 typename iterator_traits
<_InputIterator2
>::value_type
>)
5100 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5101 typename iterator_traits
<_InputIterator2
>::value_type
,
5102 typename iterator_traits
<_InputIterator1
>::value_type
>)
5103 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5104 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5105 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5106 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5108 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5109 __first2
, __last2
, __result
,
5110 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5113 template<typename _RandomAccessIterator
, typename _Compare
>
5115 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5118 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5120 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5123 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5124 _TmpBuf
__buf(__first
, std::distance(__first
, __last
));
5126 if (__buf
.begin() == 0)
5127 std::__inplace_stable_sort(__first
, __last
, __comp
);
5129 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5130 _DistanceType(__buf
.size()), __comp
);
5134 * @brief Sort the elements of a sequence, preserving the relative order
5135 * of equivalent elements.
5136 * @ingroup sorting_algorithms
5137 * @param __first An iterator.
5138 * @param __last Another iterator.
5141 * Sorts the elements in the range @p [__first,__last) in ascending order,
5142 * such that for each iterator @p i in the range @p [__first,__last-1),
5143 * @p *(i+1)<*i is false.
5145 * The relative ordering of equivalent elements is preserved, so any two
5146 * elements @p x and @p y in the range @p [__first,__last) such that
5147 * @p x<y is false and @p y<x is false will have the same relative
5148 * ordering after calling @p stable_sort().
5150 template<typename _RandomAccessIterator
>
5152 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5154 // concept requirements
5155 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5156 _RandomAccessIterator
>)
5157 __glibcxx_function_requires(_LessThanComparableConcept
<
5158 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5159 __glibcxx_requires_valid_range(__first
, __last
);
5160 __glibcxx_requires_irreflexive(__first
, __last
);
5162 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5163 __gnu_cxx::__ops::__iter_less_iter());
5167 * @brief Sort the elements of a sequence using a predicate for comparison,
5168 * preserving the relative order of equivalent elements.
5169 * @ingroup sorting_algorithms
5170 * @param __first An iterator.
5171 * @param __last Another iterator.
5172 * @param __comp A comparison functor.
5175 * Sorts the elements in the range @p [__first,__last) in ascending order,
5176 * such that for each iterator @p i in the range @p [__first,__last-1),
5177 * @p __comp(*(i+1),*i) is false.
5179 * The relative ordering of equivalent elements is preserved, so any two
5180 * elements @p x and @p y in the range @p [__first,__last) such that
5181 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5182 * relative ordering after calling @p stable_sort().
5184 template<typename _RandomAccessIterator
, typename _Compare
>
5186 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5189 // concept requirements
5190 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5191 _RandomAccessIterator
>)
5192 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5193 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5194 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5195 __glibcxx_requires_valid_range(__first
, __last
);
5196 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5198 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5199 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5202 template<typename _InputIterator1
, typename _InputIterator2
,
5203 typename _OutputIterator
,
5205 _GLIBCXX20_CONSTEXPR
5207 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5208 _InputIterator2 __first2
, _InputIterator2 __last2
,
5209 _OutputIterator __result
, _Compare __comp
)
5211 while (__first1
!= __last1
&& __first2
!= __last2
)
5213 if (__comp(__first1
, __first2
))
5215 *__result
= *__first1
;
5218 else if (__comp(__first2
, __first1
))
5220 *__result
= *__first2
;
5225 *__result
= *__first1
;
5231 return std::copy(__first2
, __last2
,
5232 std::copy(__first1
, __last1
, __result
));
5236 * @brief Return the union of two sorted ranges.
5237 * @ingroup set_algorithms
5238 * @param __first1 Start of first range.
5239 * @param __last1 End of first range.
5240 * @param __first2 Start of second range.
5241 * @param __last2 End of second range.
5242 * @param __result Start of output range.
5243 * @return End of the output range.
5244 * @ingroup set_algorithms
5246 * This operation iterates over both ranges, copying elements present in
5247 * each range in order to the output range. Iterators increment for each
5248 * range. When the current element of one range is less than the other,
5249 * that element is copied and the iterator advanced. If an element is
5250 * contained in both ranges, the element from the first range is copied and
5251 * both ranges advance. The output range may not overlap either input
5254 template<typename _InputIterator1
, typename _InputIterator2
,
5255 typename _OutputIterator
>
5256 _GLIBCXX20_CONSTEXPR
5257 inline _OutputIterator
5258 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5259 _InputIterator2 __first2
, _InputIterator2 __last2
,
5260 _OutputIterator __result
)
5262 // concept requirements
5263 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5264 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5265 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5266 typename iterator_traits
<_InputIterator1
>::value_type
>)
5267 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5268 typename iterator_traits
<_InputIterator2
>::value_type
>)
5269 __glibcxx_function_requires(_LessThanOpConcept
<
5270 typename iterator_traits
<_InputIterator1
>::value_type
,
5271 typename iterator_traits
<_InputIterator2
>::value_type
>)
5272 __glibcxx_function_requires(_LessThanOpConcept
<
5273 typename iterator_traits
<_InputIterator2
>::value_type
,
5274 typename iterator_traits
<_InputIterator1
>::value_type
>)
5275 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5276 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5277 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5278 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5280 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5281 __first2
, __last2
, __result
,
5282 __gnu_cxx::__ops::__iter_less_iter());
5286 * @brief Return the union of two sorted ranges using a comparison functor.
5287 * @ingroup set_algorithms
5288 * @param __first1 Start of first range.
5289 * @param __last1 End of first range.
5290 * @param __first2 Start of second range.
5291 * @param __last2 End of second range.
5292 * @param __result Start of output range.
5293 * @param __comp The comparison functor.
5294 * @return End of the output range.
5295 * @ingroup set_algorithms
5297 * This operation iterates over both ranges, copying elements present in
5298 * each range in order to the output range. Iterators increment for each
5299 * range. When the current element of one range is less than the other
5300 * according to @p __comp, that element is copied and the iterator advanced.
5301 * If an equivalent element according to @p __comp is contained in both
5302 * ranges, the element from the first range is copied and both ranges
5303 * advance. The output range may not overlap either input range.
5305 template<typename _InputIterator1
, typename _InputIterator2
,
5306 typename _OutputIterator
, typename _Compare
>
5307 _GLIBCXX20_CONSTEXPR
5308 inline _OutputIterator
5309 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5310 _InputIterator2 __first2
, _InputIterator2 __last2
,
5311 _OutputIterator __result
, _Compare __comp
)
5313 // concept requirements
5314 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5315 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5316 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5317 typename iterator_traits
<_InputIterator1
>::value_type
>)
5318 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5319 typename iterator_traits
<_InputIterator2
>::value_type
>)
5320 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5321 typename iterator_traits
<_InputIterator1
>::value_type
,
5322 typename iterator_traits
<_InputIterator2
>::value_type
>)
5323 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5324 typename iterator_traits
<_InputIterator2
>::value_type
,
5325 typename iterator_traits
<_InputIterator1
>::value_type
>)
5326 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5327 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5328 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5329 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5331 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5332 __first2
, __last2
, __result
,
5333 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5336 template<typename _InputIterator1
, typename _InputIterator2
,
5337 typename _OutputIterator
,
5339 _GLIBCXX20_CONSTEXPR
5341 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5342 _InputIterator2 __first2
, _InputIterator2 __last2
,
5343 _OutputIterator __result
, _Compare __comp
)
5345 while (__first1
!= __last1
&& __first2
!= __last2
)
5346 if (__comp(__first1
, __first2
))
5348 else if (__comp(__first2
, __first1
))
5352 *__result
= *__first1
;
5361 * @brief Return the intersection of two sorted ranges.
5362 * @ingroup set_algorithms
5363 * @param __first1 Start of first range.
5364 * @param __last1 End of first range.
5365 * @param __first2 Start of second range.
5366 * @param __last2 End of second range.
5367 * @param __result Start of output range.
5368 * @return End of the output range.
5369 * @ingroup set_algorithms
5371 * This operation iterates over both ranges, copying elements present in
5372 * both ranges in order to the output range. Iterators increment for each
5373 * range. When the current element of one range is less than the other,
5374 * that iterator advances. If an element is contained in both ranges, the
5375 * element from the first range is copied and both ranges advance. The
5376 * output range may not overlap either input range.
5378 template<typename _InputIterator1
, typename _InputIterator2
,
5379 typename _OutputIterator
>
5380 _GLIBCXX20_CONSTEXPR
5381 inline _OutputIterator
5382 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5383 _InputIterator2 __first2
, _InputIterator2 __last2
,
5384 _OutputIterator __result
)
5386 // concept requirements
5387 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5388 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5389 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5390 typename iterator_traits
<_InputIterator1
>::value_type
>)
5391 __glibcxx_function_requires(_LessThanOpConcept
<
5392 typename iterator_traits
<_InputIterator1
>::value_type
,
5393 typename iterator_traits
<_InputIterator2
>::value_type
>)
5394 __glibcxx_function_requires(_LessThanOpConcept
<
5395 typename iterator_traits
<_InputIterator2
>::value_type
,
5396 typename iterator_traits
<_InputIterator1
>::value_type
>)
5397 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5398 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5399 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5400 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5402 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5403 __first2
, __last2
, __result
,
5404 __gnu_cxx::__ops::__iter_less_iter());
5408 * @brief Return the intersection of two sorted ranges using comparison
5410 * @ingroup set_algorithms
5411 * @param __first1 Start of first range.
5412 * @param __last1 End of first range.
5413 * @param __first2 Start of second range.
5414 * @param __last2 End of second range.
5415 * @param __result Start of output range.
5416 * @param __comp The comparison functor.
5417 * @return End of the output range.
5418 * @ingroup set_algorithms
5420 * This operation iterates over both ranges, copying elements present in
5421 * both ranges in order to the output range. Iterators increment for each
5422 * range. When the current element of one range is less than the other
5423 * according to @p __comp, that iterator advances. If an element is
5424 * contained in both ranges according to @p __comp, the element from the
5425 * first range is copied and both ranges advance. The output range may not
5426 * overlap either input range.
5428 template<typename _InputIterator1
, typename _InputIterator2
,
5429 typename _OutputIterator
, typename _Compare
>
5430 _GLIBCXX20_CONSTEXPR
5431 inline _OutputIterator
5432 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5433 _InputIterator2 __first2
, _InputIterator2 __last2
,
5434 _OutputIterator __result
, _Compare __comp
)
5436 // concept requirements
5437 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5438 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5439 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5440 typename iterator_traits
<_InputIterator1
>::value_type
>)
5441 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5442 typename iterator_traits
<_InputIterator1
>::value_type
,
5443 typename iterator_traits
<_InputIterator2
>::value_type
>)
5444 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5445 typename iterator_traits
<_InputIterator2
>::value_type
,
5446 typename iterator_traits
<_InputIterator1
>::value_type
>)
5447 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5448 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5449 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5450 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5452 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5453 __first2
, __last2
, __result
,
5454 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5457 template<typename _InputIterator1
, typename _InputIterator2
,
5458 typename _OutputIterator
,
5460 _GLIBCXX20_CONSTEXPR
5462 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5463 _InputIterator2 __first2
, _InputIterator2 __last2
,
5464 _OutputIterator __result
, _Compare __comp
)
5466 while (__first1
!= __last1
&& __first2
!= __last2
)
5467 if (__comp(__first1
, __first2
))
5469 *__result
= *__first1
;
5473 else if (__comp(__first2
, __first1
))
5480 return std::copy(__first1
, __last1
, __result
);
5484 * @brief Return the difference of two sorted ranges.
5485 * @ingroup set_algorithms
5486 * @param __first1 Start of first range.
5487 * @param __last1 End of first range.
5488 * @param __first2 Start of second range.
5489 * @param __last2 End of second range.
5490 * @param __result Start of output range.
5491 * @return End of the output range.
5492 * @ingroup set_algorithms
5494 * This operation iterates over both ranges, copying elements present in
5495 * the first range but not the second in order to the output range.
5496 * Iterators increment for each range. When the current element of the
5497 * first range is less than the second, that element is copied and the
5498 * iterator advances. If the current element of the second range is less,
5499 * the iterator advances, but no element is copied. If an element is
5500 * contained in both ranges, no elements are copied and both ranges
5501 * advance. The output range may not overlap either input range.
5503 template<typename _InputIterator1
, typename _InputIterator2
,
5504 typename _OutputIterator
>
5505 _GLIBCXX20_CONSTEXPR
5506 inline _OutputIterator
5507 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5508 _InputIterator2 __first2
, _InputIterator2 __last2
,
5509 _OutputIterator __result
)
5511 // concept requirements
5512 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5513 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5514 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5515 typename iterator_traits
<_InputIterator1
>::value_type
>)
5516 __glibcxx_function_requires(_LessThanOpConcept
<
5517 typename iterator_traits
<_InputIterator1
>::value_type
,
5518 typename iterator_traits
<_InputIterator2
>::value_type
>)
5519 __glibcxx_function_requires(_LessThanOpConcept
<
5520 typename iterator_traits
<_InputIterator2
>::value_type
,
5521 typename iterator_traits
<_InputIterator1
>::value_type
>)
5522 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5523 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5524 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5525 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5527 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5528 __first2
, __last2
, __result
,
5529 __gnu_cxx::__ops::__iter_less_iter());
5533 * @brief Return the difference of two sorted ranges using comparison
5535 * @ingroup set_algorithms
5536 * @param __first1 Start of first range.
5537 * @param __last1 End of first range.
5538 * @param __first2 Start of second range.
5539 * @param __last2 End of second range.
5540 * @param __result Start of output range.
5541 * @param __comp The comparison functor.
5542 * @return End of the output range.
5543 * @ingroup set_algorithms
5545 * This operation iterates over both ranges, copying elements present in
5546 * the first range but not the second in order to the output range.
5547 * Iterators increment for each range. When the current element of the
5548 * first range is less than the second according to @p __comp, that element
5549 * is copied and the iterator advances. If the current element of the
5550 * second range is less, no element is copied and the iterator advances.
5551 * If an element is contained in both ranges according to @p __comp, no
5552 * elements are copied and both ranges advance. The output range may not
5553 * overlap either input range.
5555 template<typename _InputIterator1
, typename _InputIterator2
,
5556 typename _OutputIterator
, typename _Compare
>
5557 _GLIBCXX20_CONSTEXPR
5558 inline _OutputIterator
5559 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5560 _InputIterator2 __first2
, _InputIterator2 __last2
,
5561 _OutputIterator __result
, _Compare __comp
)
5563 // concept requirements
5564 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5565 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5566 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5567 typename iterator_traits
<_InputIterator1
>::value_type
>)
5568 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5569 typename iterator_traits
<_InputIterator1
>::value_type
,
5570 typename iterator_traits
<_InputIterator2
>::value_type
>)
5571 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5572 typename iterator_traits
<_InputIterator2
>::value_type
,
5573 typename iterator_traits
<_InputIterator1
>::value_type
>)
5574 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5575 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5576 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5577 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5579 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5580 __first2
, __last2
, __result
,
5581 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5584 template<typename _InputIterator1
, typename _InputIterator2
,
5585 typename _OutputIterator
,
5587 _GLIBCXX20_CONSTEXPR
5589 __set_symmetric_difference(_InputIterator1 __first1
,
5590 _InputIterator1 __last1
,
5591 _InputIterator2 __first2
,
5592 _InputIterator2 __last2
,
5593 _OutputIterator __result
,
5596 while (__first1
!= __last1
&& __first2
!= __last2
)
5597 if (__comp(__first1
, __first2
))
5599 *__result
= *__first1
;
5603 else if (__comp(__first2
, __first1
))
5605 *__result
= *__first2
;
5614 return std::copy(__first2
, __last2
,
5615 std::copy(__first1
, __last1
, __result
));
5619 * @brief Return the symmetric difference of two sorted ranges.
5620 * @ingroup set_algorithms
5621 * @param __first1 Start of first range.
5622 * @param __last1 End of first range.
5623 * @param __first2 Start of second range.
5624 * @param __last2 End of second range.
5625 * @param __result Start of output range.
5626 * @return End of the output range.
5627 * @ingroup set_algorithms
5629 * This operation iterates over both ranges, copying elements present in
5630 * one range but not the other in order to the output range. Iterators
5631 * increment for each range. When the current element of one range is less
5632 * than the other, that element is copied and the iterator advances. If an
5633 * element is contained in both ranges, no elements are copied and both
5634 * ranges advance. The output range may not overlap either input range.
5636 template<typename _InputIterator1
, typename _InputIterator2
,
5637 typename _OutputIterator
>
5638 _GLIBCXX20_CONSTEXPR
5639 inline _OutputIterator
5640 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5641 _InputIterator2 __first2
, _InputIterator2 __last2
,
5642 _OutputIterator __result
)
5644 // concept requirements
5645 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5646 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5647 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5648 typename iterator_traits
<_InputIterator1
>::value_type
>)
5649 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5650 typename iterator_traits
<_InputIterator2
>::value_type
>)
5651 __glibcxx_function_requires(_LessThanOpConcept
<
5652 typename iterator_traits
<_InputIterator1
>::value_type
,
5653 typename iterator_traits
<_InputIterator2
>::value_type
>)
5654 __glibcxx_function_requires(_LessThanOpConcept
<
5655 typename iterator_traits
<_InputIterator2
>::value_type
,
5656 typename iterator_traits
<_InputIterator1
>::value_type
>)
5657 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5658 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5659 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5660 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5662 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5663 __first2
, __last2
, __result
,
5664 __gnu_cxx::__ops::__iter_less_iter());
5668 * @brief Return the symmetric difference of two sorted ranges using
5669 * comparison functor.
5670 * @ingroup set_algorithms
5671 * @param __first1 Start of first range.
5672 * @param __last1 End of first range.
5673 * @param __first2 Start of second range.
5674 * @param __last2 End of second range.
5675 * @param __result Start of output range.
5676 * @param __comp The comparison functor.
5677 * @return End of the output range.
5678 * @ingroup set_algorithms
5680 * This operation iterates over both ranges, copying elements present in
5681 * one range but not the other in order to the output range. Iterators
5682 * increment for each range. When the current element of one range is less
5683 * than the other according to @p comp, that element is copied and the
5684 * iterator advances. If an element is contained in both ranges according
5685 * to @p __comp, no elements are copied and both ranges advance. The output
5686 * range may not overlap either input range.
5688 template<typename _InputIterator1
, typename _InputIterator2
,
5689 typename _OutputIterator
, typename _Compare
>
5690 _GLIBCXX20_CONSTEXPR
5691 inline _OutputIterator
5692 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5693 _InputIterator2 __first2
, _InputIterator2 __last2
,
5694 _OutputIterator __result
,
5697 // concept requirements
5698 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5699 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5700 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5701 typename iterator_traits
<_InputIterator1
>::value_type
>)
5702 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5703 typename iterator_traits
<_InputIterator2
>::value_type
>)
5704 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5705 typename iterator_traits
<_InputIterator1
>::value_type
,
5706 typename iterator_traits
<_InputIterator2
>::value_type
>)
5707 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5708 typename iterator_traits
<_InputIterator2
>::value_type
,
5709 typename iterator_traits
<_InputIterator1
>::value_type
>)
5710 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5711 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5712 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5713 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5715 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5716 __first2
, __last2
, __result
,
5717 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5720 template<typename _ForwardIterator
, typename _Compare
>
5721 _GLIBCXX14_CONSTEXPR
5723 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5726 if (__first
== __last
)
5728 _ForwardIterator __result
= __first
;
5729 while (++__first
!= __last
)
5730 if (__comp(__first
, __result
))
5736 * @brief Return the minimum element in a range.
5737 * @ingroup sorting_algorithms
5738 * @param __first Start of range.
5739 * @param __last End of range.
5740 * @return Iterator referencing the first instance of the smallest value.
5742 template<typename _ForwardIterator
>
5743 _GLIBCXX14_CONSTEXPR
5745 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5747 // concept requirements
5748 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5749 __glibcxx_function_requires(_LessThanComparableConcept
<
5750 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5751 __glibcxx_requires_valid_range(__first
, __last
);
5752 __glibcxx_requires_irreflexive(__first
, __last
);
5754 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5755 __gnu_cxx::__ops::__iter_less_iter());
5759 * @brief Return the minimum element in a range using comparison functor.
5760 * @ingroup sorting_algorithms
5761 * @param __first Start of range.
5762 * @param __last End of range.
5763 * @param __comp Comparison functor.
5764 * @return Iterator referencing the first instance of the smallest value
5765 * according to __comp.
5767 template<typename _ForwardIterator
, typename _Compare
>
5768 _GLIBCXX14_CONSTEXPR
5769 inline _ForwardIterator
5770 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5773 // concept requirements
5774 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5775 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5776 typename iterator_traits
<_ForwardIterator
>::value_type
,
5777 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5778 __glibcxx_requires_valid_range(__first
, __last
);
5779 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5781 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5782 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5785 template<typename _ForwardIterator
, typename _Compare
>
5786 _GLIBCXX14_CONSTEXPR
5788 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5791 if (__first
== __last
) return __first
;
5792 _ForwardIterator __result
= __first
;
5793 while (++__first
!= __last
)
5794 if (__comp(__result
, __first
))
5800 * @brief Return the maximum element in a range.
5801 * @ingroup sorting_algorithms
5802 * @param __first Start of range.
5803 * @param __last End of range.
5804 * @return Iterator referencing the first instance of the largest value.
5806 template<typename _ForwardIterator
>
5807 _GLIBCXX14_CONSTEXPR
5808 inline _ForwardIterator
5809 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5811 // concept requirements
5812 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5813 __glibcxx_function_requires(_LessThanComparableConcept
<
5814 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5815 __glibcxx_requires_valid_range(__first
, __last
);
5816 __glibcxx_requires_irreflexive(__first
, __last
);
5818 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5819 __gnu_cxx::__ops::__iter_less_iter());
5823 * @brief Return the maximum element in a range using comparison functor.
5824 * @ingroup sorting_algorithms
5825 * @param __first Start of range.
5826 * @param __last End of range.
5827 * @param __comp Comparison functor.
5828 * @return Iterator referencing the first instance of the largest value
5829 * according to __comp.
5831 template<typename _ForwardIterator
, typename _Compare
>
5832 _GLIBCXX14_CONSTEXPR
5833 inline _ForwardIterator
5834 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5837 // concept requirements
5838 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5839 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5840 typename iterator_traits
<_ForwardIterator
>::value_type
,
5841 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5842 __glibcxx_requires_valid_range(__first
, __last
);
5843 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5845 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5846 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5849 #if __cplusplus >= 201402L
5850 /// Reservoir sampling algorithm.
5851 template<typename _InputIterator
, typename _RandomAccessIterator
,
5852 typename _Size
, typename _UniformRandomBitGenerator
>
5853 _RandomAccessIterator
5854 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5855 _RandomAccessIterator __out
, random_access_iterator_tag
,
5856 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5858 using __distrib_type
= uniform_int_distribution
<_Size
>;
5859 using __param_type
= typename
__distrib_type::param_type
;
5860 __distrib_type __d
{};
5861 _Size __sample_sz
= 0;
5862 while (__first
!= __last
&& __sample_sz
!= __n
)
5864 __out
[__sample_sz
++] = *__first
;
5867 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5868 ++__first
, (void) ++__pop_sz
)
5870 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5872 __out
[__k
] = *__first
;
5874 return __out
+ __sample_sz
;
5877 /// Selection sampling algorithm.
5878 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5879 typename _Size
, typename _UniformRandomBitGenerator
>
5881 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5882 forward_iterator_tag
,
5883 _OutputIterator __out
, _Cat
,
5884 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5886 using __distrib_type
= uniform_int_distribution
<_Size
>;
5887 using __param_type
= typename
__distrib_type::param_type
;
5888 using _USize
= make_unsigned_t
<_Size
>;
5889 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5890 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5892 __distrib_type __d
{};
5893 _Size __unsampled_sz
= std::distance(__first
, __last
);
5894 __n
= std::min(__n
, __unsampled_sz
);
5896 // If possible, we use __gen_two_uniform_ints to efficiently produce
5897 // two random numbers using a single distribution invocation:
5899 const __uc_type __urngrange
= __g
.max() - __g
.min();
5900 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5901 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5904 while (__n
!= 0 && __unsampled_sz
>= 2)
5906 const pair
<_Size
, _Size
> __p
=
5907 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5910 if (__p
.first
< __n
)
5912 *__out
++ = *__first
;
5918 if (__n
== 0) break;
5921 if (__p
.second
< __n
)
5923 *__out
++ = *__first
;
5931 // The loop above is otherwise equivalent to this one-at-a-time version:
5933 for (; __n
!= 0; ++__first
)
5934 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5936 *__out
++ = *__first
;
5942 #if __cplusplus > 201402L
5943 #define __cpp_lib_sample 201603
5944 /// Take a random sample from a population.
5945 template<typename _PopulationIterator
, typename _SampleIterator
,
5946 typename _Distance
, typename _UniformRandomBitGenerator
>
5948 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5949 _SampleIterator __out
, _Distance __n
,
5950 _UniformRandomBitGenerator
&& __g
)
5952 using __pop_cat
= typename
5953 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5954 using __samp_cat
= typename
5955 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5958 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5959 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5960 "output range must use a RandomAccessIterator when input range"
5961 " does not meet the ForwardIterator requirements");
5963 static_assert(is_integral
<_Distance
>::value
,
5964 "sample size must be an integer type");
5966 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5967 return _GLIBCXX_STD_A::
5968 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5969 std::forward
<_UniformRandomBitGenerator
>(__g
));
5974 _GLIBCXX_END_NAMESPACE_ALGO
5975 _GLIBCXX_END_NAMESPACE_VERSION
5978 #endif /* _STL_ALGO_H */