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
));
3449 template<typename _Tp
>
3450 _GLIBCXX14_CONSTEXPR
3452 min(initializer_list
<_Tp
> __l
)
3453 { return *std::min_element(__l
.begin(), __l
.end()); }
3455 template<typename _Tp
, typename _Compare
>
3456 _GLIBCXX14_CONSTEXPR
3458 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3459 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3461 template<typename _Tp
>
3462 _GLIBCXX14_CONSTEXPR
3464 max(initializer_list
<_Tp
> __l
)
3465 { return *std::max_element(__l
.begin(), __l
.end()); }
3467 template<typename _Tp
, typename _Compare
>
3468 _GLIBCXX14_CONSTEXPR
3470 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3471 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3473 template<typename _Tp
>
3474 _GLIBCXX14_CONSTEXPR
3475 inline pair
<_Tp
, _Tp
>
3476 minmax(initializer_list
<_Tp
> __l
)
3478 pair
<const _Tp
*, const _Tp
*> __p
=
3479 std::minmax_element(__l
.begin(), __l
.end());
3480 return std::make_pair(*__p
.first
, *__p
.second
);
3483 template<typename _Tp
, typename _Compare
>
3484 _GLIBCXX14_CONSTEXPR
3485 inline pair
<_Tp
, _Tp
>
3486 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3488 pair
<const _Tp
*, const _Tp
*> __p
=
3489 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3490 return std::make_pair(*__p
.first
, *__p
.second
);
3494 * @brief Checks whether a permutation of the second sequence is equal
3495 * to the first sequence.
3496 * @ingroup non_mutating_algorithms
3497 * @param __first1 Start of first range.
3498 * @param __last1 End of first range.
3499 * @param __first2 Start of second range.
3500 * @param __pred A binary predicate.
3501 * @return true if there exists a permutation of the elements in
3502 * the range [__first2, __first2 + (__last1 - __first1)),
3503 * beginning with ForwardIterator2 begin, such that
3504 * equal(__first1, __last1, __begin, __pred) returns true;
3505 * otherwise, returns false.
3507 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3508 typename _BinaryPredicate
>
3509 _GLIBCXX20_CONSTEXPR
3511 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3512 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3514 // concept requirements
3515 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3516 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3517 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3518 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3519 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3520 __glibcxx_requires_valid_range(__first1
, __last1
);
3522 return std::__is_permutation(__first1
, __last1
, __first2
,
3523 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3526 #if __cplusplus > 201103L
3527 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3528 typename _BinaryPredicate
>
3529 _GLIBCXX20_CONSTEXPR
3531 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3532 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3533 _BinaryPredicate __pred
)
3536 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3538 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3539 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3540 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3541 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3544 auto __d1
= std::distance(__first1
, __last1
);
3545 auto __d2
= std::distance(__first2
, __last2
);
3550 // Efficiently compare identical prefixes: O(N) if sequences
3551 // have the same elements in the same order.
3552 for (; __first1
!= __last1
&& __first2
!= __last2
;
3553 ++__first1
, (void)++__first2
)
3554 if (!__pred(__first1
, __first2
))
3559 if (__first1
== __last1
)
3564 auto __d1
= std::distance(__first1
, __last1
);
3565 auto __d2
= std::distance(__first2
, __last2
);
3566 if (__d1
== 0 && __d2
== 0)
3572 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3574 if (__scan
!= std::__find_if(__first1
, __scan
,
3575 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3576 continue; // We've seen this one before.
3578 auto __matches
= std::__count_if(__first2
, __last2
,
3579 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3581 || std::__count_if(__scan
, __last1
,
3582 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3590 * @brief Checks whether a permutaion of the second sequence is equal
3591 * to the first sequence.
3592 * @ingroup non_mutating_algorithms
3593 * @param __first1 Start of first range.
3594 * @param __last1 End of first range.
3595 * @param __first2 Start of second range.
3596 * @param __last2 End of first range.
3597 * @return true if there exists a permutation of the elements in the range
3598 * [__first2, __last2), beginning with ForwardIterator2 begin,
3599 * such that equal(__first1, __last1, begin) returns true;
3600 * otherwise, returns false.
3602 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3603 _GLIBCXX20_CONSTEXPR
3605 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3606 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3608 __glibcxx_requires_valid_range(__first1
, __last1
);
3609 __glibcxx_requires_valid_range(__first2
, __last2
);
3612 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3613 __gnu_cxx::__ops::__iter_equal_to_iter());
3617 * @brief Checks whether a permutation of the second sequence is equal
3618 * to the first sequence.
3619 * @ingroup non_mutating_algorithms
3620 * @param __first1 Start of first range.
3621 * @param __last1 End of first range.
3622 * @param __first2 Start of second range.
3623 * @param __last2 End of first range.
3624 * @param __pred A binary predicate.
3625 * @return true if there exists a permutation of the elements in the range
3626 * [__first2, __last2), beginning with ForwardIterator2 begin,
3627 * such that equal(__first1, __last1, __begin, __pred) returns true;
3628 * otherwise, returns false.
3630 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3631 typename _BinaryPredicate
>
3632 _GLIBCXX20_CONSTEXPR
3634 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3635 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3636 _BinaryPredicate __pred
)
3638 __glibcxx_requires_valid_range(__first1
, __last1
);
3639 __glibcxx_requires_valid_range(__first2
, __last2
);
3641 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3642 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3645 #if __cplusplus > 201402L
3647 #define __cpp_lib_clamp 201603
3650 * @brief Returns the value clamped between lo and hi.
3651 * @ingroup sorting_algorithms
3652 * @param __val A value of arbitrary type.
3653 * @param __lo A lower limit of arbitrary type.
3654 * @param __hi An upper limit of arbitrary type.
3655 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
3657 template<typename _Tp
>
3658 constexpr const _Tp
&
3659 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3661 __glibcxx_assert(!(__hi
< __lo
));
3662 return (__val
< __lo
) ? __lo
: (__hi
< __val
) ? __hi
: __val
;
3666 * @brief Returns the value clamped between lo and hi.
3667 * @ingroup sorting_algorithms
3668 * @param __val A value of arbitrary type.
3669 * @param __lo A lower limit of arbitrary type.
3670 * @param __hi An upper limit of arbitrary type.
3671 * @param __comp A comparison functor.
3672 * @return max(__val, __lo, __comp) if __comp(__val, __hi)
3673 * or min(__val, __hi, __comp) otherwise.
3675 template<typename _Tp
, typename _Compare
>
3676 constexpr const _Tp
&
3677 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3679 __glibcxx_assert(!__comp(__hi
, __lo
));
3680 return __comp(__val
, __lo
) ? __lo
: __comp(__hi
, __val
) ? __hi
: __val
;
3685 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3687 * @brief Generate two uniformly distributed integers using a
3688 * single distribution invocation.
3689 * @param __b0 The upper bound for the first integer.
3690 * @param __b1 The upper bound for the second integer.
3691 * @param __g A UniformRandomBitGenerator.
3692 * @return A pair (i, j) with i and j uniformly distributed
3693 * over [0, __b0) and [0, __b1), respectively.
3695 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3697 * Using uniform_int_distribution with a range that is very
3698 * small relative to the range of the generator ends up wasting
3699 * potentially expensively generated randomness, since
3700 * uniform_int_distribution does not store leftover randomness
3701 * between invocations.
3703 * If we know we want two integers in ranges that are sufficiently
3704 * small, we can compose the ranges, use a single distribution
3705 * invocation, and significantly reduce the waste.
3707 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3708 pair
<_IntType
, _IntType
>
3709 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3710 _UniformRandomBitGenerator
&& __g
)
3713 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3714 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3718 * @brief Shuffle the elements of a sequence using a uniform random
3720 * @ingroup mutating_algorithms
3721 * @param __first A forward iterator.
3722 * @param __last A forward iterator.
3723 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3726 * Reorders the elements in the range @p [__first,__last) using @p __g to
3727 * provide random numbers.
3729 template<typename _RandomAccessIterator
,
3730 typename _UniformRandomNumberGenerator
>
3732 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3733 _UniformRandomNumberGenerator
&& __g
)
3735 // concept requirements
3736 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3737 _RandomAccessIterator
>)
3738 __glibcxx_requires_valid_range(__first
, __last
);
3740 if (__first
== __last
)
3743 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3746 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3747 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3748 typedef typename
__distr_type::param_type __p_type
;
3750 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3752 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3755 const __uc_type __urngrange
= __g
.max() - __g
.min();
3756 const __uc_type __urange
= __uc_type(__last
- __first
);
3758 if (__urngrange
/ __urange
>= __urange
)
3759 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3761 _RandomAccessIterator __i
= __first
+ 1;
3763 // Since we know the range isn't empty, an even number of elements
3764 // means an uneven number of elements /to swap/, in which case we
3765 // do the first one up front:
3767 if ((__urange
% 2) == 0)
3769 __distr_type __d
{0, 1};
3770 std::iter_swap(__i
++, __first
+ __d(__g
));
3773 // Now we know that __last - __i is even, so we do the rest in pairs,
3774 // using a single distribution invocation to produce swap positions
3775 // for two successive elements at a time:
3777 while (__i
!= __last
)
3779 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3781 const pair
<__uc_type
, __uc_type
> __pospos
=
3782 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3784 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3785 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3793 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3794 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3800 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3803 * @brief Apply a function to every element of a sequence.
3804 * @ingroup non_mutating_algorithms
3805 * @param __first An input iterator.
3806 * @param __last An input iterator.
3807 * @param __f A unary function object.
3810 * Applies the function object @p __f to each element in the range
3811 * @p [first,last). @p __f must not modify the order of the sequence.
3812 * If @p __f has a return value it is ignored.
3814 template<typename _InputIterator
, typename _Function
>
3815 _GLIBCXX20_CONSTEXPR
3817 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3819 // concept requirements
3820 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3821 __glibcxx_requires_valid_range(__first
, __last
);
3822 for (; __first
!= __last
; ++__first
)
3824 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3827 #if __cplusplus >= 201703L
3829 * @brief Apply a function to every element of a sequence.
3830 * @ingroup non_mutating_algorithms
3831 * @param __first An input iterator.
3832 * @param __n A value convertible to an integer.
3833 * @param __f A unary function object.
3834 * @return `__first+__n`
3836 * Applies the function object `__f` to each element in the range
3837 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3838 * If `__f` has a return value it is ignored.
3840 template<typename _InputIterator
, typename _Size
, typename _Function
>
3841 _GLIBCXX20_CONSTEXPR
3843 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3845 auto __n2
= std::__size_to_integer(__n
);
3846 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3847 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3851 auto __last
= __first
+ __n2
;
3852 std::for_each(__first
, __last
, std::move(__f
));
3868 * @brief Find the first occurrence of a value in a sequence.
3869 * @ingroup non_mutating_algorithms
3870 * @param __first An input iterator.
3871 * @param __last An input iterator.
3872 * @param __val The value to find.
3873 * @return The first iterator @c i in the range @p [__first,__last)
3874 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3876 template<typename _InputIterator
, typename _Tp
>
3877 _GLIBCXX20_CONSTEXPR
3878 inline _InputIterator
3879 find(_InputIterator __first
, _InputIterator __last
,
3882 // concept requirements
3883 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3884 __glibcxx_function_requires(_EqualOpConcept
<
3885 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3886 __glibcxx_requires_valid_range(__first
, __last
);
3887 return std::__find_if(__first
, __last
,
3888 __gnu_cxx::__ops::__iter_equals_val(__val
));
3892 * @brief Find the first element in a sequence for which a
3893 * predicate is true.
3894 * @ingroup non_mutating_algorithms
3895 * @param __first An input iterator.
3896 * @param __last An input iterator.
3897 * @param __pred A predicate.
3898 * @return The first iterator @c i in the range @p [__first,__last)
3899 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3901 template<typename _InputIterator
, typename _Predicate
>
3902 _GLIBCXX20_CONSTEXPR
3903 inline _InputIterator
3904 find_if(_InputIterator __first
, _InputIterator __last
,
3907 // concept requirements
3908 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3909 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3910 typename iterator_traits
<_InputIterator
>::value_type
>)
3911 __glibcxx_requires_valid_range(__first
, __last
);
3913 return std::__find_if(__first
, __last
,
3914 __gnu_cxx::__ops::__pred_iter(__pred
));
3918 * @brief Find element from a set in a sequence.
3919 * @ingroup non_mutating_algorithms
3920 * @param __first1 Start of range to search.
3921 * @param __last1 End of range to search.
3922 * @param __first2 Start of match candidates.
3923 * @param __last2 End of match candidates.
3924 * @return The first iterator @c i in the range
3925 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3926 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3928 * Searches the range @p [__first1,__last1) for an element that is
3929 * equal to some element in the range [__first2,__last2). If
3930 * found, returns an iterator in the range [__first1,__last1),
3931 * otherwise returns @p __last1.
3933 template<typename _InputIterator
, typename _ForwardIterator
>
3934 _GLIBCXX20_CONSTEXPR
3936 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3937 _ForwardIterator __first2
, _ForwardIterator __last2
)
3939 // concept requirements
3940 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3941 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3942 __glibcxx_function_requires(_EqualOpConcept
<
3943 typename iterator_traits
<_InputIterator
>::value_type
,
3944 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3945 __glibcxx_requires_valid_range(__first1
, __last1
);
3946 __glibcxx_requires_valid_range(__first2
, __last2
);
3948 for (; __first1
!= __last1
; ++__first1
)
3949 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3950 if (*__first1
== *__iter
)
3956 * @brief Find element from a set in a sequence using a predicate.
3957 * @ingroup non_mutating_algorithms
3958 * @param __first1 Start of range to search.
3959 * @param __last1 End of range to search.
3960 * @param __first2 Start of match candidates.
3961 * @param __last2 End of match candidates.
3962 * @param __comp Predicate to use.
3963 * @return The first iterator @c i in the range
3964 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3965 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3966 * such iterator exists.
3969 * Searches the range @p [__first1,__last1) for an element that is
3970 * equal to some element in the range [__first2,__last2). If
3971 * found, returns an iterator in the range [__first1,__last1),
3972 * otherwise returns @p __last1.
3974 template<typename _InputIterator
, typename _ForwardIterator
,
3975 typename _BinaryPredicate
>
3976 _GLIBCXX20_CONSTEXPR
3978 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3979 _ForwardIterator __first2
, _ForwardIterator __last2
,
3980 _BinaryPredicate __comp
)
3982 // concept requirements
3983 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3984 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3985 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3986 typename iterator_traits
<_InputIterator
>::value_type
,
3987 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3988 __glibcxx_requires_valid_range(__first1
, __last1
);
3989 __glibcxx_requires_valid_range(__first2
, __last2
);
3991 for (; __first1
!= __last1
; ++__first1
)
3992 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3993 if (__comp(*__first1
, *__iter
))
3999 * @brief Find two adjacent values in a sequence that are equal.
4000 * @ingroup non_mutating_algorithms
4001 * @param __first A forward iterator.
4002 * @param __last A forward iterator.
4003 * @return The first iterator @c i such that @c i and @c i+1 are both
4004 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4005 * or @p __last if no such iterator exists.
4007 template<typename _ForwardIterator
>
4008 _GLIBCXX20_CONSTEXPR
4009 inline _ForwardIterator
4010 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4012 // concept requirements
4013 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4014 __glibcxx_function_requires(_EqualityComparableConcept
<
4015 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4016 __glibcxx_requires_valid_range(__first
, __last
);
4018 return std::__adjacent_find(__first
, __last
,
4019 __gnu_cxx::__ops::__iter_equal_to_iter());
4023 * @brief Find two adjacent values in a sequence using a predicate.
4024 * @ingroup non_mutating_algorithms
4025 * @param __first A forward iterator.
4026 * @param __last A forward iterator.
4027 * @param __binary_pred A binary predicate.
4028 * @return The first iterator @c i such that @c i and @c i+1 are both
4029 * valid iterators in @p [__first,__last) and such that
4030 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4033 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4034 _GLIBCXX20_CONSTEXPR
4035 inline _ForwardIterator
4036 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4037 _BinaryPredicate __binary_pred
)
4039 // concept requirements
4040 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4041 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4042 typename iterator_traits
<_ForwardIterator
>::value_type
,
4043 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4044 __glibcxx_requires_valid_range(__first
, __last
);
4046 return std::__adjacent_find(__first
, __last
,
4047 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4051 * @brief Count the number of copies of a value in a sequence.
4052 * @ingroup non_mutating_algorithms
4053 * @param __first An input iterator.
4054 * @param __last An input iterator.
4055 * @param __value The value to be counted.
4056 * @return The number of iterators @c i in the range @p [__first,__last)
4057 * for which @c *i == @p __value
4059 template<typename _InputIterator
, typename _Tp
>
4060 _GLIBCXX20_CONSTEXPR
4061 inline typename iterator_traits
<_InputIterator
>::difference_type
4062 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4064 // concept requirements
4065 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4066 __glibcxx_function_requires(_EqualOpConcept
<
4067 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4068 __glibcxx_requires_valid_range(__first
, __last
);
4070 return std::__count_if(__first
, __last
,
4071 __gnu_cxx::__ops::__iter_equals_val(__value
));
4075 * @brief Count the elements of a sequence for which a predicate is true.
4076 * @ingroup non_mutating_algorithms
4077 * @param __first An input iterator.
4078 * @param __last An input iterator.
4079 * @param __pred A predicate.
4080 * @return The number of iterators @c i in the range @p [__first,__last)
4081 * for which @p __pred(*i) is true.
4083 template<typename _InputIterator
, typename _Predicate
>
4084 _GLIBCXX20_CONSTEXPR
4085 inline typename iterator_traits
<_InputIterator
>::difference_type
4086 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4088 // concept requirements
4089 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4090 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4091 typename iterator_traits
<_InputIterator
>::value_type
>)
4092 __glibcxx_requires_valid_range(__first
, __last
);
4094 return std::__count_if(__first
, __last
,
4095 __gnu_cxx::__ops::__pred_iter(__pred
));
4099 * @brief Search a sequence for a matching sub-sequence.
4100 * @ingroup non_mutating_algorithms
4101 * @param __first1 A forward iterator.
4102 * @param __last1 A forward iterator.
4103 * @param __first2 A forward iterator.
4104 * @param __last2 A forward iterator.
4105 * @return The first iterator @c i in the range @p
4106 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4107 * *(__first2+N) for each @c N in the range @p
4108 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4110 * Searches the range @p [__first1,__last1) for a sub-sequence that
4111 * compares equal value-by-value with the sequence given by @p
4112 * [__first2,__last2) and returns an iterator to the first element
4113 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4116 * Because the sub-sequence must lie completely within the range @p
4117 * [__first1,__last1) it must start at a position less than @p
4118 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4119 * length of the sub-sequence.
4121 * This means that the returned iterator @c i will be in the range
4122 * @p [__first1,__last1-(__last2-__first2))
4124 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4125 _GLIBCXX20_CONSTEXPR
4126 inline _ForwardIterator1
4127 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4128 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4130 // concept requirements
4131 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4132 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4133 __glibcxx_function_requires(_EqualOpConcept
<
4134 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4135 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4136 __glibcxx_requires_valid_range(__first1
, __last1
);
4137 __glibcxx_requires_valid_range(__first2
, __last2
);
4139 return std::__search(__first1
, __last1
, __first2
, __last2
,
4140 __gnu_cxx::__ops::__iter_equal_to_iter());
4144 * @brief Search a sequence for a matching sub-sequence using a predicate.
4145 * @ingroup non_mutating_algorithms
4146 * @param __first1 A forward iterator.
4147 * @param __last1 A forward iterator.
4148 * @param __first2 A forward iterator.
4149 * @param __last2 A forward iterator.
4150 * @param __predicate A binary predicate.
4151 * @return The first iterator @c i in the range
4152 * @p [__first1,__last1-(__last2-__first2)) such that
4153 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4154 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4156 * Searches the range @p [__first1,__last1) for a sub-sequence that
4157 * compares equal value-by-value with the sequence given by @p
4158 * [__first2,__last2), using @p __predicate to determine equality,
4159 * and returns an iterator to the first element of the
4160 * sub-sequence, or @p __last1 if no such iterator exists.
4162 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4164 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4165 typename _BinaryPredicate
>
4166 _GLIBCXX20_CONSTEXPR
4167 inline _ForwardIterator1
4168 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4169 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4170 _BinaryPredicate __predicate
)
4172 // concept requirements
4173 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4174 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4175 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4176 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4177 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4178 __glibcxx_requires_valid_range(__first1
, __last1
);
4179 __glibcxx_requires_valid_range(__first2
, __last2
);
4181 return std::__search(__first1
, __last1
, __first2
, __last2
,
4182 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4186 * @brief Search a sequence for a number of consecutive values.
4187 * @ingroup non_mutating_algorithms
4188 * @param __first A forward iterator.
4189 * @param __last A forward iterator.
4190 * @param __count The number of consecutive values.
4191 * @param __val The value to find.
4192 * @return The first iterator @c i in the range @p
4193 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4194 * each @c N in the range @p [0,__count), or @p __last if no such
4197 * Searches the range @p [__first,__last) for @p count consecutive elements
4198 * equal to @p __val.
4200 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4201 _GLIBCXX20_CONSTEXPR
4202 inline _ForwardIterator
4203 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4204 _Integer __count
, const _Tp
& __val
)
4206 // concept requirements
4207 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4208 __glibcxx_function_requires(_EqualOpConcept
<
4209 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4210 __glibcxx_requires_valid_range(__first
, __last
);
4212 return std::__search_n(__first
, __last
, __count
,
4213 __gnu_cxx::__ops::__iter_equals_val(__val
));
4218 * @brief Search a sequence for a number of consecutive values using a
4220 * @ingroup non_mutating_algorithms
4221 * @param __first A forward iterator.
4222 * @param __last A forward iterator.
4223 * @param __count The number of consecutive values.
4224 * @param __val The value to find.
4225 * @param __binary_pred A binary predicate.
4226 * @return The first iterator @c i in the range @p
4227 * [__first,__last-__count) such that @p
4228 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4229 * @p [0,__count), or @p __last if no such iterator exists.
4231 * Searches the range @p [__first,__last) for @p __count
4232 * consecutive elements for which the predicate returns true.
4234 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4235 typename _BinaryPredicate
>
4236 _GLIBCXX20_CONSTEXPR
4237 inline _ForwardIterator
4238 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4239 _Integer __count
, const _Tp
& __val
,
4240 _BinaryPredicate __binary_pred
)
4242 // concept requirements
4243 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4244 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4245 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4246 __glibcxx_requires_valid_range(__first
, __last
);
4248 return std::__search_n(__first
, __last
, __count
,
4249 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4252 #if __cplusplus >= 201703L
4253 /** @brief Search a sequence using a Searcher object.
4255 * @param __first A forward iterator.
4256 * @param __last A forward iterator.
4257 * @param __searcher A callable object.
4258 * @return @p __searcher(__first,__last).first
4260 template<typename _ForwardIterator
, typename _Searcher
>
4261 _GLIBCXX20_CONSTEXPR
4262 inline _ForwardIterator
4263 search(_ForwardIterator __first
, _ForwardIterator __last
,
4264 const _Searcher
& __searcher
)
4265 { return __searcher(__first
, __last
).first
; }
4269 * @brief Perform an operation on a sequence.
4270 * @ingroup mutating_algorithms
4271 * @param __first An input iterator.
4272 * @param __last An input iterator.
4273 * @param __result An output iterator.
4274 * @param __unary_op A unary operator.
4275 * @return An output iterator equal to @p __result+(__last-__first).
4277 * Applies the operator to each element in the input range and assigns
4278 * the results to successive elements of the output sequence.
4279 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4280 * range @p [0,__last-__first).
4282 * @p unary_op must not alter its argument.
4284 template<typename _InputIterator
, typename _OutputIterator
,
4285 typename _UnaryOperation
>
4286 _GLIBCXX20_CONSTEXPR
4288 transform(_InputIterator __first
, _InputIterator __last
,
4289 _OutputIterator __result
, _UnaryOperation __unary_op
)
4291 // concept requirements
4292 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4293 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4294 // "the type returned by a _UnaryOperation"
4295 __typeof__(__unary_op(*__first
))>)
4296 __glibcxx_requires_valid_range(__first
, __last
);
4298 for (; __first
!= __last
; ++__first
, (void)++__result
)
4299 *__result
= __unary_op(*__first
);
4304 * @brief Perform an operation on corresponding elements of two sequences.
4305 * @ingroup mutating_algorithms
4306 * @param __first1 An input iterator.
4307 * @param __last1 An input iterator.
4308 * @param __first2 An input iterator.
4309 * @param __result An output iterator.
4310 * @param __binary_op A binary operator.
4311 * @return An output iterator equal to @p result+(last-first).
4313 * Applies the operator to the corresponding elements in the two
4314 * input ranges and assigns the results to successive elements of the
4317 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4318 * @c N in the range @p [0,__last1-__first1).
4320 * @p binary_op must not alter either of its arguments.
4322 template<typename _InputIterator1
, typename _InputIterator2
,
4323 typename _OutputIterator
, typename _BinaryOperation
>
4324 _GLIBCXX20_CONSTEXPR
4326 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4327 _InputIterator2 __first2
, _OutputIterator __result
,
4328 _BinaryOperation __binary_op
)
4330 // concept requirements
4331 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4332 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4333 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4334 // "the type returned by a _BinaryOperation"
4335 __typeof__(__binary_op(*__first1
,*__first2
))>)
4336 __glibcxx_requires_valid_range(__first1
, __last1
);
4338 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4339 *__result
= __binary_op(*__first1
, *__first2
);
4344 * @brief Replace each occurrence of one value in a sequence with another
4346 * @ingroup mutating_algorithms
4347 * @param __first A forward iterator.
4348 * @param __last A forward iterator.
4349 * @param __old_value The value to be replaced.
4350 * @param __new_value The replacement value.
4351 * @return replace() returns no value.
4353 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4354 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4356 template<typename _ForwardIterator
, typename _Tp
>
4357 _GLIBCXX20_CONSTEXPR
4359 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4360 const _Tp
& __old_value
, const _Tp
& __new_value
)
4362 // concept requirements
4363 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4365 __glibcxx_function_requires(_EqualOpConcept
<
4366 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4367 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4368 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4369 __glibcxx_requires_valid_range(__first
, __last
);
4371 for (; __first
!= __last
; ++__first
)
4372 if (*__first
== __old_value
)
4373 *__first
= __new_value
;
4377 * @brief Replace each value in a sequence for which a predicate returns
4378 * true with another value.
4379 * @ingroup mutating_algorithms
4380 * @param __first A forward iterator.
4381 * @param __last A forward iterator.
4382 * @param __pred A predicate.
4383 * @param __new_value The replacement value.
4384 * @return replace_if() returns no value.
4386 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4387 * is true then the assignment @c *i = @p __new_value is performed.
4389 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4390 _GLIBCXX20_CONSTEXPR
4392 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4393 _Predicate __pred
, const _Tp
& __new_value
)
4395 // concept requirements
4396 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4398 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4399 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4400 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4401 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4402 __glibcxx_requires_valid_range(__first
, __last
);
4404 for (; __first
!= __last
; ++__first
)
4405 if (__pred(*__first
))
4406 *__first
= __new_value
;
4410 * @brief Assign the result of a function object to each value in a
4412 * @ingroup mutating_algorithms
4413 * @param __first A forward iterator.
4414 * @param __last A forward iterator.
4415 * @param __gen A function object taking no arguments and returning
4416 * std::iterator_traits<_ForwardIterator>::value_type
4417 * @return generate() returns no value.
4419 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4420 * @p [__first,__last).
4422 template<typename _ForwardIterator
, typename _Generator
>
4423 _GLIBCXX20_CONSTEXPR
4425 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4428 // concept requirements
4429 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4430 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4431 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4432 __glibcxx_requires_valid_range(__first
, __last
);
4434 for (; __first
!= __last
; ++__first
)
4439 * @brief Assign the result of a function object to each value in a
4441 * @ingroup mutating_algorithms
4442 * @param __first A forward iterator.
4443 * @param __n The length of the sequence.
4444 * @param __gen A function object taking no arguments and returning
4445 * std::iterator_traits<_ForwardIterator>::value_type
4446 * @return The end of the sequence, @p __first+__n
4448 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4449 * @p [__first,__first+__n).
4451 * If @p __n is negative, the function does nothing and returns @p __first.
4453 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4454 // DR 865. More algorithms that throw away information
4455 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4456 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4457 _GLIBCXX20_CONSTEXPR
4459 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4461 // concept requirements
4462 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4463 // "the type returned by a _Generator"
4464 __typeof__(__gen())>)
4466 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4467 for (_IntSize __niter
= std::__size_to_integer(__n
);
4468 __niter
> 0; --__niter
, (void) ++__first
)
4474 * @brief Copy a sequence, removing consecutive duplicate values.
4475 * @ingroup mutating_algorithms
4476 * @param __first An input iterator.
4477 * @param __last An input iterator.
4478 * @param __result An output iterator.
4479 * @return An iterator designating the end of the resulting sequence.
4481 * Copies each element in the range @p [__first,__last) to the range
4482 * beginning at @p __result, except that only the first element is copied
4483 * from groups of consecutive elements that compare equal.
4484 * unique_copy() is stable, so the relative order of elements that are
4485 * copied is unchanged.
4487 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4488 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4490 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4491 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4494 template<typename _InputIterator
, typename _OutputIterator
>
4495 _GLIBCXX20_CONSTEXPR
4496 inline _OutputIterator
4497 unique_copy(_InputIterator __first
, _InputIterator __last
,
4498 _OutputIterator __result
)
4500 // concept requirements
4501 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4502 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4503 typename iterator_traits
<_InputIterator
>::value_type
>)
4504 __glibcxx_function_requires(_EqualityComparableConcept
<
4505 typename iterator_traits
<_InputIterator
>::value_type
>)
4506 __glibcxx_requires_valid_range(__first
, __last
);
4508 if (__first
== __last
)
4510 return std::__unique_copy(__first
, __last
, __result
,
4511 __gnu_cxx::__ops::__iter_equal_to_iter(),
4512 std::__iterator_category(__first
),
4513 std::__iterator_category(__result
));
4517 * @brief Copy a sequence, removing consecutive values using a predicate.
4518 * @ingroup mutating_algorithms
4519 * @param __first An input iterator.
4520 * @param __last An input iterator.
4521 * @param __result An output iterator.
4522 * @param __binary_pred A binary predicate.
4523 * @return An iterator designating the end of the resulting sequence.
4525 * Copies each element in the range @p [__first,__last) to the range
4526 * beginning at @p __result, except that only the first element is copied
4527 * from groups of consecutive elements for which @p __binary_pred returns
4529 * unique_copy() is stable, so the relative order of elements that are
4530 * copied is unchanged.
4532 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4533 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4535 template<typename _InputIterator
, typename _OutputIterator
,
4536 typename _BinaryPredicate
>
4537 _GLIBCXX20_CONSTEXPR
4538 inline _OutputIterator
4539 unique_copy(_InputIterator __first
, _InputIterator __last
,
4540 _OutputIterator __result
,
4541 _BinaryPredicate __binary_pred
)
4543 // concept requirements -- predicates checked later
4544 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4545 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4546 typename iterator_traits
<_InputIterator
>::value_type
>)
4547 __glibcxx_requires_valid_range(__first
, __last
);
4549 if (__first
== __last
)
4551 return std::__unique_copy(__first
, __last
, __result
,
4552 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4553 std::__iterator_category(__first
),
4554 std::__iterator_category(__result
));
4557 #if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4560 * @brief Randomly shuffle the elements of a sequence.
4561 * @ingroup mutating_algorithms
4562 * @param __first A forward iterator.
4563 * @param __last A forward iterator.
4566 * Reorder the elements in the range @p [__first,__last) using a random
4567 * distribution, so that every possible ordering of the sequence is
4570 template<typename _RandomAccessIterator
>
4571 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4573 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4575 // concept requirements
4576 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4577 _RandomAccessIterator
>)
4578 __glibcxx_requires_valid_range(__first
, __last
);
4580 if (__first
!= __last
)
4581 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4583 // XXX rand() % N is not uniformly distributed
4584 _RandomAccessIterator __j
= __first
4585 + std::rand() % ((__i
- __first
) + 1);
4587 std::iter_swap(__i
, __j
);
4593 * @brief Shuffle the elements of a sequence using a random number
4595 * @ingroup mutating_algorithms
4596 * @param __first A forward iterator.
4597 * @param __last A forward iterator.
4598 * @param __rand The RNG functor or function.
4601 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4602 * provide a random distribution. Calling @p __rand(N) for a positive
4603 * integer @p N should return a randomly chosen integer from the
4606 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4607 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4609 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4610 #if __cplusplus >= 201103L
4611 _RandomNumberGenerator
&& __rand
)
4613 _RandomNumberGenerator
& __rand
)
4616 // concept requirements
4617 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4618 _RandomAccessIterator
>)
4619 __glibcxx_requires_valid_range(__first
, __last
);
4621 if (__first
== __last
)
4623 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4625 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4627 std::iter_swap(__i
, __j
);
4630 #endif // C++11 || USE_DEPRECATED
4633 * @brief Move elements for which a predicate is true to the beginning
4635 * @ingroup mutating_algorithms
4636 * @param __first A forward iterator.
4637 * @param __last A forward iterator.
4638 * @param __pred A predicate functor.
4639 * @return An iterator @p middle such that @p __pred(i) is true for each
4640 * iterator @p i in the range @p [__first,middle) and false for each @p i
4641 * in the range @p [middle,__last).
4643 * @p __pred must not modify its operand. @p partition() does not preserve
4644 * the relative ordering of elements in each group, use
4645 * @p stable_partition() if this is needed.
4647 template<typename _ForwardIterator
, typename _Predicate
>
4648 _GLIBCXX20_CONSTEXPR
4649 inline _ForwardIterator
4650 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4653 // concept requirements
4654 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4656 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4657 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4658 __glibcxx_requires_valid_range(__first
, __last
);
4660 return std::__partition(__first
, __last
, __pred
,
4661 std::__iterator_category(__first
));
4666 * @brief Sort the smallest elements of a sequence.
4667 * @ingroup sorting_algorithms
4668 * @param __first An iterator.
4669 * @param __middle Another iterator.
4670 * @param __last Another iterator.
4673 * Sorts the smallest @p (__middle-__first) elements in the range
4674 * @p [first,last) and moves them to the range @p [__first,__middle). The
4675 * order of the remaining elements in the range @p [__middle,__last) is
4677 * After the sort if @e i and @e j are iterators in the range
4678 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4679 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4681 template<typename _RandomAccessIterator
>
4682 _GLIBCXX20_CONSTEXPR
4684 partial_sort(_RandomAccessIterator __first
,
4685 _RandomAccessIterator __middle
,
4686 _RandomAccessIterator __last
)
4688 // concept requirements
4689 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4690 _RandomAccessIterator
>)
4691 __glibcxx_function_requires(_LessThanComparableConcept
<
4692 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4693 __glibcxx_requires_valid_range(__first
, __middle
);
4694 __glibcxx_requires_valid_range(__middle
, __last
);
4695 __glibcxx_requires_irreflexive(__first
, __last
);
4697 std::__partial_sort(__first
, __middle
, __last
,
4698 __gnu_cxx::__ops::__iter_less_iter());
4702 * @brief Sort the smallest elements of a sequence using a predicate
4704 * @ingroup sorting_algorithms
4705 * @param __first An iterator.
4706 * @param __middle Another iterator.
4707 * @param __last Another iterator.
4708 * @param __comp A comparison functor.
4711 * Sorts the smallest @p (__middle-__first) elements in the range
4712 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4713 * order of the remaining elements in the range @p [__middle,__last) is
4715 * After the sort if @e i and @e j are iterators in the range
4716 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4717 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4720 template<typename _RandomAccessIterator
, typename _Compare
>
4721 _GLIBCXX20_CONSTEXPR
4723 partial_sort(_RandomAccessIterator __first
,
4724 _RandomAccessIterator __middle
,
4725 _RandomAccessIterator __last
,
4728 // concept requirements
4729 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4730 _RandomAccessIterator
>)
4731 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4732 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4733 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4734 __glibcxx_requires_valid_range(__first
, __middle
);
4735 __glibcxx_requires_valid_range(__middle
, __last
);
4736 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4738 std::__partial_sort(__first
, __middle
, __last
,
4739 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4743 * @brief Sort a sequence just enough to find a particular position.
4744 * @ingroup sorting_algorithms
4745 * @param __first An iterator.
4746 * @param __nth Another iterator.
4747 * @param __last Another iterator.
4750 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4751 * is the same element that would have been in that position had the
4752 * whole sequence been sorted. The elements either side of @p *__nth are
4753 * not completely sorted, but for any iterator @e i in the range
4754 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4755 * holds that *j < *i is false.
4757 template<typename _RandomAccessIterator
>
4758 _GLIBCXX20_CONSTEXPR
4760 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4761 _RandomAccessIterator __last
)
4763 // concept requirements
4764 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4765 _RandomAccessIterator
>)
4766 __glibcxx_function_requires(_LessThanComparableConcept
<
4767 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4768 __glibcxx_requires_valid_range(__first
, __nth
);
4769 __glibcxx_requires_valid_range(__nth
, __last
);
4770 __glibcxx_requires_irreflexive(__first
, __last
);
4772 if (__first
== __last
|| __nth
== __last
)
4775 std::__introselect(__first
, __nth
, __last
,
4776 std::__lg(__last
- __first
) * 2,
4777 __gnu_cxx::__ops::__iter_less_iter());
4781 * @brief Sort a sequence just enough to find a particular position
4782 * using a predicate for comparison.
4783 * @ingroup sorting_algorithms
4784 * @param __first An iterator.
4785 * @param __nth Another iterator.
4786 * @param __last Another iterator.
4787 * @param __comp A comparison functor.
4790 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4791 * is the same element that would have been in that position had the
4792 * whole sequence been sorted. The elements either side of @p *__nth are
4793 * not completely sorted, but for any iterator @e i in the range
4794 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4795 * holds that @p __comp(*j,*i) is false.
4797 template<typename _RandomAccessIterator
, typename _Compare
>
4798 _GLIBCXX20_CONSTEXPR
4800 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4801 _RandomAccessIterator __last
, _Compare __comp
)
4803 // concept requirements
4804 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4805 _RandomAccessIterator
>)
4806 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4807 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4808 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4809 __glibcxx_requires_valid_range(__first
, __nth
);
4810 __glibcxx_requires_valid_range(__nth
, __last
);
4811 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4813 if (__first
== __last
|| __nth
== __last
)
4816 std::__introselect(__first
, __nth
, __last
,
4817 std::__lg(__last
- __first
) * 2,
4818 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4822 * @brief Sort the elements of a sequence.
4823 * @ingroup sorting_algorithms
4824 * @param __first An iterator.
4825 * @param __last Another iterator.
4828 * Sorts the elements in the range @p [__first,__last) in ascending order,
4829 * such that for each iterator @e i in the range @p [__first,__last-1),
4830 * *(i+1)<*i is false.
4832 * The relative ordering of equivalent elements is not preserved, use
4833 * @p stable_sort() if this is needed.
4835 template<typename _RandomAccessIterator
>
4836 _GLIBCXX20_CONSTEXPR
4838 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4840 // concept requirements
4841 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4842 _RandomAccessIterator
>)
4843 __glibcxx_function_requires(_LessThanComparableConcept
<
4844 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4845 __glibcxx_requires_valid_range(__first
, __last
);
4846 __glibcxx_requires_irreflexive(__first
, __last
);
4848 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4852 * @brief Sort the elements of a sequence using a predicate for comparison.
4853 * @ingroup sorting_algorithms
4854 * @param __first An iterator.
4855 * @param __last Another iterator.
4856 * @param __comp A comparison functor.
4859 * Sorts the elements in the range @p [__first,__last) in ascending order,
4860 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4861 * range @p [__first,__last-1).
4863 * The relative ordering of equivalent elements is not preserved, use
4864 * @p stable_sort() if this is needed.
4866 template<typename _RandomAccessIterator
, typename _Compare
>
4867 _GLIBCXX20_CONSTEXPR
4869 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4872 // concept requirements
4873 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4874 _RandomAccessIterator
>)
4875 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4876 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4877 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4878 __glibcxx_requires_valid_range(__first
, __last
);
4879 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4881 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4884 template<typename _InputIterator1
, typename _InputIterator2
,
4885 typename _OutputIterator
, typename _Compare
>
4886 _GLIBCXX20_CONSTEXPR
4888 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4889 _InputIterator2 __first2
, _InputIterator2 __last2
,
4890 _OutputIterator __result
, _Compare __comp
)
4892 while (__first1
!= __last1
&& __first2
!= __last2
)
4894 if (__comp(__first2
, __first1
))
4896 *__result
= *__first2
;
4901 *__result
= *__first1
;
4906 return std::copy(__first2
, __last2
,
4907 std::copy(__first1
, __last1
, __result
));
4911 * @brief Merges two sorted ranges.
4912 * @ingroup sorting_algorithms
4913 * @param __first1 An iterator.
4914 * @param __first2 Another iterator.
4915 * @param __last1 Another iterator.
4916 * @param __last2 Another iterator.
4917 * @param __result An iterator pointing to the end of the merged range.
4918 * @return An output iterator equal to @p __result + (__last1 - __first1)
4919 * + (__last2 - __first2).
4921 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4922 * the sorted range @p [__result, __result + (__last1-__first1) +
4923 * (__last2-__first2)). Both input ranges must be sorted, and the
4924 * output range must not overlap with either of the input ranges.
4925 * The sort is @e stable, that is, for equivalent elements in the
4926 * two ranges, elements from the first range will always come
4927 * before elements from the second.
4929 template<typename _InputIterator1
, typename _InputIterator2
,
4930 typename _OutputIterator
>
4931 _GLIBCXX20_CONSTEXPR
4932 inline _OutputIterator
4933 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4934 _InputIterator2 __first2
, _InputIterator2 __last2
,
4935 _OutputIterator __result
)
4937 // concept requirements
4938 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4939 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4940 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4941 typename iterator_traits
<_InputIterator1
>::value_type
>)
4942 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4943 typename iterator_traits
<_InputIterator2
>::value_type
>)
4944 __glibcxx_function_requires(_LessThanOpConcept
<
4945 typename iterator_traits
<_InputIterator2
>::value_type
,
4946 typename iterator_traits
<_InputIterator1
>::value_type
>)
4947 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4948 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4949 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4950 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4952 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4953 __first2
, __last2
, __result
,
4954 __gnu_cxx::__ops::__iter_less_iter());
4958 * @brief Merges two sorted ranges.
4959 * @ingroup sorting_algorithms
4960 * @param __first1 An iterator.
4961 * @param __first2 Another iterator.
4962 * @param __last1 Another iterator.
4963 * @param __last2 Another iterator.
4964 * @param __result An iterator pointing to the end of the merged range.
4965 * @param __comp A functor to use for comparisons.
4966 * @return An output iterator equal to @p __result + (__last1 - __first1)
4967 * + (__last2 - __first2).
4969 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4970 * the sorted range @p [__result, __result + (__last1-__first1) +
4971 * (__last2-__first2)). Both input ranges must be sorted, and the
4972 * output range must not overlap with either of the input ranges.
4973 * The sort is @e stable, that is, for equivalent elements in the
4974 * two ranges, elements from the first range will always come
4975 * before elements from the second.
4977 * The comparison function should have the same effects on ordering as
4978 * the function used for the initial sort.
4980 template<typename _InputIterator1
, typename _InputIterator2
,
4981 typename _OutputIterator
, typename _Compare
>
4982 _GLIBCXX20_CONSTEXPR
4983 inline _OutputIterator
4984 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4985 _InputIterator2 __first2
, _InputIterator2 __last2
,
4986 _OutputIterator __result
, _Compare __comp
)
4988 // concept requirements
4989 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4990 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4991 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4992 typename iterator_traits
<_InputIterator1
>::value_type
>)
4993 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4994 typename iterator_traits
<_InputIterator2
>::value_type
>)
4995 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4996 typename iterator_traits
<_InputIterator2
>::value_type
,
4997 typename iterator_traits
<_InputIterator1
>::value_type
>)
4998 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4999 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5000 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5001 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5003 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5004 __first2
, __last2
, __result
,
5005 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5008 template<typename _RandomAccessIterator
, typename _Compare
>
5010 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5013 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5015 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5017 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5019 if (__first
== __last
)
5022 // __stable_sort_adaptive sorts the range in two halves,
5023 // so the buffer only needs to fit half the range at once.
5024 _TmpBuf
__buf(__first
, (__last
- __first
+ 1) / 2);
5026 if (__buf
.begin() == 0)
5027 std::__inplace_stable_sort(__first
, __last
, __comp
);
5029 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5030 _DistanceType(__buf
.size()), __comp
);
5034 * @brief Sort the elements of a sequence, preserving the relative order
5035 * of equivalent elements.
5036 * @ingroup sorting_algorithms
5037 * @param __first An iterator.
5038 * @param __last Another iterator.
5041 * Sorts the elements in the range @p [__first,__last) in ascending order,
5042 * such that for each iterator @p i in the range @p [__first,__last-1),
5043 * @p *(i+1)<*i is false.
5045 * The relative ordering of equivalent elements is preserved, so any two
5046 * elements @p x and @p y in the range @p [__first,__last) such that
5047 * @p x<y is false and @p y<x is false will have the same relative
5048 * ordering after calling @p stable_sort().
5050 template<typename _RandomAccessIterator
>
5052 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5054 // concept requirements
5055 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5056 _RandomAccessIterator
>)
5057 __glibcxx_function_requires(_LessThanComparableConcept
<
5058 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5059 __glibcxx_requires_valid_range(__first
, __last
);
5060 __glibcxx_requires_irreflexive(__first
, __last
);
5062 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5063 __gnu_cxx::__ops::__iter_less_iter());
5067 * @brief Sort the elements of a sequence using a predicate for comparison,
5068 * preserving the relative order of equivalent elements.
5069 * @ingroup sorting_algorithms
5070 * @param __first An iterator.
5071 * @param __last Another iterator.
5072 * @param __comp A comparison functor.
5075 * Sorts the elements in the range @p [__first,__last) in ascending order,
5076 * such that for each iterator @p i in the range @p [__first,__last-1),
5077 * @p __comp(*(i+1),*i) is false.
5079 * The relative ordering of equivalent elements is preserved, so any two
5080 * elements @p x and @p y in the range @p [__first,__last) such that
5081 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5082 * relative ordering after calling @p stable_sort().
5084 template<typename _RandomAccessIterator
, typename _Compare
>
5086 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5089 // concept requirements
5090 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5091 _RandomAccessIterator
>)
5092 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5093 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5094 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5095 __glibcxx_requires_valid_range(__first
, __last
);
5096 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5098 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5099 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5102 template<typename _InputIterator1
, typename _InputIterator2
,
5103 typename _OutputIterator
,
5105 _GLIBCXX20_CONSTEXPR
5107 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5108 _InputIterator2 __first2
, _InputIterator2 __last2
,
5109 _OutputIterator __result
, _Compare __comp
)
5111 while (__first1
!= __last1
&& __first2
!= __last2
)
5113 if (__comp(__first1
, __first2
))
5115 *__result
= *__first1
;
5118 else if (__comp(__first2
, __first1
))
5120 *__result
= *__first2
;
5125 *__result
= *__first1
;
5131 return std::copy(__first2
, __last2
,
5132 std::copy(__first1
, __last1
, __result
));
5136 * @brief Return the union of two sorted ranges.
5137 * @ingroup set_algorithms
5138 * @param __first1 Start of first range.
5139 * @param __last1 End of first range.
5140 * @param __first2 Start of second range.
5141 * @param __last2 End of second range.
5142 * @param __result Start of output range.
5143 * @return End of the output range.
5144 * @ingroup set_algorithms
5146 * This operation iterates over both ranges, copying elements present in
5147 * each range in order to the output range. Iterators increment for each
5148 * range. When the current element of one range is less than the other,
5149 * that element is copied and the iterator advanced. If an element is
5150 * contained in both ranges, the element from the first range is copied and
5151 * both ranges advance. The output range may not overlap either input
5154 template<typename _InputIterator1
, typename _InputIterator2
,
5155 typename _OutputIterator
>
5156 _GLIBCXX20_CONSTEXPR
5157 inline _OutputIterator
5158 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5159 _InputIterator2 __first2
, _InputIterator2 __last2
,
5160 _OutputIterator __result
)
5162 // concept requirements
5163 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5164 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5165 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5166 typename iterator_traits
<_InputIterator1
>::value_type
>)
5167 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5168 typename iterator_traits
<_InputIterator2
>::value_type
>)
5169 __glibcxx_function_requires(_LessThanOpConcept
<
5170 typename iterator_traits
<_InputIterator1
>::value_type
,
5171 typename iterator_traits
<_InputIterator2
>::value_type
>)
5172 __glibcxx_function_requires(_LessThanOpConcept
<
5173 typename iterator_traits
<_InputIterator2
>::value_type
,
5174 typename iterator_traits
<_InputIterator1
>::value_type
>)
5175 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5176 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5177 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5178 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5180 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5181 __first2
, __last2
, __result
,
5182 __gnu_cxx::__ops::__iter_less_iter());
5186 * @brief Return the union of two sorted ranges using a comparison functor.
5187 * @ingroup set_algorithms
5188 * @param __first1 Start of first range.
5189 * @param __last1 End of first range.
5190 * @param __first2 Start of second range.
5191 * @param __last2 End of second range.
5192 * @param __result Start of output range.
5193 * @param __comp The comparison functor.
5194 * @return End of the output range.
5195 * @ingroup set_algorithms
5197 * This operation iterates over both ranges, copying elements present in
5198 * each range in order to the output range. Iterators increment for each
5199 * range. When the current element of one range is less than the other
5200 * according to @p __comp, that element is copied and the iterator advanced.
5201 * If an equivalent element according to @p __comp is contained in both
5202 * ranges, the element from the first range is copied and both ranges
5203 * advance. The output range may not overlap either input range.
5205 template<typename _InputIterator1
, typename _InputIterator2
,
5206 typename _OutputIterator
, typename _Compare
>
5207 _GLIBCXX20_CONSTEXPR
5208 inline _OutputIterator
5209 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5210 _InputIterator2 __first2
, _InputIterator2 __last2
,
5211 _OutputIterator __result
, _Compare __comp
)
5213 // concept requirements
5214 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5215 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5216 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5217 typename iterator_traits
<_InputIterator1
>::value_type
>)
5218 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5219 typename iterator_traits
<_InputIterator2
>::value_type
>)
5220 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5221 typename iterator_traits
<_InputIterator1
>::value_type
,
5222 typename iterator_traits
<_InputIterator2
>::value_type
>)
5223 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5224 typename iterator_traits
<_InputIterator2
>::value_type
,
5225 typename iterator_traits
<_InputIterator1
>::value_type
>)
5226 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5227 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5228 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5229 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5231 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5232 __first2
, __last2
, __result
,
5233 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5236 template<typename _InputIterator1
, typename _InputIterator2
,
5237 typename _OutputIterator
,
5239 _GLIBCXX20_CONSTEXPR
5241 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5242 _InputIterator2 __first2
, _InputIterator2 __last2
,
5243 _OutputIterator __result
, _Compare __comp
)
5245 while (__first1
!= __last1
&& __first2
!= __last2
)
5246 if (__comp(__first1
, __first2
))
5248 else if (__comp(__first2
, __first1
))
5252 *__result
= *__first1
;
5261 * @brief Return the intersection of two sorted ranges.
5262 * @ingroup set_algorithms
5263 * @param __first1 Start of first range.
5264 * @param __last1 End of first range.
5265 * @param __first2 Start of second range.
5266 * @param __last2 End of second range.
5267 * @param __result Start of output range.
5268 * @return End of the output range.
5269 * @ingroup set_algorithms
5271 * This operation iterates over both ranges, copying elements present in
5272 * both ranges in order to the output range. Iterators increment for each
5273 * range. When the current element of one range is less than the other,
5274 * that iterator advances. If an element is contained in both ranges, the
5275 * element from the first range is copied and both ranges advance. The
5276 * output range may not overlap either input range.
5278 template<typename _InputIterator1
, typename _InputIterator2
,
5279 typename _OutputIterator
>
5280 _GLIBCXX20_CONSTEXPR
5281 inline _OutputIterator
5282 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5283 _InputIterator2 __first2
, _InputIterator2 __last2
,
5284 _OutputIterator __result
)
5286 // concept requirements
5287 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5288 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5289 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5290 typename iterator_traits
<_InputIterator1
>::value_type
>)
5291 __glibcxx_function_requires(_LessThanOpConcept
<
5292 typename iterator_traits
<_InputIterator1
>::value_type
,
5293 typename iterator_traits
<_InputIterator2
>::value_type
>)
5294 __glibcxx_function_requires(_LessThanOpConcept
<
5295 typename iterator_traits
<_InputIterator2
>::value_type
,
5296 typename iterator_traits
<_InputIterator1
>::value_type
>)
5297 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5298 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5299 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5300 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5302 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5303 __first2
, __last2
, __result
,
5304 __gnu_cxx::__ops::__iter_less_iter());
5308 * @brief Return the intersection of two sorted ranges using comparison
5310 * @ingroup set_algorithms
5311 * @param __first1 Start of first range.
5312 * @param __last1 End of first range.
5313 * @param __first2 Start of second range.
5314 * @param __last2 End of second range.
5315 * @param __result Start of output range.
5316 * @param __comp The comparison functor.
5317 * @return End of the output range.
5318 * @ingroup set_algorithms
5320 * This operation iterates over both ranges, copying elements present in
5321 * both ranges in order to the output range. Iterators increment for each
5322 * range. When the current element of one range is less than the other
5323 * according to @p __comp, that iterator advances. If an element is
5324 * contained in both ranges according to @p __comp, the element from the
5325 * first range is copied and both ranges advance. The output range may not
5326 * overlap either input range.
5328 template<typename _InputIterator1
, typename _InputIterator2
,
5329 typename _OutputIterator
, typename _Compare
>
5330 _GLIBCXX20_CONSTEXPR
5331 inline _OutputIterator
5332 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5333 _InputIterator2 __first2
, _InputIterator2 __last2
,
5334 _OutputIterator __result
, _Compare __comp
)
5336 // concept requirements
5337 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5338 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5339 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5340 typename iterator_traits
<_InputIterator1
>::value_type
>)
5341 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5342 typename iterator_traits
<_InputIterator1
>::value_type
,
5343 typename iterator_traits
<_InputIterator2
>::value_type
>)
5344 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5345 typename iterator_traits
<_InputIterator2
>::value_type
,
5346 typename iterator_traits
<_InputIterator1
>::value_type
>)
5347 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5348 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5349 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5350 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5352 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5353 __first2
, __last2
, __result
,
5354 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5357 template<typename _InputIterator1
, typename _InputIterator2
,
5358 typename _OutputIterator
,
5360 _GLIBCXX20_CONSTEXPR
5362 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5363 _InputIterator2 __first2
, _InputIterator2 __last2
,
5364 _OutputIterator __result
, _Compare __comp
)
5366 while (__first1
!= __last1
&& __first2
!= __last2
)
5367 if (__comp(__first1
, __first2
))
5369 *__result
= *__first1
;
5373 else if (__comp(__first2
, __first1
))
5380 return std::copy(__first1
, __last1
, __result
);
5384 * @brief Return the difference of two sorted ranges.
5385 * @ingroup set_algorithms
5386 * @param __first1 Start of first range.
5387 * @param __last1 End of first range.
5388 * @param __first2 Start of second range.
5389 * @param __last2 End of second range.
5390 * @param __result Start of output range.
5391 * @return End of the output range.
5392 * @ingroup set_algorithms
5394 * This operation iterates over both ranges, copying elements present in
5395 * the first range but not the second in order to the output range.
5396 * Iterators increment for each range. When the current element of the
5397 * first range is less than the second, that element is copied and the
5398 * iterator advances. If the current element of the second range is less,
5399 * the iterator advances, but no element is copied. If an element is
5400 * contained in both ranges, no elements are copied and both ranges
5401 * advance. The output range may not overlap either input range.
5403 template<typename _InputIterator1
, typename _InputIterator2
,
5404 typename _OutputIterator
>
5405 _GLIBCXX20_CONSTEXPR
5406 inline _OutputIterator
5407 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5408 _InputIterator2 __first2
, _InputIterator2 __last2
,
5409 _OutputIterator __result
)
5411 // concept requirements
5412 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5413 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5414 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5415 typename iterator_traits
<_InputIterator1
>::value_type
>)
5416 __glibcxx_function_requires(_LessThanOpConcept
<
5417 typename iterator_traits
<_InputIterator1
>::value_type
,
5418 typename iterator_traits
<_InputIterator2
>::value_type
>)
5419 __glibcxx_function_requires(_LessThanOpConcept
<
5420 typename iterator_traits
<_InputIterator2
>::value_type
,
5421 typename iterator_traits
<_InputIterator1
>::value_type
>)
5422 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5423 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5424 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5425 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5427 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5428 __first2
, __last2
, __result
,
5429 __gnu_cxx::__ops::__iter_less_iter());
5433 * @brief Return the difference of two sorted ranges using comparison
5435 * @ingroup set_algorithms
5436 * @param __first1 Start of first range.
5437 * @param __last1 End of first range.
5438 * @param __first2 Start of second range.
5439 * @param __last2 End of second range.
5440 * @param __result Start of output range.
5441 * @param __comp The comparison functor.
5442 * @return End of the output range.
5443 * @ingroup set_algorithms
5445 * This operation iterates over both ranges, copying elements present in
5446 * the first range but not the second in order to the output range.
5447 * Iterators increment for each range. When the current element of the
5448 * first range is less than the second according to @p __comp, that element
5449 * is copied and the iterator advances. If the current element of the
5450 * second range is less, no element is copied and the iterator advances.
5451 * If an element is contained in both ranges according to @p __comp, no
5452 * elements are copied and both ranges advance. The output range may not
5453 * overlap either input range.
5455 template<typename _InputIterator1
, typename _InputIterator2
,
5456 typename _OutputIterator
, typename _Compare
>
5457 _GLIBCXX20_CONSTEXPR
5458 inline _OutputIterator
5459 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5460 _InputIterator2 __first2
, _InputIterator2 __last2
,
5461 _OutputIterator __result
, _Compare __comp
)
5463 // concept requirements
5464 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5465 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5466 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5467 typename iterator_traits
<_InputIterator1
>::value_type
>)
5468 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5469 typename iterator_traits
<_InputIterator1
>::value_type
,
5470 typename iterator_traits
<_InputIterator2
>::value_type
>)
5471 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5472 typename iterator_traits
<_InputIterator2
>::value_type
,
5473 typename iterator_traits
<_InputIterator1
>::value_type
>)
5474 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5475 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5476 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5477 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5479 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5480 __first2
, __last2
, __result
,
5481 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5484 template<typename _InputIterator1
, typename _InputIterator2
,
5485 typename _OutputIterator
,
5487 _GLIBCXX20_CONSTEXPR
5489 __set_symmetric_difference(_InputIterator1 __first1
,
5490 _InputIterator1 __last1
,
5491 _InputIterator2 __first2
,
5492 _InputIterator2 __last2
,
5493 _OutputIterator __result
,
5496 while (__first1
!= __last1
&& __first2
!= __last2
)
5497 if (__comp(__first1
, __first2
))
5499 *__result
= *__first1
;
5503 else if (__comp(__first2
, __first1
))
5505 *__result
= *__first2
;
5514 return std::copy(__first2
, __last2
,
5515 std::copy(__first1
, __last1
, __result
));
5519 * @brief Return the symmetric difference of two sorted ranges.
5520 * @ingroup set_algorithms
5521 * @param __first1 Start of first range.
5522 * @param __last1 End of first range.
5523 * @param __first2 Start of second range.
5524 * @param __last2 End of second range.
5525 * @param __result Start of output range.
5526 * @return End of the output range.
5527 * @ingroup set_algorithms
5529 * This operation iterates over both ranges, copying elements present in
5530 * one range but not the other in order to the output range. Iterators
5531 * increment for each range. When the current element of one range is less
5532 * than the other, that element is copied and the iterator advances. If an
5533 * element is contained in both ranges, no elements are copied and both
5534 * ranges advance. The output range may not overlap either input range.
5536 template<typename _InputIterator1
, typename _InputIterator2
,
5537 typename _OutputIterator
>
5538 _GLIBCXX20_CONSTEXPR
5539 inline _OutputIterator
5540 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5541 _InputIterator2 __first2
, _InputIterator2 __last2
,
5542 _OutputIterator __result
)
5544 // concept requirements
5545 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5546 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5547 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5548 typename iterator_traits
<_InputIterator1
>::value_type
>)
5549 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5550 typename iterator_traits
<_InputIterator2
>::value_type
>)
5551 __glibcxx_function_requires(_LessThanOpConcept
<
5552 typename iterator_traits
<_InputIterator1
>::value_type
,
5553 typename iterator_traits
<_InputIterator2
>::value_type
>)
5554 __glibcxx_function_requires(_LessThanOpConcept
<
5555 typename iterator_traits
<_InputIterator2
>::value_type
,
5556 typename iterator_traits
<_InputIterator1
>::value_type
>)
5557 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5558 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5559 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5560 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5562 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5563 __first2
, __last2
, __result
,
5564 __gnu_cxx::__ops::__iter_less_iter());
5568 * @brief Return the symmetric difference of two sorted ranges using
5569 * comparison functor.
5570 * @ingroup set_algorithms
5571 * @param __first1 Start of first range.
5572 * @param __last1 End of first range.
5573 * @param __first2 Start of second range.
5574 * @param __last2 End of second range.
5575 * @param __result Start of output range.
5576 * @param __comp The comparison functor.
5577 * @return End of the output range.
5578 * @ingroup set_algorithms
5580 * This operation iterates over both ranges, copying elements present in
5581 * one range but not the other in order to the output range. Iterators
5582 * increment for each range. When the current element of one range is less
5583 * than the other according to @p comp, that element is copied and the
5584 * iterator advances. If an element is contained in both ranges according
5585 * to @p __comp, no elements are copied and both ranges advance. The output
5586 * range may not overlap either input range.
5588 template<typename _InputIterator1
, typename _InputIterator2
,
5589 typename _OutputIterator
, typename _Compare
>
5590 _GLIBCXX20_CONSTEXPR
5591 inline _OutputIterator
5592 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5593 _InputIterator2 __first2
, _InputIterator2 __last2
,
5594 _OutputIterator __result
,
5597 // concept requirements
5598 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5599 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5600 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5601 typename iterator_traits
<_InputIterator1
>::value_type
>)
5602 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5603 typename iterator_traits
<_InputIterator2
>::value_type
>)
5604 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5605 typename iterator_traits
<_InputIterator1
>::value_type
,
5606 typename iterator_traits
<_InputIterator2
>::value_type
>)
5607 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5608 typename iterator_traits
<_InputIterator2
>::value_type
,
5609 typename iterator_traits
<_InputIterator1
>::value_type
>)
5610 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5611 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5612 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5613 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5615 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5616 __first2
, __last2
, __result
,
5617 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5620 template<typename _ForwardIterator
, typename _Compare
>
5621 _GLIBCXX14_CONSTEXPR
5623 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5626 if (__first
== __last
)
5628 _ForwardIterator __result
= __first
;
5629 while (++__first
!= __last
)
5630 if (__comp(__first
, __result
))
5636 * @brief Return the minimum element in a range.
5637 * @ingroup sorting_algorithms
5638 * @param __first Start of range.
5639 * @param __last End of range.
5640 * @return Iterator referencing the first instance of the smallest value.
5642 template<typename _ForwardIterator
>
5643 _GLIBCXX14_CONSTEXPR
5645 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5647 // concept requirements
5648 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5649 __glibcxx_function_requires(_LessThanComparableConcept
<
5650 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5651 __glibcxx_requires_valid_range(__first
, __last
);
5652 __glibcxx_requires_irreflexive(__first
, __last
);
5654 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5655 __gnu_cxx::__ops::__iter_less_iter());
5659 * @brief Return the minimum element in a range using comparison functor.
5660 * @ingroup sorting_algorithms
5661 * @param __first Start of range.
5662 * @param __last End of range.
5663 * @param __comp Comparison functor.
5664 * @return Iterator referencing the first instance of the smallest value
5665 * according to __comp.
5667 template<typename _ForwardIterator
, typename _Compare
>
5668 _GLIBCXX14_CONSTEXPR
5669 inline _ForwardIterator
5670 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5673 // concept requirements
5674 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5675 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5676 typename iterator_traits
<_ForwardIterator
>::value_type
,
5677 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5678 __glibcxx_requires_valid_range(__first
, __last
);
5679 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5681 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5682 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5685 template<typename _ForwardIterator
, typename _Compare
>
5686 _GLIBCXX14_CONSTEXPR
5688 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5691 if (__first
== __last
) return __first
;
5692 _ForwardIterator __result
= __first
;
5693 while (++__first
!= __last
)
5694 if (__comp(__result
, __first
))
5700 * @brief Return the maximum element in a range.
5701 * @ingroup sorting_algorithms
5702 * @param __first Start of range.
5703 * @param __last End of range.
5704 * @return Iterator referencing the first instance of the largest value.
5706 template<typename _ForwardIterator
>
5707 _GLIBCXX14_CONSTEXPR
5708 inline _ForwardIterator
5709 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5711 // concept requirements
5712 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5713 __glibcxx_function_requires(_LessThanComparableConcept
<
5714 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5715 __glibcxx_requires_valid_range(__first
, __last
);
5716 __glibcxx_requires_irreflexive(__first
, __last
);
5718 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5719 __gnu_cxx::__ops::__iter_less_iter());
5723 * @brief Return the maximum element in a range using comparison functor.
5724 * @ingroup sorting_algorithms
5725 * @param __first Start of range.
5726 * @param __last End of range.
5727 * @param __comp Comparison functor.
5728 * @return Iterator referencing the first instance of the largest value
5729 * according to __comp.
5731 template<typename _ForwardIterator
, typename _Compare
>
5732 _GLIBCXX14_CONSTEXPR
5733 inline _ForwardIterator
5734 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5737 // concept requirements
5738 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5739 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5740 typename iterator_traits
<_ForwardIterator
>::value_type
,
5741 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5742 __glibcxx_requires_valid_range(__first
, __last
);
5743 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5745 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5746 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5749 #if __cplusplus >= 201402L
5750 /// Reservoir sampling algorithm.
5751 template<typename _InputIterator
, typename _RandomAccessIterator
,
5752 typename _Size
, typename _UniformRandomBitGenerator
>
5753 _RandomAccessIterator
5754 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5755 _RandomAccessIterator __out
, random_access_iterator_tag
,
5756 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5758 using __distrib_type
= uniform_int_distribution
<_Size
>;
5759 using __param_type
= typename
__distrib_type::param_type
;
5760 __distrib_type __d
{};
5761 _Size __sample_sz
= 0;
5762 while (__first
!= __last
&& __sample_sz
!= __n
)
5764 __out
[__sample_sz
++] = *__first
;
5767 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5768 ++__first
, (void) ++__pop_sz
)
5770 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5772 __out
[__k
] = *__first
;
5774 return __out
+ __sample_sz
;
5777 /// Selection sampling algorithm.
5778 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5779 typename _Size
, typename _UniformRandomBitGenerator
>
5781 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5782 forward_iterator_tag
,
5783 _OutputIterator __out
, _Cat
,
5784 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5786 using __distrib_type
= uniform_int_distribution
<_Size
>;
5787 using __param_type
= typename
__distrib_type::param_type
;
5788 using _USize
= make_unsigned_t
<_Size
>;
5789 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5790 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5792 if (__first
== __last
)
5795 __distrib_type __d
{};
5796 _Size __unsampled_sz
= std::distance(__first
, __last
);
5797 __n
= std::min(__n
, __unsampled_sz
);
5799 // If possible, we use __gen_two_uniform_ints to efficiently produce
5800 // two random numbers using a single distribution invocation:
5802 const __uc_type __urngrange
= __g
.max() - __g
.min();
5803 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5804 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5807 while (__n
!= 0 && __unsampled_sz
>= 2)
5809 const pair
<_Size
, _Size
> __p
=
5810 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5813 if (__p
.first
< __n
)
5815 *__out
++ = *__first
;
5821 if (__n
== 0) break;
5824 if (__p
.second
< __n
)
5826 *__out
++ = *__first
;
5834 // The loop above is otherwise equivalent to this one-at-a-time version:
5836 for (; __n
!= 0; ++__first
)
5837 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5839 *__out
++ = *__first
;
5845 #if __cplusplus > 201402L
5846 #define __cpp_lib_sample 201603
5847 /// Take a random sample from a population.
5848 template<typename _PopulationIterator
, typename _SampleIterator
,
5849 typename _Distance
, typename _UniformRandomBitGenerator
>
5851 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5852 _SampleIterator __out
, _Distance __n
,
5853 _UniformRandomBitGenerator
&& __g
)
5855 using __pop_cat
= typename
5856 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5857 using __samp_cat
= typename
5858 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5861 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5862 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5863 "output range must use a RandomAccessIterator when input range"
5864 " does not meet the ForwardIterator requirements");
5866 static_assert(is_integral
<_Distance
>::value
,
5867 "sample size must be an integer type");
5869 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5870 return _GLIBCXX_STD_A::
5871 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5872 std::forward
<_UniformRandomBitGenerator
>(__g
));
5877 _GLIBCXX_END_NAMESPACE_ALGO
5878 _GLIBCXX_END_NAMESPACE_VERSION
5881 #endif /* _STL_ALGO_H */