1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2022 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 <bits/algorithmfwd.h>
60 #include <bits/stl_algobase.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 #if _GLIBCXX_HOSTED && (__cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED)
70 #include <cstdlib> // for rand
73 // See concept_check.h for the __glibcxx_*_requires macros.
75 namespace std
_GLIBCXX_VISIBILITY(default)
77 _GLIBCXX_BEGIN_NAMESPACE_VERSION
79 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
80 template<typename _Iterator
, typename _Compare
>
83 __move_median_to_first(_Iterator __result
,_Iterator __a
, _Iterator __b
,
84 _Iterator __c
, _Compare __comp
)
89 std::iter_swap(__result
, __b
);
90 else if (__comp(__a
, __c
))
91 std::iter_swap(__result
, __c
);
93 std::iter_swap(__result
, __a
);
95 else if (__comp(__a
, __c
))
96 std::iter_swap(__result
, __a
);
97 else if (__comp(__b
, __c
))
98 std::iter_swap(__result
, __c
);
100 std::iter_swap(__result
, __b
);
103 /// Provided for stable_partition to use.
104 template<typename _InputIterator
, typename _Predicate
>
106 inline _InputIterator
107 __find_if_not(_InputIterator __first
, _InputIterator __last
,
110 return std::__find_if(__first
, __last
,
111 __gnu_cxx::__ops::__negate(__pred
),
112 std::__iterator_category(__first
));
115 /// Like find_if_not(), but uses and updates a count of the
116 /// remaining range length instead of comparing against an end
118 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
121 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
123 for (; __len
; --__len
, (void) ++__first
)
124 if (!__pred(__first
))
131 // set_symmetric_difference
142 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
143 typename _BinaryPredicate
>
146 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
147 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
148 _BinaryPredicate __predicate
)
150 // Test for empty ranges
151 if (__first1
== __last1
|| __first2
== __last2
)
154 // Test for a pattern of length 1.
155 _ForwardIterator2
__p1(__first2
);
156 if (++__p1
== __last2
)
157 return std::__find_if(__first1
, __last1
,
158 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
161 _ForwardIterator1 __current
= __first1
;
166 std::__find_if(__first1
, __last1
,
167 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
169 if (__first1
== __last1
)
172 _ForwardIterator2 __p
= __p1
;
173 __current
= __first1
;
174 if (++__current
== __last1
)
177 while (__predicate(__current
, __p
))
179 if (++__p
== __last2
)
181 if (++__current
== __last1
)
192 * This is an helper function for search_n overloaded for forward iterators.
194 template<typename _ForwardIterator
, typename _Integer
,
195 typename _UnaryPredicate
>
198 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
199 _Integer __count
, _UnaryPredicate __unary_pred
,
200 std::forward_iterator_tag
)
202 __first
= std::__find_if(__first
, __last
, __unary_pred
);
203 while (__first
!= __last
)
205 typename iterator_traits
<_ForwardIterator
>::difference_type
207 _ForwardIterator __i
= __first
;
209 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
218 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
224 * This is an helper function for search_n overloaded for random access
227 template<typename _RandomAccessIter
, typename _Integer
,
228 typename _UnaryPredicate
>
231 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
232 _Integer __count
, _UnaryPredicate __unary_pred
,
233 std::random_access_iterator_tag
)
235 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
238 _DistanceType __tailSize
= __last
- __first
;
239 _DistanceType __remainder
= __count
;
241 while (__remainder
<= __tailSize
) // the main loop...
243 __first
+= __remainder
;
244 __tailSize
-= __remainder
;
245 // __first here is always pointing to one past the last element of
246 // next possible match.
247 _RandomAccessIter __backTrack
= __first
;
248 while (__unary_pred(--__backTrack
))
250 if (--__remainder
== 0)
251 return (__first
- __count
); // Success
253 __remainder
= __count
+ 1 - (__first
- __backTrack
);
255 return __last
; // Failure
258 template<typename _ForwardIterator
, typename _Integer
,
259 typename _UnaryPredicate
>
262 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
264 _UnaryPredicate __unary_pred
)
270 return std::__find_if(__first
, __last
, __unary_pred
);
272 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
273 std::__iterator_category(__first
));
276 // find_end for forward iterators.
277 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
278 typename _BinaryPredicate
>
281 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
282 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
283 forward_iterator_tag
, forward_iterator_tag
,
284 _BinaryPredicate __comp
)
286 if (__first2
== __last2
)
289 _ForwardIterator1 __result
= __last1
;
292 _ForwardIterator1 __new_result
293 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
294 if (__new_result
== __last1
)
298 __result
= __new_result
;
299 __first1
= __new_result
;
305 // find_end for bidirectional iterators (much faster).
306 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
307 typename _BinaryPredicate
>
309 _BidirectionalIterator1
310 __find_end(_BidirectionalIterator1 __first1
,
311 _BidirectionalIterator1 __last1
,
312 _BidirectionalIterator2 __first2
,
313 _BidirectionalIterator2 __last2
,
314 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
315 _BinaryPredicate __comp
)
317 // concept requirements
318 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
319 _BidirectionalIterator1
>)
320 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
321 _BidirectionalIterator2
>)
323 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
324 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
326 _RevIterator1
__rlast1(__first1
);
327 _RevIterator2
__rlast2(__first2
);
328 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
329 _RevIterator2(__last2
), __rlast2
,
332 if (__rresult
== __rlast1
)
336 _BidirectionalIterator1 __result
= __rresult
.base();
337 std::advance(__result
, -std::distance(__first2
, __last2
));
343 * @brief Find last matching subsequence in a sequence.
344 * @ingroup non_mutating_algorithms
345 * @param __first1 Start of range to search.
346 * @param __last1 End of range to search.
347 * @param __first2 Start of sequence to match.
348 * @param __last2 End of sequence to match.
349 * @return The last iterator @c i in the range
350 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
351 * @p *(__first2+N) for each @c N in the range @p
352 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
354 * Searches the range @p [__first1,__last1) for a sub-sequence that
355 * compares equal value-by-value with the sequence given by @p
356 * [__first2,__last2) and returns an iterator to the __first
357 * element of the sub-sequence, or @p __last1 if the sub-sequence
358 * is not found. The sub-sequence will be the last such
359 * subsequence contained in [__first1,__last1).
361 * Because the sub-sequence must lie completely within the range @p
362 * [__first1,__last1) it must start at a position less than @p
363 * __last1-(__last2-__first2) where @p __last2-__first2 is the
364 * length of the sub-sequence. This means that the returned
365 * iterator @c i will be in the range @p
366 * [__first1,__last1-(__last2-__first2))
368 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
370 inline _ForwardIterator1
371 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
372 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
374 // concept requirements
375 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
376 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
377 __glibcxx_function_requires(_EqualOpConcept
<
378 typename iterator_traits
<_ForwardIterator1
>::value_type
,
379 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
380 __glibcxx_requires_valid_range(__first1
, __last1
);
381 __glibcxx_requires_valid_range(__first2
, __last2
);
383 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
384 std::__iterator_category(__first1
),
385 std::__iterator_category(__first2
),
386 __gnu_cxx::__ops::__iter_equal_to_iter());
390 * @brief Find last matching subsequence in a sequence using a predicate.
391 * @ingroup non_mutating_algorithms
392 * @param __first1 Start of range to search.
393 * @param __last1 End of range to search.
394 * @param __first2 Start of sequence to match.
395 * @param __last2 End of sequence to match.
396 * @param __comp The predicate to use.
397 * @return The last iterator @c i in the range @p
398 * [__first1,__last1-(__last2-__first2)) such that @c
399 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
400 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
403 * Searches the range @p [__first1,__last1) for a sub-sequence that
404 * compares equal value-by-value with the sequence given by @p
405 * [__first2,__last2) using comp as a predicate and returns an
406 * iterator to the first element of the sub-sequence, or @p __last1
407 * if the sub-sequence is not found. The sub-sequence will be the
408 * last such subsequence contained in [__first,__last1).
410 * Because the sub-sequence must lie completely within the range @p
411 * [__first1,__last1) it must start at a position less than @p
412 * __last1-(__last2-__first2) where @p __last2-__first2 is the
413 * length of the sub-sequence. This means that the returned
414 * iterator @c i will be in the range @p
415 * [__first1,__last1-(__last2-__first2))
417 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
418 typename _BinaryPredicate
>
420 inline _ForwardIterator1
421 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
422 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
423 _BinaryPredicate __comp
)
425 // concept requirements
426 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
427 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
428 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
429 typename iterator_traits
<_ForwardIterator1
>::value_type
,
430 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
431 __glibcxx_requires_valid_range(__first1
, __last1
);
432 __glibcxx_requires_valid_range(__first2
, __last2
);
434 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
435 std::__iterator_category(__first1
),
436 std::__iterator_category(__first2
),
437 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
440 #if __cplusplus >= 201103L
442 * @brief Checks that a predicate is true for all the elements
444 * @ingroup non_mutating_algorithms
445 * @param __first An input iterator.
446 * @param __last An input iterator.
447 * @param __pred A predicate.
448 * @return True if the check is true, false otherwise.
450 * Returns true if @p __pred is true for each element in the range
451 * @p [__first,__last), and false otherwise.
453 template<typename _InputIterator
, typename _Predicate
>
456 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
457 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
460 * @brief Checks that a predicate is false for all the elements
462 * @ingroup non_mutating_algorithms
463 * @param __first An input iterator.
464 * @param __last An input iterator.
465 * @param __pred A predicate.
466 * @return True if the check is true, false otherwise.
468 * Returns true if @p __pred is false for each element in the range
469 * @p [__first,__last), and false otherwise.
471 template<typename _InputIterator
, typename _Predicate
>
474 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
475 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
478 * @brief Checks that a predicate is true for at least one element
480 * @ingroup non_mutating_algorithms
481 * @param __first An input iterator.
482 * @param __last An input iterator.
483 * @param __pred A predicate.
484 * @return True if the check is true, false otherwise.
486 * Returns true if an element exists in the range @p
487 * [__first,__last) such that @p __pred is true, and false
490 template<typename _InputIterator
, typename _Predicate
>
493 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
494 { return !std::none_of(__first
, __last
, __pred
); }
497 * @brief Find the first element in a sequence for which a
498 * predicate is false.
499 * @ingroup non_mutating_algorithms
500 * @param __first An input iterator.
501 * @param __last An input iterator.
502 * @param __pred A predicate.
503 * @return The first iterator @c i in the range @p [__first,__last)
504 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
506 template<typename _InputIterator
, typename _Predicate
>
508 inline _InputIterator
509 find_if_not(_InputIterator __first
, _InputIterator __last
,
512 // concept requirements
513 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
514 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
515 typename iterator_traits
<_InputIterator
>::value_type
>)
516 __glibcxx_requires_valid_range(__first
, __last
);
517 return std::__find_if_not(__first
, __last
,
518 __gnu_cxx::__ops::__pred_iter(__pred
));
522 * @brief Checks whether the sequence is partitioned.
523 * @ingroup mutating_algorithms
524 * @param __first An input iterator.
525 * @param __last An input iterator.
526 * @param __pred A predicate.
527 * @return True if the range @p [__first,__last) is partioned by @p __pred,
528 * i.e. if all elements that satisfy @p __pred appear before those that
531 template<typename _InputIterator
, typename _Predicate
>
534 is_partitioned(_InputIterator __first
, _InputIterator __last
,
537 __first
= std::find_if_not(__first
, __last
, __pred
);
538 if (__first
== __last
)
541 return std::none_of(__first
, __last
, __pred
);
545 * @brief Find the partition point of a partitioned range.
546 * @ingroup mutating_algorithms
547 * @param __first An iterator.
548 * @param __last Another iterator.
549 * @param __pred A predicate.
550 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
551 * and @p none_of(mid, __last, __pred) are both true.
553 template<typename _ForwardIterator
, typename _Predicate
>
556 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
559 // concept requirements
560 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
561 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
562 typename iterator_traits
<_ForwardIterator
>::value_type
>)
564 // A specific debug-mode test will be necessary...
565 __glibcxx_requires_valid_range(__first
, __last
);
567 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
570 _DistanceType __len
= std::distance(__first
, __last
);
574 _DistanceType __half
= __len
>> 1;
575 _ForwardIterator __middle
= __first
;
576 std::advance(__middle
, __half
);
577 if (__pred(*__middle
))
581 __len
= __len
- __half
- 1;
590 template<typename _InputIterator
, typename _OutputIterator
,
594 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
595 _OutputIterator __result
, _Predicate __pred
)
597 for (; __first
!= __last
; ++__first
)
598 if (!__pred(__first
))
600 *__result
= *__first
;
607 * @brief Copy a sequence, removing elements of a given value.
608 * @ingroup mutating_algorithms
609 * @param __first An input iterator.
610 * @param __last An input iterator.
611 * @param __result An output iterator.
612 * @param __value The value to be removed.
613 * @return An iterator designating the end of the resulting sequence.
615 * Copies each element in the range @p [__first,__last) not equal
616 * to @p __value to the range beginning at @p __result.
617 * remove_copy() is stable, so the relative order of elements that
618 * are copied is unchanged.
620 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
622 inline _OutputIterator
623 remove_copy(_InputIterator __first
, _InputIterator __last
,
624 _OutputIterator __result
, const _Tp
& __value
)
626 // concept requirements
627 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
628 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
629 typename iterator_traits
<_InputIterator
>::value_type
>)
630 __glibcxx_function_requires(_EqualOpConcept
<
631 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
632 __glibcxx_requires_valid_range(__first
, __last
);
634 return std::__remove_copy_if(__first
, __last
, __result
,
635 __gnu_cxx::__ops::__iter_equals_val(__value
));
639 * @brief Copy a sequence, removing elements for which a predicate is true.
640 * @ingroup mutating_algorithms
641 * @param __first An input iterator.
642 * @param __last An input iterator.
643 * @param __result An output iterator.
644 * @param __pred A predicate.
645 * @return An iterator designating the end of the resulting sequence.
647 * Copies each element in the range @p [__first,__last) for which
648 * @p __pred returns false to the range beginning at @p __result.
650 * remove_copy_if() is stable, so the relative order of elements that are
651 * copied is unchanged.
653 template<typename _InputIterator
, typename _OutputIterator
,
656 inline _OutputIterator
657 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
658 _OutputIterator __result
, _Predicate __pred
)
660 // concept requirements
661 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
662 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
663 typename iterator_traits
<_InputIterator
>::value_type
>)
664 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
665 typename iterator_traits
<_InputIterator
>::value_type
>)
666 __glibcxx_requires_valid_range(__first
, __last
);
668 return std::__remove_copy_if(__first
, __last
, __result
,
669 __gnu_cxx::__ops::__pred_iter(__pred
));
672 #if __cplusplus >= 201103L
674 * @brief Copy the elements of a sequence for which a predicate is true.
675 * @ingroup mutating_algorithms
676 * @param __first An input iterator.
677 * @param __last An input iterator.
678 * @param __result An output iterator.
679 * @param __pred A predicate.
680 * @return An iterator designating the end of the resulting sequence.
682 * Copies each element in the range @p [__first,__last) for which
683 * @p __pred returns true to the range beginning at @p __result.
685 * copy_if() is stable, so the relative order of elements that are
686 * copied is unchanged.
688 template<typename _InputIterator
, typename _OutputIterator
,
692 copy_if(_InputIterator __first
, _InputIterator __last
,
693 _OutputIterator __result
, _Predicate __pred
)
695 // concept requirements
696 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
697 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
698 typename iterator_traits
<_InputIterator
>::value_type
>)
699 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
700 typename iterator_traits
<_InputIterator
>::value_type
>)
701 __glibcxx_requires_valid_range(__first
, __last
);
703 for (; __first
!= __last
; ++__first
)
704 if (__pred(*__first
))
706 *__result
= *__first
;
712 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
715 __copy_n(_InputIterator __first
, _Size __n
,
716 _OutputIterator __result
, input_iterator_tag
)
718 return std::__niter_wrap(__result
,
719 __copy_n_a(__first
, __n
,
720 std::__niter_base(__result
), true));
723 template<typename _RandomAccessIterator
, typename _Size
,
724 typename _OutputIterator
>
726 inline _OutputIterator
727 __copy_n(_RandomAccessIterator __first
, _Size __n
,
728 _OutputIterator __result
, random_access_iterator_tag
)
729 { return std::copy(__first
, __first
+ __n
, __result
); }
732 * @brief Copies the range [first,first+n) into [result,result+n).
733 * @ingroup mutating_algorithms
734 * @param __first An input iterator.
735 * @param __n The number of elements to copy.
736 * @param __result An output iterator.
739 * This inline function will boil down to a call to @c memmove whenever
740 * possible. Failing that, if random access iterators are passed, then the
741 * loop count will be known (and therefore a candidate for compiler
742 * optimizations such as unrolling).
744 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
746 inline _OutputIterator
747 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
749 // concept requirements
750 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
751 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
752 typename iterator_traits
<_InputIterator
>::value_type
>)
754 const auto __n2
= std::__size_to_integer(__n
);
758 __glibcxx_requires_can_increment(__first
, __n2
);
759 __glibcxx_requires_can_increment(__result
, __n2
);
761 return std::__copy_n(__first
, __n2
, __result
,
762 std::__iterator_category(__first
));
766 * @brief Copy the elements of a sequence to separate output sequences
767 * depending on the truth value of a predicate.
768 * @ingroup mutating_algorithms
769 * @param __first An input iterator.
770 * @param __last An input iterator.
771 * @param __out_true An output iterator.
772 * @param __out_false An output iterator.
773 * @param __pred A predicate.
774 * @return A pair designating the ends of the resulting sequences.
776 * Copies each element in the range @p [__first,__last) for which
777 * @p __pred returns true to the range beginning at @p out_true
778 * and each element for which @p __pred returns false to @p __out_false.
780 template<typename _InputIterator
, typename _OutputIterator1
,
781 typename _OutputIterator2
, typename _Predicate
>
783 pair
<_OutputIterator1
, _OutputIterator2
>
784 partition_copy(_InputIterator __first
, _InputIterator __last
,
785 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
788 // concept requirements
789 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
790 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
791 typename iterator_traits
<_InputIterator
>::value_type
>)
792 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
793 typename iterator_traits
<_InputIterator
>::value_type
>)
794 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
795 typename iterator_traits
<_InputIterator
>::value_type
>)
796 __glibcxx_requires_valid_range(__first
, __last
);
798 for (; __first
!= __last
; ++__first
)
799 if (__pred(*__first
))
801 *__out_true
= *__first
;
806 *__out_false
= *__first
;
810 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
815 * @brief Remove elements from a sequence.
816 * @ingroup mutating_algorithms
817 * @param __first An input iterator.
818 * @param __last An input iterator.
819 * @param __value The value to be removed.
820 * @return An iterator designating the end of the resulting sequence.
822 * All elements equal to @p __value are removed from the range
823 * @p [__first,__last).
825 * remove() is stable, so the relative order of elements that are
826 * not removed is unchanged.
828 * Elements between the end of the resulting sequence and @p __last
829 * are still present, but their value is unspecified.
831 template<typename _ForwardIterator
, typename _Tp
>
833 inline _ForwardIterator
834 remove(_ForwardIterator __first
, _ForwardIterator __last
,
837 // concept requirements
838 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
840 __glibcxx_function_requires(_EqualOpConcept
<
841 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
842 __glibcxx_requires_valid_range(__first
, __last
);
844 return std::__remove_if(__first
, __last
,
845 __gnu_cxx::__ops::__iter_equals_val(__value
));
849 * @brief Remove elements from a sequence using a predicate.
850 * @ingroup mutating_algorithms
851 * @param __first A forward iterator.
852 * @param __last A forward iterator.
853 * @param __pred A predicate.
854 * @return An iterator designating the end of the resulting sequence.
856 * All elements for which @p __pred returns true are removed from the range
857 * @p [__first,__last).
859 * remove_if() is stable, so the relative order of elements that are
860 * not removed is unchanged.
862 * Elements between the end of the resulting sequence and @p __last
863 * are still present, but their value is unspecified.
865 template<typename _ForwardIterator
, typename _Predicate
>
867 inline _ForwardIterator
868 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
871 // concept requirements
872 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
874 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
875 typename iterator_traits
<_ForwardIterator
>::value_type
>)
876 __glibcxx_requires_valid_range(__first
, __last
);
878 return std::__remove_if(__first
, __last
,
879 __gnu_cxx::__ops::__pred_iter(__pred
));
882 template<typename _ForwardIterator
, typename _BinaryPredicate
>
885 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
886 _BinaryPredicate __binary_pred
)
888 if (__first
== __last
)
890 _ForwardIterator __next
= __first
;
891 while (++__next
!= __last
)
893 if (__binary_pred(__first
, __next
))
900 template<typename _ForwardIterator
, typename _BinaryPredicate
>
903 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
904 _BinaryPredicate __binary_pred
)
906 // Skip the beginning, if already unique.
907 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
908 if (__first
== __last
)
911 // Do the real copy work.
912 _ForwardIterator __dest
= __first
;
914 while (++__first
!= __last
)
915 if (!__binary_pred(__dest
, __first
))
916 *++__dest
= _GLIBCXX_MOVE(*__first
);
921 * @brief Remove consecutive duplicate values from a sequence.
922 * @ingroup mutating_algorithms
923 * @param __first A forward iterator.
924 * @param __last A forward iterator.
925 * @return An iterator designating the end of the resulting sequence.
927 * Removes all but the first element from each group of consecutive
928 * values that compare equal.
929 * unique() is stable, so the relative order of elements that are
930 * not removed is unchanged.
931 * Elements between the end of the resulting sequence and @p __last
932 * are still present, but their value is unspecified.
934 template<typename _ForwardIterator
>
936 inline _ForwardIterator
937 unique(_ForwardIterator __first
, _ForwardIterator __last
)
939 // concept requirements
940 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
942 __glibcxx_function_requires(_EqualityComparableConcept
<
943 typename iterator_traits
<_ForwardIterator
>::value_type
>)
944 __glibcxx_requires_valid_range(__first
, __last
);
946 return std::__unique(__first
, __last
,
947 __gnu_cxx::__ops::__iter_equal_to_iter());
951 * @brief Remove consecutive values from a sequence using a predicate.
952 * @ingroup mutating_algorithms
953 * @param __first A forward iterator.
954 * @param __last A forward iterator.
955 * @param __binary_pred A binary predicate.
956 * @return An iterator designating the end of the resulting sequence.
958 * Removes all but the first element from each group of consecutive
959 * values for which @p __binary_pred returns true.
960 * unique() is stable, so the relative order of elements that are
961 * not removed is unchanged.
962 * Elements between the end of the resulting sequence and @p __last
963 * are still present, but their value is unspecified.
965 template<typename _ForwardIterator
, typename _BinaryPredicate
>
967 inline _ForwardIterator
968 unique(_ForwardIterator __first
, _ForwardIterator __last
,
969 _BinaryPredicate __binary_pred
)
971 // concept requirements
972 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
974 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
975 typename iterator_traits
<_ForwardIterator
>::value_type
,
976 typename iterator_traits
<_ForwardIterator
>::value_type
>)
977 __glibcxx_requires_valid_range(__first
, __last
);
979 return std::__unique(__first
, __last
,
980 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
984 * This is an uglified
985 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
987 * overloaded for forward iterators and output iterator as result.
989 template<typename _ForwardIterator
, typename _OutputIterator
,
990 typename _BinaryPredicate
>
993 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
994 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
995 forward_iterator_tag
, output_iterator_tag
)
997 // concept requirements -- iterators already checked
998 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
999 typename iterator_traits
<_ForwardIterator
>::value_type
,
1000 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1002 _ForwardIterator __next
= __first
;
1003 *__result
= *__first
;
1004 while (++__next
!= __last
)
1005 if (!__binary_pred(__first
, __next
))
1008 *++__result
= *__first
;
1014 * This is an uglified
1015 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1017 * overloaded for input iterators and output iterator as result.
1019 template<typename _InputIterator
, typename _OutputIterator
,
1020 typename _BinaryPredicate
>
1021 _GLIBCXX20_CONSTEXPR
1023 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1024 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1025 input_iterator_tag
, output_iterator_tag
)
1027 // concept requirements -- iterators already checked
1028 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1029 typename iterator_traits
<_InputIterator
>::value_type
,
1030 typename iterator_traits
<_InputIterator
>::value_type
>)
1032 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1033 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1035 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1036 *__result
= __value
;
1037 while (++__first
!= __last
)
1038 if (!__rebound_pred(__first
, __value
))
1041 *++__result
= __value
;
1047 * This is an uglified
1048 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1050 * overloaded for input iterators and forward iterator as result.
1052 template<typename _InputIterator
, typename _ForwardIterator
,
1053 typename _BinaryPredicate
>
1054 _GLIBCXX20_CONSTEXPR
1056 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1057 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1058 input_iterator_tag
, forward_iterator_tag
)
1060 // concept requirements -- iterators already checked
1061 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1062 typename iterator_traits
<_ForwardIterator
>::value_type
,
1063 typename iterator_traits
<_InputIterator
>::value_type
>)
1064 *__result
= *__first
;
1065 while (++__first
!= __last
)
1066 if (!__binary_pred(__result
, __first
))
1067 *++__result
= *__first
;
1072 * This is an uglified reverse(_BidirectionalIterator,
1073 * _BidirectionalIterator)
1074 * overloaded for bidirectional iterators.
1076 template<typename _BidirectionalIterator
>
1077 _GLIBCXX20_CONSTEXPR
1079 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1080 bidirectional_iterator_tag
)
1083 if (__first
== __last
|| __first
== --__last
)
1087 std::iter_swap(__first
, __last
);
1093 * This is an uglified reverse(_BidirectionalIterator,
1094 * _BidirectionalIterator)
1095 * overloaded for random access iterators.
1097 template<typename _RandomAccessIterator
>
1098 _GLIBCXX20_CONSTEXPR
1100 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1101 random_access_iterator_tag
)
1103 if (__first
== __last
)
1106 while (__first
< __last
)
1108 std::iter_swap(__first
, __last
);
1115 * @brief Reverse a sequence.
1116 * @ingroup mutating_algorithms
1117 * @param __first A bidirectional iterator.
1118 * @param __last A bidirectional iterator.
1119 * @return reverse() returns no value.
1121 * Reverses the order of the elements in the range @p [__first,__last),
1122 * so that the first element becomes the last etc.
1123 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1124 * swaps @p *(__first+i) and @p *(__last-(i+1))
1126 template<typename _BidirectionalIterator
>
1127 _GLIBCXX20_CONSTEXPR
1129 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1131 // concept requirements
1132 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1133 _BidirectionalIterator
>)
1134 __glibcxx_requires_valid_range(__first
, __last
);
1135 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1139 * @brief Copy a sequence, reversing its elements.
1140 * @ingroup mutating_algorithms
1141 * @param __first A bidirectional iterator.
1142 * @param __last A bidirectional iterator.
1143 * @param __result An output iterator.
1144 * @return An iterator designating the end of the resulting sequence.
1146 * Copies the elements in the range @p [__first,__last) to the
1147 * range @p [__result,__result+(__last-__first)) such that the
1148 * order of the elements is reversed. For every @c i such that @p
1149 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1150 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1151 * The ranges @p [__first,__last) and @p
1152 * [__result,__result+(__last-__first)) must not overlap.
1154 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1155 _GLIBCXX20_CONSTEXPR
1157 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1158 _OutputIterator __result
)
1160 // concept requirements
1161 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1162 _BidirectionalIterator
>)
1163 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1164 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1165 __glibcxx_requires_valid_range(__first
, __last
);
1167 while (__first
!= __last
)
1170 *__result
= *__last
;
1177 * This is a helper function for the rotate algorithm specialized on RAIs.
1178 * It returns the greatest common divisor of two integer values.
1180 template<typename _EuclideanRingElement
>
1181 _GLIBCXX20_CONSTEXPR
1182 _EuclideanRingElement
1183 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1187 _EuclideanRingElement __t
= __m
% __n
;
1194 _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2
)
1196 /// This is a helper function for the rotate algorithm.
1197 template<typename _ForwardIterator
>
1198 _GLIBCXX20_CONSTEXPR
1200 __rotate(_ForwardIterator __first
,
1201 _ForwardIterator __middle
,
1202 _ForwardIterator __last
,
1203 forward_iterator_tag
)
1205 if (__first
== __middle
)
1207 else if (__last
== __middle
)
1210 _ForwardIterator __first2
= __middle
;
1213 std::iter_swap(__first
, __first2
);
1216 if (__first
== __middle
)
1217 __middle
= __first2
;
1219 while (__first2
!= __last
);
1221 _ForwardIterator __ret
= __first
;
1223 __first2
= __middle
;
1225 while (__first2
!= __last
)
1227 std::iter_swap(__first
, __first2
);
1230 if (__first
== __middle
)
1231 __middle
= __first2
;
1232 else if (__first2
== __last
)
1233 __first2
= __middle
;
1238 /// This is a helper function for the rotate algorithm.
1239 template<typename _BidirectionalIterator
>
1240 _GLIBCXX20_CONSTEXPR
1241 _BidirectionalIterator
1242 __rotate(_BidirectionalIterator __first
,
1243 _BidirectionalIterator __middle
,
1244 _BidirectionalIterator __last
,
1245 bidirectional_iterator_tag
)
1247 // concept requirements
1248 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1249 _BidirectionalIterator
>)
1251 if (__first
== __middle
)
1253 else if (__last
== __middle
)
1256 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1257 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1259 while (__first
!= __middle
&& __middle
!= __last
)
1261 std::iter_swap(__first
, --__last
);
1265 if (__first
== __middle
)
1267 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1272 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1277 /// This is a helper function for the rotate algorithm.
1278 template<typename _RandomAccessIterator
>
1279 _GLIBCXX20_CONSTEXPR
1280 _RandomAccessIterator
1281 __rotate(_RandomAccessIterator __first
,
1282 _RandomAccessIterator __middle
,
1283 _RandomAccessIterator __last
,
1284 random_access_iterator_tag
)
1286 // concept requirements
1287 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1288 _RandomAccessIterator
>)
1290 if (__first
== __middle
)
1292 else if (__last
== __middle
)
1295 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1297 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1300 _Distance __n
= __last
- __first
;
1301 _Distance __k
= __middle
- __first
;
1303 if (__k
== __n
- __k
)
1305 std::swap_ranges(__first
, __middle
, __middle
);
1309 _RandomAccessIterator __p
= __first
;
1310 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1314 if (__k
< __n
- __k
)
1316 if (__is_pod(_ValueType
) && __k
== 1)
1318 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1319 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1320 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1323 _RandomAccessIterator __q
= __p
+ __k
;
1324 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1326 std::iter_swap(__p
, __q
);
1333 std::swap(__n
, __k
);
1339 if (__is_pod(_ValueType
) && __k
== 1)
1341 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1342 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1343 *__p
= _GLIBCXX_MOVE(__t
);
1346 _RandomAccessIterator __q
= __p
+ __n
;
1348 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1352 std::iter_swap(__p
, __q
);
1357 std::swap(__n
, __k
);
1362 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1363 // DR 488. rotate throws away useful information
1365 * @brief Rotate the elements of a sequence.
1366 * @ingroup mutating_algorithms
1367 * @param __first A forward iterator.
1368 * @param __middle A forward iterator.
1369 * @param __last A forward iterator.
1370 * @return first + (last - middle).
1372 * Rotates the elements of the range @p [__first,__last) by
1373 * @p (__middle - __first) positions so that the element at @p __middle
1374 * is moved to @p __first, the element at @p __middle+1 is moved to
1375 * @p __first+1 and so on for each element in the range
1376 * @p [__first,__last).
1378 * This effectively swaps the ranges @p [__first,__middle) and
1379 * @p [__middle,__last).
1382 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1383 * for each @p n in the range @p [0,__last-__first).
1385 template<typename _ForwardIterator
>
1386 _GLIBCXX20_CONSTEXPR
1387 inline _ForwardIterator
1388 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1389 _ForwardIterator __last
)
1391 // concept requirements
1392 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1394 __glibcxx_requires_valid_range(__first
, __middle
);
1395 __glibcxx_requires_valid_range(__middle
, __last
);
1397 return std::__rotate(__first
, __middle
, __last
,
1398 std::__iterator_category(__first
));
1401 _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2
)
1404 * @brief Copy a sequence, rotating its elements.
1405 * @ingroup mutating_algorithms
1406 * @param __first A forward iterator.
1407 * @param __middle A forward iterator.
1408 * @param __last A forward iterator.
1409 * @param __result An output iterator.
1410 * @return An iterator designating the end of the resulting sequence.
1412 * Copies the elements of the range @p [__first,__last) to the
1413 * range beginning at @result, rotating the copied elements by
1414 * @p (__middle-__first) positions so that the element at @p __middle
1415 * is moved to @p __result, the element at @p __middle+1 is moved
1416 * to @p __result+1 and so on for each element in the range @p
1420 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1421 * for each @p n in the range @p [0,__last-__first).
1423 template<typename _ForwardIterator
, typename _OutputIterator
>
1424 _GLIBCXX20_CONSTEXPR
1425 inline _OutputIterator
1426 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1427 _ForwardIterator __last
, _OutputIterator __result
)
1429 // concept requirements
1430 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1431 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1432 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1433 __glibcxx_requires_valid_range(__first
, __middle
);
1434 __glibcxx_requires_valid_range(__middle
, __last
);
1436 return std::copy(__first
, __middle
,
1437 std::copy(__middle
, __last
, __result
));
1440 /// This is a helper function...
1441 template<typename _ForwardIterator
, typename _Predicate
>
1442 _GLIBCXX20_CONSTEXPR
1444 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1445 _Predicate __pred
, forward_iterator_tag
)
1447 if (__first
== __last
)
1450 while (__pred(*__first
))
1451 if (++__first
== __last
)
1454 _ForwardIterator __next
= __first
;
1456 while (++__next
!= __last
)
1457 if (__pred(*__next
))
1459 std::iter_swap(__first
, __next
);
1466 /// This is a helper function...
1467 template<typename _BidirectionalIterator
, typename _Predicate
>
1468 _GLIBCXX20_CONSTEXPR
1469 _BidirectionalIterator
1470 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1471 _Predicate __pred
, bidirectional_iterator_tag
)
1476 if (__first
== __last
)
1478 else if (__pred(*__first
))
1484 if (__first
== __last
)
1486 else if (!bool(__pred(*__last
)))
1490 std::iter_swap(__first
, __last
);
1497 /// This is a helper function...
1498 /// Requires __first != __last and !__pred(__first)
1499 /// and __len == distance(__first, __last).
1501 /// !__pred(__first) allows us to guarantee that we don't
1502 /// move-assign an element onto itself.
1503 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1506 __stable_partition_adaptive(_ForwardIterator __first
,
1507 _ForwardIterator __last
,
1508 _Predicate __pred
, _Distance __len
,
1510 _Distance __buffer_size
)
1515 if (__len
<= __buffer_size
)
1517 _ForwardIterator __result1
= __first
;
1518 _Pointer __result2
= __buffer
;
1520 // The precondition guarantees that !__pred(__first), so
1521 // move that element to the buffer before starting the loop.
1522 // This ensures that we only call __pred once per element.
1523 *__result2
= _GLIBCXX_MOVE(*__first
);
1526 for (; __first
!= __last
; ++__first
)
1527 if (__pred(__first
))
1529 *__result1
= _GLIBCXX_MOVE(*__first
);
1534 *__result2
= _GLIBCXX_MOVE(*__first
);
1538 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1542 _ForwardIterator __middle
= __first
;
1543 std::advance(__middle
, __len
/ 2);
1544 _ForwardIterator __left_split
=
1545 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1546 __len
/ 2, __buffer
,
1549 // Advance past true-predicate values to satisfy this
1550 // function's preconditions.
1551 _Distance __right_len
= __len
- __len
/ 2;
1552 _ForwardIterator __right_split
=
1553 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1557 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1559 __buffer
, __buffer_size
);
1561 return std::rotate(__left_split
, __middle
, __right_split
);
1564 template<typename _ForwardIterator
, typename _Predicate
>
1566 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1569 __first
= std::__find_if_not(__first
, __last
, __pred
);
1571 if (__first
== __last
)
1574 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1576 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1579 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1580 __buf(__first
, std::distance(__first
, __last
));
1582 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1583 _DistanceType(__buf
.requested_size()),
1585 _DistanceType(__buf
.size()));
1589 * @brief Move elements for which a predicate is true to the beginning
1590 * of a sequence, preserving relative ordering.
1591 * @ingroup mutating_algorithms
1592 * @param __first A forward iterator.
1593 * @param __last A forward iterator.
1594 * @param __pred A predicate functor.
1595 * @return An iterator @p middle such that @p __pred(i) is true for each
1596 * iterator @p i in the range @p [first,middle) and false for each @p i
1597 * in the range @p [middle,last).
1599 * Performs the same function as @p partition() with the additional
1600 * guarantee that the relative ordering of elements in each group is
1601 * preserved, so any two elements @p x and @p y in the range
1602 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1603 * relative ordering after calling @p stable_partition().
1605 template<typename _ForwardIterator
, typename _Predicate
>
1606 inline _ForwardIterator
1607 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1610 // concept requirements
1611 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1613 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1614 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1615 __glibcxx_requires_valid_range(__first
, __last
);
1617 return std::__stable_partition(__first
, __last
,
1618 __gnu_cxx::__ops::__pred_iter(__pred
));
1621 /// @cond undocumented
1623 /// This is a helper function for the sort routines.
1624 template<typename _RandomAccessIterator
, typename _Compare
>
1625 _GLIBCXX20_CONSTEXPR
1627 __heap_select(_RandomAccessIterator __first
,
1628 _RandomAccessIterator __middle
,
1629 _RandomAccessIterator __last
, _Compare __comp
)
1631 std::__make_heap(__first
, __middle
, __comp
);
1632 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1633 if (__comp(__i
, __first
))
1634 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1639 template<typename _InputIterator
, typename _RandomAccessIterator
,
1641 _GLIBCXX20_CONSTEXPR
1642 _RandomAccessIterator
1643 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1644 _RandomAccessIterator __result_first
,
1645 _RandomAccessIterator __result_last
,
1648 typedef typename iterator_traits
<_InputIterator
>::value_type
1650 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1651 typedef typename
_RItTraits::difference_type _DistanceType
;
1653 if (__result_first
== __result_last
)
1654 return __result_last
;
1655 _RandomAccessIterator __result_real_last
= __result_first
;
1656 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1658 *__result_real_last
= *__first
;
1659 ++__result_real_last
;
1663 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1664 while (__first
!= __last
)
1666 if (__comp(__first
, __result_first
))
1667 std::__adjust_heap(__result_first
, _DistanceType(0),
1668 _DistanceType(__result_real_last
1670 _InputValueType(*__first
), __comp
);
1673 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1674 return __result_real_last
;
1680 * @brief Copy the smallest elements of a sequence.
1681 * @ingroup sorting_algorithms
1682 * @param __first An iterator.
1683 * @param __last Another iterator.
1684 * @param __result_first A random-access iterator.
1685 * @param __result_last Another random-access iterator.
1686 * @return An iterator indicating the end of the resulting sequence.
1688 * Copies and sorts the smallest `N` values from the range
1689 * `[__first, __last)` to the range beginning at `__result_first`, where
1690 * the number of elements to be copied, `N`, is the smaller of
1691 * `(__last - __first)` and `(__result_last - __result_first)`.
1692 * After the sort if `i` and `j` are iterators in the range
1693 * `[__result_first,__result_first + N)` such that `i` precedes `j` then
1694 * `*j < *i` is false.
1695 * The value returned is `__result_first + N`.
1697 template<typename _InputIterator
, typename _RandomAccessIterator
>
1698 _GLIBCXX20_CONSTEXPR
1699 inline _RandomAccessIterator
1700 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1701 _RandomAccessIterator __result_first
,
1702 _RandomAccessIterator __result_last
)
1704 #ifdef _GLIBCXX_CONCEPT_CHECKS
1705 typedef typename iterator_traits
<_InputIterator
>::value_type
1707 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1711 // concept requirements
1712 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1713 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1715 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1717 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1718 __glibcxx_requires_valid_range(__first
, __last
);
1719 __glibcxx_requires_irreflexive(__first
, __last
);
1720 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1722 return std::__partial_sort_copy(__first
, __last
,
1723 __result_first
, __result_last
,
1724 __gnu_cxx::__ops::__iter_less_iter());
1728 * @brief Copy the smallest elements of a sequence using a predicate for
1730 * @ingroup sorting_algorithms
1731 * @param __first An input iterator.
1732 * @param __last Another input iterator.
1733 * @param __result_first A random-access iterator.
1734 * @param __result_last Another random-access iterator.
1735 * @param __comp A comparison functor.
1736 * @return An iterator indicating the end of the resulting sequence.
1738 * Copies and sorts the smallest `N` values from the range
1739 * `[__first, __last)` to the range beginning at `result_first`, where
1740 * the number of elements to be copied, `N`, is the smaller of
1741 * `(__last - __first)` and `(__result_last - __result_first)`.
1742 * After the sort if `i` and `j` are iterators in the range
1743 * `[__result_first, __result_first + N)` such that `i` precedes `j` then
1744 * `__comp(*j, *i)` is false.
1745 * The value returned is `__result_first + N`.
1747 template<typename _InputIterator
, typename _RandomAccessIterator
,
1749 _GLIBCXX20_CONSTEXPR
1750 inline _RandomAccessIterator
1751 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1752 _RandomAccessIterator __result_first
,
1753 _RandomAccessIterator __result_last
,
1756 #ifdef _GLIBCXX_CONCEPT_CHECKS
1757 typedef typename iterator_traits
<_InputIterator
>::value_type
1759 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1763 // concept requirements
1764 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1765 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1766 _RandomAccessIterator
>)
1767 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1769 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1770 _InputValueType
, _OutputValueType
>)
1771 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1772 _OutputValueType
, _OutputValueType
>)
1773 __glibcxx_requires_valid_range(__first
, __last
);
1774 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1775 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1777 return std::__partial_sort_copy(__first
, __last
,
1778 __result_first
, __result_last
,
1779 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1782 /// @cond undocumented
1784 /// This is a helper function for the sort routine.
1785 template<typename _RandomAccessIterator
, typename _Compare
>
1786 _GLIBCXX20_CONSTEXPR
1788 __unguarded_linear_insert(_RandomAccessIterator __last
,
1791 typename iterator_traits
<_RandomAccessIterator
>::value_type
1792 __val
= _GLIBCXX_MOVE(*__last
);
1793 _RandomAccessIterator __next
= __last
;
1795 while (__comp(__val
, __next
))
1797 *__last
= _GLIBCXX_MOVE(*__next
);
1801 *__last
= _GLIBCXX_MOVE(__val
);
1804 /// This is a helper function for the sort routine.
1805 template<typename _RandomAccessIterator
, typename _Compare
>
1806 _GLIBCXX20_CONSTEXPR
1808 __insertion_sort(_RandomAccessIterator __first
,
1809 _RandomAccessIterator __last
, _Compare __comp
)
1811 if (__first
== __last
) return;
1813 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1815 if (__comp(__i
, __first
))
1817 typename iterator_traits
<_RandomAccessIterator
>::value_type
1818 __val
= _GLIBCXX_MOVE(*__i
);
1819 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1820 *__first
= _GLIBCXX_MOVE(__val
);
1823 std::__unguarded_linear_insert(__i
,
1824 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1828 /// This is a helper function for the sort routine.
1829 template<typename _RandomAccessIterator
, typename _Compare
>
1830 _GLIBCXX20_CONSTEXPR
1832 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1833 _RandomAccessIterator __last
, _Compare __comp
)
1835 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1836 std::__unguarded_linear_insert(__i
,
1837 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1842 * This controls some aspect of the sort routines.
1844 enum { _S_threshold
= 16 };
1846 /// This is a helper function for the sort routine.
1847 template<typename _RandomAccessIterator
, typename _Compare
>
1848 _GLIBCXX20_CONSTEXPR
1850 __final_insertion_sort(_RandomAccessIterator __first
,
1851 _RandomAccessIterator __last
, _Compare __comp
)
1853 if (__last
- __first
> int(_S_threshold
))
1855 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1856 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1860 std::__insertion_sort(__first
, __last
, __comp
);
1863 /// This is a helper function...
1864 template<typename _RandomAccessIterator
, typename _Compare
>
1865 _GLIBCXX20_CONSTEXPR
1866 _RandomAccessIterator
1867 __unguarded_partition(_RandomAccessIterator __first
,
1868 _RandomAccessIterator __last
,
1869 _RandomAccessIterator __pivot
, _Compare __comp
)
1873 while (__comp(__first
, __pivot
))
1876 while (__comp(__pivot
, __last
))
1878 if (!(__first
< __last
))
1880 std::iter_swap(__first
, __last
);
1885 /// This is a helper function...
1886 template<typename _RandomAccessIterator
, typename _Compare
>
1887 _GLIBCXX20_CONSTEXPR
1888 inline _RandomAccessIterator
1889 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1890 _RandomAccessIterator __last
, _Compare __comp
)
1892 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1893 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1895 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1898 template<typename _RandomAccessIterator
, typename _Compare
>
1899 _GLIBCXX20_CONSTEXPR
1901 __partial_sort(_RandomAccessIterator __first
,
1902 _RandomAccessIterator __middle
,
1903 _RandomAccessIterator __last
,
1906 std::__heap_select(__first
, __middle
, __last
, __comp
);
1907 std::__sort_heap(__first
, __middle
, __comp
);
1910 /// This is a helper function for the sort routine.
1911 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1912 _GLIBCXX20_CONSTEXPR
1914 __introsort_loop(_RandomAccessIterator __first
,
1915 _RandomAccessIterator __last
,
1916 _Size __depth_limit
, _Compare __comp
)
1918 while (__last
- __first
> int(_S_threshold
))
1920 if (__depth_limit
== 0)
1922 std::__partial_sort(__first
, __last
, __last
, __comp
);
1926 _RandomAccessIterator __cut
=
1927 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1928 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1935 template<typename _RandomAccessIterator
, typename _Compare
>
1936 _GLIBCXX20_CONSTEXPR
1938 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1941 if (__first
!= __last
)
1943 std::__introsort_loop(__first
, __last
,
1944 std::__lg(__last
- __first
) * 2,
1946 std::__final_insertion_sort(__first
, __last
, __comp
);
1950 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1951 _GLIBCXX20_CONSTEXPR
1953 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1954 _RandomAccessIterator __last
, _Size __depth_limit
,
1957 while (__last
- __first
> 3)
1959 if (__depth_limit
== 0)
1961 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1962 // Place the nth largest element in its final position.
1963 std::iter_swap(__first
, __nth
);
1967 _RandomAccessIterator __cut
=
1968 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1974 std::__insertion_sort(__first
, __last
, __comp
);
1981 // lower_bound moved to stl_algobase.h
1984 * @brief Finds the first position in which `__val` could be inserted
1985 * without changing the ordering.
1986 * @ingroup binary_search_algorithms
1987 * @param __first An iterator to the start of a sorted range.
1988 * @param __last A past-the-end iterator for the sorted range.
1989 * @param __val The search term.
1990 * @param __comp A functor to use for comparisons.
1991 * @return An iterator pointing to the first element _not less than_
1992 * `__val`, or `end()` if every element is less than `__val`.
1993 * @ingroup binary_search_algorithms
1995 * The comparison function should have the same effects on ordering as
1996 * the function used for the initial sort.
1998 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1999 _GLIBCXX20_CONSTEXPR
2000 inline _ForwardIterator
2001 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2002 const _Tp
& __val
, _Compare __comp
)
2004 // concept requirements
2005 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2006 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2007 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2008 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2011 return std::__lower_bound(__first
, __last
, __val
,
2012 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2015 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2016 _GLIBCXX20_CONSTEXPR
2018 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2019 const _Tp
& __val
, _Compare __comp
)
2021 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2024 _DistanceType __len
= std::distance(__first
, __last
);
2028 _DistanceType __half
= __len
>> 1;
2029 _ForwardIterator __middle
= __first
;
2030 std::advance(__middle
, __half
);
2031 if (__comp(__val
, __middle
))
2037 __len
= __len
- __half
- 1;
2044 * @brief Finds the last position in which @p __val could be inserted
2045 * without changing the ordering.
2046 * @ingroup binary_search_algorithms
2047 * @param __first An iterator.
2048 * @param __last Another iterator.
2049 * @param __val The search term.
2050 * @return An iterator pointing to the first element greater than @p __val,
2051 * or end() if no elements are greater than @p __val.
2052 * @ingroup binary_search_algorithms
2054 template<typename _ForwardIterator
, typename _Tp
>
2055 _GLIBCXX20_CONSTEXPR
2056 inline _ForwardIterator
2057 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2060 // concept requirements
2061 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2062 __glibcxx_function_requires(_LessThanOpConcept
<
2063 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2064 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2066 return std::__upper_bound(__first
, __last
, __val
,
2067 __gnu_cxx::__ops::__val_less_iter());
2071 * @brief Finds the last position in which @p __val could be inserted
2072 * without changing the ordering.
2073 * @ingroup binary_search_algorithms
2074 * @param __first An iterator.
2075 * @param __last Another iterator.
2076 * @param __val The search term.
2077 * @param __comp A functor to use for comparisons.
2078 * @return An iterator pointing to the first element greater than @p __val,
2079 * or end() if no elements are greater than @p __val.
2080 * @ingroup binary_search_algorithms
2082 * The comparison function should have the same effects on ordering as
2083 * the function used for the initial sort.
2085 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2086 _GLIBCXX20_CONSTEXPR
2087 inline _ForwardIterator
2088 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2089 const _Tp
& __val
, _Compare __comp
)
2091 // concept requirements
2092 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2093 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2094 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2095 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2098 return std::__upper_bound(__first
, __last
, __val
,
2099 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2102 template<typename _ForwardIterator
, typename _Tp
,
2103 typename _CompareItTp
, typename _CompareTpIt
>
2104 _GLIBCXX20_CONSTEXPR
2105 pair
<_ForwardIterator
, _ForwardIterator
>
2106 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2108 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2110 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2113 _DistanceType __len
= std::distance(__first
, __last
);
2117 _DistanceType __half
= __len
>> 1;
2118 _ForwardIterator __middle
= __first
;
2119 std::advance(__middle
, __half
);
2120 if (__comp_it_val(__middle
, __val
))
2124 __len
= __len
- __half
- 1;
2126 else if (__comp_val_it(__val
, __middle
))
2130 _ForwardIterator __left
2131 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2132 std::advance(__first
, __len
);
2133 _ForwardIterator __right
2134 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2135 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2138 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2142 * @brief Finds the largest subrange in which @p __val could be inserted
2143 * at any place in it without changing the ordering.
2144 * @ingroup binary_search_algorithms
2145 * @param __first An iterator.
2146 * @param __last Another iterator.
2147 * @param __val The search term.
2148 * @return An pair of iterators defining the subrange.
2149 * @ingroup binary_search_algorithms
2151 * This is equivalent to
2153 * std::make_pair(lower_bound(__first, __last, __val),
2154 * upper_bound(__first, __last, __val))
2156 * but does not actually call those functions.
2158 template<typename _ForwardIterator
, typename _Tp
>
2159 _GLIBCXX20_CONSTEXPR
2160 inline pair
<_ForwardIterator
, _ForwardIterator
>
2161 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2164 // concept requirements
2165 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2166 __glibcxx_function_requires(_LessThanOpConcept
<
2167 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2168 __glibcxx_function_requires(_LessThanOpConcept
<
2169 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2170 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2171 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2173 return std::__equal_range(__first
, __last
, __val
,
2174 __gnu_cxx::__ops::__iter_less_val(),
2175 __gnu_cxx::__ops::__val_less_iter());
2179 * @brief Finds the largest subrange in which @p __val could be inserted
2180 * at any place in it without changing the ordering.
2181 * @param __first An iterator.
2182 * @param __last Another iterator.
2183 * @param __val The search term.
2184 * @param __comp A functor to use for comparisons.
2185 * @return An pair of iterators defining the subrange.
2186 * @ingroup binary_search_algorithms
2188 * This is equivalent to
2190 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2191 * upper_bound(__first, __last, __val, __comp))
2193 * but does not actually call those functions.
2195 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2196 _GLIBCXX20_CONSTEXPR
2197 inline pair
<_ForwardIterator
, _ForwardIterator
>
2198 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2199 const _Tp
& __val
, _Compare __comp
)
2201 // concept requirements
2202 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2203 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2204 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2205 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2206 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2207 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2209 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2212 return std::__equal_range(__first
, __last
, __val
,
2213 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2214 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2218 * @brief Determines whether an element exists in a range.
2219 * @ingroup binary_search_algorithms
2220 * @param __first An iterator.
2221 * @param __last Another iterator.
2222 * @param __val The search term.
2223 * @return True if @p __val (or its equivalent) is in [@p
2224 * __first,@p __last ].
2226 * Note that this does not actually return an iterator to @p __val. For
2227 * that, use std::find or a container's specialized find member functions.
2229 template<typename _ForwardIterator
, typename _Tp
>
2230 _GLIBCXX20_CONSTEXPR
2232 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2235 // concept requirements
2236 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2237 __glibcxx_function_requires(_LessThanOpConcept
<
2238 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2239 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2240 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2242 _ForwardIterator __i
2243 = std::__lower_bound(__first
, __last
, __val
,
2244 __gnu_cxx::__ops::__iter_less_val());
2245 return __i
!= __last
&& !(__val
< *__i
);
2249 * @brief Determines whether an element exists in a range.
2250 * @ingroup binary_search_algorithms
2251 * @param __first An iterator.
2252 * @param __last Another iterator.
2253 * @param __val The search term.
2254 * @param __comp A functor to use for comparisons.
2255 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2257 * Note that this does not actually return an iterator to @p __val. For
2258 * that, use std::find or a container's specialized find member functions.
2260 * The comparison function should have the same effects on ordering as
2261 * the function used for the initial sort.
2263 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2264 _GLIBCXX20_CONSTEXPR
2266 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2267 const _Tp
& __val
, _Compare __comp
)
2269 // concept requirements
2270 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2271 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2272 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2273 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2275 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2278 _ForwardIterator __i
2279 = std::__lower_bound(__first
, __last
, __val
,
2280 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2281 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2286 /// This is a helper function for the __merge_adaptive routines.
2287 template<typename _InputIterator1
, typename _InputIterator2
,
2288 typename _OutputIterator
, typename _Compare
>
2290 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2291 _InputIterator2 __first2
, _InputIterator2 __last2
,
2292 _OutputIterator __result
, _Compare __comp
)
2294 while (__first1
!= __last1
&& __first2
!= __last2
)
2296 if (__comp(__first2
, __first1
))
2298 *__result
= _GLIBCXX_MOVE(*__first2
);
2303 *__result
= _GLIBCXX_MOVE(*__first1
);
2308 if (__first1
!= __last1
)
2309 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2312 /// This is a helper function for the __merge_adaptive routines.
2313 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2314 typename _BidirectionalIterator3
, typename _Compare
>
2316 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2317 _BidirectionalIterator1 __last1
,
2318 _BidirectionalIterator2 __first2
,
2319 _BidirectionalIterator2 __last2
,
2320 _BidirectionalIterator3 __result
,
2323 if (__first1
== __last1
)
2325 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2328 else if (__first2
== __last2
)
2335 if (__comp(__last2
, __last1
))
2337 *--__result
= _GLIBCXX_MOVE(*__last1
);
2338 if (__first1
== __last1
)
2340 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2347 *--__result
= _GLIBCXX_MOVE(*__last2
);
2348 if (__first2
== __last2
)
2355 /// This is a helper function for the merge routines.
2356 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2358 _BidirectionalIterator1
2359 __rotate_adaptive(_BidirectionalIterator1 __first
,
2360 _BidirectionalIterator1 __middle
,
2361 _BidirectionalIterator1 __last
,
2362 _Distance __len1
, _Distance __len2
,
2363 _BidirectionalIterator2 __buffer
,
2364 _Distance __buffer_size
)
2366 _BidirectionalIterator2 __buffer_end
;
2367 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2371 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2372 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2373 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2378 else if (__len1
<= __buffer_size
)
2382 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2383 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2384 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2390 return std::rotate(__first
, __middle
, __last
);
2393 /// This is a helper function for the merge routines.
2394 template<typename _BidirectionalIterator
, typename _Distance
,
2395 typename _Pointer
, typename _Compare
>
2397 __merge_adaptive(_BidirectionalIterator __first
,
2398 _BidirectionalIterator __middle
,
2399 _BidirectionalIterator __last
,
2400 _Distance __len1
, _Distance __len2
,
2401 _Pointer __buffer
, _Compare __comp
)
2403 if (__len1
<= __len2
)
2405 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2406 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2411 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2412 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2413 __buffer_end
, __last
, __comp
);
2417 template<typename _BidirectionalIterator
, typename _Distance
,
2418 typename _Pointer
, typename _Compare
>
2420 __merge_adaptive_resize(_BidirectionalIterator __first
,
2421 _BidirectionalIterator __middle
,
2422 _BidirectionalIterator __last
,
2423 _Distance __len1
, _Distance __len2
,
2424 _Pointer __buffer
, _Distance __buffer_size
,
2427 if (__len1
<= __buffer_size
|| __len2
<= __buffer_size
)
2428 std::__merge_adaptive(__first
, __middle
, __last
,
2429 __len1
, __len2
, __buffer
, __comp
);
2432 _BidirectionalIterator __first_cut
= __first
;
2433 _BidirectionalIterator __second_cut
= __middle
;
2434 _Distance __len11
= 0;
2435 _Distance __len22
= 0;
2436 if (__len1
> __len2
)
2438 __len11
= __len1
/ 2;
2439 std::advance(__first_cut
, __len11
);
2441 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2442 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2443 __len22
= std::distance(__middle
, __second_cut
);
2447 __len22
= __len2
/ 2;
2448 std::advance(__second_cut
, __len22
);
2450 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2451 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2452 __len11
= std::distance(__first
, __first_cut
);
2455 _BidirectionalIterator __new_middle
2456 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2457 __len1
- __len11
, __len22
,
2458 __buffer
, __buffer_size
);
2459 std::__merge_adaptive_resize(__first
, __first_cut
, __new_middle
,
2461 __buffer
, __buffer_size
, __comp
);
2462 std::__merge_adaptive_resize(__new_middle
, __second_cut
, __last
,
2463 __len1
- __len11
, __len2
- __len22
,
2464 __buffer
, __buffer_size
, __comp
);
2468 /// This is a helper function for the merge routines.
2469 template<typename _BidirectionalIterator
, typename _Distance
,
2472 __merge_without_buffer(_BidirectionalIterator __first
,
2473 _BidirectionalIterator __middle
,
2474 _BidirectionalIterator __last
,
2475 _Distance __len1
, _Distance __len2
,
2478 if (__len1
== 0 || __len2
== 0)
2481 if (__len1
+ __len2
== 2)
2483 if (__comp(__middle
, __first
))
2484 std::iter_swap(__first
, __middle
);
2488 _BidirectionalIterator __first_cut
= __first
;
2489 _BidirectionalIterator __second_cut
= __middle
;
2490 _Distance __len11
= 0;
2491 _Distance __len22
= 0;
2492 if (__len1
> __len2
)
2494 __len11
= __len1
/ 2;
2495 std::advance(__first_cut
, __len11
);
2497 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2498 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2499 __len22
= std::distance(__middle
, __second_cut
);
2503 __len22
= __len2
/ 2;
2504 std::advance(__second_cut
, __len22
);
2506 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2507 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2508 __len11
= std::distance(__first
, __first_cut
);
2511 _BidirectionalIterator __new_middle
2512 = std::rotate(__first_cut
, __middle
, __second_cut
);
2513 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2514 __len11
, __len22
, __comp
);
2515 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2516 __len1
- __len11
, __len2
- __len22
, __comp
);
2519 template<typename _BidirectionalIterator
, typename _Compare
>
2521 __inplace_merge(_BidirectionalIterator __first
,
2522 _BidirectionalIterator __middle
,
2523 _BidirectionalIterator __last
,
2526 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2528 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2530 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2532 if (__first
== __middle
|| __middle
== __last
)
2535 const _DistanceType __len1
= std::distance(__first
, __middle
);
2536 const _DistanceType __len2
= std::distance(__middle
, __last
);
2538 // __merge_adaptive will use a buffer for the smaller of
2539 // [first,middle) and [middle,last).
2540 _TmpBuf
__buf(__first
, std::min(__len1
, __len2
));
2542 if (__builtin_expect(__buf
.size() == __buf
.requested_size(), true))
2543 std::__merge_adaptive
2544 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(), __comp
);
2545 else if (__builtin_expect(__buf
.begin() == 0, false))
2546 std::__merge_without_buffer
2547 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2549 std::__merge_adaptive_resize
2550 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2551 _DistanceType(__buf
.size()), __comp
);
2555 * @brief Merges two sorted ranges in place.
2556 * @ingroup sorting_algorithms
2557 * @param __first An iterator.
2558 * @param __middle Another iterator.
2559 * @param __last Another iterator.
2562 * Merges two sorted and consecutive ranges, [__first,__middle) and
2563 * [__middle,__last), and puts the result in [__first,__last). The
2564 * output will be sorted. The sort is @e stable, that is, for
2565 * equivalent elements in the two ranges, elements from the first
2566 * range will always come before elements from the second.
2568 * If enough additional memory is available, this takes (__last-__first)-1
2569 * comparisons. Otherwise an NlogN algorithm is used, where N is
2570 * distance(__first,__last).
2572 template<typename _BidirectionalIterator
>
2574 inplace_merge(_BidirectionalIterator __first
,
2575 _BidirectionalIterator __middle
,
2576 _BidirectionalIterator __last
)
2578 // concept requirements
2579 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2580 _BidirectionalIterator
>)
2581 __glibcxx_function_requires(_LessThanComparableConcept
<
2582 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2583 __glibcxx_requires_sorted(__first
, __middle
);
2584 __glibcxx_requires_sorted(__middle
, __last
);
2585 __glibcxx_requires_irreflexive(__first
, __last
);
2587 std::__inplace_merge(__first
, __middle
, __last
,
2588 __gnu_cxx::__ops::__iter_less_iter());
2592 * @brief Merges two sorted ranges in place.
2593 * @ingroup sorting_algorithms
2594 * @param __first An iterator.
2595 * @param __middle Another iterator.
2596 * @param __last Another iterator.
2597 * @param __comp A functor to use for comparisons.
2600 * Merges two sorted and consecutive ranges, [__first,__middle) and
2601 * [middle,last), and puts the result in [__first,__last). The output will
2602 * be sorted. The sort is @e stable, that is, for equivalent
2603 * elements in the two ranges, elements from the first range will always
2604 * come before elements from the second.
2606 * If enough additional memory is available, this takes (__last-__first)-1
2607 * comparisons. Otherwise an NlogN algorithm is used, where N is
2608 * distance(__first,__last).
2610 * The comparison function should have the same effects on ordering as
2611 * the function used for the initial sort.
2613 template<typename _BidirectionalIterator
, typename _Compare
>
2615 inplace_merge(_BidirectionalIterator __first
,
2616 _BidirectionalIterator __middle
,
2617 _BidirectionalIterator __last
,
2620 // concept requirements
2621 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2622 _BidirectionalIterator
>)
2623 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2624 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2625 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2626 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2627 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2628 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2630 std::__inplace_merge(__first
, __middle
, __last
,
2631 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2635 /// This is a helper function for the __merge_sort_loop routines.
2636 template<typename _InputIterator
, typename _OutputIterator
,
2639 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2640 _InputIterator __first2
, _InputIterator __last2
,
2641 _OutputIterator __result
, _Compare __comp
)
2643 while (__first1
!= __last1
&& __first2
!= __last2
)
2645 if (__comp(__first2
, __first1
))
2647 *__result
= _GLIBCXX_MOVE(*__first2
);
2652 *__result
= _GLIBCXX_MOVE(*__first1
);
2657 return _GLIBCXX_MOVE3(__first2
, __last2
,
2658 _GLIBCXX_MOVE3(__first1
, __last1
,
2662 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2663 typename _Distance
, typename _Compare
>
2665 __merge_sort_loop(_RandomAccessIterator1 __first
,
2666 _RandomAccessIterator1 __last
,
2667 _RandomAccessIterator2 __result
, _Distance __step_size
,
2670 const _Distance __two_step
= 2 * __step_size
;
2672 while (__last
- __first
>= __two_step
)
2674 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2675 __first
+ __step_size
,
2676 __first
+ __two_step
,
2678 __first
+= __two_step
;
2680 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2682 std::__move_merge(__first
, __first
+ __step_size
,
2683 __first
+ __step_size
, __last
, __result
, __comp
);
2686 template<typename _RandomAccessIterator
, typename _Distance
,
2688 _GLIBCXX20_CONSTEXPR
2690 __chunk_insertion_sort(_RandomAccessIterator __first
,
2691 _RandomAccessIterator __last
,
2692 _Distance __chunk_size
, _Compare __comp
)
2694 while (__last
- __first
>= __chunk_size
)
2696 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2697 __first
+= __chunk_size
;
2699 std::__insertion_sort(__first
, __last
, __comp
);
2702 enum { _S_chunk_size
= 7 };
2704 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2706 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2707 _RandomAccessIterator __last
,
2708 _Pointer __buffer
, _Compare __comp
)
2710 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2713 const _Distance __len
= __last
- __first
;
2714 const _Pointer __buffer_last
= __buffer
+ __len
;
2716 _Distance __step_size
= _S_chunk_size
;
2717 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2719 while (__step_size
< __len
)
2721 std::__merge_sort_loop(__first
, __last
, __buffer
,
2722 __step_size
, __comp
);
2724 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2725 __step_size
, __comp
);
2730 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2732 __stable_sort_adaptive(_RandomAccessIterator __first
,
2733 _RandomAccessIterator __middle
,
2734 _RandomAccessIterator __last
,
2735 _Pointer __buffer
, _Compare __comp
)
2737 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2738 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2740 std::__merge_adaptive(__first
, __middle
, __last
,
2741 __middle
- __first
, __last
- __middle
,
2745 template<typename _RandomAccessIterator
, typename _Pointer
,
2746 typename _Distance
, typename _Compare
>
2748 __stable_sort_adaptive_resize(_RandomAccessIterator __first
,
2749 _RandomAccessIterator __last
,
2750 _Pointer __buffer
, _Distance __buffer_size
,
2753 const _Distance __len
= (__last
- __first
+ 1) / 2;
2754 const _RandomAccessIterator __middle
= __first
+ __len
;
2755 if (__len
> __buffer_size
)
2757 std::__stable_sort_adaptive_resize(__first
, __middle
, __buffer
,
2758 __buffer_size
, __comp
);
2759 std::__stable_sort_adaptive_resize(__middle
, __last
, __buffer
,
2760 __buffer_size
, __comp
);
2761 std::__merge_adaptive_resize(__first
, __middle
, __last
,
2762 _Distance(__middle
- __first
),
2763 _Distance(__last
- __middle
),
2764 __buffer
, __buffer_size
,
2768 std::__stable_sort_adaptive(__first
, __middle
, __last
,
2772 /// This is a helper function for the stable sorting routines.
2773 template<typename _RandomAccessIterator
, typename _Compare
>
2775 __inplace_stable_sort(_RandomAccessIterator __first
,
2776 _RandomAccessIterator __last
, _Compare __comp
)
2778 if (__last
- __first
< 15)
2780 std::__insertion_sort(__first
, __last
, __comp
);
2783 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2784 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2785 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2786 std::__merge_without_buffer(__first
, __middle
, __last
,
2794 // Set algorithms: includes, set_union, set_intersection, set_difference,
2795 // set_symmetric_difference. All of these algorithms have the precondition
2796 // that their input ranges are sorted and the postcondition that their output
2797 // ranges are sorted.
2799 template<typename _InputIterator1
, typename _InputIterator2
,
2801 _GLIBCXX20_CONSTEXPR
2803 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2804 _InputIterator2 __first2
, _InputIterator2 __last2
,
2807 while (__first1
!= __last1
&& __first2
!= __last2
)
2809 if (__comp(__first2
, __first1
))
2811 if (!__comp(__first1
, __first2
))
2816 return __first2
== __last2
;
2820 * @brief Determines whether all elements of a sequence exists in a range.
2821 * @param __first1 Start of search range.
2822 * @param __last1 End of search range.
2823 * @param __first2 Start of sequence
2824 * @param __last2 End of sequence.
2825 * @return True if each element in [__first2,__last2) is contained in order
2826 * within [__first1,__last1). False otherwise.
2827 * @ingroup set_algorithms
2829 * This operation expects both [__first1,__last1) and
2830 * [__first2,__last2) to be sorted. Searches for the presence of
2831 * each element in [__first2,__last2) within [__first1,__last1).
2832 * The iterators over each range only move forward, so this is a
2833 * linear algorithm. If an element in [__first2,__last2) is not
2834 * found before the search iterator reaches @p __last2, false is
2837 template<typename _InputIterator1
, typename _InputIterator2
>
2838 _GLIBCXX20_CONSTEXPR
2840 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2841 _InputIterator2 __first2
, _InputIterator2 __last2
)
2843 // concept requirements
2844 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2845 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2846 __glibcxx_function_requires(_LessThanOpConcept
<
2847 typename iterator_traits
<_InputIterator1
>::value_type
,
2848 typename iterator_traits
<_InputIterator2
>::value_type
>)
2849 __glibcxx_function_requires(_LessThanOpConcept
<
2850 typename iterator_traits
<_InputIterator2
>::value_type
,
2851 typename iterator_traits
<_InputIterator1
>::value_type
>)
2852 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2853 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2854 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2855 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2857 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2858 __gnu_cxx::__ops::__iter_less_iter());
2862 * @brief Determines whether all elements of a sequence exists in a range
2864 * @ingroup set_algorithms
2865 * @param __first1 Start of search range.
2866 * @param __last1 End of search range.
2867 * @param __first2 Start of sequence
2868 * @param __last2 End of sequence.
2869 * @param __comp Comparison function to use.
2870 * @return True if each element in [__first2,__last2) is contained
2871 * in order within [__first1,__last1) according to comp. False
2872 * otherwise. @ingroup set_algorithms
2874 * This operation expects both [__first1,__last1) and
2875 * [__first2,__last2) to be sorted. Searches for the presence of
2876 * each element in [__first2,__last2) within [__first1,__last1),
2877 * using comp to decide. The iterators over each range only move
2878 * forward, so this is a linear algorithm. If an element in
2879 * [__first2,__last2) is not found before the search iterator
2880 * reaches @p __last2, false is returned.
2882 template<typename _InputIterator1
, typename _InputIterator2
,
2884 _GLIBCXX20_CONSTEXPR
2886 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2887 _InputIterator2 __first2
, _InputIterator2 __last2
,
2890 // concept requirements
2891 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2893 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2894 typename iterator_traits
<_InputIterator1
>::value_type
,
2895 typename iterator_traits
<_InputIterator2
>::value_type
>)
2896 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2897 typename iterator_traits
<_InputIterator2
>::value_type
,
2898 typename iterator_traits
<_InputIterator1
>::value_type
>)
2899 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2900 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2901 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2902 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2904 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2905 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2914 // set_symmetric_difference
2918 template<typename _BidirectionalIterator
, typename _Compare
>
2919 _GLIBCXX20_CONSTEXPR
2921 __next_permutation(_BidirectionalIterator __first
,
2922 _BidirectionalIterator __last
, _Compare __comp
)
2924 if (__first
== __last
)
2926 _BidirectionalIterator __i
= __first
;
2935 _BidirectionalIterator __ii
= __i
;
2937 if (__comp(__i
, __ii
))
2939 _BidirectionalIterator __j
= __last
;
2940 while (!__comp(__i
, --__j
))
2942 std::iter_swap(__i
, __j
);
2943 std::__reverse(__ii
, __last
,
2944 std::__iterator_category(__first
));
2949 std::__reverse(__first
, __last
,
2950 std::__iterator_category(__first
));
2957 * @brief Permute range into the next @e dictionary ordering.
2958 * @ingroup sorting_algorithms
2959 * @param __first Start of range.
2960 * @param __last End of range.
2961 * @return False if wrapped to first permutation, true otherwise.
2963 * Treats all permutations of the range as a set of @e dictionary sorted
2964 * sequences. Permutes the current sequence into the next one of this set.
2965 * Returns true if there are more sequences to generate. If the sequence
2966 * is the largest of the set, the smallest is generated and false returned.
2968 template<typename _BidirectionalIterator
>
2969 _GLIBCXX20_CONSTEXPR
2971 next_permutation(_BidirectionalIterator __first
,
2972 _BidirectionalIterator __last
)
2974 // concept requirements
2975 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2976 _BidirectionalIterator
>)
2977 __glibcxx_function_requires(_LessThanComparableConcept
<
2978 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2979 __glibcxx_requires_valid_range(__first
, __last
);
2980 __glibcxx_requires_irreflexive(__first
, __last
);
2982 return std::__next_permutation
2983 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2987 * @brief Permute range into the next @e dictionary ordering using
2988 * comparison functor.
2989 * @ingroup sorting_algorithms
2990 * @param __first Start of range.
2991 * @param __last End of range.
2992 * @param __comp A comparison functor.
2993 * @return False if wrapped to first permutation, true otherwise.
2995 * Treats all permutations of the range [__first,__last) as a set of
2996 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2997 * sequence into the next one of this set. Returns true if there are more
2998 * sequences to generate. If the sequence is the largest of the set, the
2999 * smallest is generated and false returned.
3001 template<typename _BidirectionalIterator
, typename _Compare
>
3002 _GLIBCXX20_CONSTEXPR
3004 next_permutation(_BidirectionalIterator __first
,
3005 _BidirectionalIterator __last
, _Compare __comp
)
3007 // concept requirements
3008 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3009 _BidirectionalIterator
>)
3010 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3011 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3012 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3013 __glibcxx_requires_valid_range(__first
, __last
);
3014 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3016 return std::__next_permutation
3017 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3020 template<typename _BidirectionalIterator
, typename _Compare
>
3021 _GLIBCXX20_CONSTEXPR
3023 __prev_permutation(_BidirectionalIterator __first
,
3024 _BidirectionalIterator __last
, _Compare __comp
)
3026 if (__first
== __last
)
3028 _BidirectionalIterator __i
= __first
;
3037 _BidirectionalIterator __ii
= __i
;
3039 if (__comp(__ii
, __i
))
3041 _BidirectionalIterator __j
= __last
;
3042 while (!__comp(--__j
, __i
))
3044 std::iter_swap(__i
, __j
);
3045 std::__reverse(__ii
, __last
,
3046 std::__iterator_category(__first
));
3051 std::__reverse(__first
, __last
,
3052 std::__iterator_category(__first
));
3059 * @brief Permute range into the previous @e dictionary ordering.
3060 * @ingroup sorting_algorithms
3061 * @param __first Start of range.
3062 * @param __last End of range.
3063 * @return False if wrapped to last permutation, true otherwise.
3065 * Treats all permutations of the range as a set of @e dictionary sorted
3066 * sequences. Permutes the current sequence into the previous one of this
3067 * set. Returns true if there are more sequences to generate. If the
3068 * sequence is the smallest of the set, the largest is generated and false
3071 template<typename _BidirectionalIterator
>
3072 _GLIBCXX20_CONSTEXPR
3074 prev_permutation(_BidirectionalIterator __first
,
3075 _BidirectionalIterator __last
)
3077 // concept requirements
3078 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3079 _BidirectionalIterator
>)
3080 __glibcxx_function_requires(_LessThanComparableConcept
<
3081 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3082 __glibcxx_requires_valid_range(__first
, __last
);
3083 __glibcxx_requires_irreflexive(__first
, __last
);
3085 return std::__prev_permutation(__first
, __last
,
3086 __gnu_cxx::__ops::__iter_less_iter());
3090 * @brief Permute range into the previous @e dictionary ordering using
3091 * comparison functor.
3092 * @ingroup sorting_algorithms
3093 * @param __first Start of range.
3094 * @param __last End of range.
3095 * @param __comp A comparison functor.
3096 * @return False if wrapped to last permutation, true otherwise.
3098 * Treats all permutations of the range [__first,__last) as a set of
3099 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3100 * sequence into the previous one of this set. Returns true if there are
3101 * more sequences to generate. If the sequence is the smallest of the set,
3102 * the largest is generated and false returned.
3104 template<typename _BidirectionalIterator
, typename _Compare
>
3105 _GLIBCXX20_CONSTEXPR
3107 prev_permutation(_BidirectionalIterator __first
,
3108 _BidirectionalIterator __last
, _Compare __comp
)
3110 // concept requirements
3111 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3112 _BidirectionalIterator
>)
3113 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3114 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3115 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3116 __glibcxx_requires_valid_range(__first
, __last
);
3117 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3119 return std::__prev_permutation(__first
, __last
,
3120 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3126 template<typename _InputIterator
, typename _OutputIterator
,
3127 typename _Predicate
, typename _Tp
>
3128 _GLIBCXX20_CONSTEXPR
3130 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3131 _OutputIterator __result
,
3132 _Predicate __pred
, const _Tp
& __new_value
)
3134 for (; __first
!= __last
; ++__first
, (void)++__result
)
3135 if (__pred(__first
))
3136 *__result
= __new_value
;
3138 *__result
= *__first
;
3143 * @brief Copy a sequence, replacing each element of one value with another
3145 * @param __first An input iterator.
3146 * @param __last An input iterator.
3147 * @param __result An output iterator.
3148 * @param __old_value The value to be replaced.
3149 * @param __new_value The replacement value.
3150 * @return The end of the output sequence, @p result+(last-first).
3152 * Copies each element in the input range @p [__first,__last) to the
3153 * output range @p [__result,__result+(__last-__first)) replacing elements
3154 * equal to @p __old_value with @p __new_value.
3156 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3157 _GLIBCXX20_CONSTEXPR
3158 inline _OutputIterator
3159 replace_copy(_InputIterator __first
, _InputIterator __last
,
3160 _OutputIterator __result
,
3161 const _Tp
& __old_value
, const _Tp
& __new_value
)
3163 // concept requirements
3164 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3165 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3166 typename iterator_traits
<_InputIterator
>::value_type
>)
3167 __glibcxx_function_requires(_EqualOpConcept
<
3168 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3169 __glibcxx_requires_valid_range(__first
, __last
);
3171 return std::__replace_copy_if(__first
, __last
, __result
,
3172 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3177 * @brief Copy a sequence, replacing each value for which a predicate
3178 * returns true with another value.
3179 * @ingroup mutating_algorithms
3180 * @param __first An input iterator.
3181 * @param __last An input iterator.
3182 * @param __result An output iterator.
3183 * @param __pred A predicate.
3184 * @param __new_value The replacement value.
3185 * @return The end of the output sequence, @p __result+(__last-__first).
3187 * Copies each element in the range @p [__first,__last) to the range
3188 * @p [__result,__result+(__last-__first)) replacing elements for which
3189 * @p __pred returns true with @p __new_value.
3191 template<typename _InputIterator
, typename _OutputIterator
,
3192 typename _Predicate
, typename _Tp
>
3193 _GLIBCXX20_CONSTEXPR
3194 inline _OutputIterator
3195 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3196 _OutputIterator __result
,
3197 _Predicate __pred
, const _Tp
& __new_value
)
3199 // concept requirements
3200 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3201 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3202 typename iterator_traits
<_InputIterator
>::value_type
>)
3203 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3204 typename iterator_traits
<_InputIterator
>::value_type
>)
3205 __glibcxx_requires_valid_range(__first
, __last
);
3207 return std::__replace_copy_if(__first
, __last
, __result
,
3208 __gnu_cxx::__ops::__pred_iter(__pred
),
3212 #if __cplusplus >= 201103L
3214 * @brief Determines whether the elements of a sequence are sorted.
3215 * @ingroup sorting_algorithms
3216 * @param __first An iterator.
3217 * @param __last Another iterator.
3218 * @return True if the elements are sorted, false otherwise.
3220 template<typename _ForwardIterator
>
3221 _GLIBCXX20_CONSTEXPR
3223 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3224 { return std::is_sorted_until(__first
, __last
) == __last
; }
3227 * @brief Determines whether the elements of a sequence are sorted
3228 * according to a comparison functor.
3229 * @ingroup sorting_algorithms
3230 * @param __first An iterator.
3231 * @param __last Another iterator.
3232 * @param __comp A comparison functor.
3233 * @return True if the elements are sorted, false otherwise.
3235 template<typename _ForwardIterator
, typename _Compare
>
3236 _GLIBCXX20_CONSTEXPR
3238 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3240 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3242 template<typename _ForwardIterator
, typename _Compare
>
3243 _GLIBCXX20_CONSTEXPR
3245 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3248 if (__first
== __last
)
3251 _ForwardIterator __next
= __first
;
3252 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3253 if (__comp(__next
, __first
))
3259 * @brief Determines the end of a sorted sequence.
3260 * @ingroup sorting_algorithms
3261 * @param __first An iterator.
3262 * @param __last Another iterator.
3263 * @return An iterator pointing to the last iterator i in [__first, __last)
3264 * for which the range [__first, i) is sorted.
3266 template<typename _ForwardIterator
>
3267 _GLIBCXX20_CONSTEXPR
3268 inline _ForwardIterator
3269 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3271 // concept requirements
3272 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3273 __glibcxx_function_requires(_LessThanComparableConcept
<
3274 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3275 __glibcxx_requires_valid_range(__first
, __last
);
3276 __glibcxx_requires_irreflexive(__first
, __last
);
3278 return std::__is_sorted_until(__first
, __last
,
3279 __gnu_cxx::__ops::__iter_less_iter());
3283 * @brief Determines the end of a sorted sequence using comparison functor.
3284 * @ingroup sorting_algorithms
3285 * @param __first An iterator.
3286 * @param __last Another iterator.
3287 * @param __comp A comparison functor.
3288 * @return An iterator pointing to the last iterator i in [__first, __last)
3289 * for which the range [__first, i) is sorted.
3291 template<typename _ForwardIterator
, typename _Compare
>
3292 _GLIBCXX20_CONSTEXPR
3293 inline _ForwardIterator
3294 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3297 // concept requirements
3298 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3299 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3300 typename iterator_traits
<_ForwardIterator
>::value_type
,
3301 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3302 __glibcxx_requires_valid_range(__first
, __last
);
3303 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3305 return std::__is_sorted_until(__first
, __last
,
3306 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3310 * @brief Determines min and max at once as an ordered pair.
3311 * @ingroup sorting_algorithms
3312 * @param __a A thing of arbitrary type.
3313 * @param __b Another thing of arbitrary type.
3314 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3317 template<typename _Tp
>
3318 _GLIBCXX14_CONSTEXPR
3319 inline pair
<const _Tp
&, const _Tp
&>
3320 minmax(const _Tp
& __a
, const _Tp
& __b
)
3322 // concept requirements
3323 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3325 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3326 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3330 * @brief Determines min and max at once as an ordered pair.
3331 * @ingroup sorting_algorithms
3332 * @param __a A thing of arbitrary type.
3333 * @param __b Another thing of arbitrary type.
3334 * @param __comp A @link comparison_functors comparison functor @endlink.
3335 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3338 template<typename _Tp
, typename _Compare
>
3339 _GLIBCXX14_CONSTEXPR
3340 inline pair
<const _Tp
&, const _Tp
&>
3341 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3343 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3344 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3347 template<typename _ForwardIterator
, typename _Compare
>
3348 _GLIBCXX14_CONSTEXPR
3349 pair
<_ForwardIterator
, _ForwardIterator
>
3350 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3353 _ForwardIterator __next
= __first
;
3354 if (__first
== __last
3355 || ++__next
== __last
)
3356 return std::make_pair(__first
, __first
);
3358 _ForwardIterator __min
{}, __max
{};
3359 if (__comp(__next
, __first
))
3373 while (__first
!= __last
)
3376 if (++__next
== __last
)
3378 if (__comp(__first
, __min
))
3380 else if (!__comp(__first
, __max
))
3385 if (__comp(__next
, __first
))
3387 if (__comp(__next
, __min
))
3389 if (!__comp(__first
, __max
))
3394 if (__comp(__first
, __min
))
3396 if (!__comp(__next
, __max
))
3404 return std::make_pair(__min
, __max
);
3408 * @brief Return a pair of iterators pointing to the minimum and maximum
3409 * elements in a range.
3410 * @ingroup sorting_algorithms
3411 * @param __first Start of range.
3412 * @param __last End of range.
3413 * @return make_pair(m, M), where m is the first iterator i in
3414 * [__first, __last) such that no other element in the range is
3415 * smaller, and where M is the last iterator i in [__first, __last)
3416 * such that no other element in the range is larger.
3418 template<typename _ForwardIterator
>
3419 _GLIBCXX14_CONSTEXPR
3420 inline pair
<_ForwardIterator
, _ForwardIterator
>
3421 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3423 // concept requirements
3424 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3425 __glibcxx_function_requires(_LessThanComparableConcept
<
3426 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3427 __glibcxx_requires_valid_range(__first
, __last
);
3428 __glibcxx_requires_irreflexive(__first
, __last
);
3430 return std::__minmax_element(__first
, __last
,
3431 __gnu_cxx::__ops::__iter_less_iter());
3435 * @brief Return a pair of iterators pointing to the minimum and maximum
3436 * elements in a range.
3437 * @ingroup sorting_algorithms
3438 * @param __first Start of range.
3439 * @param __last End of range.
3440 * @param __comp Comparison functor.
3441 * @return make_pair(m, M), where m is the first iterator i in
3442 * [__first, __last) such that no other element in the range is
3443 * smaller, and where M is the last iterator i in [__first, __last)
3444 * such that no other element in the range is larger.
3446 template<typename _ForwardIterator
, typename _Compare
>
3447 _GLIBCXX14_CONSTEXPR
3448 inline pair
<_ForwardIterator
, _ForwardIterator
>
3449 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3452 // concept requirements
3453 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3454 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3455 typename iterator_traits
<_ForwardIterator
>::value_type
,
3456 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3457 __glibcxx_requires_valid_range(__first
, __last
);
3458 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3460 return std::__minmax_element(__first
, __last
,
3461 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3464 template<typename _Tp
>
3465 _GLIBCXX14_CONSTEXPR
3466 inline pair
<_Tp
, _Tp
>
3467 minmax(initializer_list
<_Tp
> __l
)
3469 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
3470 pair
<const _Tp
*, const _Tp
*> __p
=
3471 std::__minmax_element(__l
.begin(), __l
.end(),
3472 __gnu_cxx::__ops::__iter_less_iter());
3473 return std::make_pair(*__p
.first
, *__p
.second
);
3476 template<typename _Tp
, typename _Compare
>
3477 _GLIBCXX14_CONSTEXPR
3478 inline pair
<_Tp
, _Tp
>
3479 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3481 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
3482 pair
<const _Tp
*, const _Tp
*> __p
=
3483 std::__minmax_element(__l
.begin(), __l
.end(),
3484 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3485 return std::make_pair(*__p
.first
, *__p
.second
);
3489 * @brief Checks whether a permutation of the second sequence is equal
3490 * to the first sequence.
3491 * @ingroup non_mutating_algorithms
3492 * @param __first1 Start of first range.
3493 * @param __last1 End of first range.
3494 * @param __first2 Start of second range.
3495 * @param __pred A binary predicate.
3496 * @return true if there exists a permutation of the elements in
3497 * the range [__first2, __first2 + (__last1 - __first1)),
3498 * beginning with ForwardIterator2 begin, such that
3499 * equal(__first1, __last1, __begin, __pred) returns true;
3500 * otherwise, returns false.
3502 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3503 typename _BinaryPredicate
>
3504 _GLIBCXX20_CONSTEXPR
3506 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3507 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3509 // concept requirements
3510 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3511 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3512 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3513 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3514 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3515 __glibcxx_requires_valid_range(__first1
, __last1
);
3517 return std::__is_permutation(__first1
, __last1
, __first2
,
3518 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3521 #if __cplusplus > 201103L
3522 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3523 typename _BinaryPredicate
>
3524 _GLIBCXX20_CONSTEXPR
3526 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3527 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3528 _BinaryPredicate __pred
)
3531 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3533 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3534 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3535 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3536 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3539 auto __d1
= std::distance(__first1
, __last1
);
3540 auto __d2
= std::distance(__first2
, __last2
);
3545 // Efficiently compare identical prefixes: O(N) if sequences
3546 // have the same elements in the same order.
3547 for (; __first1
!= __last1
&& __first2
!= __last2
;
3548 ++__first1
, (void)++__first2
)
3549 if (!__pred(__first1
, __first2
))
3554 if (__first1
== __last1
)
3559 auto __d1
= std::distance(__first1
, __last1
);
3560 auto __d2
= std::distance(__first2
, __last2
);
3561 if (__d1
== 0 && __d2
== 0)
3567 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3569 if (__scan
!= std::__find_if(__first1
, __scan
,
3570 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3571 continue; // We've seen this one before.
3573 auto __matches
= std::__count_if(__first2
, __last2
,
3574 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3576 || std::__count_if(__scan
, __last1
,
3577 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3585 * @brief Checks whether a permutaion of the second sequence is equal
3586 * to the first sequence.
3587 * @ingroup non_mutating_algorithms
3588 * @param __first1 Start of first range.
3589 * @param __last1 End of first range.
3590 * @param __first2 Start of second range.
3591 * @param __last2 End of first range.
3592 * @return true if there exists a permutation of the elements in the range
3593 * [__first2, __last2), beginning with ForwardIterator2 begin,
3594 * such that equal(__first1, __last1, begin) returns true;
3595 * otherwise, returns false.
3597 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3598 _GLIBCXX20_CONSTEXPR
3600 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3601 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3603 __glibcxx_requires_valid_range(__first1
, __last1
);
3604 __glibcxx_requires_valid_range(__first2
, __last2
);
3607 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3608 __gnu_cxx::__ops::__iter_equal_to_iter());
3612 * @brief Checks whether a permutation of the second sequence is equal
3613 * to the first sequence.
3614 * @ingroup non_mutating_algorithms
3615 * @param __first1 Start of first range.
3616 * @param __last1 End of first range.
3617 * @param __first2 Start of second range.
3618 * @param __last2 End of first range.
3619 * @param __pred A binary predicate.
3620 * @return true if there exists a permutation of the elements in the range
3621 * [__first2, __last2), beginning with ForwardIterator2 begin,
3622 * such that equal(__first1, __last1, __begin, __pred) returns true;
3623 * otherwise, returns false.
3625 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3626 typename _BinaryPredicate
>
3627 _GLIBCXX20_CONSTEXPR
3629 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3630 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3631 _BinaryPredicate __pred
)
3633 __glibcxx_requires_valid_range(__first1
, __last1
);
3634 __glibcxx_requires_valid_range(__first2
, __last2
);
3636 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3637 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3640 #if __cplusplus >= 201703L
3642 #define __cpp_lib_clamp 201603L
3645 * @brief Returns the value clamped between lo and hi.
3646 * @ingroup sorting_algorithms
3647 * @param __val A value of arbitrary type.
3648 * @param __lo A lower limit of arbitrary type.
3649 * @param __hi An upper limit of arbitrary type.
3650 * @retval `__lo` if `__val < __lo`
3651 * @retval `__hi` if `__hi < __val`
3652 * @retval `__val` otherwise.
3653 * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
3655 template<typename _Tp
>
3656 constexpr const _Tp
&
3657 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3659 __glibcxx_assert(!(__hi
< __lo
));
3660 return std::min(std::max(__val
, __lo
), __hi
);
3664 * @brief Returns the value clamped between lo and hi.
3665 * @ingroup sorting_algorithms
3666 * @param __val A value of arbitrary type.
3667 * @param __lo A lower limit of arbitrary type.
3668 * @param __hi An upper limit of arbitrary type.
3669 * @param __comp A comparison functor.
3670 * @retval `__lo` if `__comp(__val, __lo)`
3671 * @retval `__hi` if `__comp(__hi, __val)`
3672 * @retval `__val` otherwise.
3673 * @pre `__comp(__hi, __lo)` is false.
3675 template<typename _Tp
, typename _Compare
>
3676 constexpr const _Tp
&
3677 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3679 __glibcxx_assert(!__comp(__hi
, __lo
));
3680 return std::min(std::max(__val
, __lo
, __comp
), __hi
, __comp
);
3685 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3687 * @brief Generate two uniformly distributed integers using a
3688 * single distribution invocation.
3689 * @param __b0 The upper bound for the first integer.
3690 * @param __b1 The upper bound for the second integer.
3691 * @param __g A UniformRandomBitGenerator.
3692 * @return A pair (i, j) with i and j uniformly distributed
3693 * over [0, __b0) and [0, __b1), respectively.
3695 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3697 * Using uniform_int_distribution with a range that is very
3698 * small relative to the range of the generator ends up wasting
3699 * potentially expensively generated randomness, since
3700 * uniform_int_distribution does not store leftover randomness
3701 * between invocations.
3703 * If we know we want two integers in ranges that are sufficiently
3704 * small, we can compose the ranges, use a single distribution
3705 * invocation, and significantly reduce the waste.
3707 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3708 pair
<_IntType
, _IntType
>
3709 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3710 _UniformRandomBitGenerator
&& __g
)
3713 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3714 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3718 * @brief Shuffle the elements of a sequence using a uniform random
3720 * @ingroup mutating_algorithms
3721 * @param __first A forward iterator.
3722 * @param __last A forward iterator.
3723 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3726 * Reorders the elements in the range @p [__first,__last) using @p __g to
3727 * provide random numbers.
3729 template<typename _RandomAccessIterator
,
3730 typename _UniformRandomNumberGenerator
>
3732 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3733 _UniformRandomNumberGenerator
&& __g
)
3735 // concept requirements
3736 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3737 _RandomAccessIterator
>)
3738 __glibcxx_requires_valid_range(__first
, __last
);
3740 if (__first
== __last
)
3743 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3746 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3747 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3748 typedef typename
__distr_type::param_type __p_type
;
3750 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3752 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3755 const __uc_type __urngrange
= __g
.max() - __g
.min();
3756 const __uc_type __urange
= __uc_type(__last
- __first
);
3758 if (__urngrange
/ __urange
>= __urange
)
3759 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3761 _RandomAccessIterator __i
= __first
+ 1;
3763 // Since we know the range isn't empty, an even number of elements
3764 // means an uneven number of elements /to swap/, in which case we
3765 // do the first one up front:
3767 if ((__urange
% 2) == 0)
3769 __distr_type __d
{0, 1};
3770 std::iter_swap(__i
++, __first
+ __d(__g
));
3773 // Now we know that __last - __i is even, so we do the rest in pairs,
3774 // using a single distribution invocation to produce swap positions
3775 // for two successive elements at a time:
3777 while (__i
!= __last
)
3779 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3781 const pair
<__uc_type
, __uc_type
> __pospos
=
3782 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3784 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3785 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3793 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3794 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3796 #endif // USE C99_STDINT
3800 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3803 * @brief Apply a function to every element of a sequence.
3804 * @ingroup non_mutating_algorithms
3805 * @param __first An input iterator.
3806 * @param __last An input iterator.
3807 * @param __f A unary function object.
3810 * Applies the function object @p __f to each element in the range
3811 * @p [first,last). @p __f must not modify the order of the sequence.
3812 * If @p __f has a return value it is ignored.
3814 template<typename _InputIterator
, typename _Function
>
3815 _GLIBCXX20_CONSTEXPR
3817 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3819 // concept requirements
3820 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3821 __glibcxx_requires_valid_range(__first
, __last
);
3822 for (; __first
!= __last
; ++__first
)
3824 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3827 #if __cplusplus >= 201703L
3829 * @brief Apply a function to every element of a sequence.
3830 * @ingroup non_mutating_algorithms
3831 * @param __first An input iterator.
3832 * @param __n A value convertible to an integer.
3833 * @param __f A unary function object.
3834 * @return `__first+__n`
3836 * Applies the function object `__f` to each element in the range
3837 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3838 * If `__f` has a return value it is ignored.
3840 template<typename _InputIterator
, typename _Size
, typename _Function
>
3841 _GLIBCXX20_CONSTEXPR
3843 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3845 auto __n2
= std::__size_to_integer(__n
);
3846 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3847 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3851 auto __last
= __first
+ __n2
;
3852 std::for_each(__first
, __last
, std::move(__f
));
3868 * @brief Find the first occurrence of a value in a sequence.
3869 * @ingroup non_mutating_algorithms
3870 * @param __first An input iterator.
3871 * @param __last An input iterator.
3872 * @param __val The value to find.
3873 * @return The first iterator @c i in the range @p [__first,__last)
3874 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3876 template<typename _InputIterator
, typename _Tp
>
3877 _GLIBCXX20_CONSTEXPR
3878 inline _InputIterator
3879 find(_InputIterator __first
, _InputIterator __last
,
3882 // concept requirements
3883 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3884 __glibcxx_function_requires(_EqualOpConcept
<
3885 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3886 __glibcxx_requires_valid_range(__first
, __last
);
3887 return std::__find_if(__first
, __last
,
3888 __gnu_cxx::__ops::__iter_equals_val(__val
));
3892 * @brief Find the first element in a sequence for which a
3893 * predicate is true.
3894 * @ingroup non_mutating_algorithms
3895 * @param __first An input iterator.
3896 * @param __last An input iterator.
3897 * @param __pred A predicate.
3898 * @return The first iterator @c i in the range @p [__first,__last)
3899 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3901 template<typename _InputIterator
, typename _Predicate
>
3902 _GLIBCXX20_CONSTEXPR
3903 inline _InputIterator
3904 find_if(_InputIterator __first
, _InputIterator __last
,
3907 // concept requirements
3908 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3909 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3910 typename iterator_traits
<_InputIterator
>::value_type
>)
3911 __glibcxx_requires_valid_range(__first
, __last
);
3913 return std::__find_if(__first
, __last
,
3914 __gnu_cxx::__ops::__pred_iter(__pred
));
3918 * @brief Find element from a set in a sequence.
3919 * @ingroup non_mutating_algorithms
3920 * @param __first1 Start of range to search.
3921 * @param __last1 End of range to search.
3922 * @param __first2 Start of match candidates.
3923 * @param __last2 End of match candidates.
3924 * @return The first iterator @c i in the range
3925 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3926 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3928 * Searches the range @p [__first1,__last1) for an element that is
3929 * equal to some element in the range [__first2,__last2). If
3930 * found, returns an iterator in the range [__first1,__last1),
3931 * otherwise returns @p __last1.
3933 template<typename _InputIterator
, typename _ForwardIterator
>
3934 _GLIBCXX20_CONSTEXPR
3936 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3937 _ForwardIterator __first2
, _ForwardIterator __last2
)
3939 // concept requirements
3940 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3941 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3942 __glibcxx_function_requires(_EqualOpConcept
<
3943 typename iterator_traits
<_InputIterator
>::value_type
,
3944 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3945 __glibcxx_requires_valid_range(__first1
, __last1
);
3946 __glibcxx_requires_valid_range(__first2
, __last2
);
3948 for (; __first1
!= __last1
; ++__first1
)
3949 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3950 if (*__first1
== *__iter
)
3956 * @brief Find element from a set in a sequence using a predicate.
3957 * @ingroup non_mutating_algorithms
3958 * @param __first1 Start of range to search.
3959 * @param __last1 End of range to search.
3960 * @param __first2 Start of match candidates.
3961 * @param __last2 End of match candidates.
3962 * @param __comp Predicate to use.
3963 * @return The first iterator @c i in the range
3964 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3965 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3966 * such iterator exists.
3969 * Searches the range @p [__first1,__last1) for an element that is
3970 * equal to some element in the range [__first2,__last2). If
3971 * found, returns an iterator in the range [__first1,__last1),
3972 * otherwise returns @p __last1.
3974 template<typename _InputIterator
, typename _ForwardIterator
,
3975 typename _BinaryPredicate
>
3976 _GLIBCXX20_CONSTEXPR
3978 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3979 _ForwardIterator __first2
, _ForwardIterator __last2
,
3980 _BinaryPredicate __comp
)
3982 // concept requirements
3983 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3984 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3985 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3986 typename iterator_traits
<_InputIterator
>::value_type
,
3987 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3988 __glibcxx_requires_valid_range(__first1
, __last1
);
3989 __glibcxx_requires_valid_range(__first2
, __last2
);
3991 for (; __first1
!= __last1
; ++__first1
)
3992 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3993 if (__comp(*__first1
, *__iter
))
3999 * @brief Find two adjacent values in a sequence that are equal.
4000 * @ingroup non_mutating_algorithms
4001 * @param __first A forward iterator.
4002 * @param __last A forward iterator.
4003 * @return The first iterator @c i such that @c i and @c i+1 are both
4004 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4005 * or @p __last if no such iterator exists.
4007 template<typename _ForwardIterator
>
4008 _GLIBCXX20_CONSTEXPR
4009 inline _ForwardIterator
4010 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4012 // concept requirements
4013 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4014 __glibcxx_function_requires(_EqualityComparableConcept
<
4015 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4016 __glibcxx_requires_valid_range(__first
, __last
);
4018 return std::__adjacent_find(__first
, __last
,
4019 __gnu_cxx::__ops::__iter_equal_to_iter());
4023 * @brief Find two adjacent values in a sequence using a predicate.
4024 * @ingroup non_mutating_algorithms
4025 * @param __first A forward iterator.
4026 * @param __last A forward iterator.
4027 * @param __binary_pred A binary predicate.
4028 * @return The first iterator @c i such that @c i and @c i+1 are both
4029 * valid iterators in @p [__first,__last) and such that
4030 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4033 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4034 _GLIBCXX20_CONSTEXPR
4035 inline _ForwardIterator
4036 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4037 _BinaryPredicate __binary_pred
)
4039 // concept requirements
4040 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4041 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4042 typename iterator_traits
<_ForwardIterator
>::value_type
,
4043 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4044 __glibcxx_requires_valid_range(__first
, __last
);
4046 return std::__adjacent_find(__first
, __last
,
4047 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4051 * @brief Count the number of copies of a value in a sequence.
4052 * @ingroup non_mutating_algorithms
4053 * @param __first An input iterator.
4054 * @param __last An input iterator.
4055 * @param __value The value to be counted.
4056 * @return The number of iterators @c i in the range @p [__first,__last)
4057 * for which @c *i == @p __value
4059 template<typename _InputIterator
, typename _Tp
>
4060 _GLIBCXX20_CONSTEXPR
4061 inline typename iterator_traits
<_InputIterator
>::difference_type
4062 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4064 // concept requirements
4065 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4066 __glibcxx_function_requires(_EqualOpConcept
<
4067 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4068 __glibcxx_requires_valid_range(__first
, __last
);
4070 return std::__count_if(__first
, __last
,
4071 __gnu_cxx::__ops::__iter_equals_val(__value
));
4075 * @brief Count the elements of a sequence for which a predicate is true.
4076 * @ingroup non_mutating_algorithms
4077 * @param __first An input iterator.
4078 * @param __last An input iterator.
4079 * @param __pred A predicate.
4080 * @return The number of iterators @c i in the range @p [__first,__last)
4081 * for which @p __pred(*i) is true.
4083 template<typename _InputIterator
, typename _Predicate
>
4084 _GLIBCXX20_CONSTEXPR
4085 inline typename iterator_traits
<_InputIterator
>::difference_type
4086 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4088 // concept requirements
4089 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4090 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4091 typename iterator_traits
<_InputIterator
>::value_type
>)
4092 __glibcxx_requires_valid_range(__first
, __last
);
4094 return std::__count_if(__first
, __last
,
4095 __gnu_cxx::__ops::__pred_iter(__pred
));
4099 * @brief Search a sequence for a matching sub-sequence.
4100 * @ingroup non_mutating_algorithms
4101 * @param __first1 A forward iterator.
4102 * @param __last1 A forward iterator.
4103 * @param __first2 A forward iterator.
4104 * @param __last2 A forward iterator.
4105 * @return The first iterator @c i in the range @p
4106 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4107 * *(__first2+N) for each @c N in the range @p
4108 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4110 * Searches the range @p [__first1,__last1) for a sub-sequence that
4111 * compares equal value-by-value with the sequence given by @p
4112 * [__first2,__last2) and returns an iterator to the first element
4113 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4116 * Because the sub-sequence must lie completely within the range @p
4117 * [__first1,__last1) it must start at a position less than @p
4118 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4119 * length of the sub-sequence.
4121 * This means that the returned iterator @c i will be in the range
4122 * @p [__first1,__last1-(__last2-__first2))
4124 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4125 _GLIBCXX20_CONSTEXPR
4126 inline _ForwardIterator1
4127 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4128 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4130 // concept requirements
4131 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4132 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4133 __glibcxx_function_requires(_EqualOpConcept
<
4134 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4135 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4136 __glibcxx_requires_valid_range(__first1
, __last1
);
4137 __glibcxx_requires_valid_range(__first2
, __last2
);
4139 return std::__search(__first1
, __last1
, __first2
, __last2
,
4140 __gnu_cxx::__ops::__iter_equal_to_iter());
4144 * @brief Search a sequence for a matching sub-sequence using a predicate.
4145 * @ingroup non_mutating_algorithms
4146 * @param __first1 A forward iterator.
4147 * @param __last1 A forward iterator.
4148 * @param __first2 A forward iterator.
4149 * @param __last2 A forward iterator.
4150 * @param __predicate A binary predicate.
4151 * @return The first iterator @c i in the range
4152 * @p [__first1,__last1-(__last2-__first2)) such that
4153 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4154 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4156 * Searches the range @p [__first1,__last1) for a sub-sequence that
4157 * compares equal value-by-value with the sequence given by @p
4158 * [__first2,__last2), using @p __predicate to determine equality,
4159 * and returns an iterator to the first element of the
4160 * sub-sequence, or @p __last1 if no such iterator exists.
4162 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4164 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4165 typename _BinaryPredicate
>
4166 _GLIBCXX20_CONSTEXPR
4167 inline _ForwardIterator1
4168 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4169 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4170 _BinaryPredicate __predicate
)
4172 // concept requirements
4173 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4174 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4175 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4176 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4177 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4178 __glibcxx_requires_valid_range(__first1
, __last1
);
4179 __glibcxx_requires_valid_range(__first2
, __last2
);
4181 return std::__search(__first1
, __last1
, __first2
, __last2
,
4182 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4186 * @brief Search a sequence for a number of consecutive values.
4187 * @ingroup non_mutating_algorithms
4188 * @param __first A forward iterator.
4189 * @param __last A forward iterator.
4190 * @param __count The number of consecutive values.
4191 * @param __val The value to find.
4192 * @return The first iterator @c i in the range @p
4193 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4194 * each @c N in the range @p [0,__count), or @p __last if no such
4197 * Searches the range @p [__first,__last) for @p count consecutive elements
4198 * equal to @p __val.
4200 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4201 _GLIBCXX20_CONSTEXPR
4202 inline _ForwardIterator
4203 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4204 _Integer __count
, const _Tp
& __val
)
4206 // concept requirements
4207 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4208 __glibcxx_function_requires(_EqualOpConcept
<
4209 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4210 __glibcxx_requires_valid_range(__first
, __last
);
4212 return std::__search_n(__first
, __last
, __count
,
4213 __gnu_cxx::__ops::__iter_equals_val(__val
));
4218 * @brief Search a sequence for a number of consecutive values using a
4220 * @ingroup non_mutating_algorithms
4221 * @param __first A forward iterator.
4222 * @param __last A forward iterator.
4223 * @param __count The number of consecutive values.
4224 * @param __val The value to find.
4225 * @param __binary_pred A binary predicate.
4226 * @return The first iterator @c i in the range @p
4227 * [__first,__last-__count) such that @p
4228 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4229 * @p [0,__count), or @p __last if no such iterator exists.
4231 * Searches the range @p [__first,__last) for @p __count
4232 * consecutive elements for which the predicate returns true.
4234 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4235 typename _BinaryPredicate
>
4236 _GLIBCXX20_CONSTEXPR
4237 inline _ForwardIterator
4238 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4239 _Integer __count
, const _Tp
& __val
,
4240 _BinaryPredicate __binary_pred
)
4242 // concept requirements
4243 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4244 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4245 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4246 __glibcxx_requires_valid_range(__first
, __last
);
4248 return std::__search_n(__first
, __last
, __count
,
4249 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4252 #if __cplusplus >= 201703L
4253 /** @brief Search a sequence using a Searcher object.
4255 * @param __first A forward iterator.
4256 * @param __last A forward iterator.
4257 * @param __searcher A callable object.
4258 * @return @p __searcher(__first,__last).first
4260 template<typename _ForwardIterator
, typename _Searcher
>
4261 _GLIBCXX20_CONSTEXPR
4262 inline _ForwardIterator
4263 search(_ForwardIterator __first
, _ForwardIterator __last
,
4264 const _Searcher
& __searcher
)
4265 { return __searcher(__first
, __last
).first
; }
4269 * @brief Perform an operation on a sequence.
4270 * @ingroup mutating_algorithms
4271 * @param __first An input iterator.
4272 * @param __last An input iterator.
4273 * @param __result An output iterator.
4274 * @param __unary_op A unary operator.
4275 * @return An output iterator equal to @p __result+(__last-__first).
4277 * Applies the operator to each element in the input range and assigns
4278 * the results to successive elements of the output sequence.
4279 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4280 * range @p [0,__last-__first).
4282 * @p unary_op must not alter its argument.
4284 template<typename _InputIterator
, typename _OutputIterator
,
4285 typename _UnaryOperation
>
4286 _GLIBCXX20_CONSTEXPR
4288 transform(_InputIterator __first
, _InputIterator __last
,
4289 _OutputIterator __result
, _UnaryOperation __unary_op
)
4291 // concept requirements
4292 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4293 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4294 // "the type returned by a _UnaryOperation"
4295 __typeof__(__unary_op(*__first
))>)
4296 __glibcxx_requires_valid_range(__first
, __last
);
4298 for (; __first
!= __last
; ++__first
, (void)++__result
)
4299 *__result
= __unary_op(*__first
);
4304 * @brief Perform an operation on corresponding elements of two sequences.
4305 * @ingroup mutating_algorithms
4306 * @param __first1 An input iterator.
4307 * @param __last1 An input iterator.
4308 * @param __first2 An input iterator.
4309 * @param __result An output iterator.
4310 * @param __binary_op A binary operator.
4311 * @return An output iterator equal to @p result+(last-first).
4313 * Applies the operator to the corresponding elements in the two
4314 * input ranges and assigns the results to successive elements of the
4317 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4318 * @c N in the range @p [0,__last1-__first1).
4320 * @p binary_op must not alter either of its arguments.
4322 template<typename _InputIterator1
, typename _InputIterator2
,
4323 typename _OutputIterator
, typename _BinaryOperation
>
4324 _GLIBCXX20_CONSTEXPR
4326 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4327 _InputIterator2 __first2
, _OutputIterator __result
,
4328 _BinaryOperation __binary_op
)
4330 // concept requirements
4331 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4332 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4333 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4334 // "the type returned by a _BinaryOperation"
4335 __typeof__(__binary_op(*__first1
,*__first2
))>)
4336 __glibcxx_requires_valid_range(__first1
, __last1
);
4338 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4339 *__result
= __binary_op(*__first1
, *__first2
);
4344 * @brief Replace each occurrence of one value in a sequence with another
4346 * @ingroup mutating_algorithms
4347 * @param __first A forward iterator.
4348 * @param __last A forward iterator.
4349 * @param __old_value The value to be replaced.
4350 * @param __new_value The replacement value.
4351 * @return replace() returns no value.
4353 * For each iterator `i` in the range `[__first,__last)` if
4354 * `*i == __old_value` then the assignment `*i = __new_value` is performed.
4356 template<typename _ForwardIterator
, typename _Tp
>
4357 _GLIBCXX20_CONSTEXPR
4359 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4360 const _Tp
& __old_value
, const _Tp
& __new_value
)
4362 // concept requirements
4363 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4365 __glibcxx_function_requires(_EqualOpConcept
<
4366 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4367 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4368 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4369 __glibcxx_requires_valid_range(__first
, __last
);
4371 for (; __first
!= __last
; ++__first
)
4372 if (*__first
== __old_value
)
4373 *__first
= __new_value
;
4377 * @brief Replace each value in a sequence for which a predicate returns
4378 * true with another value.
4379 * @ingroup mutating_algorithms
4380 * @param __first A forward iterator.
4381 * @param __last A forward iterator.
4382 * @param __pred A predicate.
4383 * @param __new_value The replacement value.
4384 * @return replace_if() returns no value.
4386 * For each iterator `i` in the range `[__first,__last)` if `__pred(*i)`
4387 * is true then the assignment `*i = __new_value` is performed.
4389 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4390 _GLIBCXX20_CONSTEXPR
4392 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4393 _Predicate __pred
, const _Tp
& __new_value
)
4395 // concept requirements
4396 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4398 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4399 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4400 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4401 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4402 __glibcxx_requires_valid_range(__first
, __last
);
4404 for (; __first
!= __last
; ++__first
)
4405 if (__pred(*__first
))
4406 *__first
= __new_value
;
4410 * @brief Assign the result of a function object to each value in a
4412 * @ingroup mutating_algorithms
4413 * @param __first A forward iterator.
4414 * @param __last A forward iterator.
4415 * @param __gen A function object callable with no arguments.
4416 * @return generate() returns no value.
4418 * Performs the assignment `*i = __gen()` for each `i` in the range
4419 * `[__first, __last)`.
4421 template<typename _ForwardIterator
, typename _Generator
>
4422 _GLIBCXX20_CONSTEXPR
4424 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4427 // concept requirements
4428 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4429 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4430 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4431 __glibcxx_requires_valid_range(__first
, __last
);
4433 for (; __first
!= __last
; ++__first
)
4438 * @brief Assign the result of a function object to each value in a
4440 * @ingroup mutating_algorithms
4441 * @param __first A forward iterator.
4442 * @param __n The length of the sequence.
4443 * @param __gen A function object callable with no arguments.
4444 * @return The end of the sequence, i.e., `__first + __n`
4446 * Performs the assignment `*i = __gen()` for each `i` in the range
4447 * `[__first, __first + __n)`.
4449 * If `__n` is negative, the function does nothing and returns `__first`.
4451 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4452 // DR 865. More algorithms that throw away information
4453 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4454 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4455 _GLIBCXX20_CONSTEXPR
4457 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4459 // concept requirements
4460 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4461 // "the type returned by a _Generator"
4462 __typeof__(__gen())>)
4464 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4465 for (_IntSize __niter
= std::__size_to_integer(__n
);
4466 __niter
> 0; --__niter
, (void) ++__first
)
4472 * @brief Copy a sequence, removing consecutive duplicate values.
4473 * @ingroup mutating_algorithms
4474 * @param __first An input iterator.
4475 * @param __last An input iterator.
4476 * @param __result An output iterator.
4477 * @return An iterator designating the end of the resulting sequence.
4479 * Copies each element in the range `[__first, __last)` to the range
4480 * beginning at `__result`, except that only the first element is copied
4481 * from groups of consecutive elements that compare equal.
4482 * `unique_copy()` is stable, so the relative order of elements that are
4483 * copied is unchanged.
4485 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4486 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4487 // DR 538. 241 again: Does unique_copy() require CopyConstructible and
4489 template<typename _InputIterator
, typename _OutputIterator
>
4490 _GLIBCXX20_CONSTEXPR
4491 inline _OutputIterator
4492 unique_copy(_InputIterator __first
, _InputIterator __last
,
4493 _OutputIterator __result
)
4495 // concept requirements
4496 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4497 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4498 typename iterator_traits
<_InputIterator
>::value_type
>)
4499 __glibcxx_function_requires(_EqualityComparableConcept
<
4500 typename iterator_traits
<_InputIterator
>::value_type
>)
4501 __glibcxx_requires_valid_range(__first
, __last
);
4503 if (__first
== __last
)
4505 return std::__unique_copy(__first
, __last
, __result
,
4506 __gnu_cxx::__ops::__iter_equal_to_iter(),
4507 std::__iterator_category(__first
),
4508 std::__iterator_category(__result
));
4512 * @brief Copy a sequence, removing consecutive values using a predicate.
4513 * @ingroup mutating_algorithms
4514 * @param __first An input iterator.
4515 * @param __last An input iterator.
4516 * @param __result An output iterator.
4517 * @param __binary_pred A binary predicate.
4518 * @return An iterator designating the end of the resulting sequence.
4520 * Copies each element in the range `[__first, __last)` to the range
4521 * beginning at `__result`, except that only the first element is copied
4522 * from groups of consecutive elements for which `__binary_pred` returns
4524 * `unique_copy()` is stable, so the relative order of elements that are
4525 * copied is unchanged.
4527 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4528 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4529 template<typename _InputIterator
, typename _OutputIterator
,
4530 typename _BinaryPredicate
>
4531 _GLIBCXX20_CONSTEXPR
4532 inline _OutputIterator
4533 unique_copy(_InputIterator __first
, _InputIterator __last
,
4534 _OutputIterator __result
,
4535 _BinaryPredicate __binary_pred
)
4537 // concept requirements -- predicates checked later
4538 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4539 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4540 typename iterator_traits
<_InputIterator
>::value_type
>)
4541 __glibcxx_requires_valid_range(__first
, __last
);
4543 if (__first
== __last
)
4545 return std::__unique_copy(__first
, __last
, __result
,
4546 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4547 std::__iterator_category(__first
),
4548 std::__iterator_category(__result
));
4551 #if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4554 * @brief Randomly shuffle the elements of a sequence.
4555 * @ingroup mutating_algorithms
4556 * @param __first A forward iterator.
4557 * @param __last A forward iterator.
4560 * Reorder the elements in the range `[__first, __last)` using a random
4561 * distribution, so that every possible ordering of the sequence is
4565 * Since C++14 `std::random_shuffle` is not part of the C++ standard.
4566 * Use `std::shuffle` instead, which was introduced in C++11.
4568 template<typename _RandomAccessIterator
>
4569 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4571 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4573 // concept requirements
4574 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4575 _RandomAccessIterator
>)
4576 __glibcxx_requires_valid_range(__first
, __last
);
4578 if (__first
!= __last
)
4579 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4581 // XXX rand() % N is not uniformly distributed
4582 _RandomAccessIterator __j
= __first
4583 + std::rand() % ((__i
- __first
) + 1);
4585 std::iter_swap(__i
, __j
);
4591 * @brief Shuffle the elements of a sequence using a random number
4593 * @ingroup mutating_algorithms
4594 * @param __first A forward iterator.
4595 * @param __last A forward iterator.
4596 * @param __rand The RNG functor or function.
4599 * Reorders the elements in the range `[__first, __last)` using `__rand`
4600 * to provide a random distribution. Calling `__rand(N)` for a positive
4601 * integer `N` should return a randomly chosen integer from the
4605 * Since C++14 `std::random_shuffle` is not part of the C++ standard.
4606 * Use `std::shuffle` instead, which was introduced in C++11.
4608 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4609 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4611 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4612 #if __cplusplus >= 201103L
4613 _RandomNumberGenerator
&& __rand
)
4615 _RandomNumberGenerator
& __rand
)
4618 // concept requirements
4619 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4620 _RandomAccessIterator
>)
4621 __glibcxx_requires_valid_range(__first
, __last
);
4623 if (__first
== __last
)
4625 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4627 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4629 std::iter_swap(__i
, __j
);
4632 #endif // C++11 || USE_DEPRECATED
4635 * @brief Move elements for which a predicate is true to the beginning
4637 * @ingroup mutating_algorithms
4638 * @param __first A forward iterator.
4639 * @param __last A forward iterator.
4640 * @param __pred A predicate functor.
4641 * @return An iterator `middle` such that `__pred(i)` is true for each
4642 * iterator `i` in the range `[__first, middle)` and false for each `i`
4643 * in the range `[middle, __last)`.
4645 * `__pred` must not modify its operand. `partition()` does not preserve
4646 * the relative ordering of elements in each group, use
4647 * `stable_partition()` if this is needed.
4649 template<typename _ForwardIterator
, typename _Predicate
>
4650 _GLIBCXX20_CONSTEXPR
4651 inline _ForwardIterator
4652 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4655 // concept requirements
4656 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4658 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4659 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4660 __glibcxx_requires_valid_range(__first
, __last
);
4662 return std::__partition(__first
, __last
, __pred
,
4663 std::__iterator_category(__first
));
4668 * @brief Sort the smallest elements of a sequence.
4669 * @ingroup sorting_algorithms
4670 * @param __first An iterator.
4671 * @param __middle Another iterator.
4672 * @param __last Another iterator.
4675 * Sorts the smallest `(__middle - __first)` elements in the range
4676 * `[first, last)` and moves them to the range `[__first, __middle)`. The
4677 * order of the remaining elements in the range `[__middle, __last)` is
4679 * After the sort if `i` and `j` are iterators in the range
4680 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4681 * in the range `[__middle, __last)` then `*j < *i` and `*k < *i` are
4684 template<typename _RandomAccessIterator
>
4685 _GLIBCXX20_CONSTEXPR
4687 partial_sort(_RandomAccessIterator __first
,
4688 _RandomAccessIterator __middle
,
4689 _RandomAccessIterator __last
)
4691 // concept requirements
4692 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4693 _RandomAccessIterator
>)
4694 __glibcxx_function_requires(_LessThanComparableConcept
<
4695 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4696 __glibcxx_requires_valid_range(__first
, __middle
);
4697 __glibcxx_requires_valid_range(__middle
, __last
);
4698 __glibcxx_requires_irreflexive(__first
, __last
);
4700 std::__partial_sort(__first
, __middle
, __last
,
4701 __gnu_cxx::__ops::__iter_less_iter());
4705 * @brief Sort the smallest elements of a sequence using a predicate
4707 * @ingroup sorting_algorithms
4708 * @param __first An iterator.
4709 * @param __middle Another iterator.
4710 * @param __last Another iterator.
4711 * @param __comp A comparison functor.
4714 * Sorts the smallest `(__middle - __first)` elements in the range
4715 * `[__first, __last)` and moves them to the range `[__first, __middle)`.
4716 * The order of the remaining elements in the range `[__middle, __last)` is
4718 * After the sort if `i` and `j` are iterators in the range
4719 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4720 * in the range `[__middle, __last)` then `*__comp(j, *i)` and
4721 * `__comp(*k, *i)` are both false.
4723 template<typename _RandomAccessIterator
, typename _Compare
>
4724 _GLIBCXX20_CONSTEXPR
4726 partial_sort(_RandomAccessIterator __first
,
4727 _RandomAccessIterator __middle
,
4728 _RandomAccessIterator __last
,
4731 // concept requirements
4732 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4733 _RandomAccessIterator
>)
4734 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4735 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4736 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4737 __glibcxx_requires_valid_range(__first
, __middle
);
4738 __glibcxx_requires_valid_range(__middle
, __last
);
4739 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4741 std::__partial_sort(__first
, __middle
, __last
,
4742 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4746 * @brief Sort a sequence just enough to find a particular position.
4747 * @ingroup sorting_algorithms
4748 * @param __first An iterator.
4749 * @param __nth Another iterator.
4750 * @param __last Another iterator.
4753 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4754 * is the same element that would have been in that position had the
4755 * whole sequence been sorted. The elements either side of `*__nth` are
4756 * not completely sorted, but for any iterator `i` in the range
4757 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)` it
4758 * holds that `*j < *i` is false.
4760 template<typename _RandomAccessIterator
>
4761 _GLIBCXX20_CONSTEXPR
4763 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4764 _RandomAccessIterator __last
)
4766 // concept requirements
4767 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4768 _RandomAccessIterator
>)
4769 __glibcxx_function_requires(_LessThanComparableConcept
<
4770 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4771 __glibcxx_requires_valid_range(__first
, __nth
);
4772 __glibcxx_requires_valid_range(__nth
, __last
);
4773 __glibcxx_requires_irreflexive(__first
, __last
);
4775 if (__first
== __last
|| __nth
== __last
)
4778 std::__introselect(__first
, __nth
, __last
,
4779 std::__lg(__last
- __first
) * 2,
4780 __gnu_cxx::__ops::__iter_less_iter());
4784 * @brief Sort a sequence just enough to find a particular position
4785 * using a predicate for comparison.
4786 * @ingroup sorting_algorithms
4787 * @param __first An iterator.
4788 * @param __nth Another iterator.
4789 * @param __last Another iterator.
4790 * @param __comp A comparison functor.
4793 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4794 * is the same element that would have been in that position had the
4795 * whole sequence been sorted. The elements either side of `*__nth` are
4796 * not completely sorted, but for any iterator `i` in the range
4797 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)`
4798 * it holds that `__comp(*j, *i)` is false.
4800 template<typename _RandomAccessIterator
, typename _Compare
>
4801 _GLIBCXX20_CONSTEXPR
4803 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4804 _RandomAccessIterator __last
, _Compare __comp
)
4806 // concept requirements
4807 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4808 _RandomAccessIterator
>)
4809 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4810 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4811 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4812 __glibcxx_requires_valid_range(__first
, __nth
);
4813 __glibcxx_requires_valid_range(__nth
, __last
);
4814 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4816 if (__first
== __last
|| __nth
== __last
)
4819 std::__introselect(__first
, __nth
, __last
,
4820 std::__lg(__last
- __first
) * 2,
4821 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4825 * @brief Sort the elements of a sequence.
4826 * @ingroup sorting_algorithms
4827 * @param __first An iterator.
4828 * @param __last Another iterator.
4831 * Sorts the elements in the range `[__first, __last)` in ascending order,
4832 * such that for each iterator `i` in the range `[__first, __last - 1)`,
4833 * `*(i+1) < *i` is false.
4835 * The relative ordering of equivalent elements is not preserved, use
4836 * `stable_sort()` if this is needed.
4838 template<typename _RandomAccessIterator
>
4839 _GLIBCXX20_CONSTEXPR
4841 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4843 // concept requirements
4844 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4845 _RandomAccessIterator
>)
4846 __glibcxx_function_requires(_LessThanComparableConcept
<
4847 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4848 __glibcxx_requires_valid_range(__first
, __last
);
4849 __glibcxx_requires_irreflexive(__first
, __last
);
4851 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4855 * @brief Sort the elements of a sequence using a predicate for comparison.
4856 * @ingroup sorting_algorithms
4857 * @param __first An iterator.
4858 * @param __last Another iterator.
4859 * @param __comp A comparison functor.
4862 * Sorts the elements in the range `[__first, __last)` in ascending order,
4863 * such that `__comp(*(i+1), *i)` is false for every iterator `i` in the
4864 * range `[__first, __last - 1)`.
4866 * The relative ordering of equivalent elements is not preserved, use
4867 * `stable_sort()` if this is needed.
4869 template<typename _RandomAccessIterator
, typename _Compare
>
4870 _GLIBCXX20_CONSTEXPR
4872 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4875 // concept requirements
4876 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4877 _RandomAccessIterator
>)
4878 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4879 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4880 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4881 __glibcxx_requires_valid_range(__first
, __last
);
4882 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4884 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4887 template<typename _InputIterator1
, typename _InputIterator2
,
4888 typename _OutputIterator
, typename _Compare
>
4889 _GLIBCXX20_CONSTEXPR
4891 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4892 _InputIterator2 __first2
, _InputIterator2 __last2
,
4893 _OutputIterator __result
, _Compare __comp
)
4895 while (__first1
!= __last1
&& __first2
!= __last2
)
4897 if (__comp(__first2
, __first1
))
4899 *__result
= *__first2
;
4904 *__result
= *__first1
;
4909 return std::copy(__first2
, __last2
,
4910 std::copy(__first1
, __last1
, __result
));
4914 * @brief Merges two sorted ranges.
4915 * @ingroup sorting_algorithms
4916 * @param __first1 An iterator.
4917 * @param __first2 Another iterator.
4918 * @param __last1 Another iterator.
4919 * @param __last2 Another iterator.
4920 * @param __result An iterator pointing to the end of the merged range.
4921 * @return An output iterator equal to @p __result + (__last1 - __first1)
4922 * + (__last2 - __first2).
4924 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4925 * the sorted range @p [__result, __result + (__last1-__first1) +
4926 * (__last2-__first2)). Both input ranges must be sorted, and the
4927 * output range must not overlap with either of the input ranges.
4928 * The sort is @e stable, that is, for equivalent elements in the
4929 * two ranges, elements from the first range will always come
4930 * before elements from the second.
4932 template<typename _InputIterator1
, typename _InputIterator2
,
4933 typename _OutputIterator
>
4934 _GLIBCXX20_CONSTEXPR
4935 inline _OutputIterator
4936 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4937 _InputIterator2 __first2
, _InputIterator2 __last2
,
4938 _OutputIterator __result
)
4940 // concept requirements
4941 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4942 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4943 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4944 typename iterator_traits
<_InputIterator1
>::value_type
>)
4945 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4946 typename iterator_traits
<_InputIterator2
>::value_type
>)
4947 __glibcxx_function_requires(_LessThanOpConcept
<
4948 typename iterator_traits
<_InputIterator2
>::value_type
,
4949 typename iterator_traits
<_InputIterator1
>::value_type
>)
4950 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4951 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4952 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4953 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4955 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4956 __first2
, __last2
, __result
,
4957 __gnu_cxx::__ops::__iter_less_iter());
4961 * @brief Merges two sorted ranges.
4962 * @ingroup sorting_algorithms
4963 * @param __first1 An iterator.
4964 * @param __first2 Another iterator.
4965 * @param __last1 Another iterator.
4966 * @param __last2 Another iterator.
4967 * @param __result An iterator pointing to the end of the merged range.
4968 * @param __comp A functor to use for comparisons.
4969 * @return An output iterator equal to @p __result + (__last1 - __first1)
4970 * + (__last2 - __first2).
4972 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4973 * the sorted range @p [__result, __result + (__last1-__first1) +
4974 * (__last2-__first2)). Both input ranges must be sorted, and the
4975 * output range must not overlap with either of the input ranges.
4976 * The sort is @e stable, that is, for equivalent elements in the
4977 * two ranges, elements from the first range will always come
4978 * before elements from the second.
4980 * The comparison function should have the same effects on ordering as
4981 * the function used for the initial sort.
4983 template<typename _InputIterator1
, typename _InputIterator2
,
4984 typename _OutputIterator
, typename _Compare
>
4985 _GLIBCXX20_CONSTEXPR
4986 inline _OutputIterator
4987 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4988 _InputIterator2 __first2
, _InputIterator2 __last2
,
4989 _OutputIterator __result
, _Compare __comp
)
4991 // concept requirements
4992 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4993 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4994 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4995 typename iterator_traits
<_InputIterator1
>::value_type
>)
4996 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4997 typename iterator_traits
<_InputIterator2
>::value_type
>)
4998 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4999 typename iterator_traits
<_InputIterator2
>::value_type
,
5000 typename iterator_traits
<_InputIterator1
>::value_type
>)
5001 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5002 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5003 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5004 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5006 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5007 __first2
, __last2
, __result
,
5008 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5011 template<typename _RandomAccessIterator
, typename _Compare
>
5013 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5016 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5018 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5020 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5022 if (__first
== __last
)
5025 // __stable_sort_adaptive sorts the range in two halves,
5026 // so the buffer only needs to fit half the range at once.
5027 _TmpBuf
__buf(__first
, (__last
- __first
+ 1) / 2);
5029 if (__builtin_expect(__buf
.requested_size() == __buf
.size(), true))
5030 std::__stable_sort_adaptive(__first
,
5031 __first
+ _DistanceType(__buf
.size()),
5032 __last
, __buf
.begin(), __comp
);
5033 else if (__builtin_expect(__buf
.begin() == 0, false))
5034 std::__inplace_stable_sort(__first
, __last
, __comp
);
5036 std::__stable_sort_adaptive_resize(__first
, __last
, __buf
.begin(),
5037 _DistanceType(__buf
.size()), __comp
);
5041 * @brief Sort the elements of a sequence, preserving the relative order
5042 * of equivalent elements.
5043 * @ingroup sorting_algorithms
5044 * @param __first An iterator.
5045 * @param __last Another iterator.
5048 * Sorts the elements in the range @p [__first,__last) in ascending order,
5049 * such that for each iterator @p i in the range @p [__first,__last-1),
5050 * @p *(i+1)<*i is false.
5052 * The relative ordering of equivalent elements is preserved, so any two
5053 * elements @p x and @p y in the range @p [__first,__last) such that
5054 * @p x<y is false and @p y<x is false will have the same relative
5055 * ordering after calling @p stable_sort().
5057 template<typename _RandomAccessIterator
>
5059 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5061 // concept requirements
5062 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5063 _RandomAccessIterator
>)
5064 __glibcxx_function_requires(_LessThanComparableConcept
<
5065 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5066 __glibcxx_requires_valid_range(__first
, __last
);
5067 __glibcxx_requires_irreflexive(__first
, __last
);
5069 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5070 __gnu_cxx::__ops::__iter_less_iter());
5074 * @brief Sort the elements of a sequence using a predicate for comparison,
5075 * preserving the relative order of equivalent elements.
5076 * @ingroup sorting_algorithms
5077 * @param __first An iterator.
5078 * @param __last Another iterator.
5079 * @param __comp A comparison functor.
5082 * Sorts the elements in the range @p [__first,__last) in ascending order,
5083 * such that for each iterator @p i in the range @p [__first,__last-1),
5084 * @p __comp(*(i+1),*i) is false.
5086 * The relative ordering of equivalent elements is preserved, so any two
5087 * elements @p x and @p y in the range @p [__first,__last) such that
5088 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5089 * relative ordering after calling @p stable_sort().
5091 template<typename _RandomAccessIterator
, typename _Compare
>
5093 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5096 // concept requirements
5097 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5098 _RandomAccessIterator
>)
5099 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5100 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5101 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5102 __glibcxx_requires_valid_range(__first
, __last
);
5103 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5105 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5106 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5109 template<typename _InputIterator1
, typename _InputIterator2
,
5110 typename _OutputIterator
,
5112 _GLIBCXX20_CONSTEXPR
5114 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5115 _InputIterator2 __first2
, _InputIterator2 __last2
,
5116 _OutputIterator __result
, _Compare __comp
)
5118 while (__first1
!= __last1
&& __first2
!= __last2
)
5120 if (__comp(__first1
, __first2
))
5122 *__result
= *__first1
;
5125 else if (__comp(__first2
, __first1
))
5127 *__result
= *__first2
;
5132 *__result
= *__first1
;
5138 return std::copy(__first2
, __last2
,
5139 std::copy(__first1
, __last1
, __result
));
5143 * @brief Return the union of two sorted ranges.
5144 * @ingroup set_algorithms
5145 * @param __first1 Start of first range.
5146 * @param __last1 End of first range.
5147 * @param __first2 Start of second range.
5148 * @param __last2 End of second range.
5149 * @param __result Start of output range.
5150 * @return End of the output range.
5151 * @ingroup set_algorithms
5153 * This operation iterates over both ranges, copying elements present in
5154 * each range in order to the output range. Iterators increment for each
5155 * range. When the current element of one range is less than the other,
5156 * that element is copied and the iterator advanced. If an element is
5157 * contained in both ranges, the element from the first range is copied and
5158 * both ranges advance. The output range may not overlap either input
5161 template<typename _InputIterator1
, typename _InputIterator2
,
5162 typename _OutputIterator
>
5163 _GLIBCXX20_CONSTEXPR
5164 inline _OutputIterator
5165 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5166 _InputIterator2 __first2
, _InputIterator2 __last2
,
5167 _OutputIterator __result
)
5169 // concept requirements
5170 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5171 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5172 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5173 typename iterator_traits
<_InputIterator1
>::value_type
>)
5174 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5175 typename iterator_traits
<_InputIterator2
>::value_type
>)
5176 __glibcxx_function_requires(_LessThanOpConcept
<
5177 typename iterator_traits
<_InputIterator1
>::value_type
,
5178 typename iterator_traits
<_InputIterator2
>::value_type
>)
5179 __glibcxx_function_requires(_LessThanOpConcept
<
5180 typename iterator_traits
<_InputIterator2
>::value_type
,
5181 typename iterator_traits
<_InputIterator1
>::value_type
>)
5182 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5183 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5184 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5185 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5187 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5188 __first2
, __last2
, __result
,
5189 __gnu_cxx::__ops::__iter_less_iter());
5193 * @brief Return the union of two sorted ranges using a comparison functor.
5194 * @ingroup set_algorithms
5195 * @param __first1 Start of first range.
5196 * @param __last1 End of first range.
5197 * @param __first2 Start of second range.
5198 * @param __last2 End of second range.
5199 * @param __result Start of output range.
5200 * @param __comp The comparison functor.
5201 * @return End of the output range.
5202 * @ingroup set_algorithms
5204 * This operation iterates over both ranges, copying elements present in
5205 * each range in order to the output range. Iterators increment for each
5206 * range. When the current element of one range is less than the other
5207 * according to @p __comp, that element is copied and the iterator advanced.
5208 * If an equivalent element according to @p __comp is contained in both
5209 * ranges, the element from the first range is copied and both ranges
5210 * advance. The output range may not overlap either input range.
5212 template<typename _InputIterator1
, typename _InputIterator2
,
5213 typename _OutputIterator
, typename _Compare
>
5214 _GLIBCXX20_CONSTEXPR
5215 inline _OutputIterator
5216 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5217 _InputIterator2 __first2
, _InputIterator2 __last2
,
5218 _OutputIterator __result
, _Compare __comp
)
5220 // concept requirements
5221 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5222 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5223 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5224 typename iterator_traits
<_InputIterator1
>::value_type
>)
5225 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5226 typename iterator_traits
<_InputIterator2
>::value_type
>)
5227 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5228 typename iterator_traits
<_InputIterator1
>::value_type
,
5229 typename iterator_traits
<_InputIterator2
>::value_type
>)
5230 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5231 typename iterator_traits
<_InputIterator2
>::value_type
,
5232 typename iterator_traits
<_InputIterator1
>::value_type
>)
5233 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5234 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5235 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5236 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5238 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5239 __first2
, __last2
, __result
,
5240 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5243 template<typename _InputIterator1
, typename _InputIterator2
,
5244 typename _OutputIterator
,
5246 _GLIBCXX20_CONSTEXPR
5248 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5249 _InputIterator2 __first2
, _InputIterator2 __last2
,
5250 _OutputIterator __result
, _Compare __comp
)
5252 while (__first1
!= __last1
&& __first2
!= __last2
)
5253 if (__comp(__first1
, __first2
))
5255 else if (__comp(__first2
, __first1
))
5259 *__result
= *__first1
;
5268 * @brief Return the intersection of two sorted ranges.
5269 * @ingroup set_algorithms
5270 * @param __first1 Start of first range.
5271 * @param __last1 End of first range.
5272 * @param __first2 Start of second range.
5273 * @param __last2 End of second range.
5274 * @param __result Start of output range.
5275 * @return End of the output range.
5276 * @ingroup set_algorithms
5278 * This operation iterates over both ranges, copying elements present in
5279 * both ranges in order to the output range. Iterators increment for each
5280 * range. When the current element of one range is less than the other,
5281 * that iterator advances. If an element is contained in both ranges, the
5282 * element from the first range is copied and both ranges advance. The
5283 * output range may not overlap either input range.
5285 template<typename _InputIterator1
, typename _InputIterator2
,
5286 typename _OutputIterator
>
5287 _GLIBCXX20_CONSTEXPR
5288 inline _OutputIterator
5289 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5290 _InputIterator2 __first2
, _InputIterator2 __last2
,
5291 _OutputIterator __result
)
5293 // concept requirements
5294 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5295 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5296 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5297 typename iterator_traits
<_InputIterator1
>::value_type
>)
5298 __glibcxx_function_requires(_LessThanOpConcept
<
5299 typename iterator_traits
<_InputIterator1
>::value_type
,
5300 typename iterator_traits
<_InputIterator2
>::value_type
>)
5301 __glibcxx_function_requires(_LessThanOpConcept
<
5302 typename iterator_traits
<_InputIterator2
>::value_type
,
5303 typename iterator_traits
<_InputIterator1
>::value_type
>)
5304 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5305 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5306 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5307 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5309 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5310 __first2
, __last2
, __result
,
5311 __gnu_cxx::__ops::__iter_less_iter());
5315 * @brief Return the intersection of two sorted ranges using comparison
5317 * @ingroup set_algorithms
5318 * @param __first1 Start of first range.
5319 * @param __last1 End of first range.
5320 * @param __first2 Start of second range.
5321 * @param __last2 End of second range.
5322 * @param __result Start of output range.
5323 * @param __comp The comparison functor.
5324 * @return End of the output range.
5325 * @ingroup set_algorithms
5327 * This operation iterates over both ranges, copying elements present in
5328 * both ranges in order to the output range. Iterators increment for each
5329 * range. When the current element of one range is less than the other
5330 * according to @p __comp, that iterator advances. If an element is
5331 * contained in both ranges according to @p __comp, the element from the
5332 * first range is copied and both ranges advance. The output range may not
5333 * overlap either input range.
5335 template<typename _InputIterator1
, typename _InputIterator2
,
5336 typename _OutputIterator
, typename _Compare
>
5337 _GLIBCXX20_CONSTEXPR
5338 inline _OutputIterator
5339 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5340 _InputIterator2 __first2
, _InputIterator2 __last2
,
5341 _OutputIterator __result
, _Compare __comp
)
5343 // concept requirements
5344 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5345 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5346 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5347 typename iterator_traits
<_InputIterator1
>::value_type
>)
5348 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5349 typename iterator_traits
<_InputIterator1
>::value_type
,
5350 typename iterator_traits
<_InputIterator2
>::value_type
>)
5351 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5352 typename iterator_traits
<_InputIterator2
>::value_type
,
5353 typename iterator_traits
<_InputIterator1
>::value_type
>)
5354 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5355 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5356 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5357 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5359 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5360 __first2
, __last2
, __result
,
5361 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5364 template<typename _InputIterator1
, typename _InputIterator2
,
5365 typename _OutputIterator
,
5367 _GLIBCXX20_CONSTEXPR
5369 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5370 _InputIterator2 __first2
, _InputIterator2 __last2
,
5371 _OutputIterator __result
, _Compare __comp
)
5373 while (__first1
!= __last1
&& __first2
!= __last2
)
5374 if (__comp(__first1
, __first2
))
5376 *__result
= *__first1
;
5380 else if (__comp(__first2
, __first1
))
5387 return std::copy(__first1
, __last1
, __result
);
5391 * @brief Return the difference of two sorted ranges.
5392 * @ingroup set_algorithms
5393 * @param __first1 Start of first range.
5394 * @param __last1 End of first range.
5395 * @param __first2 Start of second range.
5396 * @param __last2 End of second range.
5397 * @param __result Start of output range.
5398 * @return End of the output range.
5399 * @ingroup set_algorithms
5401 * This operation iterates over both ranges, copying elements present in
5402 * the first range but not the second in order to the output range.
5403 * Iterators increment for each range. When the current element of the
5404 * first range is less than the second, that element is copied and the
5405 * iterator advances. If the current element of the second range is less,
5406 * the iterator advances, but no element is copied. If an element is
5407 * contained in both ranges, no elements are copied and both ranges
5408 * advance. The output range may not overlap either input range.
5410 template<typename _InputIterator1
, typename _InputIterator2
,
5411 typename _OutputIterator
>
5412 _GLIBCXX20_CONSTEXPR
5413 inline _OutputIterator
5414 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5415 _InputIterator2 __first2
, _InputIterator2 __last2
,
5416 _OutputIterator __result
)
5418 // concept requirements
5419 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5420 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5421 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5422 typename iterator_traits
<_InputIterator1
>::value_type
>)
5423 __glibcxx_function_requires(_LessThanOpConcept
<
5424 typename iterator_traits
<_InputIterator1
>::value_type
,
5425 typename iterator_traits
<_InputIterator2
>::value_type
>)
5426 __glibcxx_function_requires(_LessThanOpConcept
<
5427 typename iterator_traits
<_InputIterator2
>::value_type
,
5428 typename iterator_traits
<_InputIterator1
>::value_type
>)
5429 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5430 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5431 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5432 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5434 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5435 __first2
, __last2
, __result
,
5436 __gnu_cxx::__ops::__iter_less_iter());
5440 * @brief Return the difference of two sorted ranges using comparison
5442 * @ingroup set_algorithms
5443 * @param __first1 Start of first range.
5444 * @param __last1 End of first range.
5445 * @param __first2 Start of second range.
5446 * @param __last2 End of second range.
5447 * @param __result Start of output range.
5448 * @param __comp The comparison functor.
5449 * @return End of the output range.
5450 * @ingroup set_algorithms
5452 * This operation iterates over both ranges, copying elements present in
5453 * the first range but not the second in order to the output range.
5454 * Iterators increment for each range. When the current element of the
5455 * first range is less than the second according to @p __comp, that element
5456 * is copied and the iterator advances. If the current element of the
5457 * second range is less, no element is copied and the iterator advances.
5458 * If an element is contained in both ranges according to @p __comp, no
5459 * elements are copied and both ranges advance. The output range may not
5460 * overlap either input range.
5462 template<typename _InputIterator1
, typename _InputIterator2
,
5463 typename _OutputIterator
, typename _Compare
>
5464 _GLIBCXX20_CONSTEXPR
5465 inline _OutputIterator
5466 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5467 _InputIterator2 __first2
, _InputIterator2 __last2
,
5468 _OutputIterator __result
, _Compare __comp
)
5470 // concept requirements
5471 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5472 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5473 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5474 typename iterator_traits
<_InputIterator1
>::value_type
>)
5475 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5476 typename iterator_traits
<_InputIterator1
>::value_type
,
5477 typename iterator_traits
<_InputIterator2
>::value_type
>)
5478 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5479 typename iterator_traits
<_InputIterator2
>::value_type
,
5480 typename iterator_traits
<_InputIterator1
>::value_type
>)
5481 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5482 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5483 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5484 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5486 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5487 __first2
, __last2
, __result
,
5488 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5491 template<typename _InputIterator1
, typename _InputIterator2
,
5492 typename _OutputIterator
,
5494 _GLIBCXX20_CONSTEXPR
5496 __set_symmetric_difference(_InputIterator1 __first1
,
5497 _InputIterator1 __last1
,
5498 _InputIterator2 __first2
,
5499 _InputIterator2 __last2
,
5500 _OutputIterator __result
,
5503 while (__first1
!= __last1
&& __first2
!= __last2
)
5504 if (__comp(__first1
, __first2
))
5506 *__result
= *__first1
;
5510 else if (__comp(__first2
, __first1
))
5512 *__result
= *__first2
;
5521 return std::copy(__first2
, __last2
,
5522 std::copy(__first1
, __last1
, __result
));
5526 * @brief Return the symmetric difference of two sorted ranges.
5527 * @ingroup set_algorithms
5528 * @param __first1 Start of first range.
5529 * @param __last1 End of first range.
5530 * @param __first2 Start of second range.
5531 * @param __last2 End of second range.
5532 * @param __result Start of output range.
5533 * @return End of the output range.
5534 * @ingroup set_algorithms
5536 * This operation iterates over both ranges, copying elements present in
5537 * one range but not the other in order to the output range. Iterators
5538 * increment for each range. When the current element of one range is less
5539 * than the other, that element is copied and the iterator advances. If an
5540 * element is contained in both ranges, no elements are copied and both
5541 * ranges advance. The output range may not overlap either input range.
5543 template<typename _InputIterator1
, typename _InputIterator2
,
5544 typename _OutputIterator
>
5545 _GLIBCXX20_CONSTEXPR
5546 inline _OutputIterator
5547 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5548 _InputIterator2 __first2
, _InputIterator2 __last2
,
5549 _OutputIterator __result
)
5551 // concept requirements
5552 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5553 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5554 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5555 typename iterator_traits
<_InputIterator1
>::value_type
>)
5556 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5557 typename iterator_traits
<_InputIterator2
>::value_type
>)
5558 __glibcxx_function_requires(_LessThanOpConcept
<
5559 typename iterator_traits
<_InputIterator1
>::value_type
,
5560 typename iterator_traits
<_InputIterator2
>::value_type
>)
5561 __glibcxx_function_requires(_LessThanOpConcept
<
5562 typename iterator_traits
<_InputIterator2
>::value_type
,
5563 typename iterator_traits
<_InputIterator1
>::value_type
>)
5564 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5565 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5566 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5567 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5569 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5570 __first2
, __last2
, __result
,
5571 __gnu_cxx::__ops::__iter_less_iter());
5575 * @brief Return the symmetric difference of two sorted ranges using
5576 * comparison functor.
5577 * @ingroup set_algorithms
5578 * @param __first1 Start of first range.
5579 * @param __last1 End of first range.
5580 * @param __first2 Start of second range.
5581 * @param __last2 End of second range.
5582 * @param __result Start of output range.
5583 * @param __comp The comparison functor.
5584 * @return End of the output range.
5585 * @ingroup set_algorithms
5587 * This operation iterates over both ranges, copying elements present in
5588 * one range but not the other in order to the output range. Iterators
5589 * increment for each range. When the current element of one range is less
5590 * than the other according to @p comp, that element is copied and the
5591 * iterator advances. If an element is contained in both ranges according
5592 * to @p __comp, no elements are copied and both ranges advance. The output
5593 * range may not overlap either input range.
5595 template<typename _InputIterator1
, typename _InputIterator2
,
5596 typename _OutputIterator
, typename _Compare
>
5597 _GLIBCXX20_CONSTEXPR
5598 inline _OutputIterator
5599 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5600 _InputIterator2 __first2
, _InputIterator2 __last2
,
5601 _OutputIterator __result
,
5604 // concept requirements
5605 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5606 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5607 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5608 typename iterator_traits
<_InputIterator1
>::value_type
>)
5609 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5610 typename iterator_traits
<_InputIterator2
>::value_type
>)
5611 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5612 typename iterator_traits
<_InputIterator1
>::value_type
,
5613 typename iterator_traits
<_InputIterator2
>::value_type
>)
5614 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5615 typename iterator_traits
<_InputIterator2
>::value_type
,
5616 typename iterator_traits
<_InputIterator1
>::value_type
>)
5617 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5618 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5619 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5620 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5622 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5623 __first2
, __last2
, __result
,
5624 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5627 template<typename _ForwardIterator
, typename _Compare
>
5628 _GLIBCXX14_CONSTEXPR
5630 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5633 if (__first
== __last
)
5635 _ForwardIterator __result
= __first
;
5636 while (++__first
!= __last
)
5637 if (__comp(__first
, __result
))
5643 * @brief Return the minimum element in a range.
5644 * @ingroup sorting_algorithms
5645 * @param __first Start of range.
5646 * @param __last End of range.
5647 * @return Iterator referencing the first instance of the smallest value.
5649 template<typename _ForwardIterator
>
5650 _GLIBCXX14_CONSTEXPR
5652 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5654 // concept requirements
5655 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5656 __glibcxx_function_requires(_LessThanComparableConcept
<
5657 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5658 __glibcxx_requires_valid_range(__first
, __last
);
5659 __glibcxx_requires_irreflexive(__first
, __last
);
5661 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5662 __gnu_cxx::__ops::__iter_less_iter());
5666 * @brief Return the minimum element in a range using comparison functor.
5667 * @ingroup sorting_algorithms
5668 * @param __first Start of range.
5669 * @param __last End of range.
5670 * @param __comp Comparison functor.
5671 * @return Iterator referencing the first instance of the smallest value
5672 * according to __comp.
5674 template<typename _ForwardIterator
, typename _Compare
>
5675 _GLIBCXX14_CONSTEXPR
5676 inline _ForwardIterator
5677 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5680 // concept requirements
5681 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5682 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5683 typename iterator_traits
<_ForwardIterator
>::value_type
,
5684 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5685 __glibcxx_requires_valid_range(__first
, __last
);
5686 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5688 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5689 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5692 template<typename _ForwardIterator
, typename _Compare
>
5693 _GLIBCXX14_CONSTEXPR
5695 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5698 if (__first
== __last
) return __first
;
5699 _ForwardIterator __result
= __first
;
5700 while (++__first
!= __last
)
5701 if (__comp(__result
, __first
))
5707 * @brief Return the maximum element in a range.
5708 * @ingroup sorting_algorithms
5709 * @param __first Start of range.
5710 * @param __last End of range.
5711 * @return Iterator referencing the first instance of the largest value.
5713 template<typename _ForwardIterator
>
5714 _GLIBCXX14_CONSTEXPR
5715 inline _ForwardIterator
5716 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5718 // concept requirements
5719 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5720 __glibcxx_function_requires(_LessThanComparableConcept
<
5721 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5722 __glibcxx_requires_valid_range(__first
, __last
);
5723 __glibcxx_requires_irreflexive(__first
, __last
);
5725 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5726 __gnu_cxx::__ops::__iter_less_iter());
5730 * @brief Return the maximum element in a range using comparison functor.
5731 * @ingroup sorting_algorithms
5732 * @param __first Start of range.
5733 * @param __last End of range.
5734 * @param __comp Comparison functor.
5735 * @return Iterator referencing the first instance of the largest value
5736 * according to __comp.
5738 template<typename _ForwardIterator
, typename _Compare
>
5739 _GLIBCXX14_CONSTEXPR
5740 inline _ForwardIterator
5741 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5744 // concept requirements
5745 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5746 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5747 typename iterator_traits
<_ForwardIterator
>::value_type
,
5748 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5749 __glibcxx_requires_valid_range(__first
, __last
);
5750 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5752 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5753 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5756 #if __cplusplus >= 201103L
5758 template<typename _Tp
>
5759 _GLIBCXX14_CONSTEXPR
5761 min(initializer_list
<_Tp
> __l
)
5763 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5764 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5765 __gnu_cxx::__ops::__iter_less_iter());
5768 template<typename _Tp
, typename _Compare
>
5769 _GLIBCXX14_CONSTEXPR
5771 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
5773 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5774 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5775 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5778 template<typename _Tp
>
5779 _GLIBCXX14_CONSTEXPR
5781 max(initializer_list
<_Tp
> __l
)
5783 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5784 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5785 __gnu_cxx::__ops::__iter_less_iter());
5788 template<typename _Tp
, typename _Compare
>
5789 _GLIBCXX14_CONSTEXPR
5791 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
5793 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5794 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5795 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5799 #if __cplusplus >= 201402L
5800 /// Reservoir sampling algorithm.
5801 template<typename _InputIterator
, typename _RandomAccessIterator
,
5802 typename _Size
, typename _UniformRandomBitGenerator
>
5803 _RandomAccessIterator
5804 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5805 _RandomAccessIterator __out
, random_access_iterator_tag
,
5806 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5808 using __distrib_type
= uniform_int_distribution
<_Size
>;
5809 using __param_type
= typename
__distrib_type::param_type
;
5810 __distrib_type __d
{};
5811 _Size __sample_sz
= 0;
5812 while (__first
!= __last
&& __sample_sz
!= __n
)
5814 __out
[__sample_sz
++] = *__first
;
5817 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5818 ++__first
, (void) ++__pop_sz
)
5820 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5822 __out
[__k
] = *__first
;
5824 return __out
+ __sample_sz
;
5827 /// Selection sampling algorithm.
5828 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5829 typename _Size
, typename _UniformRandomBitGenerator
>
5831 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5832 forward_iterator_tag
,
5833 _OutputIterator __out
, _Cat
,
5834 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5836 using __distrib_type
= uniform_int_distribution
<_Size
>;
5837 using __param_type
= typename
__distrib_type::param_type
;
5838 using _USize
= make_unsigned_t
<_Size
>;
5839 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5840 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5842 if (__first
== __last
)
5845 __distrib_type __d
{};
5846 _Size __unsampled_sz
= std::distance(__first
, __last
);
5847 __n
= std::min(__n
, __unsampled_sz
);
5849 // If possible, we use __gen_two_uniform_ints to efficiently produce
5850 // two random numbers using a single distribution invocation:
5852 const __uc_type __urngrange
= __g
.max() - __g
.min();
5853 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5854 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5857 while (__n
!= 0 && __unsampled_sz
>= 2)
5859 const pair
<_Size
, _Size
> __p
=
5860 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5863 if (__p
.first
< __n
)
5865 *__out
++ = *__first
;
5871 if (__n
== 0) break;
5874 if (__p
.second
< __n
)
5876 *__out
++ = *__first
;
5884 // The loop above is otherwise equivalent to this one-at-a-time version:
5886 for (; __n
!= 0; ++__first
)
5887 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5889 *__out
++ = *__first
;
5895 #if __cplusplus > 201402L
5896 #define __cpp_lib_sample 201603L
5897 /// Take a random sample from a population.
5898 template<typename _PopulationIterator
, typename _SampleIterator
,
5899 typename _Distance
, typename _UniformRandomBitGenerator
>
5901 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5902 _SampleIterator __out
, _Distance __n
,
5903 _UniformRandomBitGenerator
&& __g
)
5905 using __pop_cat
= typename
5906 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5907 using __samp_cat
= typename
5908 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5911 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5912 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5913 "output range must use a RandomAccessIterator when input range"
5914 " does not meet the ForwardIterator requirements");
5916 static_assert(is_integral
<_Distance
>::value
,
5917 "sample size must be an integer type");
5919 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5920 return _GLIBCXX_STD_A::
5921 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5922 std::forward
<_UniformRandomBitGenerator
>(__g
));
5927 _GLIBCXX_END_NAMESPACE_ALGO
5928 _GLIBCXX_END_NAMESPACE_VERSION
5931 #endif /* _STL_ALGO_H */