1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2021 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
);
813 template<typename _ForwardIterator
, typename _Predicate
>
816 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
819 __first
= std::__find_if(__first
, __last
, __pred
);
820 if (__first
== __last
)
822 _ForwardIterator __result
= __first
;
824 for (; __first
!= __last
; ++__first
)
825 if (!__pred(__first
))
827 *__result
= _GLIBCXX_MOVE(*__first
);
834 * @brief Remove elements from a sequence.
835 * @ingroup mutating_algorithms
836 * @param __first An input iterator.
837 * @param __last An input iterator.
838 * @param __value The value to be removed.
839 * @return An iterator designating the end of the resulting sequence.
841 * All elements equal to @p __value are removed from the range
842 * @p [__first,__last).
844 * remove() is stable, so the relative order of elements that are
845 * not removed is unchanged.
847 * Elements between the end of the resulting sequence and @p __last
848 * are still present, but their value is unspecified.
850 template<typename _ForwardIterator
, typename _Tp
>
852 inline _ForwardIterator
853 remove(_ForwardIterator __first
, _ForwardIterator __last
,
856 // concept requirements
857 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
859 __glibcxx_function_requires(_EqualOpConcept
<
860 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
861 __glibcxx_requires_valid_range(__first
, __last
);
863 return std::__remove_if(__first
, __last
,
864 __gnu_cxx::__ops::__iter_equals_val(__value
));
868 * @brief Remove elements from a sequence using a predicate.
869 * @ingroup mutating_algorithms
870 * @param __first A forward iterator.
871 * @param __last A forward iterator.
872 * @param __pred A predicate.
873 * @return An iterator designating the end of the resulting sequence.
875 * All elements for which @p __pred returns true are removed from the range
876 * @p [__first,__last).
878 * remove_if() is stable, so the relative order of elements that are
879 * not removed is unchanged.
881 * Elements between the end of the resulting sequence and @p __last
882 * are still present, but their value is unspecified.
884 template<typename _ForwardIterator
, typename _Predicate
>
886 inline _ForwardIterator
887 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
890 // concept requirements
891 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
893 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
894 typename iterator_traits
<_ForwardIterator
>::value_type
>)
895 __glibcxx_requires_valid_range(__first
, __last
);
897 return std::__remove_if(__first
, __last
,
898 __gnu_cxx::__ops::__pred_iter(__pred
));
901 template<typename _ForwardIterator
, typename _BinaryPredicate
>
904 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
905 _BinaryPredicate __binary_pred
)
907 if (__first
== __last
)
909 _ForwardIterator __next
= __first
;
910 while (++__next
!= __last
)
912 if (__binary_pred(__first
, __next
))
919 template<typename _ForwardIterator
, typename _BinaryPredicate
>
922 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
923 _BinaryPredicate __binary_pred
)
925 // Skip the beginning, if already unique.
926 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
927 if (__first
== __last
)
930 // Do the real copy work.
931 _ForwardIterator __dest
= __first
;
933 while (++__first
!= __last
)
934 if (!__binary_pred(__dest
, __first
))
935 *++__dest
= _GLIBCXX_MOVE(*__first
);
940 * @brief Remove consecutive duplicate values from a sequence.
941 * @ingroup mutating_algorithms
942 * @param __first A forward iterator.
943 * @param __last A forward iterator.
944 * @return An iterator designating the end of the resulting sequence.
946 * Removes all but the first element from each group of consecutive
947 * values that compare equal.
948 * unique() is stable, so the relative order of elements that are
949 * not removed is unchanged.
950 * Elements between the end of the resulting sequence and @p __last
951 * are still present, but their value is unspecified.
953 template<typename _ForwardIterator
>
955 inline _ForwardIterator
956 unique(_ForwardIterator __first
, _ForwardIterator __last
)
958 // concept requirements
959 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
961 __glibcxx_function_requires(_EqualityComparableConcept
<
962 typename iterator_traits
<_ForwardIterator
>::value_type
>)
963 __glibcxx_requires_valid_range(__first
, __last
);
965 return std::__unique(__first
, __last
,
966 __gnu_cxx::__ops::__iter_equal_to_iter());
970 * @brief Remove consecutive values from a sequence using a predicate.
971 * @ingroup mutating_algorithms
972 * @param __first A forward iterator.
973 * @param __last A forward iterator.
974 * @param __binary_pred A binary predicate.
975 * @return An iterator designating the end of the resulting sequence.
977 * Removes all but the first element from each group of consecutive
978 * values for which @p __binary_pred returns true.
979 * unique() is stable, so the relative order of elements that are
980 * not removed is unchanged.
981 * Elements between the end of the resulting sequence and @p __last
982 * are still present, but their value is unspecified.
984 template<typename _ForwardIterator
, typename _BinaryPredicate
>
986 inline _ForwardIterator
987 unique(_ForwardIterator __first
, _ForwardIterator __last
,
988 _BinaryPredicate __binary_pred
)
990 // concept requirements
991 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
993 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
994 typename iterator_traits
<_ForwardIterator
>::value_type
,
995 typename iterator_traits
<_ForwardIterator
>::value_type
>)
996 __glibcxx_requires_valid_range(__first
, __last
);
998 return std::__unique(__first
, __last
,
999 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1003 * This is an uglified
1004 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1006 * overloaded for forward iterators and output iterator as result.
1008 template<typename _ForwardIterator
, typename _OutputIterator
,
1009 typename _BinaryPredicate
>
1010 _GLIBCXX20_CONSTEXPR
1012 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1013 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1014 forward_iterator_tag
, output_iterator_tag
)
1016 // concept requirements -- iterators already checked
1017 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1018 typename iterator_traits
<_ForwardIterator
>::value_type
,
1019 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1021 _ForwardIterator __next
= __first
;
1022 *__result
= *__first
;
1023 while (++__next
!= __last
)
1024 if (!__binary_pred(__first
, __next
))
1027 *++__result
= *__first
;
1033 * This is an uglified
1034 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1036 * overloaded for input iterators and output iterator as result.
1038 template<typename _InputIterator
, typename _OutputIterator
,
1039 typename _BinaryPredicate
>
1040 _GLIBCXX20_CONSTEXPR
1042 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1043 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1044 input_iterator_tag
, output_iterator_tag
)
1046 // concept requirements -- iterators already checked
1047 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1048 typename iterator_traits
<_InputIterator
>::value_type
,
1049 typename iterator_traits
<_InputIterator
>::value_type
>)
1051 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1052 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1054 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1055 *__result
= __value
;
1056 while (++__first
!= __last
)
1057 if (!__rebound_pred(__first
, __value
))
1060 *++__result
= __value
;
1066 * This is an uglified
1067 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1069 * overloaded for input iterators and forward iterator as result.
1071 template<typename _InputIterator
, typename _ForwardIterator
,
1072 typename _BinaryPredicate
>
1073 _GLIBCXX20_CONSTEXPR
1075 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1076 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1077 input_iterator_tag
, forward_iterator_tag
)
1079 // concept requirements -- iterators already checked
1080 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1081 typename iterator_traits
<_ForwardIterator
>::value_type
,
1082 typename iterator_traits
<_InputIterator
>::value_type
>)
1083 *__result
= *__first
;
1084 while (++__first
!= __last
)
1085 if (!__binary_pred(__result
, __first
))
1086 *++__result
= *__first
;
1091 * This is an uglified reverse(_BidirectionalIterator,
1092 * _BidirectionalIterator)
1093 * overloaded for bidirectional iterators.
1095 template<typename _BidirectionalIterator
>
1096 _GLIBCXX20_CONSTEXPR
1098 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1099 bidirectional_iterator_tag
)
1102 if (__first
== __last
|| __first
== --__last
)
1106 std::iter_swap(__first
, __last
);
1112 * This is an uglified reverse(_BidirectionalIterator,
1113 * _BidirectionalIterator)
1114 * overloaded for random access iterators.
1116 template<typename _RandomAccessIterator
>
1117 _GLIBCXX20_CONSTEXPR
1119 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1120 random_access_iterator_tag
)
1122 if (__first
== __last
)
1125 while (__first
< __last
)
1127 std::iter_swap(__first
, __last
);
1134 * @brief Reverse a sequence.
1135 * @ingroup mutating_algorithms
1136 * @param __first A bidirectional iterator.
1137 * @param __last A bidirectional iterator.
1138 * @return reverse() returns no value.
1140 * Reverses the order of the elements in the range @p [__first,__last),
1141 * so that the first element becomes the last etc.
1142 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1143 * swaps @p *(__first+i) and @p *(__last-(i+1))
1145 template<typename _BidirectionalIterator
>
1146 _GLIBCXX20_CONSTEXPR
1148 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1150 // concept requirements
1151 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1152 _BidirectionalIterator
>)
1153 __glibcxx_requires_valid_range(__first
, __last
);
1154 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1158 * @brief Copy a sequence, reversing its elements.
1159 * @ingroup mutating_algorithms
1160 * @param __first A bidirectional iterator.
1161 * @param __last A bidirectional iterator.
1162 * @param __result An output iterator.
1163 * @return An iterator designating the end of the resulting sequence.
1165 * Copies the elements in the range @p [__first,__last) to the
1166 * range @p [__result,__result+(__last-__first)) such that the
1167 * order of the elements is reversed. For every @c i such that @p
1168 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1169 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1170 * The ranges @p [__first,__last) and @p
1171 * [__result,__result+(__last-__first)) must not overlap.
1173 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1174 _GLIBCXX20_CONSTEXPR
1176 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1177 _OutputIterator __result
)
1179 // concept requirements
1180 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1181 _BidirectionalIterator
>)
1182 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1183 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1184 __glibcxx_requires_valid_range(__first
, __last
);
1186 while (__first
!= __last
)
1189 *__result
= *__last
;
1196 * This is a helper function for the rotate algorithm specialized on RAIs.
1197 * It returns the greatest common divisor of two integer values.
1199 template<typename _EuclideanRingElement
>
1200 _GLIBCXX20_CONSTEXPR
1201 _EuclideanRingElement
1202 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1206 _EuclideanRingElement __t
= __m
% __n
;
1213 inline namespace _V2
1216 /// This is a helper function for the rotate algorithm.
1217 template<typename _ForwardIterator
>
1218 _GLIBCXX20_CONSTEXPR
1220 __rotate(_ForwardIterator __first
,
1221 _ForwardIterator __middle
,
1222 _ForwardIterator __last
,
1223 forward_iterator_tag
)
1225 if (__first
== __middle
)
1227 else if (__last
== __middle
)
1230 _ForwardIterator __first2
= __middle
;
1233 std::iter_swap(__first
, __first2
);
1236 if (__first
== __middle
)
1237 __middle
= __first2
;
1239 while (__first2
!= __last
);
1241 _ForwardIterator __ret
= __first
;
1243 __first2
= __middle
;
1245 while (__first2
!= __last
)
1247 std::iter_swap(__first
, __first2
);
1250 if (__first
== __middle
)
1251 __middle
= __first2
;
1252 else if (__first2
== __last
)
1253 __first2
= __middle
;
1258 /// This is a helper function for the rotate algorithm.
1259 template<typename _BidirectionalIterator
>
1260 _GLIBCXX20_CONSTEXPR
1261 _BidirectionalIterator
1262 __rotate(_BidirectionalIterator __first
,
1263 _BidirectionalIterator __middle
,
1264 _BidirectionalIterator __last
,
1265 bidirectional_iterator_tag
)
1267 // concept requirements
1268 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1269 _BidirectionalIterator
>)
1271 if (__first
== __middle
)
1273 else if (__last
== __middle
)
1276 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1277 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1279 while (__first
!= __middle
&& __middle
!= __last
)
1281 std::iter_swap(__first
, --__last
);
1285 if (__first
== __middle
)
1287 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1292 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1297 /// This is a helper function for the rotate algorithm.
1298 template<typename _RandomAccessIterator
>
1299 _GLIBCXX20_CONSTEXPR
1300 _RandomAccessIterator
1301 __rotate(_RandomAccessIterator __first
,
1302 _RandomAccessIterator __middle
,
1303 _RandomAccessIterator __last
,
1304 random_access_iterator_tag
)
1306 // concept requirements
1307 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1308 _RandomAccessIterator
>)
1310 if (__first
== __middle
)
1312 else if (__last
== __middle
)
1315 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1317 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1320 _Distance __n
= __last
- __first
;
1321 _Distance __k
= __middle
- __first
;
1323 if (__k
== __n
- __k
)
1325 std::swap_ranges(__first
, __middle
, __middle
);
1329 _RandomAccessIterator __p
= __first
;
1330 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1334 if (__k
< __n
- __k
)
1336 if (__is_pod(_ValueType
) && __k
== 1)
1338 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1339 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1340 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1343 _RandomAccessIterator __q
= __p
+ __k
;
1344 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1346 std::iter_swap(__p
, __q
);
1353 std::swap(__n
, __k
);
1359 if (__is_pod(_ValueType
) && __k
== 1)
1361 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1362 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1363 *__p
= _GLIBCXX_MOVE(__t
);
1366 _RandomAccessIterator __q
= __p
+ __n
;
1368 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1372 std::iter_swap(__p
, __q
);
1377 std::swap(__n
, __k
);
1382 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1383 // DR 488. rotate throws away useful information
1385 * @brief Rotate the elements of a sequence.
1386 * @ingroup mutating_algorithms
1387 * @param __first A forward iterator.
1388 * @param __middle A forward iterator.
1389 * @param __last A forward iterator.
1390 * @return first + (last - middle).
1392 * Rotates the elements of the range @p [__first,__last) by
1393 * @p (__middle - __first) positions so that the element at @p __middle
1394 * is moved to @p __first, the element at @p __middle+1 is moved to
1395 * @p __first+1 and so on for each element in the range
1396 * @p [__first,__last).
1398 * This effectively swaps the ranges @p [__first,__middle) and
1399 * @p [__middle,__last).
1402 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1403 * for each @p n in the range @p [0,__last-__first).
1405 template<typename _ForwardIterator
>
1406 _GLIBCXX20_CONSTEXPR
1407 inline _ForwardIterator
1408 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1409 _ForwardIterator __last
)
1411 // concept requirements
1412 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1414 __glibcxx_requires_valid_range(__first
, __middle
);
1415 __glibcxx_requires_valid_range(__middle
, __last
);
1417 return std::__rotate(__first
, __middle
, __last
,
1418 std::__iterator_category(__first
));
1424 * @brief Copy a sequence, rotating its elements.
1425 * @ingroup mutating_algorithms
1426 * @param __first A forward iterator.
1427 * @param __middle A forward iterator.
1428 * @param __last A forward iterator.
1429 * @param __result An output iterator.
1430 * @return An iterator designating the end of the resulting sequence.
1432 * Copies the elements of the range @p [__first,__last) to the
1433 * range beginning at @result, rotating the copied elements by
1434 * @p (__middle-__first) positions so that the element at @p __middle
1435 * is moved to @p __result, the element at @p __middle+1 is moved
1436 * to @p __result+1 and so on for each element in the range @p
1440 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1441 * for each @p n in the range @p [0,__last-__first).
1443 template<typename _ForwardIterator
, typename _OutputIterator
>
1444 _GLIBCXX20_CONSTEXPR
1445 inline _OutputIterator
1446 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1447 _ForwardIterator __last
, _OutputIterator __result
)
1449 // concept requirements
1450 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1451 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1452 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1453 __glibcxx_requires_valid_range(__first
, __middle
);
1454 __glibcxx_requires_valid_range(__middle
, __last
);
1456 return std::copy(__first
, __middle
,
1457 std::copy(__middle
, __last
, __result
));
1460 /// This is a helper function...
1461 template<typename _ForwardIterator
, typename _Predicate
>
1462 _GLIBCXX20_CONSTEXPR
1464 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1465 _Predicate __pred
, forward_iterator_tag
)
1467 if (__first
== __last
)
1470 while (__pred(*__first
))
1471 if (++__first
== __last
)
1474 _ForwardIterator __next
= __first
;
1476 while (++__next
!= __last
)
1477 if (__pred(*__next
))
1479 std::iter_swap(__first
, __next
);
1486 /// This is a helper function...
1487 template<typename _BidirectionalIterator
, typename _Predicate
>
1488 _GLIBCXX20_CONSTEXPR
1489 _BidirectionalIterator
1490 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1491 _Predicate __pred
, bidirectional_iterator_tag
)
1496 if (__first
== __last
)
1498 else if (__pred(*__first
))
1504 if (__first
== __last
)
1506 else if (!bool(__pred(*__last
)))
1510 std::iter_swap(__first
, __last
);
1517 /// This is a helper function...
1518 /// Requires __first != __last and !__pred(__first)
1519 /// and __len == distance(__first, __last).
1521 /// !__pred(__first) allows us to guarantee that we don't
1522 /// move-assign an element onto itself.
1523 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1526 __stable_partition_adaptive(_ForwardIterator __first
,
1527 _ForwardIterator __last
,
1528 _Predicate __pred
, _Distance __len
,
1530 _Distance __buffer_size
)
1535 if (__len
<= __buffer_size
)
1537 _ForwardIterator __result1
= __first
;
1538 _Pointer __result2
= __buffer
;
1540 // The precondition guarantees that !__pred(__first), so
1541 // move that element to the buffer before starting the loop.
1542 // This ensures that we only call __pred once per element.
1543 *__result2
= _GLIBCXX_MOVE(*__first
);
1546 for (; __first
!= __last
; ++__first
)
1547 if (__pred(__first
))
1549 *__result1
= _GLIBCXX_MOVE(*__first
);
1554 *__result2
= _GLIBCXX_MOVE(*__first
);
1558 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1562 _ForwardIterator __middle
= __first
;
1563 std::advance(__middle
, __len
/ 2);
1564 _ForwardIterator __left_split
=
1565 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1566 __len
/ 2, __buffer
,
1569 // Advance past true-predicate values to satisfy this
1570 // function's preconditions.
1571 _Distance __right_len
= __len
- __len
/ 2;
1572 _ForwardIterator __right_split
=
1573 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1577 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1579 __buffer
, __buffer_size
);
1581 return std::rotate(__left_split
, __middle
, __right_split
);
1584 template<typename _ForwardIterator
, typename _Predicate
>
1586 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1589 __first
= std::__find_if_not(__first
, __last
, __pred
);
1591 if (__first
== __last
)
1594 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1596 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1599 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1600 __buf(__first
, std::distance(__first
, __last
));
1602 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1603 _DistanceType(__buf
.requested_size()),
1605 _DistanceType(__buf
.size()));
1609 * @brief Move elements for which a predicate is true to the beginning
1610 * of a sequence, preserving relative ordering.
1611 * @ingroup mutating_algorithms
1612 * @param __first A forward iterator.
1613 * @param __last A forward iterator.
1614 * @param __pred A predicate functor.
1615 * @return An iterator @p middle such that @p __pred(i) is true for each
1616 * iterator @p i in the range @p [first,middle) and false for each @p i
1617 * in the range @p [middle,last).
1619 * Performs the same function as @p partition() with the additional
1620 * guarantee that the relative ordering of elements in each group is
1621 * preserved, so any two elements @p x and @p y in the range
1622 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1623 * relative ordering after calling @p stable_partition().
1625 template<typename _ForwardIterator
, typename _Predicate
>
1626 inline _ForwardIterator
1627 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1630 // concept requirements
1631 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1633 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1634 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1635 __glibcxx_requires_valid_range(__first
, __last
);
1637 return std::__stable_partition(__first
, __last
,
1638 __gnu_cxx::__ops::__pred_iter(__pred
));
1641 /// This is a helper function for the sort routines.
1642 template<typename _RandomAccessIterator
, typename _Compare
>
1643 _GLIBCXX20_CONSTEXPR
1645 __heap_select(_RandomAccessIterator __first
,
1646 _RandomAccessIterator __middle
,
1647 _RandomAccessIterator __last
, _Compare __comp
)
1649 std::__make_heap(__first
, __middle
, __comp
);
1650 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1651 if (__comp(__i
, __first
))
1652 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1657 template<typename _InputIterator
, typename _RandomAccessIterator
,
1659 _GLIBCXX20_CONSTEXPR
1660 _RandomAccessIterator
1661 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1662 _RandomAccessIterator __result_first
,
1663 _RandomAccessIterator __result_last
,
1666 typedef typename iterator_traits
<_InputIterator
>::value_type
1668 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1669 typedef typename
_RItTraits::difference_type _DistanceType
;
1671 if (__result_first
== __result_last
)
1672 return __result_last
;
1673 _RandomAccessIterator __result_real_last
= __result_first
;
1674 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1676 *__result_real_last
= *__first
;
1677 ++__result_real_last
;
1681 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1682 while (__first
!= __last
)
1684 if (__comp(__first
, __result_first
))
1685 std::__adjust_heap(__result_first
, _DistanceType(0),
1686 _DistanceType(__result_real_last
1688 _InputValueType(*__first
), __comp
);
1691 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1692 return __result_real_last
;
1696 * @brief Copy the smallest elements of a sequence.
1697 * @ingroup sorting_algorithms
1698 * @param __first An iterator.
1699 * @param __last Another iterator.
1700 * @param __result_first A random-access iterator.
1701 * @param __result_last Another random-access iterator.
1702 * @return An iterator indicating the end of the resulting sequence.
1704 * Copies and sorts the smallest N values from the range @p [__first,__last)
1705 * to the range beginning at @p __result_first, where the number of
1706 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1707 * @p (__result_last-__result_first).
1708 * After the sort if @e i and @e j are iterators in the range
1709 * @p [__result_first,__result_first+N) such that i precedes j then
1711 * The value returned is @p __result_first+N.
1713 template<typename _InputIterator
, typename _RandomAccessIterator
>
1714 _GLIBCXX20_CONSTEXPR
1715 inline _RandomAccessIterator
1716 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1717 _RandomAccessIterator __result_first
,
1718 _RandomAccessIterator __result_last
)
1720 #ifdef _GLIBCXX_CONCEPT_CHECKS
1721 typedef typename iterator_traits
<_InputIterator
>::value_type
1723 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1727 // concept requirements
1728 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1729 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1731 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1733 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1734 __glibcxx_requires_valid_range(__first
, __last
);
1735 __glibcxx_requires_irreflexive(__first
, __last
);
1736 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1738 return std::__partial_sort_copy(__first
, __last
,
1739 __result_first
, __result_last
,
1740 __gnu_cxx::__ops::__iter_less_iter());
1744 * @brief Copy the smallest elements of a sequence using a predicate for
1746 * @ingroup sorting_algorithms
1747 * @param __first An input iterator.
1748 * @param __last Another input iterator.
1749 * @param __result_first A random-access iterator.
1750 * @param __result_last Another random-access iterator.
1751 * @param __comp A comparison functor.
1752 * @return An iterator indicating the end of the resulting sequence.
1754 * Copies and sorts the smallest N values from the range @p [__first,__last)
1755 * to the range beginning at @p result_first, where the number of
1756 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1757 * @p (__result_last-__result_first).
1758 * After the sort if @e i and @e j are iterators in the range
1759 * @p [__result_first,__result_first+N) such that i precedes j then
1760 * @p __comp(*j,*i) is false.
1761 * The value returned is @p __result_first+N.
1763 template<typename _InputIterator
, typename _RandomAccessIterator
,
1765 _GLIBCXX20_CONSTEXPR
1766 inline _RandomAccessIterator
1767 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1768 _RandomAccessIterator __result_first
,
1769 _RandomAccessIterator __result_last
,
1772 #ifdef _GLIBCXX_CONCEPT_CHECKS
1773 typedef typename iterator_traits
<_InputIterator
>::value_type
1775 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1779 // concept requirements
1780 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1781 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1782 _RandomAccessIterator
>)
1783 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1785 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1786 _InputValueType
, _OutputValueType
>)
1787 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1788 _OutputValueType
, _OutputValueType
>)
1789 __glibcxx_requires_valid_range(__first
, __last
);
1790 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1791 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1793 return std::__partial_sort_copy(__first
, __last
,
1794 __result_first
, __result_last
,
1795 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1798 /// This is a helper function for the sort routine.
1799 template<typename _RandomAccessIterator
, typename _Compare
>
1800 _GLIBCXX20_CONSTEXPR
1802 __unguarded_linear_insert(_RandomAccessIterator __last
,
1805 typename iterator_traits
<_RandomAccessIterator
>::value_type
1806 __val
= _GLIBCXX_MOVE(*__last
);
1807 _RandomAccessIterator __next
= __last
;
1809 while (__comp(__val
, __next
))
1811 *__last
= _GLIBCXX_MOVE(*__next
);
1815 *__last
= _GLIBCXX_MOVE(__val
);
1818 /// This is a helper function for the sort routine.
1819 template<typename _RandomAccessIterator
, typename _Compare
>
1820 _GLIBCXX20_CONSTEXPR
1822 __insertion_sort(_RandomAccessIterator __first
,
1823 _RandomAccessIterator __last
, _Compare __comp
)
1825 if (__first
== __last
) return;
1827 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1829 if (__comp(__i
, __first
))
1831 typename iterator_traits
<_RandomAccessIterator
>::value_type
1832 __val
= _GLIBCXX_MOVE(*__i
);
1833 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1834 *__first
= _GLIBCXX_MOVE(__val
);
1837 std::__unguarded_linear_insert(__i
,
1838 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1842 /// This is a helper function for the sort routine.
1843 template<typename _RandomAccessIterator
, typename _Compare
>
1844 _GLIBCXX20_CONSTEXPR
1846 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1847 _RandomAccessIterator __last
, _Compare __comp
)
1849 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1850 std::__unguarded_linear_insert(__i
,
1851 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1856 * This controls some aspect of the sort routines.
1858 enum { _S_threshold
= 16 };
1860 /// This is a helper function for the sort routine.
1861 template<typename _RandomAccessIterator
, typename _Compare
>
1862 _GLIBCXX20_CONSTEXPR
1864 __final_insertion_sort(_RandomAccessIterator __first
,
1865 _RandomAccessIterator __last
, _Compare __comp
)
1867 if (__last
- __first
> int(_S_threshold
))
1869 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1870 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1874 std::__insertion_sort(__first
, __last
, __comp
);
1877 /// This is a helper function...
1878 template<typename _RandomAccessIterator
, typename _Compare
>
1879 _GLIBCXX20_CONSTEXPR
1880 _RandomAccessIterator
1881 __unguarded_partition(_RandomAccessIterator __first
,
1882 _RandomAccessIterator __last
,
1883 _RandomAccessIterator __pivot
, _Compare __comp
)
1887 while (__comp(__first
, __pivot
))
1890 while (__comp(__pivot
, __last
))
1892 if (!(__first
< __last
))
1894 std::iter_swap(__first
, __last
);
1899 /// This is a helper function...
1900 template<typename _RandomAccessIterator
, typename _Compare
>
1901 _GLIBCXX20_CONSTEXPR
1902 inline _RandomAccessIterator
1903 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1904 _RandomAccessIterator __last
, _Compare __comp
)
1906 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1907 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1909 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1912 template<typename _RandomAccessIterator
, typename _Compare
>
1913 _GLIBCXX20_CONSTEXPR
1915 __partial_sort(_RandomAccessIterator __first
,
1916 _RandomAccessIterator __middle
,
1917 _RandomAccessIterator __last
,
1920 std::__heap_select(__first
, __middle
, __last
, __comp
);
1921 std::__sort_heap(__first
, __middle
, __comp
);
1924 /// This is a helper function for the sort routine.
1925 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1926 _GLIBCXX20_CONSTEXPR
1928 __introsort_loop(_RandomAccessIterator __first
,
1929 _RandomAccessIterator __last
,
1930 _Size __depth_limit
, _Compare __comp
)
1932 while (__last
- __first
> int(_S_threshold
))
1934 if (__depth_limit
== 0)
1936 std::__partial_sort(__first
, __last
, __last
, __comp
);
1940 _RandomAccessIterator __cut
=
1941 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1942 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1949 template<typename _RandomAccessIterator
, typename _Compare
>
1950 _GLIBCXX20_CONSTEXPR
1952 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1955 if (__first
!= __last
)
1957 std::__introsort_loop(__first
, __last
,
1958 std::__lg(__last
- __first
) * 2,
1960 std::__final_insertion_sort(__first
, __last
, __comp
);
1964 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1965 _GLIBCXX20_CONSTEXPR
1967 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1968 _RandomAccessIterator __last
, _Size __depth_limit
,
1971 while (__last
- __first
> 3)
1973 if (__depth_limit
== 0)
1975 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1976 // Place the nth largest element in its final position.
1977 std::iter_swap(__first
, __nth
);
1981 _RandomAccessIterator __cut
=
1982 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1988 std::__insertion_sort(__first
, __last
, __comp
);
1993 // lower_bound moved to stl_algobase.h
1996 * @brief Finds the first position in which @p __val could be inserted
1997 * without changing the ordering.
1998 * @ingroup binary_search_algorithms
1999 * @param __first An iterator.
2000 * @param __last Another iterator.
2001 * @param __val The search term.
2002 * @param __comp A functor to use for comparisons.
2003 * @return An iterator pointing to the first element <em>not less
2004 * than</em> @p __val, or end() if every element is less
2006 * @ingroup binary_search_algorithms
2008 * The comparison function should have the same effects on ordering as
2009 * the function used for the initial sort.
2011 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2012 _GLIBCXX20_CONSTEXPR
2013 inline _ForwardIterator
2014 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2015 const _Tp
& __val
, _Compare __comp
)
2017 // concept requirements
2018 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2019 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2020 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2021 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2024 return std::__lower_bound(__first
, __last
, __val
,
2025 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2028 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2029 _GLIBCXX20_CONSTEXPR
2031 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2032 const _Tp
& __val
, _Compare __comp
)
2034 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2037 _DistanceType __len
= std::distance(__first
, __last
);
2041 _DistanceType __half
= __len
>> 1;
2042 _ForwardIterator __middle
= __first
;
2043 std::advance(__middle
, __half
);
2044 if (__comp(__val
, __middle
))
2050 __len
= __len
- __half
- 1;
2057 * @brief Finds the last position in which @p __val could be inserted
2058 * without changing the ordering.
2059 * @ingroup binary_search_algorithms
2060 * @param __first An iterator.
2061 * @param __last Another iterator.
2062 * @param __val The search term.
2063 * @return An iterator pointing to the first element greater than @p __val,
2064 * or end() if no elements are greater than @p __val.
2065 * @ingroup binary_search_algorithms
2067 template<typename _ForwardIterator
, typename _Tp
>
2068 _GLIBCXX20_CONSTEXPR
2069 inline _ForwardIterator
2070 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2073 // concept requirements
2074 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2075 __glibcxx_function_requires(_LessThanOpConcept
<
2076 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2077 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2079 return std::__upper_bound(__first
, __last
, __val
,
2080 __gnu_cxx::__ops::__val_less_iter());
2084 * @brief Finds the last position in which @p __val could be inserted
2085 * without changing the ordering.
2086 * @ingroup binary_search_algorithms
2087 * @param __first An iterator.
2088 * @param __last Another iterator.
2089 * @param __val The search term.
2090 * @param __comp A functor to use for comparisons.
2091 * @return An iterator pointing to the first element greater than @p __val,
2092 * or end() if no elements are greater than @p __val.
2093 * @ingroup binary_search_algorithms
2095 * The comparison function should have the same effects on ordering as
2096 * the function used for the initial sort.
2098 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2099 _GLIBCXX20_CONSTEXPR
2100 inline _ForwardIterator
2101 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2102 const _Tp
& __val
, _Compare __comp
)
2104 // concept requirements
2105 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2106 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2107 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2108 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2111 return std::__upper_bound(__first
, __last
, __val
,
2112 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2115 template<typename _ForwardIterator
, typename _Tp
,
2116 typename _CompareItTp
, typename _CompareTpIt
>
2117 _GLIBCXX20_CONSTEXPR
2118 pair
<_ForwardIterator
, _ForwardIterator
>
2119 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2121 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2123 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2126 _DistanceType __len
= std::distance(__first
, __last
);
2130 _DistanceType __half
= __len
>> 1;
2131 _ForwardIterator __middle
= __first
;
2132 std::advance(__middle
, __half
);
2133 if (__comp_it_val(__middle
, __val
))
2137 __len
= __len
- __half
- 1;
2139 else if (__comp_val_it(__val
, __middle
))
2143 _ForwardIterator __left
2144 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2145 std::advance(__first
, __len
);
2146 _ForwardIterator __right
2147 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2148 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2151 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2155 * @brief Finds the largest subrange in which @p __val could be inserted
2156 * at any place in it without changing the ordering.
2157 * @ingroup binary_search_algorithms
2158 * @param __first An iterator.
2159 * @param __last Another iterator.
2160 * @param __val The search term.
2161 * @return An pair of iterators defining the subrange.
2162 * @ingroup binary_search_algorithms
2164 * This is equivalent to
2166 * std::make_pair(lower_bound(__first, __last, __val),
2167 * upper_bound(__first, __last, __val))
2169 * but does not actually call those functions.
2171 template<typename _ForwardIterator
, typename _Tp
>
2172 _GLIBCXX20_CONSTEXPR
2173 inline pair
<_ForwardIterator
, _ForwardIterator
>
2174 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2177 // concept requirements
2178 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2179 __glibcxx_function_requires(_LessThanOpConcept
<
2180 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2181 __glibcxx_function_requires(_LessThanOpConcept
<
2182 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2183 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2184 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2186 return std::__equal_range(__first
, __last
, __val
,
2187 __gnu_cxx::__ops::__iter_less_val(),
2188 __gnu_cxx::__ops::__val_less_iter());
2192 * @brief Finds the largest subrange in which @p __val could be inserted
2193 * at any place in it without changing the ordering.
2194 * @param __first An iterator.
2195 * @param __last Another iterator.
2196 * @param __val The search term.
2197 * @param __comp A functor to use for comparisons.
2198 * @return An pair of iterators defining the subrange.
2199 * @ingroup binary_search_algorithms
2201 * This is equivalent to
2203 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2204 * upper_bound(__first, __last, __val, __comp))
2206 * but does not actually call those functions.
2208 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2209 _GLIBCXX20_CONSTEXPR
2210 inline pair
<_ForwardIterator
, _ForwardIterator
>
2211 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2212 const _Tp
& __val
, _Compare __comp
)
2214 // concept requirements
2215 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2216 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2217 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2218 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2219 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2220 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2222 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2225 return std::__equal_range(__first
, __last
, __val
,
2226 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2227 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2231 * @brief Determines whether an element exists in a range.
2232 * @ingroup binary_search_algorithms
2233 * @param __first An iterator.
2234 * @param __last Another iterator.
2235 * @param __val The search term.
2236 * @return True if @p __val (or its equivalent) is in [@p
2237 * __first,@p __last ].
2239 * Note that this does not actually return an iterator to @p __val. For
2240 * that, use std::find or a container's specialized find member functions.
2242 template<typename _ForwardIterator
, typename _Tp
>
2243 _GLIBCXX20_CONSTEXPR
2245 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2248 // concept requirements
2249 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2250 __glibcxx_function_requires(_LessThanOpConcept
<
2251 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2252 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2253 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2255 _ForwardIterator __i
2256 = std::__lower_bound(__first
, __last
, __val
,
2257 __gnu_cxx::__ops::__iter_less_val());
2258 return __i
!= __last
&& !(__val
< *__i
);
2262 * @brief Determines whether an element exists in a range.
2263 * @ingroup binary_search_algorithms
2264 * @param __first An iterator.
2265 * @param __last Another iterator.
2266 * @param __val The search term.
2267 * @param __comp A functor to use for comparisons.
2268 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2270 * Note that this does not actually return an iterator to @p __val. For
2271 * that, use std::find or a container's specialized find member functions.
2273 * The comparison function should have the same effects on ordering as
2274 * the function used for the initial sort.
2276 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2277 _GLIBCXX20_CONSTEXPR
2279 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2280 const _Tp
& __val
, _Compare __comp
)
2282 // concept requirements
2283 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2284 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2285 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2286 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2288 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2291 _ForwardIterator __i
2292 = std::__lower_bound(__first
, __last
, __val
,
2293 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2294 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2299 /// This is a helper function for the __merge_adaptive routines.
2300 template<typename _InputIterator1
, typename _InputIterator2
,
2301 typename _OutputIterator
, typename _Compare
>
2303 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2304 _InputIterator2 __first2
, _InputIterator2 __last2
,
2305 _OutputIterator __result
, _Compare __comp
)
2307 while (__first1
!= __last1
&& __first2
!= __last2
)
2309 if (__comp(__first2
, __first1
))
2311 *__result
= _GLIBCXX_MOVE(*__first2
);
2316 *__result
= _GLIBCXX_MOVE(*__first1
);
2321 if (__first1
!= __last1
)
2322 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2325 /// This is a helper function for the __merge_adaptive routines.
2326 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2327 typename _BidirectionalIterator3
, typename _Compare
>
2329 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2330 _BidirectionalIterator1 __last1
,
2331 _BidirectionalIterator2 __first2
,
2332 _BidirectionalIterator2 __last2
,
2333 _BidirectionalIterator3 __result
,
2336 if (__first1
== __last1
)
2338 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2341 else if (__first2
== __last2
)
2348 if (__comp(__last2
, __last1
))
2350 *--__result
= _GLIBCXX_MOVE(*__last1
);
2351 if (__first1
== __last1
)
2353 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2360 *--__result
= _GLIBCXX_MOVE(*__last2
);
2361 if (__first2
== __last2
)
2368 /// This is a helper function for the merge routines.
2369 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2371 _BidirectionalIterator1
2372 __rotate_adaptive(_BidirectionalIterator1 __first
,
2373 _BidirectionalIterator1 __middle
,
2374 _BidirectionalIterator1 __last
,
2375 _Distance __len1
, _Distance __len2
,
2376 _BidirectionalIterator2 __buffer
,
2377 _Distance __buffer_size
)
2379 _BidirectionalIterator2 __buffer_end
;
2380 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2384 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2385 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2386 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2391 else if (__len1
<= __buffer_size
)
2395 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2396 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2397 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2403 return std::rotate(__first
, __middle
, __last
);
2406 /// This is a helper function for the merge routines.
2407 template<typename _BidirectionalIterator
, typename _Distance
,
2408 typename _Pointer
, typename _Compare
>
2410 __merge_adaptive(_BidirectionalIterator __first
,
2411 _BidirectionalIterator __middle
,
2412 _BidirectionalIterator __last
,
2413 _Distance __len1
, _Distance __len2
,
2414 _Pointer __buffer
, _Distance __buffer_size
,
2417 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2419 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2420 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2423 else if (__len2
<= __buffer_size
)
2425 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2426 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2427 __buffer_end
, __last
, __comp
);
2431 _BidirectionalIterator __first_cut
= __first
;
2432 _BidirectionalIterator __second_cut
= __middle
;
2433 _Distance __len11
= 0;
2434 _Distance __len22
= 0;
2435 if (__len1
> __len2
)
2437 __len11
= __len1
/ 2;
2438 std::advance(__first_cut
, __len11
);
2440 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2441 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2442 __len22
= std::distance(__middle
, __second_cut
);
2446 __len22
= __len2
/ 2;
2447 std::advance(__second_cut
, __len22
);
2449 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2450 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2451 __len11
= std::distance(__first
, __first_cut
);
2454 _BidirectionalIterator __new_middle
2455 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2456 __len1
- __len11
, __len22
, __buffer
,
2458 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2459 __len22
, __buffer
, __buffer_size
, __comp
);
2460 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2462 __len2
- __len22
, __buffer
,
2463 __buffer_size
, __comp
);
2467 /// This is a helper function for the merge routines.
2468 template<typename _BidirectionalIterator
, typename _Distance
,
2471 __merge_without_buffer(_BidirectionalIterator __first
,
2472 _BidirectionalIterator __middle
,
2473 _BidirectionalIterator __last
,
2474 _Distance __len1
, _Distance __len2
,
2477 if (__len1
== 0 || __len2
== 0)
2480 if (__len1
+ __len2
== 2)
2482 if (__comp(__middle
, __first
))
2483 std::iter_swap(__first
, __middle
);
2487 _BidirectionalIterator __first_cut
= __first
;
2488 _BidirectionalIterator __second_cut
= __middle
;
2489 _Distance __len11
= 0;
2490 _Distance __len22
= 0;
2491 if (__len1
> __len2
)
2493 __len11
= __len1
/ 2;
2494 std::advance(__first_cut
, __len11
);
2496 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2497 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2498 __len22
= std::distance(__middle
, __second_cut
);
2502 __len22
= __len2
/ 2;
2503 std::advance(__second_cut
, __len22
);
2505 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2506 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2507 __len11
= std::distance(__first
, __first_cut
);
2510 _BidirectionalIterator __new_middle
2511 = std::rotate(__first_cut
, __middle
, __second_cut
);
2512 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2513 __len11
, __len22
, __comp
);
2514 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2515 __len1
- __len11
, __len2
- __len22
, __comp
);
2518 template<typename _BidirectionalIterator
, typename _Compare
>
2520 __inplace_merge(_BidirectionalIterator __first
,
2521 _BidirectionalIterator __middle
,
2522 _BidirectionalIterator __last
,
2525 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2527 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2529 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2531 if (__first
== __middle
|| __middle
== __last
)
2534 const _DistanceType __len1
= std::distance(__first
, __middle
);
2535 const _DistanceType __len2
= std::distance(__middle
, __last
);
2537 // __merge_adaptive will use a buffer for the smaller of
2538 // [first,middle) and [middle,last).
2539 _TmpBuf
__buf(__first
, std::min(__len1
, __len2
));
2541 if (__buf
.begin() == 0)
2542 std::__merge_without_buffer
2543 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2545 std::__merge_adaptive
2546 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2547 _DistanceType(__buf
.size()), __comp
);
2551 * @brief Merges two sorted ranges in place.
2552 * @ingroup sorting_algorithms
2553 * @param __first An iterator.
2554 * @param __middle Another iterator.
2555 * @param __last Another iterator.
2558 * Merges two sorted and consecutive ranges, [__first,__middle) and
2559 * [__middle,__last), and puts the result in [__first,__last). The
2560 * output will be sorted. The sort is @e stable, that is, for
2561 * equivalent elements in the two ranges, elements from the first
2562 * range will always come before elements from the second.
2564 * If enough additional memory is available, this takes (__last-__first)-1
2565 * comparisons. Otherwise an NlogN algorithm is used, where N is
2566 * distance(__first,__last).
2568 template<typename _BidirectionalIterator
>
2570 inplace_merge(_BidirectionalIterator __first
,
2571 _BidirectionalIterator __middle
,
2572 _BidirectionalIterator __last
)
2574 // concept requirements
2575 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2576 _BidirectionalIterator
>)
2577 __glibcxx_function_requires(_LessThanComparableConcept
<
2578 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2579 __glibcxx_requires_sorted(__first
, __middle
);
2580 __glibcxx_requires_sorted(__middle
, __last
);
2581 __glibcxx_requires_irreflexive(__first
, __last
);
2583 std::__inplace_merge(__first
, __middle
, __last
,
2584 __gnu_cxx::__ops::__iter_less_iter());
2588 * @brief Merges two sorted ranges in place.
2589 * @ingroup sorting_algorithms
2590 * @param __first An iterator.
2591 * @param __middle Another iterator.
2592 * @param __last Another iterator.
2593 * @param __comp A functor to use for comparisons.
2596 * Merges two sorted and consecutive ranges, [__first,__middle) and
2597 * [middle,last), and puts the result in [__first,__last). The output will
2598 * be sorted. The sort is @e stable, that is, for equivalent
2599 * elements in the two ranges, elements from the first range will always
2600 * come before elements from the second.
2602 * If enough additional memory is available, this takes (__last-__first)-1
2603 * comparisons. Otherwise an NlogN algorithm is used, where N is
2604 * distance(__first,__last).
2606 * The comparison function should have the same effects on ordering as
2607 * the function used for the initial sort.
2609 template<typename _BidirectionalIterator
, typename _Compare
>
2611 inplace_merge(_BidirectionalIterator __first
,
2612 _BidirectionalIterator __middle
,
2613 _BidirectionalIterator __last
,
2616 // concept requirements
2617 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2618 _BidirectionalIterator
>)
2619 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2620 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2621 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2622 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2623 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2624 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2626 std::__inplace_merge(__first
, __middle
, __last
,
2627 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2631 /// This is a helper function for the __merge_sort_loop routines.
2632 template<typename _InputIterator
, typename _OutputIterator
,
2635 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2636 _InputIterator __first2
, _InputIterator __last2
,
2637 _OutputIterator __result
, _Compare __comp
)
2639 while (__first1
!= __last1
&& __first2
!= __last2
)
2641 if (__comp(__first2
, __first1
))
2643 *__result
= _GLIBCXX_MOVE(*__first2
);
2648 *__result
= _GLIBCXX_MOVE(*__first1
);
2653 return _GLIBCXX_MOVE3(__first2
, __last2
,
2654 _GLIBCXX_MOVE3(__first1
, __last1
,
2658 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2659 typename _Distance
, typename _Compare
>
2661 __merge_sort_loop(_RandomAccessIterator1 __first
,
2662 _RandomAccessIterator1 __last
,
2663 _RandomAccessIterator2 __result
, _Distance __step_size
,
2666 const _Distance __two_step
= 2 * __step_size
;
2668 while (__last
- __first
>= __two_step
)
2670 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2671 __first
+ __step_size
,
2672 __first
+ __two_step
,
2674 __first
+= __two_step
;
2676 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2678 std::__move_merge(__first
, __first
+ __step_size
,
2679 __first
+ __step_size
, __last
, __result
, __comp
);
2682 template<typename _RandomAccessIterator
, typename _Distance
,
2684 _GLIBCXX20_CONSTEXPR
2686 __chunk_insertion_sort(_RandomAccessIterator __first
,
2687 _RandomAccessIterator __last
,
2688 _Distance __chunk_size
, _Compare __comp
)
2690 while (__last
- __first
>= __chunk_size
)
2692 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2693 __first
+= __chunk_size
;
2695 std::__insertion_sort(__first
, __last
, __comp
);
2698 enum { _S_chunk_size
= 7 };
2700 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2702 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2703 _RandomAccessIterator __last
,
2704 _Pointer __buffer
, _Compare __comp
)
2706 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2709 const _Distance __len
= __last
- __first
;
2710 const _Pointer __buffer_last
= __buffer
+ __len
;
2712 _Distance __step_size
= _S_chunk_size
;
2713 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2715 while (__step_size
< __len
)
2717 std::__merge_sort_loop(__first
, __last
, __buffer
,
2718 __step_size
, __comp
);
2720 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2721 __step_size
, __comp
);
2726 template<typename _RandomAccessIterator
, typename _Pointer
,
2727 typename _Distance
, typename _Compare
>
2729 __stable_sort_adaptive(_RandomAccessIterator __first
,
2730 _RandomAccessIterator __last
,
2731 _Pointer __buffer
, _Distance __buffer_size
,
2734 const _Distance __len
= (__last
- __first
+ 1) / 2;
2735 const _RandomAccessIterator __middle
= __first
+ __len
;
2736 if (__len
> __buffer_size
)
2738 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2739 __buffer_size
, __comp
);
2740 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2741 __buffer_size
, __comp
);
2745 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2746 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2749 std::__merge_adaptive(__first
, __middle
, __last
,
2750 _Distance(__middle
- __first
),
2751 _Distance(__last
- __middle
),
2752 __buffer
, __buffer_size
,
2756 /// This is a helper function for the stable sorting routines.
2757 template<typename _RandomAccessIterator
, typename _Compare
>
2759 __inplace_stable_sort(_RandomAccessIterator __first
,
2760 _RandomAccessIterator __last
, _Compare __comp
)
2762 if (__last
- __first
< 15)
2764 std::__insertion_sort(__first
, __last
, __comp
);
2767 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2768 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2769 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2770 std::__merge_without_buffer(__first
, __middle
, __last
,
2778 // Set algorithms: includes, set_union, set_intersection, set_difference,
2779 // set_symmetric_difference. All of these algorithms have the precondition
2780 // that their input ranges are sorted and the postcondition that their output
2781 // ranges are sorted.
2783 template<typename _InputIterator1
, typename _InputIterator2
,
2785 _GLIBCXX20_CONSTEXPR
2787 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2788 _InputIterator2 __first2
, _InputIterator2 __last2
,
2791 while (__first1
!= __last1
&& __first2
!= __last2
)
2793 if (__comp(__first2
, __first1
))
2795 if (!__comp(__first1
, __first2
))
2800 return __first2
== __last2
;
2804 * @brief Determines whether all elements of a sequence exists in a range.
2805 * @param __first1 Start of search range.
2806 * @param __last1 End of search range.
2807 * @param __first2 Start of sequence
2808 * @param __last2 End of sequence.
2809 * @return True if each element in [__first2,__last2) is contained in order
2810 * within [__first1,__last1). False otherwise.
2811 * @ingroup set_algorithms
2813 * This operation expects both [__first1,__last1) and
2814 * [__first2,__last2) to be sorted. Searches for the presence of
2815 * each element in [__first2,__last2) within [__first1,__last1).
2816 * The iterators over each range only move forward, so this is a
2817 * linear algorithm. If an element in [__first2,__last2) is not
2818 * found before the search iterator reaches @p __last2, false is
2821 template<typename _InputIterator1
, typename _InputIterator2
>
2822 _GLIBCXX20_CONSTEXPR
2824 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2825 _InputIterator2 __first2
, _InputIterator2 __last2
)
2827 // concept requirements
2828 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2829 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2830 __glibcxx_function_requires(_LessThanOpConcept
<
2831 typename iterator_traits
<_InputIterator1
>::value_type
,
2832 typename iterator_traits
<_InputIterator2
>::value_type
>)
2833 __glibcxx_function_requires(_LessThanOpConcept
<
2834 typename iterator_traits
<_InputIterator2
>::value_type
,
2835 typename iterator_traits
<_InputIterator1
>::value_type
>)
2836 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2837 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2838 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2839 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2841 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2842 __gnu_cxx::__ops::__iter_less_iter());
2846 * @brief Determines whether all elements of a sequence exists in a range
2848 * @ingroup set_algorithms
2849 * @param __first1 Start of search range.
2850 * @param __last1 End of search range.
2851 * @param __first2 Start of sequence
2852 * @param __last2 End of sequence.
2853 * @param __comp Comparison function to use.
2854 * @return True if each element in [__first2,__last2) is contained
2855 * in order within [__first1,__last1) according to comp. False
2856 * otherwise. @ingroup set_algorithms
2858 * This operation expects both [__first1,__last1) and
2859 * [__first2,__last2) to be sorted. Searches for the presence of
2860 * each element in [__first2,__last2) within [__first1,__last1),
2861 * using comp to decide. The iterators over each range only move
2862 * forward, so this is a linear algorithm. If an element in
2863 * [__first2,__last2) is not found before the search iterator
2864 * reaches @p __last2, false is returned.
2866 template<typename _InputIterator1
, typename _InputIterator2
,
2868 _GLIBCXX20_CONSTEXPR
2870 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2871 _InputIterator2 __first2
, _InputIterator2 __last2
,
2874 // concept requirements
2875 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2876 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2877 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2878 typename iterator_traits
<_InputIterator1
>::value_type
,
2879 typename iterator_traits
<_InputIterator2
>::value_type
>)
2880 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2881 typename iterator_traits
<_InputIterator2
>::value_type
,
2882 typename iterator_traits
<_InputIterator1
>::value_type
>)
2883 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2884 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2885 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2886 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2888 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2889 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2898 // set_symmetric_difference
2902 template<typename _BidirectionalIterator
, typename _Compare
>
2903 _GLIBCXX20_CONSTEXPR
2905 __next_permutation(_BidirectionalIterator __first
,
2906 _BidirectionalIterator __last
, _Compare __comp
)
2908 if (__first
== __last
)
2910 _BidirectionalIterator __i
= __first
;
2919 _BidirectionalIterator __ii
= __i
;
2921 if (__comp(__i
, __ii
))
2923 _BidirectionalIterator __j
= __last
;
2924 while (!__comp(__i
, --__j
))
2926 std::iter_swap(__i
, __j
);
2927 std::__reverse(__ii
, __last
,
2928 std::__iterator_category(__first
));
2933 std::__reverse(__first
, __last
,
2934 std::__iterator_category(__first
));
2941 * @brief Permute range into the next @e dictionary ordering.
2942 * @ingroup sorting_algorithms
2943 * @param __first Start of range.
2944 * @param __last End of range.
2945 * @return False if wrapped to first permutation, true otherwise.
2947 * Treats all permutations of the range as a set of @e dictionary sorted
2948 * sequences. Permutes the current sequence into the next one of this set.
2949 * Returns true if there are more sequences to generate. If the sequence
2950 * is the largest of the set, the smallest is generated and false returned.
2952 template<typename _BidirectionalIterator
>
2953 _GLIBCXX20_CONSTEXPR
2955 next_permutation(_BidirectionalIterator __first
,
2956 _BidirectionalIterator __last
)
2958 // concept requirements
2959 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2960 _BidirectionalIterator
>)
2961 __glibcxx_function_requires(_LessThanComparableConcept
<
2962 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2963 __glibcxx_requires_valid_range(__first
, __last
);
2964 __glibcxx_requires_irreflexive(__first
, __last
);
2966 return std::__next_permutation
2967 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2971 * @brief Permute range into the next @e dictionary ordering using
2972 * comparison functor.
2973 * @ingroup sorting_algorithms
2974 * @param __first Start of range.
2975 * @param __last End of range.
2976 * @param __comp A comparison functor.
2977 * @return False if wrapped to first permutation, true otherwise.
2979 * Treats all permutations of the range [__first,__last) as a set of
2980 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2981 * sequence into the next one of this set. Returns true if there are more
2982 * sequences to generate. If the sequence is the largest of the set, the
2983 * smallest is generated and false returned.
2985 template<typename _BidirectionalIterator
, typename _Compare
>
2986 _GLIBCXX20_CONSTEXPR
2988 next_permutation(_BidirectionalIterator __first
,
2989 _BidirectionalIterator __last
, _Compare __comp
)
2991 // concept requirements
2992 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2993 _BidirectionalIterator
>)
2994 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2995 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2996 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2997 __glibcxx_requires_valid_range(__first
, __last
);
2998 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3000 return std::__next_permutation
3001 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3004 template<typename _BidirectionalIterator
, typename _Compare
>
3005 _GLIBCXX20_CONSTEXPR
3007 __prev_permutation(_BidirectionalIterator __first
,
3008 _BidirectionalIterator __last
, _Compare __comp
)
3010 if (__first
== __last
)
3012 _BidirectionalIterator __i
= __first
;
3021 _BidirectionalIterator __ii
= __i
;
3023 if (__comp(__ii
, __i
))
3025 _BidirectionalIterator __j
= __last
;
3026 while (!__comp(--__j
, __i
))
3028 std::iter_swap(__i
, __j
);
3029 std::__reverse(__ii
, __last
,
3030 std::__iterator_category(__first
));
3035 std::__reverse(__first
, __last
,
3036 std::__iterator_category(__first
));
3043 * @brief Permute range into the previous @e dictionary ordering.
3044 * @ingroup sorting_algorithms
3045 * @param __first Start of range.
3046 * @param __last End of range.
3047 * @return False if wrapped to last permutation, true otherwise.
3049 * Treats all permutations of the range as a set of @e dictionary sorted
3050 * sequences. Permutes the current sequence into the previous one of this
3051 * set. Returns true if there are more sequences to generate. If the
3052 * sequence is the smallest of the set, the largest is generated and false
3055 template<typename _BidirectionalIterator
>
3056 _GLIBCXX20_CONSTEXPR
3058 prev_permutation(_BidirectionalIterator __first
,
3059 _BidirectionalIterator __last
)
3061 // concept requirements
3062 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3063 _BidirectionalIterator
>)
3064 __glibcxx_function_requires(_LessThanComparableConcept
<
3065 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3066 __glibcxx_requires_valid_range(__first
, __last
);
3067 __glibcxx_requires_irreflexive(__first
, __last
);
3069 return std::__prev_permutation(__first
, __last
,
3070 __gnu_cxx::__ops::__iter_less_iter());
3074 * @brief Permute range into the previous @e dictionary ordering using
3075 * comparison functor.
3076 * @ingroup sorting_algorithms
3077 * @param __first Start of range.
3078 * @param __last End of range.
3079 * @param __comp A comparison functor.
3080 * @return False if wrapped to last permutation, true otherwise.
3082 * Treats all permutations of the range [__first,__last) as a set of
3083 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3084 * sequence into the previous one of this set. Returns true if there are
3085 * more sequences to generate. If the sequence is the smallest of the set,
3086 * the largest is generated and false returned.
3088 template<typename _BidirectionalIterator
, typename _Compare
>
3089 _GLIBCXX20_CONSTEXPR
3091 prev_permutation(_BidirectionalIterator __first
,
3092 _BidirectionalIterator __last
, _Compare __comp
)
3094 // concept requirements
3095 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3096 _BidirectionalIterator
>)
3097 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3098 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3099 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3100 __glibcxx_requires_valid_range(__first
, __last
);
3101 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3103 return std::__prev_permutation(__first
, __last
,
3104 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3110 template<typename _InputIterator
, typename _OutputIterator
,
3111 typename _Predicate
, typename _Tp
>
3112 _GLIBCXX20_CONSTEXPR
3114 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3115 _OutputIterator __result
,
3116 _Predicate __pred
, const _Tp
& __new_value
)
3118 for (; __first
!= __last
; ++__first
, (void)++__result
)
3119 if (__pred(__first
))
3120 *__result
= __new_value
;
3122 *__result
= *__first
;
3127 * @brief Copy a sequence, replacing each element of one value with another
3129 * @param __first An input iterator.
3130 * @param __last An input iterator.
3131 * @param __result An output iterator.
3132 * @param __old_value The value to be replaced.
3133 * @param __new_value The replacement value.
3134 * @return The end of the output sequence, @p result+(last-first).
3136 * Copies each element in the input range @p [__first,__last) to the
3137 * output range @p [__result,__result+(__last-__first)) replacing elements
3138 * equal to @p __old_value with @p __new_value.
3140 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3141 _GLIBCXX20_CONSTEXPR
3142 inline _OutputIterator
3143 replace_copy(_InputIterator __first
, _InputIterator __last
,
3144 _OutputIterator __result
,
3145 const _Tp
& __old_value
, const _Tp
& __new_value
)
3147 // concept requirements
3148 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3149 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3150 typename iterator_traits
<_InputIterator
>::value_type
>)
3151 __glibcxx_function_requires(_EqualOpConcept
<
3152 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3153 __glibcxx_requires_valid_range(__first
, __last
);
3155 return std::__replace_copy_if(__first
, __last
, __result
,
3156 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3161 * @brief Copy a sequence, replacing each value for which a predicate
3162 * returns true with another value.
3163 * @ingroup mutating_algorithms
3164 * @param __first An input iterator.
3165 * @param __last An input iterator.
3166 * @param __result An output iterator.
3167 * @param __pred A predicate.
3168 * @param __new_value The replacement value.
3169 * @return The end of the output sequence, @p __result+(__last-__first).
3171 * Copies each element in the range @p [__first,__last) to the range
3172 * @p [__result,__result+(__last-__first)) replacing elements for which
3173 * @p __pred returns true with @p __new_value.
3175 template<typename _InputIterator
, typename _OutputIterator
,
3176 typename _Predicate
, typename _Tp
>
3177 _GLIBCXX20_CONSTEXPR
3178 inline _OutputIterator
3179 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3180 _OutputIterator __result
,
3181 _Predicate __pred
, const _Tp
& __new_value
)
3183 // concept requirements
3184 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3185 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3186 typename iterator_traits
<_InputIterator
>::value_type
>)
3187 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3188 typename iterator_traits
<_InputIterator
>::value_type
>)
3189 __glibcxx_requires_valid_range(__first
, __last
);
3191 return std::__replace_copy_if(__first
, __last
, __result
,
3192 __gnu_cxx::__ops::__pred_iter(__pred
),
3196 #if __cplusplus >= 201103L
3198 * @brief Determines whether the elements of a sequence are sorted.
3199 * @ingroup sorting_algorithms
3200 * @param __first An iterator.
3201 * @param __last Another iterator.
3202 * @return True if the elements are sorted, false otherwise.
3204 template<typename _ForwardIterator
>
3205 _GLIBCXX20_CONSTEXPR
3207 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3208 { return std::is_sorted_until(__first
, __last
) == __last
; }
3211 * @brief Determines whether the elements of a sequence are sorted
3212 * according to a comparison functor.
3213 * @ingroup sorting_algorithms
3214 * @param __first An iterator.
3215 * @param __last Another iterator.
3216 * @param __comp A comparison functor.
3217 * @return True if the elements are sorted, false otherwise.
3219 template<typename _ForwardIterator
, typename _Compare
>
3220 _GLIBCXX20_CONSTEXPR
3222 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3224 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3226 template<typename _ForwardIterator
, typename _Compare
>
3227 _GLIBCXX20_CONSTEXPR
3229 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3232 if (__first
== __last
)
3235 _ForwardIterator __next
= __first
;
3236 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3237 if (__comp(__next
, __first
))
3243 * @brief Determines the end of a sorted sequence.
3244 * @ingroup sorting_algorithms
3245 * @param __first An iterator.
3246 * @param __last Another iterator.
3247 * @return An iterator pointing to the last iterator i in [__first, __last)
3248 * for which the range [__first, i) is sorted.
3250 template<typename _ForwardIterator
>
3251 _GLIBCXX20_CONSTEXPR
3252 inline _ForwardIterator
3253 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3255 // concept requirements
3256 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3257 __glibcxx_function_requires(_LessThanComparableConcept
<
3258 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3259 __glibcxx_requires_valid_range(__first
, __last
);
3260 __glibcxx_requires_irreflexive(__first
, __last
);
3262 return std::__is_sorted_until(__first
, __last
,
3263 __gnu_cxx::__ops::__iter_less_iter());
3267 * @brief Determines the end of a sorted sequence using comparison functor.
3268 * @ingroup sorting_algorithms
3269 * @param __first An iterator.
3270 * @param __last Another iterator.
3271 * @param __comp A comparison functor.
3272 * @return An iterator pointing to the last iterator i in [__first, __last)
3273 * for which the range [__first, i) is sorted.
3275 template<typename _ForwardIterator
, typename _Compare
>
3276 _GLIBCXX20_CONSTEXPR
3277 inline _ForwardIterator
3278 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3281 // concept requirements
3282 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3283 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3284 typename iterator_traits
<_ForwardIterator
>::value_type
,
3285 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3286 __glibcxx_requires_valid_range(__first
, __last
);
3287 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3289 return std::__is_sorted_until(__first
, __last
,
3290 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3294 * @brief Determines min and max at once as an ordered pair.
3295 * @ingroup sorting_algorithms
3296 * @param __a A thing of arbitrary type.
3297 * @param __b Another thing of arbitrary type.
3298 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3301 template<typename _Tp
>
3302 _GLIBCXX14_CONSTEXPR
3303 inline pair
<const _Tp
&, const _Tp
&>
3304 minmax(const _Tp
& __a
, const _Tp
& __b
)
3306 // concept requirements
3307 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3309 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3310 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3314 * @brief Determines min and max at once as an ordered pair.
3315 * @ingroup sorting_algorithms
3316 * @param __a A thing of arbitrary type.
3317 * @param __b Another thing of arbitrary type.
3318 * @param __comp A @link comparison_functors comparison functor @endlink.
3319 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3322 template<typename _Tp
, typename _Compare
>
3323 _GLIBCXX14_CONSTEXPR
3324 inline pair
<const _Tp
&, const _Tp
&>
3325 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3327 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3328 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3331 template<typename _ForwardIterator
, typename _Compare
>
3332 _GLIBCXX14_CONSTEXPR
3333 pair
<_ForwardIterator
, _ForwardIterator
>
3334 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3337 _ForwardIterator __next
= __first
;
3338 if (__first
== __last
3339 || ++__next
== __last
)
3340 return std::make_pair(__first
, __first
);
3342 _ForwardIterator __min
{}, __max
{};
3343 if (__comp(__next
, __first
))
3357 while (__first
!= __last
)
3360 if (++__next
== __last
)
3362 if (__comp(__first
, __min
))
3364 else if (!__comp(__first
, __max
))
3369 if (__comp(__next
, __first
))
3371 if (__comp(__next
, __min
))
3373 if (!__comp(__first
, __max
))
3378 if (__comp(__first
, __min
))
3380 if (!__comp(__next
, __max
))
3388 return std::make_pair(__min
, __max
);
3392 * @brief Return a pair of iterators pointing to the minimum and maximum
3393 * elements in a range.
3394 * @ingroup sorting_algorithms
3395 * @param __first Start of range.
3396 * @param __last End of range.
3397 * @return make_pair(m, M), where m is the first iterator i in
3398 * [__first, __last) such that no other element in the range is
3399 * smaller, and where M is the last iterator i in [__first, __last)
3400 * such that no other element in the range is larger.
3402 template<typename _ForwardIterator
>
3403 _GLIBCXX14_CONSTEXPR
3404 inline pair
<_ForwardIterator
, _ForwardIterator
>
3405 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3407 // concept requirements
3408 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3409 __glibcxx_function_requires(_LessThanComparableConcept
<
3410 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3411 __glibcxx_requires_valid_range(__first
, __last
);
3412 __glibcxx_requires_irreflexive(__first
, __last
);
3414 return std::__minmax_element(__first
, __last
,
3415 __gnu_cxx::__ops::__iter_less_iter());
3419 * @brief Return a pair of iterators pointing to the minimum and maximum
3420 * elements in a range.
3421 * @ingroup sorting_algorithms
3422 * @param __first Start of range.
3423 * @param __last End of range.
3424 * @param __comp Comparison functor.
3425 * @return make_pair(m, M), where m is the first iterator i in
3426 * [__first, __last) such that no other element in the range is
3427 * smaller, and where M is the last iterator i in [__first, __last)
3428 * such that no other element in the range is larger.
3430 template<typename _ForwardIterator
, typename _Compare
>
3431 _GLIBCXX14_CONSTEXPR
3432 inline pair
<_ForwardIterator
, _ForwardIterator
>
3433 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3436 // concept requirements
3437 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3438 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3439 typename iterator_traits
<_ForwardIterator
>::value_type
,
3440 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3441 __glibcxx_requires_valid_range(__first
, __last
);
3442 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3444 return std::__minmax_element(__first
, __last
,
3445 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3448 template<typename _Tp
>
3449 _GLIBCXX14_CONSTEXPR
3450 inline pair
<_Tp
, _Tp
>
3451 minmax(initializer_list
<_Tp
> __l
)
3453 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
3454 pair
<const _Tp
*, const _Tp
*> __p
=
3455 std::__minmax_element(__l
.begin(), __l
.end(),
3456 __gnu_cxx::__ops::__iter_less_iter());
3457 return std::make_pair(*__p
.first
, *__p
.second
);
3460 template<typename _Tp
, typename _Compare
>
3461 _GLIBCXX14_CONSTEXPR
3462 inline pair
<_Tp
, _Tp
>
3463 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3465 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
3466 pair
<const _Tp
*, const _Tp
*> __p
=
3467 std::__minmax_element(__l
.begin(), __l
.end(),
3468 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3469 return std::make_pair(*__p
.first
, *__p
.second
);
3473 * @brief Checks whether a permutation of the second sequence is equal
3474 * to the first sequence.
3475 * @ingroup non_mutating_algorithms
3476 * @param __first1 Start of first range.
3477 * @param __last1 End of first range.
3478 * @param __first2 Start of second range.
3479 * @param __pred A binary predicate.
3480 * @return true if there exists a permutation of the elements in
3481 * the range [__first2, __first2 + (__last1 - __first1)),
3482 * beginning with ForwardIterator2 begin, such that
3483 * equal(__first1, __last1, __begin, __pred) returns true;
3484 * otherwise, returns false.
3486 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3487 typename _BinaryPredicate
>
3488 _GLIBCXX20_CONSTEXPR
3490 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3491 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3493 // concept requirements
3494 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3495 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3496 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3497 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3498 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3499 __glibcxx_requires_valid_range(__first1
, __last1
);
3501 return std::__is_permutation(__first1
, __last1
, __first2
,
3502 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3505 #if __cplusplus > 201103L
3506 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3507 typename _BinaryPredicate
>
3508 _GLIBCXX20_CONSTEXPR
3510 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3511 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3512 _BinaryPredicate __pred
)
3515 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3517 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3518 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3519 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3520 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3523 auto __d1
= std::distance(__first1
, __last1
);
3524 auto __d2
= std::distance(__first2
, __last2
);
3529 // Efficiently compare identical prefixes: O(N) if sequences
3530 // have the same elements in the same order.
3531 for (; __first1
!= __last1
&& __first2
!= __last2
;
3532 ++__first1
, (void)++__first2
)
3533 if (!__pred(__first1
, __first2
))
3538 if (__first1
== __last1
)
3543 auto __d1
= std::distance(__first1
, __last1
);
3544 auto __d2
= std::distance(__first2
, __last2
);
3545 if (__d1
== 0 && __d2
== 0)
3551 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3553 if (__scan
!= std::__find_if(__first1
, __scan
,
3554 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3555 continue; // We've seen this one before.
3557 auto __matches
= std::__count_if(__first2
, __last2
,
3558 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3560 || std::__count_if(__scan
, __last1
,
3561 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3569 * @brief Checks whether a permutaion of the second sequence is equal
3570 * to the first sequence.
3571 * @ingroup non_mutating_algorithms
3572 * @param __first1 Start of first range.
3573 * @param __last1 End of first range.
3574 * @param __first2 Start of second range.
3575 * @param __last2 End of first range.
3576 * @return true if there exists a permutation of the elements in the range
3577 * [__first2, __last2), beginning with ForwardIterator2 begin,
3578 * such that equal(__first1, __last1, begin) returns true;
3579 * otherwise, returns false.
3581 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3582 _GLIBCXX20_CONSTEXPR
3584 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3585 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3587 __glibcxx_requires_valid_range(__first1
, __last1
);
3588 __glibcxx_requires_valid_range(__first2
, __last2
);
3591 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3592 __gnu_cxx::__ops::__iter_equal_to_iter());
3596 * @brief Checks whether a permutation of the second sequence is equal
3597 * to the first sequence.
3598 * @ingroup non_mutating_algorithms
3599 * @param __first1 Start of first range.
3600 * @param __last1 End of first range.
3601 * @param __first2 Start of second range.
3602 * @param __last2 End of first range.
3603 * @param __pred A binary predicate.
3604 * @return true if there exists a permutation of the elements in the range
3605 * [__first2, __last2), beginning with ForwardIterator2 begin,
3606 * such that equal(__first1, __last1, __begin, __pred) returns true;
3607 * otherwise, returns false.
3609 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3610 typename _BinaryPredicate
>
3611 _GLIBCXX20_CONSTEXPR
3613 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3614 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3615 _BinaryPredicate __pred
)
3617 __glibcxx_requires_valid_range(__first1
, __last1
);
3618 __glibcxx_requires_valid_range(__first2
, __last2
);
3620 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3621 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3624 #if __cplusplus > 201402L
3626 #define __cpp_lib_clamp 201603
3629 * @brief Returns the value clamped between lo and hi.
3630 * @ingroup sorting_algorithms
3631 * @param __val A value of arbitrary type.
3632 * @param __lo A lower limit of arbitrary type.
3633 * @param __hi An upper limit of arbitrary type.
3634 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
3636 template<typename _Tp
>
3637 constexpr const _Tp
&
3638 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3640 __glibcxx_assert(!(__hi
< __lo
));
3641 return (__val
< __lo
) ? __lo
: (__hi
< __val
) ? __hi
: __val
;
3645 * @brief Returns the value clamped between lo and hi.
3646 * @ingroup sorting_algorithms
3647 * @param __val A value of arbitrary type.
3648 * @param __lo A lower limit of arbitrary type.
3649 * @param __hi An upper limit of arbitrary type.
3650 * @param __comp A comparison functor.
3651 * @return max(__val, __lo, __comp) if __comp(__val, __hi)
3652 * or min(__val, __hi, __comp) otherwise.
3654 template<typename _Tp
, typename _Compare
>
3655 constexpr const _Tp
&
3656 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3658 __glibcxx_assert(!__comp(__hi
, __lo
));
3659 return __comp(__val
, __lo
) ? __lo
: __comp(__hi
, __val
) ? __hi
: __val
;
3664 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3666 * @brief Generate two uniformly distributed integers using a
3667 * single distribution invocation.
3668 * @param __b0 The upper bound for the first integer.
3669 * @param __b1 The upper bound for the second integer.
3670 * @param __g A UniformRandomBitGenerator.
3671 * @return A pair (i, j) with i and j uniformly distributed
3672 * over [0, __b0) and [0, __b1), respectively.
3674 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3676 * Using uniform_int_distribution with a range that is very
3677 * small relative to the range of the generator ends up wasting
3678 * potentially expensively generated randomness, since
3679 * uniform_int_distribution does not store leftover randomness
3680 * between invocations.
3682 * If we know we want two integers in ranges that are sufficiently
3683 * small, we can compose the ranges, use a single distribution
3684 * invocation, and significantly reduce the waste.
3686 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3687 pair
<_IntType
, _IntType
>
3688 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3689 _UniformRandomBitGenerator
&& __g
)
3692 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3693 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3697 * @brief Shuffle the elements of a sequence using a uniform random
3699 * @ingroup mutating_algorithms
3700 * @param __first A forward iterator.
3701 * @param __last A forward iterator.
3702 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3705 * Reorders the elements in the range @p [__first,__last) using @p __g to
3706 * provide random numbers.
3708 template<typename _RandomAccessIterator
,
3709 typename _UniformRandomNumberGenerator
>
3711 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3712 _UniformRandomNumberGenerator
&& __g
)
3714 // concept requirements
3715 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3716 _RandomAccessIterator
>)
3717 __glibcxx_requires_valid_range(__first
, __last
);
3719 if (__first
== __last
)
3722 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3725 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3726 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3727 typedef typename
__distr_type::param_type __p_type
;
3729 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3731 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3734 const __uc_type __urngrange
= __g
.max() - __g
.min();
3735 const __uc_type __urange
= __uc_type(__last
- __first
);
3737 if (__urngrange
/ __urange
>= __urange
)
3738 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3740 _RandomAccessIterator __i
= __first
+ 1;
3742 // Since we know the range isn't empty, an even number of elements
3743 // means an uneven number of elements /to swap/, in which case we
3744 // do the first one up front:
3746 if ((__urange
% 2) == 0)
3748 __distr_type __d
{0, 1};
3749 std::iter_swap(__i
++, __first
+ __d(__g
));
3752 // Now we know that __last - __i is even, so we do the rest in pairs,
3753 // using a single distribution invocation to produce swap positions
3754 // for two successive elements at a time:
3756 while (__i
!= __last
)
3758 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3760 const pair
<__uc_type
, __uc_type
> __pospos
=
3761 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3763 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3764 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3772 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3773 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3775 #endif // USE C99_STDINT
3779 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3782 * @brief Apply a function to every element of a sequence.
3783 * @ingroup non_mutating_algorithms
3784 * @param __first An input iterator.
3785 * @param __last An input iterator.
3786 * @param __f A unary function object.
3789 * Applies the function object @p __f to each element in the range
3790 * @p [first,last). @p __f must not modify the order of the sequence.
3791 * If @p __f has a return value it is ignored.
3793 template<typename _InputIterator
, typename _Function
>
3794 _GLIBCXX20_CONSTEXPR
3796 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3798 // concept requirements
3799 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3800 __glibcxx_requires_valid_range(__first
, __last
);
3801 for (; __first
!= __last
; ++__first
)
3803 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3806 #if __cplusplus >= 201703L
3808 * @brief Apply a function to every element of a sequence.
3809 * @ingroup non_mutating_algorithms
3810 * @param __first An input iterator.
3811 * @param __n A value convertible to an integer.
3812 * @param __f A unary function object.
3813 * @return `__first+__n`
3815 * Applies the function object `__f` to each element in the range
3816 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3817 * If `__f` has a return value it is ignored.
3819 template<typename _InputIterator
, typename _Size
, typename _Function
>
3820 _GLIBCXX20_CONSTEXPR
3822 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3824 auto __n2
= std::__size_to_integer(__n
);
3825 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3826 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3830 auto __last
= __first
+ __n2
;
3831 std::for_each(__first
, __last
, std::move(__f
));
3847 * @brief Find the first occurrence of a value in a sequence.
3848 * @ingroup non_mutating_algorithms
3849 * @param __first An input iterator.
3850 * @param __last An input iterator.
3851 * @param __val The value to find.
3852 * @return The first iterator @c i in the range @p [__first,__last)
3853 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3855 template<typename _InputIterator
, typename _Tp
>
3856 _GLIBCXX20_CONSTEXPR
3857 inline _InputIterator
3858 find(_InputIterator __first
, _InputIterator __last
,
3861 // concept requirements
3862 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3863 __glibcxx_function_requires(_EqualOpConcept
<
3864 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3865 __glibcxx_requires_valid_range(__first
, __last
);
3866 return std::__find_if(__first
, __last
,
3867 __gnu_cxx::__ops::__iter_equals_val(__val
));
3871 * @brief Find the first element in a sequence for which a
3872 * predicate is true.
3873 * @ingroup non_mutating_algorithms
3874 * @param __first An input iterator.
3875 * @param __last An input iterator.
3876 * @param __pred A predicate.
3877 * @return The first iterator @c i in the range @p [__first,__last)
3878 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3880 template<typename _InputIterator
, typename _Predicate
>
3881 _GLIBCXX20_CONSTEXPR
3882 inline _InputIterator
3883 find_if(_InputIterator __first
, _InputIterator __last
,
3886 // concept requirements
3887 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3888 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3889 typename iterator_traits
<_InputIterator
>::value_type
>)
3890 __glibcxx_requires_valid_range(__first
, __last
);
3892 return std::__find_if(__first
, __last
,
3893 __gnu_cxx::__ops::__pred_iter(__pred
));
3897 * @brief Find element from a set in a sequence.
3898 * @ingroup non_mutating_algorithms
3899 * @param __first1 Start of range to search.
3900 * @param __last1 End of range to search.
3901 * @param __first2 Start of match candidates.
3902 * @param __last2 End of match candidates.
3903 * @return The first iterator @c i in the range
3904 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3905 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3907 * Searches the range @p [__first1,__last1) for an element that is
3908 * equal to some element in the range [__first2,__last2). If
3909 * found, returns an iterator in the range [__first1,__last1),
3910 * otherwise returns @p __last1.
3912 template<typename _InputIterator
, typename _ForwardIterator
>
3913 _GLIBCXX20_CONSTEXPR
3915 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3916 _ForwardIterator __first2
, _ForwardIterator __last2
)
3918 // concept requirements
3919 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3920 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3921 __glibcxx_function_requires(_EqualOpConcept
<
3922 typename iterator_traits
<_InputIterator
>::value_type
,
3923 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3924 __glibcxx_requires_valid_range(__first1
, __last1
);
3925 __glibcxx_requires_valid_range(__first2
, __last2
);
3927 for (; __first1
!= __last1
; ++__first1
)
3928 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3929 if (*__first1
== *__iter
)
3935 * @brief Find element from a set in a sequence using a predicate.
3936 * @ingroup non_mutating_algorithms
3937 * @param __first1 Start of range to search.
3938 * @param __last1 End of range to search.
3939 * @param __first2 Start of match candidates.
3940 * @param __last2 End of match candidates.
3941 * @param __comp Predicate to use.
3942 * @return The first iterator @c i in the range
3943 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3944 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3945 * such iterator exists.
3948 * Searches the range @p [__first1,__last1) for an element that is
3949 * equal to some element in the range [__first2,__last2). If
3950 * found, returns an iterator in the range [__first1,__last1),
3951 * otherwise returns @p __last1.
3953 template<typename _InputIterator
, typename _ForwardIterator
,
3954 typename _BinaryPredicate
>
3955 _GLIBCXX20_CONSTEXPR
3957 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3958 _ForwardIterator __first2
, _ForwardIterator __last2
,
3959 _BinaryPredicate __comp
)
3961 // concept requirements
3962 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3963 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3964 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3965 typename iterator_traits
<_InputIterator
>::value_type
,
3966 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3967 __glibcxx_requires_valid_range(__first1
, __last1
);
3968 __glibcxx_requires_valid_range(__first2
, __last2
);
3970 for (; __first1
!= __last1
; ++__first1
)
3971 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3972 if (__comp(*__first1
, *__iter
))
3978 * @brief Find two adjacent values in a sequence that are equal.
3979 * @ingroup non_mutating_algorithms
3980 * @param __first A forward iterator.
3981 * @param __last A forward iterator.
3982 * @return The first iterator @c i such that @c i and @c i+1 are both
3983 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3984 * or @p __last if no such iterator exists.
3986 template<typename _ForwardIterator
>
3987 _GLIBCXX20_CONSTEXPR
3988 inline _ForwardIterator
3989 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3991 // concept requirements
3992 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3993 __glibcxx_function_requires(_EqualityComparableConcept
<
3994 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3995 __glibcxx_requires_valid_range(__first
, __last
);
3997 return std::__adjacent_find(__first
, __last
,
3998 __gnu_cxx::__ops::__iter_equal_to_iter());
4002 * @brief Find two adjacent values in a sequence using a predicate.
4003 * @ingroup non_mutating_algorithms
4004 * @param __first A forward iterator.
4005 * @param __last A forward iterator.
4006 * @param __binary_pred A binary predicate.
4007 * @return The first iterator @c i such that @c i and @c i+1 are both
4008 * valid iterators in @p [__first,__last) and such that
4009 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4012 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4013 _GLIBCXX20_CONSTEXPR
4014 inline _ForwardIterator
4015 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4016 _BinaryPredicate __binary_pred
)
4018 // concept requirements
4019 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4020 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4021 typename iterator_traits
<_ForwardIterator
>::value_type
,
4022 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4023 __glibcxx_requires_valid_range(__first
, __last
);
4025 return std::__adjacent_find(__first
, __last
,
4026 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4030 * @brief Count the number of copies of a value in a sequence.
4031 * @ingroup non_mutating_algorithms
4032 * @param __first An input iterator.
4033 * @param __last An input iterator.
4034 * @param __value The value to be counted.
4035 * @return The number of iterators @c i in the range @p [__first,__last)
4036 * for which @c *i == @p __value
4038 template<typename _InputIterator
, typename _Tp
>
4039 _GLIBCXX20_CONSTEXPR
4040 inline typename iterator_traits
<_InputIterator
>::difference_type
4041 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4043 // concept requirements
4044 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4045 __glibcxx_function_requires(_EqualOpConcept
<
4046 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4047 __glibcxx_requires_valid_range(__first
, __last
);
4049 return std::__count_if(__first
, __last
,
4050 __gnu_cxx::__ops::__iter_equals_val(__value
));
4054 * @brief Count the elements of a sequence for which a predicate is true.
4055 * @ingroup non_mutating_algorithms
4056 * @param __first An input iterator.
4057 * @param __last An input iterator.
4058 * @param __pred A predicate.
4059 * @return The number of iterators @c i in the range @p [__first,__last)
4060 * for which @p __pred(*i) is true.
4062 template<typename _InputIterator
, typename _Predicate
>
4063 _GLIBCXX20_CONSTEXPR
4064 inline typename iterator_traits
<_InputIterator
>::difference_type
4065 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4067 // concept requirements
4068 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4069 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4070 typename iterator_traits
<_InputIterator
>::value_type
>)
4071 __glibcxx_requires_valid_range(__first
, __last
);
4073 return std::__count_if(__first
, __last
,
4074 __gnu_cxx::__ops::__pred_iter(__pred
));
4078 * @brief Search a sequence for a matching sub-sequence.
4079 * @ingroup non_mutating_algorithms
4080 * @param __first1 A forward iterator.
4081 * @param __last1 A forward iterator.
4082 * @param __first2 A forward iterator.
4083 * @param __last2 A forward iterator.
4084 * @return The first iterator @c i in the range @p
4085 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4086 * *(__first2+N) for each @c N in the range @p
4087 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4089 * Searches the range @p [__first1,__last1) for a sub-sequence that
4090 * compares equal value-by-value with the sequence given by @p
4091 * [__first2,__last2) and returns an iterator to the first element
4092 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4095 * Because the sub-sequence must lie completely within the range @p
4096 * [__first1,__last1) it must start at a position less than @p
4097 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4098 * length of the sub-sequence.
4100 * This means that the returned iterator @c i will be in the range
4101 * @p [__first1,__last1-(__last2-__first2))
4103 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4104 _GLIBCXX20_CONSTEXPR
4105 inline _ForwardIterator1
4106 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4107 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4109 // concept requirements
4110 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4111 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4112 __glibcxx_function_requires(_EqualOpConcept
<
4113 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4114 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4115 __glibcxx_requires_valid_range(__first1
, __last1
);
4116 __glibcxx_requires_valid_range(__first2
, __last2
);
4118 return std::__search(__first1
, __last1
, __first2
, __last2
,
4119 __gnu_cxx::__ops::__iter_equal_to_iter());
4123 * @brief Search a sequence for a matching sub-sequence using a predicate.
4124 * @ingroup non_mutating_algorithms
4125 * @param __first1 A forward iterator.
4126 * @param __last1 A forward iterator.
4127 * @param __first2 A forward iterator.
4128 * @param __last2 A forward iterator.
4129 * @param __predicate A binary predicate.
4130 * @return The first iterator @c i in the range
4131 * @p [__first1,__last1-(__last2-__first2)) such that
4132 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4133 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4135 * Searches the range @p [__first1,__last1) for a sub-sequence that
4136 * compares equal value-by-value with the sequence given by @p
4137 * [__first2,__last2), using @p __predicate to determine equality,
4138 * and returns an iterator to the first element of the
4139 * sub-sequence, or @p __last1 if no such iterator exists.
4141 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4143 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4144 typename _BinaryPredicate
>
4145 _GLIBCXX20_CONSTEXPR
4146 inline _ForwardIterator1
4147 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4148 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4149 _BinaryPredicate __predicate
)
4151 // concept requirements
4152 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4153 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4154 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4155 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4156 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4157 __glibcxx_requires_valid_range(__first1
, __last1
);
4158 __glibcxx_requires_valid_range(__first2
, __last2
);
4160 return std::__search(__first1
, __last1
, __first2
, __last2
,
4161 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4165 * @brief Search a sequence for a number of consecutive values.
4166 * @ingroup non_mutating_algorithms
4167 * @param __first A forward iterator.
4168 * @param __last A forward iterator.
4169 * @param __count The number of consecutive values.
4170 * @param __val The value to find.
4171 * @return The first iterator @c i in the range @p
4172 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4173 * each @c N in the range @p [0,__count), or @p __last if no such
4176 * Searches the range @p [__first,__last) for @p count consecutive elements
4177 * equal to @p __val.
4179 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4180 _GLIBCXX20_CONSTEXPR
4181 inline _ForwardIterator
4182 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4183 _Integer __count
, const _Tp
& __val
)
4185 // concept requirements
4186 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4187 __glibcxx_function_requires(_EqualOpConcept
<
4188 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4189 __glibcxx_requires_valid_range(__first
, __last
);
4191 return std::__search_n(__first
, __last
, __count
,
4192 __gnu_cxx::__ops::__iter_equals_val(__val
));
4197 * @brief Search a sequence for a number of consecutive values using a
4199 * @ingroup non_mutating_algorithms
4200 * @param __first A forward iterator.
4201 * @param __last A forward iterator.
4202 * @param __count The number of consecutive values.
4203 * @param __val The value to find.
4204 * @param __binary_pred A binary predicate.
4205 * @return The first iterator @c i in the range @p
4206 * [__first,__last-__count) such that @p
4207 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4208 * @p [0,__count), or @p __last if no such iterator exists.
4210 * Searches the range @p [__first,__last) for @p __count
4211 * consecutive elements for which the predicate returns true.
4213 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4214 typename _BinaryPredicate
>
4215 _GLIBCXX20_CONSTEXPR
4216 inline _ForwardIterator
4217 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4218 _Integer __count
, const _Tp
& __val
,
4219 _BinaryPredicate __binary_pred
)
4221 // concept requirements
4222 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4223 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4224 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4225 __glibcxx_requires_valid_range(__first
, __last
);
4227 return std::__search_n(__first
, __last
, __count
,
4228 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4231 #if __cplusplus >= 201703L
4232 /** @brief Search a sequence using a Searcher object.
4234 * @param __first A forward iterator.
4235 * @param __last A forward iterator.
4236 * @param __searcher A callable object.
4237 * @return @p __searcher(__first,__last).first
4239 template<typename _ForwardIterator
, typename _Searcher
>
4240 _GLIBCXX20_CONSTEXPR
4241 inline _ForwardIterator
4242 search(_ForwardIterator __first
, _ForwardIterator __last
,
4243 const _Searcher
& __searcher
)
4244 { return __searcher(__first
, __last
).first
; }
4248 * @brief Perform an operation on a sequence.
4249 * @ingroup mutating_algorithms
4250 * @param __first An input iterator.
4251 * @param __last An input iterator.
4252 * @param __result An output iterator.
4253 * @param __unary_op A unary operator.
4254 * @return An output iterator equal to @p __result+(__last-__first).
4256 * Applies the operator to each element in the input range and assigns
4257 * the results to successive elements of the output sequence.
4258 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4259 * range @p [0,__last-__first).
4261 * @p unary_op must not alter its argument.
4263 template<typename _InputIterator
, typename _OutputIterator
,
4264 typename _UnaryOperation
>
4265 _GLIBCXX20_CONSTEXPR
4267 transform(_InputIterator __first
, _InputIterator __last
,
4268 _OutputIterator __result
, _UnaryOperation __unary_op
)
4270 // concept requirements
4271 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4272 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4273 // "the type returned by a _UnaryOperation"
4274 __typeof__(__unary_op(*__first
))>)
4275 __glibcxx_requires_valid_range(__first
, __last
);
4277 for (; __first
!= __last
; ++__first
, (void)++__result
)
4278 *__result
= __unary_op(*__first
);
4283 * @brief Perform an operation on corresponding elements of two sequences.
4284 * @ingroup mutating_algorithms
4285 * @param __first1 An input iterator.
4286 * @param __last1 An input iterator.
4287 * @param __first2 An input iterator.
4288 * @param __result An output iterator.
4289 * @param __binary_op A binary operator.
4290 * @return An output iterator equal to @p result+(last-first).
4292 * Applies the operator to the corresponding elements in the two
4293 * input ranges and assigns the results to successive elements of the
4296 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4297 * @c N in the range @p [0,__last1-__first1).
4299 * @p binary_op must not alter either of its arguments.
4301 template<typename _InputIterator1
, typename _InputIterator2
,
4302 typename _OutputIterator
, typename _BinaryOperation
>
4303 _GLIBCXX20_CONSTEXPR
4305 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4306 _InputIterator2 __first2
, _OutputIterator __result
,
4307 _BinaryOperation __binary_op
)
4309 // concept requirements
4310 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4311 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4312 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4313 // "the type returned by a _BinaryOperation"
4314 __typeof__(__binary_op(*__first1
,*__first2
))>)
4315 __glibcxx_requires_valid_range(__first1
, __last1
);
4317 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4318 *__result
= __binary_op(*__first1
, *__first2
);
4323 * @brief Replace each occurrence of one value in a sequence with another
4325 * @ingroup mutating_algorithms
4326 * @param __first A forward iterator.
4327 * @param __last A forward iterator.
4328 * @param __old_value The value to be replaced.
4329 * @param __new_value The replacement value.
4330 * @return replace() returns no value.
4332 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4333 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4335 template<typename _ForwardIterator
, typename _Tp
>
4336 _GLIBCXX20_CONSTEXPR
4338 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4339 const _Tp
& __old_value
, const _Tp
& __new_value
)
4341 // concept requirements
4342 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4344 __glibcxx_function_requires(_EqualOpConcept
<
4345 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4346 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4347 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4348 __glibcxx_requires_valid_range(__first
, __last
);
4350 for (; __first
!= __last
; ++__first
)
4351 if (*__first
== __old_value
)
4352 *__first
= __new_value
;
4356 * @brief Replace each value in a sequence for which a predicate returns
4357 * true with another value.
4358 * @ingroup mutating_algorithms
4359 * @param __first A forward iterator.
4360 * @param __last A forward iterator.
4361 * @param __pred A predicate.
4362 * @param __new_value The replacement value.
4363 * @return replace_if() returns no value.
4365 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4366 * is true then the assignment @c *i = @p __new_value is performed.
4368 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4369 _GLIBCXX20_CONSTEXPR
4371 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4372 _Predicate __pred
, const _Tp
& __new_value
)
4374 // concept requirements
4375 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4377 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4378 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4379 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4380 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4381 __glibcxx_requires_valid_range(__first
, __last
);
4383 for (; __first
!= __last
; ++__first
)
4384 if (__pred(*__first
))
4385 *__first
= __new_value
;
4389 * @brief Assign the result of a function object to each value in a
4391 * @ingroup mutating_algorithms
4392 * @param __first A forward iterator.
4393 * @param __last A forward iterator.
4394 * @param __gen A function object taking no arguments and returning
4395 * std::iterator_traits<_ForwardIterator>::value_type
4396 * @return generate() returns no value.
4398 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4399 * @p [__first,__last).
4401 template<typename _ForwardIterator
, typename _Generator
>
4402 _GLIBCXX20_CONSTEXPR
4404 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4407 // concept requirements
4408 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4409 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4410 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4411 __glibcxx_requires_valid_range(__first
, __last
);
4413 for (; __first
!= __last
; ++__first
)
4418 * @brief Assign the result of a function object to each value in a
4420 * @ingroup mutating_algorithms
4421 * @param __first A forward iterator.
4422 * @param __n The length of the sequence.
4423 * @param __gen A function object taking no arguments and returning
4424 * std::iterator_traits<_ForwardIterator>::value_type
4425 * @return The end of the sequence, @p __first+__n
4427 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4428 * @p [__first,__first+__n).
4430 * If @p __n is negative, the function does nothing and returns @p __first.
4432 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4433 // DR 865. More algorithms that throw away information
4434 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4435 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4436 _GLIBCXX20_CONSTEXPR
4438 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4440 // concept requirements
4441 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4442 // "the type returned by a _Generator"
4443 __typeof__(__gen())>)
4445 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4446 for (_IntSize __niter
= std::__size_to_integer(__n
);
4447 __niter
> 0; --__niter
, (void) ++__first
)
4453 * @brief Copy a sequence, removing consecutive duplicate values.
4454 * @ingroup mutating_algorithms
4455 * @param __first An input iterator.
4456 * @param __last An input iterator.
4457 * @param __result An output iterator.
4458 * @return An iterator designating the end of the resulting sequence.
4460 * Copies each element in the range @p [__first,__last) to the range
4461 * beginning at @p __result, except that only the first element is copied
4462 * from groups of consecutive elements that compare equal.
4463 * unique_copy() is stable, so the relative order of elements that are
4464 * copied is unchanged.
4466 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4467 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4469 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4470 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4473 template<typename _InputIterator
, typename _OutputIterator
>
4474 _GLIBCXX20_CONSTEXPR
4475 inline _OutputIterator
4476 unique_copy(_InputIterator __first
, _InputIterator __last
,
4477 _OutputIterator __result
)
4479 // concept requirements
4480 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4481 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4482 typename iterator_traits
<_InputIterator
>::value_type
>)
4483 __glibcxx_function_requires(_EqualityComparableConcept
<
4484 typename iterator_traits
<_InputIterator
>::value_type
>)
4485 __glibcxx_requires_valid_range(__first
, __last
);
4487 if (__first
== __last
)
4489 return std::__unique_copy(__first
, __last
, __result
,
4490 __gnu_cxx::__ops::__iter_equal_to_iter(),
4491 std::__iterator_category(__first
),
4492 std::__iterator_category(__result
));
4496 * @brief Copy a sequence, removing consecutive values using a predicate.
4497 * @ingroup mutating_algorithms
4498 * @param __first An input iterator.
4499 * @param __last An input iterator.
4500 * @param __result An output iterator.
4501 * @param __binary_pred A binary predicate.
4502 * @return An iterator designating the end of the resulting sequence.
4504 * Copies each element in the range @p [__first,__last) to the range
4505 * beginning at @p __result, except that only the first element is copied
4506 * from groups of consecutive elements for which @p __binary_pred returns
4508 * unique_copy() is stable, so the relative order of elements that are
4509 * copied is unchanged.
4511 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4512 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4514 template<typename _InputIterator
, typename _OutputIterator
,
4515 typename _BinaryPredicate
>
4516 _GLIBCXX20_CONSTEXPR
4517 inline _OutputIterator
4518 unique_copy(_InputIterator __first
, _InputIterator __last
,
4519 _OutputIterator __result
,
4520 _BinaryPredicate __binary_pred
)
4522 // concept requirements -- predicates checked later
4523 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4524 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4525 typename iterator_traits
<_InputIterator
>::value_type
>)
4526 __glibcxx_requires_valid_range(__first
, __last
);
4528 if (__first
== __last
)
4530 return std::__unique_copy(__first
, __last
, __result
,
4531 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4532 std::__iterator_category(__first
),
4533 std::__iterator_category(__result
));
4536 #if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4539 * @brief Randomly shuffle the elements of a sequence.
4540 * @ingroup mutating_algorithms
4541 * @param __first A forward iterator.
4542 * @param __last A forward iterator.
4545 * Reorder the elements in the range @p [__first,__last) using a random
4546 * distribution, so that every possible ordering of the sequence is
4550 * Since C++14 `std::random_shuffle` is not part of the C++ standard.
4551 * Use `std::shuffle` instead, which was introduced in C++11.
4553 template<typename _RandomAccessIterator
>
4554 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4556 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4558 // concept requirements
4559 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4560 _RandomAccessIterator
>)
4561 __glibcxx_requires_valid_range(__first
, __last
);
4563 if (__first
!= __last
)
4564 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4566 // XXX rand() % N is not uniformly distributed
4567 _RandomAccessIterator __j
= __first
4568 + std::rand() % ((__i
- __first
) + 1);
4570 std::iter_swap(__i
, __j
);
4576 * @brief Shuffle the elements of a sequence using a random number
4578 * @ingroup mutating_algorithms
4579 * @param __first A forward iterator.
4580 * @param __last A forward iterator.
4581 * @param __rand The RNG functor or function.
4584 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4585 * provide a random distribution. Calling @p __rand(N) for a positive
4586 * integer @p N should return a randomly chosen integer from the
4590 * Since C++14 `std::random_shuffle` is not part of the C++ standard.
4591 * Use `std::shuffle` instead, which was introduced in C++11.
4593 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4594 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4596 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4597 #if __cplusplus >= 201103L
4598 _RandomNumberGenerator
&& __rand
)
4600 _RandomNumberGenerator
& __rand
)
4603 // concept requirements
4604 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4605 _RandomAccessIterator
>)
4606 __glibcxx_requires_valid_range(__first
, __last
);
4608 if (__first
== __last
)
4610 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4612 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4614 std::iter_swap(__i
, __j
);
4617 #endif // C++11 || USE_DEPRECATED
4620 * @brief Move elements for which a predicate is true to the beginning
4622 * @ingroup mutating_algorithms
4623 * @param __first A forward iterator.
4624 * @param __last A forward iterator.
4625 * @param __pred A predicate functor.
4626 * @return An iterator @p middle such that @p __pred(i) is true for each
4627 * iterator @p i in the range @p [__first,middle) and false for each @p i
4628 * in the range @p [middle,__last).
4630 * @p __pred must not modify its operand. @p partition() does not preserve
4631 * the relative ordering of elements in each group, use
4632 * @p stable_partition() if this is needed.
4634 template<typename _ForwardIterator
, typename _Predicate
>
4635 _GLIBCXX20_CONSTEXPR
4636 inline _ForwardIterator
4637 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4640 // concept requirements
4641 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4643 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4644 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4645 __glibcxx_requires_valid_range(__first
, __last
);
4647 return std::__partition(__first
, __last
, __pred
,
4648 std::__iterator_category(__first
));
4653 * @brief Sort the smallest elements of a sequence.
4654 * @ingroup sorting_algorithms
4655 * @param __first An iterator.
4656 * @param __middle Another iterator.
4657 * @param __last Another iterator.
4660 * Sorts the smallest @p (__middle-__first) elements in the range
4661 * @p [first,last) and moves them to the range @p [__first,__middle). The
4662 * order of the remaining elements in the range @p [__middle,__last) is
4664 * After the sort if @e i and @e j are iterators in the range
4665 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4666 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4668 template<typename _RandomAccessIterator
>
4669 _GLIBCXX20_CONSTEXPR
4671 partial_sort(_RandomAccessIterator __first
,
4672 _RandomAccessIterator __middle
,
4673 _RandomAccessIterator __last
)
4675 // concept requirements
4676 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4677 _RandomAccessIterator
>)
4678 __glibcxx_function_requires(_LessThanComparableConcept
<
4679 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4680 __glibcxx_requires_valid_range(__first
, __middle
);
4681 __glibcxx_requires_valid_range(__middle
, __last
);
4682 __glibcxx_requires_irreflexive(__first
, __last
);
4684 std::__partial_sort(__first
, __middle
, __last
,
4685 __gnu_cxx::__ops::__iter_less_iter());
4689 * @brief Sort the smallest elements of a sequence using a predicate
4691 * @ingroup sorting_algorithms
4692 * @param __first An iterator.
4693 * @param __middle Another iterator.
4694 * @param __last Another iterator.
4695 * @param __comp A comparison functor.
4698 * Sorts the smallest @p (__middle-__first) elements in the range
4699 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4700 * order of the remaining elements in the range @p [__middle,__last) is
4702 * After the sort if @e i and @e j are iterators in the range
4703 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4704 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4707 template<typename _RandomAccessIterator
, typename _Compare
>
4708 _GLIBCXX20_CONSTEXPR
4710 partial_sort(_RandomAccessIterator __first
,
4711 _RandomAccessIterator __middle
,
4712 _RandomAccessIterator __last
,
4715 // concept requirements
4716 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4717 _RandomAccessIterator
>)
4718 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4719 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4720 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4721 __glibcxx_requires_valid_range(__first
, __middle
);
4722 __glibcxx_requires_valid_range(__middle
, __last
);
4723 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4725 std::__partial_sort(__first
, __middle
, __last
,
4726 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4730 * @brief Sort a sequence just enough to find a particular position.
4731 * @ingroup sorting_algorithms
4732 * @param __first An iterator.
4733 * @param __nth Another iterator.
4734 * @param __last Another iterator.
4737 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4738 * is the same element that would have been in that position had the
4739 * whole sequence been sorted. The elements either side of @p *__nth are
4740 * not completely sorted, but for any iterator @e i in the range
4741 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4742 * holds that *j < *i is false.
4744 template<typename _RandomAccessIterator
>
4745 _GLIBCXX20_CONSTEXPR
4747 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4748 _RandomAccessIterator __last
)
4750 // concept requirements
4751 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4752 _RandomAccessIterator
>)
4753 __glibcxx_function_requires(_LessThanComparableConcept
<
4754 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4755 __glibcxx_requires_valid_range(__first
, __nth
);
4756 __glibcxx_requires_valid_range(__nth
, __last
);
4757 __glibcxx_requires_irreflexive(__first
, __last
);
4759 if (__first
== __last
|| __nth
== __last
)
4762 std::__introselect(__first
, __nth
, __last
,
4763 std::__lg(__last
- __first
) * 2,
4764 __gnu_cxx::__ops::__iter_less_iter());
4768 * @brief Sort a sequence just enough to find a particular position
4769 * using a predicate for comparison.
4770 * @ingroup sorting_algorithms
4771 * @param __first An iterator.
4772 * @param __nth Another iterator.
4773 * @param __last Another iterator.
4774 * @param __comp A comparison functor.
4777 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4778 * is the same element that would have been in that position had the
4779 * whole sequence been sorted. The elements either side of @p *__nth are
4780 * not completely sorted, but for any iterator @e i in the range
4781 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4782 * holds that @p __comp(*j,*i) is false.
4784 template<typename _RandomAccessIterator
, typename _Compare
>
4785 _GLIBCXX20_CONSTEXPR
4787 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4788 _RandomAccessIterator __last
, _Compare __comp
)
4790 // concept requirements
4791 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4792 _RandomAccessIterator
>)
4793 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4794 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4795 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4796 __glibcxx_requires_valid_range(__first
, __nth
);
4797 __glibcxx_requires_valid_range(__nth
, __last
);
4798 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4800 if (__first
== __last
|| __nth
== __last
)
4803 std::__introselect(__first
, __nth
, __last
,
4804 std::__lg(__last
- __first
) * 2,
4805 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4809 * @brief Sort the elements of a sequence.
4810 * @ingroup sorting_algorithms
4811 * @param __first An iterator.
4812 * @param __last Another iterator.
4815 * Sorts the elements in the range @p [__first,__last) in ascending order,
4816 * such that for each iterator @e i in the range @p [__first,__last-1),
4817 * *(i+1)<*i is false.
4819 * The relative ordering of equivalent elements is not preserved, use
4820 * @p stable_sort() if this is needed.
4822 template<typename _RandomAccessIterator
>
4823 _GLIBCXX20_CONSTEXPR
4825 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4827 // concept requirements
4828 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4829 _RandomAccessIterator
>)
4830 __glibcxx_function_requires(_LessThanComparableConcept
<
4831 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4832 __glibcxx_requires_valid_range(__first
, __last
);
4833 __glibcxx_requires_irreflexive(__first
, __last
);
4835 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4839 * @brief Sort the elements of a sequence using a predicate for comparison.
4840 * @ingroup sorting_algorithms
4841 * @param __first An iterator.
4842 * @param __last Another iterator.
4843 * @param __comp A comparison functor.
4846 * Sorts the elements in the range @p [__first,__last) in ascending order,
4847 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4848 * range @p [__first,__last-1).
4850 * The relative ordering of equivalent elements is not preserved, use
4851 * @p stable_sort() if this is needed.
4853 template<typename _RandomAccessIterator
, typename _Compare
>
4854 _GLIBCXX20_CONSTEXPR
4856 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4859 // concept requirements
4860 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4861 _RandomAccessIterator
>)
4862 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4863 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4864 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4865 __glibcxx_requires_valid_range(__first
, __last
);
4866 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4868 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4871 template<typename _InputIterator1
, typename _InputIterator2
,
4872 typename _OutputIterator
, typename _Compare
>
4873 _GLIBCXX20_CONSTEXPR
4875 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4876 _InputIterator2 __first2
, _InputIterator2 __last2
,
4877 _OutputIterator __result
, _Compare __comp
)
4879 while (__first1
!= __last1
&& __first2
!= __last2
)
4881 if (__comp(__first2
, __first1
))
4883 *__result
= *__first2
;
4888 *__result
= *__first1
;
4893 return std::copy(__first2
, __last2
,
4894 std::copy(__first1
, __last1
, __result
));
4898 * @brief Merges two sorted ranges.
4899 * @ingroup sorting_algorithms
4900 * @param __first1 An iterator.
4901 * @param __first2 Another iterator.
4902 * @param __last1 Another iterator.
4903 * @param __last2 Another iterator.
4904 * @param __result An iterator pointing to the end of the merged range.
4905 * @return An output iterator equal to @p __result + (__last1 - __first1)
4906 * + (__last2 - __first2).
4908 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4909 * the sorted range @p [__result, __result + (__last1-__first1) +
4910 * (__last2-__first2)). Both input ranges must be sorted, and the
4911 * output range must not overlap with either of the input ranges.
4912 * The sort is @e stable, that is, for equivalent elements in the
4913 * two ranges, elements from the first range will always come
4914 * before elements from the second.
4916 template<typename _InputIterator1
, typename _InputIterator2
,
4917 typename _OutputIterator
>
4918 _GLIBCXX20_CONSTEXPR
4919 inline _OutputIterator
4920 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4921 _InputIterator2 __first2
, _InputIterator2 __last2
,
4922 _OutputIterator __result
)
4924 // concept requirements
4925 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4926 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4927 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4928 typename iterator_traits
<_InputIterator1
>::value_type
>)
4929 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4930 typename iterator_traits
<_InputIterator2
>::value_type
>)
4931 __glibcxx_function_requires(_LessThanOpConcept
<
4932 typename iterator_traits
<_InputIterator2
>::value_type
,
4933 typename iterator_traits
<_InputIterator1
>::value_type
>)
4934 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4935 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4936 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4937 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4939 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4940 __first2
, __last2
, __result
,
4941 __gnu_cxx::__ops::__iter_less_iter());
4945 * @brief Merges two sorted ranges.
4946 * @ingroup sorting_algorithms
4947 * @param __first1 An iterator.
4948 * @param __first2 Another iterator.
4949 * @param __last1 Another iterator.
4950 * @param __last2 Another iterator.
4951 * @param __result An iterator pointing to the end of the merged range.
4952 * @param __comp A functor to use for comparisons.
4953 * @return An output iterator equal to @p __result + (__last1 - __first1)
4954 * + (__last2 - __first2).
4956 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4957 * the sorted range @p [__result, __result + (__last1-__first1) +
4958 * (__last2-__first2)). Both input ranges must be sorted, and the
4959 * output range must not overlap with either of the input ranges.
4960 * The sort is @e stable, that is, for equivalent elements in the
4961 * two ranges, elements from the first range will always come
4962 * before elements from the second.
4964 * The comparison function should have the same effects on ordering as
4965 * the function used for the initial sort.
4967 template<typename _InputIterator1
, typename _InputIterator2
,
4968 typename _OutputIterator
, typename _Compare
>
4969 _GLIBCXX20_CONSTEXPR
4970 inline _OutputIterator
4971 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4972 _InputIterator2 __first2
, _InputIterator2 __last2
,
4973 _OutputIterator __result
, _Compare __comp
)
4975 // concept requirements
4976 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4977 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4978 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4979 typename iterator_traits
<_InputIterator1
>::value_type
>)
4980 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4981 typename iterator_traits
<_InputIterator2
>::value_type
>)
4982 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4983 typename iterator_traits
<_InputIterator2
>::value_type
,
4984 typename iterator_traits
<_InputIterator1
>::value_type
>)
4985 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4986 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4987 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
4988 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
4990 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4991 __first2
, __last2
, __result
,
4992 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4995 template<typename _RandomAccessIterator
, typename _Compare
>
4997 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5000 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5002 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5004 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5006 if (__first
== __last
)
5009 // __stable_sort_adaptive sorts the range in two halves,
5010 // so the buffer only needs to fit half the range at once.
5011 _TmpBuf
__buf(__first
, (__last
- __first
+ 1) / 2);
5013 if (__buf
.begin() == 0)
5014 std::__inplace_stable_sort(__first
, __last
, __comp
);
5016 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5017 _DistanceType(__buf
.size()), __comp
);
5021 * @brief Sort the elements of a sequence, preserving the relative order
5022 * of equivalent elements.
5023 * @ingroup sorting_algorithms
5024 * @param __first An iterator.
5025 * @param __last Another iterator.
5028 * Sorts the elements in the range @p [__first,__last) in ascending order,
5029 * such that for each iterator @p i in the range @p [__first,__last-1),
5030 * @p *(i+1)<*i is false.
5032 * The relative ordering of equivalent elements is preserved, so any two
5033 * elements @p x and @p y in the range @p [__first,__last) such that
5034 * @p x<y is false and @p y<x is false will have the same relative
5035 * ordering after calling @p stable_sort().
5037 template<typename _RandomAccessIterator
>
5039 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5041 // concept requirements
5042 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5043 _RandomAccessIterator
>)
5044 __glibcxx_function_requires(_LessThanComparableConcept
<
5045 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5046 __glibcxx_requires_valid_range(__first
, __last
);
5047 __glibcxx_requires_irreflexive(__first
, __last
);
5049 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5050 __gnu_cxx::__ops::__iter_less_iter());
5054 * @brief Sort the elements of a sequence using a predicate for comparison,
5055 * preserving the relative order of equivalent elements.
5056 * @ingroup sorting_algorithms
5057 * @param __first An iterator.
5058 * @param __last Another iterator.
5059 * @param __comp A comparison functor.
5062 * Sorts the elements in the range @p [__first,__last) in ascending order,
5063 * such that for each iterator @p i in the range @p [__first,__last-1),
5064 * @p __comp(*(i+1),*i) is false.
5066 * The relative ordering of equivalent elements is preserved, so any two
5067 * elements @p x and @p y in the range @p [__first,__last) such that
5068 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5069 * relative ordering after calling @p stable_sort().
5071 template<typename _RandomAccessIterator
, typename _Compare
>
5073 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5076 // concept requirements
5077 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5078 _RandomAccessIterator
>)
5079 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5080 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5081 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5082 __glibcxx_requires_valid_range(__first
, __last
);
5083 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5085 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5086 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5089 template<typename _InputIterator1
, typename _InputIterator2
,
5090 typename _OutputIterator
,
5092 _GLIBCXX20_CONSTEXPR
5094 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5095 _InputIterator2 __first2
, _InputIterator2 __last2
,
5096 _OutputIterator __result
, _Compare __comp
)
5098 while (__first1
!= __last1
&& __first2
!= __last2
)
5100 if (__comp(__first1
, __first2
))
5102 *__result
= *__first1
;
5105 else if (__comp(__first2
, __first1
))
5107 *__result
= *__first2
;
5112 *__result
= *__first1
;
5118 return std::copy(__first2
, __last2
,
5119 std::copy(__first1
, __last1
, __result
));
5123 * @brief Return the union of two sorted ranges.
5124 * @ingroup set_algorithms
5125 * @param __first1 Start of first range.
5126 * @param __last1 End of first range.
5127 * @param __first2 Start of second range.
5128 * @param __last2 End of second range.
5129 * @param __result Start of output range.
5130 * @return End of the output range.
5131 * @ingroup set_algorithms
5133 * This operation iterates over both ranges, copying elements present in
5134 * each range in order to the output range. Iterators increment for each
5135 * range. When the current element of one range is less than the other,
5136 * that element is copied and the iterator advanced. If an element is
5137 * contained in both ranges, the element from the first range is copied and
5138 * both ranges advance. The output range may not overlap either input
5141 template<typename _InputIterator1
, typename _InputIterator2
,
5142 typename _OutputIterator
>
5143 _GLIBCXX20_CONSTEXPR
5144 inline _OutputIterator
5145 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5146 _InputIterator2 __first2
, _InputIterator2 __last2
,
5147 _OutputIterator __result
)
5149 // concept requirements
5150 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5151 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5152 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5153 typename iterator_traits
<_InputIterator1
>::value_type
>)
5154 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5155 typename iterator_traits
<_InputIterator2
>::value_type
>)
5156 __glibcxx_function_requires(_LessThanOpConcept
<
5157 typename iterator_traits
<_InputIterator1
>::value_type
,
5158 typename iterator_traits
<_InputIterator2
>::value_type
>)
5159 __glibcxx_function_requires(_LessThanOpConcept
<
5160 typename iterator_traits
<_InputIterator2
>::value_type
,
5161 typename iterator_traits
<_InputIterator1
>::value_type
>)
5162 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5163 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5164 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5165 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5167 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5168 __first2
, __last2
, __result
,
5169 __gnu_cxx::__ops::__iter_less_iter());
5173 * @brief Return the union of two sorted ranges using a comparison functor.
5174 * @ingroup set_algorithms
5175 * @param __first1 Start of first range.
5176 * @param __last1 End of first range.
5177 * @param __first2 Start of second range.
5178 * @param __last2 End of second range.
5179 * @param __result Start of output range.
5180 * @param __comp The comparison functor.
5181 * @return End of the output range.
5182 * @ingroup set_algorithms
5184 * This operation iterates over both ranges, copying elements present in
5185 * each range in order to the output range. Iterators increment for each
5186 * range. When the current element of one range is less than the other
5187 * according to @p __comp, that element is copied and the iterator advanced.
5188 * If an equivalent element according to @p __comp is contained in both
5189 * ranges, the element from the first range is copied and both ranges
5190 * advance. The output range may not overlap either input range.
5192 template<typename _InputIterator1
, typename _InputIterator2
,
5193 typename _OutputIterator
, typename _Compare
>
5194 _GLIBCXX20_CONSTEXPR
5195 inline _OutputIterator
5196 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5197 _InputIterator2 __first2
, _InputIterator2 __last2
,
5198 _OutputIterator __result
, _Compare __comp
)
5200 // concept requirements
5201 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5202 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5203 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5204 typename iterator_traits
<_InputIterator1
>::value_type
>)
5205 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5206 typename iterator_traits
<_InputIterator2
>::value_type
>)
5207 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5208 typename iterator_traits
<_InputIterator1
>::value_type
,
5209 typename iterator_traits
<_InputIterator2
>::value_type
>)
5210 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5211 typename iterator_traits
<_InputIterator2
>::value_type
,
5212 typename iterator_traits
<_InputIterator1
>::value_type
>)
5213 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5214 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5215 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5216 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5218 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5219 __first2
, __last2
, __result
,
5220 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5223 template<typename _InputIterator1
, typename _InputIterator2
,
5224 typename _OutputIterator
,
5226 _GLIBCXX20_CONSTEXPR
5228 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5229 _InputIterator2 __first2
, _InputIterator2 __last2
,
5230 _OutputIterator __result
, _Compare __comp
)
5232 while (__first1
!= __last1
&& __first2
!= __last2
)
5233 if (__comp(__first1
, __first2
))
5235 else if (__comp(__first2
, __first1
))
5239 *__result
= *__first1
;
5248 * @brief Return the intersection of two sorted ranges.
5249 * @ingroup set_algorithms
5250 * @param __first1 Start of first range.
5251 * @param __last1 End of first range.
5252 * @param __first2 Start of second range.
5253 * @param __last2 End of second range.
5254 * @param __result Start of output range.
5255 * @return End of the output range.
5256 * @ingroup set_algorithms
5258 * This operation iterates over both ranges, copying elements present in
5259 * both ranges in order to the output range. Iterators increment for each
5260 * range. When the current element of one range is less than the other,
5261 * that iterator advances. If an element is contained in both ranges, the
5262 * element from the first range is copied and both ranges advance. The
5263 * output range may not overlap either input range.
5265 template<typename _InputIterator1
, typename _InputIterator2
,
5266 typename _OutputIterator
>
5267 _GLIBCXX20_CONSTEXPR
5268 inline _OutputIterator
5269 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5270 _InputIterator2 __first2
, _InputIterator2 __last2
,
5271 _OutputIterator __result
)
5273 // concept requirements
5274 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5275 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5276 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5277 typename iterator_traits
<_InputIterator1
>::value_type
>)
5278 __glibcxx_function_requires(_LessThanOpConcept
<
5279 typename iterator_traits
<_InputIterator1
>::value_type
,
5280 typename iterator_traits
<_InputIterator2
>::value_type
>)
5281 __glibcxx_function_requires(_LessThanOpConcept
<
5282 typename iterator_traits
<_InputIterator2
>::value_type
,
5283 typename iterator_traits
<_InputIterator1
>::value_type
>)
5284 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5285 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5286 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5287 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5289 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5290 __first2
, __last2
, __result
,
5291 __gnu_cxx::__ops::__iter_less_iter());
5295 * @brief Return the intersection of two sorted ranges using comparison
5297 * @ingroup set_algorithms
5298 * @param __first1 Start of first range.
5299 * @param __last1 End of first range.
5300 * @param __first2 Start of second range.
5301 * @param __last2 End of second range.
5302 * @param __result Start of output range.
5303 * @param __comp The comparison functor.
5304 * @return End of the output range.
5305 * @ingroup set_algorithms
5307 * This operation iterates over both ranges, copying elements present in
5308 * both ranges in order to the output range. Iterators increment for each
5309 * range. When the current element of one range is less than the other
5310 * according to @p __comp, that iterator advances. If an element is
5311 * contained in both ranges according to @p __comp, the element from the
5312 * first range is copied and both ranges advance. The output range may not
5313 * overlap either input range.
5315 template<typename _InputIterator1
, typename _InputIterator2
,
5316 typename _OutputIterator
, typename _Compare
>
5317 _GLIBCXX20_CONSTEXPR
5318 inline _OutputIterator
5319 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5320 _InputIterator2 __first2
, _InputIterator2 __last2
,
5321 _OutputIterator __result
, _Compare __comp
)
5323 // concept requirements
5324 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5325 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5326 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5327 typename iterator_traits
<_InputIterator1
>::value_type
>)
5328 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5329 typename iterator_traits
<_InputIterator1
>::value_type
,
5330 typename iterator_traits
<_InputIterator2
>::value_type
>)
5331 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5332 typename iterator_traits
<_InputIterator2
>::value_type
,
5333 typename iterator_traits
<_InputIterator1
>::value_type
>)
5334 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5335 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5336 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5337 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5339 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5340 __first2
, __last2
, __result
,
5341 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5344 template<typename _InputIterator1
, typename _InputIterator2
,
5345 typename _OutputIterator
,
5347 _GLIBCXX20_CONSTEXPR
5349 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5350 _InputIterator2 __first2
, _InputIterator2 __last2
,
5351 _OutputIterator __result
, _Compare __comp
)
5353 while (__first1
!= __last1
&& __first2
!= __last2
)
5354 if (__comp(__first1
, __first2
))
5356 *__result
= *__first1
;
5360 else if (__comp(__first2
, __first1
))
5367 return std::copy(__first1
, __last1
, __result
);
5371 * @brief Return the difference of two sorted ranges.
5372 * @ingroup set_algorithms
5373 * @param __first1 Start of first range.
5374 * @param __last1 End of first range.
5375 * @param __first2 Start of second range.
5376 * @param __last2 End of second range.
5377 * @param __result Start of output range.
5378 * @return End of the output range.
5379 * @ingroup set_algorithms
5381 * This operation iterates over both ranges, copying elements present in
5382 * the first range but not the second in order to the output range.
5383 * Iterators increment for each range. When the current element of the
5384 * first range is less than the second, that element is copied and the
5385 * iterator advances. If the current element of the second range is less,
5386 * the iterator advances, but no element is copied. If an element is
5387 * contained in both ranges, no elements are copied and both ranges
5388 * advance. The output range may not overlap either input range.
5390 template<typename _InputIterator1
, typename _InputIterator2
,
5391 typename _OutputIterator
>
5392 _GLIBCXX20_CONSTEXPR
5393 inline _OutputIterator
5394 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5395 _InputIterator2 __first2
, _InputIterator2 __last2
,
5396 _OutputIterator __result
)
5398 // concept requirements
5399 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5400 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5401 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5402 typename iterator_traits
<_InputIterator1
>::value_type
>)
5403 __glibcxx_function_requires(_LessThanOpConcept
<
5404 typename iterator_traits
<_InputIterator1
>::value_type
,
5405 typename iterator_traits
<_InputIterator2
>::value_type
>)
5406 __glibcxx_function_requires(_LessThanOpConcept
<
5407 typename iterator_traits
<_InputIterator2
>::value_type
,
5408 typename iterator_traits
<_InputIterator1
>::value_type
>)
5409 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5410 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5411 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5412 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5414 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5415 __first2
, __last2
, __result
,
5416 __gnu_cxx::__ops::__iter_less_iter());
5420 * @brief Return the difference of two sorted ranges using comparison
5422 * @ingroup set_algorithms
5423 * @param __first1 Start of first range.
5424 * @param __last1 End of first range.
5425 * @param __first2 Start of second range.
5426 * @param __last2 End of second range.
5427 * @param __result Start of output range.
5428 * @param __comp The comparison functor.
5429 * @return End of the output range.
5430 * @ingroup set_algorithms
5432 * This operation iterates over both ranges, copying elements present in
5433 * the first range but not the second in order to the output range.
5434 * Iterators increment for each range. When the current element of the
5435 * first range is less than the second according to @p __comp, that element
5436 * is copied and the iterator advances. If the current element of the
5437 * second range is less, no element is copied and the iterator advances.
5438 * If an element is contained in both ranges according to @p __comp, no
5439 * elements are copied and both ranges advance. The output range may not
5440 * overlap either input range.
5442 template<typename _InputIterator1
, typename _InputIterator2
,
5443 typename _OutputIterator
, typename _Compare
>
5444 _GLIBCXX20_CONSTEXPR
5445 inline _OutputIterator
5446 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5447 _InputIterator2 __first2
, _InputIterator2 __last2
,
5448 _OutputIterator __result
, _Compare __comp
)
5450 // concept requirements
5451 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5452 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5453 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5454 typename iterator_traits
<_InputIterator1
>::value_type
>)
5455 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5456 typename iterator_traits
<_InputIterator1
>::value_type
,
5457 typename iterator_traits
<_InputIterator2
>::value_type
>)
5458 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5459 typename iterator_traits
<_InputIterator2
>::value_type
,
5460 typename iterator_traits
<_InputIterator1
>::value_type
>)
5461 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5462 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5463 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5464 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5466 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5467 __first2
, __last2
, __result
,
5468 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5471 template<typename _InputIterator1
, typename _InputIterator2
,
5472 typename _OutputIterator
,
5474 _GLIBCXX20_CONSTEXPR
5476 __set_symmetric_difference(_InputIterator1 __first1
,
5477 _InputIterator1 __last1
,
5478 _InputIterator2 __first2
,
5479 _InputIterator2 __last2
,
5480 _OutputIterator __result
,
5483 while (__first1
!= __last1
&& __first2
!= __last2
)
5484 if (__comp(__first1
, __first2
))
5486 *__result
= *__first1
;
5490 else if (__comp(__first2
, __first1
))
5492 *__result
= *__first2
;
5501 return std::copy(__first2
, __last2
,
5502 std::copy(__first1
, __last1
, __result
));
5506 * @brief Return the symmetric difference of two sorted ranges.
5507 * @ingroup set_algorithms
5508 * @param __first1 Start of first range.
5509 * @param __last1 End of first range.
5510 * @param __first2 Start of second range.
5511 * @param __last2 End of second range.
5512 * @param __result Start of output range.
5513 * @return End of the output range.
5514 * @ingroup set_algorithms
5516 * This operation iterates over both ranges, copying elements present in
5517 * one range but not the other in order to the output range. Iterators
5518 * increment for each range. When the current element of one range is less
5519 * than the other, that element is copied and the iterator advances. If an
5520 * element is contained in both ranges, no elements are copied and both
5521 * ranges advance. The output range may not overlap either input range.
5523 template<typename _InputIterator1
, typename _InputIterator2
,
5524 typename _OutputIterator
>
5525 _GLIBCXX20_CONSTEXPR
5526 inline _OutputIterator
5527 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5528 _InputIterator2 __first2
, _InputIterator2 __last2
,
5529 _OutputIterator __result
)
5531 // concept requirements
5532 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5533 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5534 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5535 typename iterator_traits
<_InputIterator1
>::value_type
>)
5536 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5537 typename iterator_traits
<_InputIterator2
>::value_type
>)
5538 __glibcxx_function_requires(_LessThanOpConcept
<
5539 typename iterator_traits
<_InputIterator1
>::value_type
,
5540 typename iterator_traits
<_InputIterator2
>::value_type
>)
5541 __glibcxx_function_requires(_LessThanOpConcept
<
5542 typename iterator_traits
<_InputIterator2
>::value_type
,
5543 typename iterator_traits
<_InputIterator1
>::value_type
>)
5544 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5545 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5546 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5547 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5549 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5550 __first2
, __last2
, __result
,
5551 __gnu_cxx::__ops::__iter_less_iter());
5555 * @brief Return the symmetric difference of two sorted ranges using
5556 * comparison functor.
5557 * @ingroup set_algorithms
5558 * @param __first1 Start of first range.
5559 * @param __last1 End of first range.
5560 * @param __first2 Start of second range.
5561 * @param __last2 End of second range.
5562 * @param __result Start of output range.
5563 * @param __comp The comparison functor.
5564 * @return End of the output range.
5565 * @ingroup set_algorithms
5567 * This operation iterates over both ranges, copying elements present in
5568 * one range but not the other in order to the output range. Iterators
5569 * increment for each range. When the current element of one range is less
5570 * than the other according to @p comp, that element is copied and the
5571 * iterator advances. If an element is contained in both ranges according
5572 * to @p __comp, no elements are copied and both ranges advance. The output
5573 * range may not overlap either input range.
5575 template<typename _InputIterator1
, typename _InputIterator2
,
5576 typename _OutputIterator
, typename _Compare
>
5577 _GLIBCXX20_CONSTEXPR
5578 inline _OutputIterator
5579 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5580 _InputIterator2 __first2
, _InputIterator2 __last2
,
5581 _OutputIterator __result
,
5584 // concept requirements
5585 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5586 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5587 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5588 typename iterator_traits
<_InputIterator1
>::value_type
>)
5589 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5590 typename iterator_traits
<_InputIterator2
>::value_type
>)
5591 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5592 typename iterator_traits
<_InputIterator1
>::value_type
,
5593 typename iterator_traits
<_InputIterator2
>::value_type
>)
5594 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5595 typename iterator_traits
<_InputIterator2
>::value_type
,
5596 typename iterator_traits
<_InputIterator1
>::value_type
>)
5597 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5598 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5599 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5600 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5602 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5603 __first2
, __last2
, __result
,
5604 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5607 template<typename _ForwardIterator
, typename _Compare
>
5608 _GLIBCXX14_CONSTEXPR
5610 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5613 if (__first
== __last
)
5615 _ForwardIterator __result
= __first
;
5616 while (++__first
!= __last
)
5617 if (__comp(__first
, __result
))
5623 * @brief Return the minimum element in a range.
5624 * @ingroup sorting_algorithms
5625 * @param __first Start of range.
5626 * @param __last End of range.
5627 * @return Iterator referencing the first instance of the smallest value.
5629 template<typename _ForwardIterator
>
5630 _GLIBCXX14_CONSTEXPR
5632 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5634 // concept requirements
5635 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5636 __glibcxx_function_requires(_LessThanComparableConcept
<
5637 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5638 __glibcxx_requires_valid_range(__first
, __last
);
5639 __glibcxx_requires_irreflexive(__first
, __last
);
5641 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5642 __gnu_cxx::__ops::__iter_less_iter());
5646 * @brief Return the minimum element in a range using comparison functor.
5647 * @ingroup sorting_algorithms
5648 * @param __first Start of range.
5649 * @param __last End of range.
5650 * @param __comp Comparison functor.
5651 * @return Iterator referencing the first instance of the smallest value
5652 * according to __comp.
5654 template<typename _ForwardIterator
, typename _Compare
>
5655 _GLIBCXX14_CONSTEXPR
5656 inline _ForwardIterator
5657 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5660 // concept requirements
5661 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5662 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5663 typename iterator_traits
<_ForwardIterator
>::value_type
,
5664 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5665 __glibcxx_requires_valid_range(__first
, __last
);
5666 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5668 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5669 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5672 template<typename _ForwardIterator
, typename _Compare
>
5673 _GLIBCXX14_CONSTEXPR
5675 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5678 if (__first
== __last
) return __first
;
5679 _ForwardIterator __result
= __first
;
5680 while (++__first
!= __last
)
5681 if (__comp(__result
, __first
))
5687 * @brief Return the maximum element in a range.
5688 * @ingroup sorting_algorithms
5689 * @param __first Start of range.
5690 * @param __last End of range.
5691 * @return Iterator referencing the first instance of the largest value.
5693 template<typename _ForwardIterator
>
5694 _GLIBCXX14_CONSTEXPR
5695 inline _ForwardIterator
5696 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5698 // concept requirements
5699 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5700 __glibcxx_function_requires(_LessThanComparableConcept
<
5701 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5702 __glibcxx_requires_valid_range(__first
, __last
);
5703 __glibcxx_requires_irreflexive(__first
, __last
);
5705 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5706 __gnu_cxx::__ops::__iter_less_iter());
5710 * @brief Return the maximum element in a range using comparison functor.
5711 * @ingroup sorting_algorithms
5712 * @param __first Start of range.
5713 * @param __last End of range.
5714 * @param __comp Comparison functor.
5715 * @return Iterator referencing the first instance of the largest value
5716 * according to __comp.
5718 template<typename _ForwardIterator
, typename _Compare
>
5719 _GLIBCXX14_CONSTEXPR
5720 inline _ForwardIterator
5721 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5724 // concept requirements
5725 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5726 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5727 typename iterator_traits
<_ForwardIterator
>::value_type
,
5728 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5729 __glibcxx_requires_valid_range(__first
, __last
);
5730 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5732 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5733 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5736 #if __cplusplus >= 201103L
5738 template<typename _Tp
>
5739 _GLIBCXX14_CONSTEXPR
5741 min(initializer_list
<_Tp
> __l
)
5743 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5744 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5745 __gnu_cxx::__ops::__iter_less_iter());
5748 template<typename _Tp
, typename _Compare
>
5749 _GLIBCXX14_CONSTEXPR
5751 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
5753 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5754 return *_GLIBCXX_STD_A::__min_element(__l
.begin(), __l
.end(),
5755 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5758 template<typename _Tp
>
5759 _GLIBCXX14_CONSTEXPR
5761 max(initializer_list
<_Tp
> __l
)
5763 __glibcxx_requires_irreflexive(__l
.begin(), __l
.end());
5764 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5765 __gnu_cxx::__ops::__iter_less_iter());
5768 template<typename _Tp
, typename _Compare
>
5769 _GLIBCXX14_CONSTEXPR
5771 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
5773 __glibcxx_requires_irreflexive_pred(__l
.begin(), __l
.end(), __comp
);
5774 return *_GLIBCXX_STD_A::__max_element(__l
.begin(), __l
.end(),
5775 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5779 #if __cplusplus >= 201402L
5780 /// Reservoir sampling algorithm.
5781 template<typename _InputIterator
, typename _RandomAccessIterator
,
5782 typename _Size
, typename _UniformRandomBitGenerator
>
5783 _RandomAccessIterator
5784 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5785 _RandomAccessIterator __out
, random_access_iterator_tag
,
5786 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5788 using __distrib_type
= uniform_int_distribution
<_Size
>;
5789 using __param_type
= typename
__distrib_type::param_type
;
5790 __distrib_type __d
{};
5791 _Size __sample_sz
= 0;
5792 while (__first
!= __last
&& __sample_sz
!= __n
)
5794 __out
[__sample_sz
++] = *__first
;
5797 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5798 ++__first
, (void) ++__pop_sz
)
5800 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5802 __out
[__k
] = *__first
;
5804 return __out
+ __sample_sz
;
5807 /// Selection sampling algorithm.
5808 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5809 typename _Size
, typename _UniformRandomBitGenerator
>
5811 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5812 forward_iterator_tag
,
5813 _OutputIterator __out
, _Cat
,
5814 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5816 using __distrib_type
= uniform_int_distribution
<_Size
>;
5817 using __param_type
= typename
__distrib_type::param_type
;
5818 using _USize
= make_unsigned_t
<_Size
>;
5819 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5820 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5822 if (__first
== __last
)
5825 __distrib_type __d
{};
5826 _Size __unsampled_sz
= std::distance(__first
, __last
);
5827 __n
= std::min(__n
, __unsampled_sz
);
5829 // If possible, we use __gen_two_uniform_ints to efficiently produce
5830 // two random numbers using a single distribution invocation:
5832 const __uc_type __urngrange
= __g
.max() - __g
.min();
5833 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5834 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5837 while (__n
!= 0 && __unsampled_sz
>= 2)
5839 const pair
<_Size
, _Size
> __p
=
5840 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5843 if (__p
.first
< __n
)
5845 *__out
++ = *__first
;
5851 if (__n
== 0) break;
5854 if (__p
.second
< __n
)
5856 *__out
++ = *__first
;
5864 // The loop above is otherwise equivalent to this one-at-a-time version:
5866 for (; __n
!= 0; ++__first
)
5867 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5869 *__out
++ = *__first
;
5875 #if __cplusplus > 201402L
5876 #define __cpp_lib_sample 201603
5877 /// Take a random sample from a population.
5878 template<typename _PopulationIterator
, typename _SampleIterator
,
5879 typename _Distance
, typename _UniformRandomBitGenerator
>
5881 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5882 _SampleIterator __out
, _Distance __n
,
5883 _UniformRandomBitGenerator
&& __g
)
5885 using __pop_cat
= typename
5886 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5887 using __samp_cat
= typename
5888 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5891 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5892 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5893 "output range must use a RandomAccessIterator when input range"
5894 " does not meet the ForwardIterator requirements");
5896 static_assert(is_integral
<_Distance
>::value
,
5897 "sample size must be an integer type");
5899 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5900 return _GLIBCXX_STD_A::
5901 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5902 std::forward
<_UniformRandomBitGenerator
>(__g
));
5907 _GLIBCXX_END_NAMESPACE_ALGO
5908 _GLIBCXX_END_NAMESPACE_VERSION
5911 #endif /* _STL_ALGO_H */