1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2020 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 <cstdlib> // for rand
60 #include <bits/algorithmfwd.h>
61 #include <bits/stl_heap.h>
62 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
63 #include <bits/predefined_ops.h>
65 #if __cplusplus >= 201103L
66 #include <bits/uniform_int_dist.h>
69 // See concept_check.h for the __glibcxx_*_requires macros.
71 namespace std
_GLIBCXX_VISIBILITY(default)
73 _GLIBCXX_BEGIN_NAMESPACE_VERSION
75 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
76 template<typename _Iterator
, typename _Compare
>
79 __move_median_to_first(_Iterator __result
,_Iterator __a
, _Iterator __b
,
80 _Iterator __c
, _Compare __comp
)
85 std::iter_swap(__result
, __b
);
86 else if (__comp(__a
, __c
))
87 std::iter_swap(__result
, __c
);
89 std::iter_swap(__result
, __a
);
91 else if (__comp(__a
, __c
))
92 std::iter_swap(__result
, __a
);
93 else if (__comp(__b
, __c
))
94 std::iter_swap(__result
, __c
);
96 std::iter_swap(__result
, __b
);
99 /// Provided for stable_partition to use.
100 template<typename _InputIterator
, typename _Predicate
>
102 inline _InputIterator
103 __find_if_not(_InputIterator __first
, _InputIterator __last
,
106 return std::__find_if(__first
, __last
,
107 __gnu_cxx::__ops::__negate(__pred
),
108 std::__iterator_category(__first
));
111 /// Like find_if_not(), but uses and updates a count of the
112 /// remaining range length instead of comparing against an end
114 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
117 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
119 for (; __len
; --__len
, (void) ++__first
)
120 if (!__pred(__first
))
127 // set_symmetric_difference
138 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
139 typename _BinaryPredicate
>
142 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
143 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
144 _BinaryPredicate __predicate
)
146 // Test for empty ranges
147 if (__first1
== __last1
|| __first2
== __last2
)
150 // Test for a pattern of length 1.
151 _ForwardIterator2
__p1(__first2
);
152 if (++__p1
== __last2
)
153 return std::__find_if(__first1
, __last1
,
154 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
157 _ForwardIterator1 __current
= __first1
;
162 std::__find_if(__first1
, __last1
,
163 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
165 if (__first1
== __last1
)
168 _ForwardIterator2 __p
= __p1
;
169 __current
= __first1
;
170 if (++__current
== __last1
)
173 while (__predicate(__current
, __p
))
175 if (++__p
== __last2
)
177 if (++__current
== __last1
)
188 * This is an helper function for search_n overloaded for forward iterators.
190 template<typename _ForwardIterator
, typename _Integer
,
191 typename _UnaryPredicate
>
194 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
195 _Integer __count
, _UnaryPredicate __unary_pred
,
196 std::forward_iterator_tag
)
198 __first
= std::__find_if(__first
, __last
, __unary_pred
);
199 while (__first
!= __last
)
201 typename iterator_traits
<_ForwardIterator
>::difference_type
203 _ForwardIterator __i
= __first
;
205 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
214 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
220 * This is an helper function for search_n overloaded for random access
223 template<typename _RandomAccessIter
, typename _Integer
,
224 typename _UnaryPredicate
>
227 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
228 _Integer __count
, _UnaryPredicate __unary_pred
,
229 std::random_access_iterator_tag
)
231 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
234 _DistanceType __tailSize
= __last
- __first
;
235 _DistanceType __remainder
= __count
;
237 while (__remainder
<= __tailSize
) // the main loop...
239 __first
+= __remainder
;
240 __tailSize
-= __remainder
;
241 // __first here is always pointing to one past the last element of
242 // next possible match.
243 _RandomAccessIter __backTrack
= __first
;
244 while (__unary_pred(--__backTrack
))
246 if (--__remainder
== 0)
247 return (__first
- __count
); // Success
249 __remainder
= __count
+ 1 - (__first
- __backTrack
);
251 return __last
; // Failure
254 template<typename _ForwardIterator
, typename _Integer
,
255 typename _UnaryPredicate
>
258 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
260 _UnaryPredicate __unary_pred
)
266 return std::__find_if(__first
, __last
, __unary_pred
);
268 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
269 std::__iterator_category(__first
));
272 // find_end for forward iterators.
273 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
274 typename _BinaryPredicate
>
277 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
278 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
279 forward_iterator_tag
, forward_iterator_tag
,
280 _BinaryPredicate __comp
)
282 if (__first2
== __last2
)
285 _ForwardIterator1 __result
= __last1
;
288 _ForwardIterator1 __new_result
289 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
290 if (__new_result
== __last1
)
294 __result
= __new_result
;
295 __first1
= __new_result
;
301 // find_end for bidirectional iterators (much faster).
302 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
303 typename _BinaryPredicate
>
305 _BidirectionalIterator1
306 __find_end(_BidirectionalIterator1 __first1
,
307 _BidirectionalIterator1 __last1
,
308 _BidirectionalIterator2 __first2
,
309 _BidirectionalIterator2 __last2
,
310 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
311 _BinaryPredicate __comp
)
313 // concept requirements
314 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
315 _BidirectionalIterator1
>)
316 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
317 _BidirectionalIterator2
>)
319 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
320 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
322 _RevIterator1
__rlast1(__first1
);
323 _RevIterator2
__rlast2(__first2
);
324 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
325 _RevIterator2(__last2
), __rlast2
,
328 if (__rresult
== __rlast1
)
332 _BidirectionalIterator1 __result
= __rresult
.base();
333 std::advance(__result
, -std::distance(__first2
, __last2
));
339 * @brief Find last matching subsequence in a sequence.
340 * @ingroup non_mutating_algorithms
341 * @param __first1 Start of range to search.
342 * @param __last1 End of range to search.
343 * @param __first2 Start of sequence to match.
344 * @param __last2 End of sequence to match.
345 * @return The last iterator @c i in the range
346 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
347 * @p *(__first2+N) for each @c N in the range @p
348 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
350 * Searches the range @p [__first1,__last1) for a sub-sequence that
351 * compares equal value-by-value with the sequence given by @p
352 * [__first2,__last2) and returns an iterator to the __first
353 * element of the sub-sequence, or @p __last1 if the sub-sequence
354 * is not found. The sub-sequence will be the last such
355 * subsequence contained in [__first1,__last1).
357 * Because the sub-sequence must lie completely within the range @p
358 * [__first1,__last1) it must start at a position less than @p
359 * __last1-(__last2-__first2) where @p __last2-__first2 is the
360 * length of the sub-sequence. This means that the returned
361 * iterator @c i will be in the range @p
362 * [__first1,__last1-(__last2-__first2))
364 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
366 inline _ForwardIterator1
367 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
368 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
370 // concept requirements
371 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
372 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
373 __glibcxx_function_requires(_EqualOpConcept
<
374 typename iterator_traits
<_ForwardIterator1
>::value_type
,
375 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
376 __glibcxx_requires_valid_range(__first1
, __last1
);
377 __glibcxx_requires_valid_range(__first2
, __last2
);
379 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
380 std::__iterator_category(__first1
),
381 std::__iterator_category(__first2
),
382 __gnu_cxx::__ops::__iter_equal_to_iter());
386 * @brief Find last matching subsequence in a sequence using a predicate.
387 * @ingroup non_mutating_algorithms
388 * @param __first1 Start of range to search.
389 * @param __last1 End of range to search.
390 * @param __first2 Start of sequence to match.
391 * @param __last2 End of sequence to match.
392 * @param __comp The predicate to use.
393 * @return The last iterator @c i in the range @p
394 * [__first1,__last1-(__last2-__first2)) such that @c
395 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
396 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
399 * Searches the range @p [__first1,__last1) for a sub-sequence that
400 * compares equal value-by-value with the sequence given by @p
401 * [__first2,__last2) using comp as a predicate and returns an
402 * iterator to the first element of the sub-sequence, or @p __last1
403 * if the sub-sequence is not found. The sub-sequence will be the
404 * last such subsequence contained in [__first,__last1).
406 * Because the sub-sequence must lie completely within the range @p
407 * [__first1,__last1) it must start at a position less than @p
408 * __last1-(__last2-__first2) where @p __last2-__first2 is the
409 * length of the sub-sequence. This means that the returned
410 * iterator @c i will be in the range @p
411 * [__first1,__last1-(__last2-__first2))
413 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
414 typename _BinaryPredicate
>
416 inline _ForwardIterator1
417 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
418 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
419 _BinaryPredicate __comp
)
421 // concept requirements
422 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
423 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
424 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
425 typename iterator_traits
<_ForwardIterator1
>::value_type
,
426 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
427 __glibcxx_requires_valid_range(__first1
, __last1
);
428 __glibcxx_requires_valid_range(__first2
, __last2
);
430 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
431 std::__iterator_category(__first1
),
432 std::__iterator_category(__first2
),
433 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
436 #if __cplusplus >= 201103L
438 * @brief Checks that a predicate is true for all the elements
440 * @ingroup non_mutating_algorithms
441 * @param __first An input iterator.
442 * @param __last An input iterator.
443 * @param __pred A predicate.
444 * @return True if the check is true, false otherwise.
446 * Returns true if @p __pred is true for each element in the range
447 * @p [__first,__last), and false otherwise.
449 template<typename _InputIterator
, typename _Predicate
>
452 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
453 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
456 * @brief Checks that a predicate is false for all the elements
458 * @ingroup non_mutating_algorithms
459 * @param __first An input iterator.
460 * @param __last An input iterator.
461 * @param __pred A predicate.
462 * @return True if the check is true, false otherwise.
464 * Returns true if @p __pred is false for each element in the range
465 * @p [__first,__last), and false otherwise.
467 template<typename _InputIterator
, typename _Predicate
>
470 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
471 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
474 * @brief Checks that a predicate is true for at least one element
476 * @ingroup non_mutating_algorithms
477 * @param __first An input iterator.
478 * @param __last An input iterator.
479 * @param __pred A predicate.
480 * @return True if the check is true, false otherwise.
482 * Returns true if an element exists in the range @p
483 * [__first,__last) such that @p __pred is true, and false
486 template<typename _InputIterator
, typename _Predicate
>
489 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
490 { return !std::none_of(__first
, __last
, __pred
); }
493 * @brief Find the first element in a sequence for which a
494 * predicate is false.
495 * @ingroup non_mutating_algorithms
496 * @param __first An input iterator.
497 * @param __last An input iterator.
498 * @param __pred A predicate.
499 * @return The first iterator @c i in the range @p [__first,__last)
500 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
502 template<typename _InputIterator
, typename _Predicate
>
504 inline _InputIterator
505 find_if_not(_InputIterator __first
, _InputIterator __last
,
508 // concept requirements
509 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
510 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
511 typename iterator_traits
<_InputIterator
>::value_type
>)
512 __glibcxx_requires_valid_range(__first
, __last
);
513 return std::__find_if_not(__first
, __last
,
514 __gnu_cxx::__ops::__pred_iter(__pred
));
518 * @brief Checks whether the sequence is partitioned.
519 * @ingroup mutating_algorithms
520 * @param __first An input iterator.
521 * @param __last An input iterator.
522 * @param __pred A predicate.
523 * @return True if the range @p [__first,__last) is partioned by @p __pred,
524 * i.e. if all elements that satisfy @p __pred appear before those that
527 template<typename _InputIterator
, typename _Predicate
>
530 is_partitioned(_InputIterator __first
, _InputIterator __last
,
533 __first
= std::find_if_not(__first
, __last
, __pred
);
534 if (__first
== __last
)
537 return std::none_of(__first
, __last
, __pred
);
541 * @brief Find the partition point of a partitioned range.
542 * @ingroup mutating_algorithms
543 * @param __first An iterator.
544 * @param __last Another iterator.
545 * @param __pred A predicate.
546 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
547 * and @p none_of(mid, __last, __pred) are both true.
549 template<typename _ForwardIterator
, typename _Predicate
>
552 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
555 // concept requirements
556 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
557 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
558 typename iterator_traits
<_ForwardIterator
>::value_type
>)
560 // A specific debug-mode test will be necessary...
561 __glibcxx_requires_valid_range(__first
, __last
);
563 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
566 _DistanceType __len
= std::distance(__first
, __last
);
570 _DistanceType __half
= __len
>> 1;
571 _ForwardIterator __middle
= __first
;
572 std::advance(__middle
, __half
);
573 if (__pred(*__middle
))
577 __len
= __len
- __half
- 1;
586 template<typename _InputIterator
, typename _OutputIterator
,
590 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
591 _OutputIterator __result
, _Predicate __pred
)
593 for (; __first
!= __last
; ++__first
)
594 if (!__pred(__first
))
596 *__result
= *__first
;
603 * @brief Copy a sequence, removing elements of a given value.
604 * @ingroup mutating_algorithms
605 * @param __first An input iterator.
606 * @param __last An input iterator.
607 * @param __result An output iterator.
608 * @param __value The value to be removed.
609 * @return An iterator designating the end of the resulting sequence.
611 * Copies each element in the range @p [__first,__last) not equal
612 * to @p __value to the range beginning at @p __result.
613 * remove_copy() is stable, so the relative order of elements that
614 * are copied is unchanged.
616 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
618 inline _OutputIterator
619 remove_copy(_InputIterator __first
, _InputIterator __last
,
620 _OutputIterator __result
, const _Tp
& __value
)
622 // concept requirements
623 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
624 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
625 typename iterator_traits
<_InputIterator
>::value_type
>)
626 __glibcxx_function_requires(_EqualOpConcept
<
627 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
628 __glibcxx_requires_valid_range(__first
, __last
);
630 return std::__remove_copy_if(__first
, __last
, __result
,
631 __gnu_cxx::__ops::__iter_equals_val(__value
));
635 * @brief Copy a sequence, removing elements for which a predicate is true.
636 * @ingroup mutating_algorithms
637 * @param __first An input iterator.
638 * @param __last An input iterator.
639 * @param __result An output iterator.
640 * @param __pred A predicate.
641 * @return An iterator designating the end of the resulting sequence.
643 * Copies each element in the range @p [__first,__last) for which
644 * @p __pred returns false to the range beginning at @p __result.
646 * remove_copy_if() is stable, so the relative order of elements that are
647 * copied is unchanged.
649 template<typename _InputIterator
, typename _OutputIterator
,
652 inline _OutputIterator
653 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
654 _OutputIterator __result
, _Predicate __pred
)
656 // concept requirements
657 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
658 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
659 typename iterator_traits
<_InputIterator
>::value_type
>)
660 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
661 typename iterator_traits
<_InputIterator
>::value_type
>)
662 __glibcxx_requires_valid_range(__first
, __last
);
664 return std::__remove_copy_if(__first
, __last
, __result
,
665 __gnu_cxx::__ops::__pred_iter(__pred
));
668 #if __cplusplus >= 201103L
670 * @brief Copy the elements of a sequence for which a predicate is true.
671 * @ingroup mutating_algorithms
672 * @param __first An input iterator.
673 * @param __last An input iterator.
674 * @param __result An output iterator.
675 * @param __pred A predicate.
676 * @return An iterator designating the end of the resulting sequence.
678 * Copies each element in the range @p [__first,__last) for which
679 * @p __pred returns true to the range beginning at @p __result.
681 * copy_if() is stable, so the relative order of elements that are
682 * copied is unchanged.
684 template<typename _InputIterator
, typename _OutputIterator
,
688 copy_if(_InputIterator __first
, _InputIterator __last
,
689 _OutputIterator __result
, _Predicate __pred
)
691 // concept requirements
692 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
693 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
694 typename iterator_traits
<_InputIterator
>::value_type
>)
695 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
696 typename iterator_traits
<_InputIterator
>::value_type
>)
697 __glibcxx_requires_valid_range(__first
, __last
);
699 for (; __first
!= __last
; ++__first
)
700 if (__pred(*__first
))
702 *__result
= *__first
;
708 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
711 __copy_n(_InputIterator __first
, _Size __n
,
712 _OutputIterator __result
, input_iterator_tag
)
714 return std::__niter_wrap(__result
,
715 __copy_n_a(__first
, __n
,
716 std::__niter_base(__result
), true));
719 template<typename _RandomAccessIterator
, typename _Size
,
720 typename _OutputIterator
>
722 inline _OutputIterator
723 __copy_n(_RandomAccessIterator __first
, _Size __n
,
724 _OutputIterator __result
, random_access_iterator_tag
)
725 { return std::copy(__first
, __first
+ __n
, __result
); }
728 * @brief Copies the range [first,first+n) into [result,result+n).
729 * @ingroup mutating_algorithms
730 * @param __first An input iterator.
731 * @param __n The number of elements to copy.
732 * @param __result An output iterator.
735 * This inline function will boil down to a call to @c memmove whenever
736 * possible. Failing that, if random access iterators are passed, then the
737 * loop count will be known (and therefore a candidate for compiler
738 * optimizations such as unrolling).
740 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
742 inline _OutputIterator
743 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
745 // concept requirements
746 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
747 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
748 typename iterator_traits
<_InputIterator
>::value_type
>)
750 const auto __n2
= std::__size_to_integer(__n
);
754 __glibcxx_requires_can_increment(__first
, __n2
);
755 __glibcxx_requires_can_increment(__result
, __n2
);
757 return std::__copy_n(__first
, __n2
, __result
,
758 std::__iterator_category(__first
));
762 * @brief Copy the elements of a sequence to separate output sequences
763 * depending on the truth value of a predicate.
764 * @ingroup mutating_algorithms
765 * @param __first An input iterator.
766 * @param __last An input iterator.
767 * @param __out_true An output iterator.
768 * @param __out_false An output iterator.
769 * @param __pred A predicate.
770 * @return A pair designating the ends of the resulting sequences.
772 * Copies each element in the range @p [__first,__last) for which
773 * @p __pred returns true to the range beginning at @p out_true
774 * and each element for which @p __pred returns false to @p __out_false.
776 template<typename _InputIterator
, typename _OutputIterator1
,
777 typename _OutputIterator2
, typename _Predicate
>
779 pair
<_OutputIterator1
, _OutputIterator2
>
780 partition_copy(_InputIterator __first
, _InputIterator __last
,
781 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
784 // concept requirements
785 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
786 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
787 typename iterator_traits
<_InputIterator
>::value_type
>)
788 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
789 typename iterator_traits
<_InputIterator
>::value_type
>)
790 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
791 typename iterator_traits
<_InputIterator
>::value_type
>)
792 __glibcxx_requires_valid_range(__first
, __last
);
794 for (; __first
!= __last
; ++__first
)
795 if (__pred(*__first
))
797 *__out_true
= *__first
;
802 *__out_false
= *__first
;
806 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
810 template<typename _ForwardIterator
, typename _Predicate
>
813 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
816 __first
= std::__find_if(__first
, __last
, __pred
);
817 if (__first
== __last
)
819 _ForwardIterator __result
= __first
;
821 for (; __first
!= __last
; ++__first
)
822 if (!__pred(__first
))
824 *__result
= _GLIBCXX_MOVE(*__first
);
831 * @brief Remove elements from a sequence.
832 * @ingroup mutating_algorithms
833 * @param __first An input iterator.
834 * @param __last An input iterator.
835 * @param __value The value to be removed.
836 * @return An iterator designating the end of the resulting sequence.
838 * All elements equal to @p __value are removed from the range
839 * @p [__first,__last).
841 * remove() is stable, so the relative order of elements that are
842 * not removed is unchanged.
844 * Elements between the end of the resulting sequence and @p __last
845 * are still present, but their value is unspecified.
847 template<typename _ForwardIterator
, typename _Tp
>
849 inline _ForwardIterator
850 remove(_ForwardIterator __first
, _ForwardIterator __last
,
853 // concept requirements
854 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
856 __glibcxx_function_requires(_EqualOpConcept
<
857 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
858 __glibcxx_requires_valid_range(__first
, __last
);
860 return std::__remove_if(__first
, __last
,
861 __gnu_cxx::__ops::__iter_equals_val(__value
));
865 * @brief Remove elements from a sequence using a predicate.
866 * @ingroup mutating_algorithms
867 * @param __first A forward iterator.
868 * @param __last A forward iterator.
869 * @param __pred A predicate.
870 * @return An iterator designating the end of the resulting sequence.
872 * All elements for which @p __pred returns true are removed from the range
873 * @p [__first,__last).
875 * remove_if() is stable, so the relative order of elements that are
876 * not removed is unchanged.
878 * Elements between the end of the resulting sequence and @p __last
879 * are still present, but their value is unspecified.
881 template<typename _ForwardIterator
, typename _Predicate
>
883 inline _ForwardIterator
884 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
887 // concept requirements
888 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
890 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
891 typename iterator_traits
<_ForwardIterator
>::value_type
>)
892 __glibcxx_requires_valid_range(__first
, __last
);
894 return std::__remove_if(__first
, __last
,
895 __gnu_cxx::__ops::__pred_iter(__pred
));
898 template<typename _ForwardIterator
, typename _BinaryPredicate
>
901 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
902 _BinaryPredicate __binary_pred
)
904 if (__first
== __last
)
906 _ForwardIterator __next
= __first
;
907 while (++__next
!= __last
)
909 if (__binary_pred(__first
, __next
))
916 template<typename _ForwardIterator
, typename _BinaryPredicate
>
919 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
920 _BinaryPredicate __binary_pred
)
922 // Skip the beginning, if already unique.
923 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
924 if (__first
== __last
)
927 // Do the real copy work.
928 _ForwardIterator __dest
= __first
;
930 while (++__first
!= __last
)
931 if (!__binary_pred(__dest
, __first
))
932 *++__dest
= _GLIBCXX_MOVE(*__first
);
937 * @brief Remove consecutive duplicate values from a sequence.
938 * @ingroup mutating_algorithms
939 * @param __first A forward iterator.
940 * @param __last A forward iterator.
941 * @return An iterator designating the end of the resulting sequence.
943 * Removes all but the first element from each group of consecutive
944 * values that compare equal.
945 * unique() is stable, so the relative order of elements that are
946 * not removed is unchanged.
947 * Elements between the end of the resulting sequence and @p __last
948 * are still present, but their value is unspecified.
950 template<typename _ForwardIterator
>
952 inline _ForwardIterator
953 unique(_ForwardIterator __first
, _ForwardIterator __last
)
955 // concept requirements
956 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
958 __glibcxx_function_requires(_EqualityComparableConcept
<
959 typename iterator_traits
<_ForwardIterator
>::value_type
>)
960 __glibcxx_requires_valid_range(__first
, __last
);
962 return std::__unique(__first
, __last
,
963 __gnu_cxx::__ops::__iter_equal_to_iter());
967 * @brief Remove consecutive values from a sequence using a predicate.
968 * @ingroup mutating_algorithms
969 * @param __first A forward iterator.
970 * @param __last A forward iterator.
971 * @param __binary_pred A binary predicate.
972 * @return An iterator designating the end of the resulting sequence.
974 * Removes all but the first element from each group of consecutive
975 * values for which @p __binary_pred returns true.
976 * unique() is stable, so the relative order of elements that are
977 * not removed is unchanged.
978 * Elements between the end of the resulting sequence and @p __last
979 * are still present, but their value is unspecified.
981 template<typename _ForwardIterator
, typename _BinaryPredicate
>
983 inline _ForwardIterator
984 unique(_ForwardIterator __first
, _ForwardIterator __last
,
985 _BinaryPredicate __binary_pred
)
987 // concept requirements
988 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
990 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
991 typename iterator_traits
<_ForwardIterator
>::value_type
,
992 typename iterator_traits
<_ForwardIterator
>::value_type
>)
993 __glibcxx_requires_valid_range(__first
, __last
);
995 return std::__unique(__first
, __last
,
996 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1000 * This is an uglified
1001 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1003 * overloaded for forward iterators and output iterator as result.
1005 template<typename _ForwardIterator
, typename _OutputIterator
,
1006 typename _BinaryPredicate
>
1007 _GLIBCXX20_CONSTEXPR
1009 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1010 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1011 forward_iterator_tag
, output_iterator_tag
)
1013 // concept requirements -- iterators already checked
1014 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1015 typename iterator_traits
<_ForwardIterator
>::value_type
,
1016 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1018 _ForwardIterator __next
= __first
;
1019 *__result
= *__first
;
1020 while (++__next
!= __last
)
1021 if (!__binary_pred(__first
, __next
))
1024 *++__result
= *__first
;
1030 * This is an uglified
1031 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1033 * overloaded for input iterators and output iterator as result.
1035 template<typename _InputIterator
, typename _OutputIterator
,
1036 typename _BinaryPredicate
>
1037 _GLIBCXX20_CONSTEXPR
1039 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1040 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1041 input_iterator_tag
, output_iterator_tag
)
1043 // concept requirements -- iterators already checked
1044 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1045 typename iterator_traits
<_InputIterator
>::value_type
,
1046 typename iterator_traits
<_InputIterator
>::value_type
>)
1048 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1049 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1051 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1052 *__result
= __value
;
1053 while (++__first
!= __last
)
1054 if (!__rebound_pred(__first
, __value
))
1057 *++__result
= __value
;
1063 * This is an uglified
1064 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1066 * overloaded for input iterators and forward iterator as result.
1068 template<typename _InputIterator
, typename _ForwardIterator
,
1069 typename _BinaryPredicate
>
1070 _GLIBCXX20_CONSTEXPR
1072 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1073 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1074 input_iterator_tag
, forward_iterator_tag
)
1076 // concept requirements -- iterators already checked
1077 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1078 typename iterator_traits
<_ForwardIterator
>::value_type
,
1079 typename iterator_traits
<_InputIterator
>::value_type
>)
1080 *__result
= *__first
;
1081 while (++__first
!= __last
)
1082 if (!__binary_pred(__result
, __first
))
1083 *++__result
= *__first
;
1088 * This is an uglified reverse(_BidirectionalIterator,
1089 * _BidirectionalIterator)
1090 * overloaded for bidirectional iterators.
1092 template<typename _BidirectionalIterator
>
1093 _GLIBCXX20_CONSTEXPR
1095 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1096 bidirectional_iterator_tag
)
1099 if (__first
== __last
|| __first
== --__last
)
1103 std::iter_swap(__first
, __last
);
1109 * This is an uglified reverse(_BidirectionalIterator,
1110 * _BidirectionalIterator)
1111 * overloaded for random access iterators.
1113 template<typename _RandomAccessIterator
>
1114 _GLIBCXX20_CONSTEXPR
1116 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1117 random_access_iterator_tag
)
1119 if (__first
== __last
)
1122 while (__first
< __last
)
1124 std::iter_swap(__first
, __last
);
1131 * @brief Reverse a sequence.
1132 * @ingroup mutating_algorithms
1133 * @param __first A bidirectional iterator.
1134 * @param __last A bidirectional iterator.
1135 * @return reverse() returns no value.
1137 * Reverses the order of the elements in the range @p [__first,__last),
1138 * so that the first element becomes the last etc.
1139 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1140 * swaps @p *(__first+i) and @p *(__last-(i+1))
1142 template<typename _BidirectionalIterator
>
1143 _GLIBCXX20_CONSTEXPR
1145 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1147 // concept requirements
1148 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1149 _BidirectionalIterator
>)
1150 __glibcxx_requires_valid_range(__first
, __last
);
1151 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1155 * @brief Copy a sequence, reversing its elements.
1156 * @ingroup mutating_algorithms
1157 * @param __first A bidirectional iterator.
1158 * @param __last A bidirectional iterator.
1159 * @param __result An output iterator.
1160 * @return An iterator designating the end of the resulting sequence.
1162 * Copies the elements in the range @p [__first,__last) to the
1163 * range @p [__result,__result+(__last-__first)) such that the
1164 * order of the elements is reversed. For every @c i such that @p
1165 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1166 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1167 * The ranges @p [__first,__last) and @p
1168 * [__result,__result+(__last-__first)) must not overlap.
1170 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1171 _GLIBCXX20_CONSTEXPR
1173 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1174 _OutputIterator __result
)
1176 // concept requirements
1177 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1178 _BidirectionalIterator
>)
1179 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1180 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1181 __glibcxx_requires_valid_range(__first
, __last
);
1183 while (__first
!= __last
)
1186 *__result
= *__last
;
1193 * This is a helper function for the rotate algorithm specialized on RAIs.
1194 * It returns the greatest common divisor of two integer values.
1196 template<typename _EuclideanRingElement
>
1197 _GLIBCXX20_CONSTEXPR
1198 _EuclideanRingElement
1199 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1203 _EuclideanRingElement __t
= __m
% __n
;
1210 inline namespace _V2
1213 /// This is a helper function for the rotate algorithm.
1214 template<typename _ForwardIterator
>
1215 _GLIBCXX20_CONSTEXPR
1217 __rotate(_ForwardIterator __first
,
1218 _ForwardIterator __middle
,
1219 _ForwardIterator __last
,
1220 forward_iterator_tag
)
1222 if (__first
== __middle
)
1224 else if (__last
== __middle
)
1227 _ForwardIterator __first2
= __middle
;
1230 std::iter_swap(__first
, __first2
);
1233 if (__first
== __middle
)
1234 __middle
= __first2
;
1236 while (__first2
!= __last
);
1238 _ForwardIterator __ret
= __first
;
1240 __first2
= __middle
;
1242 while (__first2
!= __last
)
1244 std::iter_swap(__first
, __first2
);
1247 if (__first
== __middle
)
1248 __middle
= __first2
;
1249 else if (__first2
== __last
)
1250 __first2
= __middle
;
1255 /// This is a helper function for the rotate algorithm.
1256 template<typename _BidirectionalIterator
>
1257 _GLIBCXX20_CONSTEXPR
1258 _BidirectionalIterator
1259 __rotate(_BidirectionalIterator __first
,
1260 _BidirectionalIterator __middle
,
1261 _BidirectionalIterator __last
,
1262 bidirectional_iterator_tag
)
1264 // concept requirements
1265 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1266 _BidirectionalIterator
>)
1268 if (__first
== __middle
)
1270 else if (__last
== __middle
)
1273 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1274 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1276 while (__first
!= __middle
&& __middle
!= __last
)
1278 std::iter_swap(__first
, --__last
);
1282 if (__first
== __middle
)
1284 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1289 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1294 /// This is a helper function for the rotate algorithm.
1295 template<typename _RandomAccessIterator
>
1296 _GLIBCXX20_CONSTEXPR
1297 _RandomAccessIterator
1298 __rotate(_RandomAccessIterator __first
,
1299 _RandomAccessIterator __middle
,
1300 _RandomAccessIterator __last
,
1301 random_access_iterator_tag
)
1303 // concept requirements
1304 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1305 _RandomAccessIterator
>)
1307 if (__first
== __middle
)
1309 else if (__last
== __middle
)
1312 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1314 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1317 _Distance __n
= __last
- __first
;
1318 _Distance __k
= __middle
- __first
;
1320 if (__k
== __n
- __k
)
1322 std::swap_ranges(__first
, __middle
, __middle
);
1326 _RandomAccessIterator __p
= __first
;
1327 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1331 if (__k
< __n
- __k
)
1333 if (__is_pod(_ValueType
) && __k
== 1)
1335 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1336 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1337 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1340 _RandomAccessIterator __q
= __p
+ __k
;
1341 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1343 std::iter_swap(__p
, __q
);
1350 std::swap(__n
, __k
);
1356 if (__is_pod(_ValueType
) && __k
== 1)
1358 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1359 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1360 *__p
= _GLIBCXX_MOVE(__t
);
1363 _RandomAccessIterator __q
= __p
+ __n
;
1365 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1369 std::iter_swap(__p
, __q
);
1374 std::swap(__n
, __k
);
1379 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1380 // DR 488. rotate throws away useful information
1382 * @brief Rotate the elements of a sequence.
1383 * @ingroup mutating_algorithms
1384 * @param __first A forward iterator.
1385 * @param __middle A forward iterator.
1386 * @param __last A forward iterator.
1387 * @return first + (last - middle).
1389 * Rotates the elements of the range @p [__first,__last) by
1390 * @p (__middle - __first) positions so that the element at @p __middle
1391 * is moved to @p __first, the element at @p __middle+1 is moved to
1392 * @p __first+1 and so on for each element in the range
1393 * @p [__first,__last).
1395 * This effectively swaps the ranges @p [__first,__middle) and
1396 * @p [__middle,__last).
1399 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1400 * for each @p n in the range @p [0,__last-__first).
1402 template<typename _ForwardIterator
>
1403 _GLIBCXX20_CONSTEXPR
1404 inline _ForwardIterator
1405 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1406 _ForwardIterator __last
)
1408 // concept requirements
1409 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1411 __glibcxx_requires_valid_range(__first
, __middle
);
1412 __glibcxx_requires_valid_range(__middle
, __last
);
1414 return std::__rotate(__first
, __middle
, __last
,
1415 std::__iterator_category(__first
));
1421 * @brief Copy a sequence, rotating its elements.
1422 * @ingroup mutating_algorithms
1423 * @param __first A forward iterator.
1424 * @param __middle A forward iterator.
1425 * @param __last A forward iterator.
1426 * @param __result An output iterator.
1427 * @return An iterator designating the end of the resulting sequence.
1429 * Copies the elements of the range @p [__first,__last) to the
1430 * range beginning at @result, rotating the copied elements by
1431 * @p (__middle-__first) positions so that the element at @p __middle
1432 * is moved to @p __result, the element at @p __middle+1 is moved
1433 * to @p __result+1 and so on for each element in the range @p
1437 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1438 * for each @p n in the range @p [0,__last-__first).
1440 template<typename _ForwardIterator
, typename _OutputIterator
>
1441 _GLIBCXX20_CONSTEXPR
1442 inline _OutputIterator
1443 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1444 _ForwardIterator __last
, _OutputIterator __result
)
1446 // concept requirements
1447 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1448 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1449 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1450 __glibcxx_requires_valid_range(__first
, __middle
);
1451 __glibcxx_requires_valid_range(__middle
, __last
);
1453 return std::copy(__first
, __middle
,
1454 std::copy(__middle
, __last
, __result
));
1457 /// This is a helper function...
1458 template<typename _ForwardIterator
, typename _Predicate
>
1459 _GLIBCXX20_CONSTEXPR
1461 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1462 _Predicate __pred
, forward_iterator_tag
)
1464 if (__first
== __last
)
1467 while (__pred(*__first
))
1468 if (++__first
== __last
)
1471 _ForwardIterator __next
= __first
;
1473 while (++__next
!= __last
)
1474 if (__pred(*__next
))
1476 std::iter_swap(__first
, __next
);
1483 /// This is a helper function...
1484 template<typename _BidirectionalIterator
, typename _Predicate
>
1485 _GLIBCXX20_CONSTEXPR
1486 _BidirectionalIterator
1487 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1488 _Predicate __pred
, bidirectional_iterator_tag
)
1493 if (__first
== __last
)
1495 else if (__pred(*__first
))
1501 if (__first
== __last
)
1503 else if (!bool(__pred(*__last
)))
1507 std::iter_swap(__first
, __last
);
1514 /// This is a helper function...
1515 /// Requires __first != __last and !__pred(__first)
1516 /// and __len == distance(__first, __last).
1518 /// !__pred(__first) allows us to guarantee that we don't
1519 /// move-assign an element onto itself.
1520 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1523 __stable_partition_adaptive(_ForwardIterator __first
,
1524 _ForwardIterator __last
,
1525 _Predicate __pred
, _Distance __len
,
1527 _Distance __buffer_size
)
1532 if (__len
<= __buffer_size
)
1534 _ForwardIterator __result1
= __first
;
1535 _Pointer __result2
= __buffer
;
1537 // The precondition guarantees that !__pred(__first), so
1538 // move that element to the buffer before starting the loop.
1539 // This ensures that we only call __pred once per element.
1540 *__result2
= _GLIBCXX_MOVE(*__first
);
1543 for (; __first
!= __last
; ++__first
)
1544 if (__pred(__first
))
1546 *__result1
= _GLIBCXX_MOVE(*__first
);
1551 *__result2
= _GLIBCXX_MOVE(*__first
);
1555 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1559 _ForwardIterator __middle
= __first
;
1560 std::advance(__middle
, __len
/ 2);
1561 _ForwardIterator __left_split
=
1562 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1563 __len
/ 2, __buffer
,
1566 // Advance past true-predicate values to satisfy this
1567 // function's preconditions.
1568 _Distance __right_len
= __len
- __len
/ 2;
1569 _ForwardIterator __right_split
=
1570 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1574 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1576 __buffer
, __buffer_size
);
1578 return std::rotate(__left_split
, __middle
, __right_split
);
1581 template<typename _ForwardIterator
, typename _Predicate
>
1583 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1586 __first
= std::__find_if_not(__first
, __last
, __pred
);
1588 if (__first
== __last
)
1591 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1593 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1596 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1597 __buf(__first
, std::distance(__first
, __last
));
1599 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1600 _DistanceType(__buf
.requested_size()),
1602 _DistanceType(__buf
.size()));
1606 * @brief Move elements for which a predicate is true to the beginning
1607 * of a sequence, preserving relative ordering.
1608 * @ingroup mutating_algorithms
1609 * @param __first A forward iterator.
1610 * @param __last A forward iterator.
1611 * @param __pred A predicate functor.
1612 * @return An iterator @p middle such that @p __pred(i) is true for each
1613 * iterator @p i in the range @p [first,middle) and false for each @p i
1614 * in the range @p [middle,last).
1616 * Performs the same function as @p partition() with the additional
1617 * guarantee that the relative ordering of elements in each group is
1618 * preserved, so any two elements @p x and @p y in the range
1619 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1620 * relative ordering after calling @p stable_partition().
1622 template<typename _ForwardIterator
, typename _Predicate
>
1623 inline _ForwardIterator
1624 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1627 // concept requirements
1628 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1630 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1631 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1632 __glibcxx_requires_valid_range(__first
, __last
);
1634 return std::__stable_partition(__first
, __last
,
1635 __gnu_cxx::__ops::__pred_iter(__pred
));
1638 /// This is a helper function for the sort routines.
1639 template<typename _RandomAccessIterator
, typename _Compare
>
1640 _GLIBCXX20_CONSTEXPR
1642 __heap_select(_RandomAccessIterator __first
,
1643 _RandomAccessIterator __middle
,
1644 _RandomAccessIterator __last
, _Compare __comp
)
1646 std::__make_heap(__first
, __middle
, __comp
);
1647 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1648 if (__comp(__i
, __first
))
1649 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1654 template<typename _InputIterator
, typename _RandomAccessIterator
,
1656 _GLIBCXX20_CONSTEXPR
1657 _RandomAccessIterator
1658 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1659 _RandomAccessIterator __result_first
,
1660 _RandomAccessIterator __result_last
,
1663 typedef typename iterator_traits
<_InputIterator
>::value_type
1665 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1666 typedef typename
_RItTraits::difference_type _DistanceType
;
1668 if (__result_first
== __result_last
)
1669 return __result_last
;
1670 _RandomAccessIterator __result_real_last
= __result_first
;
1671 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1673 *__result_real_last
= *__first
;
1674 ++__result_real_last
;
1678 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1679 while (__first
!= __last
)
1681 if (__comp(__first
, __result_first
))
1682 std::__adjust_heap(__result_first
, _DistanceType(0),
1683 _DistanceType(__result_real_last
1685 _InputValueType(*__first
), __comp
);
1688 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1689 return __result_real_last
;
1693 * @brief Copy the smallest elements of a sequence.
1694 * @ingroup sorting_algorithms
1695 * @param __first An iterator.
1696 * @param __last Another iterator.
1697 * @param __result_first A random-access iterator.
1698 * @param __result_last Another random-access iterator.
1699 * @return An iterator indicating the end of the resulting sequence.
1701 * Copies and sorts the smallest N values from the range @p [__first,__last)
1702 * to the range beginning at @p __result_first, where the number of
1703 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1704 * @p (__result_last-__result_first).
1705 * After the sort if @e i and @e j are iterators in the range
1706 * @p [__result_first,__result_first+N) such that i precedes j then
1708 * The value returned is @p __result_first+N.
1710 template<typename _InputIterator
, typename _RandomAccessIterator
>
1711 _GLIBCXX20_CONSTEXPR
1712 inline _RandomAccessIterator
1713 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1714 _RandomAccessIterator __result_first
,
1715 _RandomAccessIterator __result_last
)
1717 #ifdef _GLIBCXX_CONCEPT_CHECKS
1718 typedef typename iterator_traits
<_InputIterator
>::value_type
1720 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1724 // concept requirements
1725 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1726 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1728 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1730 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1731 __glibcxx_requires_valid_range(__first
, __last
);
1732 __glibcxx_requires_irreflexive(__first
, __last
);
1733 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1735 return std::__partial_sort_copy(__first
, __last
,
1736 __result_first
, __result_last
,
1737 __gnu_cxx::__ops::__iter_less_iter());
1741 * @brief Copy the smallest elements of a sequence using a predicate for
1743 * @ingroup sorting_algorithms
1744 * @param __first An input iterator.
1745 * @param __last Another input iterator.
1746 * @param __result_first A random-access iterator.
1747 * @param __result_last Another random-access iterator.
1748 * @param __comp A comparison functor.
1749 * @return An iterator indicating the end of the resulting sequence.
1751 * Copies and sorts the smallest N values from the range @p [__first,__last)
1752 * to the range beginning at @p result_first, where the number of
1753 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1754 * @p (__result_last-__result_first).
1755 * After the sort if @e i and @e j are iterators in the range
1756 * @p [__result_first,__result_first+N) such that i precedes j then
1757 * @p __comp(*j,*i) is false.
1758 * The value returned is @p __result_first+N.
1760 template<typename _InputIterator
, typename _RandomAccessIterator
,
1762 _GLIBCXX20_CONSTEXPR
1763 inline _RandomAccessIterator
1764 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1765 _RandomAccessIterator __result_first
,
1766 _RandomAccessIterator __result_last
,
1769 #ifdef _GLIBCXX_CONCEPT_CHECKS
1770 typedef typename iterator_traits
<_InputIterator
>::value_type
1772 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1776 // concept requirements
1777 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1778 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1779 _RandomAccessIterator
>)
1780 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1782 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1783 _InputValueType
, _OutputValueType
>)
1784 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1785 _OutputValueType
, _OutputValueType
>)
1786 __glibcxx_requires_valid_range(__first
, __last
);
1787 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1788 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1790 return std::__partial_sort_copy(__first
, __last
,
1791 __result_first
, __result_last
,
1792 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1795 /// This is a helper function for the sort routine.
1796 template<typename _RandomAccessIterator
, typename _Compare
>
1797 _GLIBCXX20_CONSTEXPR
1799 __unguarded_linear_insert(_RandomAccessIterator __last
,
1802 typename iterator_traits
<_RandomAccessIterator
>::value_type
1803 __val
= _GLIBCXX_MOVE(*__last
);
1804 _RandomAccessIterator __next
= __last
;
1806 while (__comp(__val
, __next
))
1808 *__last
= _GLIBCXX_MOVE(*__next
);
1812 *__last
= _GLIBCXX_MOVE(__val
);
1815 /// This is a helper function for the sort routine.
1816 template<typename _RandomAccessIterator
, typename _Compare
>
1817 _GLIBCXX20_CONSTEXPR
1819 __insertion_sort(_RandomAccessIterator __first
,
1820 _RandomAccessIterator __last
, _Compare __comp
)
1822 if (__first
== __last
) return;
1824 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1826 if (__comp(__i
, __first
))
1828 typename iterator_traits
<_RandomAccessIterator
>::value_type
1829 __val
= _GLIBCXX_MOVE(*__i
);
1830 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1831 *__first
= _GLIBCXX_MOVE(__val
);
1834 std::__unguarded_linear_insert(__i
,
1835 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1839 /// This is a helper function for the sort routine.
1840 template<typename _RandomAccessIterator
, typename _Compare
>
1841 _GLIBCXX20_CONSTEXPR
1843 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1844 _RandomAccessIterator __last
, _Compare __comp
)
1846 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1847 std::__unguarded_linear_insert(__i
,
1848 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1853 * This controls some aspect of the sort routines.
1855 enum { _S_threshold
= 16 };
1857 /// This is a helper function for the sort routine.
1858 template<typename _RandomAccessIterator
, typename _Compare
>
1859 _GLIBCXX20_CONSTEXPR
1861 __final_insertion_sort(_RandomAccessIterator __first
,
1862 _RandomAccessIterator __last
, _Compare __comp
)
1864 if (__last
- __first
> int(_S_threshold
))
1866 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1867 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1871 std::__insertion_sort(__first
, __last
, __comp
);
1874 /// This is a helper function...
1875 template<typename _RandomAccessIterator
, typename _Compare
>
1876 _GLIBCXX20_CONSTEXPR
1877 _RandomAccessIterator
1878 __unguarded_partition(_RandomAccessIterator __first
,
1879 _RandomAccessIterator __last
,
1880 _RandomAccessIterator __pivot
, _Compare __comp
)
1884 while (__comp(__first
, __pivot
))
1887 while (__comp(__pivot
, __last
))
1889 if (!(__first
< __last
))
1891 std::iter_swap(__first
, __last
);
1896 /// This is a helper function...
1897 template<typename _RandomAccessIterator
, typename _Compare
>
1898 _GLIBCXX20_CONSTEXPR
1899 inline _RandomAccessIterator
1900 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1901 _RandomAccessIterator __last
, _Compare __comp
)
1903 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1904 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1906 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1909 template<typename _RandomAccessIterator
, typename _Compare
>
1910 _GLIBCXX20_CONSTEXPR
1912 __partial_sort(_RandomAccessIterator __first
,
1913 _RandomAccessIterator __middle
,
1914 _RandomAccessIterator __last
,
1917 std::__heap_select(__first
, __middle
, __last
, __comp
);
1918 std::__sort_heap(__first
, __middle
, __comp
);
1921 /// This is a helper function for the sort routine.
1922 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1923 _GLIBCXX20_CONSTEXPR
1925 __introsort_loop(_RandomAccessIterator __first
,
1926 _RandomAccessIterator __last
,
1927 _Size __depth_limit
, _Compare __comp
)
1929 while (__last
- __first
> int(_S_threshold
))
1931 if (__depth_limit
== 0)
1933 std::__partial_sort(__first
, __last
, __last
, __comp
);
1937 _RandomAccessIterator __cut
=
1938 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1939 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1946 template<typename _RandomAccessIterator
, typename _Compare
>
1947 _GLIBCXX20_CONSTEXPR
1949 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1952 if (__first
!= __last
)
1954 std::__introsort_loop(__first
, __last
,
1955 std::__lg(__last
- __first
) * 2,
1957 std::__final_insertion_sort(__first
, __last
, __comp
);
1961 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1962 _GLIBCXX20_CONSTEXPR
1964 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1965 _RandomAccessIterator __last
, _Size __depth_limit
,
1968 while (__last
- __first
> 3)
1970 if (__depth_limit
== 0)
1972 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1973 // Place the nth largest element in its final position.
1974 std::iter_swap(__first
, __nth
);
1978 _RandomAccessIterator __cut
=
1979 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1985 std::__insertion_sort(__first
, __last
, __comp
);
1990 // lower_bound moved to stl_algobase.h
1993 * @brief Finds the first position in which @p __val could be inserted
1994 * without changing the ordering.
1995 * @ingroup binary_search_algorithms
1996 * @param __first An iterator.
1997 * @param __last Another iterator.
1998 * @param __val The search term.
1999 * @param __comp A functor to use for comparisons.
2000 * @return An iterator pointing to the first element <em>not less
2001 * than</em> @p __val, or end() if every element is less
2003 * @ingroup binary_search_algorithms
2005 * The comparison function should have the same effects on ordering as
2006 * the function used for the initial sort.
2008 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2009 _GLIBCXX20_CONSTEXPR
2010 inline _ForwardIterator
2011 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2012 const _Tp
& __val
, _Compare __comp
)
2014 // concept requirements
2015 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2016 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2017 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2018 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2021 return std::__lower_bound(__first
, __last
, __val
,
2022 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2025 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2026 _GLIBCXX20_CONSTEXPR
2028 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2029 const _Tp
& __val
, _Compare __comp
)
2031 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2034 _DistanceType __len
= std::distance(__first
, __last
);
2038 _DistanceType __half
= __len
>> 1;
2039 _ForwardIterator __middle
= __first
;
2040 std::advance(__middle
, __half
);
2041 if (__comp(__val
, __middle
))
2047 __len
= __len
- __half
- 1;
2054 * @brief Finds the last position in which @p __val could be inserted
2055 * without changing the ordering.
2056 * @ingroup binary_search_algorithms
2057 * @param __first An iterator.
2058 * @param __last Another iterator.
2059 * @param __val The search term.
2060 * @return An iterator pointing to the first element greater than @p __val,
2061 * or end() if no elements are greater than @p __val.
2062 * @ingroup binary_search_algorithms
2064 template<typename _ForwardIterator
, typename _Tp
>
2065 _GLIBCXX20_CONSTEXPR
2066 inline _ForwardIterator
2067 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2070 // concept requirements
2071 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2072 __glibcxx_function_requires(_LessThanOpConcept
<
2073 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2074 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2076 return std::__upper_bound(__first
, __last
, __val
,
2077 __gnu_cxx::__ops::__val_less_iter());
2081 * @brief Finds the last position in which @p __val could be inserted
2082 * without changing the ordering.
2083 * @ingroup binary_search_algorithms
2084 * @param __first An iterator.
2085 * @param __last Another iterator.
2086 * @param __val The search term.
2087 * @param __comp A functor to use for comparisons.
2088 * @return An iterator pointing to the first element greater than @p __val,
2089 * or end() if no elements are greater than @p __val.
2090 * @ingroup binary_search_algorithms
2092 * The comparison function should have the same effects on ordering as
2093 * the function used for the initial sort.
2095 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2096 _GLIBCXX20_CONSTEXPR
2097 inline _ForwardIterator
2098 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2099 const _Tp
& __val
, _Compare __comp
)
2101 // concept requirements
2102 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2103 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2104 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2105 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2108 return std::__upper_bound(__first
, __last
, __val
,
2109 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2112 template<typename _ForwardIterator
, typename _Tp
,
2113 typename _CompareItTp
, typename _CompareTpIt
>
2114 _GLIBCXX20_CONSTEXPR
2115 pair
<_ForwardIterator
, _ForwardIterator
>
2116 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2118 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2120 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2123 _DistanceType __len
= std::distance(__first
, __last
);
2127 _DistanceType __half
= __len
>> 1;
2128 _ForwardIterator __middle
= __first
;
2129 std::advance(__middle
, __half
);
2130 if (__comp_it_val(__middle
, __val
))
2134 __len
= __len
- __half
- 1;
2136 else if (__comp_val_it(__val
, __middle
))
2140 _ForwardIterator __left
2141 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2142 std::advance(__first
, __len
);
2143 _ForwardIterator __right
2144 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2145 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2148 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2152 * @brief Finds the largest subrange in which @p __val could be inserted
2153 * at any place in it without changing the ordering.
2154 * @ingroup binary_search_algorithms
2155 * @param __first An iterator.
2156 * @param __last Another iterator.
2157 * @param __val The search term.
2158 * @return An pair of iterators defining the subrange.
2159 * @ingroup binary_search_algorithms
2161 * This is equivalent to
2163 * std::make_pair(lower_bound(__first, __last, __val),
2164 * upper_bound(__first, __last, __val))
2166 * but does not actually call those functions.
2168 template<typename _ForwardIterator
, typename _Tp
>
2169 _GLIBCXX20_CONSTEXPR
2170 inline pair
<_ForwardIterator
, _ForwardIterator
>
2171 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2174 // concept requirements
2175 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2176 __glibcxx_function_requires(_LessThanOpConcept
<
2177 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2178 __glibcxx_function_requires(_LessThanOpConcept
<
2179 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2180 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2181 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2183 return std::__equal_range(__first
, __last
, __val
,
2184 __gnu_cxx::__ops::__iter_less_val(),
2185 __gnu_cxx::__ops::__val_less_iter());
2189 * @brief Finds the largest subrange in which @p __val could be inserted
2190 * at any place in it without changing the ordering.
2191 * @param __first An iterator.
2192 * @param __last Another iterator.
2193 * @param __val The search term.
2194 * @param __comp A functor to use for comparisons.
2195 * @return An pair of iterators defining the subrange.
2196 * @ingroup binary_search_algorithms
2198 * This is equivalent to
2200 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2201 * upper_bound(__first, __last, __val, __comp))
2203 * but does not actually call those functions.
2205 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2206 _GLIBCXX20_CONSTEXPR
2207 inline pair
<_ForwardIterator
, _ForwardIterator
>
2208 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2209 const _Tp
& __val
, _Compare __comp
)
2211 // concept requirements
2212 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2213 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2214 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2215 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2216 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2217 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2219 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2222 return std::__equal_range(__first
, __last
, __val
,
2223 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2224 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2228 * @brief Determines whether an element exists in a range.
2229 * @ingroup binary_search_algorithms
2230 * @param __first An iterator.
2231 * @param __last Another iterator.
2232 * @param __val The search term.
2233 * @return True if @p __val (or its equivalent) is in [@p
2234 * __first,@p __last ].
2236 * Note that this does not actually return an iterator to @p __val. For
2237 * that, use std::find or a container's specialized find member functions.
2239 template<typename _ForwardIterator
, typename _Tp
>
2240 _GLIBCXX20_CONSTEXPR
2242 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2245 // concept requirements
2246 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2247 __glibcxx_function_requires(_LessThanOpConcept
<
2248 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2249 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2250 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2252 _ForwardIterator __i
2253 = std::__lower_bound(__first
, __last
, __val
,
2254 __gnu_cxx::__ops::__iter_less_val());
2255 return __i
!= __last
&& !(__val
< *__i
);
2259 * @brief Determines whether an element exists in a range.
2260 * @ingroup binary_search_algorithms
2261 * @param __first An iterator.
2262 * @param __last Another iterator.
2263 * @param __val The search term.
2264 * @param __comp A functor to use for comparisons.
2265 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2267 * Note that this does not actually return an iterator to @p __val. For
2268 * that, use std::find or a container's specialized find member functions.
2270 * The comparison function should have the same effects on ordering as
2271 * the function used for the initial sort.
2273 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2274 _GLIBCXX20_CONSTEXPR
2276 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2277 const _Tp
& __val
, _Compare __comp
)
2279 // concept requirements
2280 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2281 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2282 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2283 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2285 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2288 _ForwardIterator __i
2289 = std::__lower_bound(__first
, __last
, __val
,
2290 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2291 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2296 /// This is a helper function for the __merge_adaptive routines.
2297 template<typename _InputIterator1
, typename _InputIterator2
,
2298 typename _OutputIterator
, typename _Compare
>
2300 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2301 _InputIterator2 __first2
, _InputIterator2 __last2
,
2302 _OutputIterator __result
, _Compare __comp
)
2304 while (__first1
!= __last1
&& __first2
!= __last2
)
2306 if (__comp(__first2
, __first1
))
2308 *__result
= _GLIBCXX_MOVE(*__first2
);
2313 *__result
= _GLIBCXX_MOVE(*__first1
);
2318 if (__first1
!= __last1
)
2319 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2322 /// This is a helper function for the __merge_adaptive routines.
2323 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2324 typename _BidirectionalIterator3
, typename _Compare
>
2326 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2327 _BidirectionalIterator1 __last1
,
2328 _BidirectionalIterator2 __first2
,
2329 _BidirectionalIterator2 __last2
,
2330 _BidirectionalIterator3 __result
,
2333 if (__first1
== __last1
)
2335 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2338 else if (__first2
== __last2
)
2345 if (__comp(__last2
, __last1
))
2347 *--__result
= _GLIBCXX_MOVE(*__last1
);
2348 if (__first1
== __last1
)
2350 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2357 *--__result
= _GLIBCXX_MOVE(*__last2
);
2358 if (__first2
== __last2
)
2365 /// This is a helper function for the merge routines.
2366 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2368 _BidirectionalIterator1
2369 __rotate_adaptive(_BidirectionalIterator1 __first
,
2370 _BidirectionalIterator1 __middle
,
2371 _BidirectionalIterator1 __last
,
2372 _Distance __len1
, _Distance __len2
,
2373 _BidirectionalIterator2 __buffer
,
2374 _Distance __buffer_size
)
2376 _BidirectionalIterator2 __buffer_end
;
2377 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2381 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2382 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2383 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2388 else if (__len1
<= __buffer_size
)
2392 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2393 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2394 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2400 return std::rotate(__first
, __middle
, __last
);
2403 /// This is a helper function for the merge routines.
2404 template<typename _BidirectionalIterator
, typename _Distance
,
2405 typename _Pointer
, typename _Compare
>
2407 __merge_adaptive(_BidirectionalIterator __first
,
2408 _BidirectionalIterator __middle
,
2409 _BidirectionalIterator __last
,
2410 _Distance __len1
, _Distance __len2
,
2411 _Pointer __buffer
, _Distance __buffer_size
,
2414 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2416 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2417 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2420 else if (__len2
<= __buffer_size
)
2422 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2423 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2424 __buffer_end
, __last
, __comp
);
2428 _BidirectionalIterator __first_cut
= __first
;
2429 _BidirectionalIterator __second_cut
= __middle
;
2430 _Distance __len11
= 0;
2431 _Distance __len22
= 0;
2432 if (__len1
> __len2
)
2434 __len11
= __len1
/ 2;
2435 std::advance(__first_cut
, __len11
);
2437 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2438 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2439 __len22
= std::distance(__middle
, __second_cut
);
2443 __len22
= __len2
/ 2;
2444 std::advance(__second_cut
, __len22
);
2446 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2447 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2448 __len11
= std::distance(__first
, __first_cut
);
2451 _BidirectionalIterator __new_middle
2452 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2453 __len1
- __len11
, __len22
, __buffer
,
2455 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2456 __len22
, __buffer
, __buffer_size
, __comp
);
2457 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2459 __len2
- __len22
, __buffer
,
2460 __buffer_size
, __comp
);
2464 /// This is a helper function for the merge routines.
2465 template<typename _BidirectionalIterator
, typename _Distance
,
2468 __merge_without_buffer(_BidirectionalIterator __first
,
2469 _BidirectionalIterator __middle
,
2470 _BidirectionalIterator __last
,
2471 _Distance __len1
, _Distance __len2
,
2474 if (__len1
== 0 || __len2
== 0)
2477 if (__len1
+ __len2
== 2)
2479 if (__comp(__middle
, __first
))
2480 std::iter_swap(__first
, __middle
);
2484 _BidirectionalIterator __first_cut
= __first
;
2485 _BidirectionalIterator __second_cut
= __middle
;
2486 _Distance __len11
= 0;
2487 _Distance __len22
= 0;
2488 if (__len1
> __len2
)
2490 __len11
= __len1
/ 2;
2491 std::advance(__first_cut
, __len11
);
2493 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2494 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2495 __len22
= std::distance(__middle
, __second_cut
);
2499 __len22
= __len2
/ 2;
2500 std::advance(__second_cut
, __len22
);
2502 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2503 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2504 __len11
= std::distance(__first
, __first_cut
);
2507 _BidirectionalIterator __new_middle
2508 = std::rotate(__first_cut
, __middle
, __second_cut
);
2509 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2510 __len11
, __len22
, __comp
);
2511 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2512 __len1
- __len11
, __len2
- __len22
, __comp
);
2515 template<typename _BidirectionalIterator
, typename _Compare
>
2517 __inplace_merge(_BidirectionalIterator __first
,
2518 _BidirectionalIterator __middle
,
2519 _BidirectionalIterator __last
,
2522 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2524 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2527 if (__first
== __middle
|| __middle
== __last
)
2530 const _DistanceType __len1
= std::distance(__first
, __middle
);
2531 const _DistanceType __len2
= std::distance(__middle
, __last
);
2533 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2534 _TmpBuf
__buf(__first
, __len1
+ __len2
);
2536 if (__buf
.begin() == 0)
2537 std::__merge_without_buffer
2538 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2540 std::__merge_adaptive
2541 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2542 _DistanceType(__buf
.size()), __comp
);
2546 * @brief Merges two sorted ranges in place.
2547 * @ingroup sorting_algorithms
2548 * @param __first An iterator.
2549 * @param __middle Another iterator.
2550 * @param __last Another iterator.
2553 * Merges two sorted and consecutive ranges, [__first,__middle) and
2554 * [__middle,__last), and puts the result in [__first,__last). The
2555 * output will be sorted. The sort is @e stable, that is, for
2556 * equivalent elements in the two ranges, elements from the first
2557 * range will always come before elements from the second.
2559 * If enough additional memory is available, this takes (__last-__first)-1
2560 * comparisons. Otherwise an NlogN algorithm is used, where N is
2561 * distance(__first,__last).
2563 template<typename _BidirectionalIterator
>
2565 inplace_merge(_BidirectionalIterator __first
,
2566 _BidirectionalIterator __middle
,
2567 _BidirectionalIterator __last
)
2569 // concept requirements
2570 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2571 _BidirectionalIterator
>)
2572 __glibcxx_function_requires(_LessThanComparableConcept
<
2573 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2574 __glibcxx_requires_sorted(__first
, __middle
);
2575 __glibcxx_requires_sorted(__middle
, __last
);
2576 __glibcxx_requires_irreflexive(__first
, __last
);
2578 std::__inplace_merge(__first
, __middle
, __last
,
2579 __gnu_cxx::__ops::__iter_less_iter());
2583 * @brief Merges two sorted ranges in place.
2584 * @ingroup sorting_algorithms
2585 * @param __first An iterator.
2586 * @param __middle Another iterator.
2587 * @param __last Another iterator.
2588 * @param __comp A functor to use for comparisons.
2591 * Merges two sorted and consecutive ranges, [__first,__middle) and
2592 * [middle,last), and puts the result in [__first,__last). The output will
2593 * be sorted. The sort is @e stable, that is, for equivalent
2594 * elements in the two ranges, elements from the first range will always
2595 * come before elements from the second.
2597 * If enough additional memory is available, this takes (__last-__first)-1
2598 * comparisons. Otherwise an NlogN algorithm is used, where N is
2599 * distance(__first,__last).
2601 * The comparison function should have the same effects on ordering as
2602 * the function used for the initial sort.
2604 template<typename _BidirectionalIterator
, typename _Compare
>
2606 inplace_merge(_BidirectionalIterator __first
,
2607 _BidirectionalIterator __middle
,
2608 _BidirectionalIterator __last
,
2611 // concept requirements
2612 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2613 _BidirectionalIterator
>)
2614 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2615 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2616 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2617 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2618 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2619 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2621 std::__inplace_merge(__first
, __middle
, __last
,
2622 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2626 /// This is a helper function for the __merge_sort_loop routines.
2627 template<typename _InputIterator
, typename _OutputIterator
,
2630 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2631 _InputIterator __first2
, _InputIterator __last2
,
2632 _OutputIterator __result
, _Compare __comp
)
2634 while (__first1
!= __last1
&& __first2
!= __last2
)
2636 if (__comp(__first2
, __first1
))
2638 *__result
= _GLIBCXX_MOVE(*__first2
);
2643 *__result
= _GLIBCXX_MOVE(*__first1
);
2648 return _GLIBCXX_MOVE3(__first2
, __last2
,
2649 _GLIBCXX_MOVE3(__first1
, __last1
,
2653 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2654 typename _Distance
, typename _Compare
>
2656 __merge_sort_loop(_RandomAccessIterator1 __first
,
2657 _RandomAccessIterator1 __last
,
2658 _RandomAccessIterator2 __result
, _Distance __step_size
,
2661 const _Distance __two_step
= 2 * __step_size
;
2663 while (__last
- __first
>= __two_step
)
2665 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2666 __first
+ __step_size
,
2667 __first
+ __two_step
,
2669 __first
+= __two_step
;
2671 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2673 std::__move_merge(__first
, __first
+ __step_size
,
2674 __first
+ __step_size
, __last
, __result
, __comp
);
2677 template<typename _RandomAccessIterator
, typename _Distance
,
2679 _GLIBCXX20_CONSTEXPR
2681 __chunk_insertion_sort(_RandomAccessIterator __first
,
2682 _RandomAccessIterator __last
,
2683 _Distance __chunk_size
, _Compare __comp
)
2685 while (__last
- __first
>= __chunk_size
)
2687 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2688 __first
+= __chunk_size
;
2690 std::__insertion_sort(__first
, __last
, __comp
);
2693 enum { _S_chunk_size
= 7 };
2695 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2697 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2698 _RandomAccessIterator __last
,
2699 _Pointer __buffer
, _Compare __comp
)
2701 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2704 const _Distance __len
= __last
- __first
;
2705 const _Pointer __buffer_last
= __buffer
+ __len
;
2707 _Distance __step_size
= _S_chunk_size
;
2708 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2710 while (__step_size
< __len
)
2712 std::__merge_sort_loop(__first
, __last
, __buffer
,
2713 __step_size
, __comp
);
2715 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2716 __step_size
, __comp
);
2721 template<typename _RandomAccessIterator
, typename _Pointer
,
2722 typename _Distance
, typename _Compare
>
2724 __stable_sort_adaptive(_RandomAccessIterator __first
,
2725 _RandomAccessIterator __last
,
2726 _Pointer __buffer
, _Distance __buffer_size
,
2729 const _Distance __len
= (__last
- __first
+ 1) / 2;
2730 const _RandomAccessIterator __middle
= __first
+ __len
;
2731 if (__len
> __buffer_size
)
2733 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2734 __buffer_size
, __comp
);
2735 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2736 __buffer_size
, __comp
);
2740 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2741 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2743 std::__merge_adaptive(__first
, __middle
, __last
,
2744 _Distance(__middle
- __first
),
2745 _Distance(__last
- __middle
),
2746 __buffer
, __buffer_size
,
2750 /// This is a helper function for the stable sorting routines.
2751 template<typename _RandomAccessIterator
, typename _Compare
>
2753 __inplace_stable_sort(_RandomAccessIterator __first
,
2754 _RandomAccessIterator __last
, _Compare __comp
)
2756 if (__last
- __first
< 15)
2758 std::__insertion_sort(__first
, __last
, __comp
);
2761 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2762 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2763 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2764 std::__merge_without_buffer(__first
, __middle
, __last
,
2772 // Set algorithms: includes, set_union, set_intersection, set_difference,
2773 // set_symmetric_difference. All of these algorithms have the precondition
2774 // that their input ranges are sorted and the postcondition that their output
2775 // ranges are sorted.
2777 template<typename _InputIterator1
, typename _InputIterator2
,
2779 _GLIBCXX20_CONSTEXPR
2781 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2782 _InputIterator2 __first2
, _InputIterator2 __last2
,
2785 while (__first1
!= __last1
&& __first2
!= __last2
)
2787 if (__comp(__first2
, __first1
))
2789 if (!__comp(__first1
, __first2
))
2794 return __first2
== __last2
;
2798 * @brief Determines whether all elements of a sequence exists in a range.
2799 * @param __first1 Start of search range.
2800 * @param __last1 End of search range.
2801 * @param __first2 Start of sequence
2802 * @param __last2 End of sequence.
2803 * @return True if each element in [__first2,__last2) is contained in order
2804 * within [__first1,__last1). False otherwise.
2805 * @ingroup set_algorithms
2807 * This operation expects both [__first1,__last1) and
2808 * [__first2,__last2) to be sorted. Searches for the presence of
2809 * each element in [__first2,__last2) within [__first1,__last1).
2810 * The iterators over each range only move forward, so this is a
2811 * linear algorithm. If an element in [__first2,__last2) is not
2812 * found before the search iterator reaches @p __last2, false is
2815 template<typename _InputIterator1
, typename _InputIterator2
>
2816 _GLIBCXX20_CONSTEXPR
2818 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2819 _InputIterator2 __first2
, _InputIterator2 __last2
)
2821 // concept requirements
2822 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2823 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2824 __glibcxx_function_requires(_LessThanOpConcept
<
2825 typename iterator_traits
<_InputIterator1
>::value_type
,
2826 typename iterator_traits
<_InputIterator2
>::value_type
>)
2827 __glibcxx_function_requires(_LessThanOpConcept
<
2828 typename iterator_traits
<_InputIterator2
>::value_type
,
2829 typename iterator_traits
<_InputIterator1
>::value_type
>)
2830 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2831 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2832 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2833 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2835 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2836 __gnu_cxx::__ops::__iter_less_iter());
2840 * @brief Determines whether all elements of a sequence exists in a range
2842 * @ingroup set_algorithms
2843 * @param __first1 Start of search range.
2844 * @param __last1 End of search range.
2845 * @param __first2 Start of sequence
2846 * @param __last2 End of sequence.
2847 * @param __comp Comparison function to use.
2848 * @return True if each element in [__first2,__last2) is contained
2849 * in order within [__first1,__last1) according to comp. False
2850 * otherwise. @ingroup set_algorithms
2852 * This operation expects both [__first1,__last1) and
2853 * [__first2,__last2) to be sorted. Searches for the presence of
2854 * each element in [__first2,__last2) within [__first1,__last1),
2855 * using comp to decide. The iterators over each range only move
2856 * forward, so this is a linear algorithm. If an element in
2857 * [__first2,__last2) is not found before the search iterator
2858 * reaches @p __last2, false is returned.
2860 template<typename _InputIterator1
, typename _InputIterator2
,
2862 _GLIBCXX20_CONSTEXPR
2864 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2865 _InputIterator2 __first2
, _InputIterator2 __last2
,
2868 // concept requirements
2869 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2870 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2871 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2872 typename iterator_traits
<_InputIterator1
>::value_type
,
2873 typename iterator_traits
<_InputIterator2
>::value_type
>)
2874 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2875 typename iterator_traits
<_InputIterator2
>::value_type
,
2876 typename iterator_traits
<_InputIterator1
>::value_type
>)
2877 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2878 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2879 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2880 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2882 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2883 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2892 // set_symmetric_difference
2896 template<typename _BidirectionalIterator
, typename _Compare
>
2897 _GLIBCXX20_CONSTEXPR
2899 __next_permutation(_BidirectionalIterator __first
,
2900 _BidirectionalIterator __last
, _Compare __comp
)
2902 if (__first
== __last
)
2904 _BidirectionalIterator __i
= __first
;
2913 _BidirectionalIterator __ii
= __i
;
2915 if (__comp(__i
, __ii
))
2917 _BidirectionalIterator __j
= __last
;
2918 while (!__comp(__i
, --__j
))
2920 std::iter_swap(__i
, __j
);
2921 std::__reverse(__ii
, __last
,
2922 std::__iterator_category(__first
));
2927 std::__reverse(__first
, __last
,
2928 std::__iterator_category(__first
));
2935 * @brief Permute range into the next @e dictionary ordering.
2936 * @ingroup sorting_algorithms
2937 * @param __first Start of range.
2938 * @param __last End of range.
2939 * @return False if wrapped to first permutation, true otherwise.
2941 * Treats all permutations of the range as a set of @e dictionary sorted
2942 * sequences. Permutes the current sequence into the next one of this set.
2943 * Returns true if there are more sequences to generate. If the sequence
2944 * is the largest of the set, the smallest is generated and false returned.
2946 template<typename _BidirectionalIterator
>
2947 _GLIBCXX20_CONSTEXPR
2949 next_permutation(_BidirectionalIterator __first
,
2950 _BidirectionalIterator __last
)
2952 // concept requirements
2953 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2954 _BidirectionalIterator
>)
2955 __glibcxx_function_requires(_LessThanComparableConcept
<
2956 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2957 __glibcxx_requires_valid_range(__first
, __last
);
2958 __glibcxx_requires_irreflexive(__first
, __last
);
2960 return std::__next_permutation
2961 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2965 * @brief Permute range into the next @e dictionary ordering using
2966 * comparison functor.
2967 * @ingroup sorting_algorithms
2968 * @param __first Start of range.
2969 * @param __last End of range.
2970 * @param __comp A comparison functor.
2971 * @return False if wrapped to first permutation, true otherwise.
2973 * Treats all permutations of the range [__first,__last) as a set of
2974 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2975 * sequence into the next one of this set. Returns true if there are more
2976 * sequences to generate. If the sequence is the largest of the set, the
2977 * smallest is generated and false returned.
2979 template<typename _BidirectionalIterator
, typename _Compare
>
2980 _GLIBCXX20_CONSTEXPR
2982 next_permutation(_BidirectionalIterator __first
,
2983 _BidirectionalIterator __last
, _Compare __comp
)
2985 // concept requirements
2986 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2987 _BidirectionalIterator
>)
2988 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2989 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2990 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2991 __glibcxx_requires_valid_range(__first
, __last
);
2992 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2994 return std::__next_permutation
2995 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2998 template<typename _BidirectionalIterator
, typename _Compare
>
2999 _GLIBCXX20_CONSTEXPR
3001 __prev_permutation(_BidirectionalIterator __first
,
3002 _BidirectionalIterator __last
, _Compare __comp
)
3004 if (__first
== __last
)
3006 _BidirectionalIterator __i
= __first
;
3015 _BidirectionalIterator __ii
= __i
;
3017 if (__comp(__ii
, __i
))
3019 _BidirectionalIterator __j
= __last
;
3020 while (!__comp(--__j
, __i
))
3022 std::iter_swap(__i
, __j
);
3023 std::__reverse(__ii
, __last
,
3024 std::__iterator_category(__first
));
3029 std::__reverse(__first
, __last
,
3030 std::__iterator_category(__first
));
3037 * @brief Permute range into the previous @e dictionary ordering.
3038 * @ingroup sorting_algorithms
3039 * @param __first Start of range.
3040 * @param __last End of range.
3041 * @return False if wrapped to last permutation, true otherwise.
3043 * Treats all permutations of the range as a set of @e dictionary sorted
3044 * sequences. Permutes the current sequence into the previous one of this
3045 * set. Returns true if there are more sequences to generate. If the
3046 * sequence is the smallest of the set, the largest is generated and false
3049 template<typename _BidirectionalIterator
>
3050 _GLIBCXX20_CONSTEXPR
3052 prev_permutation(_BidirectionalIterator __first
,
3053 _BidirectionalIterator __last
)
3055 // concept requirements
3056 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3057 _BidirectionalIterator
>)
3058 __glibcxx_function_requires(_LessThanComparableConcept
<
3059 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3060 __glibcxx_requires_valid_range(__first
, __last
);
3061 __glibcxx_requires_irreflexive(__first
, __last
);
3063 return std::__prev_permutation(__first
, __last
,
3064 __gnu_cxx::__ops::__iter_less_iter());
3068 * @brief Permute range into the previous @e dictionary ordering using
3069 * comparison functor.
3070 * @ingroup sorting_algorithms
3071 * @param __first Start of range.
3072 * @param __last End of range.
3073 * @param __comp A comparison functor.
3074 * @return False if wrapped to last permutation, true otherwise.
3076 * Treats all permutations of the range [__first,__last) as a set of
3077 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3078 * sequence into the previous one of this set. Returns true if there are
3079 * more sequences to generate. If the sequence is the smallest of the set,
3080 * the largest is generated and false returned.
3082 template<typename _BidirectionalIterator
, typename _Compare
>
3083 _GLIBCXX20_CONSTEXPR
3085 prev_permutation(_BidirectionalIterator __first
,
3086 _BidirectionalIterator __last
, _Compare __comp
)
3088 // concept requirements
3089 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3090 _BidirectionalIterator
>)
3091 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3092 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3093 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3094 __glibcxx_requires_valid_range(__first
, __last
);
3095 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3097 return std::__prev_permutation(__first
, __last
,
3098 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3104 template<typename _InputIterator
, typename _OutputIterator
,
3105 typename _Predicate
, typename _Tp
>
3106 _GLIBCXX20_CONSTEXPR
3108 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3109 _OutputIterator __result
,
3110 _Predicate __pred
, const _Tp
& __new_value
)
3112 for (; __first
!= __last
; ++__first
, (void)++__result
)
3113 if (__pred(__first
))
3114 *__result
= __new_value
;
3116 *__result
= *__first
;
3121 * @brief Copy a sequence, replacing each element of one value with another
3123 * @param __first An input iterator.
3124 * @param __last An input iterator.
3125 * @param __result An output iterator.
3126 * @param __old_value The value to be replaced.
3127 * @param __new_value The replacement value.
3128 * @return The end of the output sequence, @p result+(last-first).
3130 * Copies each element in the input range @p [__first,__last) to the
3131 * output range @p [__result,__result+(__last-__first)) replacing elements
3132 * equal to @p __old_value with @p __new_value.
3134 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3135 _GLIBCXX20_CONSTEXPR
3136 inline _OutputIterator
3137 replace_copy(_InputIterator __first
, _InputIterator __last
,
3138 _OutputIterator __result
,
3139 const _Tp
& __old_value
, const _Tp
& __new_value
)
3141 // concept requirements
3142 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3143 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3144 typename iterator_traits
<_InputIterator
>::value_type
>)
3145 __glibcxx_function_requires(_EqualOpConcept
<
3146 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3147 __glibcxx_requires_valid_range(__first
, __last
);
3149 return std::__replace_copy_if(__first
, __last
, __result
,
3150 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3155 * @brief Copy a sequence, replacing each value for which a predicate
3156 * returns true with another value.
3157 * @ingroup mutating_algorithms
3158 * @param __first An input iterator.
3159 * @param __last An input iterator.
3160 * @param __result An output iterator.
3161 * @param __pred A predicate.
3162 * @param __new_value The replacement value.
3163 * @return The end of the output sequence, @p __result+(__last-__first).
3165 * Copies each element in the range @p [__first,__last) to the range
3166 * @p [__result,__result+(__last-__first)) replacing elements for which
3167 * @p __pred returns true with @p __new_value.
3169 template<typename _InputIterator
, typename _OutputIterator
,
3170 typename _Predicate
, typename _Tp
>
3171 _GLIBCXX20_CONSTEXPR
3172 inline _OutputIterator
3173 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3174 _OutputIterator __result
,
3175 _Predicate __pred
, const _Tp
& __new_value
)
3177 // concept requirements
3178 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3179 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3180 typename iterator_traits
<_InputIterator
>::value_type
>)
3181 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3182 typename iterator_traits
<_InputIterator
>::value_type
>)
3183 __glibcxx_requires_valid_range(__first
, __last
);
3185 return std::__replace_copy_if(__first
, __last
, __result
,
3186 __gnu_cxx::__ops::__pred_iter(__pred
),
3190 #if __cplusplus >= 201103L
3192 * @brief Determines whether the elements of a sequence are sorted.
3193 * @ingroup sorting_algorithms
3194 * @param __first An iterator.
3195 * @param __last Another iterator.
3196 * @return True if the elements are sorted, false otherwise.
3198 template<typename _ForwardIterator
>
3199 _GLIBCXX20_CONSTEXPR
3201 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3202 { return std::is_sorted_until(__first
, __last
) == __last
; }
3205 * @brief Determines whether the elements of a sequence are sorted
3206 * according to a comparison functor.
3207 * @ingroup sorting_algorithms
3208 * @param __first An iterator.
3209 * @param __last Another iterator.
3210 * @param __comp A comparison functor.
3211 * @return True if the elements are sorted, false otherwise.
3213 template<typename _ForwardIterator
, typename _Compare
>
3214 _GLIBCXX20_CONSTEXPR
3216 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3218 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3220 template<typename _ForwardIterator
, typename _Compare
>
3221 _GLIBCXX20_CONSTEXPR
3223 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3226 if (__first
== __last
)
3229 _ForwardIterator __next
= __first
;
3230 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3231 if (__comp(__next
, __first
))
3237 * @brief Determines the end of a sorted sequence.
3238 * @ingroup sorting_algorithms
3239 * @param __first An iterator.
3240 * @param __last Another iterator.
3241 * @return An iterator pointing to the last iterator i in [__first, __last)
3242 * for which the range [__first, i) is sorted.
3244 template<typename _ForwardIterator
>
3245 _GLIBCXX20_CONSTEXPR
3246 inline _ForwardIterator
3247 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3249 // concept requirements
3250 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3251 __glibcxx_function_requires(_LessThanComparableConcept
<
3252 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3253 __glibcxx_requires_valid_range(__first
, __last
);
3254 __glibcxx_requires_irreflexive(__first
, __last
);
3256 return std::__is_sorted_until(__first
, __last
,
3257 __gnu_cxx::__ops::__iter_less_iter());
3261 * @brief Determines the end of a sorted sequence using comparison functor.
3262 * @ingroup sorting_algorithms
3263 * @param __first An iterator.
3264 * @param __last Another iterator.
3265 * @param __comp A comparison functor.
3266 * @return An iterator pointing to the last iterator i in [__first, __last)
3267 * for which the range [__first, i) is sorted.
3269 template<typename _ForwardIterator
, typename _Compare
>
3270 _GLIBCXX20_CONSTEXPR
3271 inline _ForwardIterator
3272 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3275 // concept requirements
3276 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3277 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3278 typename iterator_traits
<_ForwardIterator
>::value_type
,
3279 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3280 __glibcxx_requires_valid_range(__first
, __last
);
3281 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3283 return std::__is_sorted_until(__first
, __last
,
3284 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3288 * @brief Determines min and max at once as an ordered pair.
3289 * @ingroup sorting_algorithms
3290 * @param __a A thing of arbitrary type.
3291 * @param __b Another thing of arbitrary type.
3292 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3295 template<typename _Tp
>
3296 _GLIBCXX14_CONSTEXPR
3297 inline pair
<const _Tp
&, const _Tp
&>
3298 minmax(const _Tp
& __a
, const _Tp
& __b
)
3300 // concept requirements
3301 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3303 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3304 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3308 * @brief Determines min and max at once as an ordered pair.
3309 * @ingroup sorting_algorithms
3310 * @param __a A thing of arbitrary type.
3311 * @param __b Another thing of arbitrary type.
3312 * @param __comp A @link comparison_functors comparison functor @endlink.
3313 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3316 template<typename _Tp
, typename _Compare
>
3317 _GLIBCXX14_CONSTEXPR
3318 inline pair
<const _Tp
&, const _Tp
&>
3319 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3321 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3322 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3325 template<typename _ForwardIterator
, typename _Compare
>
3326 _GLIBCXX14_CONSTEXPR
3327 pair
<_ForwardIterator
, _ForwardIterator
>
3328 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3331 _ForwardIterator __next
= __first
;
3332 if (__first
== __last
3333 || ++__next
== __last
)
3334 return std::make_pair(__first
, __first
);
3336 _ForwardIterator __min
{}, __max
{};
3337 if (__comp(__next
, __first
))
3351 while (__first
!= __last
)
3354 if (++__next
== __last
)
3356 if (__comp(__first
, __min
))
3358 else if (!__comp(__first
, __max
))
3363 if (__comp(__next
, __first
))
3365 if (__comp(__next
, __min
))
3367 if (!__comp(__first
, __max
))
3372 if (__comp(__first
, __min
))
3374 if (!__comp(__next
, __max
))
3382 return std::make_pair(__min
, __max
);
3386 * @brief Return a pair of iterators pointing to the minimum and maximum
3387 * elements in a range.
3388 * @ingroup sorting_algorithms
3389 * @param __first Start of range.
3390 * @param __last End of range.
3391 * @return make_pair(m, M), where m is the first iterator i in
3392 * [__first, __last) such that no other element in the range is
3393 * smaller, and where M is the last iterator i in [__first, __last)
3394 * such that no other element in the range is larger.
3396 template<typename _ForwardIterator
>
3397 _GLIBCXX14_CONSTEXPR
3398 inline pair
<_ForwardIterator
, _ForwardIterator
>
3399 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3401 // concept requirements
3402 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3403 __glibcxx_function_requires(_LessThanComparableConcept
<
3404 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3405 __glibcxx_requires_valid_range(__first
, __last
);
3406 __glibcxx_requires_irreflexive(__first
, __last
);
3408 return std::__minmax_element(__first
, __last
,
3409 __gnu_cxx::__ops::__iter_less_iter());
3413 * @brief Return a pair of iterators pointing to the minimum and maximum
3414 * elements in a range.
3415 * @ingroup sorting_algorithms
3416 * @param __first Start of range.
3417 * @param __last End of range.
3418 * @param __comp Comparison functor.
3419 * @return make_pair(m, M), where m is the first iterator i in
3420 * [__first, __last) such that no other element in the range is
3421 * smaller, and where M is the last iterator i in [__first, __last)
3422 * such that no other element in the range is larger.
3424 template<typename _ForwardIterator
, typename _Compare
>
3425 _GLIBCXX14_CONSTEXPR
3426 inline pair
<_ForwardIterator
, _ForwardIterator
>
3427 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3430 // concept requirements
3431 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3432 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3433 typename iterator_traits
<_ForwardIterator
>::value_type
,
3434 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3435 __glibcxx_requires_valid_range(__first
, __last
);
3436 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3438 return std::__minmax_element(__first
, __last
,
3439 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3443 template<typename _Tp
>
3444 _GLIBCXX14_CONSTEXPR
3446 min(initializer_list
<_Tp
> __l
)
3447 { return *std::min_element(__l
.begin(), __l
.end()); }
3449 template<typename _Tp
, typename _Compare
>
3450 _GLIBCXX14_CONSTEXPR
3452 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3453 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3455 template<typename _Tp
>
3456 _GLIBCXX14_CONSTEXPR
3458 max(initializer_list
<_Tp
> __l
)
3459 { return *std::max_element(__l
.begin(), __l
.end()); }
3461 template<typename _Tp
, typename _Compare
>
3462 _GLIBCXX14_CONSTEXPR
3464 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3465 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3467 template<typename _Tp
>
3468 _GLIBCXX14_CONSTEXPR
3469 inline pair
<_Tp
, _Tp
>
3470 minmax(initializer_list
<_Tp
> __l
)
3472 pair
<const _Tp
*, const _Tp
*> __p
=
3473 std::minmax_element(__l
.begin(), __l
.end());
3474 return std::make_pair(*__p
.first
, *__p
.second
);
3477 template<typename _Tp
, typename _Compare
>
3478 _GLIBCXX14_CONSTEXPR
3479 inline pair
<_Tp
, _Tp
>
3480 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3482 pair
<const _Tp
*, const _Tp
*> __p
=
3483 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3484 return std::make_pair(*__p
.first
, *__p
.second
);
3488 * @brief Checks whether a permutation of the second sequence is equal
3489 * to the first sequence.
3490 * @ingroup non_mutating_algorithms
3491 * @param __first1 Start of first range.
3492 * @param __last1 End of first range.
3493 * @param __first2 Start of second range.
3494 * @param __pred A binary predicate.
3495 * @return true if there exists a permutation of the elements in
3496 * the range [__first2, __first2 + (__last1 - __first1)),
3497 * beginning with ForwardIterator2 begin, such that
3498 * equal(__first1, __last1, __begin, __pred) returns true;
3499 * otherwise, returns false.
3501 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3502 typename _BinaryPredicate
>
3503 _GLIBCXX20_CONSTEXPR
3505 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3506 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3508 // concept requirements
3509 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3510 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3511 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3512 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3513 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3514 __glibcxx_requires_valid_range(__first1
, __last1
);
3516 return std::__is_permutation(__first1
, __last1
, __first2
,
3517 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3520 #if __cplusplus > 201103L
3521 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3522 typename _BinaryPredicate
>
3523 _GLIBCXX20_CONSTEXPR
3525 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3526 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3527 _BinaryPredicate __pred
)
3530 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3532 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3533 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3534 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3535 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3538 auto __d1
= std::distance(__first1
, __last1
);
3539 auto __d2
= std::distance(__first2
, __last2
);
3544 // Efficiently compare identical prefixes: O(N) if sequences
3545 // have the same elements in the same order.
3546 for (; __first1
!= __last1
&& __first2
!= __last2
;
3547 ++__first1
, (void)++__first2
)
3548 if (!__pred(__first1
, __first2
))
3553 if (__first1
== __last1
)
3558 auto __d1
= std::distance(__first1
, __last1
);
3559 auto __d2
= std::distance(__first2
, __last2
);
3560 if (__d1
== 0 && __d2
== 0)
3566 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3568 if (__scan
!= std::__find_if(__first1
, __scan
,
3569 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3570 continue; // We've seen this one before.
3572 auto __matches
= std::__count_if(__first2
, __last2
,
3573 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3575 || std::__count_if(__scan
, __last1
,
3576 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3584 * @brief Checks whether a permutaion of the second sequence is equal
3585 * to the first sequence.
3586 * @ingroup non_mutating_algorithms
3587 * @param __first1 Start of first range.
3588 * @param __last1 End of first range.
3589 * @param __first2 Start of second range.
3590 * @param __last2 End of first range.
3591 * @return true if there exists a permutation of the elements in the range
3592 * [__first2, __last2), beginning with ForwardIterator2 begin,
3593 * such that equal(__first1, __last1, begin) returns true;
3594 * otherwise, returns false.
3596 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3597 _GLIBCXX20_CONSTEXPR
3599 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3600 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3602 __glibcxx_requires_valid_range(__first1
, __last1
);
3603 __glibcxx_requires_valid_range(__first2
, __last2
);
3606 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3607 __gnu_cxx::__ops::__iter_equal_to_iter());
3611 * @brief Checks whether a permutation of the second sequence is equal
3612 * to the first sequence.
3613 * @ingroup non_mutating_algorithms
3614 * @param __first1 Start of first range.
3615 * @param __last1 End of first range.
3616 * @param __first2 Start of second range.
3617 * @param __last2 End of first range.
3618 * @param __pred A binary predicate.
3619 * @return true if there exists a permutation of the elements in the range
3620 * [__first2, __last2), beginning with ForwardIterator2 begin,
3621 * such that equal(__first1, __last1, __begin, __pred) returns true;
3622 * otherwise, returns false.
3624 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3625 typename _BinaryPredicate
>
3626 _GLIBCXX20_CONSTEXPR
3628 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3629 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3630 _BinaryPredicate __pred
)
3632 __glibcxx_requires_valid_range(__first1
, __last1
);
3633 __glibcxx_requires_valid_range(__first2
, __last2
);
3635 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3636 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3639 #if __cplusplus > 201402L
3641 #define __cpp_lib_clamp 201603
3644 * @brief Returns the value clamped between lo and hi.
3645 * @ingroup sorting_algorithms
3646 * @param __val A value of arbitrary type.
3647 * @param __lo A lower limit of arbitrary type.
3648 * @param __hi An upper limit of arbitrary type.
3649 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
3651 template<typename _Tp
>
3652 constexpr const _Tp
&
3653 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3655 __glibcxx_assert(!(__hi
< __lo
));
3656 return (__val
< __lo
) ? __lo
: (__hi
< __val
) ? __hi
: __val
;
3660 * @brief Returns the value clamped between lo and hi.
3661 * @ingroup sorting_algorithms
3662 * @param __val A value of arbitrary type.
3663 * @param __lo A lower limit of arbitrary type.
3664 * @param __hi An upper limit of arbitrary type.
3665 * @param __comp A comparison functor.
3666 * @return max(__val, __lo, __comp) if __comp(__val, __hi)
3667 * or min(__val, __hi, __comp) otherwise.
3669 template<typename _Tp
, typename _Compare
>
3670 constexpr const _Tp
&
3671 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3673 __glibcxx_assert(!__comp(__hi
, __lo
));
3674 return __comp(__val
, __lo
) ? __lo
: __comp(__hi
, __val
) ? __hi
: __val
;
3679 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3681 * @brief Generate two uniformly distributed integers using a
3682 * single distribution invocation.
3683 * @param __b0 The upper bound for the first integer.
3684 * @param __b1 The upper bound for the second integer.
3685 * @param __g A UniformRandomBitGenerator.
3686 * @return A pair (i, j) with i and j uniformly distributed
3687 * over [0, __b0) and [0, __b1), respectively.
3689 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3691 * Using uniform_int_distribution with a range that is very
3692 * small relative to the range of the generator ends up wasting
3693 * potentially expensively generated randomness, since
3694 * uniform_int_distribution does not store leftover randomness
3695 * between invocations.
3697 * If we know we want two integers in ranges that are sufficiently
3698 * small, we can compose the ranges, use a single distribution
3699 * invocation, and significantly reduce the waste.
3701 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3702 pair
<_IntType
, _IntType
>
3703 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3704 _UniformRandomBitGenerator
&& __g
)
3707 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3708 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3712 * @brief Shuffle the elements of a sequence using a uniform random
3714 * @ingroup mutating_algorithms
3715 * @param __first A forward iterator.
3716 * @param __last A forward iterator.
3717 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3720 * Reorders the elements in the range @p [__first,__last) using @p __g to
3721 * provide random numbers.
3723 template<typename _RandomAccessIterator
,
3724 typename _UniformRandomNumberGenerator
>
3726 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3727 _UniformRandomNumberGenerator
&& __g
)
3729 // concept requirements
3730 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3731 _RandomAccessIterator
>)
3732 __glibcxx_requires_valid_range(__first
, __last
);
3734 if (__first
== __last
)
3737 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3740 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3741 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3742 typedef typename
__distr_type::param_type __p_type
;
3744 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3746 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3749 const __uc_type __urngrange
= __g
.max() - __g
.min();
3750 const __uc_type __urange
= __uc_type(__last
- __first
);
3752 if (__urngrange
/ __urange
>= __urange
)
3753 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3755 _RandomAccessIterator __i
= __first
+ 1;
3757 // Since we know the range isn't empty, an even number of elements
3758 // means an uneven number of elements /to swap/, in which case we
3759 // do the first one up front:
3761 if ((__urange
% 2) == 0)
3763 __distr_type __d
{0, 1};
3764 std::iter_swap(__i
++, __first
+ __d(__g
));
3767 // Now we know that __last - __i is even, so we do the rest in pairs,
3768 // using a single distribution invocation to produce swap positions
3769 // for two successive elements at a time:
3771 while (__i
!= __last
)
3773 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3775 const pair
<__uc_type
, __uc_type
> __pospos
=
3776 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3778 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3779 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3787 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3788 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3794 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3797 * @brief Apply a function to every element of a sequence.
3798 * @ingroup non_mutating_algorithms
3799 * @param __first An input iterator.
3800 * @param __last An input iterator.
3801 * @param __f A unary function object.
3804 * Applies the function object @p __f to each element in the range
3805 * @p [first,last). @p __f must not modify the order of the sequence.
3806 * If @p __f has a return value it is ignored.
3808 template<typename _InputIterator
, typename _Function
>
3809 _GLIBCXX20_CONSTEXPR
3811 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3813 // concept requirements
3814 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3815 __glibcxx_requires_valid_range(__first
, __last
);
3816 for (; __first
!= __last
; ++__first
)
3818 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3821 #if __cplusplus >= 201703L
3823 * @brief Apply a function to every element of a sequence.
3824 * @ingroup non_mutating_algorithms
3825 * @param __first An input iterator.
3826 * @param __n A value convertible to an integer.
3827 * @param __f A unary function object.
3828 * @return `__first+__n`
3830 * Applies the function object `__f` to each element in the range
3831 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3832 * If `__f` has a return value it is ignored.
3834 template<typename _InputIterator
, typename _Size
, typename _Function
>
3835 _GLIBCXX20_CONSTEXPR
3837 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3839 auto __n2
= std::__size_to_integer(__n
);
3840 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3841 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3845 auto __last
= __first
+ __n2
;
3846 std::for_each(__first
, __last
, std::move(__f
));
3862 * @brief Find the first occurrence of a value in a sequence.
3863 * @ingroup non_mutating_algorithms
3864 * @param __first An input iterator.
3865 * @param __last An input iterator.
3866 * @param __val The value to find.
3867 * @return The first iterator @c i in the range @p [__first,__last)
3868 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3870 template<typename _InputIterator
, typename _Tp
>
3871 _GLIBCXX20_CONSTEXPR
3872 inline _InputIterator
3873 find(_InputIterator __first
, _InputIterator __last
,
3876 // concept requirements
3877 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3878 __glibcxx_function_requires(_EqualOpConcept
<
3879 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3880 __glibcxx_requires_valid_range(__first
, __last
);
3881 return std::__find_if(__first
, __last
,
3882 __gnu_cxx::__ops::__iter_equals_val(__val
));
3886 * @brief Find the first element in a sequence for which a
3887 * predicate is true.
3888 * @ingroup non_mutating_algorithms
3889 * @param __first An input iterator.
3890 * @param __last An input iterator.
3891 * @param __pred A predicate.
3892 * @return The first iterator @c i in the range @p [__first,__last)
3893 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3895 template<typename _InputIterator
, typename _Predicate
>
3896 _GLIBCXX20_CONSTEXPR
3897 inline _InputIterator
3898 find_if(_InputIterator __first
, _InputIterator __last
,
3901 // concept requirements
3902 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3903 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3904 typename iterator_traits
<_InputIterator
>::value_type
>)
3905 __glibcxx_requires_valid_range(__first
, __last
);
3907 return std::__find_if(__first
, __last
,
3908 __gnu_cxx::__ops::__pred_iter(__pred
));
3912 * @brief Find element from a set in a sequence.
3913 * @ingroup non_mutating_algorithms
3914 * @param __first1 Start of range to search.
3915 * @param __last1 End of range to search.
3916 * @param __first2 Start of match candidates.
3917 * @param __last2 End of match candidates.
3918 * @return The first iterator @c i in the range
3919 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3920 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3922 * Searches the range @p [__first1,__last1) for an element that is
3923 * equal to some element in the range [__first2,__last2). If
3924 * found, returns an iterator in the range [__first1,__last1),
3925 * otherwise returns @p __last1.
3927 template<typename _InputIterator
, typename _ForwardIterator
>
3928 _GLIBCXX20_CONSTEXPR
3930 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3931 _ForwardIterator __first2
, _ForwardIterator __last2
)
3933 // concept requirements
3934 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3935 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3936 __glibcxx_function_requires(_EqualOpConcept
<
3937 typename iterator_traits
<_InputIterator
>::value_type
,
3938 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3939 __glibcxx_requires_valid_range(__first1
, __last1
);
3940 __glibcxx_requires_valid_range(__first2
, __last2
);
3942 for (; __first1
!= __last1
; ++__first1
)
3943 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3944 if (*__first1
== *__iter
)
3950 * @brief Find element from a set in a sequence using a predicate.
3951 * @ingroup non_mutating_algorithms
3952 * @param __first1 Start of range to search.
3953 * @param __last1 End of range to search.
3954 * @param __first2 Start of match candidates.
3955 * @param __last2 End of match candidates.
3956 * @param __comp Predicate to use.
3957 * @return The first iterator @c i in the range
3958 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3959 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3960 * such iterator exists.
3963 * Searches the range @p [__first1,__last1) for an element that is
3964 * equal to some element in the range [__first2,__last2). If
3965 * found, returns an iterator in the range [__first1,__last1),
3966 * otherwise returns @p __last1.
3968 template<typename _InputIterator
, typename _ForwardIterator
,
3969 typename _BinaryPredicate
>
3970 _GLIBCXX20_CONSTEXPR
3972 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3973 _ForwardIterator __first2
, _ForwardIterator __last2
,
3974 _BinaryPredicate __comp
)
3976 // concept requirements
3977 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3978 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3979 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3980 typename iterator_traits
<_InputIterator
>::value_type
,
3981 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3982 __glibcxx_requires_valid_range(__first1
, __last1
);
3983 __glibcxx_requires_valid_range(__first2
, __last2
);
3985 for (; __first1
!= __last1
; ++__first1
)
3986 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3987 if (__comp(*__first1
, *__iter
))
3993 * @brief Find two adjacent values in a sequence that are equal.
3994 * @ingroup non_mutating_algorithms
3995 * @param __first A forward iterator.
3996 * @param __last A forward iterator.
3997 * @return The first iterator @c i such that @c i and @c i+1 are both
3998 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3999 * or @p __last if no such iterator exists.
4001 template<typename _ForwardIterator
>
4002 _GLIBCXX20_CONSTEXPR
4003 inline _ForwardIterator
4004 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4006 // concept requirements
4007 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4008 __glibcxx_function_requires(_EqualityComparableConcept
<
4009 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4010 __glibcxx_requires_valid_range(__first
, __last
);
4012 return std::__adjacent_find(__first
, __last
,
4013 __gnu_cxx::__ops::__iter_equal_to_iter());
4017 * @brief Find two adjacent values in a sequence using a predicate.
4018 * @ingroup non_mutating_algorithms
4019 * @param __first A forward iterator.
4020 * @param __last A forward iterator.
4021 * @param __binary_pred A binary predicate.
4022 * @return The first iterator @c i such that @c i and @c i+1 are both
4023 * valid iterators in @p [__first,__last) and such that
4024 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4027 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4028 _GLIBCXX20_CONSTEXPR
4029 inline _ForwardIterator
4030 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4031 _BinaryPredicate __binary_pred
)
4033 // concept requirements
4034 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4035 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4036 typename iterator_traits
<_ForwardIterator
>::value_type
,
4037 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4038 __glibcxx_requires_valid_range(__first
, __last
);
4040 return std::__adjacent_find(__first
, __last
,
4041 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4045 * @brief Count the number of copies of a value in a sequence.
4046 * @ingroup non_mutating_algorithms
4047 * @param __first An input iterator.
4048 * @param __last An input iterator.
4049 * @param __value The value to be counted.
4050 * @return The number of iterators @c i in the range @p [__first,__last)
4051 * for which @c *i == @p __value
4053 template<typename _InputIterator
, typename _Tp
>
4054 _GLIBCXX20_CONSTEXPR
4055 inline typename iterator_traits
<_InputIterator
>::difference_type
4056 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4058 // concept requirements
4059 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4060 __glibcxx_function_requires(_EqualOpConcept
<
4061 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4062 __glibcxx_requires_valid_range(__first
, __last
);
4064 return std::__count_if(__first
, __last
,
4065 __gnu_cxx::__ops::__iter_equals_val(__value
));
4069 * @brief Count the elements of a sequence for which a predicate is true.
4070 * @ingroup non_mutating_algorithms
4071 * @param __first An input iterator.
4072 * @param __last An input iterator.
4073 * @param __pred A predicate.
4074 * @return The number of iterators @c i in the range @p [__first,__last)
4075 * for which @p __pred(*i) is true.
4077 template<typename _InputIterator
, typename _Predicate
>
4078 _GLIBCXX20_CONSTEXPR
4079 inline typename iterator_traits
<_InputIterator
>::difference_type
4080 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4082 // concept requirements
4083 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4084 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4085 typename iterator_traits
<_InputIterator
>::value_type
>)
4086 __glibcxx_requires_valid_range(__first
, __last
);
4088 return std::__count_if(__first
, __last
,
4089 __gnu_cxx::__ops::__pred_iter(__pred
));
4093 * @brief Search a sequence for a matching sub-sequence.
4094 * @ingroup non_mutating_algorithms
4095 * @param __first1 A forward iterator.
4096 * @param __last1 A forward iterator.
4097 * @param __first2 A forward iterator.
4098 * @param __last2 A forward iterator.
4099 * @return The first iterator @c i in the range @p
4100 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4101 * *(__first2+N) for each @c N in the range @p
4102 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4104 * Searches the range @p [__first1,__last1) for a sub-sequence that
4105 * compares equal value-by-value with the sequence given by @p
4106 * [__first2,__last2) and returns an iterator to the first element
4107 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4110 * Because the sub-sequence must lie completely within the range @p
4111 * [__first1,__last1) it must start at a position less than @p
4112 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4113 * length of the sub-sequence.
4115 * This means that the returned iterator @c i will be in the range
4116 * @p [__first1,__last1-(__last2-__first2))
4118 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4119 _GLIBCXX20_CONSTEXPR
4120 inline _ForwardIterator1
4121 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4122 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4124 // concept requirements
4125 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4126 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4127 __glibcxx_function_requires(_EqualOpConcept
<
4128 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4129 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4130 __glibcxx_requires_valid_range(__first1
, __last1
);
4131 __glibcxx_requires_valid_range(__first2
, __last2
);
4133 return std::__search(__first1
, __last1
, __first2
, __last2
,
4134 __gnu_cxx::__ops::__iter_equal_to_iter());
4138 * @brief Search a sequence for a matching sub-sequence using a predicate.
4139 * @ingroup non_mutating_algorithms
4140 * @param __first1 A forward iterator.
4141 * @param __last1 A forward iterator.
4142 * @param __first2 A forward iterator.
4143 * @param __last2 A forward iterator.
4144 * @param __predicate A binary predicate.
4145 * @return The first iterator @c i in the range
4146 * @p [__first1,__last1-(__last2-__first2)) such that
4147 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4148 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4150 * Searches the range @p [__first1,__last1) for a sub-sequence that
4151 * compares equal value-by-value with the sequence given by @p
4152 * [__first2,__last2), using @p __predicate to determine equality,
4153 * and returns an iterator to the first element of the
4154 * sub-sequence, or @p __last1 if no such iterator exists.
4156 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4158 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4159 typename _BinaryPredicate
>
4160 _GLIBCXX20_CONSTEXPR
4161 inline _ForwardIterator1
4162 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4163 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4164 _BinaryPredicate __predicate
)
4166 // concept requirements
4167 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4168 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4169 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4170 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4171 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4172 __glibcxx_requires_valid_range(__first1
, __last1
);
4173 __glibcxx_requires_valid_range(__first2
, __last2
);
4175 return std::__search(__first1
, __last1
, __first2
, __last2
,
4176 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4180 * @brief Search a sequence for a number of consecutive values.
4181 * @ingroup non_mutating_algorithms
4182 * @param __first A forward iterator.
4183 * @param __last A forward iterator.
4184 * @param __count The number of consecutive values.
4185 * @param __val The value to find.
4186 * @return The first iterator @c i in the range @p
4187 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4188 * each @c N in the range @p [0,__count), or @p __last if no such
4191 * Searches the range @p [__first,__last) for @p count consecutive elements
4192 * equal to @p __val.
4194 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4195 _GLIBCXX20_CONSTEXPR
4196 inline _ForwardIterator
4197 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4198 _Integer __count
, const _Tp
& __val
)
4200 // concept requirements
4201 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4202 __glibcxx_function_requires(_EqualOpConcept
<
4203 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4204 __glibcxx_requires_valid_range(__first
, __last
);
4206 return std::__search_n(__first
, __last
, __count
,
4207 __gnu_cxx::__ops::__iter_equals_val(__val
));
4212 * @brief Search a sequence for a number of consecutive values using a
4214 * @ingroup non_mutating_algorithms
4215 * @param __first A forward iterator.
4216 * @param __last A forward iterator.
4217 * @param __count The number of consecutive values.
4218 * @param __val The value to find.
4219 * @param __binary_pred A binary predicate.
4220 * @return The first iterator @c i in the range @p
4221 * [__first,__last-__count) such that @p
4222 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4223 * @p [0,__count), or @p __last if no such iterator exists.
4225 * Searches the range @p [__first,__last) for @p __count
4226 * consecutive elements for which the predicate returns true.
4228 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4229 typename _BinaryPredicate
>
4230 _GLIBCXX20_CONSTEXPR
4231 inline _ForwardIterator
4232 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4233 _Integer __count
, const _Tp
& __val
,
4234 _BinaryPredicate __binary_pred
)
4236 // concept requirements
4237 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4238 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4239 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4240 __glibcxx_requires_valid_range(__first
, __last
);
4242 return std::__search_n(__first
, __last
, __count
,
4243 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4246 #if __cplusplus > 201402L
4247 /** @brief Search a sequence using a Searcher object.
4249 * @param __first A forward iterator.
4250 * @param __last A forward iterator.
4251 * @param __searcher A callable object.
4252 * @return @p __searcher(__first,__last).first
4254 template<typename _ForwardIterator
, typename _Searcher
>
4255 _GLIBCXX20_CONSTEXPR
4256 inline _ForwardIterator
4257 search(_ForwardIterator __first
, _ForwardIterator __last
,
4258 const _Searcher
& __searcher
)
4259 { return __searcher(__first
, __last
).first
; }
4263 * @brief Perform an operation on a sequence.
4264 * @ingroup mutating_algorithms
4265 * @param __first An input iterator.
4266 * @param __last An input iterator.
4267 * @param __result An output iterator.
4268 * @param __unary_op A unary operator.
4269 * @return An output iterator equal to @p __result+(__last-__first).
4271 * Applies the operator to each element in the input range and assigns
4272 * the results to successive elements of the output sequence.
4273 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4274 * range @p [0,__last-__first).
4276 * @p unary_op must not alter its argument.
4278 template<typename _InputIterator
, typename _OutputIterator
,
4279 typename _UnaryOperation
>
4280 _GLIBCXX20_CONSTEXPR
4282 transform(_InputIterator __first
, _InputIterator __last
,
4283 _OutputIterator __result
, _UnaryOperation __unary_op
)
4285 // concept requirements
4286 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4287 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4288 // "the type returned by a _UnaryOperation"
4289 __typeof__(__unary_op(*__first
))>)
4290 __glibcxx_requires_valid_range(__first
, __last
);
4292 for (; __first
!= __last
; ++__first
, (void)++__result
)
4293 *__result
= __unary_op(*__first
);
4298 * @brief Perform an operation on corresponding elements of two sequences.
4299 * @ingroup mutating_algorithms
4300 * @param __first1 An input iterator.
4301 * @param __last1 An input iterator.
4302 * @param __first2 An input iterator.
4303 * @param __result An output iterator.
4304 * @param __binary_op A binary operator.
4305 * @return An output iterator equal to @p result+(last-first).
4307 * Applies the operator to the corresponding elements in the two
4308 * input ranges and assigns the results to successive elements of the
4311 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4312 * @c N in the range @p [0,__last1-__first1).
4314 * @p binary_op must not alter either of its arguments.
4316 template<typename _InputIterator1
, typename _InputIterator2
,
4317 typename _OutputIterator
, typename _BinaryOperation
>
4318 _GLIBCXX20_CONSTEXPR
4320 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4321 _InputIterator2 __first2
, _OutputIterator __result
,
4322 _BinaryOperation __binary_op
)
4324 // concept requirements
4325 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4326 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4327 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4328 // "the type returned by a _BinaryOperation"
4329 __typeof__(__binary_op(*__first1
,*__first2
))>)
4330 __glibcxx_requires_valid_range(__first1
, __last1
);
4332 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4333 *__result
= __binary_op(*__first1
, *__first2
);
4338 * @brief Replace each occurrence of one value in a sequence with another
4340 * @ingroup mutating_algorithms
4341 * @param __first A forward iterator.
4342 * @param __last A forward iterator.
4343 * @param __old_value The value to be replaced.
4344 * @param __new_value The replacement value.
4345 * @return replace() returns no value.
4347 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4348 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4350 template<typename _ForwardIterator
, typename _Tp
>
4351 _GLIBCXX20_CONSTEXPR
4353 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4354 const _Tp
& __old_value
, const _Tp
& __new_value
)
4356 // concept requirements
4357 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4359 __glibcxx_function_requires(_EqualOpConcept
<
4360 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4361 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4362 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4363 __glibcxx_requires_valid_range(__first
, __last
);
4365 for (; __first
!= __last
; ++__first
)
4366 if (*__first
== __old_value
)
4367 *__first
= __new_value
;
4371 * @brief Replace each value in a sequence for which a predicate returns
4372 * true with another value.
4373 * @ingroup mutating_algorithms
4374 * @param __first A forward iterator.
4375 * @param __last A forward iterator.
4376 * @param __pred A predicate.
4377 * @param __new_value The replacement value.
4378 * @return replace_if() returns no value.
4380 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4381 * is true then the assignment @c *i = @p __new_value is performed.
4383 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4384 _GLIBCXX20_CONSTEXPR
4386 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4387 _Predicate __pred
, const _Tp
& __new_value
)
4389 // concept requirements
4390 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4392 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4393 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4394 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4395 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4396 __glibcxx_requires_valid_range(__first
, __last
);
4398 for (; __first
!= __last
; ++__first
)
4399 if (__pred(*__first
))
4400 *__first
= __new_value
;
4404 * @brief Assign the result of a function object to each value in a
4406 * @ingroup mutating_algorithms
4407 * @param __first A forward iterator.
4408 * @param __last A forward iterator.
4409 * @param __gen A function object taking no arguments and returning
4410 * std::iterator_traits<_ForwardIterator>::value_type
4411 * @return generate() returns no value.
4413 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4414 * @p [__first,__last).
4416 template<typename _ForwardIterator
, typename _Generator
>
4417 _GLIBCXX20_CONSTEXPR
4419 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4422 // concept requirements
4423 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4424 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4425 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4426 __glibcxx_requires_valid_range(__first
, __last
);
4428 for (; __first
!= __last
; ++__first
)
4433 * @brief Assign the result of a function object to each value in a
4435 * @ingroup mutating_algorithms
4436 * @param __first A forward iterator.
4437 * @param __n The length of the sequence.
4438 * @param __gen A function object taking no arguments and returning
4439 * std::iterator_traits<_ForwardIterator>::value_type
4440 * @return The end of the sequence, @p __first+__n
4442 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4443 * @p [__first,__first+__n).
4445 * If @p __n is negative, the function does nothing and returns @p __first.
4447 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4448 // DR 865. More algorithms that throw away information
4449 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4450 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4451 _GLIBCXX20_CONSTEXPR
4453 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4455 // concept requirements
4456 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4457 // "the type returned by a _Generator"
4458 __typeof__(__gen())>)
4460 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4461 for (_IntSize __niter
= std::__size_to_integer(__n
);
4462 __niter
> 0; --__niter
, (void) ++__first
)
4468 * @brief Copy a sequence, removing consecutive duplicate values.
4469 * @ingroup mutating_algorithms
4470 * @param __first An input iterator.
4471 * @param __last An input iterator.
4472 * @param __result An output iterator.
4473 * @return An iterator designating the end of the resulting sequence.
4475 * Copies each element in the range @p [__first,__last) to the range
4476 * beginning at @p __result, except that only the first element is copied
4477 * from groups of consecutive elements that compare equal.
4478 * unique_copy() is stable, so the relative order of elements that are
4479 * copied is unchanged.
4481 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4482 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4484 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4485 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4488 template<typename _InputIterator
, typename _OutputIterator
>
4489 _GLIBCXX20_CONSTEXPR
4490 inline _OutputIterator
4491 unique_copy(_InputIterator __first
, _InputIterator __last
,
4492 _OutputIterator __result
)
4494 // concept requirements
4495 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4496 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4497 typename iterator_traits
<_InputIterator
>::value_type
>)
4498 __glibcxx_function_requires(_EqualityComparableConcept
<
4499 typename iterator_traits
<_InputIterator
>::value_type
>)
4500 __glibcxx_requires_valid_range(__first
, __last
);
4502 if (__first
== __last
)
4504 return std::__unique_copy(__first
, __last
, __result
,
4505 __gnu_cxx::__ops::__iter_equal_to_iter(),
4506 std::__iterator_category(__first
),
4507 std::__iterator_category(__result
));
4511 * @brief Copy a sequence, removing consecutive values using a predicate.
4512 * @ingroup mutating_algorithms
4513 * @param __first An input iterator.
4514 * @param __last An input iterator.
4515 * @param __result An output iterator.
4516 * @param __binary_pred A binary predicate.
4517 * @return An iterator designating the end of the resulting sequence.
4519 * Copies each element in the range @p [__first,__last) to the range
4520 * beginning at @p __result, except that only the first element is copied
4521 * from groups of consecutive elements for which @p __binary_pred returns
4523 * unique_copy() is stable, so the relative order of elements that are
4524 * copied is unchanged.
4526 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4527 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4529 template<typename _InputIterator
, typename _OutputIterator
,
4530 typename _BinaryPredicate
>
4531 _GLIBCXX20_CONSTEXPR
4532 inline _OutputIterator
4533 unique_copy(_InputIterator __first
, _InputIterator __last
,
4534 _OutputIterator __result
,
4535 _BinaryPredicate __binary_pred
)
4537 // concept requirements -- predicates checked later
4538 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4539 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4540 typename iterator_traits
<_InputIterator
>::value_type
>)
4541 __glibcxx_requires_valid_range(__first
, __last
);
4543 if (__first
== __last
)
4545 return std::__unique_copy(__first
, __last
, __result
,
4546 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4547 std::__iterator_category(__first
),
4548 std::__iterator_category(__result
));
4553 * @brief Randomly shuffle the elements of a sequence.
4554 * @ingroup mutating_algorithms
4555 * @param __first A forward iterator.
4556 * @param __last A forward iterator.
4559 * Reorder the elements in the range @p [__first,__last) using a random
4560 * distribution, so that every possible ordering of the sequence is
4563 template<typename _RandomAccessIterator
>
4565 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4567 // concept requirements
4568 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4569 _RandomAccessIterator
>)
4570 __glibcxx_requires_valid_range(__first
, __last
);
4572 if (__first
!= __last
)
4573 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4575 // XXX rand() % N is not uniformly distributed
4576 _RandomAccessIterator __j
= __first
4577 + std::rand() % ((__i
- __first
) + 1);
4579 std::iter_swap(__i
, __j
);
4585 * @brief Shuffle the elements of a sequence using a random number
4587 * @ingroup mutating_algorithms
4588 * @param __first A forward iterator.
4589 * @param __last A forward iterator.
4590 * @param __rand The RNG functor or function.
4593 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4594 * provide a random distribution. Calling @p __rand(N) for a positive
4595 * integer @p N should return a randomly chosen integer from the
4598 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4600 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4601 #if __cplusplus >= 201103L
4602 _RandomNumberGenerator
&& __rand
)
4604 _RandomNumberGenerator
& __rand
)
4607 // concept requirements
4608 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4609 _RandomAccessIterator
>)
4610 __glibcxx_requires_valid_range(__first
, __last
);
4612 if (__first
== __last
)
4614 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4616 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4618 std::iter_swap(__i
, __j
);
4624 * @brief Move elements for which a predicate is true to the beginning
4626 * @ingroup mutating_algorithms
4627 * @param __first A forward iterator.
4628 * @param __last A forward iterator.
4629 * @param __pred A predicate functor.
4630 * @return An iterator @p middle such that @p __pred(i) is true for each
4631 * iterator @p i in the range @p [__first,middle) and false for each @p i
4632 * in the range @p [middle,__last).
4634 * @p __pred must not modify its operand. @p partition() does not preserve
4635 * the relative ordering of elements in each group, use
4636 * @p stable_partition() if this is needed.
4638 template<typename _ForwardIterator
, typename _Predicate
>
4639 _GLIBCXX20_CONSTEXPR
4640 inline _ForwardIterator
4641 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4644 // concept requirements
4645 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4647 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4648 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4649 __glibcxx_requires_valid_range(__first
, __last
);
4651 return std::__partition(__first
, __last
, __pred
,
4652 std::__iterator_category(__first
));
4657 * @brief Sort the smallest elements of a sequence.
4658 * @ingroup sorting_algorithms
4659 * @param __first An iterator.
4660 * @param __middle Another iterator.
4661 * @param __last Another iterator.
4664 * Sorts the smallest @p (__middle-__first) elements in the range
4665 * @p [first,last) and moves them to the range @p [__first,__middle). The
4666 * order of the remaining elements in the range @p [__middle,__last) is
4668 * After the sort if @e i and @e j are iterators in the range
4669 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4670 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4672 template<typename _RandomAccessIterator
>
4673 _GLIBCXX20_CONSTEXPR
4675 partial_sort(_RandomAccessIterator __first
,
4676 _RandomAccessIterator __middle
,
4677 _RandomAccessIterator __last
)
4679 // concept requirements
4680 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4681 _RandomAccessIterator
>)
4682 __glibcxx_function_requires(_LessThanComparableConcept
<
4683 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4684 __glibcxx_requires_valid_range(__first
, __middle
);
4685 __glibcxx_requires_valid_range(__middle
, __last
);
4686 __glibcxx_requires_irreflexive(__first
, __last
);
4688 std::__partial_sort(__first
, __middle
, __last
,
4689 __gnu_cxx::__ops::__iter_less_iter());
4693 * @brief Sort the smallest elements of a sequence using a predicate
4695 * @ingroup sorting_algorithms
4696 * @param __first An iterator.
4697 * @param __middle Another iterator.
4698 * @param __last Another iterator.
4699 * @param __comp A comparison functor.
4702 * Sorts the smallest @p (__middle-__first) elements in the range
4703 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4704 * order of the remaining elements in the range @p [__middle,__last) is
4706 * After the sort if @e i and @e j are iterators in the range
4707 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4708 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4711 template<typename _RandomAccessIterator
, typename _Compare
>
4712 _GLIBCXX20_CONSTEXPR
4714 partial_sort(_RandomAccessIterator __first
,
4715 _RandomAccessIterator __middle
,
4716 _RandomAccessIterator __last
,
4719 // concept requirements
4720 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4721 _RandomAccessIterator
>)
4722 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4723 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4724 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4725 __glibcxx_requires_valid_range(__first
, __middle
);
4726 __glibcxx_requires_valid_range(__middle
, __last
);
4727 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4729 std::__partial_sort(__first
, __middle
, __last
,
4730 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4734 * @brief Sort a sequence just enough to find a particular position.
4735 * @ingroup sorting_algorithms
4736 * @param __first An iterator.
4737 * @param __nth Another iterator.
4738 * @param __last Another iterator.
4741 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4742 * is the same element that would have been in that position had the
4743 * whole sequence been sorted. The elements either side of @p *__nth are
4744 * not completely sorted, but for any iterator @e i in the range
4745 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4746 * holds that *j < *i is false.
4748 template<typename _RandomAccessIterator
>
4749 _GLIBCXX20_CONSTEXPR
4751 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4752 _RandomAccessIterator __last
)
4754 // concept requirements
4755 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4756 _RandomAccessIterator
>)
4757 __glibcxx_function_requires(_LessThanComparableConcept
<
4758 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4759 __glibcxx_requires_valid_range(__first
, __nth
);
4760 __glibcxx_requires_valid_range(__nth
, __last
);
4761 __glibcxx_requires_irreflexive(__first
, __last
);
4763 if (__first
== __last
|| __nth
== __last
)
4766 std::__introselect(__first
, __nth
, __last
,
4767 std::__lg(__last
- __first
) * 2,
4768 __gnu_cxx::__ops::__iter_less_iter());
4772 * @brief Sort a sequence just enough to find a particular position
4773 * using a predicate for comparison.
4774 * @ingroup sorting_algorithms
4775 * @param __first An iterator.
4776 * @param __nth Another iterator.
4777 * @param __last Another iterator.
4778 * @param __comp A comparison functor.
4781 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4782 * is the same element that would have been in that position had the
4783 * whole sequence been sorted. The elements either side of @p *__nth are
4784 * not completely sorted, but for any iterator @e i in the range
4785 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4786 * holds that @p __comp(*j,*i) is false.
4788 template<typename _RandomAccessIterator
, typename _Compare
>
4789 _GLIBCXX20_CONSTEXPR
4791 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4792 _RandomAccessIterator __last
, _Compare __comp
)
4794 // concept requirements
4795 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4796 _RandomAccessIterator
>)
4797 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4798 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4799 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4800 __glibcxx_requires_valid_range(__first
, __nth
);
4801 __glibcxx_requires_valid_range(__nth
, __last
);
4802 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4804 if (__first
== __last
|| __nth
== __last
)
4807 std::__introselect(__first
, __nth
, __last
,
4808 std::__lg(__last
- __first
) * 2,
4809 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4813 * @brief Sort the elements of a sequence.
4814 * @ingroup sorting_algorithms
4815 * @param __first An iterator.
4816 * @param __last Another iterator.
4819 * Sorts the elements in the range @p [__first,__last) in ascending order,
4820 * such that for each iterator @e i in the range @p [__first,__last-1),
4821 * *(i+1)<*i is false.
4823 * The relative ordering of equivalent elements is not preserved, use
4824 * @p stable_sort() if this is needed.
4826 template<typename _RandomAccessIterator
>
4827 _GLIBCXX20_CONSTEXPR
4829 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4831 // concept requirements
4832 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4833 _RandomAccessIterator
>)
4834 __glibcxx_function_requires(_LessThanComparableConcept
<
4835 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4836 __glibcxx_requires_valid_range(__first
, __last
);
4837 __glibcxx_requires_irreflexive(__first
, __last
);
4839 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4843 * @brief Sort the elements of a sequence using a predicate for comparison.
4844 * @ingroup sorting_algorithms
4845 * @param __first An iterator.
4846 * @param __last Another iterator.
4847 * @param __comp A comparison functor.
4850 * Sorts the elements in the range @p [__first,__last) in ascending order,
4851 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4852 * range @p [__first,__last-1).
4854 * The relative ordering of equivalent elements is not preserved, use
4855 * @p stable_sort() if this is needed.
4857 template<typename _RandomAccessIterator
, typename _Compare
>
4858 _GLIBCXX20_CONSTEXPR
4860 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4863 // concept requirements
4864 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4865 _RandomAccessIterator
>)
4866 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4867 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4868 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4869 __glibcxx_requires_valid_range(__first
, __last
);
4870 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4872 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4875 template<typename _InputIterator1
, typename _InputIterator2
,
4876 typename _OutputIterator
, typename _Compare
>
4877 _GLIBCXX20_CONSTEXPR
4879 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4880 _InputIterator2 __first2
, _InputIterator2 __last2
,
4881 _OutputIterator __result
, _Compare __comp
)
4883 while (__first1
!= __last1
&& __first2
!= __last2
)
4885 if (__comp(__first2
, __first1
))
4887 *__result
= *__first2
;
4892 *__result
= *__first1
;
4897 return std::copy(__first2
, __last2
,
4898 std::copy(__first1
, __last1
, __result
));
4902 * @brief Merges two sorted ranges.
4903 * @ingroup sorting_algorithms
4904 * @param __first1 An iterator.
4905 * @param __first2 Another iterator.
4906 * @param __last1 Another iterator.
4907 * @param __last2 Another iterator.
4908 * @param __result An iterator pointing to the end of the merged range.
4909 * @return An output iterator equal to @p __result + (__last1 - __first1)
4910 * + (__last2 - __first2).
4912 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4913 * the sorted range @p [__result, __result + (__last1-__first1) +
4914 * (__last2-__first2)). Both input ranges must be sorted, and the
4915 * output range must not overlap with either of the input ranges.
4916 * The sort is @e stable, that is, for equivalent elements in the
4917 * two ranges, elements from the first range will always come
4918 * before elements from the second.
4920 template<typename _InputIterator1
, typename _InputIterator2
,
4921 typename _OutputIterator
>
4922 _GLIBCXX20_CONSTEXPR
4923 inline _OutputIterator
4924 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4925 _InputIterator2 __first2
, _InputIterator2 __last2
,
4926 _OutputIterator __result
)
4928 // concept requirements
4929 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4930 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4931 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4932 typename iterator_traits
<_InputIterator1
>::value_type
>)
4933 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4934 typename iterator_traits
<_InputIterator2
>::value_type
>)
4935 __glibcxx_function_requires(_LessThanOpConcept
<
4936 typename iterator_traits
<_InputIterator2
>::value_type
,
4937 typename iterator_traits
<_InputIterator1
>::value_type
>)
4938 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4939 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4940 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4941 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4943 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4944 __first2
, __last2
, __result
,
4945 __gnu_cxx::__ops::__iter_less_iter());
4949 * @brief Merges two sorted ranges.
4950 * @ingroup sorting_algorithms
4951 * @param __first1 An iterator.
4952 * @param __first2 Another iterator.
4953 * @param __last1 Another iterator.
4954 * @param __last2 Another iterator.
4955 * @param __result An iterator pointing to the end of the merged range.
4956 * @param __comp A functor to use for comparisons.
4957 * @return An output iterator equal to @p __result + (__last1 - __first1)
4958 * + (__last2 - __first2).
4960 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4961 * the sorted range @p [__result, __result + (__last1-__first1) +
4962 * (__last2-__first2)). Both input ranges must be sorted, and the
4963 * output range must not overlap with either of the input ranges.
4964 * The sort is @e stable, that is, for equivalent elements in the
4965 * two ranges, elements from the first range will always come
4966 * before elements from the second.
4968 * The comparison function should have the same effects on ordering as
4969 * the function used for the initial sort.
4971 template<typename _InputIterator1
, typename _InputIterator2
,
4972 typename _OutputIterator
, typename _Compare
>
4973 _GLIBCXX20_CONSTEXPR
4974 inline _OutputIterator
4975 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4976 _InputIterator2 __first2
, _InputIterator2 __last2
,
4977 _OutputIterator __result
, _Compare __comp
)
4979 // concept requirements
4980 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4981 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4982 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4983 typename iterator_traits
<_InputIterator1
>::value_type
>)
4984 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4985 typename iterator_traits
<_InputIterator2
>::value_type
>)
4986 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4987 typename iterator_traits
<_InputIterator2
>::value_type
,
4988 typename iterator_traits
<_InputIterator1
>::value_type
>)
4989 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4990 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4991 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
4992 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
4994 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4995 __first2
, __last2
, __result
,
4996 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4999 template<typename _RandomAccessIterator
, typename _Compare
>
5001 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5004 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5006 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5009 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5010 _TmpBuf
__buf(__first
, std::distance(__first
, __last
));
5012 if (__buf
.begin() == 0)
5013 std::__inplace_stable_sort(__first
, __last
, __comp
);
5015 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5016 _DistanceType(__buf
.size()), __comp
);
5020 * @brief Sort the elements of a sequence, preserving the relative order
5021 * of equivalent elements.
5022 * @ingroup sorting_algorithms
5023 * @param __first An iterator.
5024 * @param __last Another iterator.
5027 * Sorts the elements in the range @p [__first,__last) in ascending order,
5028 * such that for each iterator @p i in the range @p [__first,__last-1),
5029 * @p *(i+1)<*i is false.
5031 * The relative ordering of equivalent elements is preserved, so any two
5032 * elements @p x and @p y in the range @p [__first,__last) such that
5033 * @p x<y is false and @p y<x is false will have the same relative
5034 * ordering after calling @p stable_sort().
5036 template<typename _RandomAccessIterator
>
5038 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5040 // concept requirements
5041 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5042 _RandomAccessIterator
>)
5043 __glibcxx_function_requires(_LessThanComparableConcept
<
5044 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5045 __glibcxx_requires_valid_range(__first
, __last
);
5046 __glibcxx_requires_irreflexive(__first
, __last
);
5048 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5049 __gnu_cxx::__ops::__iter_less_iter());
5053 * @brief Sort the elements of a sequence using a predicate for comparison,
5054 * preserving the relative order of equivalent elements.
5055 * @ingroup sorting_algorithms
5056 * @param __first An iterator.
5057 * @param __last Another iterator.
5058 * @param __comp A comparison functor.
5061 * Sorts the elements in the range @p [__first,__last) in ascending order,
5062 * such that for each iterator @p i in the range @p [__first,__last-1),
5063 * @p __comp(*(i+1),*i) is false.
5065 * The relative ordering of equivalent elements is preserved, so any two
5066 * elements @p x and @p y in the range @p [__first,__last) such that
5067 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5068 * relative ordering after calling @p stable_sort().
5070 template<typename _RandomAccessIterator
, typename _Compare
>
5072 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5075 // concept requirements
5076 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5077 _RandomAccessIterator
>)
5078 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5079 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5080 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5081 __glibcxx_requires_valid_range(__first
, __last
);
5082 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5084 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5085 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5088 template<typename _InputIterator1
, typename _InputIterator2
,
5089 typename _OutputIterator
,
5091 _GLIBCXX20_CONSTEXPR
5093 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5094 _InputIterator2 __first2
, _InputIterator2 __last2
,
5095 _OutputIterator __result
, _Compare __comp
)
5097 while (__first1
!= __last1
&& __first2
!= __last2
)
5099 if (__comp(__first1
, __first2
))
5101 *__result
= *__first1
;
5104 else if (__comp(__first2
, __first1
))
5106 *__result
= *__first2
;
5111 *__result
= *__first1
;
5117 return std::copy(__first2
, __last2
,
5118 std::copy(__first1
, __last1
, __result
));
5122 * @brief Return the union of two sorted ranges.
5123 * @ingroup set_algorithms
5124 * @param __first1 Start of first range.
5125 * @param __last1 End of first range.
5126 * @param __first2 Start of second range.
5127 * @param __last2 End of second range.
5128 * @param __result Start of output range.
5129 * @return End of the output range.
5130 * @ingroup set_algorithms
5132 * This operation iterates over both ranges, copying elements present in
5133 * each range in order to the output range. Iterators increment for each
5134 * range. When the current element of one range is less than the other,
5135 * that element is copied and the iterator advanced. If an element is
5136 * contained in both ranges, the element from the first range is copied and
5137 * both ranges advance. The output range may not overlap either input
5140 template<typename _InputIterator1
, typename _InputIterator2
,
5141 typename _OutputIterator
>
5142 _GLIBCXX20_CONSTEXPR
5143 inline _OutputIterator
5144 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5145 _InputIterator2 __first2
, _InputIterator2 __last2
,
5146 _OutputIterator __result
)
5148 // concept requirements
5149 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5150 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5151 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5152 typename iterator_traits
<_InputIterator1
>::value_type
>)
5153 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5154 typename iterator_traits
<_InputIterator2
>::value_type
>)
5155 __glibcxx_function_requires(_LessThanOpConcept
<
5156 typename iterator_traits
<_InputIterator1
>::value_type
,
5157 typename iterator_traits
<_InputIterator2
>::value_type
>)
5158 __glibcxx_function_requires(_LessThanOpConcept
<
5159 typename iterator_traits
<_InputIterator2
>::value_type
,
5160 typename iterator_traits
<_InputIterator1
>::value_type
>)
5161 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5162 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5163 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5164 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5166 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5167 __first2
, __last2
, __result
,
5168 __gnu_cxx::__ops::__iter_less_iter());
5172 * @brief Return the union of two sorted ranges using a comparison functor.
5173 * @ingroup set_algorithms
5174 * @param __first1 Start of first range.
5175 * @param __last1 End of first range.
5176 * @param __first2 Start of second range.
5177 * @param __last2 End of second range.
5178 * @param __result Start of output range.
5179 * @param __comp The comparison functor.
5180 * @return End of the output range.
5181 * @ingroup set_algorithms
5183 * This operation iterates over both ranges, copying elements present in
5184 * each range in order to the output range. Iterators increment for each
5185 * range. When the current element of one range is less than the other
5186 * according to @p __comp, that element is copied and the iterator advanced.
5187 * If an equivalent element according to @p __comp is contained in both
5188 * ranges, the element from the first range is copied and both ranges
5189 * advance. The output range may not overlap either input range.
5191 template<typename _InputIterator1
, typename _InputIterator2
,
5192 typename _OutputIterator
, typename _Compare
>
5193 _GLIBCXX20_CONSTEXPR
5194 inline _OutputIterator
5195 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5196 _InputIterator2 __first2
, _InputIterator2 __last2
,
5197 _OutputIterator __result
, _Compare __comp
)
5199 // concept requirements
5200 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5201 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5202 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5203 typename iterator_traits
<_InputIterator1
>::value_type
>)
5204 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5205 typename iterator_traits
<_InputIterator2
>::value_type
>)
5206 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5207 typename iterator_traits
<_InputIterator1
>::value_type
,
5208 typename iterator_traits
<_InputIterator2
>::value_type
>)
5209 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5210 typename iterator_traits
<_InputIterator2
>::value_type
,
5211 typename iterator_traits
<_InputIterator1
>::value_type
>)
5212 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5213 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5214 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5215 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5217 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5218 __first2
, __last2
, __result
,
5219 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5222 template<typename _InputIterator1
, typename _InputIterator2
,
5223 typename _OutputIterator
,
5225 _GLIBCXX20_CONSTEXPR
5227 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5228 _InputIterator2 __first2
, _InputIterator2 __last2
,
5229 _OutputIterator __result
, _Compare __comp
)
5231 while (__first1
!= __last1
&& __first2
!= __last2
)
5232 if (__comp(__first1
, __first2
))
5234 else if (__comp(__first2
, __first1
))
5238 *__result
= *__first1
;
5247 * @brief Return the intersection of two sorted ranges.
5248 * @ingroup set_algorithms
5249 * @param __first1 Start of first range.
5250 * @param __last1 End of first range.
5251 * @param __first2 Start of second range.
5252 * @param __last2 End of second range.
5253 * @param __result Start of output range.
5254 * @return End of the output range.
5255 * @ingroup set_algorithms
5257 * This operation iterates over both ranges, copying elements present in
5258 * both ranges in order to the output range. Iterators increment for each
5259 * range. When the current element of one range is less than the other,
5260 * that iterator advances. If an element is contained in both ranges, the
5261 * element from the first range is copied and both ranges advance. The
5262 * output range may not overlap either input range.
5264 template<typename _InputIterator1
, typename _InputIterator2
,
5265 typename _OutputIterator
>
5266 _GLIBCXX20_CONSTEXPR
5267 inline _OutputIterator
5268 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5269 _InputIterator2 __first2
, _InputIterator2 __last2
,
5270 _OutputIterator __result
)
5272 // concept requirements
5273 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5274 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5275 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5276 typename iterator_traits
<_InputIterator1
>::value_type
>)
5277 __glibcxx_function_requires(_LessThanOpConcept
<
5278 typename iterator_traits
<_InputIterator1
>::value_type
,
5279 typename iterator_traits
<_InputIterator2
>::value_type
>)
5280 __glibcxx_function_requires(_LessThanOpConcept
<
5281 typename iterator_traits
<_InputIterator2
>::value_type
,
5282 typename iterator_traits
<_InputIterator1
>::value_type
>)
5283 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5284 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5285 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5286 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5288 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5289 __first2
, __last2
, __result
,
5290 __gnu_cxx::__ops::__iter_less_iter());
5294 * @brief Return the intersection of two sorted ranges using comparison
5296 * @ingroup set_algorithms
5297 * @param __first1 Start of first range.
5298 * @param __last1 End of first range.
5299 * @param __first2 Start of second range.
5300 * @param __last2 End of second range.
5301 * @param __result Start of output range.
5302 * @param __comp The comparison functor.
5303 * @return End of the output range.
5304 * @ingroup set_algorithms
5306 * This operation iterates over both ranges, copying elements present in
5307 * both ranges in order to the output range. Iterators increment for each
5308 * range. When the current element of one range is less than the other
5309 * according to @p __comp, that iterator advances. If an element is
5310 * contained in both ranges according to @p __comp, the element from the
5311 * first range is copied and both ranges advance. The output range may not
5312 * overlap either input range.
5314 template<typename _InputIterator1
, typename _InputIterator2
,
5315 typename _OutputIterator
, typename _Compare
>
5316 _GLIBCXX20_CONSTEXPR
5317 inline _OutputIterator
5318 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5319 _InputIterator2 __first2
, _InputIterator2 __last2
,
5320 _OutputIterator __result
, _Compare __comp
)
5322 // concept requirements
5323 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5324 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5325 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5326 typename iterator_traits
<_InputIterator1
>::value_type
>)
5327 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5328 typename iterator_traits
<_InputIterator1
>::value_type
,
5329 typename iterator_traits
<_InputIterator2
>::value_type
>)
5330 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5331 typename iterator_traits
<_InputIterator2
>::value_type
,
5332 typename iterator_traits
<_InputIterator1
>::value_type
>)
5333 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5334 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5335 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5336 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5338 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5339 __first2
, __last2
, __result
,
5340 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5343 template<typename _InputIterator1
, typename _InputIterator2
,
5344 typename _OutputIterator
,
5346 _GLIBCXX20_CONSTEXPR
5348 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5349 _InputIterator2 __first2
, _InputIterator2 __last2
,
5350 _OutputIterator __result
, _Compare __comp
)
5352 while (__first1
!= __last1
&& __first2
!= __last2
)
5353 if (__comp(__first1
, __first2
))
5355 *__result
= *__first1
;
5359 else if (__comp(__first2
, __first1
))
5366 return std::copy(__first1
, __last1
, __result
);
5370 * @brief Return the difference of two sorted ranges.
5371 * @ingroup set_algorithms
5372 * @param __first1 Start of first range.
5373 * @param __last1 End of first range.
5374 * @param __first2 Start of second range.
5375 * @param __last2 End of second range.
5376 * @param __result Start of output range.
5377 * @return End of the output range.
5378 * @ingroup set_algorithms
5380 * This operation iterates over both ranges, copying elements present in
5381 * the first range but not the second in order to the output range.
5382 * Iterators increment for each range. When the current element of the
5383 * first range is less than the second, that element is copied and the
5384 * iterator advances. If the current element of the second range is less,
5385 * the iterator advances, but no element is copied. If an element is
5386 * contained in both ranges, no elements are copied and both ranges
5387 * advance. The output range may not overlap either input range.
5389 template<typename _InputIterator1
, typename _InputIterator2
,
5390 typename _OutputIterator
>
5391 _GLIBCXX20_CONSTEXPR
5392 inline _OutputIterator
5393 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5394 _InputIterator2 __first2
, _InputIterator2 __last2
,
5395 _OutputIterator __result
)
5397 // concept requirements
5398 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5399 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5400 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5401 typename iterator_traits
<_InputIterator1
>::value_type
>)
5402 __glibcxx_function_requires(_LessThanOpConcept
<
5403 typename iterator_traits
<_InputIterator1
>::value_type
,
5404 typename iterator_traits
<_InputIterator2
>::value_type
>)
5405 __glibcxx_function_requires(_LessThanOpConcept
<
5406 typename iterator_traits
<_InputIterator2
>::value_type
,
5407 typename iterator_traits
<_InputIterator1
>::value_type
>)
5408 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5409 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5410 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5411 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5413 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5414 __first2
, __last2
, __result
,
5415 __gnu_cxx::__ops::__iter_less_iter());
5419 * @brief Return the difference of two sorted ranges using comparison
5421 * @ingroup set_algorithms
5422 * @param __first1 Start of first range.
5423 * @param __last1 End of first range.
5424 * @param __first2 Start of second range.
5425 * @param __last2 End of second range.
5426 * @param __result Start of output range.
5427 * @param __comp The comparison functor.
5428 * @return End of the output range.
5429 * @ingroup set_algorithms
5431 * This operation iterates over both ranges, copying elements present in
5432 * the first range but not the second in order to the output range.
5433 * Iterators increment for each range. When the current element of the
5434 * first range is less than the second according to @p __comp, that element
5435 * is copied and the iterator advances. If the current element of the
5436 * second range is less, no element is copied and the iterator advances.
5437 * If an element is contained in both ranges according to @p __comp, no
5438 * elements are copied and both ranges advance. The output range may not
5439 * overlap either input range.
5441 template<typename _InputIterator1
, typename _InputIterator2
,
5442 typename _OutputIterator
, typename _Compare
>
5443 _GLIBCXX20_CONSTEXPR
5444 inline _OutputIterator
5445 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5446 _InputIterator2 __first2
, _InputIterator2 __last2
,
5447 _OutputIterator __result
, _Compare __comp
)
5449 // concept requirements
5450 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5451 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5452 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5453 typename iterator_traits
<_InputIterator1
>::value_type
>)
5454 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5455 typename iterator_traits
<_InputIterator1
>::value_type
,
5456 typename iterator_traits
<_InputIterator2
>::value_type
>)
5457 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5458 typename iterator_traits
<_InputIterator2
>::value_type
,
5459 typename iterator_traits
<_InputIterator1
>::value_type
>)
5460 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5461 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5462 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5463 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5465 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5466 __first2
, __last2
, __result
,
5467 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5470 template<typename _InputIterator1
, typename _InputIterator2
,
5471 typename _OutputIterator
,
5473 _GLIBCXX20_CONSTEXPR
5475 __set_symmetric_difference(_InputIterator1 __first1
,
5476 _InputIterator1 __last1
,
5477 _InputIterator2 __first2
,
5478 _InputIterator2 __last2
,
5479 _OutputIterator __result
,
5482 while (__first1
!= __last1
&& __first2
!= __last2
)
5483 if (__comp(__first1
, __first2
))
5485 *__result
= *__first1
;
5489 else if (__comp(__first2
, __first1
))
5491 *__result
= *__first2
;
5500 return std::copy(__first2
, __last2
,
5501 std::copy(__first1
, __last1
, __result
));
5505 * @brief Return the symmetric difference of two sorted ranges.
5506 * @ingroup set_algorithms
5507 * @param __first1 Start of first range.
5508 * @param __last1 End of first range.
5509 * @param __first2 Start of second range.
5510 * @param __last2 End of second range.
5511 * @param __result Start of output range.
5512 * @return End of the output range.
5513 * @ingroup set_algorithms
5515 * This operation iterates over both ranges, copying elements present in
5516 * one range but not the other in order to the output range. Iterators
5517 * increment for each range. When the current element of one range is less
5518 * than the other, that element is copied and the iterator advances. If an
5519 * element is contained in both ranges, no elements are copied and both
5520 * ranges advance. The output range may not overlap either input range.
5522 template<typename _InputIterator1
, typename _InputIterator2
,
5523 typename _OutputIterator
>
5524 _GLIBCXX20_CONSTEXPR
5525 inline _OutputIterator
5526 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5527 _InputIterator2 __first2
, _InputIterator2 __last2
,
5528 _OutputIterator __result
)
5530 // concept requirements
5531 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5532 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5533 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5534 typename iterator_traits
<_InputIterator1
>::value_type
>)
5535 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5536 typename iterator_traits
<_InputIterator2
>::value_type
>)
5537 __glibcxx_function_requires(_LessThanOpConcept
<
5538 typename iterator_traits
<_InputIterator1
>::value_type
,
5539 typename iterator_traits
<_InputIterator2
>::value_type
>)
5540 __glibcxx_function_requires(_LessThanOpConcept
<
5541 typename iterator_traits
<_InputIterator2
>::value_type
,
5542 typename iterator_traits
<_InputIterator1
>::value_type
>)
5543 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5544 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5545 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5546 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5548 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5549 __first2
, __last2
, __result
,
5550 __gnu_cxx::__ops::__iter_less_iter());
5554 * @brief Return the symmetric difference of two sorted ranges using
5555 * comparison functor.
5556 * @ingroup set_algorithms
5557 * @param __first1 Start of first range.
5558 * @param __last1 End of first range.
5559 * @param __first2 Start of second range.
5560 * @param __last2 End of second range.
5561 * @param __result Start of output range.
5562 * @param __comp The comparison functor.
5563 * @return End of the output range.
5564 * @ingroup set_algorithms
5566 * This operation iterates over both ranges, copying elements present in
5567 * one range but not the other in order to the output range. Iterators
5568 * increment for each range. When the current element of one range is less
5569 * than the other according to @p comp, that element is copied and the
5570 * iterator advances. If an element is contained in both ranges according
5571 * to @p __comp, no elements are copied and both ranges advance. The output
5572 * range may not overlap either input range.
5574 template<typename _InputIterator1
, typename _InputIterator2
,
5575 typename _OutputIterator
, typename _Compare
>
5576 _GLIBCXX20_CONSTEXPR
5577 inline _OutputIterator
5578 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5579 _InputIterator2 __first2
, _InputIterator2 __last2
,
5580 _OutputIterator __result
,
5583 // concept requirements
5584 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5585 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5586 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5587 typename iterator_traits
<_InputIterator1
>::value_type
>)
5588 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5589 typename iterator_traits
<_InputIterator2
>::value_type
>)
5590 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5591 typename iterator_traits
<_InputIterator1
>::value_type
,
5592 typename iterator_traits
<_InputIterator2
>::value_type
>)
5593 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5594 typename iterator_traits
<_InputIterator2
>::value_type
,
5595 typename iterator_traits
<_InputIterator1
>::value_type
>)
5596 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5597 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5598 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5599 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5601 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5602 __first2
, __last2
, __result
,
5603 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5606 template<typename _ForwardIterator
, typename _Compare
>
5607 _GLIBCXX14_CONSTEXPR
5609 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5612 if (__first
== __last
)
5614 _ForwardIterator __result
= __first
;
5615 while (++__first
!= __last
)
5616 if (__comp(__first
, __result
))
5622 * @brief Return the minimum element in a range.
5623 * @ingroup sorting_algorithms
5624 * @param __first Start of range.
5625 * @param __last End of range.
5626 * @return Iterator referencing the first instance of the smallest value.
5628 template<typename _ForwardIterator
>
5629 _GLIBCXX14_CONSTEXPR
5631 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5633 // concept requirements
5634 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5635 __glibcxx_function_requires(_LessThanComparableConcept
<
5636 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5637 __glibcxx_requires_valid_range(__first
, __last
);
5638 __glibcxx_requires_irreflexive(__first
, __last
);
5640 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5641 __gnu_cxx::__ops::__iter_less_iter());
5645 * @brief Return the minimum element in a range using comparison functor.
5646 * @ingroup sorting_algorithms
5647 * @param __first Start of range.
5648 * @param __last End of range.
5649 * @param __comp Comparison functor.
5650 * @return Iterator referencing the first instance of the smallest value
5651 * according to __comp.
5653 template<typename _ForwardIterator
, typename _Compare
>
5654 _GLIBCXX14_CONSTEXPR
5655 inline _ForwardIterator
5656 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5659 // concept requirements
5660 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5661 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5662 typename iterator_traits
<_ForwardIterator
>::value_type
,
5663 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5664 __glibcxx_requires_valid_range(__first
, __last
);
5665 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5667 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5668 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5671 template<typename _ForwardIterator
, typename _Compare
>
5672 _GLIBCXX14_CONSTEXPR
5674 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5677 if (__first
== __last
) return __first
;
5678 _ForwardIterator __result
= __first
;
5679 while (++__first
!= __last
)
5680 if (__comp(__result
, __first
))
5686 * @brief Return the maximum element in a range.
5687 * @ingroup sorting_algorithms
5688 * @param __first Start of range.
5689 * @param __last End of range.
5690 * @return Iterator referencing the first instance of the largest value.
5692 template<typename _ForwardIterator
>
5693 _GLIBCXX14_CONSTEXPR
5694 inline _ForwardIterator
5695 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5697 // concept requirements
5698 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5699 __glibcxx_function_requires(_LessThanComparableConcept
<
5700 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5701 __glibcxx_requires_valid_range(__first
, __last
);
5702 __glibcxx_requires_irreflexive(__first
, __last
);
5704 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5705 __gnu_cxx::__ops::__iter_less_iter());
5709 * @brief Return the maximum element in a range using comparison functor.
5710 * @ingroup sorting_algorithms
5711 * @param __first Start of range.
5712 * @param __last End of range.
5713 * @param __comp Comparison functor.
5714 * @return Iterator referencing the first instance of the largest value
5715 * according to __comp.
5717 template<typename _ForwardIterator
, typename _Compare
>
5718 _GLIBCXX14_CONSTEXPR
5719 inline _ForwardIterator
5720 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5723 // concept requirements
5724 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5725 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5726 typename iterator_traits
<_ForwardIterator
>::value_type
,
5727 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5728 __glibcxx_requires_valid_range(__first
, __last
);
5729 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5731 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5732 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5735 #if __cplusplus >= 201402L
5736 /// Reservoir sampling algorithm.
5737 template<typename _InputIterator
, typename _RandomAccessIterator
,
5738 typename _Size
, typename _UniformRandomBitGenerator
>
5739 _RandomAccessIterator
5740 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5741 _RandomAccessIterator __out
, random_access_iterator_tag
,
5742 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5744 using __distrib_type
= uniform_int_distribution
<_Size
>;
5745 using __param_type
= typename
__distrib_type::param_type
;
5746 __distrib_type __d
{};
5747 _Size __sample_sz
= 0;
5748 while (__first
!= __last
&& __sample_sz
!= __n
)
5750 __out
[__sample_sz
++] = *__first
;
5753 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5754 ++__first
, (void) ++__pop_sz
)
5756 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5758 __out
[__k
] = *__first
;
5760 return __out
+ __sample_sz
;
5763 /// Selection sampling algorithm.
5764 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5765 typename _Size
, typename _UniformRandomBitGenerator
>
5767 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5768 forward_iterator_tag
,
5769 _OutputIterator __out
, _Cat
,
5770 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5772 using __distrib_type
= uniform_int_distribution
<_Size
>;
5773 using __param_type
= typename
__distrib_type::param_type
;
5774 using _USize
= make_unsigned_t
<_Size
>;
5775 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5776 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5778 if (__first
== __last
)
5781 __distrib_type __d
{};
5782 _Size __unsampled_sz
= std::distance(__first
, __last
);
5783 __n
= std::min(__n
, __unsampled_sz
);
5785 // If possible, we use __gen_two_uniform_ints to efficiently produce
5786 // two random numbers using a single distribution invocation:
5788 const __uc_type __urngrange
= __g
.max() - __g
.min();
5789 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5790 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5793 while (__n
!= 0 && __unsampled_sz
>= 2)
5795 const pair
<_Size
, _Size
> __p
=
5796 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5799 if (__p
.first
< __n
)
5801 *__out
++ = *__first
;
5807 if (__n
== 0) break;
5810 if (__p
.second
< __n
)
5812 *__out
++ = *__first
;
5820 // The loop above is otherwise equivalent to this one-at-a-time version:
5822 for (; __n
!= 0; ++__first
)
5823 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5825 *__out
++ = *__first
;
5831 #if __cplusplus > 201402L
5832 #define __cpp_lib_sample 201603
5833 /// Take a random sample from a population.
5834 template<typename _PopulationIterator
, typename _SampleIterator
,
5835 typename _Distance
, typename _UniformRandomBitGenerator
>
5837 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5838 _SampleIterator __out
, _Distance __n
,
5839 _UniformRandomBitGenerator
&& __g
)
5841 using __pop_cat
= typename
5842 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5843 using __samp_cat
= typename
5844 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5847 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5848 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5849 "output range must use a RandomAccessIterator when input range"
5850 " does not meet the ForwardIterator requirements");
5852 static_assert(is_integral
<_Distance
>::value
,
5853 "sample size must be an integer type");
5855 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5856 return _GLIBCXX_STD_A::
5857 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5858 std::forward
<_UniformRandomBitGenerator
>(__g
));
5863 _GLIBCXX_END_NAMESPACE_ALGO
5864 _GLIBCXX_END_NAMESPACE_VERSION
5867 #endif /* _STL_ALGO_H */