1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2022 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_algo.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
59 #include <bits/algorithmfwd.h>
60 #include <bits/stl_heap.h>
61 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
62 #include <bits/predefined_ops.h>
64 #if __cplusplus >= 201103L
65 #include <bits/uniform_int_dist.h>
68 #if _GLIBCXX_HOSTED && (__cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED)
69 #include <cstdlib> // for rand
72 // See concept_check.h for the __glibcxx_*_requires macros.
74 namespace std
_GLIBCXX_VISIBILITY(default)
76 _GLIBCXX_BEGIN_NAMESPACE_VERSION
78 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
79 template<typename _Iterator
, typename _Compare
>
82 __move_median_to_first(_Iterator __result
,_Iterator __a
, _Iterator __b
,
83 _Iterator __c
, _Compare __comp
)
88 std::iter_swap(__result
, __b
);
89 else if (__comp(__a
, __c
))
90 std::iter_swap(__result
, __c
);
92 std::iter_swap(__result
, __a
);
94 else if (__comp(__a
, __c
))
95 std::iter_swap(__result
, __a
);
96 else if (__comp(__b
, __c
))
97 std::iter_swap(__result
, __c
);
99 std::iter_swap(__result
, __b
);
102 /// Provided for stable_partition to use.
103 template<typename _InputIterator
, typename _Predicate
>
105 inline _InputIterator
106 __find_if_not(_InputIterator __first
, _InputIterator __last
,
109 return std::__find_if(__first
, __last
,
110 __gnu_cxx::__ops::__negate(__pred
),
111 std::__iterator_category(__first
));
114 /// Like find_if_not(), but uses and updates a count of the
115 /// remaining range length instead of comparing against an end
117 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
120 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
122 for (; __len
; --__len
, (void) ++__first
)
123 if (!__pred(__first
))
130 // set_symmetric_difference
141 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
142 typename _BinaryPredicate
>
145 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
146 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
147 _BinaryPredicate __predicate
)
149 // Test for empty ranges
150 if (__first1
== __last1
|| __first2
== __last2
)
153 // Test for a pattern of length 1.
154 _ForwardIterator2
__p1(__first2
);
155 if (++__p1
== __last2
)
156 return std::__find_if(__first1
, __last1
,
157 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
160 _ForwardIterator1 __current
= __first1
;
165 std::__find_if(__first1
, __last1
,
166 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
168 if (__first1
== __last1
)
171 _ForwardIterator2 __p
= __p1
;
172 __current
= __first1
;
173 if (++__current
== __last1
)
176 while (__predicate(__current
, __p
))
178 if (++__p
== __last2
)
180 if (++__current
== __last1
)
191 * This is an helper function for search_n overloaded for forward iterators.
193 template<typename _ForwardIterator
, typename _Integer
,
194 typename _UnaryPredicate
>
197 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
198 _Integer __count
, _UnaryPredicate __unary_pred
,
199 std::forward_iterator_tag
)
201 __first
= std::__find_if(__first
, __last
, __unary_pred
);
202 while (__first
!= __last
)
204 typename iterator_traits
<_ForwardIterator
>::difference_type
206 _ForwardIterator __i
= __first
;
208 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
217 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
223 * This is an helper function for search_n overloaded for random access
226 template<typename _RandomAccessIter
, typename _Integer
,
227 typename _UnaryPredicate
>
230 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
231 _Integer __count
, _UnaryPredicate __unary_pred
,
232 std::random_access_iterator_tag
)
234 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
237 _DistanceType __tailSize
= __last
- __first
;
238 _DistanceType __remainder
= __count
;
240 while (__remainder
<= __tailSize
) // the main loop...
242 __first
+= __remainder
;
243 __tailSize
-= __remainder
;
244 // __first here is always pointing to one past the last element of
245 // next possible match.
246 _RandomAccessIter __backTrack
= __first
;
247 while (__unary_pred(--__backTrack
))
249 if (--__remainder
== 0)
250 return (__first
- __count
); // Success
252 __remainder
= __count
+ 1 - (__first
- __backTrack
);
254 return __last
; // Failure
257 template<typename _ForwardIterator
, typename _Integer
,
258 typename _UnaryPredicate
>
261 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
263 _UnaryPredicate __unary_pred
)
269 return std::__find_if(__first
, __last
, __unary_pred
);
271 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
272 std::__iterator_category(__first
));
275 // find_end for forward iterators.
276 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
277 typename _BinaryPredicate
>
280 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
281 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
282 forward_iterator_tag
, forward_iterator_tag
,
283 _BinaryPredicate __comp
)
285 if (__first2
== __last2
)
288 _ForwardIterator1 __result
= __last1
;
291 _ForwardIterator1 __new_result
292 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
293 if (__new_result
== __last1
)
297 __result
= __new_result
;
298 __first1
= __new_result
;
304 // find_end for bidirectional iterators (much faster).
305 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
306 typename _BinaryPredicate
>
308 _BidirectionalIterator1
309 __find_end(_BidirectionalIterator1 __first1
,
310 _BidirectionalIterator1 __last1
,
311 _BidirectionalIterator2 __first2
,
312 _BidirectionalIterator2 __last2
,
313 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
314 _BinaryPredicate __comp
)
316 // concept requirements
317 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
318 _BidirectionalIterator1
>)
319 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
320 _BidirectionalIterator2
>)
322 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
323 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
325 _RevIterator1
__rlast1(__first1
);
326 _RevIterator2
__rlast2(__first2
);
327 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
328 _RevIterator2(__last2
), __rlast2
,
331 if (__rresult
== __rlast1
)
335 _BidirectionalIterator1 __result
= __rresult
.base();
336 std::advance(__result
, -std::distance(__first2
, __last2
));
342 * @brief Find last matching subsequence in a sequence.
343 * @ingroup non_mutating_algorithms
344 * @param __first1 Start of range to search.
345 * @param __last1 End of range to search.
346 * @param __first2 Start of sequence to match.
347 * @param __last2 End of sequence to match.
348 * @return The last iterator @c i in the range
349 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
350 * @p *(__first2+N) for each @c N in the range @p
351 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
353 * Searches the range @p [__first1,__last1) for a sub-sequence that
354 * compares equal value-by-value with the sequence given by @p
355 * [__first2,__last2) and returns an iterator to the __first
356 * element of the sub-sequence, or @p __last1 if the sub-sequence
357 * is not found. The sub-sequence will be the last such
358 * subsequence contained in [__first1,__last1).
360 * Because the sub-sequence must lie completely within the range @p
361 * [__first1,__last1) it must start at a position less than @p
362 * __last1-(__last2-__first2) where @p __last2-__first2 is the
363 * length of the sub-sequence. This means that the returned
364 * iterator @c i will be in the range @p
365 * [__first1,__last1-(__last2-__first2))
367 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
369 inline _ForwardIterator1
370 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
371 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
373 // concept requirements
374 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
375 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
376 __glibcxx_function_requires(_EqualOpConcept
<
377 typename iterator_traits
<_ForwardIterator1
>::value_type
,
378 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
379 __glibcxx_requires_valid_range(__first1
, __last1
);
380 __glibcxx_requires_valid_range(__first2
, __last2
);
382 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
383 std::__iterator_category(__first1
),
384 std::__iterator_category(__first2
),
385 __gnu_cxx::__ops::__iter_equal_to_iter());
389 * @brief Find last matching subsequence in a sequence using a predicate.
390 * @ingroup non_mutating_algorithms
391 * @param __first1 Start of range to search.
392 * @param __last1 End of range to search.
393 * @param __first2 Start of sequence to match.
394 * @param __last2 End of sequence to match.
395 * @param __comp The predicate to use.
396 * @return The last iterator @c i in the range @p
397 * [__first1,__last1-(__last2-__first2)) such that @c
398 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
399 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
402 * Searches the range @p [__first1,__last1) for a sub-sequence that
403 * compares equal value-by-value with the sequence given by @p
404 * [__first2,__last2) using comp as a predicate and returns an
405 * iterator to the first element of the sub-sequence, or @p __last1
406 * if the sub-sequence is not found. The sub-sequence will be the
407 * last such subsequence contained in [__first,__last1).
409 * Because the sub-sequence must lie completely within the range @p
410 * [__first1,__last1) it must start at a position less than @p
411 * __last1-(__last2-__first2) where @p __last2-__first2 is the
412 * length of the sub-sequence. This means that the returned
413 * iterator @c i will be in the range @p
414 * [__first1,__last1-(__last2-__first2))
416 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
417 typename _BinaryPredicate
>
419 inline _ForwardIterator1
420 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
421 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
422 _BinaryPredicate __comp
)
424 // concept requirements
425 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
426 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
427 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
428 typename iterator_traits
<_ForwardIterator1
>::value_type
,
429 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
430 __glibcxx_requires_valid_range(__first1
, __last1
);
431 __glibcxx_requires_valid_range(__first2
, __last2
);
433 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
434 std::__iterator_category(__first1
),
435 std::__iterator_category(__first2
),
436 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
439 #if __cplusplus >= 201103L
441 * @brief Checks that a predicate is true for all the elements
443 * @ingroup non_mutating_algorithms
444 * @param __first An input iterator.
445 * @param __last An input iterator.
446 * @param __pred A predicate.
447 * @return True if the check is true, false otherwise.
449 * Returns true if @p __pred is true for each element in the range
450 * @p [__first,__last), and false otherwise.
452 template<typename _InputIterator
, typename _Predicate
>
455 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
456 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
459 * @brief Checks that a predicate is false for all the elements
461 * @ingroup non_mutating_algorithms
462 * @param __first An input iterator.
463 * @param __last An input iterator.
464 * @param __pred A predicate.
465 * @return True if the check is true, false otherwise.
467 * Returns true if @p __pred is false for each element in the range
468 * @p [__first,__last), and false otherwise.
470 template<typename _InputIterator
, typename _Predicate
>
473 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
474 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
477 * @brief Checks that a predicate is true for at least one element
479 * @ingroup non_mutating_algorithms
480 * @param __first An input iterator.
481 * @param __last An input iterator.
482 * @param __pred A predicate.
483 * @return True if the check is true, false otherwise.
485 * Returns true if an element exists in the range @p
486 * [__first,__last) such that @p __pred is true, and false
489 template<typename _InputIterator
, typename _Predicate
>
492 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
493 { return !std::none_of(__first
, __last
, __pred
); }
496 * @brief Find the first element in a sequence for which a
497 * predicate is false.
498 * @ingroup non_mutating_algorithms
499 * @param __first An input iterator.
500 * @param __last An input iterator.
501 * @param __pred A predicate.
502 * @return The first iterator @c i in the range @p [__first,__last)
503 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
505 template<typename _InputIterator
, typename _Predicate
>
507 inline _InputIterator
508 find_if_not(_InputIterator __first
, _InputIterator __last
,
511 // concept requirements
512 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
513 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
514 typename iterator_traits
<_InputIterator
>::value_type
>)
515 __glibcxx_requires_valid_range(__first
, __last
);
516 return std::__find_if_not(__first
, __last
,
517 __gnu_cxx::__ops::__pred_iter(__pred
));
521 * @brief Checks whether the sequence is partitioned.
522 * @ingroup mutating_algorithms
523 * @param __first An input iterator.
524 * @param __last An input iterator.
525 * @param __pred A predicate.
526 * @return True if the range @p [__first,__last) is partioned by @p __pred,
527 * i.e. if all elements that satisfy @p __pred appear before those that
530 template<typename _InputIterator
, typename _Predicate
>
533 is_partitioned(_InputIterator __first
, _InputIterator __last
,
536 __first
= std::find_if_not(__first
, __last
, __pred
);
537 if (__first
== __last
)
540 return std::none_of(__first
, __last
, __pred
);
544 * @brief Find the partition point of a partitioned range.
545 * @ingroup mutating_algorithms
546 * @param __first An iterator.
547 * @param __last Another iterator.
548 * @param __pred A predicate.
549 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
550 * and @p none_of(mid, __last, __pred) are both true.
552 template<typename _ForwardIterator
, typename _Predicate
>
555 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
558 // concept requirements
559 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
560 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
561 typename iterator_traits
<_ForwardIterator
>::value_type
>)
563 // A specific debug-mode test will be necessary...
564 __glibcxx_requires_valid_range(__first
, __last
);
566 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
569 _DistanceType __len
= std::distance(__first
, __last
);
573 _DistanceType __half
= __len
>> 1;
574 _ForwardIterator __middle
= __first
;
575 std::advance(__middle
, __half
);
576 if (__pred(*__middle
))
580 __len
= __len
- __half
- 1;
589 template<typename _InputIterator
, typename _OutputIterator
,
593 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
594 _OutputIterator __result
, _Predicate __pred
)
596 for (; __first
!= __last
; ++__first
)
597 if (!__pred(__first
))
599 *__result
= *__first
;
606 * @brief Copy a sequence, removing elements of a given value.
607 * @ingroup mutating_algorithms
608 * @param __first An input iterator.
609 * @param __last An input iterator.
610 * @param __result An output iterator.
611 * @param __value The value to be removed.
612 * @return An iterator designating the end of the resulting sequence.
614 * Copies each element in the range @p [__first,__last) not equal
615 * to @p __value to the range beginning at @p __result.
616 * remove_copy() is stable, so the relative order of elements that
617 * are copied is unchanged.
619 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
621 inline _OutputIterator
622 remove_copy(_InputIterator __first
, _InputIterator __last
,
623 _OutputIterator __result
, const _Tp
& __value
)
625 // concept requirements
626 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
627 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
628 typename iterator_traits
<_InputIterator
>::value_type
>)
629 __glibcxx_function_requires(_EqualOpConcept
<
630 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
631 __glibcxx_requires_valid_range(__first
, __last
);
633 return std::__remove_copy_if(__first
, __last
, __result
,
634 __gnu_cxx::__ops::__iter_equals_val(__value
));
638 * @brief Copy a sequence, removing elements for which a predicate is true.
639 * @ingroup mutating_algorithms
640 * @param __first An input iterator.
641 * @param __last An input iterator.
642 * @param __result An output iterator.
643 * @param __pred A predicate.
644 * @return An iterator designating the end of the resulting sequence.
646 * Copies each element in the range @p [__first,__last) for which
647 * @p __pred returns false to the range beginning at @p __result.
649 * remove_copy_if() is stable, so the relative order of elements that are
650 * copied is unchanged.
652 template<typename _InputIterator
, typename _OutputIterator
,
655 inline _OutputIterator
656 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
657 _OutputIterator __result
, _Predicate __pred
)
659 // concept requirements
660 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
661 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
662 typename iterator_traits
<_InputIterator
>::value_type
>)
663 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
664 typename iterator_traits
<_InputIterator
>::value_type
>)
665 __glibcxx_requires_valid_range(__first
, __last
);
667 return std::__remove_copy_if(__first
, __last
, __result
,
668 __gnu_cxx::__ops::__pred_iter(__pred
));
671 #if __cplusplus >= 201103L
673 * @brief Copy the elements of a sequence for which a predicate is true.
674 * @ingroup mutating_algorithms
675 * @param __first An input iterator.
676 * @param __last An input iterator.
677 * @param __result An output iterator.
678 * @param __pred A predicate.
679 * @return An iterator designating the end of the resulting sequence.
681 * Copies each element in the range @p [__first,__last) for which
682 * @p __pred returns true to the range beginning at @p __result.
684 * copy_if() is stable, so the relative order of elements that are
685 * copied is unchanged.
687 template<typename _InputIterator
, typename _OutputIterator
,
691 copy_if(_InputIterator __first
, _InputIterator __last
,
692 _OutputIterator __result
, _Predicate __pred
)
694 // concept requirements
695 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
696 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
697 typename iterator_traits
<_InputIterator
>::value_type
>)
698 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
699 typename iterator_traits
<_InputIterator
>::value_type
>)
700 __glibcxx_requires_valid_range(__first
, __last
);
702 for (; __first
!= __last
; ++__first
)
703 if (__pred(*__first
))
705 *__result
= *__first
;
711 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
714 __copy_n(_InputIterator __first
, _Size __n
,
715 _OutputIterator __result
, input_iterator_tag
)
717 return std::__niter_wrap(__result
,
718 __copy_n_a(__first
, __n
,
719 std::__niter_base(__result
), true));
722 template<typename _RandomAccessIterator
, typename _Size
,
723 typename _OutputIterator
>
725 inline _OutputIterator
726 __copy_n(_RandomAccessIterator __first
, _Size __n
,
727 _OutputIterator __result
, random_access_iterator_tag
)
728 { return std::copy(__first
, __first
+ __n
, __result
); }
731 * @brief Copies the range [first,first+n) into [result,result+n).
732 * @ingroup mutating_algorithms
733 * @param __first An input iterator.
734 * @param __n The number of elements to copy.
735 * @param __result An output iterator.
738 * This inline function will boil down to a call to @c memmove whenever
739 * possible. Failing that, if random access iterators are passed, then the
740 * loop count will be known (and therefore a candidate for compiler
741 * optimizations such as unrolling).
743 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
745 inline _OutputIterator
746 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
748 // concept requirements
749 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
750 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
751 typename iterator_traits
<_InputIterator
>::value_type
>)
753 const auto __n2
= std::__size_to_integer(__n
);
757 __glibcxx_requires_can_increment(__first
, __n2
);
758 __glibcxx_requires_can_increment(__result
, __n2
);
760 return std::__copy_n(__first
, __n2
, __result
,
761 std::__iterator_category(__first
));
765 * @brief Copy the elements of a sequence to separate output sequences
766 * depending on the truth value of a predicate.
767 * @ingroup mutating_algorithms
768 * @param __first An input iterator.
769 * @param __last An input iterator.
770 * @param __out_true An output iterator.
771 * @param __out_false An output iterator.
772 * @param __pred A predicate.
773 * @return A pair designating the ends of the resulting sequences.
775 * Copies each element in the range @p [__first,__last) for which
776 * @p __pred returns true to the range beginning at @p out_true
777 * and each element for which @p __pred returns false to @p __out_false.
779 template<typename _InputIterator
, typename _OutputIterator1
,
780 typename _OutputIterator2
, typename _Predicate
>
782 pair
<_OutputIterator1
, _OutputIterator2
>
783 partition_copy(_InputIterator __first
, _InputIterator __last
,
784 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
787 // concept requirements
788 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
789 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
790 typename iterator_traits
<_InputIterator
>::value_type
>)
791 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
792 typename iterator_traits
<_InputIterator
>::value_type
>)
793 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
794 typename iterator_traits
<_InputIterator
>::value_type
>)
795 __glibcxx_requires_valid_range(__first
, __last
);
797 for (; __first
!= __last
; ++__first
)
798 if (__pred(*__first
))
800 *__out_true
= *__first
;
805 *__out_false
= *__first
;
809 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
814 * @brief Remove elements from a sequence.
815 * @ingroup mutating_algorithms
816 * @param __first An input iterator.
817 * @param __last An input iterator.
818 * @param __value The value to be removed.
819 * @return An iterator designating the end of the resulting sequence.
821 * All elements equal to @p __value are removed from the range
822 * @p [__first,__last).
824 * remove() is stable, so the relative order of elements that are
825 * not removed is unchanged.
827 * Elements between the end of the resulting sequence and @p __last
828 * are still present, but their value is unspecified.
830 template<typename _ForwardIterator
, typename _Tp
>
832 inline _ForwardIterator
833 remove(_ForwardIterator __first
, _ForwardIterator __last
,
836 // concept requirements
837 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
839 __glibcxx_function_requires(_EqualOpConcept
<
840 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
841 __glibcxx_requires_valid_range(__first
, __last
);
843 return std::__remove_if(__first
, __last
,
844 __gnu_cxx::__ops::__iter_equals_val(__value
));
848 * @brief Remove elements from a sequence using a predicate.
849 * @ingroup mutating_algorithms
850 * @param __first A forward iterator.
851 * @param __last A forward iterator.
852 * @param __pred A predicate.
853 * @return An iterator designating the end of the resulting sequence.
855 * All elements for which @p __pred returns true are removed from the range
856 * @p [__first,__last).
858 * remove_if() is stable, so the relative order of elements that are
859 * not removed is unchanged.
861 * Elements between the end of the resulting sequence and @p __last
862 * are still present, but their value is unspecified.
864 template<typename _ForwardIterator
, typename _Predicate
>
866 inline _ForwardIterator
867 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
870 // concept requirements
871 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
873 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
874 typename iterator_traits
<_ForwardIterator
>::value_type
>)
875 __glibcxx_requires_valid_range(__first
, __last
);
877 return std::__remove_if(__first
, __last
,
878 __gnu_cxx::__ops::__pred_iter(__pred
));
881 template<typename _ForwardIterator
, typename _BinaryPredicate
>
884 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
885 _BinaryPredicate __binary_pred
)
887 if (__first
== __last
)
889 _ForwardIterator __next
= __first
;
890 while (++__next
!= __last
)
892 if (__binary_pred(__first
, __next
))
899 template<typename _ForwardIterator
, typename _BinaryPredicate
>
902 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
903 _BinaryPredicate __binary_pred
)
905 // Skip the beginning, if already unique.
906 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
907 if (__first
== __last
)
910 // Do the real copy work.
911 _ForwardIterator __dest
= __first
;
913 while (++__first
!= __last
)
914 if (!__binary_pred(__dest
, __first
))
915 *++__dest
= _GLIBCXX_MOVE(*__first
);
920 * @brief Remove consecutive duplicate values from a sequence.
921 * @ingroup mutating_algorithms
922 * @param __first A forward iterator.
923 * @param __last A forward iterator.
924 * @return An iterator designating the end of the resulting sequence.
926 * Removes all but the first element from each group of consecutive
927 * values that compare equal.
928 * unique() is stable, so the relative order of elements that are
929 * not removed is unchanged.
930 * Elements between the end of the resulting sequence and @p __last
931 * are still present, but their value is unspecified.
933 template<typename _ForwardIterator
>
935 inline _ForwardIterator
936 unique(_ForwardIterator __first
, _ForwardIterator __last
)
938 // concept requirements
939 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
941 __glibcxx_function_requires(_EqualityComparableConcept
<
942 typename iterator_traits
<_ForwardIterator
>::value_type
>)
943 __glibcxx_requires_valid_range(__first
, __last
);
945 return std::__unique(__first
, __last
,
946 __gnu_cxx::__ops::__iter_equal_to_iter());
950 * @brief Remove consecutive values from a sequence using a predicate.
951 * @ingroup mutating_algorithms
952 * @param __first A forward iterator.
953 * @param __last A forward iterator.
954 * @param __binary_pred A binary predicate.
955 * @return An iterator designating the end of the resulting sequence.
957 * Removes all but the first element from each group of consecutive
958 * values for which @p __binary_pred returns true.
959 * unique() is stable, so the relative order of elements that are
960 * not removed is unchanged.
961 * Elements between the end of the resulting sequence and @p __last
962 * are still present, but their value is unspecified.
964 template<typename _ForwardIterator
, typename _BinaryPredicate
>
966 inline _ForwardIterator
967 unique(_ForwardIterator __first
, _ForwardIterator __last
,
968 _BinaryPredicate __binary_pred
)
970 // concept requirements
971 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
973 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
974 typename iterator_traits
<_ForwardIterator
>::value_type
,
975 typename iterator_traits
<_ForwardIterator
>::value_type
>)
976 __glibcxx_requires_valid_range(__first
, __last
);
978 return std::__unique(__first
, __last
,
979 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
983 * This is an uglified
984 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
986 * overloaded for forward iterators and output iterator as result.
988 template<typename _ForwardIterator
, typename _OutputIterator
,
989 typename _BinaryPredicate
>
992 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
993 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
994 forward_iterator_tag
, output_iterator_tag
)
996 // concept requirements -- iterators already checked
997 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
998 typename iterator_traits
<_ForwardIterator
>::value_type
,
999 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1001 _ForwardIterator __next
= __first
;
1002 *__result
= *__first
;
1003 while (++__next
!= __last
)
1004 if (!__binary_pred(__first
, __next
))
1007 *++__result
= *__first
;
1013 * This is an uglified
1014 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1016 * overloaded for input iterators and output iterator as result.
1018 template<typename _InputIterator
, typename _OutputIterator
,
1019 typename _BinaryPredicate
>
1020 _GLIBCXX20_CONSTEXPR
1022 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1023 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1024 input_iterator_tag
, output_iterator_tag
)
1026 // concept requirements -- iterators already checked
1027 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1028 typename iterator_traits
<_InputIterator
>::value_type
,
1029 typename iterator_traits
<_InputIterator
>::value_type
>)
1031 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1032 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1034 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1035 *__result
= __value
;
1036 while (++__first
!= __last
)
1037 if (!__rebound_pred(__first
, __value
))
1040 *++__result
= __value
;
1046 * This is an uglified
1047 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1049 * overloaded for input iterators and forward iterator as result.
1051 template<typename _InputIterator
, typename _ForwardIterator
,
1052 typename _BinaryPredicate
>
1053 _GLIBCXX20_CONSTEXPR
1055 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1056 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1057 input_iterator_tag
, forward_iterator_tag
)
1059 // concept requirements -- iterators already checked
1060 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1061 typename iterator_traits
<_ForwardIterator
>::value_type
,
1062 typename iterator_traits
<_InputIterator
>::value_type
>)
1063 *__result
= *__first
;
1064 while (++__first
!= __last
)
1065 if (!__binary_pred(__result
, __first
))
1066 *++__result
= *__first
;
1071 * This is an uglified reverse(_BidirectionalIterator,
1072 * _BidirectionalIterator)
1073 * overloaded for bidirectional iterators.
1075 template<typename _BidirectionalIterator
>
1076 _GLIBCXX20_CONSTEXPR
1078 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1079 bidirectional_iterator_tag
)
1082 if (__first
== __last
|| __first
== --__last
)
1086 std::iter_swap(__first
, __last
);
1092 * This is an uglified reverse(_BidirectionalIterator,
1093 * _BidirectionalIterator)
1094 * overloaded for random access iterators.
1096 template<typename _RandomAccessIterator
>
1097 _GLIBCXX20_CONSTEXPR
1099 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1100 random_access_iterator_tag
)
1102 if (__first
== __last
)
1105 while (__first
< __last
)
1107 std::iter_swap(__first
, __last
);
1114 * @brief Reverse a sequence.
1115 * @ingroup mutating_algorithms
1116 * @param __first A bidirectional iterator.
1117 * @param __last A bidirectional iterator.
1118 * @return reverse() returns no value.
1120 * Reverses the order of the elements in the range @p [__first,__last),
1121 * so that the first element becomes the last etc.
1122 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1123 * swaps @p *(__first+i) and @p *(__last-(i+1))
1125 template<typename _BidirectionalIterator
>
1126 _GLIBCXX20_CONSTEXPR
1128 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1130 // concept requirements
1131 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1132 _BidirectionalIterator
>)
1133 __glibcxx_requires_valid_range(__first
, __last
);
1134 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1138 * @brief Copy a sequence, reversing its elements.
1139 * @ingroup mutating_algorithms
1140 * @param __first A bidirectional iterator.
1141 * @param __last A bidirectional iterator.
1142 * @param __result An output iterator.
1143 * @return An iterator designating the end of the resulting sequence.
1145 * Copies the elements in the range @p [__first,__last) to the
1146 * range @p [__result,__result+(__last-__first)) such that the
1147 * order of the elements is reversed. For every @c i such that @p
1148 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1149 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1150 * The ranges @p [__first,__last) and @p
1151 * [__result,__result+(__last-__first)) must not overlap.
1153 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1154 _GLIBCXX20_CONSTEXPR
1156 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1157 _OutputIterator __result
)
1159 // concept requirements
1160 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1161 _BidirectionalIterator
>)
1162 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1163 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1164 __glibcxx_requires_valid_range(__first
, __last
);
1166 while (__first
!= __last
)
1169 *__result
= *__last
;
1176 * This is a helper function for the rotate algorithm specialized on RAIs.
1177 * It returns the greatest common divisor of two integer values.
1179 template<typename _EuclideanRingElement
>
1180 _GLIBCXX20_CONSTEXPR
1181 _EuclideanRingElement
1182 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1186 _EuclideanRingElement __t
= __m
% __n
;
1193 _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2
)
1195 /// This is a helper function for the rotate algorithm.
1196 template<typename _ForwardIterator
>
1197 _GLIBCXX20_CONSTEXPR
1199 __rotate(_ForwardIterator __first
,
1200 _ForwardIterator __middle
,
1201 _ForwardIterator __last
,
1202 forward_iterator_tag
)
1204 if (__first
== __middle
)
1206 else if (__last
== __middle
)
1209 _ForwardIterator __first2
= __middle
;
1212 std::iter_swap(__first
, __first2
);
1215 if (__first
== __middle
)
1216 __middle
= __first2
;
1218 while (__first2
!= __last
);
1220 _ForwardIterator __ret
= __first
;
1222 __first2
= __middle
;
1224 while (__first2
!= __last
)
1226 std::iter_swap(__first
, __first2
);
1229 if (__first
== __middle
)
1230 __middle
= __first2
;
1231 else if (__first2
== __last
)
1232 __first2
= __middle
;
1237 /// This is a helper function for the rotate algorithm.
1238 template<typename _BidirectionalIterator
>
1239 _GLIBCXX20_CONSTEXPR
1240 _BidirectionalIterator
1241 __rotate(_BidirectionalIterator __first
,
1242 _BidirectionalIterator __middle
,
1243 _BidirectionalIterator __last
,
1244 bidirectional_iterator_tag
)
1246 // concept requirements
1247 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1248 _BidirectionalIterator
>)
1250 if (__first
== __middle
)
1252 else if (__last
== __middle
)
1255 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1256 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1258 while (__first
!= __middle
&& __middle
!= __last
)
1260 std::iter_swap(__first
, --__last
);
1264 if (__first
== __middle
)
1266 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1271 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1276 /// This is a helper function for the rotate algorithm.
1277 template<typename _RandomAccessIterator
>
1278 _GLIBCXX20_CONSTEXPR
1279 _RandomAccessIterator
1280 __rotate(_RandomAccessIterator __first
,
1281 _RandomAccessIterator __middle
,
1282 _RandomAccessIterator __last
,
1283 random_access_iterator_tag
)
1285 // concept requirements
1286 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1287 _RandomAccessIterator
>)
1289 if (__first
== __middle
)
1291 else if (__last
== __middle
)
1294 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1296 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1299 _Distance __n
= __last
- __first
;
1300 _Distance __k
= __middle
- __first
;
1302 if (__k
== __n
- __k
)
1304 std::swap_ranges(__first
, __middle
, __middle
);
1308 _RandomAccessIterator __p
= __first
;
1309 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1313 if (__k
< __n
- __k
)
1315 if (__is_pod(_ValueType
) && __k
== 1)
1317 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1318 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1319 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1322 _RandomAccessIterator __q
= __p
+ __k
;
1323 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1325 std::iter_swap(__p
, __q
);
1332 std::swap(__n
, __k
);
1338 if (__is_pod(_ValueType
) && __k
== 1)
1340 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1341 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1342 *__p
= _GLIBCXX_MOVE(__t
);
1345 _RandomAccessIterator __q
= __p
+ __n
;
1347 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1351 std::iter_swap(__p
, __q
);
1356 std::swap(__n
, __k
);
1361 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1362 // DR 488. rotate throws away useful information
1364 * @brief Rotate the elements of a sequence.
1365 * @ingroup mutating_algorithms
1366 * @param __first A forward iterator.
1367 * @param __middle A forward iterator.
1368 * @param __last A forward iterator.
1369 * @return first + (last - middle).
1371 * Rotates the elements of the range @p [__first,__last) by
1372 * @p (__middle - __first) positions so that the element at @p __middle
1373 * is moved to @p __first, the element at @p __middle+1 is moved to
1374 * @p __first+1 and so on for each element in the range
1375 * @p [__first,__last).
1377 * This effectively swaps the ranges @p [__first,__middle) and
1378 * @p [__middle,__last).
1381 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1382 * for each @p n in the range @p [0,__last-__first).
1384 template<typename _ForwardIterator
>
1385 _GLIBCXX20_CONSTEXPR
1386 inline _ForwardIterator
1387 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1388 _ForwardIterator __last
)
1390 // concept requirements
1391 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1393 __glibcxx_requires_valid_range(__first
, __middle
);
1394 __glibcxx_requires_valid_range(__middle
, __last
);
1396 return std::__rotate(__first
, __middle
, __last
,
1397 std::__iterator_category(__first
));
1400 _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2
)
1403 * @brief Copy a sequence, rotating its elements.
1404 * @ingroup mutating_algorithms
1405 * @param __first A forward iterator.
1406 * @param __middle A forward iterator.
1407 * @param __last A forward iterator.
1408 * @param __result An output iterator.
1409 * @return An iterator designating the end of the resulting sequence.
1411 * Copies the elements of the range @p [__first,__last) to the
1412 * range beginning at @result, rotating the copied elements by
1413 * @p (__middle-__first) positions so that the element at @p __middle
1414 * is moved to @p __result, the element at @p __middle+1 is moved
1415 * to @p __result+1 and so on for each element in the range @p
1419 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1420 * for each @p n in the range @p [0,__last-__first).
1422 template<typename _ForwardIterator
, typename _OutputIterator
>
1423 _GLIBCXX20_CONSTEXPR
1424 inline _OutputIterator
1425 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1426 _ForwardIterator __last
, _OutputIterator __result
)
1428 // concept requirements
1429 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1430 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1431 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1432 __glibcxx_requires_valid_range(__first
, __middle
);
1433 __glibcxx_requires_valid_range(__middle
, __last
);
1435 return std::copy(__first
, __middle
,
1436 std::copy(__middle
, __last
, __result
));
1439 /// This is a helper function...
1440 template<typename _ForwardIterator
, typename _Predicate
>
1441 _GLIBCXX20_CONSTEXPR
1443 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1444 _Predicate __pred
, forward_iterator_tag
)
1446 if (__first
== __last
)
1449 while (__pred(*__first
))
1450 if (++__first
== __last
)
1453 _ForwardIterator __next
= __first
;
1455 while (++__next
!= __last
)
1456 if (__pred(*__next
))
1458 std::iter_swap(__first
, __next
);
1465 /// This is a helper function...
1466 template<typename _BidirectionalIterator
, typename _Predicate
>
1467 _GLIBCXX20_CONSTEXPR
1468 _BidirectionalIterator
1469 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1470 _Predicate __pred
, bidirectional_iterator_tag
)
1475 if (__first
== __last
)
1477 else if (__pred(*__first
))
1483 if (__first
== __last
)
1485 else if (!bool(__pred(*__last
)))
1489 std::iter_swap(__first
, __last
);
1496 /// This is a helper function...
1497 /// Requires __first != __last and !__pred(__first)
1498 /// and __len == distance(__first, __last).
1500 /// !__pred(__first) allows us to guarantee that we don't
1501 /// move-assign an element onto itself.
1502 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1505 __stable_partition_adaptive(_ForwardIterator __first
,
1506 _ForwardIterator __last
,
1507 _Predicate __pred
, _Distance __len
,
1509 _Distance __buffer_size
)
1514 if (__len
<= __buffer_size
)
1516 _ForwardIterator __result1
= __first
;
1517 _Pointer __result2
= __buffer
;
1519 // The precondition guarantees that !__pred(__first), so
1520 // move that element to the buffer before starting the loop.
1521 // This ensures that we only call __pred once per element.
1522 *__result2
= _GLIBCXX_MOVE(*__first
);
1525 for (; __first
!= __last
; ++__first
)
1526 if (__pred(__first
))
1528 *__result1
= _GLIBCXX_MOVE(*__first
);
1533 *__result2
= _GLIBCXX_MOVE(*__first
);
1537 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1541 _ForwardIterator __middle
= __first
;
1542 std::advance(__middle
, __len
/ 2);
1543 _ForwardIterator __left_split
=
1544 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1545 __len
/ 2, __buffer
,
1548 // Advance past true-predicate values to satisfy this
1549 // function's preconditions.
1550 _Distance __right_len
= __len
- __len
/ 2;
1551 _ForwardIterator __right_split
=
1552 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1556 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1558 __buffer
, __buffer_size
);
1560 return std::rotate(__left_split
, __middle
, __right_split
);
1563 template<typename _ForwardIterator
, typename _Predicate
>
1565 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1568 __first
= std::__find_if_not(__first
, __last
, __pred
);
1570 if (__first
== __last
)
1573 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1575 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1578 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1579 __buf(__first
, std::distance(__first
, __last
));
1581 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1582 _DistanceType(__buf
.requested_size()),
1584 _DistanceType(__buf
.size()));
1588 * @brief Move elements for which a predicate is true to the beginning
1589 * of a sequence, preserving relative ordering.
1590 * @ingroup mutating_algorithms
1591 * @param __first A forward iterator.
1592 * @param __last A forward iterator.
1593 * @param __pred A predicate functor.
1594 * @return An iterator @p middle such that @p __pred(i) is true for each
1595 * iterator @p i in the range @p [first,middle) and false for each @p i
1596 * in the range @p [middle,last).
1598 * Performs the same function as @p partition() with the additional
1599 * guarantee that the relative ordering of elements in each group is
1600 * preserved, so any two elements @p x and @p y in the range
1601 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1602 * relative ordering after calling @p stable_partition().
1604 template<typename _ForwardIterator
, typename _Predicate
>
1605 inline _ForwardIterator
1606 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1609 // concept requirements
1610 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1612 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1613 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1614 __glibcxx_requires_valid_range(__first
, __last
);
1616 return std::__stable_partition(__first
, __last
,
1617 __gnu_cxx::__ops::__pred_iter(__pred
));
1620 /// This is a helper function for the sort routines.
1621 template<typename _RandomAccessIterator
, typename _Compare
>
1622 _GLIBCXX20_CONSTEXPR
1624 __heap_select(_RandomAccessIterator __first
,
1625 _RandomAccessIterator __middle
,
1626 _RandomAccessIterator __last
, _Compare __comp
)
1628 std::__make_heap(__first
, __middle
, __comp
);
1629 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1630 if (__comp(__i
, __first
))
1631 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1636 template<typename _InputIterator
, typename _RandomAccessIterator
,
1638 _GLIBCXX20_CONSTEXPR
1639 _RandomAccessIterator
1640 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1641 _RandomAccessIterator __result_first
,
1642 _RandomAccessIterator __result_last
,
1645 typedef typename iterator_traits
<_InputIterator
>::value_type
1647 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1648 typedef typename
_RItTraits::difference_type _DistanceType
;
1650 if (__result_first
== __result_last
)
1651 return __result_last
;
1652 _RandomAccessIterator __result_real_last
= __result_first
;
1653 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1655 *__result_real_last
= *__first
;
1656 ++__result_real_last
;
1660 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1661 while (__first
!= __last
)
1663 if (__comp(__first
, __result_first
))
1664 std::__adjust_heap(__result_first
, _DistanceType(0),
1665 _DistanceType(__result_real_last
1667 _InputValueType(*__first
), __comp
);
1670 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1671 return __result_real_last
;
1675 * @brief Copy the smallest elements of a sequence.
1676 * @ingroup sorting_algorithms
1677 * @param __first An iterator.
1678 * @param __last Another iterator.
1679 * @param __result_first A random-access iterator.
1680 * @param __result_last Another random-access iterator.
1681 * @return An iterator indicating the end of the resulting sequence.
1683 * Copies and sorts the smallest N values from the range @p [__first,__last)
1684 * to the range beginning at @p __result_first, where the number of
1685 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1686 * @p (__result_last-__result_first).
1687 * After the sort if @e i and @e j are iterators in the range
1688 * @p [__result_first,__result_first+N) such that i precedes j then
1690 * The value returned is @p __result_first+N.
1692 template<typename _InputIterator
, typename _RandomAccessIterator
>
1693 _GLIBCXX20_CONSTEXPR
1694 inline _RandomAccessIterator
1695 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1696 _RandomAccessIterator __result_first
,
1697 _RandomAccessIterator __result_last
)
1699 #ifdef _GLIBCXX_CONCEPT_CHECKS
1700 typedef typename iterator_traits
<_InputIterator
>::value_type
1702 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1706 // concept requirements
1707 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1708 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1710 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1712 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1713 __glibcxx_requires_valid_range(__first
, __last
);
1714 __glibcxx_requires_irreflexive(__first
, __last
);
1715 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1717 return std::__partial_sort_copy(__first
, __last
,
1718 __result_first
, __result_last
,
1719 __gnu_cxx::__ops::__iter_less_iter());
1723 * @brief Copy the smallest elements of a sequence using a predicate for
1725 * @ingroup sorting_algorithms
1726 * @param __first An input iterator.
1727 * @param __last Another input iterator.
1728 * @param __result_first A random-access iterator.
1729 * @param __result_last Another random-access iterator.
1730 * @param __comp A comparison functor.
1731 * @return An iterator indicating the end of the resulting sequence.
1733 * Copies and sorts the smallest N values from the range @p [__first,__last)
1734 * to the range beginning at @p result_first, where the number of
1735 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1736 * @p (__result_last-__result_first).
1737 * After the sort if @e i and @e j are iterators in the range
1738 * @p [__result_first,__result_first+N) such that i precedes j then
1739 * @p __comp(*j,*i) is false.
1740 * The value returned is @p __result_first+N.
1742 template<typename _InputIterator
, typename _RandomAccessIterator
,
1744 _GLIBCXX20_CONSTEXPR
1745 inline _RandomAccessIterator
1746 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1747 _RandomAccessIterator __result_first
,
1748 _RandomAccessIterator __result_last
,
1751 #ifdef _GLIBCXX_CONCEPT_CHECKS
1752 typedef typename iterator_traits
<_InputIterator
>::value_type
1754 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1758 // concept requirements
1759 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1760 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1761 _RandomAccessIterator
>)
1762 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1764 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1765 _InputValueType
, _OutputValueType
>)
1766 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1767 _OutputValueType
, _OutputValueType
>)
1768 __glibcxx_requires_valid_range(__first
, __last
);
1769 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1770 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1772 return std::__partial_sort_copy(__first
, __last
,
1773 __result_first
, __result_last
,
1774 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1777 /// This is a helper function for the sort routine.
1778 template<typename _RandomAccessIterator
, typename _Compare
>
1779 _GLIBCXX20_CONSTEXPR
1781 __unguarded_linear_insert(_RandomAccessIterator __last
,
1784 typename iterator_traits
<_RandomAccessIterator
>::value_type
1785 __val
= _GLIBCXX_MOVE(*__last
);
1786 _RandomAccessIterator __next
= __last
;
1788 while (__comp(__val
, __next
))
1790 *__last
= _GLIBCXX_MOVE(*__next
);
1794 *__last
= _GLIBCXX_MOVE(__val
);
1797 /// This is a helper function for the sort routine.
1798 template<typename _RandomAccessIterator
, typename _Compare
>
1799 _GLIBCXX20_CONSTEXPR
1801 __insertion_sort(_RandomAccessIterator __first
,
1802 _RandomAccessIterator __last
, _Compare __comp
)
1804 if (__first
== __last
) return;
1806 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1808 if (__comp(__i
, __first
))
1810 typename iterator_traits
<_RandomAccessIterator
>::value_type
1811 __val
= _GLIBCXX_MOVE(*__i
);
1812 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1813 *__first
= _GLIBCXX_MOVE(__val
);
1816 std::__unguarded_linear_insert(__i
,
1817 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1821 /// This is a helper function for the sort routine.
1822 template<typename _RandomAccessIterator
, typename _Compare
>
1823 _GLIBCXX20_CONSTEXPR
1825 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1826 _RandomAccessIterator __last
, _Compare __comp
)
1828 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1829 std::__unguarded_linear_insert(__i
,
1830 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1835 * This controls some aspect of the sort routines.
1837 enum { _S_threshold
= 16 };
1839 /// This is a helper function for the sort routine.
1840 template<typename _RandomAccessIterator
, typename _Compare
>
1841 _GLIBCXX20_CONSTEXPR
1843 __final_insertion_sort(_RandomAccessIterator __first
,
1844 _RandomAccessIterator __last
, _Compare __comp
)
1846 if (__last
- __first
> int(_S_threshold
))
1848 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1849 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1853 std::__insertion_sort(__first
, __last
, __comp
);
1856 /// This is a helper function...
1857 template<typename _RandomAccessIterator
, typename _Compare
>
1858 _GLIBCXX20_CONSTEXPR
1859 _RandomAccessIterator
1860 __unguarded_partition(_RandomAccessIterator __first
,
1861 _RandomAccessIterator __last
,
1862 _RandomAccessIterator __pivot
, _Compare __comp
)
1866 while (__comp(__first
, __pivot
))
1869 while (__comp(__pivot
, __last
))
1871 if (!(__first
< __last
))
1873 std::iter_swap(__first
, __last
);
1878 /// This is a helper function...
1879 template<typename _RandomAccessIterator
, typename _Compare
>
1880 _GLIBCXX20_CONSTEXPR
1881 inline _RandomAccessIterator
1882 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1883 _RandomAccessIterator __last
, _Compare __comp
)
1885 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1886 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1888 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1891 template<typename _RandomAccessIterator
, typename _Compare
>
1892 _GLIBCXX20_CONSTEXPR
1894 __partial_sort(_RandomAccessIterator __first
,
1895 _RandomAccessIterator __middle
,
1896 _RandomAccessIterator __last
,
1899 std::__heap_select(__first
, __middle
, __last
, __comp
);
1900 std::__sort_heap(__first
, __middle
, __comp
);
1903 /// This is a helper function for the sort routine.
1904 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1905 _GLIBCXX20_CONSTEXPR
1907 __introsort_loop(_RandomAccessIterator __first
,
1908 _RandomAccessIterator __last
,
1909 _Size __depth_limit
, _Compare __comp
)
1911 while (__last
- __first
> int(_S_threshold
))
1913 if (__depth_limit
== 0)
1915 std::__partial_sort(__first
, __last
, __last
, __comp
);
1919 _RandomAccessIterator __cut
=
1920 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1921 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1928 template<typename _RandomAccessIterator
, typename _Compare
>
1929 _GLIBCXX20_CONSTEXPR
1931 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1934 if (__first
!= __last
)
1936 std::__introsort_loop(__first
, __last
,
1937 std::__lg(__last
- __first
) * 2,
1939 std::__final_insertion_sort(__first
, __last
, __comp
);
1943 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1944 _GLIBCXX20_CONSTEXPR
1946 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1947 _RandomAccessIterator __last
, _Size __depth_limit
,
1950 while (__last
- __first
> 3)
1952 if (__depth_limit
== 0)
1954 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1955 // Place the nth largest element in its final position.
1956 std::iter_swap(__first
, __nth
);
1960 _RandomAccessIterator __cut
=
1961 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1967 std::__insertion_sort(__first
, __last
, __comp
);
1972 // lower_bound moved to stl_algobase.h
1975 * @brief Finds the first position in which @p __val could be inserted
1976 * without changing the ordering.
1977 * @ingroup binary_search_algorithms
1978 * @param __first An iterator.
1979 * @param __last Another iterator.
1980 * @param __val The search term.
1981 * @param __comp A functor to use for comparisons.
1982 * @return An iterator pointing to the first element <em>not less
1983 * than</em> @p __val, or end() if every element is less
1985 * @ingroup binary_search_algorithms
1987 * The comparison function should have the same effects on ordering as
1988 * the function used for the initial sort.
1990 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
1991 _GLIBCXX20_CONSTEXPR
1992 inline _ForwardIterator
1993 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1994 const _Tp
& __val
, _Compare __comp
)
1996 // concept requirements
1997 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1998 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1999 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2000 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2003 return std::__lower_bound(__first
, __last
, __val
,
2004 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2007 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2008 _GLIBCXX20_CONSTEXPR
2010 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2011 const _Tp
& __val
, _Compare __comp
)
2013 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2016 _DistanceType __len
= std::distance(__first
, __last
);
2020 _DistanceType __half
= __len
>> 1;
2021 _ForwardIterator __middle
= __first
;
2022 std::advance(__middle
, __half
);
2023 if (__comp(__val
, __middle
))
2029 __len
= __len
- __half
- 1;
2036 * @brief Finds the last position in which @p __val could be inserted
2037 * without changing the ordering.
2038 * @ingroup binary_search_algorithms
2039 * @param __first An iterator.
2040 * @param __last Another iterator.
2041 * @param __val The search term.
2042 * @return An iterator pointing to the first element greater than @p __val,
2043 * or end() if no elements are greater than @p __val.
2044 * @ingroup binary_search_algorithms
2046 template<typename _ForwardIterator
, typename _Tp
>
2047 _GLIBCXX20_CONSTEXPR
2048 inline _ForwardIterator
2049 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2052 // concept requirements
2053 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2054 __glibcxx_function_requires(_LessThanOpConcept
<
2055 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2056 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2058 return std::__upper_bound(__first
, __last
, __val
,
2059 __gnu_cxx::__ops::__val_less_iter());
2063 * @brief Finds the last position in which @p __val could be inserted
2064 * without changing the ordering.
2065 * @ingroup binary_search_algorithms
2066 * @param __first An iterator.
2067 * @param __last Another iterator.
2068 * @param __val The search term.
2069 * @param __comp A functor to use for comparisons.
2070 * @return An iterator pointing to the first element greater than @p __val,
2071 * or end() if no elements are greater than @p __val.
2072 * @ingroup binary_search_algorithms
2074 * The comparison function should have the same effects on ordering as
2075 * the function used for the initial sort.
2077 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2078 _GLIBCXX20_CONSTEXPR
2079 inline _ForwardIterator
2080 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2081 const _Tp
& __val
, _Compare __comp
)
2083 // concept requirements
2084 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2085 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2086 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2087 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2090 return std::__upper_bound(__first
, __last
, __val
,
2091 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2094 template<typename _ForwardIterator
, typename _Tp
,
2095 typename _CompareItTp
, typename _CompareTpIt
>
2096 _GLIBCXX20_CONSTEXPR
2097 pair
<_ForwardIterator
, _ForwardIterator
>
2098 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2100 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2102 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2105 _DistanceType __len
= std::distance(__first
, __last
);
2109 _DistanceType __half
= __len
>> 1;
2110 _ForwardIterator __middle
= __first
;
2111 std::advance(__middle
, __half
);
2112 if (__comp_it_val(__middle
, __val
))
2116 __len
= __len
- __half
- 1;
2118 else if (__comp_val_it(__val
, __middle
))
2122 _ForwardIterator __left
2123 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2124 std::advance(__first
, __len
);
2125 _ForwardIterator __right
2126 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2127 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2130 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2134 * @brief Finds the largest subrange in which @p __val could be inserted
2135 * at any place in it without changing the ordering.
2136 * @ingroup binary_search_algorithms
2137 * @param __first An iterator.
2138 * @param __last Another iterator.
2139 * @param __val The search term.
2140 * @return An pair of iterators defining the subrange.
2141 * @ingroup binary_search_algorithms
2143 * This is equivalent to
2145 * std::make_pair(lower_bound(__first, __last, __val),
2146 * upper_bound(__first, __last, __val))
2148 * but does not actually call those functions.
2150 template<typename _ForwardIterator
, typename _Tp
>
2151 _GLIBCXX20_CONSTEXPR
2152 inline pair
<_ForwardIterator
, _ForwardIterator
>
2153 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2156 // concept requirements
2157 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2158 __glibcxx_function_requires(_LessThanOpConcept
<
2159 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2160 __glibcxx_function_requires(_LessThanOpConcept
<
2161 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2162 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2163 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2165 return std::__equal_range(__first
, __last
, __val
,
2166 __gnu_cxx::__ops::__iter_less_val(),
2167 __gnu_cxx::__ops::__val_less_iter());
2171 * @brief Finds the largest subrange in which @p __val could be inserted
2172 * at any place in it without changing the ordering.
2173 * @param __first An iterator.
2174 * @param __last Another iterator.
2175 * @param __val The search term.
2176 * @param __comp A functor to use for comparisons.
2177 * @return An pair of iterators defining the subrange.
2178 * @ingroup binary_search_algorithms
2180 * This is equivalent to
2182 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2183 * upper_bound(__first, __last, __val, __comp))
2185 * but does not actually call those functions.
2187 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2188 _GLIBCXX20_CONSTEXPR
2189 inline pair
<_ForwardIterator
, _ForwardIterator
>
2190 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2191 const _Tp
& __val
, _Compare __comp
)
2193 // concept requirements
2194 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2195 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2196 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2197 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2198 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2199 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2201 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2204 return std::__equal_range(__first
, __last
, __val
,
2205 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2206 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2210 * @brief Determines whether an element exists in a range.
2211 * @ingroup binary_search_algorithms
2212 * @param __first An iterator.
2213 * @param __last Another iterator.
2214 * @param __val The search term.
2215 * @return True if @p __val (or its equivalent) is in [@p
2216 * __first,@p __last ].
2218 * Note that this does not actually return an iterator to @p __val. For
2219 * that, use std::find or a container's specialized find member functions.
2221 template<typename _ForwardIterator
, typename _Tp
>
2222 _GLIBCXX20_CONSTEXPR
2224 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2227 // concept requirements
2228 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2229 __glibcxx_function_requires(_LessThanOpConcept
<
2230 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2231 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2232 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2234 _ForwardIterator __i
2235 = std::__lower_bound(__first
, __last
, __val
,
2236 __gnu_cxx::__ops::__iter_less_val());
2237 return __i
!= __last
&& !(__val
< *__i
);
2241 * @brief Determines whether an element exists in a range.
2242 * @ingroup binary_search_algorithms
2243 * @param __first An iterator.
2244 * @param __last Another iterator.
2245 * @param __val The search term.
2246 * @param __comp A functor to use for comparisons.
2247 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2249 * Note that this does not actually return an iterator to @p __val. For
2250 * that, use std::find or a container's specialized find member functions.
2252 * The comparison function should have the same effects on ordering as
2253 * the function used for the initial sort.
2255 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2256 _GLIBCXX20_CONSTEXPR
2258 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2259 const _Tp
& __val
, _Compare __comp
)
2261 // concept requirements
2262 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2263 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2264 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2265 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2267 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2270 _ForwardIterator __i
2271 = std::__lower_bound(__first
, __last
, __val
,
2272 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2273 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2278 /// This is a helper function for the __merge_adaptive routines.
2279 template<typename _InputIterator1
, typename _InputIterator2
,
2280 typename _OutputIterator
, typename _Compare
>
2282 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2283 _InputIterator2 __first2
, _InputIterator2 __last2
,
2284 _OutputIterator __result
, _Compare __comp
)
2286 while (__first1
!= __last1
&& __first2
!= __last2
)
2288 if (__comp(__first2
, __first1
))
2290 *__result
= _GLIBCXX_MOVE(*__first2
);
2295 *__result
= _GLIBCXX_MOVE(*__first1
);
2300 if (__first1
!= __last1
)
2301 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2304 /// This is a helper function for the __merge_adaptive routines.
2305 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2306 typename _BidirectionalIterator3
, typename _Compare
>
2308 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2309 _BidirectionalIterator1 __last1
,
2310 _BidirectionalIterator2 __first2
,
2311 _BidirectionalIterator2 __last2
,
2312 _BidirectionalIterator3 __result
,
2315 if (__first1
== __last1
)
2317 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2320 else if (__first2
== __last2
)
2327 if (__comp(__last2
, __last1
))
2329 *--__result
= _GLIBCXX_MOVE(*__last1
);
2330 if (__first1
== __last1
)
2332 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2339 *--__result
= _GLIBCXX_MOVE(*__last2
);
2340 if (__first2
== __last2
)
2347 /// This is a helper function for the merge routines.
2348 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2350 _BidirectionalIterator1
2351 __rotate_adaptive(_BidirectionalIterator1 __first
,
2352 _BidirectionalIterator1 __middle
,
2353 _BidirectionalIterator1 __last
,
2354 _Distance __len1
, _Distance __len2
,
2355 _BidirectionalIterator2 __buffer
,
2356 _Distance __buffer_size
)
2358 _BidirectionalIterator2 __buffer_end
;
2359 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2363 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2364 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2365 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2370 else if (__len1
<= __buffer_size
)
2374 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2375 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2376 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2382 return std::rotate(__first
, __middle
, __last
);
2385 /// This is a helper function for the merge routines.
2386 template<typename _BidirectionalIterator
, typename _Distance
,
2387 typename _Pointer
, typename _Compare
>
2389 __merge_adaptive(_BidirectionalIterator __first
,
2390 _BidirectionalIterator __middle
,
2391 _BidirectionalIterator __last
,
2392 _Distance __len1
, _Distance __len2
,
2393 _Pointer __buffer
, _Distance __buffer_size
,
2396 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2398 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2399 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2402 else if (__len2
<= __buffer_size
)
2404 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2405 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2406 __buffer_end
, __last
, __comp
);
2410 _BidirectionalIterator __first_cut
= __first
;
2411 _BidirectionalIterator __second_cut
= __middle
;
2412 _Distance __len11
= 0;
2413 _Distance __len22
= 0;
2414 if (__len1
> __len2
)
2416 __len11
= __len1
/ 2;
2417 std::advance(__first_cut
, __len11
);
2419 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2420 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2421 __len22
= std::distance(__middle
, __second_cut
);
2425 __len22
= __len2
/ 2;
2426 std::advance(__second_cut
, __len22
);
2428 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2429 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2430 __len11
= std::distance(__first
, __first_cut
);
2433 _BidirectionalIterator __new_middle
2434 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2435 __len1
- __len11
, __len22
, __buffer
,
2437 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2438 __len22
, __buffer
, __buffer_size
, __comp
);
2439 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2441 __len2
- __len22
, __buffer
,
2442 __buffer_size
, __comp
);
2446 /// This is a helper function for the merge routines.
2447 template<typename _BidirectionalIterator
, typename _Distance
,
2450 __merge_without_buffer(_BidirectionalIterator __first
,
2451 _BidirectionalIterator __middle
,
2452 _BidirectionalIterator __last
,
2453 _Distance __len1
, _Distance __len2
,
2456 if (__len1
== 0 || __len2
== 0)
2459 if (__len1
+ __len2
== 2)
2461 if (__comp(__middle
, __first
))
2462 std::iter_swap(__first
, __middle
);
2466 _BidirectionalIterator __first_cut
= __first
;
2467 _BidirectionalIterator __second_cut
= __middle
;
2468 _Distance __len11
= 0;
2469 _Distance __len22
= 0;
2470 if (__len1
> __len2
)
2472 __len11
= __len1
/ 2;
2473 std::advance(__first_cut
, __len11
);
2475 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2476 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2477 __len22
= std::distance(__middle
, __second_cut
);
2481 __len22
= __len2
/ 2;
2482 std::advance(__second_cut
, __len22
);
2484 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2485 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2486 __len11
= std::distance(__first
, __first_cut
);
2489 _BidirectionalIterator __new_middle
2490 = std::rotate(__first_cut
, __middle
, __second_cut
);
2491 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2492 __len11
, __len22
, __comp
);
2493 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2494 __len1
- __len11
, __len2
- __len22
, __comp
);
2497 template<typename _BidirectionalIterator
, typename _Compare
>
2499 __inplace_merge(_BidirectionalIterator __first
,
2500 _BidirectionalIterator __middle
,
2501 _BidirectionalIterator __last
,
2504 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2506 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2508 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2510 if (__first
== __middle
|| __middle
== __last
)
2513 const _DistanceType __len1
= std::distance(__first
, __middle
);
2514 const _DistanceType __len2
= std::distance(__middle
, __last
);
2516 // __merge_adaptive will use a buffer for the smaller of
2517 // [first,middle) and [middle,last).
2518 _TmpBuf
__buf(__first
, std::min(__len1
, __len2
));
2520 if (__buf
.begin() == 0)
2521 std::__merge_without_buffer
2522 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2524 std::__merge_adaptive
2525 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2526 _DistanceType(__buf
.size()), __comp
);
2530 * @brief Merges two sorted ranges in place.
2531 * @ingroup sorting_algorithms
2532 * @param __first An iterator.
2533 * @param __middle Another iterator.
2534 * @param __last Another iterator.
2537 * Merges two sorted and consecutive ranges, [__first,__middle) and
2538 * [__middle,__last), and puts the result in [__first,__last). The
2539 * output will be sorted. The sort is @e stable, that is, for
2540 * equivalent elements in the two ranges, elements from the first
2541 * range will always come before elements from the second.
2543 * If enough additional memory is available, this takes (__last-__first)-1
2544 * comparisons. Otherwise an NlogN algorithm is used, where N is
2545 * distance(__first,__last).
2547 template<typename _BidirectionalIterator
>
2549 inplace_merge(_BidirectionalIterator __first
,
2550 _BidirectionalIterator __middle
,
2551 _BidirectionalIterator __last
)
2553 // concept requirements
2554 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2555 _BidirectionalIterator
>)
2556 __glibcxx_function_requires(_LessThanComparableConcept
<
2557 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2558 __glibcxx_requires_sorted(__first
, __middle
);
2559 __glibcxx_requires_sorted(__middle
, __last
);
2560 __glibcxx_requires_irreflexive(__first
, __last
);
2562 std::__inplace_merge(__first
, __middle
, __last
,
2563 __gnu_cxx::__ops::__iter_less_iter());
2567 * @brief Merges two sorted ranges in place.
2568 * @ingroup sorting_algorithms
2569 * @param __first An iterator.
2570 * @param __middle Another iterator.
2571 * @param __last Another iterator.
2572 * @param __comp A functor to use for comparisons.
2575 * Merges two sorted and consecutive ranges, [__first,__middle) and
2576 * [middle,last), and puts the result in [__first,__last). The output will
2577 * be sorted. The sort is @e stable, that is, for equivalent
2578 * elements in the two ranges, elements from the first range will always
2579 * come before elements from the second.
2581 * If enough additional memory is available, this takes (__last-__first)-1
2582 * comparisons. Otherwise an NlogN algorithm is used, where N is
2583 * distance(__first,__last).
2585 * The comparison function should have the same effects on ordering as
2586 * the function used for the initial sort.
2588 template<typename _BidirectionalIterator
, typename _Compare
>
2590 inplace_merge(_BidirectionalIterator __first
,
2591 _BidirectionalIterator __middle
,
2592 _BidirectionalIterator __last
,
2595 // concept requirements
2596 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2597 _BidirectionalIterator
>)
2598 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2599 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2600 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2601 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2602 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2603 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2605 std::__inplace_merge(__first
, __middle
, __last
,
2606 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2610 /// This is a helper function for the __merge_sort_loop routines.
2611 template<typename _InputIterator
, typename _OutputIterator
,
2614 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2615 _InputIterator __first2
, _InputIterator __last2
,
2616 _OutputIterator __result
, _Compare __comp
)
2618 while (__first1
!= __last1
&& __first2
!= __last2
)
2620 if (__comp(__first2
, __first1
))
2622 *__result
= _GLIBCXX_MOVE(*__first2
);
2627 *__result
= _GLIBCXX_MOVE(*__first1
);
2632 return _GLIBCXX_MOVE3(__first2
, __last2
,
2633 _GLIBCXX_MOVE3(__first1
, __last1
,
2637 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2638 typename _Distance
, typename _Compare
>
2640 __merge_sort_loop(_RandomAccessIterator1 __first
,
2641 _RandomAccessIterator1 __last
,
2642 _RandomAccessIterator2 __result
, _Distance __step_size
,
2645 const _Distance __two_step
= 2 * __step_size
;
2647 while (__last
- __first
>= __two_step
)
2649 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2650 __first
+ __step_size
,
2651 __first
+ __two_step
,
2653 __first
+= __two_step
;
2655 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2657 std::__move_merge(__first
, __first
+ __step_size
,
2658 __first
+ __step_size
, __last
, __result
, __comp
);
2661 template<typename _RandomAccessIterator
, typename _Distance
,
2663 _GLIBCXX20_CONSTEXPR
2665 __chunk_insertion_sort(_RandomAccessIterator __first
,
2666 _RandomAccessIterator __last
,
2667 _Distance __chunk_size
, _Compare __comp
)
2669 while (__last
- __first
>= __chunk_size
)
2671 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2672 __first
+= __chunk_size
;
2674 std::__insertion_sort(__first
, __last
, __comp
);
2677 enum { _S_chunk_size
= 7 };
2679 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2681 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2682 _RandomAccessIterator __last
,
2683 _Pointer __buffer
, _Compare __comp
)
2685 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2688 const _Distance __len
= __last
- __first
;
2689 const _Pointer __buffer_last
= __buffer
+ __len
;
2691 _Distance __step_size
= _S_chunk_size
;
2692 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2694 while (__step_size
< __len
)
2696 std::__merge_sort_loop(__first
, __last
, __buffer
,
2697 __step_size
, __comp
);
2699 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2700 __step_size
, __comp
);
2705 template<typename _RandomAccessIterator
, typename _Pointer
,
2706 typename _Distance
, typename _Compare
>
2708 __stable_sort_adaptive(_RandomAccessIterator __first
,
2709 _RandomAccessIterator __last
,
2710 _Pointer __buffer
, _Distance __buffer_size
,
2713 const _Distance __len
= (__last
- __first
+ 1) / 2;
2714 const _RandomAccessIterator __middle
= __first
+ __len
;
2715 if (__len
> __buffer_size
)
2717 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2718 __buffer_size
, __comp
);
2719 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2720 __buffer_size
, __comp
);
2724 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2725 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2728 std::__merge_adaptive(__first
, __middle
, __last
,
2729 _Distance(__middle
- __first
),
2730 _Distance(__last
- __middle
),
2731 __buffer
, __buffer_size
,
2735 /// This is a helper function for the stable sorting routines.
2736 template<typename _RandomAccessIterator
, typename _Compare
>
2738 __inplace_stable_sort(_RandomAccessIterator __first
,
2739 _RandomAccessIterator __last
, _Compare __comp
)
2741 if (__last
- __first
< 15)
2743 std::__insertion_sort(__first
, __last
, __comp
);
2746 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2747 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2748 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2749 std::__merge_without_buffer(__first
, __middle
, __last
,
2757 // Set algorithms: includes, set_union, set_intersection, set_difference,
2758 // set_symmetric_difference. All of these algorithms have the precondition
2759 // that their input ranges are sorted and the postcondition that their output
2760 // ranges are sorted.
2762 template<typename _InputIterator1
, typename _InputIterator2
,
2764 _GLIBCXX20_CONSTEXPR
2766 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2767 _InputIterator2 __first2
, _InputIterator2 __last2
,
2770 while (__first1
!= __last1
&& __first2
!= __last2
)
2772 if (__comp(__first2
, __first1
))
2774 if (!__comp(__first1
, __first2
))
2779 return __first2
== __last2
;
2783 * @brief Determines whether all elements of a sequence exists in a range.
2784 * @param __first1 Start of search range.
2785 * @param __last1 End of search range.
2786 * @param __first2 Start of sequence
2787 * @param __last2 End of sequence.
2788 * @return True if each element in [__first2,__last2) is contained in order
2789 * within [__first1,__last1). False otherwise.
2790 * @ingroup set_algorithms
2792 * This operation expects both [__first1,__last1) and
2793 * [__first2,__last2) to be sorted. Searches for the presence of
2794 * each element in [__first2,__last2) within [__first1,__last1).
2795 * The iterators over each range only move forward, so this is a
2796 * linear algorithm. If an element in [__first2,__last2) is not
2797 * found before the search iterator reaches @p __last2, false is
2800 template<typename _InputIterator1
, typename _InputIterator2
>
2801 _GLIBCXX20_CONSTEXPR
2803 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2804 _InputIterator2 __first2
, _InputIterator2 __last2
)
2806 // concept requirements
2807 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2808 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2809 __glibcxx_function_requires(_LessThanOpConcept
<
2810 typename iterator_traits
<_InputIterator1
>::value_type
,
2811 typename iterator_traits
<_InputIterator2
>::value_type
>)
2812 __glibcxx_function_requires(_LessThanOpConcept
<
2813 typename iterator_traits
<_InputIterator2
>::value_type
,
2814 typename iterator_traits
<_InputIterator1
>::value_type
>)
2815 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2816 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2817 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2818 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2820 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2821 __gnu_cxx::__ops::__iter_less_iter());
2825 * @brief Determines whether all elements of a sequence exists in a range
2827 * @ingroup set_algorithms
2828 * @param __first1 Start of search range.
2829 * @param __last1 End of search range.
2830 * @param __first2 Start of sequence
2831 * @param __last2 End of sequence.
2832 * @param __comp Comparison function to use.
2833 * @return True if each element in [__first2,__last2) is contained
2834 * in order within [__first1,__last1) according to comp. False
2835 * otherwise. @ingroup set_algorithms
2837 * This operation expects both [__first1,__last1) and
2838 * [__first2,__last2) to be sorted. Searches for the presence of
2839 * each element in [__first2,__last2) within [__first1,__last1),
2840 * using comp to decide. The iterators over each range only move
2841 * forward, so this is a linear algorithm. If an element in
2842 * [__first2,__last2) is not found before the search iterator
2843 * reaches @p __last2, false is returned.
2845 template<typename _InputIterator1
, typename _InputIterator2
,
2847 _GLIBCXX20_CONSTEXPR
2849 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2850 _InputIterator2 __first2
, _InputIterator2 __last2
,
2853 // concept requirements
2854 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2855 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2856 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2857 typename iterator_traits
<_InputIterator1
>::value_type
,
2858 typename iterator_traits
<_InputIterator2
>::value_type
>)
2859 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2860 typename iterator_traits
<_InputIterator2
>::value_type
,
2861 typename iterator_traits
<_InputIterator1
>::value_type
>)
2862 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2863 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2864 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2865 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2867 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2868 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2877 // set_symmetric_difference
2881 template<typename _BidirectionalIterator
, typename _Compare
>
2882 _GLIBCXX20_CONSTEXPR
2884 __next_permutation(_BidirectionalIterator __first
,
2885 _BidirectionalIterator __last
, _Compare __comp
)
2887 if (__first
== __last
)
2889 _BidirectionalIterator __i
= __first
;
2898 _BidirectionalIterator __ii
= __i
;
2900 if (__comp(__i
, __ii
))
2902 _BidirectionalIterator __j
= __last
;
2903 while (!__comp(__i
, --__j
))
2905 std::iter_swap(__i
, __j
);
2906 std::__reverse(__ii
, __last
,
2907 std::__iterator_category(__first
));
2912 std::__reverse(__first
, __last
,
2913 std::__iterator_category(__first
));
2920 * @brief Permute range into the next @e dictionary ordering.
2921 * @ingroup sorting_algorithms
2922 * @param __first Start of range.
2923 * @param __last End of range.
2924 * @return False if wrapped to first permutation, true otherwise.
2926 * Treats all permutations of the range as a set of @e dictionary sorted
2927 * sequences. Permutes the current sequence into the next one of this set.
2928 * Returns true if there are more sequences to generate. If the sequence
2929 * is the largest of the set, the smallest is generated and false returned.
2931 template<typename _BidirectionalIterator
>
2932 _GLIBCXX20_CONSTEXPR
2934 next_permutation(_BidirectionalIterator __first
,
2935 _BidirectionalIterator __last
)
2937 // concept requirements
2938 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2939 _BidirectionalIterator
>)
2940 __glibcxx_function_requires(_LessThanComparableConcept
<
2941 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2942 __glibcxx_requires_valid_range(__first
, __last
);
2943 __glibcxx_requires_irreflexive(__first
, __last
);
2945 return std::__next_permutation
2946 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2950 * @brief Permute range into the next @e dictionary ordering using
2951 * comparison functor.
2952 * @ingroup sorting_algorithms
2953 * @param __first Start of range.
2954 * @param __last End of range.
2955 * @param __comp A comparison functor.
2956 * @return False if wrapped to first permutation, true otherwise.
2958 * Treats all permutations of the range [__first,__last) as a set of
2959 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2960 * sequence into the next one of this set. Returns true if there are more
2961 * sequences to generate. If the sequence is the largest of the set, the
2962 * smallest is generated and false returned.
2964 template<typename _BidirectionalIterator
, typename _Compare
>
2965 _GLIBCXX20_CONSTEXPR
2967 next_permutation(_BidirectionalIterator __first
,
2968 _BidirectionalIterator __last
, _Compare __comp
)
2970 // concept requirements
2971 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2972 _BidirectionalIterator
>)
2973 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2974 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2975 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2976 __glibcxx_requires_valid_range(__first
, __last
);
2977 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2979 return std::__next_permutation
2980 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2983 template<typename _BidirectionalIterator
, typename _Compare
>
2984 _GLIBCXX20_CONSTEXPR
2986 __prev_permutation(_BidirectionalIterator __first
,
2987 _BidirectionalIterator __last
, _Compare __comp
)
2989 if (__first
== __last
)
2991 _BidirectionalIterator __i
= __first
;
3000 _BidirectionalIterator __ii
= __i
;
3002 if (__comp(__ii
, __i
))
3004 _BidirectionalIterator __j
= __last
;
3005 while (!__comp(--__j
, __i
))
3007 std::iter_swap(__i
, __j
);
3008 std::__reverse(__ii
, __last
,
3009 std::__iterator_category(__first
));
3014 std::__reverse(__first
, __last
,
3015 std::__iterator_category(__first
));
3022 * @brief Permute range into the previous @e dictionary ordering.
3023 * @ingroup sorting_algorithms
3024 * @param __first Start of range.
3025 * @param __last End of range.
3026 * @return False if wrapped to last permutation, true otherwise.
3028 * Treats all permutations of the range as a set of @e dictionary sorted
3029 * sequences. Permutes the current sequence into the previous one of this
3030 * set. Returns true if there are more sequences to generate. If the
3031 * sequence is the smallest of the set, the largest is generated and false
3034 template<typename _BidirectionalIterator
>
3035 _GLIBCXX20_CONSTEXPR
3037 prev_permutation(_BidirectionalIterator __first
,
3038 _BidirectionalIterator __last
)
3040 // concept requirements
3041 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3042 _BidirectionalIterator
>)
3043 __glibcxx_function_requires(_LessThanComparableConcept
<
3044 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3045 __glibcxx_requires_valid_range(__first
, __last
);
3046 __glibcxx_requires_irreflexive(__first
, __last
);
3048 return std::__prev_permutation(__first
, __last
,
3049 __gnu_cxx::__ops::__iter_less_iter());
3053 * @brief Permute range into the previous @e dictionary ordering using
3054 * comparison functor.
3055 * @ingroup sorting_algorithms
3056 * @param __first Start of range.
3057 * @param __last End of range.
3058 * @param __comp A comparison functor.
3059 * @return False if wrapped to last permutation, true otherwise.
3061 * Treats all permutations of the range [__first,__last) as a set of
3062 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3063 * sequence into the previous one of this set. Returns true if there are
3064 * more sequences to generate. If the sequence is the smallest of the set,
3065 * the largest is generated and false returned.
3067 template<typename _BidirectionalIterator
, typename _Compare
>
3068 _GLIBCXX20_CONSTEXPR
3070 prev_permutation(_BidirectionalIterator __first
,
3071 _BidirectionalIterator __last
, _Compare __comp
)
3073 // concept requirements
3074 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3075 _BidirectionalIterator
>)
3076 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3077 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3078 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3079 __glibcxx_requires_valid_range(__first
, __last
);
3080 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3082 return std::__prev_permutation(__first
, __last
,
3083 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3089 template<typename _InputIterator
, typename _OutputIterator
,
3090 typename _Predicate
, typename _Tp
>
3091 _GLIBCXX20_CONSTEXPR
3093 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3094 _OutputIterator __result
,
3095 _Predicate __pred
, const _Tp
& __new_value
)
3097 for (; __first
!= __last
; ++__first
, (void)++__result
)
3098 if (__pred(__first
))
3099 *__result
= __new_value
;
3101 *__result
= *__first
;
3106 * @brief Copy a sequence, replacing each element of one value with another
3108 * @param __first An input iterator.
3109 * @param __last An input iterator.
3110 * @param __result An output iterator.
3111 * @param __old_value The value to be replaced.
3112 * @param __new_value The replacement value.
3113 * @return The end of the output sequence, @p result+(last-first).
3115 * Copies each element in the input range @p [__first,__last) to the
3116 * output range @p [__result,__result+(__last-__first)) replacing elements
3117 * equal to @p __old_value with @p __new_value.
3119 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3120 _GLIBCXX20_CONSTEXPR
3121 inline _OutputIterator
3122 replace_copy(_InputIterator __first
, _InputIterator __last
,
3123 _OutputIterator __result
,
3124 const _Tp
& __old_value
, const _Tp
& __new_value
)
3126 // concept requirements
3127 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3128 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3129 typename iterator_traits
<_InputIterator
>::value_type
>)
3130 __glibcxx_function_requires(_EqualOpConcept
<
3131 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3132 __glibcxx_requires_valid_range(__first
, __last
);
3134 return std::__replace_copy_if(__first
, __last
, __result
,
3135 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3140 * @brief Copy a sequence, replacing each value for which a predicate
3141 * returns true with another value.
3142 * @ingroup mutating_algorithms
3143 * @param __first An input iterator.
3144 * @param __last An input iterator.
3145 * @param __result An output iterator.
3146 * @param __pred A predicate.
3147 * @param __new_value The replacement value.
3148 * @return The end of the output sequence, @p __result+(__last-__first).
3150 * Copies each element in the range @p [__first,__last) to the range
3151 * @p [__result,__result+(__last-__first)) replacing elements for which
3152 * @p __pred returns true with @p __new_value.
3154 template<typename _InputIterator
, typename _OutputIterator
,
3155 typename _Predicate
, typename _Tp
>
3156 _GLIBCXX20_CONSTEXPR
3157 inline _OutputIterator
3158 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3159 _OutputIterator __result
,
3160 _Predicate __pred
, const _Tp
& __new_value
)
3162 // concept requirements
3163 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3164 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3165 typename iterator_traits
<_InputIterator
>::value_type
>)
3166 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3167 typename iterator_traits
<_InputIterator
>::value_type
>)
3168 __glibcxx_requires_valid_range(__first
, __last
);
3170 return std::__replace_copy_if(__first
, __last
, __result
,
3171 __gnu_cxx::__ops::__pred_iter(__pred
),
3175 #if __cplusplus >= 201103L
3177 * @brief Determines whether the elements of a sequence are sorted.
3178 * @ingroup sorting_algorithms
3179 * @param __first An iterator.
3180 * @param __last Another iterator.
3181 * @return True if the elements are sorted, false otherwise.
3183 template<typename _ForwardIterator
>
3184 _GLIBCXX20_CONSTEXPR
3186 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3187 { return std::is_sorted_until(__first
, __last
) == __last
; }
3190 * @brief Determines whether the elements of a sequence are sorted
3191 * according to a comparison functor.
3192 * @ingroup sorting_algorithms
3193 * @param __first An iterator.
3194 * @param __last Another iterator.
3195 * @param __comp A comparison functor.
3196 * @return True if the elements are sorted, false otherwise.
3198 template<typename _ForwardIterator
, typename _Compare
>
3199 _GLIBCXX20_CONSTEXPR
3201 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3203 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3205 template<typename _ForwardIterator
, typename _Compare
>
3206 _GLIBCXX20_CONSTEXPR
3208 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3211 if (__first
== __last
)
3214 _ForwardIterator __next
= __first
;
3215 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3216 if (__comp(__next
, __first
))
3222 * @brief Determines the end of a sorted sequence.
3223 * @ingroup sorting_algorithms
3224 * @param __first An iterator.
3225 * @param __last Another iterator.
3226 * @return An iterator pointing to the last iterator i in [__first, __last)
3227 * for which the range [__first, i) is sorted.
3229 template<typename _ForwardIterator
>
3230 _GLIBCXX20_CONSTEXPR
3231 inline _ForwardIterator
3232 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3234 // concept requirements
3235 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3236 __glibcxx_function_requires(_LessThanComparableConcept
<
3237 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3238 __glibcxx_requires_valid_range(__first
, __last
);
3239 __glibcxx_requires_irreflexive(__first
, __last
);
3241 return std::__is_sorted_until(__first
, __last
,
3242 __gnu_cxx::__ops::__iter_less_iter());
3246 * @brief Determines the end of a sorted sequence using comparison functor.
3247 * @ingroup sorting_algorithms
3248 * @param __first An iterator.
3249 * @param __last Another iterator.
3250 * @param __comp A comparison functor.
3251 * @return An iterator pointing to the last iterator i in [__first, __last)
3252 * for which the range [__first, i) is sorted.
3254 template<typename _ForwardIterator
, typename _Compare
>
3255 _GLIBCXX20_CONSTEXPR
3256 inline _ForwardIterator
3257 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3260 // concept requirements
3261 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3262 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3263 typename iterator_traits
<_ForwardIterator
>::value_type
,
3264 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3265 __glibcxx_requires_valid_range(__first
, __last
);
3266 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3268 return std::__is_sorted_until(__first
, __last
,
3269 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3273 * @brief Determines min and max at once as an ordered pair.
3274 * @ingroup sorting_algorithms
3275 * @param __a A thing of arbitrary type.
3276 * @param __b Another thing of arbitrary type.
3277 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3280 template<typename _Tp
>
3281 _GLIBCXX14_CONSTEXPR
3282 inline pair
<const _Tp
&, const _Tp
&>
3283 minmax(const _Tp
& __a
, const _Tp
& __b
)
3285 // concept requirements
3286 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3288 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3289 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3293 * @brief Determines min and max at once as an ordered pair.
3294 * @ingroup sorting_algorithms
3295 * @param __a A thing of arbitrary type.
3296 * @param __b Another thing of arbitrary type.
3297 * @param __comp A @link comparison_functors comparison functor @endlink.
3298 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3301 template<typename _Tp
, typename _Compare
>
3302 _GLIBCXX14_CONSTEXPR
3303 inline pair
<const _Tp
&, const _Tp
&>
3304 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3306 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3307 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3310 template<typename _ForwardIterator
, typename _Compare
>
3311 _GLIBCXX14_CONSTEXPR
3312 pair
<_ForwardIterator
, _ForwardIterator
>
3313 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3316 _ForwardIterator __next
= __first
;
3317 if (__first
== __last
3318 || ++__next
== __last
)
3319 return std::make_pair(__first
, __first
);
3321 _ForwardIterator __min
{}, __max
{};
3322 if (__comp(__next
, __first
))
3336 while (__first
!= __last
)
3339 if (++__next
== __last
)
3341 if (__comp(__first
, __min
))
3343 else if (!__comp(__first
, __max
))
3348 if (__comp(__next
, __first
))
3350 if (__comp(__next
, __min
))
3352 if (!__comp(__first
, __max
))
3357 if (__comp(__first
, __min
))
3359 if (!__comp(__next
, __max
))
3367 return std::make_pair(__min
, __max
);
3371 * @brief Return a pair of iterators pointing to the minimum and maximum
3372 * elements in a range.
3373 * @ingroup sorting_algorithms
3374 * @param __first Start of range.
3375 * @param __last End of range.
3376 * @return make_pair(m, M), where m is the first iterator i in
3377 * [__first, __last) such that no other element in the range is
3378 * smaller, and where M is the last iterator i in [__first, __last)
3379 * such that no other element in the range is larger.
3381 template<typename _ForwardIterator
>
3382 _GLIBCXX14_CONSTEXPR
3383 inline pair
<_ForwardIterator
, _ForwardIterator
>
3384 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3386 // concept requirements
3387 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3388 __glibcxx_function_requires(_LessThanComparableConcept
<
3389 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3390 __glibcxx_requires_valid_range(__first
, __last
);
3391 __glibcxx_requires_irreflexive(__first
, __last
);
3393 return std::__minmax_element(__first
, __last
,
3394 __gnu_cxx::__ops::__iter_less_iter());
3398 * @brief Return a pair of iterators pointing to the minimum and maximum
3399 * elements in a range.
3400 * @ingroup sorting_algorithms
3401 * @param __first Start of range.
3402 * @param __last End of range.
3403 * @param __comp Comparison functor.
3404 * @return make_pair(m, M), where m is the first iterator i in
3405 * [__first, __last) such that no other element in the range is
3406 * smaller, and where M is the last iterator i in [__first, __last)
3407 * such that no other element in the range is larger.
3409 template<typename _ForwardIterator
, typename _Compare
>
3410 _GLIBCXX14_CONSTEXPR
3411 inline pair
<_ForwardIterator
, _ForwardIterator
>
3412 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3415 // concept requirements
3416 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3417 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3418 typename iterator_traits
<_ForwardIterator
>::value_type
,
3419 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3420 __glibcxx_requires_valid_range(__first
, __last
);
3421 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3423 return std::__minmax_element(__first
, __last
,
3424 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3427 template<typename _Tp
>
3428 _GLIBCXX14_CONSTEXPR
3429 inline pair
<_Tp
, _Tp
>
3430 minmax(initializer_list
<_Tp
> __l
)
3432 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
3433 pair
<const _Tp
*, const _Tp
*> __p
=
3434 std::__minmax_element(__l
.begin(), __l
.end(),
3435 __gnu_cxx::__ops::__iter_less_iter());
3436 return std::make_pair(*__p
.first
, *__p
.second
);
3439 template<typename _Tp
, typename _Compare
>
3440 _GLIBCXX14_CONSTEXPR
3441 inline pair
<_Tp
, _Tp
>
3442 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3444 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
3445 pair
<const _Tp
*, const _Tp
*> __p
=
3446 std::__minmax_element(__l
.begin(), __l
.end(),
3447 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3448 return std::make_pair(*__p
.first
, *__p
.second
);
3452 * @brief Checks whether a permutation of the second sequence is equal
3453 * to the first sequence.
3454 * @ingroup non_mutating_algorithms
3455 * @param __first1 Start of first range.
3456 * @param __last1 End of first range.
3457 * @param __first2 Start of second range.
3458 * @param __pred A binary predicate.
3459 * @return true if there exists a permutation of the elements in
3460 * the range [__first2, __first2 + (__last1 - __first1)),
3461 * beginning with ForwardIterator2 begin, such that
3462 * equal(__first1, __last1, __begin, __pred) returns true;
3463 * otherwise, returns false.
3465 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3466 typename _BinaryPredicate
>
3467 _GLIBCXX20_CONSTEXPR
3469 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3470 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3472 // concept requirements
3473 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3474 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3475 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3476 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3477 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3478 __glibcxx_requires_valid_range(__first1
, __last1
);
3480 return std::__is_permutation(__first1
, __last1
, __first2
,
3481 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3484 #if __cplusplus > 201103L
3485 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3486 typename _BinaryPredicate
>
3487 _GLIBCXX20_CONSTEXPR
3489 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3490 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3491 _BinaryPredicate __pred
)
3494 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3496 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3497 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3498 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3499 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3502 auto __d1
= std::distance(__first1
, __last1
);
3503 auto __d2
= std::distance(__first2
, __last2
);
3508 // Efficiently compare identical prefixes: O(N) if sequences
3509 // have the same elements in the same order.
3510 for (; __first1
!= __last1
&& __first2
!= __last2
;
3511 ++__first1
, (void)++__first2
)
3512 if (!__pred(__first1
, __first2
))
3517 if (__first1
== __last1
)
3522 auto __d1
= std::distance(__first1
, __last1
);
3523 auto __d2
= std::distance(__first2
, __last2
);
3524 if (__d1
== 0 && __d2
== 0)
3530 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3532 if (__scan
!= std::__find_if(__first1
, __scan
,
3533 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3534 continue; // We've seen this one before.
3536 auto __matches
= std::__count_if(__first2
, __last2
,
3537 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3539 || std::__count_if(__scan
, __last1
,
3540 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3548 * @brief Checks whether a permutaion of the second sequence is equal
3549 * to the first sequence.
3550 * @ingroup non_mutating_algorithms
3551 * @param __first1 Start of first range.
3552 * @param __last1 End of first range.
3553 * @param __first2 Start of second range.
3554 * @param __last2 End of first range.
3555 * @return true if there exists a permutation of the elements in the range
3556 * [__first2, __last2), beginning with ForwardIterator2 begin,
3557 * such that equal(__first1, __last1, begin) returns true;
3558 * otherwise, returns false.
3560 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3561 _GLIBCXX20_CONSTEXPR
3563 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3564 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3566 __glibcxx_requires_valid_range(__first1
, __last1
);
3567 __glibcxx_requires_valid_range(__first2
, __last2
);
3570 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3571 __gnu_cxx::__ops::__iter_equal_to_iter());
3575 * @brief Checks whether a permutation of the second sequence is equal
3576 * to the first sequence.
3577 * @ingroup non_mutating_algorithms
3578 * @param __first1 Start of first range.
3579 * @param __last1 End of first range.
3580 * @param __first2 Start of second range.
3581 * @param __last2 End of first range.
3582 * @param __pred A binary predicate.
3583 * @return true if there exists a permutation of the elements in the range
3584 * [__first2, __last2), beginning with ForwardIterator2 begin,
3585 * such that equal(__first1, __last1, __begin, __pred) returns true;
3586 * otherwise, returns false.
3588 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3589 typename _BinaryPredicate
>
3590 _GLIBCXX20_CONSTEXPR
3592 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3593 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3594 _BinaryPredicate __pred
)
3596 __glibcxx_requires_valid_range(__first1
, __last1
);
3597 __glibcxx_requires_valid_range(__first2
, __last2
);
3599 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3600 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3603 #if __cplusplus >= 201703L
3605 #define __cpp_lib_clamp 201603L
3608 * @brief Returns the value clamped between lo and hi.
3609 * @ingroup sorting_algorithms
3610 * @param __val A value of arbitrary type.
3611 * @param __lo A lower limit of arbitrary type.
3612 * @param __hi An upper limit of arbitrary type.
3613 * @retval `__lo` if `__val < __lo`
3614 * @retval `__hi` if `__hi < __val`
3615 * @retval `__val` otherwise.
3616 * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
3618 template<typename _Tp
>
3619 constexpr const _Tp
&
3620 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3622 __glibcxx_assert(!(__hi
< __lo
));
3623 return std::min(std::max(__val
, __lo
), __hi
);
3627 * @brief Returns the value clamped between lo and hi.
3628 * @ingroup sorting_algorithms
3629 * @param __val A value of arbitrary type.
3630 * @param __lo A lower limit of arbitrary type.
3631 * @param __hi An upper limit of arbitrary type.
3632 * @param __comp A comparison functor.
3633 * @retval `__lo` if `__comp(__val, __lo)`
3634 * @retval `__hi` if `__comp(__hi, __val)`
3635 * @retval `__val` otherwise.
3636 * @pre `__comp(__hi, __lo)` is false.
3638 template<typename _Tp
, typename _Compare
>
3639 constexpr const _Tp
&
3640 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3642 __glibcxx_assert(!__comp(__hi
, __lo
));
3643 return std::min(std::max(__val
, __lo
, __comp
), __hi
, __comp
);
3648 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3650 * @brief Generate two uniformly distributed integers using a
3651 * single distribution invocation.
3652 * @param __b0 The upper bound for the first integer.
3653 * @param __b1 The upper bound for the second integer.
3654 * @param __g A UniformRandomBitGenerator.
3655 * @return A pair (i, j) with i and j uniformly distributed
3656 * over [0, __b0) and [0, __b1), respectively.
3658 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3660 * Using uniform_int_distribution with a range that is very
3661 * small relative to the range of the generator ends up wasting
3662 * potentially expensively generated randomness, since
3663 * uniform_int_distribution does not store leftover randomness
3664 * between invocations.
3666 * If we know we want two integers in ranges that are sufficiently
3667 * small, we can compose the ranges, use a single distribution
3668 * invocation, and significantly reduce the waste.
3670 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3671 pair
<_IntType
, _IntType
>
3672 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3673 _UniformRandomBitGenerator
&& __g
)
3676 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3677 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3681 * @brief Shuffle the elements of a sequence using a uniform random
3683 * @ingroup mutating_algorithms
3684 * @param __first A forward iterator.
3685 * @param __last A forward iterator.
3686 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3689 * Reorders the elements in the range @p [__first,__last) using @p __g to
3690 * provide random numbers.
3692 template<typename _RandomAccessIterator
,
3693 typename _UniformRandomNumberGenerator
>
3695 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3696 _UniformRandomNumberGenerator
&& __g
)
3698 // concept requirements
3699 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3700 _RandomAccessIterator
>)
3701 __glibcxx_requires_valid_range(__first
, __last
);
3703 if (__first
== __last
)
3706 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3709 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3710 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3711 typedef typename
__distr_type::param_type __p_type
;
3713 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3715 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3718 const __uc_type __urngrange
= __g
.max() - __g
.min();
3719 const __uc_type __urange
= __uc_type(__last
- __first
);
3721 if (__urngrange
/ __urange
>= __urange
)
3722 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3724 _RandomAccessIterator __i
= __first
+ 1;
3726 // Since we know the range isn't empty, an even number of elements
3727 // means an uneven number of elements /to swap/, in which case we
3728 // do the first one up front:
3730 if ((__urange
% 2) == 0)
3732 __distr_type __d
{0, 1};
3733 std::iter_swap(__i
++, __first
+ __d(__g
));
3736 // Now we know that __last - __i is even, so we do the rest in pairs,
3737 // using a single distribution invocation to produce swap positions
3738 // for two successive elements at a time:
3740 while (__i
!= __last
)
3742 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3744 const pair
<__uc_type
, __uc_type
> __pospos
=
3745 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3747 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3748 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3756 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3757 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3759 #endif // USE C99_STDINT
3763 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3766 * @brief Apply a function to every element of a sequence.
3767 * @ingroup non_mutating_algorithms
3768 * @param __first An input iterator.
3769 * @param __last An input iterator.
3770 * @param __f A unary function object.
3773 * Applies the function object @p __f to each element in the range
3774 * @p [first,last). @p __f must not modify the order of the sequence.
3775 * If @p __f has a return value it is ignored.
3777 template<typename _InputIterator
, typename _Function
>
3778 _GLIBCXX20_CONSTEXPR
3780 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3782 // concept requirements
3783 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3784 __glibcxx_requires_valid_range(__first
, __last
);
3785 for (; __first
!= __last
; ++__first
)
3787 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3790 #if __cplusplus >= 201703L
3792 * @brief Apply a function to every element of a sequence.
3793 * @ingroup non_mutating_algorithms
3794 * @param __first An input iterator.
3795 * @param __n A value convertible to an integer.
3796 * @param __f A unary function object.
3797 * @return `__first+__n`
3799 * Applies the function object `__f` to each element in the range
3800 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3801 * If `__f` has a return value it is ignored.
3803 template<typename _InputIterator
, typename _Size
, typename _Function
>
3804 _GLIBCXX20_CONSTEXPR
3806 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3808 auto __n2
= std::__size_to_integer(__n
);
3809 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3810 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3814 auto __last
= __first
+ __n2
;
3815 std::for_each(__first
, __last
, std::move(__f
));
3831 * @brief Find the first occurrence of a value in a sequence.
3832 * @ingroup non_mutating_algorithms
3833 * @param __first An input iterator.
3834 * @param __last An input iterator.
3835 * @param __val The value to find.
3836 * @return The first iterator @c i in the range @p [__first,__last)
3837 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3839 template<typename _InputIterator
, typename _Tp
>
3840 _GLIBCXX20_CONSTEXPR
3841 inline _InputIterator
3842 find(_InputIterator __first
, _InputIterator __last
,
3845 // concept requirements
3846 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3847 __glibcxx_function_requires(_EqualOpConcept
<
3848 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3849 __glibcxx_requires_valid_range(__first
, __last
);
3850 return std::__find_if(__first
, __last
,
3851 __gnu_cxx::__ops::__iter_equals_val(__val
));
3855 * @brief Find the first element in a sequence for which a
3856 * predicate is true.
3857 * @ingroup non_mutating_algorithms
3858 * @param __first An input iterator.
3859 * @param __last An input iterator.
3860 * @param __pred A predicate.
3861 * @return The first iterator @c i in the range @p [__first,__last)
3862 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3864 template<typename _InputIterator
, typename _Predicate
>
3865 _GLIBCXX20_CONSTEXPR
3866 inline _InputIterator
3867 find_if(_InputIterator __first
, _InputIterator __last
,
3870 // concept requirements
3871 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3872 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3873 typename iterator_traits
<_InputIterator
>::value_type
>)
3874 __glibcxx_requires_valid_range(__first
, __last
);
3876 return std::__find_if(__first
, __last
,
3877 __gnu_cxx::__ops::__pred_iter(__pred
));
3881 * @brief Find element from a set in a sequence.
3882 * @ingroup non_mutating_algorithms
3883 * @param __first1 Start of range to search.
3884 * @param __last1 End of range to search.
3885 * @param __first2 Start of match candidates.
3886 * @param __last2 End of match candidates.
3887 * @return The first iterator @c i in the range
3888 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3889 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3891 * Searches the range @p [__first1,__last1) for an element that is
3892 * equal to some element in the range [__first2,__last2). If
3893 * found, returns an iterator in the range [__first1,__last1),
3894 * otherwise returns @p __last1.
3896 template<typename _InputIterator
, typename _ForwardIterator
>
3897 _GLIBCXX20_CONSTEXPR
3899 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3900 _ForwardIterator __first2
, _ForwardIterator __last2
)
3902 // concept requirements
3903 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3904 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3905 __glibcxx_function_requires(_EqualOpConcept
<
3906 typename iterator_traits
<_InputIterator
>::value_type
,
3907 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3908 __glibcxx_requires_valid_range(__first1
, __last1
);
3909 __glibcxx_requires_valid_range(__first2
, __last2
);
3911 for (; __first1
!= __last1
; ++__first1
)
3912 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3913 if (*__first1
== *__iter
)
3919 * @brief Find element from a set in a sequence using a predicate.
3920 * @ingroup non_mutating_algorithms
3921 * @param __first1 Start of range to search.
3922 * @param __last1 End of range to search.
3923 * @param __first2 Start of match candidates.
3924 * @param __last2 End of match candidates.
3925 * @param __comp Predicate to use.
3926 * @return The first iterator @c i in the range
3927 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3928 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3929 * such iterator exists.
3932 * Searches the range @p [__first1,__last1) for an element that is
3933 * equal to some element in the range [__first2,__last2). If
3934 * found, returns an iterator in the range [__first1,__last1),
3935 * otherwise returns @p __last1.
3937 template<typename _InputIterator
, typename _ForwardIterator
,
3938 typename _BinaryPredicate
>
3939 _GLIBCXX20_CONSTEXPR
3941 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3942 _ForwardIterator __first2
, _ForwardIterator __last2
,
3943 _BinaryPredicate __comp
)
3945 // concept requirements
3946 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3947 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3948 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3949 typename iterator_traits
<_InputIterator
>::value_type
,
3950 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3951 __glibcxx_requires_valid_range(__first1
, __last1
);
3952 __glibcxx_requires_valid_range(__first2
, __last2
);
3954 for (; __first1
!= __last1
; ++__first1
)
3955 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3956 if (__comp(*__first1
, *__iter
))
3962 * @brief Find two adjacent values in a sequence that are equal.
3963 * @ingroup non_mutating_algorithms
3964 * @param __first A forward iterator.
3965 * @param __last A forward iterator.
3966 * @return The first iterator @c i such that @c i and @c i+1 are both
3967 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3968 * or @p __last if no such iterator exists.
3970 template<typename _ForwardIterator
>
3971 _GLIBCXX20_CONSTEXPR
3972 inline _ForwardIterator
3973 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3975 // concept requirements
3976 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3977 __glibcxx_function_requires(_EqualityComparableConcept
<
3978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3979 __glibcxx_requires_valid_range(__first
, __last
);
3981 return std::__adjacent_find(__first
, __last
,
3982 __gnu_cxx::__ops::__iter_equal_to_iter());
3986 * @brief Find two adjacent values in a sequence using a predicate.
3987 * @ingroup non_mutating_algorithms
3988 * @param __first A forward iterator.
3989 * @param __last A forward iterator.
3990 * @param __binary_pred A binary predicate.
3991 * @return The first iterator @c i such that @c i and @c i+1 are both
3992 * valid iterators in @p [__first,__last) and such that
3993 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3996 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3997 _GLIBCXX20_CONSTEXPR
3998 inline _ForwardIterator
3999 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4000 _BinaryPredicate __binary_pred
)
4002 // concept requirements
4003 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4004 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4005 typename iterator_traits
<_ForwardIterator
>::value_type
,
4006 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4007 __glibcxx_requires_valid_range(__first
, __last
);
4009 return std::__adjacent_find(__first
, __last
,
4010 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4014 * @brief Count the number of copies of a value in a sequence.
4015 * @ingroup non_mutating_algorithms
4016 * @param __first An input iterator.
4017 * @param __last An input iterator.
4018 * @param __value The value to be counted.
4019 * @return The number of iterators @c i in the range @p [__first,__last)
4020 * for which @c *i == @p __value
4022 template<typename _InputIterator
, typename _Tp
>
4023 _GLIBCXX20_CONSTEXPR
4024 inline typename iterator_traits
<_InputIterator
>::difference_type
4025 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4027 // concept requirements
4028 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4029 __glibcxx_function_requires(_EqualOpConcept
<
4030 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4031 __glibcxx_requires_valid_range(__first
, __last
);
4033 return std::__count_if(__first
, __last
,
4034 __gnu_cxx::__ops::__iter_equals_val(__value
));
4038 * @brief Count the elements of a sequence for which a predicate is true.
4039 * @ingroup non_mutating_algorithms
4040 * @param __first An input iterator.
4041 * @param __last An input iterator.
4042 * @param __pred A predicate.
4043 * @return The number of iterators @c i in the range @p [__first,__last)
4044 * for which @p __pred(*i) is true.
4046 template<typename _InputIterator
, typename _Predicate
>
4047 _GLIBCXX20_CONSTEXPR
4048 inline typename iterator_traits
<_InputIterator
>::difference_type
4049 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4051 // concept requirements
4052 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4053 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4054 typename iterator_traits
<_InputIterator
>::value_type
>)
4055 __glibcxx_requires_valid_range(__first
, __last
);
4057 return std::__count_if(__first
, __last
,
4058 __gnu_cxx::__ops::__pred_iter(__pred
));
4062 * @brief Search a sequence for a matching sub-sequence.
4063 * @ingroup non_mutating_algorithms
4064 * @param __first1 A forward iterator.
4065 * @param __last1 A forward iterator.
4066 * @param __first2 A forward iterator.
4067 * @param __last2 A forward iterator.
4068 * @return The first iterator @c i in the range @p
4069 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4070 * *(__first2+N) for each @c N in the range @p
4071 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4073 * Searches the range @p [__first1,__last1) for a sub-sequence that
4074 * compares equal value-by-value with the sequence given by @p
4075 * [__first2,__last2) and returns an iterator to the first element
4076 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4079 * Because the sub-sequence must lie completely within the range @p
4080 * [__first1,__last1) it must start at a position less than @p
4081 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4082 * length of the sub-sequence.
4084 * This means that the returned iterator @c i will be in the range
4085 * @p [__first1,__last1-(__last2-__first2))
4087 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4088 _GLIBCXX20_CONSTEXPR
4089 inline _ForwardIterator1
4090 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4091 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4093 // concept requirements
4094 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4095 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4096 __glibcxx_function_requires(_EqualOpConcept
<
4097 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4098 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4099 __glibcxx_requires_valid_range(__first1
, __last1
);
4100 __glibcxx_requires_valid_range(__first2
, __last2
);
4102 return std::__search(__first1
, __last1
, __first2
, __last2
,
4103 __gnu_cxx::__ops::__iter_equal_to_iter());
4107 * @brief Search a sequence for a matching sub-sequence using a predicate.
4108 * @ingroup non_mutating_algorithms
4109 * @param __first1 A forward iterator.
4110 * @param __last1 A forward iterator.
4111 * @param __first2 A forward iterator.
4112 * @param __last2 A forward iterator.
4113 * @param __predicate A binary predicate.
4114 * @return The first iterator @c i in the range
4115 * @p [__first1,__last1-(__last2-__first2)) such that
4116 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4117 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4119 * Searches the range @p [__first1,__last1) for a sub-sequence that
4120 * compares equal value-by-value with the sequence given by @p
4121 * [__first2,__last2), using @p __predicate to determine equality,
4122 * and returns an iterator to the first element of the
4123 * sub-sequence, or @p __last1 if no such iterator exists.
4125 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4127 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4128 typename _BinaryPredicate
>
4129 _GLIBCXX20_CONSTEXPR
4130 inline _ForwardIterator1
4131 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4132 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4133 _BinaryPredicate __predicate
)
4135 // concept requirements
4136 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4137 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4138 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4139 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4140 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4141 __glibcxx_requires_valid_range(__first1
, __last1
);
4142 __glibcxx_requires_valid_range(__first2
, __last2
);
4144 return std::__search(__first1
, __last1
, __first2
, __last2
,
4145 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4149 * @brief Search a sequence for a number of consecutive values.
4150 * @ingroup non_mutating_algorithms
4151 * @param __first A forward iterator.
4152 * @param __last A forward iterator.
4153 * @param __count The number of consecutive values.
4154 * @param __val The value to find.
4155 * @return The first iterator @c i in the range @p
4156 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4157 * each @c N in the range @p [0,__count), or @p __last if no such
4160 * Searches the range @p [__first,__last) for @p count consecutive elements
4161 * equal to @p __val.
4163 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4164 _GLIBCXX20_CONSTEXPR
4165 inline _ForwardIterator
4166 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4167 _Integer __count
, const _Tp
& __val
)
4169 // concept requirements
4170 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4171 __glibcxx_function_requires(_EqualOpConcept
<
4172 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4173 __glibcxx_requires_valid_range(__first
, __last
);
4175 return std::__search_n(__first
, __last
, __count
,
4176 __gnu_cxx::__ops::__iter_equals_val(__val
));
4181 * @brief Search a sequence for a number of consecutive values using a
4183 * @ingroup non_mutating_algorithms
4184 * @param __first A forward iterator.
4185 * @param __last A forward iterator.
4186 * @param __count The number of consecutive values.
4187 * @param __val The value to find.
4188 * @param __binary_pred A binary predicate.
4189 * @return The first iterator @c i in the range @p
4190 * [__first,__last-__count) such that @p
4191 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4192 * @p [0,__count), or @p __last if no such iterator exists.
4194 * Searches the range @p [__first,__last) for @p __count
4195 * consecutive elements for which the predicate returns true.
4197 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4198 typename _BinaryPredicate
>
4199 _GLIBCXX20_CONSTEXPR
4200 inline _ForwardIterator
4201 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4202 _Integer __count
, const _Tp
& __val
,
4203 _BinaryPredicate __binary_pred
)
4205 // concept requirements
4206 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4207 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4208 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4209 __glibcxx_requires_valid_range(__first
, __last
);
4211 return std::__search_n(__first
, __last
, __count
,
4212 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4215 #if __cplusplus >= 201703L
4216 /** @brief Search a sequence using a Searcher object.
4218 * @param __first A forward iterator.
4219 * @param __last A forward iterator.
4220 * @param __searcher A callable object.
4221 * @return @p __searcher(__first,__last).first
4223 template<typename _ForwardIterator
, typename _Searcher
>
4224 _GLIBCXX20_CONSTEXPR
4225 inline _ForwardIterator
4226 search(_ForwardIterator __first
, _ForwardIterator __last
,
4227 const _Searcher
& __searcher
)
4228 { return __searcher(__first
, __last
).first
; }
4232 * @brief Perform an operation on a sequence.
4233 * @ingroup mutating_algorithms
4234 * @param __first An input iterator.
4235 * @param __last An input iterator.
4236 * @param __result An output iterator.
4237 * @param __unary_op A unary operator.
4238 * @return An output iterator equal to @p __result+(__last-__first).
4240 * Applies the operator to each element in the input range and assigns
4241 * the results to successive elements of the output sequence.
4242 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4243 * range @p [0,__last-__first).
4245 * @p unary_op must not alter its argument.
4247 template<typename _InputIterator
, typename _OutputIterator
,
4248 typename _UnaryOperation
>
4249 _GLIBCXX20_CONSTEXPR
4251 transform(_InputIterator __first
, _InputIterator __last
,
4252 _OutputIterator __result
, _UnaryOperation __unary_op
)
4254 // concept requirements
4255 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4256 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4257 // "the type returned by a _UnaryOperation"
4258 __typeof__(__unary_op(*__first
))>)
4259 __glibcxx_requires_valid_range(__first
, __last
);
4261 for (; __first
!= __last
; ++__first
, (void)++__result
)
4262 *__result
= __unary_op(*__first
);
4267 * @brief Perform an operation on corresponding elements of two sequences.
4268 * @ingroup mutating_algorithms
4269 * @param __first1 An input iterator.
4270 * @param __last1 An input iterator.
4271 * @param __first2 An input iterator.
4272 * @param __result An output iterator.
4273 * @param __binary_op A binary operator.
4274 * @return An output iterator equal to @p result+(last-first).
4276 * Applies the operator to the corresponding elements in the two
4277 * input ranges and assigns the results to successive elements of the
4280 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4281 * @c N in the range @p [0,__last1-__first1).
4283 * @p binary_op must not alter either of its arguments.
4285 template<typename _InputIterator1
, typename _InputIterator2
,
4286 typename _OutputIterator
, typename _BinaryOperation
>
4287 _GLIBCXX20_CONSTEXPR
4289 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4290 _InputIterator2 __first2
, _OutputIterator __result
,
4291 _BinaryOperation __binary_op
)
4293 // concept requirements
4294 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4295 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4296 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4297 // "the type returned by a _BinaryOperation"
4298 __typeof__(__binary_op(*__first1
,*__first2
))>)
4299 __glibcxx_requires_valid_range(__first1
, __last1
);
4301 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4302 *__result
= __binary_op(*__first1
, *__first2
);
4307 * @brief Replace each occurrence of one value in a sequence with another
4309 * @ingroup mutating_algorithms
4310 * @param __first A forward iterator.
4311 * @param __last A forward iterator.
4312 * @param __old_value The value to be replaced.
4313 * @param __new_value The replacement value.
4314 * @return replace() returns no value.
4316 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4317 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4319 template<typename _ForwardIterator
, typename _Tp
>
4320 _GLIBCXX20_CONSTEXPR
4322 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4323 const _Tp
& __old_value
, const _Tp
& __new_value
)
4325 // concept requirements
4326 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4328 __glibcxx_function_requires(_EqualOpConcept
<
4329 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4330 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4331 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4332 __glibcxx_requires_valid_range(__first
, __last
);
4334 for (; __first
!= __last
; ++__first
)
4335 if (*__first
== __old_value
)
4336 *__first
= __new_value
;
4340 * @brief Replace each value in a sequence for which a predicate returns
4341 * true with another value.
4342 * @ingroup mutating_algorithms
4343 * @param __first A forward iterator.
4344 * @param __last A forward iterator.
4345 * @param __pred A predicate.
4346 * @param __new_value The replacement value.
4347 * @return replace_if() returns no value.
4349 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4350 * is true then the assignment @c *i = @p __new_value is performed.
4352 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4353 _GLIBCXX20_CONSTEXPR
4355 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4356 _Predicate __pred
, const _Tp
& __new_value
)
4358 // concept requirements
4359 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4361 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4362 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4363 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4364 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4365 __glibcxx_requires_valid_range(__first
, __last
);
4367 for (; __first
!= __last
; ++__first
)
4368 if (__pred(*__first
))
4369 *__first
= __new_value
;
4373 * @brief Assign the result of a function object to each value in a
4375 * @ingroup mutating_algorithms
4376 * @param __first A forward iterator.
4377 * @param __last A forward iterator.
4378 * @param __gen A function object taking no arguments and returning
4379 * std::iterator_traits<_ForwardIterator>::value_type
4380 * @return generate() returns no value.
4382 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4383 * @p [__first,__last).
4385 template<typename _ForwardIterator
, typename _Generator
>
4386 _GLIBCXX20_CONSTEXPR
4388 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4391 // concept requirements
4392 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4393 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4394 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4395 __glibcxx_requires_valid_range(__first
, __last
);
4397 for (; __first
!= __last
; ++__first
)
4402 * @brief Assign the result of a function object to each value in a
4404 * @ingroup mutating_algorithms
4405 * @param __first A forward iterator.
4406 * @param __n The length of the sequence.
4407 * @param __gen A function object taking no arguments and returning
4408 * std::iterator_traits<_ForwardIterator>::value_type
4409 * @return The end of the sequence, @p __first+__n
4411 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4412 * @p [__first,__first+__n).
4414 * If @p __n is negative, the function does nothing and returns @p __first.
4416 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4417 // DR 865. More algorithms that throw away information
4418 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4419 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4420 _GLIBCXX20_CONSTEXPR
4422 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4424 // concept requirements
4425 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4426 // "the type returned by a _Generator"
4427 __typeof__(__gen())>)
4429 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4430 for (_IntSize __niter
= std::__size_to_integer(__n
);
4431 __niter
> 0; --__niter
, (void) ++__first
)
4437 * @brief Copy a sequence, removing consecutive duplicate values.
4438 * @ingroup mutating_algorithms
4439 * @param __first An input iterator.
4440 * @param __last An input iterator.
4441 * @param __result An output iterator.
4442 * @return An iterator designating the end of the resulting sequence.
4444 * Copies each element in the range @p [__first,__last) to the range
4445 * beginning at @p __result, except that only the first element is copied
4446 * from groups of consecutive elements that compare equal.
4447 * unique_copy() is stable, so the relative order of elements that are
4448 * copied is unchanged.
4450 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4451 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4453 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4454 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4457 template<typename _InputIterator
, typename _OutputIterator
>
4458 _GLIBCXX20_CONSTEXPR
4459 inline _OutputIterator
4460 unique_copy(_InputIterator __first
, _InputIterator __last
,
4461 _OutputIterator __result
)
4463 // concept requirements
4464 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4465 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4466 typename iterator_traits
<_InputIterator
>::value_type
>)
4467 __glibcxx_function_requires(_EqualityComparableConcept
<
4468 typename iterator_traits
<_InputIterator
>::value_type
>)
4469 __glibcxx_requires_valid_range(__first
, __last
);
4471 if (__first
== __last
)
4473 return std::__unique_copy(__first
, __last
, __result
,
4474 __gnu_cxx::__ops::__iter_equal_to_iter(),
4475 std::__iterator_category(__first
),
4476 std::__iterator_category(__result
));
4480 * @brief Copy a sequence, removing consecutive values using a predicate.
4481 * @ingroup mutating_algorithms
4482 * @param __first An input iterator.
4483 * @param __last An input iterator.
4484 * @param __result An output iterator.
4485 * @param __binary_pred A binary predicate.
4486 * @return An iterator designating the end of the resulting sequence.
4488 * Copies each element in the range @p [__first,__last) to the range
4489 * beginning at @p __result, except that only the first element is copied
4490 * from groups of consecutive elements for which @p __binary_pred returns
4492 * unique_copy() is stable, so the relative order of elements that are
4493 * copied is unchanged.
4495 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4496 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4498 template<typename _InputIterator
, typename _OutputIterator
,
4499 typename _BinaryPredicate
>
4500 _GLIBCXX20_CONSTEXPR
4501 inline _OutputIterator
4502 unique_copy(_InputIterator __first
, _InputIterator __last
,
4503 _OutputIterator __result
,
4504 _BinaryPredicate __binary_pred
)
4506 // concept requirements -- predicates checked later
4507 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4508 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4509 typename iterator_traits
<_InputIterator
>::value_type
>)
4510 __glibcxx_requires_valid_range(__first
, __last
);
4512 if (__first
== __last
)
4514 return std::__unique_copy(__first
, __last
, __result
,
4515 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4516 std::__iterator_category(__first
),
4517 std::__iterator_category(__result
));
4520 #if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4523 * @brief Randomly shuffle the elements of a sequence.
4524 * @ingroup mutating_algorithms
4525 * @param __first A forward iterator.
4526 * @param __last A forward iterator.
4529 * Reorder the elements in the range @p [__first,__last) using a random
4530 * distribution, so that every possible ordering of the sequence is
4534 * Since C++14 `std::random_shuffle` is not part of the C++ standard.
4535 * Use `std::shuffle` instead, which was introduced in C++11.
4537 template<typename _RandomAccessIterator
>
4538 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4540 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4542 // concept requirements
4543 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4544 _RandomAccessIterator
>)
4545 __glibcxx_requires_valid_range(__first
, __last
);
4547 if (__first
!= __last
)
4548 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4550 // XXX rand() % N is not uniformly distributed
4551 _RandomAccessIterator __j
= __first
4552 + std::rand() % ((__i
- __first
) + 1);
4554 std::iter_swap(__i
, __j
);
4560 * @brief Shuffle the elements of a sequence using a random number
4562 * @ingroup mutating_algorithms
4563 * @param __first A forward iterator.
4564 * @param __last A forward iterator.
4565 * @param __rand The RNG functor or function.
4568 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4569 * provide a random distribution. Calling @p __rand(N) for a positive
4570 * integer @p N should return a randomly chosen integer from the
4574 * Since C++14 `std::random_shuffle` is not part of the C++ standard.
4575 * Use `std::shuffle` instead, which was introduced in C++11.
4577 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4578 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4580 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4581 #if __cplusplus >= 201103L
4582 _RandomNumberGenerator
&& __rand
)
4584 _RandomNumberGenerator
& __rand
)
4587 // concept requirements
4588 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4589 _RandomAccessIterator
>)
4590 __glibcxx_requires_valid_range(__first
, __last
);
4592 if (__first
== __last
)
4594 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4596 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4598 std::iter_swap(__i
, __j
);
4601 #endif // C++11 || USE_DEPRECATED
4604 * @brief Move elements for which a predicate is true to the beginning
4606 * @ingroup mutating_algorithms
4607 * @param __first A forward iterator.
4608 * @param __last A forward iterator.
4609 * @param __pred A predicate functor.
4610 * @return An iterator @p middle such that @p __pred(i) is true for each
4611 * iterator @p i in the range @p [__first,middle) and false for each @p i
4612 * in the range @p [middle,__last).
4614 * @p __pred must not modify its operand. @p partition() does not preserve
4615 * the relative ordering of elements in each group, use
4616 * @p stable_partition() if this is needed.
4618 template<typename _ForwardIterator
, typename _Predicate
>
4619 _GLIBCXX20_CONSTEXPR
4620 inline _ForwardIterator
4621 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4624 // concept requirements
4625 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4627 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4628 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4629 __glibcxx_requires_valid_range(__first
, __last
);
4631 return std::__partition(__first
, __last
, __pred
,
4632 std::__iterator_category(__first
));
4637 * @brief Sort the smallest elements of a sequence.
4638 * @ingroup sorting_algorithms
4639 * @param __first An iterator.
4640 * @param __middle Another iterator.
4641 * @param __last Another iterator.
4644 * Sorts the smallest @p (__middle-__first) elements in the range
4645 * @p [first,last) and moves them to the range @p [__first,__middle). The
4646 * order of the remaining elements in the range @p [__middle,__last) is
4648 * After the sort if @e i and @e j are iterators in the range
4649 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4650 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4652 template<typename _RandomAccessIterator
>
4653 _GLIBCXX20_CONSTEXPR
4655 partial_sort(_RandomAccessIterator __first
,
4656 _RandomAccessIterator __middle
,
4657 _RandomAccessIterator __last
)
4659 // concept requirements
4660 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4661 _RandomAccessIterator
>)
4662 __glibcxx_function_requires(_LessThanComparableConcept
<
4663 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4664 __glibcxx_requires_valid_range(__first
, __middle
);
4665 __glibcxx_requires_valid_range(__middle
, __last
);
4666 __glibcxx_requires_irreflexive(__first
, __last
);
4668 std::__partial_sort(__first
, __middle
, __last
,
4669 __gnu_cxx::__ops::__iter_less_iter());
4673 * @brief Sort the smallest elements of a sequence using a predicate
4675 * @ingroup sorting_algorithms
4676 * @param __first An iterator.
4677 * @param __middle Another iterator.
4678 * @param __last Another iterator.
4679 * @param __comp A comparison functor.
4682 * Sorts the smallest @p (__middle-__first) elements in the range
4683 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4684 * order of the remaining elements in the range @p [__middle,__last) is
4686 * After the sort if @e i and @e j are iterators in the range
4687 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4688 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4691 template<typename _RandomAccessIterator
, typename _Compare
>
4692 _GLIBCXX20_CONSTEXPR
4694 partial_sort(_RandomAccessIterator __first
,
4695 _RandomAccessIterator __middle
,
4696 _RandomAccessIterator __last
,
4699 // concept requirements
4700 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4701 _RandomAccessIterator
>)
4702 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4703 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4704 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4705 __glibcxx_requires_valid_range(__first
, __middle
);
4706 __glibcxx_requires_valid_range(__middle
, __last
);
4707 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4709 std::__partial_sort(__first
, __middle
, __last
,
4710 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4714 * @brief Sort a sequence just enough to find a particular position.
4715 * @ingroup sorting_algorithms
4716 * @param __first An iterator.
4717 * @param __nth Another iterator.
4718 * @param __last Another iterator.
4721 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4722 * is the same element that would have been in that position had the
4723 * whole sequence been sorted. The elements either side of @p *__nth are
4724 * not completely sorted, but for any iterator @e i in the range
4725 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4726 * holds that *j < *i is false.
4728 template<typename _RandomAccessIterator
>
4729 _GLIBCXX20_CONSTEXPR
4731 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4732 _RandomAccessIterator __last
)
4734 // concept requirements
4735 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4736 _RandomAccessIterator
>)
4737 __glibcxx_function_requires(_LessThanComparableConcept
<
4738 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4739 __glibcxx_requires_valid_range(__first
, __nth
);
4740 __glibcxx_requires_valid_range(__nth
, __last
);
4741 __glibcxx_requires_irreflexive(__first
, __last
);
4743 if (__first
== __last
|| __nth
== __last
)
4746 std::__introselect(__first
, __nth
, __last
,
4747 std::__lg(__last
- __first
) * 2,
4748 __gnu_cxx::__ops::__iter_less_iter());
4752 * @brief Sort a sequence just enough to find a particular position
4753 * using a predicate for comparison.
4754 * @ingroup sorting_algorithms
4755 * @param __first An iterator.
4756 * @param __nth Another iterator.
4757 * @param __last Another iterator.
4758 * @param __comp A comparison functor.
4761 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4762 * is the same element that would have been in that position had the
4763 * whole sequence been sorted. The elements either side of @p *__nth are
4764 * not completely sorted, but for any iterator @e i in the range
4765 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4766 * holds that @p __comp(*j,*i) is false.
4768 template<typename _RandomAccessIterator
, typename _Compare
>
4769 _GLIBCXX20_CONSTEXPR
4771 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4772 _RandomAccessIterator __last
, _Compare __comp
)
4774 // concept requirements
4775 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4776 _RandomAccessIterator
>)
4777 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4778 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4779 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4780 __glibcxx_requires_valid_range(__first
, __nth
);
4781 __glibcxx_requires_valid_range(__nth
, __last
);
4782 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4784 if (__first
== __last
|| __nth
== __last
)
4787 std::__introselect(__first
, __nth
, __last
,
4788 std::__lg(__last
- __first
) * 2,
4789 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4793 * @brief Sort the elements of a sequence.
4794 * @ingroup sorting_algorithms
4795 * @param __first An iterator.
4796 * @param __last Another iterator.
4799 * Sorts the elements in the range @p [__first,__last) in ascending order,
4800 * such that for each iterator @e i in the range @p [__first,__last-1),
4801 * *(i+1)<*i is false.
4803 * The relative ordering of equivalent elements is not preserved, use
4804 * @p stable_sort() if this is needed.
4806 template<typename _RandomAccessIterator
>
4807 _GLIBCXX20_CONSTEXPR
4809 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4811 // concept requirements
4812 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4813 _RandomAccessIterator
>)
4814 __glibcxx_function_requires(_LessThanComparableConcept
<
4815 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4816 __glibcxx_requires_valid_range(__first
, __last
);
4817 __glibcxx_requires_irreflexive(__first
, __last
);
4819 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4823 * @brief Sort the elements of a sequence using a predicate for comparison.
4824 * @ingroup sorting_algorithms
4825 * @param __first An iterator.
4826 * @param __last Another iterator.
4827 * @param __comp A comparison functor.
4830 * Sorts the elements in the range @p [__first,__last) in ascending order,
4831 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4832 * range @p [__first,__last-1).
4834 * The relative ordering of equivalent elements is not preserved, use
4835 * @p stable_sort() if this is needed.
4837 template<typename _RandomAccessIterator
, typename _Compare
>
4838 _GLIBCXX20_CONSTEXPR
4840 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4843 // concept requirements
4844 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4845 _RandomAccessIterator
>)
4846 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4847 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4848 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4849 __glibcxx_requires_valid_range(__first
, __last
);
4850 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4852 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4855 template<typename _InputIterator1
, typename _InputIterator2
,
4856 typename _OutputIterator
, typename _Compare
>
4857 _GLIBCXX20_CONSTEXPR
4859 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4860 _InputIterator2 __first2
, _InputIterator2 __last2
,
4861 _OutputIterator __result
, _Compare __comp
)
4863 while (__first1
!= __last1
&& __first2
!= __last2
)
4865 if (__comp(__first2
, __first1
))
4867 *__result
= *__first2
;
4872 *__result
= *__first1
;
4877 return std::copy(__first2
, __last2
,
4878 std::copy(__first1
, __last1
, __result
));
4882 * @brief Merges two sorted ranges.
4883 * @ingroup sorting_algorithms
4884 * @param __first1 An iterator.
4885 * @param __first2 Another iterator.
4886 * @param __last1 Another iterator.
4887 * @param __last2 Another iterator.
4888 * @param __result An iterator pointing to the end of the merged range.
4889 * @return An output iterator equal to @p __result + (__last1 - __first1)
4890 * + (__last2 - __first2).
4892 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4893 * the sorted range @p [__result, __result + (__last1-__first1) +
4894 * (__last2-__first2)). Both input ranges must be sorted, and the
4895 * output range must not overlap with either of the input ranges.
4896 * The sort is @e stable, that is, for equivalent elements in the
4897 * two ranges, elements from the first range will always come
4898 * before elements from the second.
4900 template<typename _InputIterator1
, typename _InputIterator2
,
4901 typename _OutputIterator
>
4902 _GLIBCXX20_CONSTEXPR
4903 inline _OutputIterator
4904 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4905 _InputIterator2 __first2
, _InputIterator2 __last2
,
4906 _OutputIterator __result
)
4908 // concept requirements
4909 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4910 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4911 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4912 typename iterator_traits
<_InputIterator1
>::value_type
>)
4913 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4914 typename iterator_traits
<_InputIterator2
>::value_type
>)
4915 __glibcxx_function_requires(_LessThanOpConcept
<
4916 typename iterator_traits
<_InputIterator2
>::value_type
,
4917 typename iterator_traits
<_InputIterator1
>::value_type
>)
4918 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4919 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4920 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4921 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4923 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4924 __first2
, __last2
, __result
,
4925 __gnu_cxx::__ops::__iter_less_iter());
4929 * @brief Merges two sorted ranges.
4930 * @ingroup sorting_algorithms
4931 * @param __first1 An iterator.
4932 * @param __first2 Another iterator.
4933 * @param __last1 Another iterator.
4934 * @param __last2 Another iterator.
4935 * @param __result An iterator pointing to the end of the merged range.
4936 * @param __comp A functor to use for comparisons.
4937 * @return An output iterator equal to @p __result + (__last1 - __first1)
4938 * + (__last2 - __first2).
4940 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4941 * the sorted range @p [__result, __result + (__last1-__first1) +
4942 * (__last2-__first2)). Both input ranges must be sorted, and the
4943 * output range must not overlap with either of the input ranges.
4944 * The sort is @e stable, that is, for equivalent elements in the
4945 * two ranges, elements from the first range will always come
4946 * before elements from the second.
4948 * The comparison function should have the same effects on ordering as
4949 * the function used for the initial sort.
4951 template<typename _InputIterator1
, typename _InputIterator2
,
4952 typename _OutputIterator
, typename _Compare
>
4953 _GLIBCXX20_CONSTEXPR
4954 inline _OutputIterator
4955 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4956 _InputIterator2 __first2
, _InputIterator2 __last2
,
4957 _OutputIterator __result
, _Compare __comp
)
4959 // concept requirements
4960 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4961 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4962 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4963 typename iterator_traits
<_InputIterator1
>::value_type
>)
4964 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4965 typename iterator_traits
<_InputIterator2
>::value_type
>)
4966 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4967 typename iterator_traits
<_InputIterator2
>::value_type
,
4968 typename iterator_traits
<_InputIterator1
>::value_type
>)
4969 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4970 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4971 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
4972 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
4974 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4975 __first2
, __last2
, __result
,
4976 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4979 template<typename _RandomAccessIterator
, typename _Compare
>
4981 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4984 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4986 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4988 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
4990 if (__first
== __last
)
4993 // __stable_sort_adaptive sorts the range in two halves,
4994 // so the buffer only needs to fit half the range at once.
4995 _TmpBuf
__buf(__first
, (__last
- __first
+ 1) / 2);
4997 if (__buf
.begin() == 0)
4998 std::__inplace_stable_sort(__first
, __last
, __comp
);
5000 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5001 _DistanceType(__buf
.size()), __comp
);
5005 * @brief Sort the elements of a sequence, preserving the relative order
5006 * of equivalent elements.
5007 * @ingroup sorting_algorithms
5008 * @param __first An iterator.
5009 * @param __last Another iterator.
5012 * Sorts the elements in the range @p [__first,__last) in ascending order,
5013 * such that for each iterator @p i in the range @p [__first,__last-1),
5014 * @p *(i+1)<*i is false.
5016 * The relative ordering of equivalent elements is preserved, so any two
5017 * elements @p x and @p y in the range @p [__first,__last) such that
5018 * @p x<y is false and @p y<x is false will have the same relative
5019 * ordering after calling @p stable_sort().
5021 template<typename _RandomAccessIterator
>
5023 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5025 // concept requirements
5026 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5027 _RandomAccessIterator
>)
5028 __glibcxx_function_requires(_LessThanComparableConcept
<
5029 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5030 __glibcxx_requires_valid_range(__first
, __last
);
5031 __glibcxx_requires_irreflexive(__first
, __last
);
5033 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5034 __gnu_cxx::__ops::__iter_less_iter());
5038 * @brief Sort the elements of a sequence using a predicate for comparison,
5039 * preserving the relative order of equivalent elements.
5040 * @ingroup sorting_algorithms
5041 * @param __first An iterator.
5042 * @param __last Another iterator.
5043 * @param __comp A comparison functor.
5046 * Sorts the elements in the range @p [__first,__last) in ascending order,
5047 * such that for each iterator @p i in the range @p [__first,__last-1),
5048 * @p __comp(*(i+1),*i) is false.
5050 * The relative ordering of equivalent elements is preserved, so any two
5051 * elements @p x and @p y in the range @p [__first,__last) such that
5052 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5053 * relative ordering after calling @p stable_sort().
5055 template<typename _RandomAccessIterator
, typename _Compare
>
5057 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5060 // concept requirements
5061 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5062 _RandomAccessIterator
>)
5063 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5064 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5065 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5066 __glibcxx_requires_valid_range(__first
, __last
);
5067 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5069 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5070 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5073 template<typename _InputIterator1
, typename _InputIterator2
,
5074 typename _OutputIterator
,
5076 _GLIBCXX20_CONSTEXPR
5078 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5079 _InputIterator2 __first2
, _InputIterator2 __last2
,
5080 _OutputIterator __result
, _Compare __comp
)
5082 while (__first1
!= __last1
&& __first2
!= __last2
)
5084 if (__comp(__first1
, __first2
))
5086 *__result
= *__first1
;
5089 else if (__comp(__first2
, __first1
))
5091 *__result
= *__first2
;
5096 *__result
= *__first1
;
5102 return std::copy(__first2
, __last2
,
5103 std::copy(__first1
, __last1
, __result
));
5107 * @brief Return the union of two sorted ranges.
5108 * @ingroup set_algorithms
5109 * @param __first1 Start of first range.
5110 * @param __last1 End of first range.
5111 * @param __first2 Start of second range.
5112 * @param __last2 End of second range.
5113 * @param __result Start of output range.
5114 * @return End of the output range.
5115 * @ingroup set_algorithms
5117 * This operation iterates over both ranges, copying elements present in
5118 * each range in order to the output range. Iterators increment for each
5119 * range. When the current element of one range is less than the other,
5120 * that element is copied and the iterator advanced. If an element is
5121 * contained in both ranges, the element from the first range is copied and
5122 * both ranges advance. The output range may not overlap either input
5125 template<typename _InputIterator1
, typename _InputIterator2
,
5126 typename _OutputIterator
>
5127 _GLIBCXX20_CONSTEXPR
5128 inline _OutputIterator
5129 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5130 _InputIterator2 __first2
, _InputIterator2 __last2
,
5131 _OutputIterator __result
)
5133 // concept requirements
5134 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5135 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5136 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5137 typename iterator_traits
<_InputIterator1
>::value_type
>)
5138 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5139 typename iterator_traits
<_InputIterator2
>::value_type
>)
5140 __glibcxx_function_requires(_LessThanOpConcept
<
5141 typename iterator_traits
<_InputIterator1
>::value_type
,
5142 typename iterator_traits
<_InputIterator2
>::value_type
>)
5143 __glibcxx_function_requires(_LessThanOpConcept
<
5144 typename iterator_traits
<_InputIterator2
>::value_type
,
5145 typename iterator_traits
<_InputIterator1
>::value_type
>)
5146 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5147 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5148 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5149 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5151 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5152 __first2
, __last2
, __result
,
5153 __gnu_cxx::__ops::__iter_less_iter());
5157 * @brief Return the union of two sorted ranges using a comparison functor.
5158 * @ingroup set_algorithms
5159 * @param __first1 Start of first range.
5160 * @param __last1 End of first range.
5161 * @param __first2 Start of second range.
5162 * @param __last2 End of second range.
5163 * @param __result Start of output range.
5164 * @param __comp The comparison functor.
5165 * @return End of the output range.
5166 * @ingroup set_algorithms
5168 * This operation iterates over both ranges, copying elements present in
5169 * each range in order to the output range. Iterators increment for each
5170 * range. When the current element of one range is less than the other
5171 * according to @p __comp, that element is copied and the iterator advanced.
5172 * If an equivalent element according to @p __comp is contained in both
5173 * ranges, the element from the first range is copied and both ranges
5174 * advance. The output range may not overlap either input range.
5176 template<typename _InputIterator1
, typename _InputIterator2
,
5177 typename _OutputIterator
, typename _Compare
>
5178 _GLIBCXX20_CONSTEXPR
5179 inline _OutputIterator
5180 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5181 _InputIterator2 __first2
, _InputIterator2 __last2
,
5182 _OutputIterator __result
, _Compare __comp
)
5184 // concept requirements
5185 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5186 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5187 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5188 typename iterator_traits
<_InputIterator1
>::value_type
>)
5189 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5190 typename iterator_traits
<_InputIterator2
>::value_type
>)
5191 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5192 typename iterator_traits
<_InputIterator1
>::value_type
,
5193 typename iterator_traits
<_InputIterator2
>::value_type
>)
5194 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5195 typename iterator_traits
<_InputIterator2
>::value_type
,
5196 typename iterator_traits
<_InputIterator1
>::value_type
>)
5197 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5198 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5199 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5200 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5202 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5203 __first2
, __last2
, __result
,
5204 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5207 template<typename _InputIterator1
, typename _InputIterator2
,
5208 typename _OutputIterator
,
5210 _GLIBCXX20_CONSTEXPR
5212 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5213 _InputIterator2 __first2
, _InputIterator2 __last2
,
5214 _OutputIterator __result
, _Compare __comp
)
5216 while (__first1
!= __last1
&& __first2
!= __last2
)
5217 if (__comp(__first1
, __first2
))
5219 else if (__comp(__first2
, __first1
))
5223 *__result
= *__first1
;
5232 * @brief Return the intersection of two sorted ranges.
5233 * @ingroup set_algorithms
5234 * @param __first1 Start of first range.
5235 * @param __last1 End of first range.
5236 * @param __first2 Start of second range.
5237 * @param __last2 End of second range.
5238 * @param __result Start of output range.
5239 * @return End of the output range.
5240 * @ingroup set_algorithms
5242 * This operation iterates over both ranges, copying elements present in
5243 * both ranges in order to the output range. Iterators increment for each
5244 * range. When the current element of one range is less than the other,
5245 * that iterator advances. If an element is contained in both ranges, the
5246 * element from the first range is copied and both ranges advance. The
5247 * output range may not overlap either input range.
5249 template<typename _InputIterator1
, typename _InputIterator2
,
5250 typename _OutputIterator
>
5251 _GLIBCXX20_CONSTEXPR
5252 inline _OutputIterator
5253 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5254 _InputIterator2 __first2
, _InputIterator2 __last2
,
5255 _OutputIterator __result
)
5257 // concept requirements
5258 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5259 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5260 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5261 typename iterator_traits
<_InputIterator1
>::value_type
>)
5262 __glibcxx_function_requires(_LessThanOpConcept
<
5263 typename iterator_traits
<_InputIterator1
>::value_type
,
5264 typename iterator_traits
<_InputIterator2
>::value_type
>)
5265 __glibcxx_function_requires(_LessThanOpConcept
<
5266 typename iterator_traits
<_InputIterator2
>::value_type
,
5267 typename iterator_traits
<_InputIterator1
>::value_type
>)
5268 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5269 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5270 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5271 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5273 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5274 __first2
, __last2
, __result
,
5275 __gnu_cxx::__ops::__iter_less_iter());
5279 * @brief Return the intersection of two sorted ranges using comparison
5281 * @ingroup set_algorithms
5282 * @param __first1 Start of first range.
5283 * @param __last1 End of first range.
5284 * @param __first2 Start of second range.
5285 * @param __last2 End of second range.
5286 * @param __result Start of output range.
5287 * @param __comp The comparison functor.
5288 * @return End of the output range.
5289 * @ingroup set_algorithms
5291 * This operation iterates over both ranges, copying elements present in
5292 * both ranges in order to the output range. Iterators increment for each
5293 * range. When the current element of one range is less than the other
5294 * according to @p __comp, that iterator advances. If an element is
5295 * contained in both ranges according to @p __comp, the element from the
5296 * first range is copied and both ranges advance. The output range may not
5297 * overlap either input range.
5299 template<typename _InputIterator1
, typename _InputIterator2
,
5300 typename _OutputIterator
, typename _Compare
>
5301 _GLIBCXX20_CONSTEXPR
5302 inline _OutputIterator
5303 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5304 _InputIterator2 __first2
, _InputIterator2 __last2
,
5305 _OutputIterator __result
, _Compare __comp
)
5307 // concept requirements
5308 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5309 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5310 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5311 typename iterator_traits
<_InputIterator1
>::value_type
>)
5312 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5313 typename iterator_traits
<_InputIterator1
>::value_type
,
5314 typename iterator_traits
<_InputIterator2
>::value_type
>)
5315 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5316 typename iterator_traits
<_InputIterator2
>::value_type
,
5317 typename iterator_traits
<_InputIterator1
>::value_type
>)
5318 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5319 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5320 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5321 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5323 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5324 __first2
, __last2
, __result
,
5325 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5328 template<typename _InputIterator1
, typename _InputIterator2
,
5329 typename _OutputIterator
,
5331 _GLIBCXX20_CONSTEXPR
5333 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5334 _InputIterator2 __first2
, _InputIterator2 __last2
,
5335 _OutputIterator __result
, _Compare __comp
)
5337 while (__first1
!= __last1
&& __first2
!= __last2
)
5338 if (__comp(__first1
, __first2
))
5340 *__result
= *__first1
;
5344 else if (__comp(__first2
, __first1
))
5351 return std::copy(__first1
, __last1
, __result
);
5355 * @brief Return the difference of two sorted ranges.
5356 * @ingroup set_algorithms
5357 * @param __first1 Start of first range.
5358 * @param __last1 End of first range.
5359 * @param __first2 Start of second range.
5360 * @param __last2 End of second range.
5361 * @param __result Start of output range.
5362 * @return End of the output range.
5363 * @ingroup set_algorithms
5365 * This operation iterates over both ranges, copying elements present in
5366 * the first range but not the second in order to the output range.
5367 * Iterators increment for each range. When the current element of the
5368 * first range is less than the second, that element is copied and the
5369 * iterator advances. If the current element of the second range is less,
5370 * the iterator advances, but no element is copied. If an element is
5371 * contained in both ranges, no elements are copied and both ranges
5372 * advance. The output range may not overlap either input range.
5374 template<typename _InputIterator1
, typename _InputIterator2
,
5375 typename _OutputIterator
>
5376 _GLIBCXX20_CONSTEXPR
5377 inline _OutputIterator
5378 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5379 _InputIterator2 __first2
, _InputIterator2 __last2
,
5380 _OutputIterator __result
)
5382 // concept requirements
5383 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5384 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5385 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5386 typename iterator_traits
<_InputIterator1
>::value_type
>)
5387 __glibcxx_function_requires(_LessThanOpConcept
<
5388 typename iterator_traits
<_InputIterator1
>::value_type
,
5389 typename iterator_traits
<_InputIterator2
>::value_type
>)
5390 __glibcxx_function_requires(_LessThanOpConcept
<
5391 typename iterator_traits
<_InputIterator2
>::value_type
,
5392 typename iterator_traits
<_InputIterator1
>::value_type
>)
5393 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5394 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5395 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5396 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5398 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5399 __first2
, __last2
, __result
,
5400 __gnu_cxx::__ops::__iter_less_iter());
5404 * @brief Return the difference of two sorted ranges using comparison
5406 * @ingroup set_algorithms
5407 * @param __first1 Start of first range.
5408 * @param __last1 End of first range.
5409 * @param __first2 Start of second range.
5410 * @param __last2 End of second range.
5411 * @param __result Start of output range.
5412 * @param __comp The comparison functor.
5413 * @return End of the output range.
5414 * @ingroup set_algorithms
5416 * This operation iterates over both ranges, copying elements present in
5417 * the first range but not the second in order to the output range.
5418 * Iterators increment for each range. When the current element of the
5419 * first range is less than the second according to @p __comp, that element
5420 * is copied and the iterator advances. If the current element of the
5421 * second range is less, no element is copied and the iterator advances.
5422 * If an element is contained in both ranges according to @p __comp, no
5423 * elements are copied and both ranges advance. The output range may not
5424 * overlap either input range.
5426 template<typename _InputIterator1
, typename _InputIterator2
,
5427 typename _OutputIterator
, typename _Compare
>
5428 _GLIBCXX20_CONSTEXPR
5429 inline _OutputIterator
5430 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5431 _InputIterator2 __first2
, _InputIterator2 __last2
,
5432 _OutputIterator __result
, _Compare __comp
)
5434 // concept requirements
5435 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5437 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5438 typename iterator_traits
<_InputIterator1
>::value_type
>)
5439 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5440 typename iterator_traits
<_InputIterator1
>::value_type
,
5441 typename iterator_traits
<_InputIterator2
>::value_type
>)
5442 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5443 typename iterator_traits
<_InputIterator2
>::value_type
,
5444 typename iterator_traits
<_InputIterator1
>::value_type
>)
5445 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5446 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5447 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5448 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5450 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5451 __first2
, __last2
, __result
,
5452 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5455 template<typename _InputIterator1
, typename _InputIterator2
,
5456 typename _OutputIterator
,
5458 _GLIBCXX20_CONSTEXPR
5460 __set_symmetric_difference(_InputIterator1 __first1
,
5461 _InputIterator1 __last1
,
5462 _InputIterator2 __first2
,
5463 _InputIterator2 __last2
,
5464 _OutputIterator __result
,
5467 while (__first1
!= __last1
&& __first2
!= __last2
)
5468 if (__comp(__first1
, __first2
))
5470 *__result
= *__first1
;
5474 else if (__comp(__first2
, __first1
))
5476 *__result
= *__first2
;
5485 return std::copy(__first2
, __last2
,
5486 std::copy(__first1
, __last1
, __result
));
5490 * @brief Return the symmetric difference of two sorted ranges.
5491 * @ingroup set_algorithms
5492 * @param __first1 Start of first range.
5493 * @param __last1 End of first range.
5494 * @param __first2 Start of second range.
5495 * @param __last2 End of second range.
5496 * @param __result Start of output range.
5497 * @return End of the output range.
5498 * @ingroup set_algorithms
5500 * This operation iterates over both ranges, copying elements present in
5501 * one range but not the other in order to the output range. Iterators
5502 * increment for each range. When the current element of one range is less
5503 * than the other, that element is copied and the iterator advances. If an
5504 * element is contained in both ranges, no elements are copied and both
5505 * ranges advance. The output range may not overlap either input range.
5507 template<typename _InputIterator1
, typename _InputIterator2
,
5508 typename _OutputIterator
>
5509 _GLIBCXX20_CONSTEXPR
5510 inline _OutputIterator
5511 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5512 _InputIterator2 __first2
, _InputIterator2 __last2
,
5513 _OutputIterator __result
)
5515 // concept requirements
5516 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5517 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5518 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5519 typename iterator_traits
<_InputIterator1
>::value_type
>)
5520 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5521 typename iterator_traits
<_InputIterator2
>::value_type
>)
5522 __glibcxx_function_requires(_LessThanOpConcept
<
5523 typename iterator_traits
<_InputIterator1
>::value_type
,
5524 typename iterator_traits
<_InputIterator2
>::value_type
>)
5525 __glibcxx_function_requires(_LessThanOpConcept
<
5526 typename iterator_traits
<_InputIterator2
>::value_type
,
5527 typename iterator_traits
<_InputIterator1
>::value_type
>)
5528 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5529 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5530 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5531 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5533 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5534 __first2
, __last2
, __result
,
5535 __gnu_cxx::__ops::__iter_less_iter());
5539 * @brief Return the symmetric difference of two sorted ranges using
5540 * comparison functor.
5541 * @ingroup set_algorithms
5542 * @param __first1 Start of first range.
5543 * @param __last1 End of first range.
5544 * @param __first2 Start of second range.
5545 * @param __last2 End of second range.
5546 * @param __result Start of output range.
5547 * @param __comp The comparison functor.
5548 * @return End of the output range.
5549 * @ingroup set_algorithms
5551 * This operation iterates over both ranges, copying elements present in
5552 * one range but not the other in order to the output range. Iterators
5553 * increment for each range. When the current element of one range is less
5554 * than the other according to @p comp, that element is copied and the
5555 * iterator advances. If an element is contained in both ranges according
5556 * to @p __comp, no elements are copied and both ranges advance. The output
5557 * range may not overlap either input range.
5559 template<typename _InputIterator1
, typename _InputIterator2
,
5560 typename _OutputIterator
, typename _Compare
>
5561 _GLIBCXX20_CONSTEXPR
5562 inline _OutputIterator
5563 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5564 _InputIterator2 __first2
, _InputIterator2 __last2
,
5565 _OutputIterator __result
,
5568 // concept requirements
5569 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5570 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5571 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5572 typename iterator_traits
<_InputIterator1
>::value_type
>)
5573 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5574 typename iterator_traits
<_InputIterator2
>::value_type
>)
5575 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5576 typename iterator_traits
<_InputIterator1
>::value_type
,
5577 typename iterator_traits
<_InputIterator2
>::value_type
>)
5578 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5579 typename iterator_traits
<_InputIterator2
>::value_type
,
5580 typename iterator_traits
<_InputIterator1
>::value_type
>)
5581 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5582 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5583 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5584 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5586 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5587 __first2
, __last2
, __result
,
5588 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5591 template<typename _ForwardIterator
, typename _Compare
>
5592 _GLIBCXX14_CONSTEXPR
5594 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5597 if (__first
== __last
)
5599 _ForwardIterator __result
= __first
;
5600 while (++__first
!= __last
)
5601 if (__comp(__first
, __result
))
5607 * @brief Return the minimum element in a range.
5608 * @ingroup sorting_algorithms
5609 * @param __first Start of range.
5610 * @param __last End of range.
5611 * @return Iterator referencing the first instance of the smallest value.
5613 template<typename _ForwardIterator
>
5614 _GLIBCXX14_CONSTEXPR
5616 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5618 // concept requirements
5619 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5620 __glibcxx_function_requires(_LessThanComparableConcept
<
5621 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5622 __glibcxx_requires_valid_range(__first
, __last
);
5623 __glibcxx_requires_irreflexive(__first
, __last
);
5625 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5626 __gnu_cxx::__ops::__iter_less_iter());
5630 * @brief Return the minimum element in a range using comparison functor.
5631 * @ingroup sorting_algorithms
5632 * @param __first Start of range.
5633 * @param __last End of range.
5634 * @param __comp Comparison functor.
5635 * @return Iterator referencing the first instance of the smallest value
5636 * according to __comp.
5638 template<typename _ForwardIterator
, typename _Compare
>
5639 _GLIBCXX14_CONSTEXPR
5640 inline _ForwardIterator
5641 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5644 // concept requirements
5645 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5646 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5647 typename iterator_traits
<_ForwardIterator
>::value_type
,
5648 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5649 __glibcxx_requires_valid_range(__first
, __last
);
5650 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5652 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5653 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5656 template<typename _ForwardIterator
, typename _Compare
>
5657 _GLIBCXX14_CONSTEXPR
5659 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5662 if (__first
== __last
) return __first
;
5663 _ForwardIterator __result
= __first
;
5664 while (++__first
!= __last
)
5665 if (__comp(__result
, __first
))
5671 * @brief Return the maximum element in a range.
5672 * @ingroup sorting_algorithms
5673 * @param __first Start of range.
5674 * @param __last End of range.
5675 * @return Iterator referencing the first instance of the largest value.
5677 template<typename _ForwardIterator
>
5678 _GLIBCXX14_CONSTEXPR
5679 inline _ForwardIterator
5680 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5682 // concept requirements
5683 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5684 __glibcxx_function_requires(_LessThanComparableConcept
<
5685 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5686 __glibcxx_requires_valid_range(__first
, __last
);
5687 __glibcxx_requires_irreflexive(__first
, __last
);
5689 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5690 __gnu_cxx::__ops::__iter_less_iter());
5694 * @brief Return the maximum element in a range using comparison functor.
5695 * @ingroup sorting_algorithms
5696 * @param __first Start of range.
5697 * @param __last End of range.
5698 * @param __comp Comparison functor.
5699 * @return Iterator referencing the first instance of the largest value
5700 * according to __comp.
5702 template<typename _ForwardIterator
, typename _Compare
>
5703 _GLIBCXX14_CONSTEXPR
5704 inline _ForwardIterator
5705 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5708 // concept requirements
5709 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5710 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5711 typename iterator_traits
<_ForwardIterator
>::value_type
,
5712 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5713 __glibcxx_requires_valid_range(__first
, __last
);
5714 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5716 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5717 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5720 #if __cplusplus >= 201103L
5722 template<typename _Tp
>
5723 _GLIBCXX14_CONSTEXPR
5725 min(initializer_list
<_Tp
> __l
)
5727 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5728 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5729 __gnu_cxx::__ops::__iter_less_iter());
5732 template<typename _Tp
, typename _Compare
>
5733 _GLIBCXX14_CONSTEXPR
5735 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
5737 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5738 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5739 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5742 template<typename _Tp
>
5743 _GLIBCXX14_CONSTEXPR
5745 max(initializer_list
<_Tp
> __l
)
5747 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5748 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5749 __gnu_cxx::__ops::__iter_less_iter());
5752 template<typename _Tp
, typename _Compare
>
5753 _GLIBCXX14_CONSTEXPR
5755 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
5757 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5758 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5759 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5763 #if __cplusplus >= 201402L
5764 /// Reservoir sampling algorithm.
5765 template<typename _InputIterator
, typename _RandomAccessIterator
,
5766 typename _Size
, typename _UniformRandomBitGenerator
>
5767 _RandomAccessIterator
5768 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5769 _RandomAccessIterator __out
, random_access_iterator_tag
,
5770 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5772 using __distrib_type
= uniform_int_distribution
<_Size
>;
5773 using __param_type
= typename
__distrib_type::param_type
;
5774 __distrib_type __d
{};
5775 _Size __sample_sz
= 0;
5776 while (__first
!= __last
&& __sample_sz
!= __n
)
5778 __out
[__sample_sz
++] = *__first
;
5781 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5782 ++__first
, (void) ++__pop_sz
)
5784 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5786 __out
[__k
] = *__first
;
5788 return __out
+ __sample_sz
;
5791 /// Selection sampling algorithm.
5792 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5793 typename _Size
, typename _UniformRandomBitGenerator
>
5795 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5796 forward_iterator_tag
,
5797 _OutputIterator __out
, _Cat
,
5798 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5800 using __distrib_type
= uniform_int_distribution
<_Size
>;
5801 using __param_type
= typename
__distrib_type::param_type
;
5802 using _USize
= make_unsigned_t
<_Size
>;
5803 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5804 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5806 if (__first
== __last
)
5809 __distrib_type __d
{};
5810 _Size __unsampled_sz
= std::distance(__first
, __last
);
5811 __n
= std::min(__n
, __unsampled_sz
);
5813 // If possible, we use __gen_two_uniform_ints to efficiently produce
5814 // two random numbers using a single distribution invocation:
5816 const __uc_type __urngrange
= __g
.max() - __g
.min();
5817 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5818 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5821 while (__n
!= 0 && __unsampled_sz
>= 2)
5823 const pair
<_Size
, _Size
> __p
=
5824 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5827 if (__p
.first
< __n
)
5829 *__out
++ = *__first
;
5835 if (__n
== 0) break;
5838 if (__p
.second
< __n
)
5840 *__out
++ = *__first
;
5848 // The loop above is otherwise equivalent to this one-at-a-time version:
5850 for (; __n
!= 0; ++__first
)
5851 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5853 *__out
++ = *__first
;
5859 #if __cplusplus > 201402L
5860 #define __cpp_lib_sample 201603L
5861 /// Take a random sample from a population.
5862 template<typename _PopulationIterator
, typename _SampleIterator
,
5863 typename _Distance
, typename _UniformRandomBitGenerator
>
5865 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5866 _SampleIterator __out
, _Distance __n
,
5867 _UniformRandomBitGenerator
&& __g
)
5869 using __pop_cat
= typename
5870 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5871 using __samp_cat
= typename
5872 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5875 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5876 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5877 "output range must use a RandomAccessIterator when input range"
5878 " does not meet the ForwardIterator requirements");
5880 static_assert(is_integral
<_Distance
>::value
,
5881 "sample size must be an integer type");
5883 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5884 return _GLIBCXX_STD_A::
5885 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5886 std::forward
<_UniformRandomBitGenerator
>(__g
));
5891 _GLIBCXX_END_NAMESPACE_ALGO
5892 _GLIBCXX_END_NAMESPACE_VERSION
5895 #endif /* _STL_ALGO_H */