1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2024 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/predefined_ops.h>
64 #if __cplusplus >= 201103L
65 #include <bits/uniform_int_dist.h>
69 # include <bits/stl_tempbuf.h> // for _Temporary_buffer
70 # if (__cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED)
71 # include <cstdlib> // for rand
75 // See concept_check.h for the __glibcxx_*_requires macros.
77 namespace std
_GLIBCXX_VISIBILITY(default)
79 _GLIBCXX_BEGIN_NAMESPACE_VERSION
81 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
82 template<typename _Iterator
, typename _Compare
>
85 __move_median_to_first(_Iterator __result
,_Iterator __a
, _Iterator __b
,
86 _Iterator __c
, _Compare __comp
)
91 std::iter_swap(__result
, __b
);
92 else if (__comp(__a
, __c
))
93 std::iter_swap(__result
, __c
);
95 std::iter_swap(__result
, __a
);
97 else if (__comp(__a
, __c
))
98 std::iter_swap(__result
, __a
);
99 else if (__comp(__b
, __c
))
100 std::iter_swap(__result
, __c
);
102 std::iter_swap(__result
, __b
);
105 /// Provided for stable_partition to use.
106 template<typename _InputIterator
, typename _Predicate
>
108 inline _InputIterator
109 __find_if_not(_InputIterator __first
, _InputIterator __last
,
112 return std::__find_if(__first
, __last
,
113 __gnu_cxx::__ops::__negate(__pred
),
114 std::__iterator_category(__first
));
117 /// Like find_if_not(), but uses and updates a count of the
118 /// remaining range length instead of comparing against an end
120 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
123 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
125 for (; __len
; --__len
, (void) ++__first
)
126 if (!__pred(__first
))
133 // set_symmetric_difference
146 * This is an helper function for search_n overloaded for forward iterators.
148 template<typename _ForwardIterator
, typename _Integer
,
149 typename _UnaryPredicate
>
152 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
153 _Integer __count
, _UnaryPredicate __unary_pred
,
154 std::forward_iterator_tag
)
156 __first
= std::__find_if(__first
, __last
, __unary_pred
);
157 while (__first
!= __last
)
159 typename iterator_traits
<_ForwardIterator
>::difference_type
161 _ForwardIterator __i
= __first
;
163 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
172 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
178 * This is an helper function for search_n overloaded for random access
181 template<typename _RandomAccessIter
, typename _Integer
,
182 typename _UnaryPredicate
>
185 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
186 _Integer __count
, _UnaryPredicate __unary_pred
,
187 std::random_access_iterator_tag
)
189 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
192 _DistanceType __tailSize
= __last
- __first
;
193 _DistanceType __remainder
= __count
;
195 while (__remainder
<= __tailSize
) // the main loop...
197 __first
+= __remainder
;
198 __tailSize
-= __remainder
;
199 // __first here is always pointing to one past the last element of
200 // next possible match.
201 _RandomAccessIter __backTrack
= __first
;
202 while (__unary_pred(--__backTrack
))
204 if (--__remainder
== 0)
205 return (__first
- __count
); // Success
207 __remainder
= __count
+ 1 - (__first
- __backTrack
);
209 return __last
; // Failure
212 template<typename _ForwardIterator
, typename _Integer
,
213 typename _UnaryPredicate
>
216 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
218 _UnaryPredicate __unary_pred
)
224 return std::__find_if(__first
, __last
, __unary_pred
);
226 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
227 std::__iterator_category(__first
));
230 // find_end for forward iterators.
231 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
232 typename _BinaryPredicate
>
235 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
236 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
237 forward_iterator_tag
, forward_iterator_tag
,
238 _BinaryPredicate __comp
)
240 if (__first2
== __last2
)
243 _ForwardIterator1 __result
= __last1
;
246 _ForwardIterator1 __new_result
247 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
248 if (__new_result
== __last1
)
252 __result
= __new_result
;
253 __first1
= __new_result
;
259 // find_end for bidirectional iterators (much faster).
260 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
261 typename _BinaryPredicate
>
263 _BidirectionalIterator1
264 __find_end(_BidirectionalIterator1 __first1
,
265 _BidirectionalIterator1 __last1
,
266 _BidirectionalIterator2 __first2
,
267 _BidirectionalIterator2 __last2
,
268 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
269 _BinaryPredicate __comp
)
271 // concept requirements
272 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
273 _BidirectionalIterator1
>)
274 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
275 _BidirectionalIterator2
>)
277 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
278 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
280 _RevIterator1
__rlast1(__first1
);
281 _RevIterator2
__rlast2(__first2
);
282 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
283 _RevIterator2(__last2
), __rlast2
,
286 if (__rresult
== __rlast1
)
290 _BidirectionalIterator1 __result
= __rresult
.base();
291 std::advance(__result
, -std::distance(__first2
, __last2
));
297 * @brief Find last matching subsequence in a sequence.
298 * @ingroup non_mutating_algorithms
299 * @param __first1 Start of range to search.
300 * @param __last1 End of range to search.
301 * @param __first2 Start of sequence to match.
302 * @param __last2 End of sequence to match.
303 * @return The last iterator @c i in the range
304 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
305 * @p *(__first2+N) for each @c N in the range @p
306 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
308 * Searches the range @p [__first1,__last1) for a sub-sequence that
309 * compares equal value-by-value with the sequence given by @p
310 * [__first2,__last2) and returns an iterator to the __first
311 * element of the sub-sequence, or @p __last1 if the sub-sequence
312 * is not found. The sub-sequence will be the last such
313 * subsequence contained in [__first1,__last1).
315 * Because the sub-sequence must lie completely within the range @p
316 * [__first1,__last1) it must start at a position less than @p
317 * __last1-(__last2-__first2) where @p __last2-__first2 is the
318 * length of the sub-sequence. This means that the returned
319 * iterator @c i will be in the range @p
320 * [__first1,__last1-(__last2-__first2))
322 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
324 inline _ForwardIterator1
325 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
326 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
328 // concept requirements
329 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
330 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
331 __glibcxx_function_requires(_EqualOpConcept
<
332 typename iterator_traits
<_ForwardIterator1
>::value_type
,
333 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
334 __glibcxx_requires_valid_range(__first1
, __last1
);
335 __glibcxx_requires_valid_range(__first2
, __last2
);
337 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
338 std::__iterator_category(__first1
),
339 std::__iterator_category(__first2
),
340 __gnu_cxx::__ops::__iter_equal_to_iter());
344 * @brief Find last matching subsequence in a sequence using a predicate.
345 * @ingroup non_mutating_algorithms
346 * @param __first1 Start of range to search.
347 * @param __last1 End of range to search.
348 * @param __first2 Start of sequence to match.
349 * @param __last2 End of sequence to match.
350 * @param __comp The predicate to use.
351 * @return The last iterator @c i in the range @p
352 * [__first1,__last1-(__last2-__first2)) such that @c
353 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
354 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
357 * Searches the range @p [__first1,__last1) for a sub-sequence that
358 * compares equal value-by-value with the sequence given by @p
359 * [__first2,__last2) using comp as a predicate and returns an
360 * iterator to the first element of the sub-sequence, or @p __last1
361 * if the sub-sequence is not found. The sub-sequence will be the
362 * last such subsequence contained in [__first,__last1).
364 * Because the sub-sequence must lie completely within the range @p
365 * [__first1,__last1) it must start at a position less than @p
366 * __last1-(__last2-__first2) where @p __last2-__first2 is the
367 * length of the sub-sequence. This means that the returned
368 * iterator @c i will be in the range @p
369 * [__first1,__last1-(__last2-__first2))
371 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
372 typename _BinaryPredicate
>
374 inline _ForwardIterator1
375 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
376 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
377 _BinaryPredicate __comp
)
379 // concept requirements
380 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
381 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
382 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
383 typename iterator_traits
<_ForwardIterator1
>::value_type
,
384 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
385 __glibcxx_requires_valid_range(__first1
, __last1
);
386 __glibcxx_requires_valid_range(__first2
, __last2
);
388 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
389 std::__iterator_category(__first1
),
390 std::__iterator_category(__first2
),
391 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
394 #if __cplusplus >= 201103L
396 * @brief Checks that a predicate is true for all the elements
398 * @ingroup non_mutating_algorithms
399 * @param __first An input iterator.
400 * @param __last An input iterator.
401 * @param __pred A predicate.
402 * @return True if the check is true, false otherwise.
404 * Returns true if @p __pred is true for each element in the range
405 * @p [__first,__last), and false otherwise.
407 template<typename _InputIterator
, typename _Predicate
>
410 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
411 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
414 * @brief Checks that a predicate is false for all the elements
416 * @ingroup non_mutating_algorithms
417 * @param __first An input iterator.
418 * @param __last An input iterator.
419 * @param __pred A predicate.
420 * @return True if the check is true, false otherwise.
422 * Returns true if @p __pred is false for each element in the range
423 * @p [__first,__last), and false otherwise.
425 template<typename _InputIterator
, typename _Predicate
>
428 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
429 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
432 * @brief Checks that a predicate is true for at least one element
434 * @ingroup non_mutating_algorithms
435 * @param __first An input iterator.
436 * @param __last An input iterator.
437 * @param __pred A predicate.
438 * @return True if the check is true, false otherwise.
440 * Returns true if an element exists in the range @p
441 * [__first,__last) such that @p __pred is true, and false
444 template<typename _InputIterator
, typename _Predicate
>
447 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
448 { return !std::none_of(__first
, __last
, __pred
); }
451 * @brief Find the first element in a sequence for which a
452 * predicate is false.
453 * @ingroup non_mutating_algorithms
454 * @param __first An input iterator.
455 * @param __last An input iterator.
456 * @param __pred A predicate.
457 * @return The first iterator @c i in the range @p [__first,__last)
458 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
460 template<typename _InputIterator
, typename _Predicate
>
462 inline _InputIterator
463 find_if_not(_InputIterator __first
, _InputIterator __last
,
466 // concept requirements
467 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
468 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
469 typename iterator_traits
<_InputIterator
>::value_type
>)
470 __glibcxx_requires_valid_range(__first
, __last
);
471 return std::__find_if_not(__first
, __last
,
472 __gnu_cxx::__ops::__pred_iter(__pred
));
476 * @brief Checks whether the sequence is partitioned.
477 * @ingroup mutating_algorithms
478 * @param __first An input iterator.
479 * @param __last An input iterator.
480 * @param __pred A predicate.
481 * @return True if the range @p [__first,__last) is partioned by @p __pred,
482 * i.e. if all elements that satisfy @p __pred appear before those that
485 template<typename _InputIterator
, typename _Predicate
>
488 is_partitioned(_InputIterator __first
, _InputIterator __last
,
491 __first
= std::find_if_not(__first
, __last
, __pred
);
492 if (__first
== __last
)
495 return std::none_of(__first
, __last
, __pred
);
499 * @brief Find the partition point of a partitioned range.
500 * @ingroup mutating_algorithms
501 * @param __first An iterator.
502 * @param __last Another iterator.
503 * @param __pred A predicate.
504 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
505 * and @p none_of(mid, __last, __pred) are both true.
507 template<typename _ForwardIterator
, typename _Predicate
>
510 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
513 // concept requirements
514 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
515 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
516 typename iterator_traits
<_ForwardIterator
>::value_type
>)
518 // A specific debug-mode test will be necessary...
519 __glibcxx_requires_valid_range(__first
, __last
);
521 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
524 _DistanceType __len
= std::distance(__first
, __last
);
528 _DistanceType __half
= __len
>> 1;
529 _ForwardIterator __middle
= __first
;
530 std::advance(__middle
, __half
);
531 if (__pred(*__middle
))
535 __len
= __len
- __half
- 1;
544 template<typename _InputIterator
, typename _OutputIterator
,
548 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
549 _OutputIterator __result
, _Predicate __pred
)
551 for (; __first
!= __last
; ++__first
)
552 if (!__pred(__first
))
554 *__result
= *__first
;
561 * @brief Copy a sequence, removing elements of a given value.
562 * @ingroup mutating_algorithms
563 * @param __first An input iterator.
564 * @param __last An input iterator.
565 * @param __result An output iterator.
566 * @param __value The value to be removed.
567 * @return An iterator designating the end of the resulting sequence.
569 * Copies each element in the range @p [__first,__last) not equal
570 * to @p __value to the range beginning at @p __result.
571 * remove_copy() is stable, so the relative order of elements that
572 * are copied is unchanged.
574 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
576 inline _OutputIterator
577 remove_copy(_InputIterator __first
, _InputIterator __last
,
578 _OutputIterator __result
, const _Tp
& __value
)
580 // concept requirements
581 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
582 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
583 typename iterator_traits
<_InputIterator
>::value_type
>)
584 __glibcxx_function_requires(_EqualOpConcept
<
585 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
586 __glibcxx_requires_valid_range(__first
, __last
);
588 return std::__remove_copy_if(__first
, __last
, __result
,
589 __gnu_cxx::__ops::__iter_equals_val(__value
));
593 * @brief Copy a sequence, removing elements for which a predicate is true.
594 * @ingroup mutating_algorithms
595 * @param __first An input iterator.
596 * @param __last An input iterator.
597 * @param __result An output iterator.
598 * @param __pred A predicate.
599 * @return An iterator designating the end of the resulting sequence.
601 * Copies each element in the range @p [__first,__last) for which
602 * @p __pred returns false to the range beginning at @p __result.
604 * remove_copy_if() is stable, so the relative order of elements that are
605 * copied is unchanged.
607 template<typename _InputIterator
, typename _OutputIterator
,
610 inline _OutputIterator
611 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
612 _OutputIterator __result
, _Predicate __pred
)
614 // concept requirements
615 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
616 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
617 typename iterator_traits
<_InputIterator
>::value_type
>)
618 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
619 typename iterator_traits
<_InputIterator
>::value_type
>)
620 __glibcxx_requires_valid_range(__first
, __last
);
622 return std::__remove_copy_if(__first
, __last
, __result
,
623 __gnu_cxx::__ops::__pred_iter(__pred
));
626 #if __cplusplus >= 201103L
628 * @brief Copy the elements of a sequence for which a predicate is true.
629 * @ingroup mutating_algorithms
630 * @param __first An input iterator.
631 * @param __last An input iterator.
632 * @param __result An output iterator.
633 * @param __pred A predicate.
634 * @return An iterator designating the end of the resulting sequence.
636 * Copies each element in the range @p [__first,__last) for which
637 * @p __pred returns true to the range beginning at @p __result.
639 * copy_if() is stable, so the relative order of elements that are
640 * copied is unchanged.
642 template<typename _InputIterator
, typename _OutputIterator
,
646 copy_if(_InputIterator __first
, _InputIterator __last
,
647 _OutputIterator __result
, _Predicate __pred
)
649 // concept requirements
650 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
651 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
652 typename iterator_traits
<_InputIterator
>::value_type
>)
653 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
654 typename iterator_traits
<_InputIterator
>::value_type
>)
655 __glibcxx_requires_valid_range(__first
, __last
);
657 for (; __first
!= __last
; ++__first
)
658 if (__pred(*__first
))
660 *__result
= *__first
;
666 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
669 __copy_n(_InputIterator __first
, _Size __n
,
670 _OutputIterator __result
, input_iterator_tag
)
672 return std::__niter_wrap(__result
,
673 __copy_n_a(__first
, __n
,
674 std::__niter_base(__result
), true));
677 template<typename _RandomAccessIterator
, typename _Size
,
678 typename _OutputIterator
>
680 inline _OutputIterator
681 __copy_n(_RandomAccessIterator __first
, _Size __n
,
682 _OutputIterator __result
, random_access_iterator_tag
)
683 { return std::copy(__first
, __first
+ __n
, __result
); }
686 * @brief Copies the range [first,first+n) into [result,result+n).
687 * @ingroup mutating_algorithms
688 * @param __first An input iterator.
689 * @param __n The number of elements to copy.
690 * @param __result An output iterator.
693 * This inline function will boil down to a call to @c memmove whenever
694 * possible. Failing that, if random access iterators are passed, then the
695 * loop count will be known (and therefore a candidate for compiler
696 * optimizations such as unrolling).
698 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
700 inline _OutputIterator
701 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
703 // concept requirements
704 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
705 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
706 typename iterator_traits
<_InputIterator
>::value_type
>)
708 const auto __n2
= std::__size_to_integer(__n
);
712 __glibcxx_requires_can_increment(__first
, __n2
);
713 __glibcxx_requires_can_increment(__result
, __n2
);
715 return std::__copy_n(__first
, __n2
, __result
,
716 std::__iterator_category(__first
));
720 * @brief Copy the elements of a sequence to separate output sequences
721 * depending on the truth value of a predicate.
722 * @ingroup mutating_algorithms
723 * @param __first An input iterator.
724 * @param __last An input iterator.
725 * @param __out_true An output iterator.
726 * @param __out_false An output iterator.
727 * @param __pred A predicate.
728 * @return A pair designating the ends of the resulting sequences.
730 * Copies each element in the range @p [__first,__last) for which
731 * @p __pred returns true to the range beginning at @p out_true
732 * and each element for which @p __pred returns false to @p __out_false.
734 template<typename _InputIterator
, typename _OutputIterator1
,
735 typename _OutputIterator2
, typename _Predicate
>
737 pair
<_OutputIterator1
, _OutputIterator2
>
738 partition_copy(_InputIterator __first
, _InputIterator __last
,
739 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
742 // concept requirements
743 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
744 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
745 typename iterator_traits
<_InputIterator
>::value_type
>)
746 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
747 typename iterator_traits
<_InputIterator
>::value_type
>)
748 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
749 typename iterator_traits
<_InputIterator
>::value_type
>)
750 __glibcxx_requires_valid_range(__first
, __last
);
752 for (; __first
!= __last
; ++__first
)
753 if (__pred(*__first
))
755 *__out_true
= *__first
;
760 *__out_false
= *__first
;
764 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
769 * @brief Remove elements from a sequence.
770 * @ingroup mutating_algorithms
771 * @param __first An input iterator.
772 * @param __last An input iterator.
773 * @param __value The value to be removed.
774 * @return An iterator designating the end of the resulting sequence.
776 * All elements equal to @p __value are removed from the range
777 * @p [__first,__last).
779 * remove() is stable, so the relative order of elements that are
780 * not removed is unchanged.
782 * Elements between the end of the resulting sequence and @p __last
783 * are still present, but their value is unspecified.
785 template<typename _ForwardIterator
, typename _Tp
>
787 inline _ForwardIterator
788 remove(_ForwardIterator __first
, _ForwardIterator __last
,
791 // concept requirements
792 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
794 __glibcxx_function_requires(_EqualOpConcept
<
795 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
796 __glibcxx_requires_valid_range(__first
, __last
);
798 return std::__remove_if(__first
, __last
,
799 __gnu_cxx::__ops::__iter_equals_val(__value
));
803 * @brief Remove elements from a sequence using a predicate.
804 * @ingroup mutating_algorithms
805 * @param __first A forward iterator.
806 * @param __last A forward iterator.
807 * @param __pred A predicate.
808 * @return An iterator designating the end of the resulting sequence.
810 * All elements for which @p __pred returns true are removed from the range
811 * @p [__first,__last).
813 * remove_if() is stable, so the relative order of elements that are
814 * not removed is unchanged.
816 * Elements between the end of the resulting sequence and @p __last
817 * are still present, but their value is unspecified.
819 template<typename _ForwardIterator
, typename _Predicate
>
821 inline _ForwardIterator
822 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
825 // concept requirements
826 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
828 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
829 typename iterator_traits
<_ForwardIterator
>::value_type
>)
830 __glibcxx_requires_valid_range(__first
, __last
);
832 return std::__remove_if(__first
, __last
,
833 __gnu_cxx::__ops::__pred_iter(__pred
));
836 template<typename _ForwardIterator
, typename _BinaryPredicate
>
839 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
840 _BinaryPredicate __binary_pred
)
842 if (__first
== __last
)
844 _ForwardIterator __next
= __first
;
845 while (++__next
!= __last
)
847 if (__binary_pred(__first
, __next
))
854 template<typename _ForwardIterator
, typename _BinaryPredicate
>
857 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
858 _BinaryPredicate __binary_pred
)
860 // Skip the beginning, if already unique.
861 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
862 if (__first
== __last
)
865 // Do the real copy work.
866 _ForwardIterator __dest
= __first
;
868 while (++__first
!= __last
)
869 if (!__binary_pred(__dest
, __first
))
870 *++__dest
= _GLIBCXX_MOVE(*__first
);
875 * @brief Remove consecutive duplicate values from a sequence.
876 * @ingroup mutating_algorithms
877 * @param __first A forward iterator.
878 * @param __last A forward iterator.
879 * @return An iterator designating the end of the resulting sequence.
881 * Removes all but the first element from each group of consecutive
882 * values that compare equal.
883 * unique() is stable, so the relative order of elements that are
884 * not removed is unchanged.
885 * Elements between the end of the resulting sequence and @p __last
886 * are still present, but their value is unspecified.
888 template<typename _ForwardIterator
>
890 inline _ForwardIterator
891 unique(_ForwardIterator __first
, _ForwardIterator __last
)
893 // concept requirements
894 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
896 __glibcxx_function_requires(_EqualityComparableConcept
<
897 typename iterator_traits
<_ForwardIterator
>::value_type
>)
898 __glibcxx_requires_valid_range(__first
, __last
);
900 return std::__unique(__first
, __last
,
901 __gnu_cxx::__ops::__iter_equal_to_iter());
905 * @brief Remove consecutive values from a sequence using a predicate.
906 * @ingroup mutating_algorithms
907 * @param __first A forward iterator.
908 * @param __last A forward iterator.
909 * @param __binary_pred A binary predicate.
910 * @return An iterator designating the end of the resulting sequence.
912 * Removes all but the first element from each group of consecutive
913 * values for which @p __binary_pred returns true.
914 * unique() is stable, so the relative order of elements that are
915 * not removed is unchanged.
916 * Elements between the end of the resulting sequence and @p __last
917 * are still present, but their value is unspecified.
919 template<typename _ForwardIterator
, typename _BinaryPredicate
>
921 inline _ForwardIterator
922 unique(_ForwardIterator __first
, _ForwardIterator __last
,
923 _BinaryPredicate __binary_pred
)
925 // concept requirements
926 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
928 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
929 typename iterator_traits
<_ForwardIterator
>::value_type
,
930 typename iterator_traits
<_ForwardIterator
>::value_type
>)
931 __glibcxx_requires_valid_range(__first
, __last
);
933 return std::__unique(__first
, __last
,
934 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
938 * This is an uglified
939 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
941 * overloaded for forward iterators and output iterator as result.
943 template<typename _ForwardIterator
, typename _OutputIterator
,
944 typename _BinaryPredicate
>
947 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
948 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
949 forward_iterator_tag
, output_iterator_tag
)
951 // concept requirements -- iterators already checked
952 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
953 typename iterator_traits
<_ForwardIterator
>::value_type
,
954 typename iterator_traits
<_ForwardIterator
>::value_type
>)
956 _ForwardIterator __next
= __first
;
957 *__result
= *__first
;
958 while (++__next
!= __last
)
959 if (!__binary_pred(__first
, __next
))
962 *++__result
= *__first
;
968 * This is an uglified
969 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
971 * overloaded for input iterators and output iterator as result.
973 template<typename _InputIterator
, typename _OutputIterator
,
974 typename _BinaryPredicate
>
977 __unique_copy(_InputIterator __first
, _InputIterator __last
,
978 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
979 input_iterator_tag
, output_iterator_tag
)
981 // concept requirements -- iterators already checked
982 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
983 typename iterator_traits
<_InputIterator
>::value_type
,
984 typename iterator_traits
<_InputIterator
>::value_type
>)
986 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
987 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
989 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
991 while (++__first
!= __last
)
992 if (!__rebound_pred(__first
, __value
))
995 *++__result
= __value
;
1001 * This is an uglified
1002 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1004 * overloaded for input iterators and forward iterator as result.
1006 template<typename _InputIterator
, typename _ForwardIterator
,
1007 typename _BinaryPredicate
>
1008 _GLIBCXX20_CONSTEXPR
1010 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1011 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1012 input_iterator_tag
, forward_iterator_tag
)
1014 // concept requirements -- iterators already checked
1015 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1016 typename iterator_traits
<_ForwardIterator
>::value_type
,
1017 typename iterator_traits
<_InputIterator
>::value_type
>)
1018 *__result
= *__first
;
1019 while (++__first
!= __last
)
1020 if (!__binary_pred(__result
, __first
))
1021 *++__result
= *__first
;
1026 * This is an uglified reverse(_BidirectionalIterator,
1027 * _BidirectionalIterator)
1028 * overloaded for bidirectional iterators.
1030 template<typename _BidirectionalIterator
>
1031 _GLIBCXX20_CONSTEXPR
1033 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1034 bidirectional_iterator_tag
)
1037 if (__first
== __last
|| __first
== --__last
)
1041 std::iter_swap(__first
, __last
);
1047 * This is an uglified reverse(_BidirectionalIterator,
1048 * _BidirectionalIterator)
1049 * overloaded for random access iterators.
1051 template<typename _RandomAccessIterator
>
1052 _GLIBCXX20_CONSTEXPR
1054 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1055 random_access_iterator_tag
)
1057 if (__first
== __last
)
1060 while (__first
< __last
)
1062 std::iter_swap(__first
, __last
);
1069 * @brief Reverse a sequence.
1070 * @ingroup mutating_algorithms
1071 * @param __first A bidirectional iterator.
1072 * @param __last A bidirectional iterator.
1073 * @return reverse() returns no value.
1075 * Reverses the order of the elements in the range @p [__first,__last),
1076 * so that the first element becomes the last etc.
1077 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1078 * swaps @p *(__first+i) and @p *(__last-(i+1))
1080 template<typename _BidirectionalIterator
>
1081 _GLIBCXX20_CONSTEXPR
1083 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1085 // concept requirements
1086 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1087 _BidirectionalIterator
>)
1088 __glibcxx_requires_valid_range(__first
, __last
);
1089 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1093 * @brief Copy a sequence, reversing its elements.
1094 * @ingroup mutating_algorithms
1095 * @param __first A bidirectional iterator.
1096 * @param __last A bidirectional iterator.
1097 * @param __result An output iterator.
1098 * @return An iterator designating the end of the resulting sequence.
1100 * Copies the elements in the range @p [__first,__last) to the
1101 * range @p [__result,__result+(__last-__first)) such that the
1102 * order of the elements is reversed. For every @c i such that @p
1103 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1104 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1105 * The ranges @p [__first,__last) and @p
1106 * [__result,__result+(__last-__first)) must not overlap.
1108 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1109 _GLIBCXX20_CONSTEXPR
1111 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1112 _OutputIterator __result
)
1114 // concept requirements
1115 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1116 _BidirectionalIterator
>)
1117 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1118 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1119 __glibcxx_requires_valid_range(__first
, __last
);
1121 while (__first
!= __last
)
1124 *__result
= *__last
;
1131 * This is a helper function for the rotate algorithm specialized on RAIs.
1132 * It returns the greatest common divisor of two integer values.
1134 template<typename _EuclideanRingElement
>
1135 _GLIBCXX20_CONSTEXPR
1136 _EuclideanRingElement
1137 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1141 _EuclideanRingElement __t
= __m
% __n
;
1148 _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2
)
1150 /// This is a helper function for the rotate algorithm.
1151 template<typename _ForwardIterator
>
1152 _GLIBCXX20_CONSTEXPR
1154 __rotate(_ForwardIterator __first
,
1155 _ForwardIterator __middle
,
1156 _ForwardIterator __last
,
1157 forward_iterator_tag
)
1159 if (__first
== __middle
)
1161 else if (__last
== __middle
)
1164 _ForwardIterator __first2
= __middle
;
1167 std::iter_swap(__first
, __first2
);
1170 if (__first
== __middle
)
1171 __middle
= __first2
;
1173 while (__first2
!= __last
);
1175 _ForwardIterator __ret
= __first
;
1177 __first2
= __middle
;
1179 while (__first2
!= __last
)
1181 std::iter_swap(__first
, __first2
);
1184 if (__first
== __middle
)
1185 __middle
= __first2
;
1186 else if (__first2
== __last
)
1187 __first2
= __middle
;
1192 /// This is a helper function for the rotate algorithm.
1193 template<typename _BidirectionalIterator
>
1194 _GLIBCXX20_CONSTEXPR
1195 _BidirectionalIterator
1196 __rotate(_BidirectionalIterator __first
,
1197 _BidirectionalIterator __middle
,
1198 _BidirectionalIterator __last
,
1199 bidirectional_iterator_tag
)
1201 // concept requirements
1202 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1203 _BidirectionalIterator
>)
1205 if (__first
== __middle
)
1207 else if (__last
== __middle
)
1210 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1211 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1213 while (__first
!= __middle
&& __middle
!= __last
)
1215 std::iter_swap(__first
, --__last
);
1219 if (__first
== __middle
)
1221 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1226 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1231 /// This is a helper function for the rotate algorithm.
1232 template<typename _RandomAccessIterator
>
1233 _GLIBCXX20_CONSTEXPR
1234 _RandomAccessIterator
1235 __rotate(_RandomAccessIterator __first
,
1236 _RandomAccessIterator __middle
,
1237 _RandomAccessIterator __last
,
1238 random_access_iterator_tag
)
1240 // concept requirements
1241 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1242 _RandomAccessIterator
>)
1244 if (__first
== __middle
)
1246 else if (__last
== __middle
)
1249 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1251 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1254 _Distance __n
= __last
- __first
;
1255 _Distance __k
= __middle
- __first
;
1257 if (__k
== __n
- __k
)
1259 std::swap_ranges(__first
, __middle
, __middle
);
1263 _RandomAccessIterator __p
= __first
;
1264 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1268 if (__k
< __n
- __k
)
1270 if (__is_pod(_ValueType
) && __k
== 1)
1272 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1273 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1274 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1277 _RandomAccessIterator __q
= __p
+ __k
;
1278 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1280 std::iter_swap(__p
, __q
);
1287 std::swap(__n
, __k
);
1293 if (__is_pod(_ValueType
) && __k
== 1)
1295 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1296 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1297 *__p
= _GLIBCXX_MOVE(__t
);
1300 _RandomAccessIterator __q
= __p
+ __n
;
1302 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1306 std::iter_swap(__p
, __q
);
1311 std::swap(__n
, __k
);
1316 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1317 // DR 488. rotate throws away useful information
1319 * @brief Rotate the elements of a sequence.
1320 * @ingroup mutating_algorithms
1321 * @param __first A forward iterator.
1322 * @param __middle A forward iterator.
1323 * @param __last A forward iterator.
1324 * @return first + (last - middle).
1326 * Rotates the elements of the range @p [__first,__last) by
1327 * @p (__middle - __first) positions so that the element at @p __middle
1328 * is moved to @p __first, the element at @p __middle+1 is moved to
1329 * @p __first+1 and so on for each element in the range
1330 * @p [__first,__last).
1332 * This effectively swaps the ranges @p [__first,__middle) and
1333 * @p [__middle,__last).
1336 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1337 * for each @p n in the range @p [0,__last-__first).
1339 template<typename _ForwardIterator
>
1340 _GLIBCXX20_CONSTEXPR
1341 inline _ForwardIterator
1342 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1343 _ForwardIterator __last
)
1345 // concept requirements
1346 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1348 __glibcxx_requires_valid_range(__first
, __middle
);
1349 __glibcxx_requires_valid_range(__middle
, __last
);
1351 return std::__rotate(__first
, __middle
, __last
,
1352 std::__iterator_category(__first
));
1355 _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2
)
1358 * @brief Copy a sequence, rotating its elements.
1359 * @ingroup mutating_algorithms
1360 * @param __first A forward iterator.
1361 * @param __middle A forward iterator.
1362 * @param __last A forward iterator.
1363 * @param __result An output iterator.
1364 * @return An iterator designating the end of the resulting sequence.
1366 * Copies the elements of the range @p [__first,__last) to the
1367 * range beginning at @result, rotating the copied elements by
1368 * @p (__middle-__first) positions so that the element at @p __middle
1369 * is moved to @p __result, the element at @p __middle+1 is moved
1370 * to @p __result+1 and so on for each element in the range @p
1374 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1375 * for each @p n in the range @p [0,__last-__first).
1377 template<typename _ForwardIterator
, typename _OutputIterator
>
1378 _GLIBCXX20_CONSTEXPR
1379 inline _OutputIterator
1380 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1381 _ForwardIterator __last
, _OutputIterator __result
)
1383 // concept requirements
1384 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1385 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1386 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1387 __glibcxx_requires_valid_range(__first
, __middle
);
1388 __glibcxx_requires_valid_range(__middle
, __last
);
1390 return std::copy(__first
, __middle
,
1391 std::copy(__middle
, __last
, __result
));
1394 /// This is a helper function...
1395 template<typename _ForwardIterator
, typename _Predicate
>
1396 _GLIBCXX20_CONSTEXPR
1398 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1399 _Predicate __pred
, forward_iterator_tag
)
1401 if (__first
== __last
)
1404 while (__pred(*__first
))
1405 if (++__first
== __last
)
1408 _ForwardIterator __next
= __first
;
1410 while (++__next
!= __last
)
1411 if (__pred(*__next
))
1413 std::iter_swap(__first
, __next
);
1420 /// This is a helper function...
1421 template<typename _BidirectionalIterator
, typename _Predicate
>
1422 _GLIBCXX20_CONSTEXPR
1423 _BidirectionalIterator
1424 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1425 _Predicate __pred
, bidirectional_iterator_tag
)
1430 if (__first
== __last
)
1432 else if (__pred(*__first
))
1438 if (__first
== __last
)
1440 else if (!bool(__pred(*__last
)))
1444 std::iter_swap(__first
, __last
);
1452 /// This is a helper function...
1453 /// Requires __first != __last and !__pred(__first)
1454 /// and __len == distance(__first, __last).
1456 /// !__pred(__first) allows us to guarantee that we don't
1457 /// move-assign an element onto itself.
1458 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1461 __stable_partition_adaptive(_ForwardIterator __first
,
1462 _ForwardIterator __last
,
1463 _Predicate __pred
, _Distance __len
,
1465 _Distance __buffer_size
)
1470 if (__len
<= __buffer_size
)
1472 _ForwardIterator __result1
= __first
;
1473 _Pointer __result2
= __buffer
;
1475 // The precondition guarantees that !__pred(__first), so
1476 // move that element to the buffer before starting the loop.
1477 // This ensures that we only call __pred once per element.
1478 *__result2
= _GLIBCXX_MOVE(*__first
);
1481 for (; __first
!= __last
; ++__first
)
1482 if (__pred(__first
))
1484 *__result1
= _GLIBCXX_MOVE(*__first
);
1489 *__result2
= _GLIBCXX_MOVE(*__first
);
1493 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1497 _ForwardIterator __middle
= __first
;
1498 std::advance(__middle
, __len
/ 2);
1499 _ForwardIterator __left_split
=
1500 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1501 __len
/ 2, __buffer
,
1504 // Advance past true-predicate values to satisfy this
1505 // function's preconditions.
1506 _Distance __right_len
= __len
- __len
/ 2;
1507 _ForwardIterator __right_split
=
1508 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1512 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1514 __buffer
, __buffer_size
);
1516 return std::rotate(__left_split
, __middle
, __right_split
);
1519 template<typename _ForwardIterator
, typename _Predicate
>
1521 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1524 __first
= std::__find_if_not(__first
, __last
, __pred
);
1526 if (__first
== __last
)
1529 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1531 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1534 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1535 __buf(__first
, std::distance(__first
, __last
));
1537 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1538 _DistanceType(__buf
.requested_size()),
1540 _DistanceType(__buf
.size()));
1544 * @brief Move elements for which a predicate is true to the beginning
1545 * of a sequence, preserving relative ordering.
1546 * @ingroup mutating_algorithms
1547 * @param __first A forward iterator.
1548 * @param __last A forward iterator.
1549 * @param __pred A predicate functor.
1550 * @return An iterator @p middle such that @p __pred(i) is true for each
1551 * iterator @p i in the range @p [first,middle) and false for each @p i
1552 * in the range @p [middle,last).
1554 * Performs the same function as @p partition() with the additional
1555 * guarantee that the relative ordering of elements in each group is
1556 * preserved, so any two elements @p x and @p y in the range
1557 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1558 * relative ordering after calling @p stable_partition().
1560 template<typename _ForwardIterator
, typename _Predicate
>
1561 inline _ForwardIterator
1562 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1565 // concept requirements
1566 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1568 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1569 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1570 __glibcxx_requires_valid_range(__first
, __last
);
1572 return std::__stable_partition(__first
, __last
,
1573 __gnu_cxx::__ops::__pred_iter(__pred
));
1577 /// @cond undocumented
1579 /// This is a helper function for the sort routines.
1580 template<typename _RandomAccessIterator
, typename _Compare
>
1581 _GLIBCXX20_CONSTEXPR
1583 __heap_select(_RandomAccessIterator __first
,
1584 _RandomAccessIterator __middle
,
1585 _RandomAccessIterator __last
, _Compare __comp
)
1587 std::__make_heap(__first
, __middle
, __comp
);
1588 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1589 if (__comp(__i
, __first
))
1590 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1595 template<typename _InputIterator
, typename _RandomAccessIterator
,
1597 _GLIBCXX20_CONSTEXPR
1598 _RandomAccessIterator
1599 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1600 _RandomAccessIterator __result_first
,
1601 _RandomAccessIterator __result_last
,
1604 typedef typename iterator_traits
<_InputIterator
>::value_type
1606 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1607 typedef typename
_RItTraits::difference_type _DistanceType
;
1609 if (__result_first
== __result_last
)
1610 return __result_last
;
1611 _RandomAccessIterator __result_real_last
= __result_first
;
1612 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1614 *__result_real_last
= *__first
;
1615 ++__result_real_last
;
1619 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1620 while (__first
!= __last
)
1622 if (__comp(__first
, __result_first
))
1623 std::__adjust_heap(__result_first
, _DistanceType(0),
1624 _DistanceType(__result_real_last
1626 _InputValueType(*__first
), __comp
);
1629 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1630 return __result_real_last
;
1636 * @brief Copy the smallest elements of a sequence.
1637 * @ingroup sorting_algorithms
1638 * @param __first An iterator.
1639 * @param __last Another iterator.
1640 * @param __result_first A random-access iterator.
1641 * @param __result_last Another random-access iterator.
1642 * @return An iterator indicating the end of the resulting sequence.
1644 * Copies and sorts the smallest `N` values from the range
1645 * `[__first, __last)` to the range beginning at `__result_first`, where
1646 * the number of elements to be copied, `N`, is the smaller of
1647 * `(__last - __first)` and `(__result_last - __result_first)`.
1648 * After the sort if `i` and `j` are iterators in the range
1649 * `[__result_first,__result_first + N)` such that `i` precedes `j` then
1650 * `*j < *i` is false.
1651 * The value returned is `__result_first + N`.
1653 template<typename _InputIterator
, typename _RandomAccessIterator
>
1654 _GLIBCXX20_CONSTEXPR
1655 inline _RandomAccessIterator
1656 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1657 _RandomAccessIterator __result_first
,
1658 _RandomAccessIterator __result_last
)
1660 #ifdef _GLIBCXX_CONCEPT_CHECKS
1661 typedef typename iterator_traits
<_InputIterator
>::value_type
1663 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1667 // concept requirements
1668 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1669 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1671 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1673 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1674 __glibcxx_requires_valid_range(__first
, __last
);
1675 __glibcxx_requires_irreflexive(__first
, __last
);
1676 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1678 return std::__partial_sort_copy(__first
, __last
,
1679 __result_first
, __result_last
,
1680 __gnu_cxx::__ops::__iter_less_iter());
1684 * @brief Copy the smallest elements of a sequence using a predicate for
1686 * @ingroup sorting_algorithms
1687 * @param __first An input iterator.
1688 * @param __last Another input iterator.
1689 * @param __result_first A random-access iterator.
1690 * @param __result_last Another random-access iterator.
1691 * @param __comp A comparison functor.
1692 * @return An iterator indicating the end of the resulting sequence.
1694 * Copies and sorts the smallest `N` values from the range
1695 * `[__first, __last)` to the range beginning at `result_first`, where
1696 * the number of elements to be copied, `N`, is the smaller of
1697 * `(__last - __first)` and `(__result_last - __result_first)`.
1698 * After the sort if `i` and `j` are iterators in the range
1699 * `[__result_first, __result_first + N)` such that `i` precedes `j` then
1700 * `__comp(*j, *i)` is false.
1701 * The value returned is `__result_first + N`.
1703 template<typename _InputIterator
, typename _RandomAccessIterator
,
1705 _GLIBCXX20_CONSTEXPR
1706 inline _RandomAccessIterator
1707 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1708 _RandomAccessIterator __result_first
,
1709 _RandomAccessIterator __result_last
,
1712 #ifdef _GLIBCXX_CONCEPT_CHECKS
1713 typedef typename iterator_traits
<_InputIterator
>::value_type
1715 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1719 // concept requirements
1720 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1721 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1722 _RandomAccessIterator
>)
1723 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1725 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1726 _InputValueType
, _OutputValueType
>)
1727 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1728 _OutputValueType
, _OutputValueType
>)
1729 __glibcxx_requires_valid_range(__first
, __last
);
1730 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1731 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1733 return std::__partial_sort_copy(__first
, __last
,
1734 __result_first
, __result_last
,
1735 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1738 /// @cond undocumented
1740 /// This is a helper function for the sort routine.
1741 template<typename _RandomAccessIterator
, typename _Compare
>
1742 _GLIBCXX20_CONSTEXPR
1744 __unguarded_linear_insert(_RandomAccessIterator __last
,
1747 typename iterator_traits
<_RandomAccessIterator
>::value_type
1748 __val
= _GLIBCXX_MOVE(*__last
);
1749 _RandomAccessIterator __next
= __last
;
1751 while (__comp(__val
, __next
))
1753 *__last
= _GLIBCXX_MOVE(*__next
);
1757 *__last
= _GLIBCXX_MOVE(__val
);
1760 /// This is a helper function for the sort routine.
1761 template<typename _RandomAccessIterator
, typename _Compare
>
1762 _GLIBCXX20_CONSTEXPR
1764 __insertion_sort(_RandomAccessIterator __first
,
1765 _RandomAccessIterator __last
, _Compare __comp
)
1767 if (__first
== __last
) return;
1769 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1771 if (__comp(__i
, __first
))
1773 typename iterator_traits
<_RandomAccessIterator
>::value_type
1774 __val
= _GLIBCXX_MOVE(*__i
);
1775 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1776 *__first
= _GLIBCXX_MOVE(__val
);
1779 std::__unguarded_linear_insert(__i
,
1780 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1784 /// This is a helper function for the sort routine.
1785 template<typename _RandomAccessIterator
, typename _Compare
>
1786 _GLIBCXX20_CONSTEXPR
1788 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1789 _RandomAccessIterator __last
, _Compare __comp
)
1791 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1792 std::__unguarded_linear_insert(__i
,
1793 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1798 * This controls some aspect of the sort routines.
1800 enum { _S_threshold
= 16 };
1802 /// This is a helper function for the sort routine.
1803 template<typename _RandomAccessIterator
, typename _Compare
>
1804 _GLIBCXX20_CONSTEXPR
1806 __final_insertion_sort(_RandomAccessIterator __first
,
1807 _RandomAccessIterator __last
, _Compare __comp
)
1809 if (__last
- __first
> int(_S_threshold
))
1811 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1812 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1816 std::__insertion_sort(__first
, __last
, __comp
);
1819 /// This is a helper function...
1820 template<typename _RandomAccessIterator
, typename _Compare
>
1821 _GLIBCXX20_CONSTEXPR
1822 _RandomAccessIterator
1823 __unguarded_partition(_RandomAccessIterator __first
,
1824 _RandomAccessIterator __last
,
1825 _RandomAccessIterator __pivot
, _Compare __comp
)
1829 while (__comp(__first
, __pivot
))
1832 while (__comp(__pivot
, __last
))
1834 if (!(__first
< __last
))
1836 std::iter_swap(__first
, __last
);
1841 /// This is a helper function...
1842 template<typename _RandomAccessIterator
, typename _Compare
>
1843 _GLIBCXX20_CONSTEXPR
1844 inline _RandomAccessIterator
1845 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1846 _RandomAccessIterator __last
, _Compare __comp
)
1848 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1849 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1851 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1854 template<typename _RandomAccessIterator
, typename _Compare
>
1855 _GLIBCXX20_CONSTEXPR
1857 __partial_sort(_RandomAccessIterator __first
,
1858 _RandomAccessIterator __middle
,
1859 _RandomAccessIterator __last
,
1862 std::__heap_select(__first
, __middle
, __last
, __comp
);
1863 std::__sort_heap(__first
, __middle
, __comp
);
1866 /// This is a helper function for the sort routine.
1867 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1868 _GLIBCXX20_CONSTEXPR
1870 __introsort_loop(_RandomAccessIterator __first
,
1871 _RandomAccessIterator __last
,
1872 _Size __depth_limit
, _Compare __comp
)
1874 while (__last
- __first
> int(_S_threshold
))
1876 if (__depth_limit
== 0)
1878 std::__partial_sort(__first
, __last
, __last
, __comp
);
1882 _RandomAccessIterator __cut
=
1883 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1884 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1891 template<typename _RandomAccessIterator
, typename _Compare
>
1892 _GLIBCXX20_CONSTEXPR
1894 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1897 if (__first
!= __last
)
1899 std::__introsort_loop(__first
, __last
,
1900 std::__lg(__last
- __first
) * 2,
1902 std::__final_insertion_sort(__first
, __last
, __comp
);
1906 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1907 _GLIBCXX20_CONSTEXPR
1909 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1910 _RandomAccessIterator __last
, _Size __depth_limit
,
1913 while (__last
- __first
> 3)
1915 if (__depth_limit
== 0)
1917 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1918 // Place the nth largest element in its final position.
1919 std::iter_swap(__first
, __nth
);
1923 _RandomAccessIterator __cut
=
1924 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1930 std::__insertion_sort(__first
, __last
, __comp
);
1937 // lower_bound moved to stl_algobase.h
1940 * @brief Finds the first position in which `__val` could be inserted
1941 * without changing the ordering.
1942 * @ingroup binary_search_algorithms
1943 * @param __first An iterator to the start of a sorted range.
1944 * @param __last A past-the-end iterator for the sorted range.
1945 * @param __val The search term.
1946 * @param __comp A functor to use for comparisons.
1947 * @return An iterator pointing to the first element _not less than_
1948 * `__val`, or `end()` if every element is less than `__val`.
1949 * @ingroup binary_search_algorithms
1951 * The comparison function should have the same effects on ordering as
1952 * the function used for the initial sort.
1954 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1955 _GLIBCXX20_CONSTEXPR
1956 inline _ForwardIterator
1957 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1958 const _Tp
& __val
, _Compare __comp
)
1960 // concept requirements
1961 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1962 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1963 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1964 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
1967 return std::__lower_bound(__first
, __last
, __val
,
1968 __gnu_cxx::__ops::__iter_comp_val(__comp
));
1971 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1972 _GLIBCXX20_CONSTEXPR
1974 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1975 const _Tp
& __val
, _Compare __comp
)
1977 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1980 _DistanceType __len
= std::distance(__first
, __last
);
1984 _DistanceType __half
= __len
>> 1;
1985 _ForwardIterator __middle
= __first
;
1986 std::advance(__middle
, __half
);
1987 if (__comp(__val
, __middle
))
1993 __len
= __len
- __half
- 1;
2000 * @brief Finds the last position in which @p __val could be inserted
2001 * without changing the ordering.
2002 * @ingroup binary_search_algorithms
2003 * @param __first An iterator.
2004 * @param __last Another iterator.
2005 * @param __val The search term.
2006 * @return An iterator pointing to the first element greater than @p __val,
2007 * or end() if no elements are greater than @p __val.
2008 * @ingroup binary_search_algorithms
2010 template<typename _ForwardIterator
, typename _Tp
>
2011 _GLIBCXX20_CONSTEXPR
2012 inline _ForwardIterator
2013 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2016 // concept requirements
2017 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2018 __glibcxx_function_requires(_LessThanOpConcept
<
2019 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2020 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2022 return std::__upper_bound(__first
, __last
, __val
,
2023 __gnu_cxx::__ops::__val_less_iter());
2027 * @brief Finds the last position in which @p __val could be inserted
2028 * without changing the ordering.
2029 * @ingroup binary_search_algorithms
2030 * @param __first An iterator.
2031 * @param __last Another iterator.
2032 * @param __val The search term.
2033 * @param __comp A functor to use for comparisons.
2034 * @return An iterator pointing to the first element greater than @p __val,
2035 * or end() if no elements are greater than @p __val.
2036 * @ingroup binary_search_algorithms
2038 * The comparison function should have the same effects on ordering as
2039 * the function used for the initial sort.
2041 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2042 _GLIBCXX20_CONSTEXPR
2043 inline _ForwardIterator
2044 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2045 const _Tp
& __val
, _Compare __comp
)
2047 // concept requirements
2048 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2049 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2050 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2051 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2054 return std::__upper_bound(__first
, __last
, __val
,
2055 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2058 template<typename _ForwardIterator
, typename _Tp
,
2059 typename _CompareItTp
, typename _CompareTpIt
>
2060 _GLIBCXX20_CONSTEXPR
2061 pair
<_ForwardIterator
, _ForwardIterator
>
2062 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2064 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2066 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2069 _DistanceType __len
= std::distance(__first
, __last
);
2073 _DistanceType __half
= __len
>> 1;
2074 _ForwardIterator __middle
= __first
;
2075 std::advance(__middle
, __half
);
2076 if (__comp_it_val(__middle
, __val
))
2080 __len
= __len
- __half
- 1;
2082 else if (__comp_val_it(__val
, __middle
))
2086 _ForwardIterator __left
2087 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2088 std::advance(__first
, __len
);
2089 _ForwardIterator __right
2090 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2091 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2094 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2098 * @brief Finds the largest subrange in which @p __val could be inserted
2099 * at any place in it without changing the ordering.
2100 * @ingroup binary_search_algorithms
2101 * @param __first An iterator.
2102 * @param __last Another iterator.
2103 * @param __val The search term.
2104 * @return An pair of iterators defining the subrange.
2105 * @ingroup binary_search_algorithms
2107 * This is equivalent to
2109 * std::make_pair(lower_bound(__first, __last, __val),
2110 * upper_bound(__first, __last, __val))
2112 * but does not actually call those functions.
2114 template<typename _ForwardIterator
, typename _Tp
>
2115 _GLIBCXX20_CONSTEXPR
2116 inline pair
<_ForwardIterator
, _ForwardIterator
>
2117 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2120 // concept requirements
2121 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2122 __glibcxx_function_requires(_LessThanOpConcept
<
2123 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2124 __glibcxx_function_requires(_LessThanOpConcept
<
2125 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2126 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2127 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2129 return std::__equal_range(__first
, __last
, __val
,
2130 __gnu_cxx::__ops::__iter_less_val(),
2131 __gnu_cxx::__ops::__val_less_iter());
2135 * @brief Finds the largest subrange in which @p __val could be inserted
2136 * at any place in it without changing the ordering.
2137 * @param __first An iterator.
2138 * @param __last Another iterator.
2139 * @param __val The search term.
2140 * @param __comp A functor to use for comparisons.
2141 * @return An pair of iterators defining the subrange.
2142 * @ingroup binary_search_algorithms
2144 * This is equivalent to
2146 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2147 * upper_bound(__first, __last, __val, __comp))
2149 * but does not actually call those functions.
2151 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2152 _GLIBCXX20_CONSTEXPR
2153 inline pair
<_ForwardIterator
, _ForwardIterator
>
2154 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2155 const _Tp
& __val
, _Compare __comp
)
2157 // concept requirements
2158 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2159 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2160 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2161 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2162 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2163 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2165 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2168 return std::__equal_range(__first
, __last
, __val
,
2169 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2170 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2174 * @brief Determines whether an element exists in a range.
2175 * @ingroup binary_search_algorithms
2176 * @param __first An iterator.
2177 * @param __last Another iterator.
2178 * @param __val The search term.
2179 * @return True if @p __val (or its equivalent) is in [@p
2180 * __first,@p __last ].
2182 * Note that this does not actually return an iterator to @p __val. For
2183 * that, use std::find or a container's specialized find member functions.
2185 template<typename _ForwardIterator
, typename _Tp
>
2186 _GLIBCXX20_CONSTEXPR
2188 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2191 // concept requirements
2192 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2193 __glibcxx_function_requires(_LessThanOpConcept
<
2194 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2195 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2196 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2198 _ForwardIterator __i
2199 = std::__lower_bound(__first
, __last
, __val
,
2200 __gnu_cxx::__ops::__iter_less_val());
2201 return __i
!= __last
&& !(__val
< *__i
);
2205 * @brief Determines whether an element exists in a range.
2206 * @ingroup binary_search_algorithms
2207 * @param __first An iterator.
2208 * @param __last Another iterator.
2209 * @param __val The search term.
2210 * @param __comp A functor to use for comparisons.
2211 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2213 * Note that this does not actually return an iterator to @p __val. For
2214 * that, use std::find or a container's specialized find member functions.
2216 * The comparison function should have the same effects on ordering as
2217 * the function used for the initial sort.
2219 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2220 _GLIBCXX20_CONSTEXPR
2222 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2223 const _Tp
& __val
, _Compare __comp
)
2225 // concept requirements
2226 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2227 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2228 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2229 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2231 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2234 _ForwardIterator __i
2235 = std::__lower_bound(__first
, __last
, __val
,
2236 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2237 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2242 /// This is a helper function for the __merge_adaptive routines.
2243 template<typename _InputIterator1
, typename _InputIterator2
,
2244 typename _OutputIterator
, typename _Compare
>
2246 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2247 _InputIterator2 __first2
, _InputIterator2 __last2
,
2248 _OutputIterator __result
, _Compare __comp
)
2250 while (__first1
!= __last1
&& __first2
!= __last2
)
2252 if (__comp(__first2
, __first1
))
2254 *__result
= _GLIBCXX_MOVE(*__first2
);
2259 *__result
= _GLIBCXX_MOVE(*__first1
);
2264 if (__first1
!= __last1
)
2265 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2268 /// This is a helper function for the __merge_adaptive routines.
2269 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2270 typename _BidirectionalIterator3
, typename _Compare
>
2272 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2273 _BidirectionalIterator1 __last1
,
2274 _BidirectionalIterator2 __first2
,
2275 _BidirectionalIterator2 __last2
,
2276 _BidirectionalIterator3 __result
,
2279 if (__first1
== __last1
)
2281 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2284 else if (__first2
== __last2
)
2291 if (__comp(__last2
, __last1
))
2293 *--__result
= _GLIBCXX_MOVE(*__last1
);
2294 if (__first1
== __last1
)
2296 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2303 *--__result
= _GLIBCXX_MOVE(*__last2
);
2304 if (__first2
== __last2
)
2311 /// This is a helper function for the merge routines.
2312 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2314 _BidirectionalIterator1
2315 __rotate_adaptive(_BidirectionalIterator1 __first
,
2316 _BidirectionalIterator1 __middle
,
2317 _BidirectionalIterator1 __last
,
2318 _Distance __len1
, _Distance __len2
,
2319 _BidirectionalIterator2 __buffer
,
2320 _Distance __buffer_size
)
2322 _BidirectionalIterator2 __buffer_end
;
2323 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2327 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2328 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2329 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2334 else if (__len1
<= __buffer_size
)
2338 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2339 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2340 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2346 return std::rotate(__first
, __middle
, __last
);
2349 /// This is a helper function for the merge routines.
2350 template<typename _BidirectionalIterator
, typename _Distance
,
2351 typename _Pointer
, typename _Compare
>
2353 __merge_adaptive(_BidirectionalIterator __first
,
2354 _BidirectionalIterator __middle
,
2355 _BidirectionalIterator __last
,
2356 _Distance __len1
, _Distance __len2
,
2357 _Pointer __buffer
, _Compare __comp
)
2359 if (__len1
<= __len2
)
2361 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2362 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2367 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2368 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2369 __buffer_end
, __last
, __comp
);
2373 template<typename _BidirectionalIterator
, typename _Distance
,
2374 typename _Pointer
, typename _Compare
>
2376 __merge_adaptive_resize(_BidirectionalIterator __first
,
2377 _BidirectionalIterator __middle
,
2378 _BidirectionalIterator __last
,
2379 _Distance __len1
, _Distance __len2
,
2380 _Pointer __buffer
, _Distance __buffer_size
,
2383 if (__len1
<= __buffer_size
|| __len2
<= __buffer_size
)
2384 std::__merge_adaptive(__first
, __middle
, __last
,
2385 __len1
, __len2
, __buffer
, __comp
);
2388 _BidirectionalIterator __first_cut
= __first
;
2389 _BidirectionalIterator __second_cut
= __middle
;
2390 _Distance __len11
= 0;
2391 _Distance __len22
= 0;
2392 if (__len1
> __len2
)
2394 __len11
= __len1
/ 2;
2395 std::advance(__first_cut
, __len11
);
2397 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2398 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2399 __len22
= std::distance(__middle
, __second_cut
);
2403 __len22
= __len2
/ 2;
2404 std::advance(__second_cut
, __len22
);
2406 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2407 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2408 __len11
= std::distance(__first
, __first_cut
);
2411 _BidirectionalIterator __new_middle
2412 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2413 _Distance(__len1
- __len11
), __len22
,
2414 __buffer
, __buffer_size
);
2415 std::__merge_adaptive_resize(__first
, __first_cut
, __new_middle
,
2417 __buffer
, __buffer_size
, __comp
);
2418 std::__merge_adaptive_resize(__new_middle
, __second_cut
, __last
,
2419 _Distance(__len1
- __len11
),
2420 _Distance(__len2
- __len22
),
2421 __buffer
, __buffer_size
, __comp
);
2425 /// This is a helper function for the merge routines.
2426 template<typename _BidirectionalIterator
, typename _Distance
,
2429 __merge_without_buffer(_BidirectionalIterator __first
,
2430 _BidirectionalIterator __middle
,
2431 _BidirectionalIterator __last
,
2432 _Distance __len1
, _Distance __len2
,
2435 if (__len1
== 0 || __len2
== 0)
2438 if (__len1
+ __len2
== 2)
2440 if (__comp(__middle
, __first
))
2441 std::iter_swap(__first
, __middle
);
2445 _BidirectionalIterator __first_cut
= __first
;
2446 _BidirectionalIterator __second_cut
= __middle
;
2447 _Distance __len11
= 0;
2448 _Distance __len22
= 0;
2449 if (__len1
> __len2
)
2451 __len11
= __len1
/ 2;
2452 std::advance(__first_cut
, __len11
);
2454 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2455 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2456 __len22
= std::distance(__middle
, __second_cut
);
2460 __len22
= __len2
/ 2;
2461 std::advance(__second_cut
, __len22
);
2463 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2464 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2465 __len11
= std::distance(__first
, __first_cut
);
2468 _BidirectionalIterator __new_middle
2469 = std::rotate(__first_cut
, __middle
, __second_cut
);
2470 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2471 __len11
, __len22
, __comp
);
2472 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2473 __len1
- __len11
, __len2
- __len22
, __comp
);
2476 template<typename _BidirectionalIterator
, typename _Compare
>
2478 __inplace_merge(_BidirectionalIterator __first
,
2479 _BidirectionalIterator __middle
,
2480 _BidirectionalIterator __last
,
2483 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2485 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2488 if (__first
== __middle
|| __middle
== __last
)
2491 const _DistanceType __len1
= std::distance(__first
, __middle
);
2492 const _DistanceType __len2
= std::distance(__middle
, __last
);
2495 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2496 // __merge_adaptive will use a buffer for the smaller of
2497 // [first,middle) and [middle,last).
2498 _TmpBuf
__buf(__first
, std::min(__len1
, __len2
));
2500 if (__builtin_expect(__buf
.size() == __buf
.requested_size(), true))
2501 std::__merge_adaptive
2502 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(), __comp
);
2503 else if (__builtin_expect(__buf
.begin() == 0, false))
2504 std::__merge_without_buffer
2505 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2507 std::__merge_adaptive_resize
2508 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2509 _DistanceType(__buf
.size()), __comp
);
2511 std::__merge_without_buffer
2512 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2517 * @brief Merges two sorted ranges in place.
2518 * @ingroup sorting_algorithms
2519 * @param __first An iterator.
2520 * @param __middle Another iterator.
2521 * @param __last Another iterator.
2524 * Merges two sorted and consecutive ranges, [__first,__middle) and
2525 * [__middle,__last), and puts the result in [__first,__last). The
2526 * output will be sorted. The sort is @e stable, that is, for
2527 * equivalent elements in the two ranges, elements from the first
2528 * range will always come before elements from the second.
2530 * If enough additional memory is available, this takes (__last-__first)-1
2531 * comparisons. Otherwise an NlogN algorithm is used, where N is
2532 * distance(__first,__last).
2534 template<typename _BidirectionalIterator
>
2536 inplace_merge(_BidirectionalIterator __first
,
2537 _BidirectionalIterator __middle
,
2538 _BidirectionalIterator __last
)
2540 // concept requirements
2541 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2542 _BidirectionalIterator
>)
2543 __glibcxx_function_requires(_LessThanComparableConcept
<
2544 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2545 __glibcxx_requires_sorted(__first
, __middle
);
2546 __glibcxx_requires_sorted(__middle
, __last
);
2547 __glibcxx_requires_irreflexive(__first
, __last
);
2549 std::__inplace_merge(__first
, __middle
, __last
,
2550 __gnu_cxx::__ops::__iter_less_iter());
2554 * @brief Merges two sorted ranges in place.
2555 * @ingroup sorting_algorithms
2556 * @param __first An iterator.
2557 * @param __middle Another iterator.
2558 * @param __last Another iterator.
2559 * @param __comp A functor to use for comparisons.
2562 * Merges two sorted and consecutive ranges, [__first,__middle) and
2563 * [middle,last), and puts the result in [__first,__last). The output will
2564 * be sorted. The sort is @e stable, that is, for equivalent
2565 * elements in the two ranges, elements from the first range will always
2566 * 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 * The comparison function should have the same effects on ordering as
2573 * the function used for the initial sort.
2575 template<typename _BidirectionalIterator
, typename _Compare
>
2577 inplace_merge(_BidirectionalIterator __first
,
2578 _BidirectionalIterator __middle
,
2579 _BidirectionalIterator __last
,
2582 // concept requirements
2583 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2584 _BidirectionalIterator
>)
2585 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2586 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2587 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2588 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2589 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2590 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2592 std::__inplace_merge(__first
, __middle
, __last
,
2593 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2597 /// This is a helper function for the __merge_sort_loop routines.
2598 template<typename _InputIterator
, typename _OutputIterator
,
2601 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2602 _InputIterator __first2
, _InputIterator __last2
,
2603 _OutputIterator __result
, _Compare __comp
)
2605 while (__first1
!= __last1
&& __first2
!= __last2
)
2607 if (__comp(__first2
, __first1
))
2609 *__result
= _GLIBCXX_MOVE(*__first2
);
2614 *__result
= _GLIBCXX_MOVE(*__first1
);
2619 return _GLIBCXX_MOVE3(__first2
, __last2
,
2620 _GLIBCXX_MOVE3(__first1
, __last1
,
2624 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2625 typename _Distance
, typename _Compare
>
2627 __merge_sort_loop(_RandomAccessIterator1 __first
,
2628 _RandomAccessIterator1 __last
,
2629 _RandomAccessIterator2 __result
, _Distance __step_size
,
2632 const _Distance __two_step
= 2 * __step_size
;
2634 while (__last
- __first
>= __two_step
)
2636 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2637 __first
+ __step_size
,
2638 __first
+ __two_step
,
2640 __first
+= __two_step
;
2642 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2644 std::__move_merge(__first
, __first
+ __step_size
,
2645 __first
+ __step_size
, __last
, __result
, __comp
);
2648 template<typename _RandomAccessIterator
, typename _Distance
,
2650 _GLIBCXX20_CONSTEXPR
2652 __chunk_insertion_sort(_RandomAccessIterator __first
,
2653 _RandomAccessIterator __last
,
2654 _Distance __chunk_size
, _Compare __comp
)
2656 while (__last
- __first
>= __chunk_size
)
2658 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2659 __first
+= __chunk_size
;
2661 std::__insertion_sort(__first
, __last
, __comp
);
2664 enum { _S_chunk_size
= 7 };
2666 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2668 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2669 _RandomAccessIterator __last
,
2670 _Pointer __buffer
, _Compare __comp
)
2672 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2675 const _Distance __len
= __last
- __first
;
2676 const _Pointer __buffer_last
= __buffer
+ __len
;
2678 _Distance __step_size
= _S_chunk_size
;
2679 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2681 while (__step_size
< __len
)
2683 std::__merge_sort_loop(__first
, __last
, __buffer
,
2684 __step_size
, __comp
);
2686 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2687 __step_size
, __comp
);
2692 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2694 __stable_sort_adaptive(_RandomAccessIterator __first
,
2695 _RandomAccessIterator __middle
,
2696 _RandomAccessIterator __last
,
2697 _Pointer __buffer
, _Compare __comp
)
2699 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2700 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2702 std::__merge_adaptive(__first
, __middle
, __last
,
2703 __middle
- __first
, __last
- __middle
,
2707 template<typename _RandomAccessIterator
, typename _Pointer
,
2708 typename _Distance
, typename _Compare
>
2710 __stable_sort_adaptive_resize(_RandomAccessIterator __first
,
2711 _RandomAccessIterator __last
,
2712 _Pointer __buffer
, _Distance __buffer_size
,
2715 const _Distance __len
= (__last
- __first
+ 1) / 2;
2716 const _RandomAccessIterator __middle
= __first
+ __len
;
2717 if (__len
> __buffer_size
)
2719 std::__stable_sort_adaptive_resize(__first
, __middle
, __buffer
,
2720 __buffer_size
, __comp
);
2721 std::__stable_sort_adaptive_resize(__middle
, __last
, __buffer
,
2722 __buffer_size
, __comp
);
2723 std::__merge_adaptive_resize(__first
, __middle
, __last
,
2724 _Distance(__middle
- __first
),
2725 _Distance(__last
- __middle
),
2726 __buffer
, __buffer_size
,
2730 std::__stable_sort_adaptive(__first
, __middle
, __last
,
2734 /// This is a helper function for the stable sorting routines.
2735 template<typename _RandomAccessIterator
, typename _Compare
>
2737 __inplace_stable_sort(_RandomAccessIterator __first
,
2738 _RandomAccessIterator __last
, _Compare __comp
)
2740 if (__last
- __first
< 15)
2742 std::__insertion_sort(__first
, __last
, __comp
);
2745 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2746 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2747 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2748 std::__merge_without_buffer(__first
, __middle
, __last
,
2756 // Set algorithms: includes, set_union, set_intersection, set_difference,
2757 // set_symmetric_difference. All of these algorithms have the precondition
2758 // that their input ranges are sorted and the postcondition that their output
2759 // ranges are sorted.
2761 template<typename _InputIterator1
, typename _InputIterator2
,
2763 _GLIBCXX20_CONSTEXPR
2765 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2766 _InputIterator2 __first2
, _InputIterator2 __last2
,
2769 while (__first1
!= __last1
&& __first2
!= __last2
)
2771 if (__comp(__first2
, __first1
))
2773 if (!__comp(__first1
, __first2
))
2778 return __first2
== __last2
;
2782 * @brief Determines whether all elements of a sequence exists in a range.
2783 * @param __first1 Start of search range.
2784 * @param __last1 End of search range.
2785 * @param __first2 Start of sequence
2786 * @param __last2 End of sequence.
2787 * @return True if each element in [__first2,__last2) is contained in order
2788 * within [__first1,__last1). False otherwise.
2789 * @ingroup set_algorithms
2791 * This operation expects both [__first1,__last1) and
2792 * [__first2,__last2) to be sorted. Searches for the presence of
2793 * each element in [__first2,__last2) within [__first1,__last1).
2794 * The iterators over each range only move forward, so this is a
2795 * linear algorithm. If an element in [__first2,__last2) is not
2796 * found before the search iterator reaches @p __last2, false is
2799 template<typename _InputIterator1
, typename _InputIterator2
>
2800 _GLIBCXX20_CONSTEXPR
2802 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2803 _InputIterator2 __first2
, _InputIterator2 __last2
)
2805 // concept requirements
2806 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2807 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2808 __glibcxx_function_requires(_LessThanOpConcept
<
2809 typename iterator_traits
<_InputIterator1
>::value_type
,
2810 typename iterator_traits
<_InputIterator2
>::value_type
>)
2811 __glibcxx_function_requires(_LessThanOpConcept
<
2812 typename iterator_traits
<_InputIterator2
>::value_type
,
2813 typename iterator_traits
<_InputIterator1
>::value_type
>)
2814 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2815 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2816 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2817 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2819 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2820 __gnu_cxx::__ops::__iter_less_iter());
2824 * @brief Determines whether all elements of a sequence exists in a range
2826 * @ingroup set_algorithms
2827 * @param __first1 Start of search range.
2828 * @param __last1 End of search range.
2829 * @param __first2 Start of sequence
2830 * @param __last2 End of sequence.
2831 * @param __comp Comparison function to use.
2832 * @return True if each element in [__first2,__last2) is contained
2833 * in order within [__first1,__last1) according to comp. False
2834 * otherwise. @ingroup set_algorithms
2836 * This operation expects both [__first1,__last1) and
2837 * [__first2,__last2) to be sorted. Searches for the presence of
2838 * each element in [__first2,__last2) within [__first1,__last1),
2839 * using comp to decide. The iterators over each range only move
2840 * forward, so this is a linear algorithm. If an element in
2841 * [__first2,__last2) is not found before the search iterator
2842 * reaches @p __last2, false is returned.
2844 template<typename _InputIterator1
, typename _InputIterator2
,
2846 _GLIBCXX20_CONSTEXPR
2848 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2849 _InputIterator2 __first2
, _InputIterator2 __last2
,
2852 // concept requirements
2853 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2854 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2855 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2856 typename iterator_traits
<_InputIterator1
>::value_type
,
2857 typename iterator_traits
<_InputIterator2
>::value_type
>)
2858 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2859 typename iterator_traits
<_InputIterator2
>::value_type
,
2860 typename iterator_traits
<_InputIterator1
>::value_type
>)
2861 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2862 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2863 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2864 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2866 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2867 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2876 // set_symmetric_difference
2880 template<typename _BidirectionalIterator
, typename _Compare
>
2881 _GLIBCXX20_CONSTEXPR
2883 __next_permutation(_BidirectionalIterator __first
,
2884 _BidirectionalIterator __last
, _Compare __comp
)
2886 if (__first
== __last
)
2888 _BidirectionalIterator __i
= __first
;
2897 _BidirectionalIterator __ii
= __i
;
2899 if (__comp(__i
, __ii
))
2901 _BidirectionalIterator __j
= __last
;
2902 while (!__comp(__i
, --__j
))
2904 std::iter_swap(__i
, __j
);
2905 std::__reverse(__ii
, __last
,
2906 std::__iterator_category(__first
));
2911 std::__reverse(__first
, __last
,
2912 std::__iterator_category(__first
));
2919 * @brief Permute range into the next @e dictionary ordering.
2920 * @ingroup sorting_algorithms
2921 * @param __first Start of range.
2922 * @param __last End of range.
2923 * @return False if wrapped to first permutation, true otherwise.
2925 * Treats all permutations of the range as a set of @e dictionary sorted
2926 * sequences. Permutes the current sequence into the next one of this set.
2927 * Returns true if there are more sequences to generate. If the sequence
2928 * is the largest of the set, the smallest is generated and false returned.
2930 template<typename _BidirectionalIterator
>
2931 _GLIBCXX20_CONSTEXPR
2933 next_permutation(_BidirectionalIterator __first
,
2934 _BidirectionalIterator __last
)
2936 // concept requirements
2937 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2938 _BidirectionalIterator
>)
2939 __glibcxx_function_requires(_LessThanComparableConcept
<
2940 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2941 __glibcxx_requires_valid_range(__first
, __last
);
2942 __glibcxx_requires_irreflexive(__first
, __last
);
2944 return std::__next_permutation
2945 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2949 * @brief Permute range into the next @e dictionary ordering using
2950 * comparison functor.
2951 * @ingroup sorting_algorithms
2952 * @param __first Start of range.
2953 * @param __last End of range.
2954 * @param __comp A comparison functor.
2955 * @return False if wrapped to first permutation, true otherwise.
2957 * Treats all permutations of the range [__first,__last) as a set of
2958 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2959 * sequence into the next one of this set. Returns true if there are more
2960 * sequences to generate. If the sequence is the largest of the set, the
2961 * smallest is generated and false returned.
2963 template<typename _BidirectionalIterator
, typename _Compare
>
2964 _GLIBCXX20_CONSTEXPR
2966 next_permutation(_BidirectionalIterator __first
,
2967 _BidirectionalIterator __last
, _Compare __comp
)
2969 // concept requirements
2970 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2971 _BidirectionalIterator
>)
2972 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2973 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2974 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2975 __glibcxx_requires_valid_range(__first
, __last
);
2976 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2978 return std::__next_permutation
2979 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2982 template<typename _BidirectionalIterator
, typename _Compare
>
2983 _GLIBCXX20_CONSTEXPR
2985 __prev_permutation(_BidirectionalIterator __first
,
2986 _BidirectionalIterator __last
, _Compare __comp
)
2988 if (__first
== __last
)
2990 _BidirectionalIterator __i
= __first
;
2999 _BidirectionalIterator __ii
= __i
;
3001 if (__comp(__ii
, __i
))
3003 _BidirectionalIterator __j
= __last
;
3004 while (!__comp(--__j
, __i
))
3006 std::iter_swap(__i
, __j
);
3007 std::__reverse(__ii
, __last
,
3008 std::__iterator_category(__first
));
3013 std::__reverse(__first
, __last
,
3014 std::__iterator_category(__first
));
3021 * @brief Permute range into the previous @e dictionary ordering.
3022 * @ingroup sorting_algorithms
3023 * @param __first Start of range.
3024 * @param __last End of range.
3025 * @return False if wrapped to last permutation, true otherwise.
3027 * Treats all permutations of the range as a set of @e dictionary sorted
3028 * sequences. Permutes the current sequence into the previous one of this
3029 * set. Returns true if there are more sequences to generate. If the
3030 * sequence is the smallest of the set, the largest is generated and false
3033 template<typename _BidirectionalIterator
>
3034 _GLIBCXX20_CONSTEXPR
3036 prev_permutation(_BidirectionalIterator __first
,
3037 _BidirectionalIterator __last
)
3039 // concept requirements
3040 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3041 _BidirectionalIterator
>)
3042 __glibcxx_function_requires(_LessThanComparableConcept
<
3043 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3044 __glibcxx_requires_valid_range(__first
, __last
);
3045 __glibcxx_requires_irreflexive(__first
, __last
);
3047 return std::__prev_permutation(__first
, __last
,
3048 __gnu_cxx::__ops::__iter_less_iter());
3052 * @brief Permute range into the previous @e dictionary ordering using
3053 * comparison functor.
3054 * @ingroup sorting_algorithms
3055 * @param __first Start of range.
3056 * @param __last End of range.
3057 * @param __comp A comparison functor.
3058 * @return False if wrapped to last permutation, true otherwise.
3060 * Treats all permutations of the range [__first,__last) as a set of
3061 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3062 * sequence into the previous one of this set. Returns true if there are
3063 * more sequences to generate. If the sequence is the smallest of the set,
3064 * the largest is generated and false returned.
3066 template<typename _BidirectionalIterator
, typename _Compare
>
3067 _GLIBCXX20_CONSTEXPR
3069 prev_permutation(_BidirectionalIterator __first
,
3070 _BidirectionalIterator __last
, _Compare __comp
)
3072 // concept requirements
3073 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3074 _BidirectionalIterator
>)
3075 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3076 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3077 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3078 __glibcxx_requires_valid_range(__first
, __last
);
3079 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3081 return std::__prev_permutation(__first
, __last
,
3082 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3088 template<typename _InputIterator
, typename _OutputIterator
,
3089 typename _Predicate
, typename _Tp
>
3090 _GLIBCXX20_CONSTEXPR
3092 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3093 _OutputIterator __result
,
3094 _Predicate __pred
, const _Tp
& __new_value
)
3096 for (; __first
!= __last
; ++__first
, (void)++__result
)
3097 if (__pred(__first
))
3098 *__result
= __new_value
;
3100 *__result
= *__first
;
3105 * @brief Copy a sequence, replacing each element of one value with another
3107 * @param __first An input iterator.
3108 * @param __last An input iterator.
3109 * @param __result An output iterator.
3110 * @param __old_value The value to be replaced.
3111 * @param __new_value The replacement value.
3112 * @return The end of the output sequence, @p result+(last-first).
3114 * Copies each element in the input range @p [__first,__last) to the
3115 * output range @p [__result,__result+(__last-__first)) replacing elements
3116 * equal to @p __old_value with @p __new_value.
3118 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3119 _GLIBCXX20_CONSTEXPR
3120 inline _OutputIterator
3121 replace_copy(_InputIterator __first
, _InputIterator __last
,
3122 _OutputIterator __result
,
3123 const _Tp
& __old_value
, const _Tp
& __new_value
)
3125 // concept requirements
3126 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3127 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3128 typename iterator_traits
<_InputIterator
>::value_type
>)
3129 __glibcxx_function_requires(_EqualOpConcept
<
3130 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3131 __glibcxx_requires_valid_range(__first
, __last
);
3133 return std::__replace_copy_if(__first
, __last
, __result
,
3134 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3139 * @brief Copy a sequence, replacing each value for which a predicate
3140 * returns true with another value.
3141 * @ingroup mutating_algorithms
3142 * @param __first An input iterator.
3143 * @param __last An input iterator.
3144 * @param __result An output iterator.
3145 * @param __pred A predicate.
3146 * @param __new_value The replacement value.
3147 * @return The end of the output sequence, @p __result+(__last-__first).
3149 * Copies each element in the range @p [__first,__last) to the range
3150 * @p [__result,__result+(__last-__first)) replacing elements for which
3151 * @p __pred returns true with @p __new_value.
3153 template<typename _InputIterator
, typename _OutputIterator
,
3154 typename _Predicate
, typename _Tp
>
3155 _GLIBCXX20_CONSTEXPR
3156 inline _OutputIterator
3157 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3158 _OutputIterator __result
,
3159 _Predicate __pred
, const _Tp
& __new_value
)
3161 // concept requirements
3162 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3163 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3164 typename iterator_traits
<_InputIterator
>::value_type
>)
3165 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3166 typename iterator_traits
<_InputIterator
>::value_type
>)
3167 __glibcxx_requires_valid_range(__first
, __last
);
3169 return std::__replace_copy_if(__first
, __last
, __result
,
3170 __gnu_cxx::__ops::__pred_iter(__pred
),
3174 #if __cplusplus >= 201103L
3176 * @brief Determines whether the elements of a sequence are sorted.
3177 * @ingroup sorting_algorithms
3178 * @param __first An iterator.
3179 * @param __last Another iterator.
3180 * @return True if the elements are sorted, false otherwise.
3182 template<typename _ForwardIterator
>
3183 _GLIBCXX20_CONSTEXPR
3185 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3186 { return std::is_sorted_until(__first
, __last
) == __last
; }
3189 * @brief Determines whether the elements of a sequence are sorted
3190 * according to a comparison functor.
3191 * @ingroup sorting_algorithms
3192 * @param __first An iterator.
3193 * @param __last Another iterator.
3194 * @param __comp A comparison functor.
3195 * @return True if the elements are sorted, false otherwise.
3197 template<typename _ForwardIterator
, typename _Compare
>
3198 _GLIBCXX20_CONSTEXPR
3200 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3202 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3204 template<typename _ForwardIterator
, typename _Compare
>
3205 _GLIBCXX20_CONSTEXPR
3207 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3210 if (__first
== __last
)
3213 _ForwardIterator __next
= __first
;
3214 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3215 if (__comp(__next
, __first
))
3221 * @brief Determines the end of a sorted sequence.
3222 * @ingroup sorting_algorithms
3223 * @param __first An iterator.
3224 * @param __last Another iterator.
3225 * @return An iterator pointing to the last iterator i in [__first, __last)
3226 * for which the range [__first, i) is sorted.
3228 template<typename _ForwardIterator
>
3229 _GLIBCXX20_CONSTEXPR
3230 inline _ForwardIterator
3231 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3233 // concept requirements
3234 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3235 __glibcxx_function_requires(_LessThanComparableConcept
<
3236 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3237 __glibcxx_requires_valid_range(__first
, __last
);
3238 __glibcxx_requires_irreflexive(__first
, __last
);
3240 return std::__is_sorted_until(__first
, __last
,
3241 __gnu_cxx::__ops::__iter_less_iter());
3245 * @brief Determines the end of a sorted sequence using comparison functor.
3246 * @ingroup sorting_algorithms
3247 * @param __first An iterator.
3248 * @param __last Another iterator.
3249 * @param __comp A comparison functor.
3250 * @return An iterator pointing to the last iterator i in [__first, __last)
3251 * for which the range [__first, i) is sorted.
3253 template<typename _ForwardIterator
, typename _Compare
>
3254 _GLIBCXX20_CONSTEXPR
3255 inline _ForwardIterator
3256 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3259 // concept requirements
3260 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3261 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3262 typename iterator_traits
<_ForwardIterator
>::value_type
,
3263 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3264 __glibcxx_requires_valid_range(__first
, __last
);
3265 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3267 return std::__is_sorted_until(__first
, __last
,
3268 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3272 * @brief Determines min and max at once as an ordered pair.
3273 * @ingroup sorting_algorithms
3274 * @param __a A thing of arbitrary type.
3275 * @param __b Another thing of arbitrary type.
3276 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3279 template<typename _Tp
>
3280 _GLIBCXX14_CONSTEXPR
3281 inline pair
<const _Tp
&, const _Tp
&>
3282 minmax(const _Tp
& __a
, const _Tp
& __b
)
3284 // concept requirements
3285 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3287 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3288 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3292 * @brief Determines min and max at once as an ordered pair.
3293 * @ingroup sorting_algorithms
3294 * @param __a A thing of arbitrary type.
3295 * @param __b Another thing of arbitrary type.
3296 * @param __comp A @link comparison_functors comparison functor @endlink.
3297 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3300 template<typename _Tp
, typename _Compare
>
3301 _GLIBCXX14_CONSTEXPR
3302 inline pair
<const _Tp
&, const _Tp
&>
3303 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3305 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3306 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3309 template<typename _ForwardIterator
, typename _Compare
>
3310 _GLIBCXX14_CONSTEXPR
3311 pair
<_ForwardIterator
, _ForwardIterator
>
3312 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3315 _ForwardIterator __next
= __first
;
3316 if (__first
== __last
3317 || ++__next
== __last
)
3318 return std::make_pair(__first
, __first
);
3320 _ForwardIterator __min
{}, __max
{};
3321 if (__comp(__next
, __first
))
3335 while (__first
!= __last
)
3338 if (++__next
== __last
)
3340 if (__comp(__first
, __min
))
3342 else if (!__comp(__first
, __max
))
3347 if (__comp(__next
, __first
))
3349 if (__comp(__next
, __min
))
3351 if (!__comp(__first
, __max
))
3356 if (__comp(__first
, __min
))
3358 if (!__comp(__next
, __max
))
3366 return std::make_pair(__min
, __max
);
3370 * @brief Return a pair of iterators pointing to the minimum and maximum
3371 * elements in a range.
3372 * @ingroup sorting_algorithms
3373 * @param __first Start of range.
3374 * @param __last End of range.
3375 * @return make_pair(m, M), where m is the first iterator i in
3376 * [__first, __last) such that no other element in the range is
3377 * smaller, and where M is the last iterator i in [__first, __last)
3378 * such that no other element in the range is larger.
3380 template<typename _ForwardIterator
>
3381 _GLIBCXX14_CONSTEXPR
3382 inline pair
<_ForwardIterator
, _ForwardIterator
>
3383 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3385 // concept requirements
3386 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3387 __glibcxx_function_requires(_LessThanComparableConcept
<
3388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3389 __glibcxx_requires_valid_range(__first
, __last
);
3390 __glibcxx_requires_irreflexive(__first
, __last
);
3392 return std::__minmax_element(__first
, __last
,
3393 __gnu_cxx::__ops::__iter_less_iter());
3397 * @brief Return a pair of iterators pointing to the minimum and maximum
3398 * elements in a range.
3399 * @ingroup sorting_algorithms
3400 * @param __first Start of range.
3401 * @param __last End of range.
3402 * @param __comp Comparison functor.
3403 * @return make_pair(m, M), where m is the first iterator i in
3404 * [__first, __last) such that no other element in the range is
3405 * smaller, and where M is the last iterator i in [__first, __last)
3406 * such that no other element in the range is larger.
3408 template<typename _ForwardIterator
, typename _Compare
>
3409 _GLIBCXX14_CONSTEXPR
3410 inline pair
<_ForwardIterator
, _ForwardIterator
>
3411 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3414 // concept requirements
3415 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3416 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3417 typename iterator_traits
<_ForwardIterator
>::value_type
,
3418 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3419 __glibcxx_requires_valid_range(__first
, __last
);
3420 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3422 return std::__minmax_element(__first
, __last
,
3423 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3426 template<typename _Tp
>
3427 _GLIBCXX14_CONSTEXPR
3428 inline pair
<_Tp
, _Tp
>
3429 minmax(initializer_list
<_Tp
> __l
)
3431 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
3432 pair
<const _Tp
*, const _Tp
*> __p
=
3433 std::__minmax_element(__l
.begin(), __l
.end(),
3434 __gnu_cxx::__ops::__iter_less_iter());
3435 return std::make_pair(*__p
.first
, *__p
.second
);
3438 template<typename _Tp
, typename _Compare
>
3439 _GLIBCXX14_CONSTEXPR
3440 inline pair
<_Tp
, _Tp
>
3441 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3443 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
3444 pair
<const _Tp
*, const _Tp
*> __p
=
3445 std::__minmax_element(__l
.begin(), __l
.end(),
3446 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3447 return std::make_pair(*__p
.first
, *__p
.second
);
3451 * @brief Checks whether a permutation of the second sequence is equal
3452 * to the first sequence.
3453 * @ingroup non_mutating_algorithms
3454 * @param __first1 Start of first range.
3455 * @param __last1 End of first range.
3456 * @param __first2 Start of second range.
3457 * @param __pred A binary predicate.
3458 * @return true if there exists a permutation of the elements in
3459 * the range [__first2, __first2 + (__last1 - __first1)),
3460 * beginning with ForwardIterator2 begin, such that
3461 * equal(__first1, __last1, __begin, __pred) returns true;
3462 * otherwise, returns false.
3464 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3465 typename _BinaryPredicate
>
3466 _GLIBCXX20_CONSTEXPR
3468 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3469 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3471 // concept requirements
3472 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3473 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3474 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3475 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3476 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3477 __glibcxx_requires_valid_range(__first1
, __last1
);
3479 return std::__is_permutation(__first1
, __last1
, __first2
,
3480 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3483 #if __cplusplus > 201103L
3484 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3485 typename _BinaryPredicate
>
3486 _GLIBCXX20_CONSTEXPR
3488 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3489 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3490 _BinaryPredicate __pred
)
3493 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3495 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3496 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3497 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3498 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3501 auto __d1
= std::distance(__first1
, __last1
);
3502 auto __d2
= std::distance(__first2
, __last2
);
3507 // Efficiently compare identical prefixes: O(N) if sequences
3508 // have the same elements in the same order.
3509 for (; __first1
!= __last1
&& __first2
!= __last2
;
3510 ++__first1
, (void)++__first2
)
3511 if (!__pred(__first1
, __first2
))
3516 if (__first1
== __last1
)
3521 auto __d1
= std::distance(__first1
, __last1
);
3522 auto __d2
= std::distance(__first2
, __last2
);
3523 if (__d1
== 0 && __d2
== 0)
3529 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3531 if (__scan
!= std::__find_if(__first1
, __scan
,
3532 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3533 continue; // We've seen this one before.
3535 auto __matches
= std::__count_if(__first2
, __last2
,
3536 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3538 || std::__count_if(__scan
, __last1
,
3539 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3547 * @brief Checks whether a permutaion of the second sequence is equal
3548 * to the first sequence.
3549 * @ingroup non_mutating_algorithms
3550 * @param __first1 Start of first range.
3551 * @param __last1 End of first range.
3552 * @param __first2 Start of second range.
3553 * @param __last2 End of first range.
3554 * @return true if there exists a permutation of the elements in the range
3555 * [__first2, __last2), beginning with ForwardIterator2 begin,
3556 * such that equal(__first1, __last1, begin) returns true;
3557 * otherwise, returns false.
3559 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3560 _GLIBCXX20_CONSTEXPR
3562 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3563 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3565 __glibcxx_requires_valid_range(__first1
, __last1
);
3566 __glibcxx_requires_valid_range(__first2
, __last2
);
3569 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3570 __gnu_cxx::__ops::__iter_equal_to_iter());
3574 * @brief Checks whether a permutation of the second sequence is equal
3575 * to the first sequence.
3576 * @ingroup non_mutating_algorithms
3577 * @param __first1 Start of first range.
3578 * @param __last1 End of first range.
3579 * @param __first2 Start of second range.
3580 * @param __last2 End of first range.
3581 * @param __pred A binary predicate.
3582 * @return true if there exists a permutation of the elements in the range
3583 * [__first2, __last2), beginning with ForwardIterator2 begin,
3584 * such that equal(__first1, __last1, __begin, __pred) returns true;
3585 * otherwise, returns false.
3587 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3588 typename _BinaryPredicate
>
3589 _GLIBCXX20_CONSTEXPR
3591 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3592 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3593 _BinaryPredicate __pred
)
3595 __glibcxx_requires_valid_range(__first1
, __last1
);
3596 __glibcxx_requires_valid_range(__first2
, __last2
);
3598 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3599 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3603 #ifdef __glibcxx_clamp // C++ >= 17
3605 * @brief Returns the value clamped between lo and hi.
3606 * @ingroup sorting_algorithms
3607 * @param __val A value of arbitrary type.
3608 * @param __lo A lower limit of arbitrary type.
3609 * @param __hi An upper limit of arbitrary type.
3610 * @retval `__lo` if `__val < __lo`
3611 * @retval `__hi` if `__hi < __val`
3612 * @retval `__val` otherwise.
3613 * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
3615 template<typename _Tp
>
3616 constexpr const _Tp
&
3617 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3619 __glibcxx_assert(!(__hi
< __lo
));
3620 return std::min(std::max(__val
, __lo
), __hi
);
3624 * @brief Returns the value clamped between lo and hi.
3625 * @ingroup sorting_algorithms
3626 * @param __val A value of arbitrary type.
3627 * @param __lo A lower limit of arbitrary type.
3628 * @param __hi An upper limit of arbitrary type.
3629 * @param __comp A comparison functor.
3630 * @retval `__lo` if `__comp(__val, __lo)`
3631 * @retval `__hi` if `__comp(__hi, __val)`
3632 * @retval `__val` otherwise.
3633 * @pre `__comp(__hi, __lo)` is false.
3635 template<typename _Tp
, typename _Compare
>
3636 constexpr const _Tp
&
3637 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3639 __glibcxx_assert(!__comp(__hi
, __lo
));
3640 return std::min(std::max(__val
, __lo
, __comp
), __hi
, __comp
);
3642 #endif // __glibcxx_clamp
3645 * @brief Generate two uniformly distributed integers using a
3646 * single distribution invocation.
3647 * @param __b0 The upper bound for the first integer.
3648 * @param __b1 The upper bound for the second integer.
3649 * @param __g A UniformRandomBitGenerator.
3650 * @return A pair (i, j) with i and j uniformly distributed
3651 * over [0, __b0) and [0, __b1), respectively.
3653 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3655 * Using uniform_int_distribution with a range that is very
3656 * small relative to the range of the generator ends up wasting
3657 * potentially expensively generated randomness, since
3658 * uniform_int_distribution does not store leftover randomness
3659 * between invocations.
3661 * If we know we want two integers in ranges that are sufficiently
3662 * small, we can compose the ranges, use a single distribution
3663 * invocation, and significantly reduce the waste.
3665 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3666 pair
<_IntType
, _IntType
>
3667 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3668 _UniformRandomBitGenerator
&& __g
)
3671 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3672 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3676 * @brief Shuffle the elements of a sequence using a uniform random
3678 * @ingroup mutating_algorithms
3679 * @param __first A forward iterator.
3680 * @param __last A forward iterator.
3681 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3684 * Reorders the elements in the range @p [__first,__last) using @p __g to
3685 * provide random numbers.
3687 template<typename _RandomAccessIterator
,
3688 typename _UniformRandomNumberGenerator
>
3690 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3691 _UniformRandomNumberGenerator
&& __g
)
3693 // concept requirements
3694 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3695 _RandomAccessIterator
>)
3696 __glibcxx_requires_valid_range(__first
, __last
);
3698 if (__first
== __last
)
3701 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3704 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3705 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3706 typedef typename
__distr_type::param_type __p_type
;
3708 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3710 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3713 const __uc_type __urngrange
= __g
.max() - __g
.min();
3714 const __uc_type __urange
= __uc_type(__last
- __first
);
3716 if (__urngrange
/ __urange
>= __urange
)
3717 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3719 _RandomAccessIterator __i
= __first
+ 1;
3721 // Since we know the range isn't empty, an even number of elements
3722 // means an uneven number of elements /to swap/, in which case we
3723 // do the first one up front:
3725 if ((__urange
% 2) == 0)
3727 __distr_type __d
{0, 1};
3728 std::iter_swap(__i
++, __first
+ __d(__g
));
3731 // Now we know that __last - __i is even, so we do the rest in pairs,
3732 // using a single distribution invocation to produce swap positions
3733 // for two successive elements at a time:
3735 while (__i
!= __last
)
3737 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3739 const pair
<__uc_type
, __uc_type
> __pospos
=
3740 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3742 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3743 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3751 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3752 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3756 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3759 * @brief Apply a function to every element of a sequence.
3760 * @ingroup non_mutating_algorithms
3761 * @param __first An input iterator.
3762 * @param __last An input iterator.
3763 * @param __f A unary function object.
3766 * Applies the function object @p __f to each element in the range
3767 * @p [first,last). @p __f must not modify the order of the sequence.
3768 * If @p __f has a return value it is ignored.
3770 template<typename _InputIterator
, typename _Function
>
3771 _GLIBCXX20_CONSTEXPR
3773 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3775 // concept requirements
3776 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3777 __glibcxx_requires_valid_range(__first
, __last
);
3778 for (; __first
!= __last
; ++__first
)
3780 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3783 #if __cplusplus >= 201703L
3785 * @brief Apply a function to every element of a sequence.
3786 * @ingroup non_mutating_algorithms
3787 * @param __first An input iterator.
3788 * @param __n A value convertible to an integer.
3789 * @param __f A unary function object.
3790 * @return `__first+__n`
3792 * Applies the function object `__f` to each element in the range
3793 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3794 * If `__f` has a return value it is ignored.
3796 template<typename _InputIterator
, typename _Size
, typename _Function
>
3797 _GLIBCXX20_CONSTEXPR
3799 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3801 auto __n2
= std::__size_to_integer(__n
);
3802 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3803 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3807 auto __last
= __first
+ __n2
;
3808 std::for_each(__first
, __last
, std::move(__f
));
3824 * @brief Find the first occurrence of a value in a sequence.
3825 * @ingroup non_mutating_algorithms
3826 * @param __first An input iterator.
3827 * @param __last An input iterator.
3828 * @param __val The value to find.
3829 * @return The first iterator @c i in the range @p [__first,__last)
3830 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3832 template<typename _InputIterator
, typename _Tp
>
3833 _GLIBCXX20_CONSTEXPR
3834 inline _InputIterator
3835 find(_InputIterator __first
, _InputIterator __last
,
3838 // concept requirements
3839 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3840 __glibcxx_function_requires(_EqualOpConcept
<
3841 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3842 __glibcxx_requires_valid_range(__first
, __last
);
3843 return std::__find_if(__first
, __last
,
3844 __gnu_cxx::__ops::__iter_equals_val(__val
));
3848 * @brief Find the first element in a sequence for which a
3849 * predicate is true.
3850 * @ingroup non_mutating_algorithms
3851 * @param __first An input iterator.
3852 * @param __last An input iterator.
3853 * @param __pred A predicate.
3854 * @return The first iterator @c i in the range @p [__first,__last)
3855 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3857 template<typename _InputIterator
, typename _Predicate
>
3858 _GLIBCXX20_CONSTEXPR
3859 inline _InputIterator
3860 find_if(_InputIterator __first
, _InputIterator __last
,
3863 // concept requirements
3864 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3865 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3866 typename iterator_traits
<_InputIterator
>::value_type
>)
3867 __glibcxx_requires_valid_range(__first
, __last
);
3869 return std::__find_if(__first
, __last
,
3870 __gnu_cxx::__ops::__pred_iter(__pred
));
3874 * @brief Find element from a set in a sequence.
3875 * @ingroup non_mutating_algorithms
3876 * @param __first1 Start of range to search.
3877 * @param __last1 End of range to search.
3878 * @param __first2 Start of match candidates.
3879 * @param __last2 End of match candidates.
3880 * @return The first iterator @c i in the range
3881 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3882 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3884 * Searches the range @p [__first1,__last1) for an element that is
3885 * equal to some element in the range [__first2,__last2). If
3886 * found, returns an iterator in the range [__first1,__last1),
3887 * otherwise returns @p __last1.
3889 template<typename _InputIterator
, typename _ForwardIterator
>
3890 _GLIBCXX20_CONSTEXPR
3892 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3893 _ForwardIterator __first2
, _ForwardIterator __last2
)
3895 // concept requirements
3896 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3897 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3898 __glibcxx_function_requires(_EqualOpConcept
<
3899 typename iterator_traits
<_InputIterator
>::value_type
,
3900 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3901 __glibcxx_requires_valid_range(__first1
, __last1
);
3902 __glibcxx_requires_valid_range(__first2
, __last2
);
3904 for (; __first1
!= __last1
; ++__first1
)
3905 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3906 if (*__first1
== *__iter
)
3912 * @brief Find element from a set in a sequence using a predicate.
3913 * @ingroup non_mutating_algorithms
3914 * @param __first1 Start of range to search.
3915 * @param __last1 End of range to search.
3916 * @param __first2 Start of match candidates.
3917 * @param __last2 End of match candidates.
3918 * @param __comp Predicate to use.
3919 * @return The first iterator @c i in the range
3920 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3921 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3922 * such iterator exists.
3925 * Searches the range @p [__first1,__last1) for an element that is
3926 * equal to some element in the range [__first2,__last2). If
3927 * found, returns an iterator in the range [__first1,__last1),
3928 * otherwise returns @p __last1.
3930 template<typename _InputIterator
, typename _ForwardIterator
,
3931 typename _BinaryPredicate
>
3932 _GLIBCXX20_CONSTEXPR
3934 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3935 _ForwardIterator __first2
, _ForwardIterator __last2
,
3936 _BinaryPredicate __comp
)
3938 // concept requirements
3939 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3940 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3941 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3942 typename iterator_traits
<_InputIterator
>::value_type
,
3943 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3944 __glibcxx_requires_valid_range(__first1
, __last1
);
3945 __glibcxx_requires_valid_range(__first2
, __last2
);
3947 for (; __first1
!= __last1
; ++__first1
)
3948 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3949 if (__comp(*__first1
, *__iter
))
3955 * @brief Find two adjacent values in a sequence that are equal.
3956 * @ingroup non_mutating_algorithms
3957 * @param __first A forward iterator.
3958 * @param __last A forward iterator.
3959 * @return The first iterator @c i such that @c i and @c i+1 are both
3960 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3961 * or @p __last if no such iterator exists.
3963 template<typename _ForwardIterator
>
3964 _GLIBCXX20_CONSTEXPR
3965 inline _ForwardIterator
3966 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3968 // concept requirements
3969 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3970 __glibcxx_function_requires(_EqualityComparableConcept
<
3971 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3972 __glibcxx_requires_valid_range(__first
, __last
);
3974 return std::__adjacent_find(__first
, __last
,
3975 __gnu_cxx::__ops::__iter_equal_to_iter());
3979 * @brief Find two adjacent values in a sequence using a predicate.
3980 * @ingroup non_mutating_algorithms
3981 * @param __first A forward iterator.
3982 * @param __last A forward iterator.
3983 * @param __binary_pred A binary predicate.
3984 * @return The first iterator @c i such that @c i and @c i+1 are both
3985 * valid iterators in @p [__first,__last) and such that
3986 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3989 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3990 _GLIBCXX20_CONSTEXPR
3991 inline _ForwardIterator
3992 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3993 _BinaryPredicate __binary_pred
)
3995 // concept requirements
3996 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3997 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3998 typename iterator_traits
<_ForwardIterator
>::value_type
,
3999 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4000 __glibcxx_requires_valid_range(__first
, __last
);
4002 return std::__adjacent_find(__first
, __last
,
4003 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4007 * @brief Count the number of copies of a value in a sequence.
4008 * @ingroup non_mutating_algorithms
4009 * @param __first An input iterator.
4010 * @param __last An input iterator.
4011 * @param __value The value to be counted.
4012 * @return The number of iterators @c i in the range @p [__first,__last)
4013 * for which @c *i == @p __value
4015 template<typename _InputIterator
, typename _Tp
>
4016 _GLIBCXX20_CONSTEXPR
4017 inline typename iterator_traits
<_InputIterator
>::difference_type
4018 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4020 // concept requirements
4021 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4022 __glibcxx_function_requires(_EqualOpConcept
<
4023 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4024 __glibcxx_requires_valid_range(__first
, __last
);
4026 return std::__count_if(__first
, __last
,
4027 __gnu_cxx::__ops::__iter_equals_val(__value
));
4031 * @brief Count the elements of a sequence for which a predicate is true.
4032 * @ingroup non_mutating_algorithms
4033 * @param __first An input iterator.
4034 * @param __last An input iterator.
4035 * @param __pred A predicate.
4036 * @return The number of iterators @c i in the range @p [__first,__last)
4037 * for which @p __pred(*i) is true.
4039 template<typename _InputIterator
, typename _Predicate
>
4040 _GLIBCXX20_CONSTEXPR
4041 inline typename iterator_traits
<_InputIterator
>::difference_type
4042 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4044 // concept requirements
4045 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4046 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4047 typename iterator_traits
<_InputIterator
>::value_type
>)
4048 __glibcxx_requires_valid_range(__first
, __last
);
4050 return std::__count_if(__first
, __last
,
4051 __gnu_cxx::__ops::__pred_iter(__pred
));
4055 * @brief Search a sequence for a matching sub-sequence.
4056 * @ingroup non_mutating_algorithms
4057 * @param __first1 A forward iterator.
4058 * @param __last1 A forward iterator.
4059 * @param __first2 A forward iterator.
4060 * @param __last2 A forward iterator.
4061 * @return The first iterator @c i in the range @p
4062 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4063 * *(__first2+N) for each @c N in the range @p
4064 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4066 * Searches the range @p [__first1,__last1) for a sub-sequence that
4067 * compares equal value-by-value with the sequence given by @p
4068 * [__first2,__last2) and returns an iterator to the first element
4069 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4072 * Because the sub-sequence must lie completely within the range @p
4073 * [__first1,__last1) it must start at a position less than @p
4074 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4075 * length of the sub-sequence.
4077 * This means that the returned iterator @c i will be in the range
4078 * @p [__first1,__last1-(__last2-__first2))
4080 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4081 _GLIBCXX20_CONSTEXPR
4082 inline _ForwardIterator1
4083 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4084 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4086 // concept requirements
4087 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4088 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4089 __glibcxx_function_requires(_EqualOpConcept
<
4090 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4091 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4092 __glibcxx_requires_valid_range(__first1
, __last1
);
4093 __glibcxx_requires_valid_range(__first2
, __last2
);
4095 return std::__search(__first1
, __last1
, __first2
, __last2
,
4096 __gnu_cxx::__ops::__iter_equal_to_iter());
4100 * @brief Search a sequence for a number of consecutive values.
4101 * @ingroup non_mutating_algorithms
4102 * @param __first A forward iterator.
4103 * @param __last A forward iterator.
4104 * @param __count The number of consecutive values.
4105 * @param __val The value to find.
4106 * @return The first iterator @c i in the range @p
4107 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4108 * each @c N in the range @p [0,__count), or @p __last if no such
4111 * Searches the range @p [__first,__last) for @p count consecutive elements
4112 * equal to @p __val.
4114 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4115 _GLIBCXX20_CONSTEXPR
4116 inline _ForwardIterator
4117 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4118 _Integer __count
, const _Tp
& __val
)
4120 // concept requirements
4121 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4122 __glibcxx_function_requires(_EqualOpConcept
<
4123 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4124 __glibcxx_requires_valid_range(__first
, __last
);
4126 return std::__search_n(__first
, __last
, __count
,
4127 __gnu_cxx::__ops::__iter_equals_val(__val
));
4132 * @brief Search a sequence for a number of consecutive values using a
4134 * @ingroup non_mutating_algorithms
4135 * @param __first A forward iterator.
4136 * @param __last A forward iterator.
4137 * @param __count The number of consecutive values.
4138 * @param __val The value to find.
4139 * @param __binary_pred A binary predicate.
4140 * @return The first iterator @c i in the range @p
4141 * [__first,__last-__count) such that @p
4142 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4143 * @p [0,__count), or @p __last if no such iterator exists.
4145 * Searches the range @p [__first,__last) for @p __count
4146 * consecutive elements for which the predicate returns true.
4148 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4149 typename _BinaryPredicate
>
4150 _GLIBCXX20_CONSTEXPR
4151 inline _ForwardIterator
4152 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4153 _Integer __count
, const _Tp
& __val
,
4154 _BinaryPredicate __binary_pred
)
4156 // concept requirements
4157 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4158 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4159 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4160 __glibcxx_requires_valid_range(__first
, __last
);
4162 return std::__search_n(__first
, __last
, __count
,
4163 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4166 #if __cplusplus >= 201703L
4167 /** @brief Search a sequence using a Searcher object.
4169 * @param __first A forward iterator.
4170 * @param __last A forward iterator.
4171 * @param __searcher A callable object.
4172 * @return @p __searcher(__first,__last).first
4174 template<typename _ForwardIterator
, typename _Searcher
>
4175 _GLIBCXX20_CONSTEXPR
4176 inline _ForwardIterator
4177 search(_ForwardIterator __first
, _ForwardIterator __last
,
4178 const _Searcher
& __searcher
)
4179 { return __searcher(__first
, __last
).first
; }
4183 * @brief Perform an operation on a sequence.
4184 * @ingroup mutating_algorithms
4185 * @param __first An input iterator.
4186 * @param __last An input iterator.
4187 * @param __result An output iterator.
4188 * @param __unary_op A unary operator.
4189 * @return An output iterator equal to @p __result+(__last-__first).
4191 * Applies the operator to each element in the input range and assigns
4192 * the results to successive elements of the output sequence.
4193 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4194 * range @p [0,__last-__first).
4196 * @p unary_op must not alter its argument.
4198 template<typename _InputIterator
, typename _OutputIterator
,
4199 typename _UnaryOperation
>
4200 _GLIBCXX20_CONSTEXPR
4202 transform(_InputIterator __first
, _InputIterator __last
,
4203 _OutputIterator __result
, _UnaryOperation __unary_op
)
4205 // concept requirements
4206 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4207 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4208 // "the type returned by a _UnaryOperation"
4209 __typeof__(__unary_op(*__first
))>)
4210 __glibcxx_requires_valid_range(__first
, __last
);
4212 for (; __first
!= __last
; ++__first
, (void)++__result
)
4213 *__result
= __unary_op(*__first
);
4218 * @brief Perform an operation on corresponding elements of two sequences.
4219 * @ingroup mutating_algorithms
4220 * @param __first1 An input iterator.
4221 * @param __last1 An input iterator.
4222 * @param __first2 An input iterator.
4223 * @param __result An output iterator.
4224 * @param __binary_op A binary operator.
4225 * @return An output iterator equal to @p result+(last-first).
4227 * Applies the operator to the corresponding elements in the two
4228 * input ranges and assigns the results to successive elements of the
4231 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4232 * @c N in the range @p [0,__last1-__first1).
4234 * @p binary_op must not alter either of its arguments.
4236 template<typename _InputIterator1
, typename _InputIterator2
,
4237 typename _OutputIterator
, typename _BinaryOperation
>
4238 _GLIBCXX20_CONSTEXPR
4240 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4241 _InputIterator2 __first2
, _OutputIterator __result
,
4242 _BinaryOperation __binary_op
)
4244 // concept requirements
4245 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4246 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4247 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4248 // "the type returned by a _BinaryOperation"
4249 __typeof__(__binary_op(*__first1
,*__first2
))>)
4250 __glibcxx_requires_valid_range(__first1
, __last1
);
4252 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4253 *__result
= __binary_op(*__first1
, *__first2
);
4258 * @brief Replace each occurrence of one value in a sequence with another
4260 * @ingroup mutating_algorithms
4261 * @param __first A forward iterator.
4262 * @param __last A forward iterator.
4263 * @param __old_value The value to be replaced.
4264 * @param __new_value The replacement value.
4265 * @return replace() returns no value.
4267 * For each iterator `i` in the range `[__first,__last)` if
4268 * `*i == __old_value` then the assignment `*i = __new_value` is performed.
4270 template<typename _ForwardIterator
, typename _Tp
>
4271 _GLIBCXX20_CONSTEXPR
4273 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4274 const _Tp
& __old_value
, const _Tp
& __new_value
)
4276 // concept requirements
4277 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4279 __glibcxx_function_requires(_EqualOpConcept
<
4280 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4281 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4282 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4283 __glibcxx_requires_valid_range(__first
, __last
);
4285 for (; __first
!= __last
; ++__first
)
4286 if (*__first
== __old_value
)
4287 *__first
= __new_value
;
4291 * @brief Replace each value in a sequence for which a predicate returns
4292 * true with another value.
4293 * @ingroup mutating_algorithms
4294 * @param __first A forward iterator.
4295 * @param __last A forward iterator.
4296 * @param __pred A predicate.
4297 * @param __new_value The replacement value.
4298 * @return replace_if() returns no value.
4300 * For each iterator `i` in the range `[__first,__last)` if `__pred(*i)`
4301 * is true then the assignment `*i = __new_value` is performed.
4303 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4304 _GLIBCXX20_CONSTEXPR
4306 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4307 _Predicate __pred
, const _Tp
& __new_value
)
4309 // concept requirements
4310 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4312 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4313 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4314 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4315 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4316 __glibcxx_requires_valid_range(__first
, __last
);
4318 for (; __first
!= __last
; ++__first
)
4319 if (__pred(*__first
))
4320 *__first
= __new_value
;
4324 * @brief Assign the result of a function object to each value in a
4326 * @ingroup mutating_algorithms
4327 * @param __first A forward iterator.
4328 * @param __last A forward iterator.
4329 * @param __gen A function object callable with no arguments.
4330 * @return generate() returns no value.
4332 * Performs the assignment `*i = __gen()` for each `i` in the range
4333 * `[__first, __last)`.
4335 template<typename _ForwardIterator
, typename _Generator
>
4336 _GLIBCXX20_CONSTEXPR
4338 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4341 // concept requirements
4342 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4343 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4344 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4345 __glibcxx_requires_valid_range(__first
, __last
);
4347 for (; __first
!= __last
; ++__first
)
4352 * @brief Assign the result of a function object to each value in a
4354 * @ingroup mutating_algorithms
4355 * @param __first A forward iterator.
4356 * @param __n The length of the sequence.
4357 * @param __gen A function object callable with no arguments.
4358 * @return The end of the sequence, i.e., `__first + __n`
4360 * Performs the assignment `*i = __gen()` for each `i` in the range
4361 * `[__first, __first + __n)`.
4363 * If `__n` is negative, the function does nothing and returns `__first`.
4365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4366 // DR 865. More algorithms that throw away information
4367 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4368 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4369 _GLIBCXX20_CONSTEXPR
4371 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4373 // concept requirements
4374 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4375 // "the type returned by a _Generator"
4376 __typeof__(__gen())>)
4378 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4379 for (_IntSize __niter
= std::__size_to_integer(__n
);
4380 __niter
> 0; --__niter
, (void) ++__first
)
4386 * @brief Copy a sequence, removing consecutive duplicate values.
4387 * @ingroup mutating_algorithms
4388 * @param __first An input iterator.
4389 * @param __last An input iterator.
4390 * @param __result An output iterator.
4391 * @return An iterator designating the end of the resulting sequence.
4393 * Copies each element in the range `[__first, __last)` to the range
4394 * beginning at `__result`, except that only the first element is copied
4395 * from groups of consecutive elements that compare equal.
4396 * `unique_copy()` is stable, so the relative order of elements that are
4397 * copied is unchanged.
4399 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4400 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4401 // DR 538. 241 again: Does unique_copy() require CopyConstructible and
4403 template<typename _InputIterator
, typename _OutputIterator
>
4404 _GLIBCXX20_CONSTEXPR
4405 inline _OutputIterator
4406 unique_copy(_InputIterator __first
, _InputIterator __last
,
4407 _OutputIterator __result
)
4409 // concept requirements
4410 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4411 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4412 typename iterator_traits
<_InputIterator
>::value_type
>)
4413 __glibcxx_function_requires(_EqualityComparableConcept
<
4414 typename iterator_traits
<_InputIterator
>::value_type
>)
4415 __glibcxx_requires_valid_range(__first
, __last
);
4417 if (__first
== __last
)
4419 return std::__unique_copy(__first
, __last
, __result
,
4420 __gnu_cxx::__ops::__iter_equal_to_iter(),
4421 std::__iterator_category(__first
),
4422 std::__iterator_category(__result
));
4426 * @brief Copy a sequence, removing consecutive values using a predicate.
4427 * @ingroup mutating_algorithms
4428 * @param __first An input iterator.
4429 * @param __last An input iterator.
4430 * @param __result An output iterator.
4431 * @param __binary_pred A binary predicate.
4432 * @return An iterator designating the end of the resulting sequence.
4434 * Copies each element in the range `[__first, __last)` to the range
4435 * beginning at `__result`, except that only the first element is copied
4436 * from groups of consecutive elements for which `__binary_pred` returns
4438 * `unique_copy()` is stable, so the relative order of elements that are
4439 * copied is unchanged.
4441 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4442 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4443 template<typename _InputIterator
, typename _OutputIterator
,
4444 typename _BinaryPredicate
>
4445 _GLIBCXX20_CONSTEXPR
4446 inline _OutputIterator
4447 unique_copy(_InputIterator __first
, _InputIterator __last
,
4448 _OutputIterator __result
,
4449 _BinaryPredicate __binary_pred
)
4451 // concept requirements -- predicates checked later
4452 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4453 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4454 typename iterator_traits
<_InputIterator
>::value_type
>)
4455 __glibcxx_requires_valid_range(__first
, __last
);
4457 if (__first
== __last
)
4459 return std::__unique_copy(__first
, __last
, __result
,
4460 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4461 std::__iterator_category(__first
),
4462 std::__iterator_category(__result
));
4465 #if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4468 * @brief Randomly shuffle the elements of a sequence.
4469 * @ingroup mutating_algorithms
4470 * @param __first A forward iterator.
4471 * @param __last A forward iterator.
4474 * Reorder the elements in the range `[__first, __last)` using a random
4475 * distribution, so that every possible ordering of the sequence is
4479 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4480 * Use `std::shuffle` instead, which was introduced in C++11.
4482 template<typename _RandomAccessIterator
>
4483 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4485 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4487 // concept requirements
4488 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4489 _RandomAccessIterator
>)
4490 __glibcxx_requires_valid_range(__first
, __last
);
4492 if (__first
!= __last
)
4493 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4495 // XXX rand() % N is not uniformly distributed
4496 _RandomAccessIterator __j
= __first
4497 + std::rand() % ((__i
- __first
) + 1);
4499 std::iter_swap(__i
, __j
);
4504 * @brief Shuffle the elements of a sequence using a random number
4506 * @ingroup mutating_algorithms
4507 * @param __first A forward iterator.
4508 * @param __last A forward iterator.
4509 * @param __rand The RNG functor or function.
4512 * Reorders the elements in the range `[__first, __last)` using `__rand`
4513 * to provide a random distribution. Calling `__rand(N)` for a positive
4514 * integer `N` should return a randomly chosen integer from the
4518 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4519 * Use `std::shuffle` instead, which was introduced in C++11.
4521 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4522 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4524 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4525 #if __cplusplus >= 201103L
4526 _RandomNumberGenerator
&& __rand
)
4528 _RandomNumberGenerator
& __rand
)
4531 // concept requirements
4532 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4533 _RandomAccessIterator
>)
4534 __glibcxx_requires_valid_range(__first
, __last
);
4536 if (__first
== __last
)
4538 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4540 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4542 std::iter_swap(__i
, __j
);
4546 #endif // <= C++11 || USE_DEPRECATED
4549 * @brief Move elements for which a predicate is true to the beginning
4551 * @ingroup mutating_algorithms
4552 * @param __first A forward iterator.
4553 * @param __last A forward iterator.
4554 * @param __pred A predicate functor.
4555 * @return An iterator `middle` such that `__pred(i)` is true for each
4556 * iterator `i` in the range `[__first, middle)` and false for each `i`
4557 * in the range `[middle, __last)`.
4559 * `__pred` must not modify its operand. `partition()` does not preserve
4560 * the relative ordering of elements in each group, use
4561 * `stable_partition()` if this is needed.
4563 template<typename _ForwardIterator
, typename _Predicate
>
4564 _GLIBCXX20_CONSTEXPR
4565 inline _ForwardIterator
4566 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4569 // concept requirements
4570 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4572 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4573 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4574 __glibcxx_requires_valid_range(__first
, __last
);
4576 return std::__partition(__first
, __last
, __pred
,
4577 std::__iterator_category(__first
));
4582 * @brief Sort the smallest elements of a sequence.
4583 * @ingroup sorting_algorithms
4584 * @param __first An iterator.
4585 * @param __middle Another iterator.
4586 * @param __last Another iterator.
4589 * Sorts the smallest `(__middle - __first)` elements in the range
4590 * `[first, last)` and moves them to the range `[__first, __middle)`. The
4591 * order of the remaining elements in the range `[__middle, __last)` is
4593 * After the sort if `i` and `j` are iterators in the range
4594 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4595 * in the range `[__middle, __last)` then `*j < *i` and `*k < *i` are
4598 template<typename _RandomAccessIterator
>
4599 _GLIBCXX20_CONSTEXPR
4601 partial_sort(_RandomAccessIterator __first
,
4602 _RandomAccessIterator __middle
,
4603 _RandomAccessIterator __last
)
4605 // concept requirements
4606 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4607 _RandomAccessIterator
>)
4608 __glibcxx_function_requires(_LessThanComparableConcept
<
4609 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4610 __glibcxx_requires_valid_range(__first
, __middle
);
4611 __glibcxx_requires_valid_range(__middle
, __last
);
4612 __glibcxx_requires_irreflexive(__first
, __last
);
4614 std::__partial_sort(__first
, __middle
, __last
,
4615 __gnu_cxx::__ops::__iter_less_iter());
4619 * @brief Sort the smallest elements of a sequence using a predicate
4621 * @ingroup sorting_algorithms
4622 * @param __first An iterator.
4623 * @param __middle Another iterator.
4624 * @param __last Another iterator.
4625 * @param __comp A comparison functor.
4628 * Sorts the smallest `(__middle - __first)` elements in the range
4629 * `[__first, __last)` and moves them to the range `[__first, __middle)`.
4630 * The order of the remaining elements in the range `[__middle, __last)` is
4632 * After the sort if `i` and `j` are iterators in the range
4633 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4634 * in the range `[__middle, __last)` then `*__comp(j, *i)` and
4635 * `__comp(*k, *i)` are both false.
4637 template<typename _RandomAccessIterator
, typename _Compare
>
4638 _GLIBCXX20_CONSTEXPR
4640 partial_sort(_RandomAccessIterator __first
,
4641 _RandomAccessIterator __middle
,
4642 _RandomAccessIterator __last
,
4645 // concept requirements
4646 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4647 _RandomAccessIterator
>)
4648 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4649 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4650 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4651 __glibcxx_requires_valid_range(__first
, __middle
);
4652 __glibcxx_requires_valid_range(__middle
, __last
);
4653 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4655 std::__partial_sort(__first
, __middle
, __last
,
4656 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4660 * @brief Sort a sequence just enough to find a particular position.
4661 * @ingroup sorting_algorithms
4662 * @param __first An iterator.
4663 * @param __nth Another iterator.
4664 * @param __last Another iterator.
4667 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4668 * is the same element that would have been in that position had the
4669 * whole sequence been sorted. The elements either side of `*__nth` are
4670 * not completely sorted, but for any iterator `i` in the range
4671 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)` it
4672 * holds that `*j < *i` is false.
4674 template<typename _RandomAccessIterator
>
4675 _GLIBCXX20_CONSTEXPR
4677 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4678 _RandomAccessIterator __last
)
4680 // concept requirements
4681 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4682 _RandomAccessIterator
>)
4683 __glibcxx_function_requires(_LessThanComparableConcept
<
4684 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4685 __glibcxx_requires_valid_range(__first
, __nth
);
4686 __glibcxx_requires_valid_range(__nth
, __last
);
4687 __glibcxx_requires_irreflexive(__first
, __last
);
4689 if (__first
== __last
|| __nth
== __last
)
4692 std::__introselect(__first
, __nth
, __last
,
4693 std::__lg(__last
- __first
) * 2,
4694 __gnu_cxx::__ops::__iter_less_iter());
4698 * @brief Sort a sequence just enough to find a particular position
4699 * using a predicate for comparison.
4700 * @ingroup sorting_algorithms
4701 * @param __first An iterator.
4702 * @param __nth Another iterator.
4703 * @param __last Another iterator.
4704 * @param __comp A comparison functor.
4707 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4708 * is the same element that would have been in that position had the
4709 * whole sequence been sorted. The elements either side of `*__nth` are
4710 * not completely sorted, but for any iterator `i` in the range
4711 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)`
4712 * it holds that `__comp(*j, *i)` is false.
4714 template<typename _RandomAccessIterator
, typename _Compare
>
4715 _GLIBCXX20_CONSTEXPR
4717 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4718 _RandomAccessIterator __last
, _Compare __comp
)
4720 // concept requirements
4721 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4722 _RandomAccessIterator
>)
4723 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4724 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4725 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4726 __glibcxx_requires_valid_range(__first
, __nth
);
4727 __glibcxx_requires_valid_range(__nth
, __last
);
4728 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4730 if (__first
== __last
|| __nth
== __last
)
4733 std::__introselect(__first
, __nth
, __last
,
4734 std::__lg(__last
- __first
) * 2,
4735 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4739 * @brief Sort the elements of a sequence.
4740 * @ingroup sorting_algorithms
4741 * @param __first An iterator.
4742 * @param __last Another iterator.
4745 * Sorts the elements in the range `[__first, __last)` in ascending order,
4746 * such that for each iterator `i` in the range `[__first, __last - 1)`,
4747 * `*(i+1) < *i` is false.
4749 * The relative ordering of equivalent elements is not preserved, use
4750 * `stable_sort()` if this is needed.
4752 template<typename _RandomAccessIterator
>
4753 _GLIBCXX20_CONSTEXPR
4755 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4757 // concept requirements
4758 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4759 _RandomAccessIterator
>)
4760 __glibcxx_function_requires(_LessThanComparableConcept
<
4761 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4762 __glibcxx_requires_valid_range(__first
, __last
);
4763 __glibcxx_requires_irreflexive(__first
, __last
);
4765 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4769 * @brief Sort the elements of a sequence using a predicate for comparison.
4770 * @ingroup sorting_algorithms
4771 * @param __first An iterator.
4772 * @param __last Another iterator.
4773 * @param __comp A comparison functor.
4776 * Sorts the elements in the range `[__first, __last)` in ascending order,
4777 * such that `__comp(*(i+1), *i)` is false for every iterator `i` in the
4778 * range `[__first, __last - 1)`.
4780 * The relative ordering of equivalent elements is not preserved, use
4781 * `stable_sort()` if this is needed.
4783 template<typename _RandomAccessIterator
, typename _Compare
>
4784 _GLIBCXX20_CONSTEXPR
4786 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4789 // concept requirements
4790 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4791 _RandomAccessIterator
>)
4792 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4793 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4794 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4795 __glibcxx_requires_valid_range(__first
, __last
);
4796 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4798 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4801 template<typename _InputIterator1
, typename _InputIterator2
,
4802 typename _OutputIterator
, typename _Compare
>
4803 _GLIBCXX20_CONSTEXPR
4805 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4806 _InputIterator2 __first2
, _InputIterator2 __last2
,
4807 _OutputIterator __result
, _Compare __comp
)
4809 while (__first1
!= __last1
&& __first2
!= __last2
)
4811 if (__comp(__first2
, __first1
))
4813 *__result
= *__first2
;
4818 *__result
= *__first1
;
4823 return std::copy(__first2
, __last2
,
4824 std::copy(__first1
, __last1
, __result
));
4828 * @brief Merges two sorted ranges.
4829 * @ingroup sorting_algorithms
4830 * @param __first1 An iterator.
4831 * @param __first2 Another iterator.
4832 * @param __last1 Another iterator.
4833 * @param __last2 Another iterator.
4834 * @param __result An iterator pointing to the end of the merged range.
4835 * @return An output iterator equal to @p __result + (__last1 - __first1)
4836 * + (__last2 - __first2).
4838 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4839 * the sorted range @p [__result, __result + (__last1-__first1) +
4840 * (__last2-__first2)). Both input ranges must be sorted, and the
4841 * output range must not overlap with either of the input ranges.
4842 * The sort is @e stable, that is, for equivalent elements in the
4843 * two ranges, elements from the first range will always come
4844 * before elements from the second.
4846 template<typename _InputIterator1
, typename _InputIterator2
,
4847 typename _OutputIterator
>
4848 _GLIBCXX20_CONSTEXPR
4849 inline _OutputIterator
4850 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4851 _InputIterator2 __first2
, _InputIterator2 __last2
,
4852 _OutputIterator __result
)
4854 // concept requirements
4855 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4856 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4857 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4858 typename iterator_traits
<_InputIterator1
>::value_type
>)
4859 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4860 typename iterator_traits
<_InputIterator2
>::value_type
>)
4861 __glibcxx_function_requires(_LessThanOpConcept
<
4862 typename iterator_traits
<_InputIterator2
>::value_type
,
4863 typename iterator_traits
<_InputIterator1
>::value_type
>)
4864 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4865 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4866 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4867 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4869 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4870 __first2
, __last2
, __result
,
4871 __gnu_cxx::__ops::__iter_less_iter());
4875 * @brief Merges two sorted ranges.
4876 * @ingroup sorting_algorithms
4877 * @param __first1 An iterator.
4878 * @param __first2 Another iterator.
4879 * @param __last1 Another iterator.
4880 * @param __last2 Another iterator.
4881 * @param __result An iterator pointing to the end of the merged range.
4882 * @param __comp A functor to use for comparisons.
4883 * @return An output iterator equal to @p __result + (__last1 - __first1)
4884 * + (__last2 - __first2).
4886 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4887 * the sorted range @p [__result, __result + (__last1-__first1) +
4888 * (__last2-__first2)). Both input ranges must be sorted, and the
4889 * output range must not overlap with either of the input ranges.
4890 * The sort is @e stable, that is, for equivalent elements in the
4891 * two ranges, elements from the first range will always come
4892 * before elements from the second.
4894 * The comparison function should have the same effects on ordering as
4895 * the function used for the initial sort.
4897 template<typename _InputIterator1
, typename _InputIterator2
,
4898 typename _OutputIterator
, typename _Compare
>
4899 _GLIBCXX20_CONSTEXPR
4900 inline _OutputIterator
4901 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4902 _InputIterator2 __first2
, _InputIterator2 __last2
,
4903 _OutputIterator __result
, _Compare __comp
)
4905 // concept requirements
4906 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4907 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4908 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4909 typename iterator_traits
<_InputIterator1
>::value_type
>)
4910 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4911 typename iterator_traits
<_InputIterator2
>::value_type
>)
4912 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4913 typename iterator_traits
<_InputIterator2
>::value_type
,
4914 typename iterator_traits
<_InputIterator1
>::value_type
>)
4915 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4916 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4917 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
4918 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
4920 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4921 __first2
, __last2
, __result
,
4922 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4925 template<typename _RandomAccessIterator
, typename _Compare
>
4927 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4930 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4932 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4935 if (__first
== __last
)
4939 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
4940 // __stable_sort_adaptive sorts the range in two halves,
4941 // so the buffer only needs to fit half the range at once.
4942 _TmpBuf
__buf(__first
, (__last
- __first
+ 1) / 2);
4944 if (__builtin_expect(__buf
.requested_size() == __buf
.size(), true))
4945 std::__stable_sort_adaptive(__first
,
4946 __first
+ _DistanceType(__buf
.size()),
4947 __last
, __buf
.begin(), __comp
);
4948 else if (__builtin_expect(__buf
.begin() == 0, false))
4949 std::__inplace_stable_sort(__first
, __last
, __comp
);
4951 std::__stable_sort_adaptive_resize(__first
, __last
, __buf
.begin(),
4952 _DistanceType(__buf
.size()), __comp
);
4954 std::__inplace_stable_sort(__first
, __last
, __comp
);
4959 * @brief Sort the elements of a sequence, preserving the relative order
4960 * of equivalent elements.
4961 * @ingroup sorting_algorithms
4962 * @param __first An iterator.
4963 * @param __last Another iterator.
4966 * Sorts the elements in the range @p [__first,__last) in ascending order,
4967 * such that for each iterator @p i in the range @p [__first,__last-1),
4968 * @p *(i+1)<*i is false.
4970 * The relative ordering of equivalent elements is preserved, so any two
4971 * elements @p x and @p y in the range @p [__first,__last) such that
4972 * @p x<y is false and @p y<x is false will have the same relative
4973 * ordering after calling @p stable_sort().
4975 template<typename _RandomAccessIterator
>
4977 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4979 // concept requirements
4980 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4981 _RandomAccessIterator
>)
4982 __glibcxx_function_requires(_LessThanComparableConcept
<
4983 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4984 __glibcxx_requires_valid_range(__first
, __last
);
4985 __glibcxx_requires_irreflexive(__first
, __last
);
4987 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4988 __gnu_cxx::__ops::__iter_less_iter());
4992 * @brief Sort the elements of a sequence using a predicate for comparison,
4993 * preserving the relative order of equivalent elements.
4994 * @ingroup sorting_algorithms
4995 * @param __first An iterator.
4996 * @param __last Another iterator.
4997 * @param __comp A comparison functor.
5000 * Sorts the elements in the range @p [__first,__last) in ascending order,
5001 * such that for each iterator @p i in the range @p [__first,__last-1),
5002 * @p __comp(*(i+1),*i) is false.
5004 * The relative ordering of equivalent elements is preserved, so any two
5005 * elements @p x and @p y in the range @p [__first,__last) such that
5006 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5007 * relative ordering after calling @p stable_sort().
5009 template<typename _RandomAccessIterator
, typename _Compare
>
5011 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5014 // concept requirements
5015 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5016 _RandomAccessIterator
>)
5017 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5018 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5019 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5020 __glibcxx_requires_valid_range(__first
, __last
);
5021 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5023 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5024 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5027 template<typename _InputIterator1
, typename _InputIterator2
,
5028 typename _OutputIterator
,
5030 _GLIBCXX20_CONSTEXPR
5032 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5033 _InputIterator2 __first2
, _InputIterator2 __last2
,
5034 _OutputIterator __result
, _Compare __comp
)
5036 while (__first1
!= __last1
&& __first2
!= __last2
)
5038 if (__comp(__first1
, __first2
))
5040 *__result
= *__first1
;
5043 else if (__comp(__first2
, __first1
))
5045 *__result
= *__first2
;
5050 *__result
= *__first1
;
5056 return std::copy(__first2
, __last2
,
5057 std::copy(__first1
, __last1
, __result
));
5061 * @brief Return the union of two sorted ranges.
5062 * @ingroup set_algorithms
5063 * @param __first1 Start of first range.
5064 * @param __last1 End of first range.
5065 * @param __first2 Start of second range.
5066 * @param __last2 End of second range.
5067 * @param __result Start of output range.
5068 * @return End of the output range.
5069 * @ingroup set_algorithms
5071 * This operation iterates over both ranges, copying elements present in
5072 * each range in order to the output range. Iterators increment for each
5073 * range. When the current element of one range is less than the other,
5074 * that element is copied and the iterator advanced. If an element is
5075 * contained in both ranges, the element from the first range is copied and
5076 * both ranges advance. The output range may not overlap either input
5079 template<typename _InputIterator1
, typename _InputIterator2
,
5080 typename _OutputIterator
>
5081 _GLIBCXX20_CONSTEXPR
5082 inline _OutputIterator
5083 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5084 _InputIterator2 __first2
, _InputIterator2 __last2
,
5085 _OutputIterator __result
)
5087 // concept requirements
5088 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5089 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5090 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5091 typename iterator_traits
<_InputIterator1
>::value_type
>)
5092 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5093 typename iterator_traits
<_InputIterator2
>::value_type
>)
5094 __glibcxx_function_requires(_LessThanOpConcept
<
5095 typename iterator_traits
<_InputIterator1
>::value_type
,
5096 typename iterator_traits
<_InputIterator2
>::value_type
>)
5097 __glibcxx_function_requires(_LessThanOpConcept
<
5098 typename iterator_traits
<_InputIterator2
>::value_type
,
5099 typename iterator_traits
<_InputIterator1
>::value_type
>)
5100 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5101 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5102 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5103 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5105 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5106 __first2
, __last2
, __result
,
5107 __gnu_cxx::__ops::__iter_less_iter());
5111 * @brief Return the union of two sorted ranges using a comparison functor.
5112 * @ingroup set_algorithms
5113 * @param __first1 Start of first range.
5114 * @param __last1 End of first range.
5115 * @param __first2 Start of second range.
5116 * @param __last2 End of second range.
5117 * @param __result Start of output range.
5118 * @param __comp The comparison functor.
5119 * @return End of the output range.
5120 * @ingroup set_algorithms
5122 * This operation iterates over both ranges, copying elements present in
5123 * each range in order to the output range. Iterators increment for each
5124 * range. When the current element of one range is less than the other
5125 * according to @p __comp, that element is copied and the iterator advanced.
5126 * If an equivalent element according to @p __comp is contained in both
5127 * ranges, the element from the first range is copied and both ranges
5128 * advance. The output range may not overlap either input range.
5130 template<typename _InputIterator1
, typename _InputIterator2
,
5131 typename _OutputIterator
, typename _Compare
>
5132 _GLIBCXX20_CONSTEXPR
5133 inline _OutputIterator
5134 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5135 _InputIterator2 __first2
, _InputIterator2 __last2
,
5136 _OutputIterator __result
, _Compare __comp
)
5138 // concept requirements
5139 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5140 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5141 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5142 typename iterator_traits
<_InputIterator1
>::value_type
>)
5143 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5144 typename iterator_traits
<_InputIterator2
>::value_type
>)
5145 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5146 typename iterator_traits
<_InputIterator1
>::value_type
,
5147 typename iterator_traits
<_InputIterator2
>::value_type
>)
5148 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5149 typename iterator_traits
<_InputIterator2
>::value_type
,
5150 typename iterator_traits
<_InputIterator1
>::value_type
>)
5151 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5152 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5153 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5154 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5156 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5157 __first2
, __last2
, __result
,
5158 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5161 template<typename _InputIterator1
, typename _InputIterator2
,
5162 typename _OutputIterator
,
5164 _GLIBCXX20_CONSTEXPR
5166 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5167 _InputIterator2 __first2
, _InputIterator2 __last2
,
5168 _OutputIterator __result
, _Compare __comp
)
5170 while (__first1
!= __last1
&& __first2
!= __last2
)
5171 if (__comp(__first1
, __first2
))
5173 else if (__comp(__first2
, __first1
))
5177 *__result
= *__first1
;
5186 * @brief Return the intersection of two sorted ranges.
5187 * @ingroup set_algorithms
5188 * @param __first1 Start of first range.
5189 * @param __last1 End of first range.
5190 * @param __first2 Start of second range.
5191 * @param __last2 End of second range.
5192 * @param __result Start of output range.
5193 * @return End of the output range.
5194 * @ingroup set_algorithms
5196 * This operation iterates over both ranges, copying elements present in
5197 * both ranges in order to the output range. Iterators increment for each
5198 * range. When the current element of one range is less than the other,
5199 * that iterator advances. If an element is contained in both ranges, the
5200 * element from the first range is copied and both ranges advance. The
5201 * output range may not overlap either input range.
5203 template<typename _InputIterator1
, typename _InputIterator2
,
5204 typename _OutputIterator
>
5205 _GLIBCXX20_CONSTEXPR
5206 inline _OutputIterator
5207 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5208 _InputIterator2 __first2
, _InputIterator2 __last2
,
5209 _OutputIterator __result
)
5211 // concept requirements
5212 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5213 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5214 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5215 typename iterator_traits
<_InputIterator1
>::value_type
>)
5216 __glibcxx_function_requires(_LessThanOpConcept
<
5217 typename iterator_traits
<_InputIterator1
>::value_type
,
5218 typename iterator_traits
<_InputIterator2
>::value_type
>)
5219 __glibcxx_function_requires(_LessThanOpConcept
<
5220 typename iterator_traits
<_InputIterator2
>::value_type
,
5221 typename iterator_traits
<_InputIterator1
>::value_type
>)
5222 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5223 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5224 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5225 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5227 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5228 __first2
, __last2
, __result
,
5229 __gnu_cxx::__ops::__iter_less_iter());
5233 * @brief Return the intersection of two sorted ranges using comparison
5235 * @ingroup set_algorithms
5236 * @param __first1 Start of first range.
5237 * @param __last1 End of first range.
5238 * @param __first2 Start of second range.
5239 * @param __last2 End of second range.
5240 * @param __result Start of output range.
5241 * @param __comp The comparison functor.
5242 * @return End of the output range.
5243 * @ingroup set_algorithms
5245 * This operation iterates over both ranges, copying elements present in
5246 * both ranges in order to the output range. Iterators increment for each
5247 * range. When the current element of one range is less than the other
5248 * according to @p __comp, that iterator advances. If an element is
5249 * contained in both ranges according to @p __comp, the element from the
5250 * first range is copied and both ranges advance. The output range may not
5251 * overlap either input range.
5253 template<typename _InputIterator1
, typename _InputIterator2
,
5254 typename _OutputIterator
, typename _Compare
>
5255 _GLIBCXX20_CONSTEXPR
5256 inline _OutputIterator
5257 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5258 _InputIterator2 __first2
, _InputIterator2 __last2
,
5259 _OutputIterator __result
, _Compare __comp
)
5261 // concept requirements
5262 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5263 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5264 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5265 typename iterator_traits
<_InputIterator1
>::value_type
>)
5266 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5267 typename iterator_traits
<_InputIterator1
>::value_type
,
5268 typename iterator_traits
<_InputIterator2
>::value_type
>)
5269 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5270 typename iterator_traits
<_InputIterator2
>::value_type
,
5271 typename iterator_traits
<_InputIterator1
>::value_type
>)
5272 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5273 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5274 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5275 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5277 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5278 __first2
, __last2
, __result
,
5279 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5282 template<typename _InputIterator1
, typename _InputIterator2
,
5283 typename _OutputIterator
,
5285 _GLIBCXX20_CONSTEXPR
5287 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5288 _InputIterator2 __first2
, _InputIterator2 __last2
,
5289 _OutputIterator __result
, _Compare __comp
)
5291 while (__first1
!= __last1
&& __first2
!= __last2
)
5292 if (__comp(__first1
, __first2
))
5294 *__result
= *__first1
;
5298 else if (__comp(__first2
, __first1
))
5305 return std::copy(__first1
, __last1
, __result
);
5309 * @brief Return the difference of two sorted ranges.
5310 * @ingroup set_algorithms
5311 * @param __first1 Start of first range.
5312 * @param __last1 End of first range.
5313 * @param __first2 Start of second range.
5314 * @param __last2 End of second range.
5315 * @param __result Start of output range.
5316 * @return End of the output range.
5317 * @ingroup set_algorithms
5319 * This operation iterates over both ranges, copying elements present in
5320 * the first range but not the second in order to the output range.
5321 * Iterators increment for each range. When the current element of the
5322 * first range is less than the second, that element is copied and the
5323 * iterator advances. If the current element of the second range is less,
5324 * the iterator advances, but no element is copied. If an element is
5325 * contained in both ranges, no elements are copied and both ranges
5326 * advance. The output range may not overlap either input range.
5328 template<typename _InputIterator1
, typename _InputIterator2
,
5329 typename _OutputIterator
>
5330 _GLIBCXX20_CONSTEXPR
5331 inline _OutputIterator
5332 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5333 _InputIterator2 __first2
, _InputIterator2 __last2
,
5334 _OutputIterator __result
)
5336 // concept requirements
5337 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5338 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5339 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5340 typename iterator_traits
<_InputIterator1
>::value_type
>)
5341 __glibcxx_function_requires(_LessThanOpConcept
<
5342 typename iterator_traits
<_InputIterator1
>::value_type
,
5343 typename iterator_traits
<_InputIterator2
>::value_type
>)
5344 __glibcxx_function_requires(_LessThanOpConcept
<
5345 typename iterator_traits
<_InputIterator2
>::value_type
,
5346 typename iterator_traits
<_InputIterator1
>::value_type
>)
5347 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5348 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5349 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5350 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5352 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5353 __first2
, __last2
, __result
,
5354 __gnu_cxx::__ops::__iter_less_iter());
5358 * @brief Return the difference of two sorted ranges using comparison
5360 * @ingroup set_algorithms
5361 * @param __first1 Start of first range.
5362 * @param __last1 End of first range.
5363 * @param __first2 Start of second range.
5364 * @param __last2 End of second range.
5365 * @param __result Start of output range.
5366 * @param __comp The comparison functor.
5367 * @return End of the output range.
5368 * @ingroup set_algorithms
5370 * This operation iterates over both ranges, copying elements present in
5371 * the first range but not the second in order to the output range.
5372 * Iterators increment for each range. When the current element of the
5373 * first range is less than the second according to @p __comp, that element
5374 * is copied and the iterator advances. If the current element of the
5375 * second range is less, no element is copied and the iterator advances.
5376 * If an element is contained in both ranges according to @p __comp, no
5377 * elements are copied and both ranges advance. The output range may not
5378 * overlap either input range.
5380 template<typename _InputIterator1
, typename _InputIterator2
,
5381 typename _OutputIterator
, typename _Compare
>
5382 _GLIBCXX20_CONSTEXPR
5383 inline _OutputIterator
5384 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5385 _InputIterator2 __first2
, _InputIterator2 __last2
,
5386 _OutputIterator __result
, _Compare __comp
)
5388 // concept requirements
5389 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5390 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5391 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5392 typename iterator_traits
<_InputIterator1
>::value_type
>)
5393 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5394 typename iterator_traits
<_InputIterator1
>::value_type
,
5395 typename iterator_traits
<_InputIterator2
>::value_type
>)
5396 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5397 typename iterator_traits
<_InputIterator2
>::value_type
,
5398 typename iterator_traits
<_InputIterator1
>::value_type
>)
5399 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5400 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5401 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5402 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5404 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5405 __first2
, __last2
, __result
,
5406 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5409 template<typename _InputIterator1
, typename _InputIterator2
,
5410 typename _OutputIterator
,
5412 _GLIBCXX20_CONSTEXPR
5414 __set_symmetric_difference(_InputIterator1 __first1
,
5415 _InputIterator1 __last1
,
5416 _InputIterator2 __first2
,
5417 _InputIterator2 __last2
,
5418 _OutputIterator __result
,
5421 while (__first1
!= __last1
&& __first2
!= __last2
)
5422 if (__comp(__first1
, __first2
))
5424 *__result
= *__first1
;
5428 else if (__comp(__first2
, __first1
))
5430 *__result
= *__first2
;
5439 return std::copy(__first2
, __last2
,
5440 std::copy(__first1
, __last1
, __result
));
5444 * @brief Return the symmetric difference of two sorted ranges.
5445 * @ingroup set_algorithms
5446 * @param __first1 Start of first range.
5447 * @param __last1 End of first range.
5448 * @param __first2 Start of second range.
5449 * @param __last2 End of second range.
5450 * @param __result Start of output range.
5451 * @return End of the output range.
5452 * @ingroup set_algorithms
5454 * This operation iterates over both ranges, copying elements present in
5455 * one range but not the other in order to the output range. Iterators
5456 * increment for each range. When the current element of one range is less
5457 * than the other, that element is copied and the iterator advances. If an
5458 * element is contained in both ranges, no elements are copied and both
5459 * ranges advance. The output range may not overlap either input range.
5461 template<typename _InputIterator1
, typename _InputIterator2
,
5462 typename _OutputIterator
>
5463 _GLIBCXX20_CONSTEXPR
5464 inline _OutputIterator
5465 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5466 _InputIterator2 __first2
, _InputIterator2 __last2
,
5467 _OutputIterator __result
)
5469 // concept requirements
5470 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5471 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5472 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5473 typename iterator_traits
<_InputIterator1
>::value_type
>)
5474 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5475 typename iterator_traits
<_InputIterator2
>::value_type
>)
5476 __glibcxx_function_requires(_LessThanOpConcept
<
5477 typename iterator_traits
<_InputIterator1
>::value_type
,
5478 typename iterator_traits
<_InputIterator2
>::value_type
>)
5479 __glibcxx_function_requires(_LessThanOpConcept
<
5480 typename iterator_traits
<_InputIterator2
>::value_type
,
5481 typename iterator_traits
<_InputIterator1
>::value_type
>)
5482 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5483 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5484 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5485 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5487 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5488 __first2
, __last2
, __result
,
5489 __gnu_cxx::__ops::__iter_less_iter());
5493 * @brief Return the symmetric difference of two sorted ranges using
5494 * comparison functor.
5495 * @ingroup set_algorithms
5496 * @param __first1 Start of first range.
5497 * @param __last1 End of first range.
5498 * @param __first2 Start of second range.
5499 * @param __last2 End of second range.
5500 * @param __result Start of output range.
5501 * @param __comp The comparison functor.
5502 * @return End of the output range.
5503 * @ingroup set_algorithms
5505 * This operation iterates over both ranges, copying elements present in
5506 * one range but not the other in order to the output range. Iterators
5507 * increment for each range. When the current element of one range is less
5508 * than the other according to @p comp, that element is copied and the
5509 * iterator advances. If an element is contained in both ranges according
5510 * to @p __comp, no elements are copied and both ranges advance. The output
5511 * range may not overlap either input range.
5513 template<typename _InputIterator1
, typename _InputIterator2
,
5514 typename _OutputIterator
, typename _Compare
>
5515 _GLIBCXX20_CONSTEXPR
5516 inline _OutputIterator
5517 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5518 _InputIterator2 __first2
, _InputIterator2 __last2
,
5519 _OutputIterator __result
,
5522 // concept requirements
5523 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5524 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5525 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5526 typename iterator_traits
<_InputIterator1
>::value_type
>)
5527 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5528 typename iterator_traits
<_InputIterator2
>::value_type
>)
5529 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5530 typename iterator_traits
<_InputIterator1
>::value_type
,
5531 typename iterator_traits
<_InputIterator2
>::value_type
>)
5532 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5533 typename iterator_traits
<_InputIterator2
>::value_type
,
5534 typename iterator_traits
<_InputIterator1
>::value_type
>)
5535 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5536 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5537 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5538 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5540 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5541 __first2
, __last2
, __result
,
5542 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5545 template<typename _ForwardIterator
, typename _Compare
>
5546 _GLIBCXX14_CONSTEXPR
5548 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5551 if (__first
== __last
)
5553 _ForwardIterator __result
= __first
;
5554 while (++__first
!= __last
)
5555 if (__comp(__first
, __result
))
5561 * @brief Return the minimum element in a range.
5562 * @ingroup sorting_algorithms
5563 * @param __first Start of range.
5564 * @param __last End of range.
5565 * @return Iterator referencing the first instance of the smallest value.
5567 template<typename _ForwardIterator
>
5568 _GLIBCXX14_CONSTEXPR
5570 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5572 // concept requirements
5573 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5574 __glibcxx_function_requires(_LessThanComparableConcept
<
5575 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5576 __glibcxx_requires_valid_range(__first
, __last
);
5577 __glibcxx_requires_irreflexive(__first
, __last
);
5579 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5580 __gnu_cxx::__ops::__iter_less_iter());
5584 * @brief Return the minimum element in a range using comparison functor.
5585 * @ingroup sorting_algorithms
5586 * @param __first Start of range.
5587 * @param __last End of range.
5588 * @param __comp Comparison functor.
5589 * @return Iterator referencing the first instance of the smallest value
5590 * according to __comp.
5592 template<typename _ForwardIterator
, typename _Compare
>
5593 _GLIBCXX14_CONSTEXPR
5594 inline _ForwardIterator
5595 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5598 // concept requirements
5599 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5600 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5601 typename iterator_traits
<_ForwardIterator
>::value_type
,
5602 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5603 __glibcxx_requires_valid_range(__first
, __last
);
5604 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5606 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5607 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5610 template<typename _ForwardIterator
, typename _Compare
>
5611 _GLIBCXX14_CONSTEXPR
5613 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5616 if (__first
== __last
) return __first
;
5617 _ForwardIterator __result
= __first
;
5618 while (++__first
!= __last
)
5619 if (__comp(__result
, __first
))
5625 * @brief Return the maximum element in a range.
5626 * @ingroup sorting_algorithms
5627 * @param __first Start of range.
5628 * @param __last End of range.
5629 * @return Iterator referencing the first instance of the largest value.
5631 template<typename _ForwardIterator
>
5632 _GLIBCXX14_CONSTEXPR
5633 inline _ForwardIterator
5634 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5636 // concept requirements
5637 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5638 __glibcxx_function_requires(_LessThanComparableConcept
<
5639 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5640 __glibcxx_requires_valid_range(__first
, __last
);
5641 __glibcxx_requires_irreflexive(__first
, __last
);
5643 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5644 __gnu_cxx::__ops::__iter_less_iter());
5648 * @brief Return the maximum element in a range using comparison functor.
5649 * @ingroup sorting_algorithms
5650 * @param __first Start of range.
5651 * @param __last End of range.
5652 * @param __comp Comparison functor.
5653 * @return Iterator referencing the first instance of the largest value
5654 * according to __comp.
5656 template<typename _ForwardIterator
, typename _Compare
>
5657 _GLIBCXX14_CONSTEXPR
5658 inline _ForwardIterator
5659 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5662 // concept requirements
5663 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5664 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5665 typename iterator_traits
<_ForwardIterator
>::value_type
,
5666 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5667 __glibcxx_requires_valid_range(__first
, __last
);
5668 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5670 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5671 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5674 #if __cplusplus >= 201103L
5676 template<typename _Tp
>
5677 _GLIBCXX14_CONSTEXPR
5679 min(initializer_list
<_Tp
> __l
)
5681 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5682 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5683 __gnu_cxx::__ops::__iter_less_iter());
5686 template<typename _Tp
, typename _Compare
>
5687 _GLIBCXX14_CONSTEXPR
5689 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
5691 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5692 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5693 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5696 template<typename _Tp
>
5697 _GLIBCXX14_CONSTEXPR
5699 max(initializer_list
<_Tp
> __l
)
5701 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5702 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5703 __gnu_cxx::__ops::__iter_less_iter());
5706 template<typename _Tp
, typename _Compare
>
5707 _GLIBCXX14_CONSTEXPR
5709 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
5711 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5712 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5713 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5717 #if __cplusplus >= 201402L
5718 /// Reservoir sampling algorithm.
5719 template<typename _InputIterator
, typename _RandomAccessIterator
,
5720 typename _Size
, typename _UniformRandomBitGenerator
>
5721 _RandomAccessIterator
5722 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5723 _RandomAccessIterator __out
, random_access_iterator_tag
,
5724 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5726 using __distrib_type
= uniform_int_distribution
<_Size
>;
5727 using __param_type
= typename
__distrib_type::param_type
;
5728 __distrib_type __d
{};
5729 _Size __sample_sz
= 0;
5730 while (__first
!= __last
&& __sample_sz
!= __n
)
5732 __out
[__sample_sz
++] = *__first
;
5735 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5736 ++__first
, (void) ++__pop_sz
)
5738 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5740 __out
[__k
] = *__first
;
5742 return __out
+ __sample_sz
;
5745 /// Selection sampling algorithm.
5746 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5747 typename _Size
, typename _UniformRandomBitGenerator
>
5749 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5750 forward_iterator_tag
,
5751 _OutputIterator __out
, _Cat
,
5752 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5754 using __distrib_type
= uniform_int_distribution
<_Size
>;
5755 using __param_type
= typename
__distrib_type::param_type
;
5756 using _USize
= make_unsigned_t
<_Size
>;
5757 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5758 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5760 if (__first
== __last
)
5763 __distrib_type __d
{};
5764 _Size __unsampled_sz
= std::distance(__first
, __last
);
5765 __n
= std::min(__n
, __unsampled_sz
);
5767 // If possible, we use __gen_two_uniform_ints to efficiently produce
5768 // two random numbers using a single distribution invocation:
5770 const __uc_type __urngrange
= __g
.max() - __g
.min();
5771 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5772 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5775 while (__n
!= 0 && __unsampled_sz
>= 2)
5777 const pair
<_Size
, _Size
> __p
=
5778 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5781 if (__p
.first
< __n
)
5783 *__out
++ = *__first
;
5789 if (__n
== 0) break;
5792 if (__p
.second
< __n
)
5794 *__out
++ = *__first
;
5802 // The loop above is otherwise equivalent to this one-at-a-time version:
5804 for (; __n
!= 0; ++__first
)
5805 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5807 *__out
++ = *__first
;
5814 #ifdef __glibcxx_sample // C++ >= 17
5815 /// Take a random sample from a population.
5816 template<typename _PopulationIterator
, typename _SampleIterator
,
5817 typename _Distance
, typename _UniformRandomBitGenerator
>
5819 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5820 _SampleIterator __out
, _Distance __n
,
5821 _UniformRandomBitGenerator
&& __g
)
5823 using __pop_cat
= typename
5824 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5825 using __samp_cat
= typename
5826 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5829 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5830 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5831 "output range must use a RandomAccessIterator when input range"
5832 " does not meet the ForwardIterator requirements");
5834 static_assert(is_integral
<_Distance
>::value
,
5835 "sample size must be an integer type");
5837 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5838 return _GLIBCXX_STD_A::
5839 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5840 std::forward
<_UniformRandomBitGenerator
>(__g
));
5842 #endif // __glibcxx_sample
5844 _GLIBCXX_END_NAMESPACE_ALGO
5845 _GLIBCXX_END_NAMESPACE_VERSION
5848 #endif /* _STL_ALGO_H */