1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2019 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 /// This is an overload used by find algos for the Input Iterator case.
100 template<typename _InputIterator
, typename _Predicate
>
102 inline _InputIterator
103 __find_if(_InputIterator __first
, _InputIterator __last
,
104 _Predicate __pred
, input_iterator_tag
)
106 while (__first
!= __last
&& !__pred(__first
))
111 /// This is an overload used by find algos for the RAI case.
112 template<typename _RandomAccessIterator
, typename _Predicate
>
114 _RandomAccessIterator
115 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
116 _Predicate __pred
, random_access_iterator_tag
)
118 typename iterator_traits
<_RandomAccessIterator
>::difference_type
119 __trip_count
= (__last
- __first
) >> 2;
121 for (; __trip_count
> 0; --__trip_count
)
140 switch (__last
- __first
)
160 template<typename _Iterator
, typename _Predicate
>
163 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
165 return __find_if(__first
, __last
, __pred
,
166 std::__iterator_category(__first
));
169 /// Provided for stable_partition to use.
170 template<typename _InputIterator
, typename _Predicate
>
172 inline _InputIterator
173 __find_if_not(_InputIterator __first
, _InputIterator __last
,
176 return std::__find_if(__first
, __last
,
177 __gnu_cxx::__ops::__negate(__pred
),
178 std::__iterator_category(__first
));
181 /// Like find_if_not(), but uses and updates a count of the
182 /// remaining range length instead of comparing against an end
184 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
187 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
189 for (; __len
; --__len
, (void) ++__first
)
190 if (!__pred(__first
))
197 // set_symmetric_difference
208 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
209 typename _BinaryPredicate
>
212 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
213 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
214 _BinaryPredicate __predicate
)
216 // Test for empty ranges
217 if (__first1
== __last1
|| __first2
== __last2
)
220 // Test for a pattern of length 1.
221 _ForwardIterator2
__p1(__first2
);
222 if (++__p1
== __last2
)
223 return std::__find_if(__first1
, __last1
,
224 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
227 _ForwardIterator1 __current
= __first1
;
232 std::__find_if(__first1
, __last1
,
233 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
235 if (__first1
== __last1
)
238 _ForwardIterator2 __p
= __p1
;
239 __current
= __first1
;
240 if (++__current
== __last1
)
243 while (__predicate(__current
, __p
))
245 if (++__p
== __last2
)
247 if (++__current
== __last1
)
258 * This is an helper function for search_n overloaded for forward iterators.
260 template<typename _ForwardIterator
, typename _Integer
,
261 typename _UnaryPredicate
>
264 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
265 _Integer __count
, _UnaryPredicate __unary_pred
,
266 std::forward_iterator_tag
)
268 __first
= std::__find_if(__first
, __last
, __unary_pred
);
269 while (__first
!= __last
)
271 typename iterator_traits
<_ForwardIterator
>::difference_type
273 _ForwardIterator __i
= __first
;
275 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
284 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
290 * This is an helper function for search_n overloaded for random access
293 template<typename _RandomAccessIter
, typename _Integer
,
294 typename _UnaryPredicate
>
297 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
298 _Integer __count
, _UnaryPredicate __unary_pred
,
299 std::random_access_iterator_tag
)
301 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
304 _DistanceType __tailSize
= __last
- __first
;
305 _DistanceType __remainder
= __count
;
307 while (__remainder
<= __tailSize
) // the main loop...
309 __first
+= __remainder
;
310 __tailSize
-= __remainder
;
311 // __first here is always pointing to one past the last element of
312 // next possible match.
313 _RandomAccessIter __backTrack
= __first
;
314 while (__unary_pred(--__backTrack
))
316 if (--__remainder
== 0)
317 return (__first
- __count
); // Success
319 __remainder
= __count
+ 1 - (__first
- __backTrack
);
321 return __last
; // Failure
324 template<typename _ForwardIterator
, typename _Integer
,
325 typename _UnaryPredicate
>
328 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
330 _UnaryPredicate __unary_pred
)
336 return std::__find_if(__first
, __last
, __unary_pred
);
338 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
339 std::__iterator_category(__first
));
342 // find_end for forward iterators.
343 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
344 typename _BinaryPredicate
>
347 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
348 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
349 forward_iterator_tag
, forward_iterator_tag
,
350 _BinaryPredicate __comp
)
352 if (__first2
== __last2
)
355 _ForwardIterator1 __result
= __last1
;
358 _ForwardIterator1 __new_result
359 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
360 if (__new_result
== __last1
)
364 __result
= __new_result
;
365 __first1
= __new_result
;
371 // find_end for bidirectional iterators (much faster).
372 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
373 typename _BinaryPredicate
>
375 _BidirectionalIterator1
376 __find_end(_BidirectionalIterator1 __first1
,
377 _BidirectionalIterator1 __last1
,
378 _BidirectionalIterator2 __first2
,
379 _BidirectionalIterator2 __last2
,
380 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
381 _BinaryPredicate __comp
)
383 // concept requirements
384 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
385 _BidirectionalIterator1
>)
386 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
387 _BidirectionalIterator2
>)
389 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
390 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
392 _RevIterator1
__rlast1(__first1
);
393 _RevIterator2
__rlast2(__first2
);
394 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
395 _RevIterator2(__last2
), __rlast2
,
398 if (__rresult
== __rlast1
)
402 _BidirectionalIterator1 __result
= __rresult
.base();
403 std::advance(__result
, -std::distance(__first2
, __last2
));
409 * @brief Find last matching subsequence in a sequence.
410 * @ingroup non_mutating_algorithms
411 * @param __first1 Start of range to search.
412 * @param __last1 End of range to search.
413 * @param __first2 Start of sequence to match.
414 * @param __last2 End of sequence to match.
415 * @return The last iterator @c i in the range
416 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
417 * @p *(__first2+N) for each @c N in the range @p
418 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
420 * Searches the range @p [__first1,__last1) for a sub-sequence that
421 * compares equal value-by-value with the sequence given by @p
422 * [__first2,__last2) and returns an iterator to the __first
423 * element of the sub-sequence, or @p __last1 if the sub-sequence
424 * is not found. The sub-sequence will be the last such
425 * subsequence contained in [__first1,__last1).
427 * Because the sub-sequence must lie completely within the range @p
428 * [__first1,__last1) it must start at a position less than @p
429 * __last1-(__last2-__first2) where @p __last2-__first2 is the
430 * length of the sub-sequence. This means that the returned
431 * iterator @c i will be in the range @p
432 * [__first1,__last1-(__last2-__first2))
434 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
436 inline _ForwardIterator1
437 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
438 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
440 // concept requirements
441 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
442 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
443 __glibcxx_function_requires(_EqualOpConcept
<
444 typename iterator_traits
<_ForwardIterator1
>::value_type
,
445 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
446 __glibcxx_requires_valid_range(__first1
, __last1
);
447 __glibcxx_requires_valid_range(__first2
, __last2
);
449 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
450 std::__iterator_category(__first1
),
451 std::__iterator_category(__first2
),
452 __gnu_cxx::__ops::__iter_equal_to_iter());
456 * @brief Find last matching subsequence in a sequence using a predicate.
457 * @ingroup non_mutating_algorithms
458 * @param __first1 Start of range to search.
459 * @param __last1 End of range to search.
460 * @param __first2 Start of sequence to match.
461 * @param __last2 End of sequence to match.
462 * @param __comp The predicate to use.
463 * @return The last iterator @c i in the range @p
464 * [__first1,__last1-(__last2-__first2)) such that @c
465 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
466 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
469 * Searches the range @p [__first1,__last1) for a sub-sequence that
470 * compares equal value-by-value with the sequence given by @p
471 * [__first2,__last2) using comp as a predicate and returns an
472 * iterator to the first element of the sub-sequence, or @p __last1
473 * if the sub-sequence is not found. The sub-sequence will be the
474 * last such subsequence contained in [__first,__last1).
476 * Because the sub-sequence must lie completely within the range @p
477 * [__first1,__last1) it must start at a position less than @p
478 * __last1-(__last2-__first2) where @p __last2-__first2 is the
479 * length of the sub-sequence. This means that the returned
480 * iterator @c i will be in the range @p
481 * [__first1,__last1-(__last2-__first2))
483 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
484 typename _BinaryPredicate
>
486 inline _ForwardIterator1
487 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
488 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
489 _BinaryPredicate __comp
)
491 // concept requirements
492 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
493 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
494 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
495 typename iterator_traits
<_ForwardIterator1
>::value_type
,
496 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
497 __glibcxx_requires_valid_range(__first1
, __last1
);
498 __glibcxx_requires_valid_range(__first2
, __last2
);
500 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
501 std::__iterator_category(__first1
),
502 std::__iterator_category(__first2
),
503 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
506 #if __cplusplus >= 201103L
508 * @brief Checks that a predicate is true for all the elements
510 * @ingroup non_mutating_algorithms
511 * @param __first An input iterator.
512 * @param __last An input iterator.
513 * @param __pred A predicate.
514 * @return True if the check is true, false otherwise.
516 * Returns true if @p __pred is true for each element in the range
517 * @p [__first,__last), and false otherwise.
519 template<typename _InputIterator
, typename _Predicate
>
522 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
523 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
526 * @brief Checks that a predicate is false for all the elements
528 * @ingroup non_mutating_algorithms
529 * @param __first An input iterator.
530 * @param __last An input iterator.
531 * @param __pred A predicate.
532 * @return True if the check is true, false otherwise.
534 * Returns true if @p __pred is false for each element in the range
535 * @p [__first,__last), and false otherwise.
537 template<typename _InputIterator
, typename _Predicate
>
540 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
541 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
544 * @brief Checks that a predicate is false for at least an element
546 * @ingroup non_mutating_algorithms
547 * @param __first An input iterator.
548 * @param __last An input iterator.
549 * @param __pred A predicate.
550 * @return True if the check is true, false otherwise.
552 * Returns true if an element exists in the range @p
553 * [__first,__last) such that @p __pred is true, and false
556 template<typename _InputIterator
, typename _Predicate
>
559 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
560 { return !std::none_of(__first
, __last
, __pred
); }
563 * @brief Find the first element in a sequence for which a
564 * predicate is false.
565 * @ingroup non_mutating_algorithms
566 * @param __first An input iterator.
567 * @param __last An input iterator.
568 * @param __pred A predicate.
569 * @return The first iterator @c i in the range @p [__first,__last)
570 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
572 template<typename _InputIterator
, typename _Predicate
>
574 inline _InputIterator
575 find_if_not(_InputIterator __first
, _InputIterator __last
,
578 // concept requirements
579 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
580 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
581 typename iterator_traits
<_InputIterator
>::value_type
>)
582 __glibcxx_requires_valid_range(__first
, __last
);
583 return std::__find_if_not(__first
, __last
,
584 __gnu_cxx::__ops::__pred_iter(__pred
));
588 * @brief Checks whether the sequence is partitioned.
589 * @ingroup mutating_algorithms
590 * @param __first An input iterator.
591 * @param __last An input iterator.
592 * @param __pred A predicate.
593 * @return True if the range @p [__first,__last) is partioned by @p __pred,
594 * i.e. if all elements that satisfy @p __pred appear before those that
597 template<typename _InputIterator
, typename _Predicate
>
600 is_partitioned(_InputIterator __first
, _InputIterator __last
,
603 __first
= std::find_if_not(__first
, __last
, __pred
);
604 if (__first
== __last
)
607 return std::none_of(__first
, __last
, __pred
);
611 * @brief Find the partition point of a partitioned range.
612 * @ingroup mutating_algorithms
613 * @param __first An iterator.
614 * @param __last Another iterator.
615 * @param __pred A predicate.
616 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
617 * and @p none_of(mid, __last, __pred) are both true.
619 template<typename _ForwardIterator
, typename _Predicate
>
622 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
625 // concept requirements
626 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
627 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
628 typename iterator_traits
<_ForwardIterator
>::value_type
>)
630 // A specific debug-mode test will be necessary...
631 __glibcxx_requires_valid_range(__first
, __last
);
633 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
636 _DistanceType __len
= std::distance(__first
, __last
);
640 _DistanceType __half
= __len
>> 1;
641 _ForwardIterator __middle
= __first
;
642 std::advance(__middle
, __half
);
643 if (__pred(*__middle
))
647 __len
= __len
- __half
- 1;
656 template<typename _InputIterator
, typename _OutputIterator
,
660 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
661 _OutputIterator __result
, _Predicate __pred
)
663 for (; __first
!= __last
; ++__first
)
664 if (!__pred(__first
))
666 *__result
= *__first
;
673 * @brief Copy a sequence, removing elements of a given value.
674 * @ingroup mutating_algorithms
675 * @param __first An input iterator.
676 * @param __last An input iterator.
677 * @param __result An output iterator.
678 * @param __value The value to be removed.
679 * @return An iterator designating the end of the resulting sequence.
681 * Copies each element in the range @p [__first,__last) not equal
682 * to @p __value to the range beginning at @p __result.
683 * remove_copy() is stable, so the relative order of elements that
684 * are copied is unchanged.
686 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
688 inline _OutputIterator
689 remove_copy(_InputIterator __first
, _InputIterator __last
,
690 _OutputIterator __result
, const _Tp
& __value
)
692 // concept requirements
693 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
694 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
695 typename iterator_traits
<_InputIterator
>::value_type
>)
696 __glibcxx_function_requires(_EqualOpConcept
<
697 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
698 __glibcxx_requires_valid_range(__first
, __last
);
700 return std::__remove_copy_if(__first
, __last
, __result
,
701 __gnu_cxx::__ops::__iter_equals_val(__value
));
705 * @brief Copy a sequence, removing elements for which a predicate is true.
706 * @ingroup mutating_algorithms
707 * @param __first An input iterator.
708 * @param __last An input iterator.
709 * @param __result An output iterator.
710 * @param __pred A predicate.
711 * @return An iterator designating the end of the resulting sequence.
713 * Copies each element in the range @p [__first,__last) for which
714 * @p __pred returns false to the range beginning at @p __result.
716 * remove_copy_if() is stable, so the relative order of elements that are
717 * copied is unchanged.
719 template<typename _InputIterator
, typename _OutputIterator
,
722 inline _OutputIterator
723 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
724 _OutputIterator __result
, _Predicate __pred
)
726 // concept requirements
727 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
728 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
729 typename iterator_traits
<_InputIterator
>::value_type
>)
730 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
731 typename iterator_traits
<_InputIterator
>::value_type
>)
732 __glibcxx_requires_valid_range(__first
, __last
);
734 return std::__remove_copy_if(__first
, __last
, __result
,
735 __gnu_cxx::__ops::__pred_iter(__pred
));
738 #if __cplusplus >= 201103L
740 * @brief Copy the elements of a sequence for which a predicate is true.
741 * @ingroup mutating_algorithms
742 * @param __first An input iterator.
743 * @param __last An input iterator.
744 * @param __result An output iterator.
745 * @param __pred A predicate.
746 * @return An iterator designating the end of the resulting sequence.
748 * Copies each element in the range @p [__first,__last) for which
749 * @p __pred returns true to the range beginning at @p __result.
751 * copy_if() is stable, so the relative order of elements that are
752 * copied is unchanged.
754 template<typename _InputIterator
, typename _OutputIterator
,
758 copy_if(_InputIterator __first
, _InputIterator __last
,
759 _OutputIterator __result
, _Predicate __pred
)
761 // concept requirements
762 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
763 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
764 typename iterator_traits
<_InputIterator
>::value_type
>)
765 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
766 typename iterator_traits
<_InputIterator
>::value_type
>)
767 __glibcxx_requires_valid_range(__first
, __last
);
769 for (; __first
!= __last
; ++__first
)
770 if (__pred(*__first
))
772 *__result
= *__first
;
778 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
781 __copy_n(_InputIterator __first
, _Size __n
,
782 _OutputIterator __result
, input_iterator_tag
)
788 *__result
= *__first
;
799 template<typename _RandomAccessIterator
, typename _Size
,
800 typename _OutputIterator
>
802 inline _OutputIterator
803 __copy_n(_RandomAccessIterator __first
, _Size __n
,
804 _OutputIterator __result
, random_access_iterator_tag
)
805 { return std::copy(__first
, __first
+ __n
, __result
); }
808 * @brief Copies the range [first,first+n) into [result,result+n).
809 * @ingroup mutating_algorithms
810 * @param __first An input iterator.
811 * @param __n The number of elements to copy.
812 * @param __result An output iterator.
815 * This inline function will boil down to a call to @c memmove whenever
816 * possible. Failing that, if random access iterators are passed, then the
817 * loop count will be known (and therefore a candidate for compiler
818 * optimizations such as unrolling).
820 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
822 inline _OutputIterator
823 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
825 // concept requirements
826 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
827 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
828 typename iterator_traits
<_InputIterator
>::value_type
>)
830 return std::__copy_n(__first
, __n
, __result
,
831 std::__iterator_category(__first
));
835 * @brief Copy the elements of a sequence to separate output sequences
836 * depending on the truth value of a predicate.
837 * @ingroup mutating_algorithms
838 * @param __first An input iterator.
839 * @param __last An input iterator.
840 * @param __out_true An output iterator.
841 * @param __out_false An output iterator.
842 * @param __pred A predicate.
843 * @return A pair designating the ends of the resulting sequences.
845 * Copies each element in the range @p [__first,__last) for which
846 * @p __pred returns true to the range beginning at @p out_true
847 * and each element for which @p __pred returns false to @p __out_false.
849 template<typename _InputIterator
, typename _OutputIterator1
,
850 typename _OutputIterator2
, typename _Predicate
>
852 pair
<_OutputIterator1
, _OutputIterator2
>
853 partition_copy(_InputIterator __first
, _InputIterator __last
,
854 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
857 // concept requirements
858 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
859 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
860 typename iterator_traits
<_InputIterator
>::value_type
>)
861 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
862 typename iterator_traits
<_InputIterator
>::value_type
>)
863 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
864 typename iterator_traits
<_InputIterator
>::value_type
>)
865 __glibcxx_requires_valid_range(__first
, __last
);
867 for (; __first
!= __last
; ++__first
)
868 if (__pred(*__first
))
870 *__out_true
= *__first
;
875 *__out_false
= *__first
;
879 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
883 template<typename _ForwardIterator
, typename _Predicate
>
886 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
889 __first
= std::__find_if(__first
, __last
, __pred
);
890 if (__first
== __last
)
892 _ForwardIterator __result
= __first
;
894 for (; __first
!= __last
; ++__first
)
895 if (!__pred(__first
))
897 *__result
= _GLIBCXX_MOVE(*__first
);
904 * @brief Remove elements from a sequence.
905 * @ingroup mutating_algorithms
906 * @param __first An input iterator.
907 * @param __last An input iterator.
908 * @param __value The value to be removed.
909 * @return An iterator designating the end of the resulting sequence.
911 * All elements equal to @p __value are removed from the range
912 * @p [__first,__last).
914 * remove() is stable, so the relative order of elements that are
915 * not removed is unchanged.
917 * Elements between the end of the resulting sequence and @p __last
918 * are still present, but their value is unspecified.
920 template<typename _ForwardIterator
, typename _Tp
>
922 inline _ForwardIterator
923 remove(_ForwardIterator __first
, _ForwardIterator __last
,
926 // concept requirements
927 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
929 __glibcxx_function_requires(_EqualOpConcept
<
930 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
931 __glibcxx_requires_valid_range(__first
, __last
);
933 return std::__remove_if(__first
, __last
,
934 __gnu_cxx::__ops::__iter_equals_val(__value
));
938 * @brief Remove elements from a sequence using a predicate.
939 * @ingroup mutating_algorithms
940 * @param __first A forward iterator.
941 * @param __last A forward iterator.
942 * @param __pred A predicate.
943 * @return An iterator designating the end of the resulting sequence.
945 * All elements for which @p __pred returns true are removed from the range
946 * @p [__first,__last).
948 * remove_if() is stable, so the relative order of elements that are
949 * not removed is unchanged.
951 * Elements between the end of the resulting sequence and @p __last
952 * are still present, but their value is unspecified.
954 template<typename _ForwardIterator
, typename _Predicate
>
956 inline _ForwardIterator
957 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
960 // concept requirements
961 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
963 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
964 typename iterator_traits
<_ForwardIterator
>::value_type
>)
965 __glibcxx_requires_valid_range(__first
, __last
);
967 return std::__remove_if(__first
, __last
,
968 __gnu_cxx::__ops::__pred_iter(__pred
));
971 template<typename _ForwardIterator
, typename _BinaryPredicate
>
974 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
975 _BinaryPredicate __binary_pred
)
977 if (__first
== __last
)
979 _ForwardIterator __next
= __first
;
980 while (++__next
!= __last
)
982 if (__binary_pred(__first
, __next
))
989 template<typename _ForwardIterator
, typename _BinaryPredicate
>
992 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
993 _BinaryPredicate __binary_pred
)
995 // Skip the beginning, if already unique.
996 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
997 if (__first
== __last
)
1000 // Do the real copy work.
1001 _ForwardIterator __dest
= __first
;
1003 while (++__first
!= __last
)
1004 if (!__binary_pred(__dest
, __first
))
1005 *++__dest
= _GLIBCXX_MOVE(*__first
);
1010 * @brief Remove consecutive duplicate values from a sequence.
1011 * @ingroup mutating_algorithms
1012 * @param __first A forward iterator.
1013 * @param __last A forward iterator.
1014 * @return An iterator designating the end of the resulting sequence.
1016 * Removes all but the first element from each group of consecutive
1017 * values that compare equal.
1018 * unique() is stable, so the relative order of elements that are
1019 * not removed is unchanged.
1020 * Elements between the end of the resulting sequence and @p __last
1021 * are still present, but their value is unspecified.
1023 template<typename _ForwardIterator
>
1024 _GLIBCXX20_CONSTEXPR
1025 inline _ForwardIterator
1026 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1028 // concept requirements
1029 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1031 __glibcxx_function_requires(_EqualityComparableConcept
<
1032 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1033 __glibcxx_requires_valid_range(__first
, __last
);
1035 return std::__unique(__first
, __last
,
1036 __gnu_cxx::__ops::__iter_equal_to_iter());
1040 * @brief Remove consecutive values from a sequence using a predicate.
1041 * @ingroup mutating_algorithms
1042 * @param __first A forward iterator.
1043 * @param __last A forward iterator.
1044 * @param __binary_pred A binary predicate.
1045 * @return An iterator designating the end of the resulting sequence.
1047 * Removes all but the first element from each group of consecutive
1048 * values for which @p __binary_pred returns true.
1049 * unique() is stable, so the relative order of elements that are
1050 * not removed is unchanged.
1051 * Elements between the end of the resulting sequence and @p __last
1052 * are still present, but their value is unspecified.
1054 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1055 _GLIBCXX20_CONSTEXPR
1056 inline _ForwardIterator
1057 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1058 _BinaryPredicate __binary_pred
)
1060 // concept requirements
1061 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1063 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1064 typename iterator_traits
<_ForwardIterator
>::value_type
,
1065 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1066 __glibcxx_requires_valid_range(__first
, __last
);
1068 return std::__unique(__first
, __last
,
1069 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1073 * This is an uglified
1074 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1076 * overloaded for forward iterators and output iterator as result.
1078 template<typename _ForwardIterator
, typename _OutputIterator
,
1079 typename _BinaryPredicate
>
1080 _GLIBCXX20_CONSTEXPR
1082 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1083 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1084 forward_iterator_tag
, output_iterator_tag
)
1086 // concept requirements -- iterators already checked
1087 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1088 typename iterator_traits
<_ForwardIterator
>::value_type
,
1089 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1091 _ForwardIterator __next
= __first
;
1092 *__result
= *__first
;
1093 while (++__next
!= __last
)
1094 if (!__binary_pred(__first
, __next
))
1097 *++__result
= *__first
;
1103 * This is an uglified
1104 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1106 * overloaded for input iterators and output iterator as result.
1108 template<typename _InputIterator
, typename _OutputIterator
,
1109 typename _BinaryPredicate
>
1110 _GLIBCXX20_CONSTEXPR
1112 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1113 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1114 input_iterator_tag
, output_iterator_tag
)
1116 // concept requirements -- iterators already checked
1117 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1118 typename iterator_traits
<_InputIterator
>::value_type
,
1119 typename iterator_traits
<_InputIterator
>::value_type
>)
1121 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1122 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1124 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1125 *__result
= __value
;
1126 while (++__first
!= __last
)
1127 if (!__rebound_pred(__first
, __value
))
1130 *++__result
= __value
;
1136 * This is an uglified
1137 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1139 * overloaded for input iterators and forward iterator as result.
1141 template<typename _InputIterator
, typename _ForwardIterator
,
1142 typename _BinaryPredicate
>
1143 _GLIBCXX20_CONSTEXPR
1145 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1146 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1147 input_iterator_tag
, forward_iterator_tag
)
1149 // concept requirements -- iterators already checked
1150 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1151 typename iterator_traits
<_ForwardIterator
>::value_type
,
1152 typename iterator_traits
<_InputIterator
>::value_type
>)
1153 *__result
= *__first
;
1154 while (++__first
!= __last
)
1155 if (!__binary_pred(__result
, __first
))
1156 *++__result
= *__first
;
1161 * This is an uglified reverse(_BidirectionalIterator,
1162 * _BidirectionalIterator)
1163 * overloaded for bidirectional iterators.
1165 template<typename _BidirectionalIterator
>
1166 _GLIBCXX20_CONSTEXPR
1168 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1169 bidirectional_iterator_tag
)
1172 if (__first
== __last
|| __first
== --__last
)
1176 std::iter_swap(__first
, __last
);
1182 * This is an uglified reverse(_BidirectionalIterator,
1183 * _BidirectionalIterator)
1184 * overloaded for random access iterators.
1186 template<typename _RandomAccessIterator
>
1187 _GLIBCXX20_CONSTEXPR
1189 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1190 random_access_iterator_tag
)
1192 if (__first
== __last
)
1195 while (__first
< __last
)
1197 std::iter_swap(__first
, __last
);
1204 * @brief Reverse a sequence.
1205 * @ingroup mutating_algorithms
1206 * @param __first A bidirectional iterator.
1207 * @param __last A bidirectional iterator.
1208 * @return reverse() returns no value.
1210 * Reverses the order of the elements in the range @p [__first,__last),
1211 * so that the first element becomes the last etc.
1212 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1213 * swaps @p *(__first+i) and @p *(__last-(i+1))
1215 template<typename _BidirectionalIterator
>
1216 _GLIBCXX20_CONSTEXPR
1218 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1220 // concept requirements
1221 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1222 _BidirectionalIterator
>)
1223 __glibcxx_requires_valid_range(__first
, __last
);
1224 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1228 * @brief Copy a sequence, reversing its elements.
1229 * @ingroup mutating_algorithms
1230 * @param __first A bidirectional iterator.
1231 * @param __last A bidirectional iterator.
1232 * @param __result An output iterator.
1233 * @return An iterator designating the end of the resulting sequence.
1235 * Copies the elements in the range @p [__first,__last) to the
1236 * range @p [__result,__result+(__last-__first)) such that the
1237 * order of the elements is reversed. For every @c i such that @p
1238 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1239 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1240 * The ranges @p [__first,__last) and @p
1241 * [__result,__result+(__last-__first)) must not overlap.
1243 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1244 _GLIBCXX20_CONSTEXPR
1246 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1247 _OutputIterator __result
)
1249 // concept requirements
1250 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1251 _BidirectionalIterator
>)
1252 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1253 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1254 __glibcxx_requires_valid_range(__first
, __last
);
1256 while (__first
!= __last
)
1259 *__result
= *__last
;
1266 * This is a helper function for the rotate algorithm specialized on RAIs.
1267 * It returns the greatest common divisor of two integer values.
1269 template<typename _EuclideanRingElement
>
1270 _GLIBCXX20_CONSTEXPR
1271 _EuclideanRingElement
1272 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1276 _EuclideanRingElement __t
= __m
% __n
;
1283 inline namespace _V2
1286 /// This is a helper function for the rotate algorithm.
1287 template<typename _ForwardIterator
>
1288 _GLIBCXX20_CONSTEXPR
1290 __rotate(_ForwardIterator __first
,
1291 _ForwardIterator __middle
,
1292 _ForwardIterator __last
,
1293 forward_iterator_tag
)
1295 if (__first
== __middle
)
1297 else if (__last
== __middle
)
1300 _ForwardIterator __first2
= __middle
;
1303 std::iter_swap(__first
, __first2
);
1306 if (__first
== __middle
)
1307 __middle
= __first2
;
1309 while (__first2
!= __last
);
1311 _ForwardIterator __ret
= __first
;
1313 __first2
= __middle
;
1315 while (__first2
!= __last
)
1317 std::iter_swap(__first
, __first2
);
1320 if (__first
== __middle
)
1321 __middle
= __first2
;
1322 else if (__first2
== __last
)
1323 __first2
= __middle
;
1328 /// This is a helper function for the rotate algorithm.
1329 template<typename _BidirectionalIterator
>
1330 _GLIBCXX20_CONSTEXPR
1331 _BidirectionalIterator
1332 __rotate(_BidirectionalIterator __first
,
1333 _BidirectionalIterator __middle
,
1334 _BidirectionalIterator __last
,
1335 bidirectional_iterator_tag
)
1337 // concept requirements
1338 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1339 _BidirectionalIterator
>)
1341 if (__first
== __middle
)
1343 else if (__last
== __middle
)
1346 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1347 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1349 while (__first
!= __middle
&& __middle
!= __last
)
1351 std::iter_swap(__first
, --__last
);
1355 if (__first
== __middle
)
1357 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1362 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1367 /// This is a helper function for the rotate algorithm.
1368 template<typename _RandomAccessIterator
>
1369 _GLIBCXX20_CONSTEXPR
1370 _RandomAccessIterator
1371 __rotate(_RandomAccessIterator __first
,
1372 _RandomAccessIterator __middle
,
1373 _RandomAccessIterator __last
,
1374 random_access_iterator_tag
)
1376 // concept requirements
1377 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1378 _RandomAccessIterator
>)
1380 if (__first
== __middle
)
1382 else if (__last
== __middle
)
1385 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1387 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1390 _Distance __n
= __last
- __first
;
1391 _Distance __k
= __middle
- __first
;
1393 if (__k
== __n
- __k
)
1395 std::swap_ranges(__first
, __middle
, __middle
);
1399 _RandomAccessIterator __p
= __first
;
1400 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1404 if (__k
< __n
- __k
)
1406 if (__is_pod(_ValueType
) && __k
== 1)
1408 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1409 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1410 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1413 _RandomAccessIterator __q
= __p
+ __k
;
1414 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1416 std::iter_swap(__p
, __q
);
1423 std::swap(__n
, __k
);
1429 if (__is_pod(_ValueType
) && __k
== 1)
1431 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1432 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1433 *__p
= _GLIBCXX_MOVE(__t
);
1436 _RandomAccessIterator __q
= __p
+ __n
;
1438 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1442 std::iter_swap(__p
, __q
);
1447 std::swap(__n
, __k
);
1452 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1453 // DR 488. rotate throws away useful information
1455 * @brief Rotate the elements of a sequence.
1456 * @ingroup mutating_algorithms
1457 * @param __first A forward iterator.
1458 * @param __middle A forward iterator.
1459 * @param __last A forward iterator.
1460 * @return first + (last - middle).
1462 * Rotates the elements of the range @p [__first,__last) by
1463 * @p (__middle - __first) positions so that the element at @p __middle
1464 * is moved to @p __first, the element at @p __middle+1 is moved to
1465 * @p __first+1 and so on for each element in the range
1466 * @p [__first,__last).
1468 * This effectively swaps the ranges @p [__first,__middle) and
1469 * @p [__middle,__last).
1472 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1473 * for each @p n in the range @p [0,__last-__first).
1475 template<typename _ForwardIterator
>
1476 _GLIBCXX20_CONSTEXPR
1477 inline _ForwardIterator
1478 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1479 _ForwardIterator __last
)
1481 // concept requirements
1482 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1484 __glibcxx_requires_valid_range(__first
, __middle
);
1485 __glibcxx_requires_valid_range(__middle
, __last
);
1487 return std::__rotate(__first
, __middle
, __last
,
1488 std::__iterator_category(__first
));
1494 * @brief Copy a sequence, rotating its elements.
1495 * @ingroup mutating_algorithms
1496 * @param __first A forward iterator.
1497 * @param __middle A forward iterator.
1498 * @param __last A forward iterator.
1499 * @param __result An output iterator.
1500 * @return An iterator designating the end of the resulting sequence.
1502 * Copies the elements of the range @p [__first,__last) to the
1503 * range beginning at @result, rotating the copied elements by
1504 * @p (__middle-__first) positions so that the element at @p __middle
1505 * is moved to @p __result, the element at @p __middle+1 is moved
1506 * to @p __result+1 and so on for each element in the range @p
1510 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1511 * for each @p n in the range @p [0,__last-__first).
1513 template<typename _ForwardIterator
, typename _OutputIterator
>
1514 _GLIBCXX20_CONSTEXPR
1515 inline _OutputIterator
1516 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1517 _ForwardIterator __last
, _OutputIterator __result
)
1519 // concept requirements
1520 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1521 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1522 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1523 __glibcxx_requires_valid_range(__first
, __middle
);
1524 __glibcxx_requires_valid_range(__middle
, __last
);
1526 return std::copy(__first
, __middle
,
1527 std::copy(__middle
, __last
, __result
));
1530 /// This is a helper function...
1531 template<typename _ForwardIterator
, typename _Predicate
>
1532 _GLIBCXX20_CONSTEXPR
1534 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1535 _Predicate __pred
, forward_iterator_tag
)
1537 if (__first
== __last
)
1540 while (__pred(*__first
))
1541 if (++__first
== __last
)
1544 _ForwardIterator __next
= __first
;
1546 while (++__next
!= __last
)
1547 if (__pred(*__next
))
1549 std::iter_swap(__first
, __next
);
1556 /// This is a helper function...
1557 template<typename _BidirectionalIterator
, typename _Predicate
>
1558 _GLIBCXX20_CONSTEXPR
1559 _BidirectionalIterator
1560 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1561 _Predicate __pred
, bidirectional_iterator_tag
)
1566 if (__first
== __last
)
1568 else if (__pred(*__first
))
1574 if (__first
== __last
)
1576 else if (!bool(__pred(*__last
)))
1580 std::iter_swap(__first
, __last
);
1587 /// This is a helper function...
1588 /// Requires __first != __last and !__pred(__first)
1589 /// and __len == distance(__first, __last).
1591 /// !__pred(__first) allows us to guarantee that we don't
1592 /// move-assign an element onto itself.
1593 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1596 __stable_partition_adaptive(_ForwardIterator __first
,
1597 _ForwardIterator __last
,
1598 _Predicate __pred
, _Distance __len
,
1600 _Distance __buffer_size
)
1605 if (__len
<= __buffer_size
)
1607 _ForwardIterator __result1
= __first
;
1608 _Pointer __result2
= __buffer
;
1610 // The precondition guarantees that !__pred(__first), so
1611 // move that element to the buffer before starting the loop.
1612 // This ensures that we only call __pred once per element.
1613 *__result2
= _GLIBCXX_MOVE(*__first
);
1616 for (; __first
!= __last
; ++__first
)
1617 if (__pred(__first
))
1619 *__result1
= _GLIBCXX_MOVE(*__first
);
1624 *__result2
= _GLIBCXX_MOVE(*__first
);
1628 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1632 _ForwardIterator __middle
= __first
;
1633 std::advance(__middle
, __len
/ 2);
1634 _ForwardIterator __left_split
=
1635 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1636 __len
/ 2, __buffer
,
1639 // Advance past true-predicate values to satisfy this
1640 // function's preconditions.
1641 _Distance __right_len
= __len
- __len
/ 2;
1642 _ForwardIterator __right_split
=
1643 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1647 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1649 __buffer
, __buffer_size
);
1651 return std::rotate(__left_split
, __middle
, __right_split
);
1654 template<typename _ForwardIterator
, typename _Predicate
>
1656 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1659 __first
= std::__find_if_not(__first
, __last
, __pred
);
1661 if (__first
== __last
)
1664 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1666 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1669 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1670 __buf(__first
, std::distance(__first
, __last
));
1672 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1673 _DistanceType(__buf
.requested_size()),
1675 _DistanceType(__buf
.size()));
1679 * @brief Move elements for which a predicate is true to the beginning
1680 * of a sequence, preserving relative ordering.
1681 * @ingroup mutating_algorithms
1682 * @param __first A forward iterator.
1683 * @param __last A forward iterator.
1684 * @param __pred A predicate functor.
1685 * @return An iterator @p middle such that @p __pred(i) is true for each
1686 * iterator @p i in the range @p [first,middle) and false for each @p i
1687 * in the range @p [middle,last).
1689 * Performs the same function as @p partition() with the additional
1690 * guarantee that the relative ordering of elements in each group is
1691 * preserved, so any two elements @p x and @p y in the range
1692 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1693 * relative ordering after calling @p stable_partition().
1695 template<typename _ForwardIterator
, typename _Predicate
>
1696 inline _ForwardIterator
1697 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1700 // concept requirements
1701 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1703 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1704 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1705 __glibcxx_requires_valid_range(__first
, __last
);
1707 return std::__stable_partition(__first
, __last
,
1708 __gnu_cxx::__ops::__pred_iter(__pred
));
1711 /// This is a helper function for the sort routines.
1712 template<typename _RandomAccessIterator
, typename _Compare
>
1713 _GLIBCXX20_CONSTEXPR
1715 __heap_select(_RandomAccessIterator __first
,
1716 _RandomAccessIterator __middle
,
1717 _RandomAccessIterator __last
, _Compare __comp
)
1719 std::__make_heap(__first
, __middle
, __comp
);
1720 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1721 if (__comp(__i
, __first
))
1722 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1727 template<typename _InputIterator
, typename _RandomAccessIterator
,
1729 _GLIBCXX20_CONSTEXPR
1730 _RandomAccessIterator
1731 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1732 _RandomAccessIterator __result_first
,
1733 _RandomAccessIterator __result_last
,
1736 typedef typename iterator_traits
<_InputIterator
>::value_type
1738 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1739 typedef typename
_RItTraits::difference_type _DistanceType
;
1741 if (__result_first
== __result_last
)
1742 return __result_last
;
1743 _RandomAccessIterator __result_real_last
= __result_first
;
1744 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1746 *__result_real_last
= *__first
;
1747 ++__result_real_last
;
1751 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1752 while (__first
!= __last
)
1754 if (__comp(__first
, __result_first
))
1755 std::__adjust_heap(__result_first
, _DistanceType(0),
1756 _DistanceType(__result_real_last
1758 _InputValueType(*__first
), __comp
);
1761 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1762 return __result_real_last
;
1766 * @brief Copy the smallest elements of a sequence.
1767 * @ingroup sorting_algorithms
1768 * @param __first An iterator.
1769 * @param __last Another iterator.
1770 * @param __result_first A random-access iterator.
1771 * @param __result_last Another random-access iterator.
1772 * @return An iterator indicating the end of the resulting sequence.
1774 * Copies and sorts the smallest N values from the range @p [__first,__last)
1775 * to the range beginning at @p __result_first, where the number of
1776 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1777 * @p (__result_last-__result_first).
1778 * After the sort if @e i and @e j are iterators in the range
1779 * @p [__result_first,__result_first+N) such that i precedes j then
1781 * The value returned is @p __result_first+N.
1783 template<typename _InputIterator
, typename _RandomAccessIterator
>
1784 _GLIBCXX20_CONSTEXPR
1785 inline _RandomAccessIterator
1786 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1787 _RandomAccessIterator __result_first
,
1788 _RandomAccessIterator __result_last
)
1790 #ifdef _GLIBCXX_CONCEPT_CHECKS
1791 typedef typename iterator_traits
<_InputIterator
>::value_type
1793 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1797 // concept requirements
1798 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1799 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1801 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1803 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1804 __glibcxx_requires_valid_range(__first
, __last
);
1805 __glibcxx_requires_irreflexive(__first
, __last
);
1806 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1808 return std::__partial_sort_copy(__first
, __last
,
1809 __result_first
, __result_last
,
1810 __gnu_cxx::__ops::__iter_less_iter());
1814 * @brief Copy the smallest elements of a sequence using a predicate for
1816 * @ingroup sorting_algorithms
1817 * @param __first An input iterator.
1818 * @param __last Another input iterator.
1819 * @param __result_first A random-access iterator.
1820 * @param __result_last Another random-access iterator.
1821 * @param __comp A comparison functor.
1822 * @return An iterator indicating the end of the resulting sequence.
1824 * Copies and sorts the smallest N values from the range @p [__first,__last)
1825 * to the range beginning at @p result_first, where the number of
1826 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1827 * @p (__result_last-__result_first).
1828 * After the sort if @e i and @e j are iterators in the range
1829 * @p [__result_first,__result_first+N) such that i precedes j then
1830 * @p __comp(*j,*i) is false.
1831 * The value returned is @p __result_first+N.
1833 template<typename _InputIterator
, typename _RandomAccessIterator
,
1835 _GLIBCXX20_CONSTEXPR
1836 inline _RandomAccessIterator
1837 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1838 _RandomAccessIterator __result_first
,
1839 _RandomAccessIterator __result_last
,
1842 #ifdef _GLIBCXX_CONCEPT_CHECKS
1843 typedef typename iterator_traits
<_InputIterator
>::value_type
1845 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1849 // concept requirements
1850 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1851 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1852 _RandomAccessIterator
>)
1853 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1855 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1856 _InputValueType
, _OutputValueType
>)
1857 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1858 _OutputValueType
, _OutputValueType
>)
1859 __glibcxx_requires_valid_range(__first
, __last
);
1860 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1861 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1863 return std::__partial_sort_copy(__first
, __last
,
1864 __result_first
, __result_last
,
1865 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1868 /// This is a helper function for the sort routine.
1869 template<typename _RandomAccessIterator
, typename _Compare
>
1870 _GLIBCXX20_CONSTEXPR
1872 __unguarded_linear_insert(_RandomAccessIterator __last
,
1875 typename iterator_traits
<_RandomAccessIterator
>::value_type
1876 __val
= _GLIBCXX_MOVE(*__last
);
1877 _RandomAccessIterator __next
= __last
;
1879 while (__comp(__val
, __next
))
1881 *__last
= _GLIBCXX_MOVE(*__next
);
1885 *__last
= _GLIBCXX_MOVE(__val
);
1888 /// This is a helper function for the sort routine.
1889 template<typename _RandomAccessIterator
, typename _Compare
>
1890 _GLIBCXX20_CONSTEXPR
1892 __insertion_sort(_RandomAccessIterator __first
,
1893 _RandomAccessIterator __last
, _Compare __comp
)
1895 if (__first
== __last
) return;
1897 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1899 if (__comp(__i
, __first
))
1901 typename iterator_traits
<_RandomAccessIterator
>::value_type
1902 __val
= _GLIBCXX_MOVE(*__i
);
1903 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1904 *__first
= _GLIBCXX_MOVE(__val
);
1907 std::__unguarded_linear_insert(__i
,
1908 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1912 /// This is a helper function for the sort routine.
1913 template<typename _RandomAccessIterator
, typename _Compare
>
1914 _GLIBCXX20_CONSTEXPR
1916 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1917 _RandomAccessIterator __last
, _Compare __comp
)
1919 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1920 std::__unguarded_linear_insert(__i
,
1921 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1926 * This controls some aspect of the sort routines.
1928 enum { _S_threshold
= 16 };
1930 /// This is a helper function for the sort routine.
1931 template<typename _RandomAccessIterator
, typename _Compare
>
1932 _GLIBCXX20_CONSTEXPR
1934 __final_insertion_sort(_RandomAccessIterator __first
,
1935 _RandomAccessIterator __last
, _Compare __comp
)
1937 if (__last
- __first
> int(_S_threshold
))
1939 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1940 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1944 std::__insertion_sort(__first
, __last
, __comp
);
1947 /// This is a helper function...
1948 template<typename _RandomAccessIterator
, typename _Compare
>
1949 _GLIBCXX20_CONSTEXPR
1950 _RandomAccessIterator
1951 __unguarded_partition(_RandomAccessIterator __first
,
1952 _RandomAccessIterator __last
,
1953 _RandomAccessIterator __pivot
, _Compare __comp
)
1957 while (__comp(__first
, __pivot
))
1960 while (__comp(__pivot
, __last
))
1962 if (!(__first
< __last
))
1964 std::iter_swap(__first
, __last
);
1969 /// This is a helper function...
1970 template<typename _RandomAccessIterator
, typename _Compare
>
1971 _GLIBCXX20_CONSTEXPR
1972 inline _RandomAccessIterator
1973 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1974 _RandomAccessIterator __last
, _Compare __comp
)
1976 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1977 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1979 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1982 template<typename _RandomAccessIterator
, typename _Compare
>
1983 _GLIBCXX20_CONSTEXPR
1985 __partial_sort(_RandomAccessIterator __first
,
1986 _RandomAccessIterator __middle
,
1987 _RandomAccessIterator __last
,
1990 std::__heap_select(__first
, __middle
, __last
, __comp
);
1991 std::__sort_heap(__first
, __middle
, __comp
);
1994 /// This is a helper function for the sort routine.
1995 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1996 _GLIBCXX20_CONSTEXPR
1998 __introsort_loop(_RandomAccessIterator __first
,
1999 _RandomAccessIterator __last
,
2000 _Size __depth_limit
, _Compare __comp
)
2002 while (__last
- __first
> int(_S_threshold
))
2004 if (__depth_limit
== 0)
2006 std::__partial_sort(__first
, __last
, __last
, __comp
);
2010 _RandomAccessIterator __cut
=
2011 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2012 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2019 template<typename _RandomAccessIterator
, typename _Compare
>
2020 _GLIBCXX20_CONSTEXPR
2022 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2025 if (__first
!= __last
)
2027 std::__introsort_loop(__first
, __last
,
2028 std::__lg(__last
- __first
) * 2,
2030 std::__final_insertion_sort(__first
, __last
, __comp
);
2034 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2035 _GLIBCXX20_CONSTEXPR
2037 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2038 _RandomAccessIterator __last
, _Size __depth_limit
,
2041 while (__last
- __first
> 3)
2043 if (__depth_limit
== 0)
2045 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2046 // Place the nth largest element in its final position.
2047 std::iter_swap(__first
, __nth
);
2051 _RandomAccessIterator __cut
=
2052 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2058 std::__insertion_sort(__first
, __last
, __comp
);
2063 // lower_bound moved to stl_algobase.h
2066 * @brief Finds the first position in which @p __val could be inserted
2067 * without changing the ordering.
2068 * @ingroup binary_search_algorithms
2069 * @param __first An iterator.
2070 * @param __last Another iterator.
2071 * @param __val The search term.
2072 * @param __comp A functor to use for comparisons.
2073 * @return An iterator pointing to the first element <em>not less
2074 * than</em> @p __val, or end() if every element is less
2076 * @ingroup binary_search_algorithms
2078 * The comparison function should have the same effects on ordering as
2079 * the function used for the initial sort.
2081 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2082 _GLIBCXX20_CONSTEXPR
2083 inline _ForwardIterator
2084 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2085 const _Tp
& __val
, _Compare __comp
)
2087 // concept requirements
2088 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2089 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2090 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2091 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2094 return std::__lower_bound(__first
, __last
, __val
,
2095 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2098 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2099 _GLIBCXX20_CONSTEXPR
2101 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2102 const _Tp
& __val
, _Compare __comp
)
2104 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2107 _DistanceType __len
= std::distance(__first
, __last
);
2111 _DistanceType __half
= __len
>> 1;
2112 _ForwardIterator __middle
= __first
;
2113 std::advance(__middle
, __half
);
2114 if (__comp(__val
, __middle
))
2120 __len
= __len
- __half
- 1;
2127 * @brief Finds the last position in which @p __val could be inserted
2128 * without changing the ordering.
2129 * @ingroup binary_search_algorithms
2130 * @param __first An iterator.
2131 * @param __last Another iterator.
2132 * @param __val The search term.
2133 * @return An iterator pointing to the first element greater than @p __val,
2134 * or end() if no elements are greater than @p __val.
2135 * @ingroup binary_search_algorithms
2137 template<typename _ForwardIterator
, typename _Tp
>
2138 _GLIBCXX20_CONSTEXPR
2139 inline _ForwardIterator
2140 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2143 // concept requirements
2144 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2145 __glibcxx_function_requires(_LessThanOpConcept
<
2146 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2147 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2149 return std::__upper_bound(__first
, __last
, __val
,
2150 __gnu_cxx::__ops::__val_less_iter());
2154 * @brief Finds the last position in which @p __val could be inserted
2155 * without changing the ordering.
2156 * @ingroup binary_search_algorithms
2157 * @param __first An iterator.
2158 * @param __last Another iterator.
2159 * @param __val The search term.
2160 * @param __comp A functor to use for comparisons.
2161 * @return An iterator pointing to the first element greater than @p __val,
2162 * or end() if no elements are greater than @p __val.
2163 * @ingroup binary_search_algorithms
2165 * The comparison function should have the same effects on ordering as
2166 * the function used for the initial sort.
2168 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2169 _GLIBCXX20_CONSTEXPR
2170 inline _ForwardIterator
2171 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2172 const _Tp
& __val
, _Compare __comp
)
2174 // concept requirements
2175 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2176 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2177 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2178 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2181 return std::__upper_bound(__first
, __last
, __val
,
2182 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2185 template<typename _ForwardIterator
, typename _Tp
,
2186 typename _CompareItTp
, typename _CompareTpIt
>
2187 _GLIBCXX20_CONSTEXPR
2188 pair
<_ForwardIterator
, _ForwardIterator
>
2189 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2191 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2193 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2196 _DistanceType __len
= std::distance(__first
, __last
);
2200 _DistanceType __half
= __len
>> 1;
2201 _ForwardIterator __middle
= __first
;
2202 std::advance(__middle
, __half
);
2203 if (__comp_it_val(__middle
, __val
))
2207 __len
= __len
- __half
- 1;
2209 else if (__comp_val_it(__val
, __middle
))
2213 _ForwardIterator __left
2214 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2215 std::advance(__first
, __len
);
2216 _ForwardIterator __right
2217 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2218 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2221 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2225 * @brief Finds the largest subrange in which @p __val could be inserted
2226 * at any place in it without changing the ordering.
2227 * @ingroup binary_search_algorithms
2228 * @param __first An iterator.
2229 * @param __last Another iterator.
2230 * @param __val The search term.
2231 * @return An pair of iterators defining the subrange.
2232 * @ingroup binary_search_algorithms
2234 * This is equivalent to
2236 * std::make_pair(lower_bound(__first, __last, __val),
2237 * upper_bound(__first, __last, __val))
2239 * but does not actually call those functions.
2241 template<typename _ForwardIterator
, typename _Tp
>
2242 _GLIBCXX20_CONSTEXPR
2243 inline pair
<_ForwardIterator
, _ForwardIterator
>
2244 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2247 // concept requirements
2248 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2249 __glibcxx_function_requires(_LessThanOpConcept
<
2250 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2251 __glibcxx_function_requires(_LessThanOpConcept
<
2252 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2253 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2254 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2256 return std::__equal_range(__first
, __last
, __val
,
2257 __gnu_cxx::__ops::__iter_less_val(),
2258 __gnu_cxx::__ops::__val_less_iter());
2262 * @brief Finds the largest subrange in which @p __val could be inserted
2263 * at any place in it without changing the ordering.
2264 * @param __first An iterator.
2265 * @param __last Another iterator.
2266 * @param __val The search term.
2267 * @param __comp A functor to use for comparisons.
2268 * @return An pair of iterators defining the subrange.
2269 * @ingroup binary_search_algorithms
2271 * This is equivalent to
2273 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2274 * upper_bound(__first, __last, __val, __comp))
2276 * but does not actually call those functions.
2278 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2279 _GLIBCXX20_CONSTEXPR
2280 inline pair
<_ForwardIterator
, _ForwardIterator
>
2281 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2282 const _Tp
& __val
, _Compare __comp
)
2284 // concept requirements
2285 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2286 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2287 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2288 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2289 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2290 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2292 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2295 return std::__equal_range(__first
, __last
, __val
,
2296 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2297 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2301 * @brief Determines whether an element exists in a range.
2302 * @ingroup binary_search_algorithms
2303 * @param __first An iterator.
2304 * @param __last Another iterator.
2305 * @param __val The search term.
2306 * @return True if @p __val (or its equivalent) is in [@p
2307 * __first,@p __last ].
2309 * Note that this does not actually return an iterator to @p __val. For
2310 * that, use std::find or a container's specialized find member functions.
2312 template<typename _ForwardIterator
, typename _Tp
>
2313 _GLIBCXX20_CONSTEXPR
2315 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2318 // concept requirements
2319 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2320 __glibcxx_function_requires(_LessThanOpConcept
<
2321 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2322 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2323 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2325 _ForwardIterator __i
2326 = std::__lower_bound(__first
, __last
, __val
,
2327 __gnu_cxx::__ops::__iter_less_val());
2328 return __i
!= __last
&& !(__val
< *__i
);
2332 * @brief Determines whether an element exists in a range.
2333 * @ingroup binary_search_algorithms
2334 * @param __first An iterator.
2335 * @param __last Another iterator.
2336 * @param __val The search term.
2337 * @param __comp A functor to use for comparisons.
2338 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2340 * Note that this does not actually return an iterator to @p __val. For
2341 * that, use std::find or a container's specialized find member functions.
2343 * The comparison function should have the same effects on ordering as
2344 * the function used for the initial sort.
2346 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2347 _GLIBCXX20_CONSTEXPR
2349 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2350 const _Tp
& __val
, _Compare __comp
)
2352 // concept requirements
2353 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2354 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2355 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2356 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2358 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2361 _ForwardIterator __i
2362 = std::__lower_bound(__first
, __last
, __val
,
2363 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2364 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2369 /// This is a helper function for the __merge_adaptive routines.
2370 template<typename _InputIterator1
, typename _InputIterator2
,
2371 typename _OutputIterator
, typename _Compare
>
2373 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2374 _InputIterator2 __first2
, _InputIterator2 __last2
,
2375 _OutputIterator __result
, _Compare __comp
)
2377 while (__first1
!= __last1
&& __first2
!= __last2
)
2379 if (__comp(__first2
, __first1
))
2381 *__result
= _GLIBCXX_MOVE(*__first2
);
2386 *__result
= _GLIBCXX_MOVE(*__first1
);
2391 if (__first1
!= __last1
)
2392 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2395 /// This is a helper function for the __merge_adaptive routines.
2396 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2397 typename _BidirectionalIterator3
, typename _Compare
>
2399 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2400 _BidirectionalIterator1 __last1
,
2401 _BidirectionalIterator2 __first2
,
2402 _BidirectionalIterator2 __last2
,
2403 _BidirectionalIterator3 __result
,
2406 if (__first1
== __last1
)
2408 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2411 else if (__first2
== __last2
)
2418 if (__comp(__last2
, __last1
))
2420 *--__result
= _GLIBCXX_MOVE(*__last1
);
2421 if (__first1
== __last1
)
2423 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2430 *--__result
= _GLIBCXX_MOVE(*__last2
);
2431 if (__first2
== __last2
)
2438 /// This is a helper function for the merge routines.
2439 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2441 _BidirectionalIterator1
2442 __rotate_adaptive(_BidirectionalIterator1 __first
,
2443 _BidirectionalIterator1 __middle
,
2444 _BidirectionalIterator1 __last
,
2445 _Distance __len1
, _Distance __len2
,
2446 _BidirectionalIterator2 __buffer
,
2447 _Distance __buffer_size
)
2449 _BidirectionalIterator2 __buffer_end
;
2450 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2454 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2455 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2456 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2461 else if (__len1
<= __buffer_size
)
2465 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2466 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2467 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2473 return std::rotate(__first
, __middle
, __last
);
2476 /// This is a helper function for the merge routines.
2477 template<typename _BidirectionalIterator
, typename _Distance
,
2478 typename _Pointer
, typename _Compare
>
2480 __merge_adaptive(_BidirectionalIterator __first
,
2481 _BidirectionalIterator __middle
,
2482 _BidirectionalIterator __last
,
2483 _Distance __len1
, _Distance __len2
,
2484 _Pointer __buffer
, _Distance __buffer_size
,
2487 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2489 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2490 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2493 else if (__len2
<= __buffer_size
)
2495 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2496 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2497 __buffer_end
, __last
, __comp
);
2501 _BidirectionalIterator __first_cut
= __first
;
2502 _BidirectionalIterator __second_cut
= __middle
;
2503 _Distance __len11
= 0;
2504 _Distance __len22
= 0;
2505 if (__len1
> __len2
)
2507 __len11
= __len1
/ 2;
2508 std::advance(__first_cut
, __len11
);
2510 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2511 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2512 __len22
= std::distance(__middle
, __second_cut
);
2516 __len22
= __len2
/ 2;
2517 std::advance(__second_cut
, __len22
);
2519 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2520 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2521 __len11
= std::distance(__first
, __first_cut
);
2524 _BidirectionalIterator __new_middle
2525 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2526 __len1
- __len11
, __len22
, __buffer
,
2528 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2529 __len22
, __buffer
, __buffer_size
, __comp
);
2530 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2532 __len2
- __len22
, __buffer
,
2533 __buffer_size
, __comp
);
2537 /// This is a helper function for the merge routines.
2538 template<typename _BidirectionalIterator
, typename _Distance
,
2541 __merge_without_buffer(_BidirectionalIterator __first
,
2542 _BidirectionalIterator __middle
,
2543 _BidirectionalIterator __last
,
2544 _Distance __len1
, _Distance __len2
,
2547 if (__len1
== 0 || __len2
== 0)
2550 if (__len1
+ __len2
== 2)
2552 if (__comp(__middle
, __first
))
2553 std::iter_swap(__first
, __middle
);
2557 _BidirectionalIterator __first_cut
= __first
;
2558 _BidirectionalIterator __second_cut
= __middle
;
2559 _Distance __len11
= 0;
2560 _Distance __len22
= 0;
2561 if (__len1
> __len2
)
2563 __len11
= __len1
/ 2;
2564 std::advance(__first_cut
, __len11
);
2566 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2567 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2568 __len22
= std::distance(__middle
, __second_cut
);
2572 __len22
= __len2
/ 2;
2573 std::advance(__second_cut
, __len22
);
2575 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2576 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2577 __len11
= std::distance(__first
, __first_cut
);
2580 _BidirectionalIterator __new_middle
2581 = std::rotate(__first_cut
, __middle
, __second_cut
);
2582 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2583 __len11
, __len22
, __comp
);
2584 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2585 __len1
- __len11
, __len2
- __len22
, __comp
);
2588 template<typename _BidirectionalIterator
, typename _Compare
>
2590 __inplace_merge(_BidirectionalIterator __first
,
2591 _BidirectionalIterator __middle
,
2592 _BidirectionalIterator __last
,
2595 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2597 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2600 if (__first
== __middle
|| __middle
== __last
)
2603 const _DistanceType __len1
= std::distance(__first
, __middle
);
2604 const _DistanceType __len2
= std::distance(__middle
, __last
);
2606 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2607 _TmpBuf
__buf(__first
, __len1
+ __len2
);
2609 if (__buf
.begin() == 0)
2610 std::__merge_without_buffer
2611 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2613 std::__merge_adaptive
2614 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2615 _DistanceType(__buf
.size()), __comp
);
2619 * @brief Merges two sorted ranges in place.
2620 * @ingroup sorting_algorithms
2621 * @param __first An iterator.
2622 * @param __middle Another iterator.
2623 * @param __last Another iterator.
2626 * Merges two sorted and consecutive ranges, [__first,__middle) and
2627 * [__middle,__last), and puts the result in [__first,__last). The
2628 * output will be sorted. The sort is @e stable, that is, for
2629 * equivalent elements in the two ranges, elements from the first
2630 * range will always come before elements from the second.
2632 * If enough additional memory is available, this takes (__last-__first)-1
2633 * comparisons. Otherwise an NlogN algorithm is used, where N is
2634 * distance(__first,__last).
2636 template<typename _BidirectionalIterator
>
2638 inplace_merge(_BidirectionalIterator __first
,
2639 _BidirectionalIterator __middle
,
2640 _BidirectionalIterator __last
)
2642 // concept requirements
2643 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2644 _BidirectionalIterator
>)
2645 __glibcxx_function_requires(_LessThanComparableConcept
<
2646 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2647 __glibcxx_requires_sorted(__first
, __middle
);
2648 __glibcxx_requires_sorted(__middle
, __last
);
2649 __glibcxx_requires_irreflexive(__first
, __last
);
2651 std::__inplace_merge(__first
, __middle
, __last
,
2652 __gnu_cxx::__ops::__iter_less_iter());
2656 * @brief Merges two sorted ranges in place.
2657 * @ingroup sorting_algorithms
2658 * @param __first An iterator.
2659 * @param __middle Another iterator.
2660 * @param __last Another iterator.
2661 * @param __comp A functor to use for comparisons.
2664 * Merges two sorted and consecutive ranges, [__first,__middle) and
2665 * [middle,last), and puts the result in [__first,__last). The output will
2666 * be sorted. The sort is @e stable, that is, for equivalent
2667 * elements in the two ranges, elements from the first range will always
2668 * come before elements from the second.
2670 * If enough additional memory is available, this takes (__last-__first)-1
2671 * comparisons. Otherwise an NlogN algorithm is used, where N is
2672 * distance(__first,__last).
2674 * The comparison function should have the same effects on ordering as
2675 * the function used for the initial sort.
2677 template<typename _BidirectionalIterator
, typename _Compare
>
2679 inplace_merge(_BidirectionalIterator __first
,
2680 _BidirectionalIterator __middle
,
2681 _BidirectionalIterator __last
,
2684 // concept requirements
2685 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2686 _BidirectionalIterator
>)
2687 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2688 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2689 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2690 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2691 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2692 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2694 std::__inplace_merge(__first
, __middle
, __last
,
2695 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2699 /// This is a helper function for the __merge_sort_loop routines.
2700 template<typename _InputIterator
, typename _OutputIterator
,
2703 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2704 _InputIterator __first2
, _InputIterator __last2
,
2705 _OutputIterator __result
, _Compare __comp
)
2707 while (__first1
!= __last1
&& __first2
!= __last2
)
2709 if (__comp(__first2
, __first1
))
2711 *__result
= _GLIBCXX_MOVE(*__first2
);
2716 *__result
= _GLIBCXX_MOVE(*__first1
);
2721 return _GLIBCXX_MOVE3(__first2
, __last2
,
2722 _GLIBCXX_MOVE3(__first1
, __last1
,
2726 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2727 typename _Distance
, typename _Compare
>
2729 __merge_sort_loop(_RandomAccessIterator1 __first
,
2730 _RandomAccessIterator1 __last
,
2731 _RandomAccessIterator2 __result
, _Distance __step_size
,
2734 const _Distance __two_step
= 2 * __step_size
;
2736 while (__last
- __first
>= __two_step
)
2738 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2739 __first
+ __step_size
,
2740 __first
+ __two_step
,
2742 __first
+= __two_step
;
2744 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2746 std::__move_merge(__first
, __first
+ __step_size
,
2747 __first
+ __step_size
, __last
, __result
, __comp
);
2750 template<typename _RandomAccessIterator
, typename _Distance
,
2752 _GLIBCXX20_CONSTEXPR
2754 __chunk_insertion_sort(_RandomAccessIterator __first
,
2755 _RandomAccessIterator __last
,
2756 _Distance __chunk_size
, _Compare __comp
)
2758 while (__last
- __first
>= __chunk_size
)
2760 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2761 __first
+= __chunk_size
;
2763 std::__insertion_sort(__first
, __last
, __comp
);
2766 enum { _S_chunk_size
= 7 };
2768 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2770 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2771 _RandomAccessIterator __last
,
2772 _Pointer __buffer
, _Compare __comp
)
2774 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2777 const _Distance __len
= __last
- __first
;
2778 const _Pointer __buffer_last
= __buffer
+ __len
;
2780 _Distance __step_size
= _S_chunk_size
;
2781 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2783 while (__step_size
< __len
)
2785 std::__merge_sort_loop(__first
, __last
, __buffer
,
2786 __step_size
, __comp
);
2788 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2789 __step_size
, __comp
);
2794 template<typename _RandomAccessIterator
, typename _Pointer
,
2795 typename _Distance
, typename _Compare
>
2797 __stable_sort_adaptive(_RandomAccessIterator __first
,
2798 _RandomAccessIterator __last
,
2799 _Pointer __buffer
, _Distance __buffer_size
,
2802 const _Distance __len
= (__last
- __first
+ 1) / 2;
2803 const _RandomAccessIterator __middle
= __first
+ __len
;
2804 if (__len
> __buffer_size
)
2806 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2807 __buffer_size
, __comp
);
2808 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2809 __buffer_size
, __comp
);
2813 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2814 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2816 std::__merge_adaptive(__first
, __middle
, __last
,
2817 _Distance(__middle
- __first
),
2818 _Distance(__last
- __middle
),
2819 __buffer
, __buffer_size
,
2823 /// This is a helper function for the stable sorting routines.
2824 template<typename _RandomAccessIterator
, typename _Compare
>
2826 __inplace_stable_sort(_RandomAccessIterator __first
,
2827 _RandomAccessIterator __last
, _Compare __comp
)
2829 if (__last
- __first
< 15)
2831 std::__insertion_sort(__first
, __last
, __comp
);
2834 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2835 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2836 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2837 std::__merge_without_buffer(__first
, __middle
, __last
,
2845 // Set algorithms: includes, set_union, set_intersection, set_difference,
2846 // set_symmetric_difference. All of these algorithms have the precondition
2847 // that their input ranges are sorted and the postcondition that their output
2848 // ranges are sorted.
2850 template<typename _InputIterator1
, typename _InputIterator2
,
2852 _GLIBCXX20_CONSTEXPR
2854 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2855 _InputIterator2 __first2
, _InputIterator2 __last2
,
2858 while (__first1
!= __last1
&& __first2
!= __last2
)
2859 if (__comp(__first2
, __first1
))
2861 else if (__comp(__first1
, __first2
))
2869 return __first2
== __last2
;
2873 * @brief Determines whether all elements of a sequence exists in a range.
2874 * @param __first1 Start of search range.
2875 * @param __last1 End of search range.
2876 * @param __first2 Start of sequence
2877 * @param __last2 End of sequence.
2878 * @return True if each element in [__first2,__last2) is contained in order
2879 * within [__first1,__last1). False otherwise.
2880 * @ingroup set_algorithms
2882 * This operation expects both [__first1,__last1) and
2883 * [__first2,__last2) to be sorted. Searches for the presence of
2884 * each element in [__first2,__last2) within [__first1,__last1).
2885 * The iterators over each range only move forward, so this is a
2886 * linear algorithm. If an element in [__first2,__last2) is not
2887 * found before the search iterator reaches @p __last2, false is
2890 template<typename _InputIterator1
, typename _InputIterator2
>
2891 _GLIBCXX20_CONSTEXPR
2893 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2894 _InputIterator2 __first2
, _InputIterator2 __last2
)
2896 // concept requirements
2897 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2898 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2899 __glibcxx_function_requires(_LessThanOpConcept
<
2900 typename iterator_traits
<_InputIterator1
>::value_type
,
2901 typename iterator_traits
<_InputIterator2
>::value_type
>)
2902 __glibcxx_function_requires(_LessThanOpConcept
<
2903 typename iterator_traits
<_InputIterator2
>::value_type
,
2904 typename iterator_traits
<_InputIterator1
>::value_type
>)
2905 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2906 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2907 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2908 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2910 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2911 __gnu_cxx::__ops::__iter_less_iter());
2915 * @brief Determines whether all elements of a sequence exists in a range
2917 * @ingroup set_algorithms
2918 * @param __first1 Start of search range.
2919 * @param __last1 End of search range.
2920 * @param __first2 Start of sequence
2921 * @param __last2 End of sequence.
2922 * @param __comp Comparison function to use.
2923 * @return True if each element in [__first2,__last2) is contained
2924 * in order within [__first1,__last1) according to comp. False
2925 * otherwise. @ingroup set_algorithms
2927 * This operation expects both [__first1,__last1) and
2928 * [__first2,__last2) to be sorted. Searches for the presence of
2929 * each element in [__first2,__last2) within [__first1,__last1),
2930 * using comp to decide. The iterators over each range only move
2931 * forward, so this is a linear algorithm. If an element in
2932 * [__first2,__last2) is not found before the search iterator
2933 * reaches @p __last2, false is returned.
2935 template<typename _InputIterator1
, typename _InputIterator2
,
2937 _GLIBCXX20_CONSTEXPR
2939 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2940 _InputIterator2 __first2
, _InputIterator2 __last2
,
2943 // concept requirements
2944 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2945 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2946 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2947 typename iterator_traits
<_InputIterator1
>::value_type
,
2948 typename iterator_traits
<_InputIterator2
>::value_type
>)
2949 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2950 typename iterator_traits
<_InputIterator2
>::value_type
,
2951 typename iterator_traits
<_InputIterator1
>::value_type
>)
2952 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2953 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2954 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2955 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2957 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2958 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2967 // set_symmetric_difference
2971 template<typename _BidirectionalIterator
, typename _Compare
>
2972 _GLIBCXX20_CONSTEXPR
2974 __next_permutation(_BidirectionalIterator __first
,
2975 _BidirectionalIterator __last
, _Compare __comp
)
2977 if (__first
== __last
)
2979 _BidirectionalIterator __i
= __first
;
2988 _BidirectionalIterator __ii
= __i
;
2990 if (__comp(__i
, __ii
))
2992 _BidirectionalIterator __j
= __last
;
2993 while (!__comp(__i
, --__j
))
2995 std::iter_swap(__i
, __j
);
2996 std::__reverse(__ii
, __last
,
2997 std::__iterator_category(__first
));
3002 std::__reverse(__first
, __last
,
3003 std::__iterator_category(__first
));
3010 * @brief Permute range into the next @e dictionary ordering.
3011 * @ingroup sorting_algorithms
3012 * @param __first Start of range.
3013 * @param __last End of range.
3014 * @return False if wrapped to first permutation, true otherwise.
3016 * Treats all permutations of the range as a set of @e dictionary sorted
3017 * sequences. Permutes the current sequence into the next one of this set.
3018 * Returns true if there are more sequences to generate. If the sequence
3019 * is the largest of the set, the smallest is generated and false returned.
3021 template<typename _BidirectionalIterator
>
3022 _GLIBCXX20_CONSTEXPR
3024 next_permutation(_BidirectionalIterator __first
,
3025 _BidirectionalIterator __last
)
3027 // concept requirements
3028 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3029 _BidirectionalIterator
>)
3030 __glibcxx_function_requires(_LessThanComparableConcept
<
3031 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3032 __glibcxx_requires_valid_range(__first
, __last
);
3033 __glibcxx_requires_irreflexive(__first
, __last
);
3035 return std::__next_permutation
3036 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
3040 * @brief Permute range into the next @e dictionary ordering using
3041 * comparison functor.
3042 * @ingroup sorting_algorithms
3043 * @param __first Start of range.
3044 * @param __last End of range.
3045 * @param __comp A comparison functor.
3046 * @return False if wrapped to first permutation, true otherwise.
3048 * Treats all permutations of the range [__first,__last) as a set of
3049 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3050 * sequence into the next one of this set. Returns true if there are more
3051 * sequences to generate. If the sequence is the largest of the set, the
3052 * smallest is generated and false returned.
3054 template<typename _BidirectionalIterator
, typename _Compare
>
3055 _GLIBCXX20_CONSTEXPR
3057 next_permutation(_BidirectionalIterator __first
,
3058 _BidirectionalIterator __last
, _Compare __comp
)
3060 // concept requirements
3061 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3062 _BidirectionalIterator
>)
3063 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3064 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3065 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3066 __glibcxx_requires_valid_range(__first
, __last
);
3067 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3069 return std::__next_permutation
3070 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3073 template<typename _BidirectionalIterator
, typename _Compare
>
3074 _GLIBCXX20_CONSTEXPR
3076 __prev_permutation(_BidirectionalIterator __first
,
3077 _BidirectionalIterator __last
, _Compare __comp
)
3079 if (__first
== __last
)
3081 _BidirectionalIterator __i
= __first
;
3090 _BidirectionalIterator __ii
= __i
;
3092 if (__comp(__ii
, __i
))
3094 _BidirectionalIterator __j
= __last
;
3095 while (!__comp(--__j
, __i
))
3097 std::iter_swap(__i
, __j
);
3098 std::__reverse(__ii
, __last
,
3099 std::__iterator_category(__first
));
3104 std::__reverse(__first
, __last
,
3105 std::__iterator_category(__first
));
3112 * @brief Permute range into the previous @e dictionary ordering.
3113 * @ingroup sorting_algorithms
3114 * @param __first Start of range.
3115 * @param __last End of range.
3116 * @return False if wrapped to last permutation, true otherwise.
3118 * Treats all permutations of the range as a set of @e dictionary sorted
3119 * sequences. Permutes the current sequence into the previous one of this
3120 * set. Returns true if there are more sequences to generate. If the
3121 * sequence is the smallest of the set, the largest is generated and false
3124 template<typename _BidirectionalIterator
>
3125 _GLIBCXX20_CONSTEXPR
3127 prev_permutation(_BidirectionalIterator __first
,
3128 _BidirectionalIterator __last
)
3130 // concept requirements
3131 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3132 _BidirectionalIterator
>)
3133 __glibcxx_function_requires(_LessThanComparableConcept
<
3134 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3135 __glibcxx_requires_valid_range(__first
, __last
);
3136 __glibcxx_requires_irreflexive(__first
, __last
);
3138 return std::__prev_permutation(__first
, __last
,
3139 __gnu_cxx::__ops::__iter_less_iter());
3143 * @brief Permute range into the previous @e dictionary ordering using
3144 * comparison functor.
3145 * @ingroup sorting_algorithms
3146 * @param __first Start of range.
3147 * @param __last End of range.
3148 * @param __comp A comparison functor.
3149 * @return False if wrapped to last permutation, true otherwise.
3151 * Treats all permutations of the range [__first,__last) as a set of
3152 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3153 * sequence into the previous one of this set. Returns true if there are
3154 * more sequences to generate. If the sequence is the smallest of the set,
3155 * the largest is generated and false returned.
3157 template<typename _BidirectionalIterator
, typename _Compare
>
3158 _GLIBCXX20_CONSTEXPR
3160 prev_permutation(_BidirectionalIterator __first
,
3161 _BidirectionalIterator __last
, _Compare __comp
)
3163 // concept requirements
3164 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3165 _BidirectionalIterator
>)
3166 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3167 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3168 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3169 __glibcxx_requires_valid_range(__first
, __last
);
3170 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3172 return std::__prev_permutation(__first
, __last
,
3173 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3179 template<typename _InputIterator
, typename _OutputIterator
,
3180 typename _Predicate
, typename _Tp
>
3181 _GLIBCXX20_CONSTEXPR
3183 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3184 _OutputIterator __result
,
3185 _Predicate __pred
, const _Tp
& __new_value
)
3187 for (; __first
!= __last
; ++__first
, (void)++__result
)
3188 if (__pred(__first
))
3189 *__result
= __new_value
;
3191 *__result
= *__first
;
3196 * @brief Copy a sequence, replacing each element of one value with another
3198 * @param __first An input iterator.
3199 * @param __last An input iterator.
3200 * @param __result An output iterator.
3201 * @param __old_value The value to be replaced.
3202 * @param __new_value The replacement value.
3203 * @return The end of the output sequence, @p result+(last-first).
3205 * Copies each element in the input range @p [__first,__last) to the
3206 * output range @p [__result,__result+(__last-__first)) replacing elements
3207 * equal to @p __old_value with @p __new_value.
3209 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3210 _GLIBCXX20_CONSTEXPR
3211 inline _OutputIterator
3212 replace_copy(_InputIterator __first
, _InputIterator __last
,
3213 _OutputIterator __result
,
3214 const _Tp
& __old_value
, const _Tp
& __new_value
)
3216 // concept requirements
3217 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3218 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3219 typename iterator_traits
<_InputIterator
>::value_type
>)
3220 __glibcxx_function_requires(_EqualOpConcept
<
3221 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3222 __glibcxx_requires_valid_range(__first
, __last
);
3224 return std::__replace_copy_if(__first
, __last
, __result
,
3225 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3230 * @brief Copy a sequence, replacing each value for which a predicate
3231 * returns true with another value.
3232 * @ingroup mutating_algorithms
3233 * @param __first An input iterator.
3234 * @param __last An input iterator.
3235 * @param __result An output iterator.
3236 * @param __pred A predicate.
3237 * @param __new_value The replacement value.
3238 * @return The end of the output sequence, @p __result+(__last-__first).
3240 * Copies each element in the range @p [__first,__last) to the range
3241 * @p [__result,__result+(__last-__first)) replacing elements for which
3242 * @p __pred returns true with @p __new_value.
3244 template<typename _InputIterator
, typename _OutputIterator
,
3245 typename _Predicate
, typename _Tp
>
3246 _GLIBCXX20_CONSTEXPR
3247 inline _OutputIterator
3248 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3249 _OutputIterator __result
,
3250 _Predicate __pred
, const _Tp
& __new_value
)
3252 // concept requirements
3253 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3254 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3255 typename iterator_traits
<_InputIterator
>::value_type
>)
3256 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3257 typename iterator_traits
<_InputIterator
>::value_type
>)
3258 __glibcxx_requires_valid_range(__first
, __last
);
3260 return std::__replace_copy_if(__first
, __last
, __result
,
3261 __gnu_cxx::__ops::__pred_iter(__pred
),
3265 template<typename _InputIterator
, typename _Predicate
>
3266 _GLIBCXX20_CONSTEXPR
3267 typename iterator_traits
<_InputIterator
>::difference_type
3268 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3270 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3271 for (; __first
!= __last
; ++__first
)
3272 if (__pred(__first
))
3277 #if __cplusplus >= 201103L
3279 * @brief Determines whether the elements of a sequence are sorted.
3280 * @ingroup sorting_algorithms
3281 * @param __first An iterator.
3282 * @param __last Another iterator.
3283 * @return True if the elements are sorted, false otherwise.
3285 template<typename _ForwardIterator
>
3286 _GLIBCXX20_CONSTEXPR
3288 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3289 { return std::is_sorted_until(__first
, __last
) == __last
; }
3292 * @brief Determines whether the elements of a sequence are sorted
3293 * according to a comparison functor.
3294 * @ingroup sorting_algorithms
3295 * @param __first An iterator.
3296 * @param __last Another iterator.
3297 * @param __comp A comparison functor.
3298 * @return True if the elements are sorted, false otherwise.
3300 template<typename _ForwardIterator
, typename _Compare
>
3301 _GLIBCXX20_CONSTEXPR
3303 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3305 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3307 template<typename _ForwardIterator
, typename _Compare
>
3308 _GLIBCXX20_CONSTEXPR
3310 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3313 if (__first
== __last
)
3316 _ForwardIterator __next
= __first
;
3317 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3318 if (__comp(__next
, __first
))
3324 * @brief Determines the end of a sorted sequence.
3325 * @ingroup sorting_algorithms
3326 * @param __first An iterator.
3327 * @param __last Another iterator.
3328 * @return An iterator pointing to the last iterator i in [__first, __last)
3329 * for which the range [__first, i) is sorted.
3331 template<typename _ForwardIterator
>
3332 _GLIBCXX20_CONSTEXPR
3333 inline _ForwardIterator
3334 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3336 // concept requirements
3337 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3338 __glibcxx_function_requires(_LessThanComparableConcept
<
3339 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3340 __glibcxx_requires_valid_range(__first
, __last
);
3341 __glibcxx_requires_irreflexive(__first
, __last
);
3343 return std::__is_sorted_until(__first
, __last
,
3344 __gnu_cxx::__ops::__iter_less_iter());
3348 * @brief Determines the end of a sorted sequence using comparison functor.
3349 * @ingroup sorting_algorithms
3350 * @param __first An iterator.
3351 * @param __last Another iterator.
3352 * @param __comp A comparison functor.
3353 * @return An iterator pointing to the last iterator i in [__first, __last)
3354 * for which the range [__first, i) is sorted.
3356 template<typename _ForwardIterator
, typename _Compare
>
3357 _GLIBCXX20_CONSTEXPR
3358 inline _ForwardIterator
3359 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3362 // concept requirements
3363 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3364 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3365 typename iterator_traits
<_ForwardIterator
>::value_type
,
3366 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3367 __glibcxx_requires_valid_range(__first
, __last
);
3368 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3370 return std::__is_sorted_until(__first
, __last
,
3371 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3375 * @brief Determines min and max at once as an ordered pair.
3376 * @ingroup sorting_algorithms
3377 * @param __a A thing of arbitrary type.
3378 * @param __b Another thing of arbitrary type.
3379 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3382 template<typename _Tp
>
3383 _GLIBCXX14_CONSTEXPR
3384 inline pair
<const _Tp
&, const _Tp
&>
3385 minmax(const _Tp
& __a
, const _Tp
& __b
)
3387 // concept requirements
3388 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3390 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3391 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3395 * @brief Determines min and max at once as an ordered pair.
3396 * @ingroup sorting_algorithms
3397 * @param __a A thing of arbitrary type.
3398 * @param __b Another thing of arbitrary type.
3399 * @param __comp A @link comparison_functors comparison functor @endlink.
3400 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3403 template<typename _Tp
, typename _Compare
>
3404 _GLIBCXX14_CONSTEXPR
3405 inline pair
<const _Tp
&, const _Tp
&>
3406 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3408 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3409 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3412 template<typename _ForwardIterator
, typename _Compare
>
3413 _GLIBCXX14_CONSTEXPR
3414 pair
<_ForwardIterator
, _ForwardIterator
>
3415 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3418 _ForwardIterator __next
= __first
;
3419 if (__first
== __last
3420 || ++__next
== __last
)
3421 return std::make_pair(__first
, __first
);
3423 _ForwardIterator __min
{}, __max
{};
3424 if (__comp(__next
, __first
))
3438 while (__first
!= __last
)
3441 if (++__next
== __last
)
3443 if (__comp(__first
, __min
))
3445 else if (!__comp(__first
, __max
))
3450 if (__comp(__next
, __first
))
3452 if (__comp(__next
, __min
))
3454 if (!__comp(__first
, __max
))
3459 if (__comp(__first
, __min
))
3461 if (!__comp(__next
, __max
))
3469 return std::make_pair(__min
, __max
);
3473 * @brief Return a pair of iterators pointing to the minimum and maximum
3474 * elements in a range.
3475 * @ingroup sorting_algorithms
3476 * @param __first Start of range.
3477 * @param __last End of range.
3478 * @return make_pair(m, M), where m is the first iterator i in
3479 * [__first, __last) such that no other element in the range is
3480 * smaller, and where M is the last iterator i in [__first, __last)
3481 * such that no other element in the range is larger.
3483 template<typename _ForwardIterator
>
3484 _GLIBCXX14_CONSTEXPR
3485 inline pair
<_ForwardIterator
, _ForwardIterator
>
3486 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3488 // concept requirements
3489 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3490 __glibcxx_function_requires(_LessThanComparableConcept
<
3491 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3492 __glibcxx_requires_valid_range(__first
, __last
);
3493 __glibcxx_requires_irreflexive(__first
, __last
);
3495 return std::__minmax_element(__first
, __last
,
3496 __gnu_cxx::__ops::__iter_less_iter());
3500 * @brief Return a pair of iterators pointing to the minimum and maximum
3501 * elements in a range.
3502 * @ingroup sorting_algorithms
3503 * @param __first Start of range.
3504 * @param __last End of range.
3505 * @param __comp Comparison functor.
3506 * @return make_pair(m, M), where m is the first iterator i in
3507 * [__first, __last) such that no other element in the range is
3508 * smaller, and where M is the last iterator i in [__first, __last)
3509 * such that no other element in the range is larger.
3511 template<typename _ForwardIterator
, typename _Compare
>
3512 _GLIBCXX14_CONSTEXPR
3513 inline pair
<_ForwardIterator
, _ForwardIterator
>
3514 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3517 // concept requirements
3518 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3519 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3520 typename iterator_traits
<_ForwardIterator
>::value_type
,
3521 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3522 __glibcxx_requires_valid_range(__first
, __last
);
3523 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3525 return std::__minmax_element(__first
, __last
,
3526 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3530 template<typename _Tp
>
3531 _GLIBCXX14_CONSTEXPR
3533 min(initializer_list
<_Tp
> __l
)
3534 { return *std::min_element(__l
.begin(), __l
.end()); }
3536 template<typename _Tp
, typename _Compare
>
3537 _GLIBCXX14_CONSTEXPR
3539 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3540 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3542 template<typename _Tp
>
3543 _GLIBCXX14_CONSTEXPR
3545 max(initializer_list
<_Tp
> __l
)
3546 { return *std::max_element(__l
.begin(), __l
.end()); }
3548 template<typename _Tp
, typename _Compare
>
3549 _GLIBCXX14_CONSTEXPR
3551 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3552 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3554 template<typename _Tp
>
3555 _GLIBCXX14_CONSTEXPR
3556 inline pair
<_Tp
, _Tp
>
3557 minmax(initializer_list
<_Tp
> __l
)
3559 pair
<const _Tp
*, const _Tp
*> __p
=
3560 std::minmax_element(__l
.begin(), __l
.end());
3561 return std::make_pair(*__p
.first
, *__p
.second
);
3564 template<typename _Tp
, typename _Compare
>
3565 _GLIBCXX14_CONSTEXPR
3566 inline pair
<_Tp
, _Tp
>
3567 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3569 pair
<const _Tp
*, const _Tp
*> __p
=
3570 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3571 return std::make_pair(*__p
.first
, *__p
.second
);
3574 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3575 typename _BinaryPredicate
>
3576 _GLIBCXX20_CONSTEXPR
3578 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3579 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3581 // Efficiently compare identical prefixes: O(N) if sequences
3582 // have the same elements in the same order.
3583 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
3584 if (!__pred(__first1
, __first2
))
3587 if (__first1
== __last1
)
3590 // Establish __last2 assuming equal ranges by iterating over the
3591 // rest of the list.
3592 _ForwardIterator2 __last2
= __first2
;
3593 std::advance(__last2
, std::distance(__first1
, __last1
));
3594 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3596 if (__scan
!= std::__find_if(__first1
, __scan
,
3597 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3598 continue; // We've seen this one before.
3601 = std::__count_if(__first2
, __last2
,
3602 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3603 if (0 == __matches
||
3604 std::__count_if(__scan
, __last1
,
3605 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3613 * @brief Checks whether a permutation of the second sequence is equal
3614 * to the first sequence.
3615 * @ingroup non_mutating_algorithms
3616 * @param __first1 Start of first range.
3617 * @param __last1 End of first range.
3618 * @param __first2 Start of second range.
3619 * @return true if there exists a permutation of the elements in the range
3620 * [__first2, __first2 + (__last1 - __first1)), beginning with
3621 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3622 * returns true; otherwise, returns false.
3624 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3625 _GLIBCXX20_CONSTEXPR
3627 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3628 _ForwardIterator2 __first2
)
3630 // concept requirements
3631 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3632 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3633 __glibcxx_function_requires(_EqualOpConcept
<
3634 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3635 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3636 __glibcxx_requires_valid_range(__first1
, __last1
);
3638 return std::__is_permutation(__first1
, __last1
, __first2
,
3639 __gnu_cxx::__ops::__iter_equal_to_iter());
3643 * @brief Checks whether a permutation of the second sequence is equal
3644 * to the first sequence.
3645 * @ingroup non_mutating_algorithms
3646 * @param __first1 Start of first range.
3647 * @param __last1 End of first range.
3648 * @param __first2 Start of second range.
3649 * @param __pred A binary predicate.
3650 * @return true if there exists a permutation of the elements in
3651 * the range [__first2, __first2 + (__last1 - __first1)),
3652 * beginning with ForwardIterator2 begin, such that
3653 * equal(__first1, __last1, __begin, __pred) returns true;
3654 * otherwise, returns false.
3656 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3657 typename _BinaryPredicate
>
3658 _GLIBCXX20_CONSTEXPR
3660 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3661 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3663 // concept requirements
3664 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3665 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3666 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3667 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3668 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3669 __glibcxx_requires_valid_range(__first1
, __last1
);
3671 return std::__is_permutation(__first1
, __last1
, __first2
,
3672 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3675 #if __cplusplus > 201103L
3676 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3677 typename _BinaryPredicate
>
3678 _GLIBCXX20_CONSTEXPR
3680 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3681 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3682 _BinaryPredicate __pred
)
3685 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3687 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3688 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3689 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3690 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3693 auto __d1
= std::distance(__first1
, __last1
);
3694 auto __d2
= std::distance(__first2
, __last2
);
3699 // Efficiently compare identical prefixes: O(N) if sequences
3700 // have the same elements in the same order.
3701 for (; __first1
!= __last1
&& __first2
!= __last2
;
3702 ++__first1
, (void)++__first2
)
3703 if (!__pred(__first1
, __first2
))
3708 if (__first1
== __last1
)
3713 auto __d1
= std::distance(__first1
, __last1
);
3714 auto __d2
= std::distance(__first2
, __last2
);
3715 if (__d1
== 0 && __d2
== 0)
3721 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3723 if (__scan
!= std::__find_if(__first1
, __scan
,
3724 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3725 continue; // We've seen this one before.
3727 auto __matches
= std::__count_if(__first2
, __last2
,
3728 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3730 || std::__count_if(__scan
, __last1
,
3731 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3739 * @brief Checks whether a permutaion of the second sequence is equal
3740 * to the first sequence.
3741 * @ingroup non_mutating_algorithms
3742 * @param __first1 Start of first range.
3743 * @param __last1 End of first range.
3744 * @param __first2 Start of second range.
3745 * @param __last2 End of first range.
3746 * @return true if there exists a permutation of the elements in the range
3747 * [__first2, __last2), beginning with ForwardIterator2 begin,
3748 * such that equal(__first1, __last1, begin) returns true;
3749 * otherwise, returns false.
3751 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3752 _GLIBCXX20_CONSTEXPR
3754 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3755 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3757 __glibcxx_requires_valid_range(__first1
, __last1
);
3758 __glibcxx_requires_valid_range(__first2
, __last2
);
3761 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3762 __gnu_cxx::__ops::__iter_equal_to_iter());
3766 * @brief Checks whether a permutation of the second sequence is equal
3767 * to the first sequence.
3768 * @ingroup non_mutating_algorithms
3769 * @param __first1 Start of first range.
3770 * @param __last1 End of first range.
3771 * @param __first2 Start of second range.
3772 * @param __last2 End of first range.
3773 * @param __pred A binary predicate.
3774 * @return true if there exists a permutation of the elements in the range
3775 * [__first2, __last2), beginning with ForwardIterator2 begin,
3776 * such that equal(__first1, __last1, __begin, __pred) returns true;
3777 * otherwise, returns false.
3779 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3780 typename _BinaryPredicate
>
3781 _GLIBCXX20_CONSTEXPR
3783 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3784 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3785 _BinaryPredicate __pred
)
3787 __glibcxx_requires_valid_range(__first1
, __last1
);
3788 __glibcxx_requires_valid_range(__first2
, __last2
);
3790 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3791 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3794 #if __cplusplus > 201402L
3796 #define __cpp_lib_clamp 201603
3799 * @brief Returns the value clamped between lo and hi.
3800 * @ingroup sorting_algorithms
3801 * @param __val A value of arbitrary type.
3802 * @param __lo A lower limit of arbitrary type.
3803 * @param __hi An upper limit of arbitrary type.
3804 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
3806 template<typename _Tp
>
3807 constexpr const _Tp
&
3808 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3810 __glibcxx_assert(!(__hi
< __lo
));
3811 return (__val
< __lo
) ? __lo
: (__hi
< __val
) ? __hi
: __val
;
3815 * @brief Returns the value clamped between lo and hi.
3816 * @ingroup sorting_algorithms
3817 * @param __val A value of arbitrary type.
3818 * @param __lo A lower limit of arbitrary type.
3819 * @param __hi An upper limit of arbitrary type.
3820 * @param __comp A comparison functor.
3821 * @return max(__val, __lo, __comp) if __comp(__val, __hi)
3822 * or min(__val, __hi, __comp) otherwise.
3824 template<typename _Tp
, typename _Compare
>
3825 constexpr const _Tp
&
3826 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3828 __glibcxx_assert(!__comp(__hi
, __lo
));
3829 return __comp(__val
, __lo
) ? __lo
: __comp(__hi
, __val
) ? __hi
: __val
;
3834 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3836 * @brief Generate two uniformly distributed integers using a
3837 * single distribution invocation.
3838 * @param __b0 The upper bound for the first integer.
3839 * @param __b1 The upper bound for the second integer.
3840 * @param __g A UniformRandomBitGenerator.
3841 * @return A pair (i, j) with i and j uniformly distributed
3842 * over [0, __b0) and [0, __b1), respectively.
3844 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3846 * Using uniform_int_distribution with a range that is very
3847 * small relative to the range of the generator ends up wasting
3848 * potentially expensively generated randomness, since
3849 * uniform_int_distribution does not store leftover randomness
3850 * between invocations.
3852 * If we know we want two integers in ranges that are sufficiently
3853 * small, we can compose the ranges, use a single distribution
3854 * invocation, and significantly reduce the waste.
3856 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3857 pair
<_IntType
, _IntType
>
3858 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3859 _UniformRandomBitGenerator
&& __g
)
3862 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3863 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3867 * @brief Shuffle the elements of a sequence using a uniform random
3869 * @ingroup mutating_algorithms
3870 * @param __first A forward iterator.
3871 * @param __last A forward iterator.
3872 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3875 * Reorders the elements in the range @p [__first,__last) using @p __g to
3876 * provide random numbers.
3878 template<typename _RandomAccessIterator
,
3879 typename _UniformRandomNumberGenerator
>
3881 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3882 _UniformRandomNumberGenerator
&& __g
)
3884 // concept requirements
3885 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3886 _RandomAccessIterator
>)
3887 __glibcxx_requires_valid_range(__first
, __last
);
3889 if (__first
== __last
)
3892 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3895 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3896 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3897 typedef typename
__distr_type::param_type __p_type
;
3899 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3901 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3904 const __uc_type __urngrange
= __g
.max() - __g
.min();
3905 const __uc_type __urange
= __uc_type(__last
- __first
);
3907 if (__urngrange
/ __urange
>= __urange
)
3908 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3910 _RandomAccessIterator __i
= __first
+ 1;
3912 // Since we know the range isn't empty, an even number of elements
3913 // means an uneven number of elements /to swap/, in which case we
3914 // do the first one up front:
3916 if ((__urange
% 2) == 0)
3918 __distr_type __d
{0, 1};
3919 std::iter_swap(__i
++, __first
+ __d(__g
));
3922 // Now we know that __last - __i is even, so we do the rest in pairs,
3923 // using a single distribution invocation to produce swap positions
3924 // for two successive elements at a time:
3926 while (__i
!= __last
)
3928 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3930 const pair
<__uc_type
, __uc_type
> __pospos
=
3931 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3933 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3934 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3942 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3943 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3949 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3952 * @brief Apply a function to every element of a sequence.
3953 * @ingroup non_mutating_algorithms
3954 * @param __first An input iterator.
3955 * @param __last An input iterator.
3956 * @param __f A unary function object.
3959 * Applies the function object @p __f to each element in the range
3960 * @p [first,last). @p __f must not modify the order of the sequence.
3961 * If @p __f has a return value it is ignored.
3963 template<typename _InputIterator
, typename _Function
>
3964 _GLIBCXX20_CONSTEXPR
3966 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3968 // concept requirements
3969 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3970 __glibcxx_requires_valid_range(__first
, __last
);
3971 for (; __first
!= __last
; ++__first
)
3973 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3976 #if __cplusplus >= 201703L
3978 * @brief Apply a function to every element of a sequence.
3979 * @ingroup non_mutating_algorithms
3980 * @param __first An input iterator.
3981 * @param __n A value convertible to an integer.
3982 * @param __f A unary function object.
3983 * @return `__first+__n`
3985 * Applies the function object `__f` to each element in the range
3986 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3987 * If `__f` has a return value it is ignored.
3989 template<typename _InputIterator
, typename _Size
, typename _Function
>
3991 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3993 auto __n2
= std::__size_to_integer(__n
);
3994 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3995 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3997 auto __last
= __first
+ __n2
;
3998 std::for_each(__first
, __last
, std::move(__f
));
4014 * @brief Find the first occurrence of a value in a sequence.
4015 * @ingroup non_mutating_algorithms
4016 * @param __first An input iterator.
4017 * @param __last An input iterator.
4018 * @param __val The value to find.
4019 * @return The first iterator @c i in the range @p [__first,__last)
4020 * such that @c *i == @p __val, or @p __last if no such iterator exists.
4022 template<typename _InputIterator
, typename _Tp
>
4023 _GLIBCXX20_CONSTEXPR
4024 inline _InputIterator
4025 find(_InputIterator __first
, _InputIterator __last
,
4028 // concept requirements
4029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4030 __glibcxx_function_requires(_EqualOpConcept
<
4031 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4032 __glibcxx_requires_valid_range(__first
, __last
);
4033 return std::__find_if(__first
, __last
,
4034 __gnu_cxx::__ops::__iter_equals_val(__val
));
4038 * @brief Find the first element in a sequence for which a
4039 * predicate is true.
4040 * @ingroup non_mutating_algorithms
4041 * @param __first An input iterator.
4042 * @param __last An input iterator.
4043 * @param __pred A predicate.
4044 * @return The first iterator @c i in the range @p [__first,__last)
4045 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
4047 template<typename _InputIterator
, typename _Predicate
>
4048 _GLIBCXX20_CONSTEXPR
4049 inline _InputIterator
4050 find_if(_InputIterator __first
, _InputIterator __last
,
4053 // concept requirements
4054 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4055 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4056 typename iterator_traits
<_InputIterator
>::value_type
>)
4057 __glibcxx_requires_valid_range(__first
, __last
);
4059 return std::__find_if(__first
, __last
,
4060 __gnu_cxx::__ops::__pred_iter(__pred
));
4064 * @brief Find element from a set in a sequence.
4065 * @ingroup non_mutating_algorithms
4066 * @param __first1 Start of range to search.
4067 * @param __last1 End of range to search.
4068 * @param __first2 Start of match candidates.
4069 * @param __last2 End of match candidates.
4070 * @return The first iterator @c i in the range
4071 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
4072 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
4074 * Searches the range @p [__first1,__last1) for an element that is
4075 * equal to some element in the range [__first2,__last2). If
4076 * found, returns an iterator in the range [__first1,__last1),
4077 * otherwise returns @p __last1.
4079 template<typename _InputIterator
, typename _ForwardIterator
>
4080 _GLIBCXX20_CONSTEXPR
4082 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4083 _ForwardIterator __first2
, _ForwardIterator __last2
)
4085 // concept requirements
4086 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4087 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4088 __glibcxx_function_requires(_EqualOpConcept
<
4089 typename iterator_traits
<_InputIterator
>::value_type
,
4090 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4091 __glibcxx_requires_valid_range(__first1
, __last1
);
4092 __glibcxx_requires_valid_range(__first2
, __last2
);
4094 for (; __first1
!= __last1
; ++__first1
)
4095 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4096 if (*__first1
== *__iter
)
4102 * @brief Find element from a set in a sequence using a predicate.
4103 * @ingroup non_mutating_algorithms
4104 * @param __first1 Start of range to search.
4105 * @param __last1 End of range to search.
4106 * @param __first2 Start of match candidates.
4107 * @param __last2 End of match candidates.
4108 * @param __comp Predicate to use.
4109 * @return The first iterator @c i in the range
4110 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4111 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4112 * such iterator exists.
4115 * Searches the range @p [__first1,__last1) for an element that is
4116 * equal to some element in the range [__first2,__last2). If
4117 * found, returns an iterator in the range [__first1,__last1),
4118 * otherwise returns @p __last1.
4120 template<typename _InputIterator
, typename _ForwardIterator
,
4121 typename _BinaryPredicate
>
4122 _GLIBCXX20_CONSTEXPR
4124 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4125 _ForwardIterator __first2
, _ForwardIterator __last2
,
4126 _BinaryPredicate __comp
)
4128 // concept requirements
4129 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4130 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4131 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4132 typename iterator_traits
<_InputIterator
>::value_type
,
4133 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4134 __glibcxx_requires_valid_range(__first1
, __last1
);
4135 __glibcxx_requires_valid_range(__first2
, __last2
);
4137 for (; __first1
!= __last1
; ++__first1
)
4138 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4139 if (__comp(*__first1
, *__iter
))
4145 * @brief Find two adjacent values in a sequence that are equal.
4146 * @ingroup non_mutating_algorithms
4147 * @param __first A forward iterator.
4148 * @param __last A forward iterator.
4149 * @return The first iterator @c i such that @c i and @c i+1 are both
4150 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4151 * or @p __last if no such iterator exists.
4153 template<typename _ForwardIterator
>
4154 _GLIBCXX20_CONSTEXPR
4155 inline _ForwardIterator
4156 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4158 // concept requirements
4159 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4160 __glibcxx_function_requires(_EqualityComparableConcept
<
4161 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4162 __glibcxx_requires_valid_range(__first
, __last
);
4164 return std::__adjacent_find(__first
, __last
,
4165 __gnu_cxx::__ops::__iter_equal_to_iter());
4169 * @brief Find two adjacent values in a sequence using a predicate.
4170 * @ingroup non_mutating_algorithms
4171 * @param __first A forward iterator.
4172 * @param __last A forward iterator.
4173 * @param __binary_pred A binary predicate.
4174 * @return The first iterator @c i such that @c i and @c i+1 are both
4175 * valid iterators in @p [__first,__last) and such that
4176 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4179 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4180 _GLIBCXX20_CONSTEXPR
4181 inline _ForwardIterator
4182 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4183 _BinaryPredicate __binary_pred
)
4185 // concept requirements
4186 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4187 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4188 typename iterator_traits
<_ForwardIterator
>::value_type
,
4189 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4190 __glibcxx_requires_valid_range(__first
, __last
);
4192 return std::__adjacent_find(__first
, __last
,
4193 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4197 * @brief Count the number of copies of a value in a sequence.
4198 * @ingroup non_mutating_algorithms
4199 * @param __first An input iterator.
4200 * @param __last An input iterator.
4201 * @param __value The value to be counted.
4202 * @return The number of iterators @c i in the range @p [__first,__last)
4203 * for which @c *i == @p __value
4205 template<typename _InputIterator
, typename _Tp
>
4206 _GLIBCXX20_CONSTEXPR
4207 inline typename iterator_traits
<_InputIterator
>::difference_type
4208 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4210 // concept requirements
4211 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4212 __glibcxx_function_requires(_EqualOpConcept
<
4213 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4214 __glibcxx_requires_valid_range(__first
, __last
);
4216 return std::__count_if(__first
, __last
,
4217 __gnu_cxx::__ops::__iter_equals_val(__value
));
4221 * @brief Count the elements of a sequence for which a predicate is true.
4222 * @ingroup non_mutating_algorithms
4223 * @param __first An input iterator.
4224 * @param __last An input iterator.
4225 * @param __pred A predicate.
4226 * @return The number of iterators @c i in the range @p [__first,__last)
4227 * for which @p __pred(*i) is true.
4229 template<typename _InputIterator
, typename _Predicate
>
4230 _GLIBCXX20_CONSTEXPR
4231 inline typename iterator_traits
<_InputIterator
>::difference_type
4232 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4234 // concept requirements
4235 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4236 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4237 typename iterator_traits
<_InputIterator
>::value_type
>)
4238 __glibcxx_requires_valid_range(__first
, __last
);
4240 return std::__count_if(__first
, __last
,
4241 __gnu_cxx::__ops::__pred_iter(__pred
));
4245 * @brief Search a sequence for a matching sub-sequence.
4246 * @ingroup non_mutating_algorithms
4247 * @param __first1 A forward iterator.
4248 * @param __last1 A forward iterator.
4249 * @param __first2 A forward iterator.
4250 * @param __last2 A forward iterator.
4251 * @return The first iterator @c i in the range @p
4252 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4253 * *(__first2+N) for each @c N in the range @p
4254 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4256 * Searches the range @p [__first1,__last1) for a sub-sequence that
4257 * compares equal value-by-value with the sequence given by @p
4258 * [__first2,__last2) and returns an iterator to the first element
4259 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4262 * Because the sub-sequence must lie completely within the range @p
4263 * [__first1,__last1) it must start at a position less than @p
4264 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4265 * length of the sub-sequence.
4267 * This means that the returned iterator @c i will be in the range
4268 * @p [__first1,__last1-(__last2-__first2))
4270 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4271 _GLIBCXX20_CONSTEXPR
4272 inline _ForwardIterator1
4273 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4274 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4276 // concept requirements
4277 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4278 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4279 __glibcxx_function_requires(_EqualOpConcept
<
4280 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4281 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4282 __glibcxx_requires_valid_range(__first1
, __last1
);
4283 __glibcxx_requires_valid_range(__first2
, __last2
);
4285 return std::__search(__first1
, __last1
, __first2
, __last2
,
4286 __gnu_cxx::__ops::__iter_equal_to_iter());
4290 * @brief Search a sequence for a matching sub-sequence using a predicate.
4291 * @ingroup non_mutating_algorithms
4292 * @param __first1 A forward iterator.
4293 * @param __last1 A forward iterator.
4294 * @param __first2 A forward iterator.
4295 * @param __last2 A forward iterator.
4296 * @param __predicate A binary predicate.
4297 * @return The first iterator @c i in the range
4298 * @p [__first1,__last1-(__last2-__first2)) such that
4299 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4300 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4302 * Searches the range @p [__first1,__last1) for a sub-sequence that
4303 * compares equal value-by-value with the sequence given by @p
4304 * [__first2,__last2), using @p __predicate to determine equality,
4305 * and returns an iterator to the first element of the
4306 * sub-sequence, or @p __last1 if no such iterator exists.
4308 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4310 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4311 typename _BinaryPredicate
>
4312 _GLIBCXX20_CONSTEXPR
4313 inline _ForwardIterator1
4314 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4315 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4316 _BinaryPredicate __predicate
)
4318 // concept requirements
4319 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4320 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4321 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4322 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4323 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4324 __glibcxx_requires_valid_range(__first1
, __last1
);
4325 __glibcxx_requires_valid_range(__first2
, __last2
);
4327 return std::__search(__first1
, __last1
, __first2
, __last2
,
4328 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4332 * @brief Search a sequence for a number of consecutive values.
4333 * @ingroup non_mutating_algorithms
4334 * @param __first A forward iterator.
4335 * @param __last A forward iterator.
4336 * @param __count The number of consecutive values.
4337 * @param __val The value to find.
4338 * @return The first iterator @c i in the range @p
4339 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4340 * each @c N in the range @p [0,__count), or @p __last if no such
4343 * Searches the range @p [__first,__last) for @p count consecutive elements
4344 * equal to @p __val.
4346 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4347 _GLIBCXX20_CONSTEXPR
4348 inline _ForwardIterator
4349 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4350 _Integer __count
, const _Tp
& __val
)
4352 // concept requirements
4353 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4354 __glibcxx_function_requires(_EqualOpConcept
<
4355 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4356 __glibcxx_requires_valid_range(__first
, __last
);
4358 return std::__search_n(__first
, __last
, __count
,
4359 __gnu_cxx::__ops::__iter_equals_val(__val
));
4364 * @brief Search a sequence for a number of consecutive values using a
4366 * @ingroup non_mutating_algorithms
4367 * @param __first A forward iterator.
4368 * @param __last A forward iterator.
4369 * @param __count The number of consecutive values.
4370 * @param __val The value to find.
4371 * @param __binary_pred A binary predicate.
4372 * @return The first iterator @c i in the range @p
4373 * [__first,__last-__count) such that @p
4374 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4375 * @p [0,__count), or @p __last if no such iterator exists.
4377 * Searches the range @p [__first,__last) for @p __count
4378 * consecutive elements for which the predicate returns true.
4380 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4381 typename _BinaryPredicate
>
4382 _GLIBCXX20_CONSTEXPR
4383 inline _ForwardIterator
4384 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4385 _Integer __count
, const _Tp
& __val
,
4386 _BinaryPredicate __binary_pred
)
4388 // concept requirements
4389 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4390 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4391 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4392 __glibcxx_requires_valid_range(__first
, __last
);
4394 return std::__search_n(__first
, __last
, __count
,
4395 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4398 #if __cplusplus > 201402L
4399 /** @brief Search a sequence using a Searcher object.
4401 * @param __first A forward iterator.
4402 * @param __last A forward iterator.
4403 * @param __searcher A callable object.
4404 * @return @p __searcher(__first,__last).first
4406 template<typename _ForwardIterator
, typename _Searcher
>
4407 inline _ForwardIterator
4408 search(_ForwardIterator __first
, _ForwardIterator __last
,
4409 const _Searcher
& __searcher
)
4410 { return __searcher(__first
, __last
).first
; }
4414 * @brief Perform an operation on a sequence.
4415 * @ingroup mutating_algorithms
4416 * @param __first An input iterator.
4417 * @param __last An input iterator.
4418 * @param __result An output iterator.
4419 * @param __unary_op A unary operator.
4420 * @return An output iterator equal to @p __result+(__last-__first).
4422 * Applies the operator to each element in the input range and assigns
4423 * the results to successive elements of the output sequence.
4424 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4425 * range @p [0,__last-__first).
4427 * @p unary_op must not alter its argument.
4429 template<typename _InputIterator
, typename _OutputIterator
,
4430 typename _UnaryOperation
>
4431 _GLIBCXX20_CONSTEXPR
4433 transform(_InputIterator __first
, _InputIterator __last
,
4434 _OutputIterator __result
, _UnaryOperation __unary_op
)
4436 // concept requirements
4437 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4438 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4439 // "the type returned by a _UnaryOperation"
4440 __typeof__(__unary_op(*__first
))>)
4441 __glibcxx_requires_valid_range(__first
, __last
);
4443 for (; __first
!= __last
; ++__first
, (void)++__result
)
4444 *__result
= __unary_op(*__first
);
4449 * @brief Perform an operation on corresponding elements of two sequences.
4450 * @ingroup mutating_algorithms
4451 * @param __first1 An input iterator.
4452 * @param __last1 An input iterator.
4453 * @param __first2 An input iterator.
4454 * @param __result An output iterator.
4455 * @param __binary_op A binary operator.
4456 * @return An output iterator equal to @p result+(last-first).
4458 * Applies the operator to the corresponding elements in the two
4459 * input ranges and assigns the results to successive elements of the
4462 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4463 * @c N in the range @p [0,__last1-__first1).
4465 * @p binary_op must not alter either of its arguments.
4467 template<typename _InputIterator1
, typename _InputIterator2
,
4468 typename _OutputIterator
, typename _BinaryOperation
>
4469 _GLIBCXX20_CONSTEXPR
4471 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4472 _InputIterator2 __first2
, _OutputIterator __result
,
4473 _BinaryOperation __binary_op
)
4475 // concept requirements
4476 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4477 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4478 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4479 // "the type returned by a _BinaryOperation"
4480 __typeof__(__binary_op(*__first1
,*__first2
))>)
4481 __glibcxx_requires_valid_range(__first1
, __last1
);
4483 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4484 *__result
= __binary_op(*__first1
, *__first2
);
4489 * @brief Replace each occurrence of one value in a sequence with another
4491 * @ingroup mutating_algorithms
4492 * @param __first A forward iterator.
4493 * @param __last A forward iterator.
4494 * @param __old_value The value to be replaced.
4495 * @param __new_value The replacement value.
4496 * @return replace() returns no value.
4498 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4499 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4501 template<typename _ForwardIterator
, typename _Tp
>
4502 _GLIBCXX20_CONSTEXPR
4504 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4505 const _Tp
& __old_value
, const _Tp
& __new_value
)
4507 // concept requirements
4508 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4510 __glibcxx_function_requires(_EqualOpConcept
<
4511 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4512 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4513 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4514 __glibcxx_requires_valid_range(__first
, __last
);
4516 for (; __first
!= __last
; ++__first
)
4517 if (*__first
== __old_value
)
4518 *__first
= __new_value
;
4522 * @brief Replace each value in a sequence for which a predicate returns
4523 * true with another value.
4524 * @ingroup mutating_algorithms
4525 * @param __first A forward iterator.
4526 * @param __last A forward iterator.
4527 * @param __pred A predicate.
4528 * @param __new_value The replacement value.
4529 * @return replace_if() returns no value.
4531 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4532 * is true then the assignment @c *i = @p __new_value is performed.
4534 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4535 _GLIBCXX20_CONSTEXPR
4537 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4538 _Predicate __pred
, const _Tp
& __new_value
)
4540 // concept requirements
4541 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4543 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4544 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4545 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4546 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4547 __glibcxx_requires_valid_range(__first
, __last
);
4549 for (; __first
!= __last
; ++__first
)
4550 if (__pred(*__first
))
4551 *__first
= __new_value
;
4555 * @brief Assign the result of a function object to each value in a
4557 * @ingroup mutating_algorithms
4558 * @param __first A forward iterator.
4559 * @param __last A forward iterator.
4560 * @param __gen A function object taking no arguments and returning
4561 * std::iterator_traits<_ForwardIterator>::value_type
4562 * @return generate() returns no value.
4564 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4565 * @p [__first,__last).
4567 template<typename _ForwardIterator
, typename _Generator
>
4568 _GLIBCXX20_CONSTEXPR
4570 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4573 // concept requirements
4574 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4575 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4576 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4577 __glibcxx_requires_valid_range(__first
, __last
);
4579 for (; __first
!= __last
; ++__first
)
4584 * @brief Assign the result of a function object to each value in a
4586 * @ingroup mutating_algorithms
4587 * @param __first A forward iterator.
4588 * @param __n The length of the sequence.
4589 * @param __gen A function object taking no arguments and returning
4590 * std::iterator_traits<_ForwardIterator>::value_type
4591 * @return The end of the sequence, @p __first+__n
4593 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4594 * @p [__first,__first+__n).
4596 * If @p __n is negative, the function does nothing and returns @p __first.
4598 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4599 // DR 865. More algorithms that throw away information
4600 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4601 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4602 _GLIBCXX20_CONSTEXPR
4604 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4606 // concept requirements
4607 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4608 // "the type returned by a _Generator"
4609 __typeof__(__gen())>)
4611 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4612 for (_IntSize __niter
= std::__size_to_integer(__n
);
4613 __niter
> 0; --__niter
, (void) ++__first
)
4619 * @brief Copy a sequence, removing consecutive duplicate values.
4620 * @ingroup mutating_algorithms
4621 * @param __first An input iterator.
4622 * @param __last An input iterator.
4623 * @param __result An output iterator.
4624 * @return An iterator designating the end of the resulting sequence.
4626 * Copies each element in the range @p [__first,__last) to the range
4627 * beginning at @p __result, except that only the first element is copied
4628 * from groups of consecutive elements that compare equal.
4629 * unique_copy() is stable, so the relative order of elements that are
4630 * copied is unchanged.
4632 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4633 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4635 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4636 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4639 template<typename _InputIterator
, typename _OutputIterator
>
4640 _GLIBCXX20_CONSTEXPR
4641 inline _OutputIterator
4642 unique_copy(_InputIterator __first
, _InputIterator __last
,
4643 _OutputIterator __result
)
4645 // concept requirements
4646 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4647 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4648 typename iterator_traits
<_InputIterator
>::value_type
>)
4649 __glibcxx_function_requires(_EqualityComparableConcept
<
4650 typename iterator_traits
<_InputIterator
>::value_type
>)
4651 __glibcxx_requires_valid_range(__first
, __last
);
4653 if (__first
== __last
)
4655 return std::__unique_copy(__first
, __last
, __result
,
4656 __gnu_cxx::__ops::__iter_equal_to_iter(),
4657 std::__iterator_category(__first
),
4658 std::__iterator_category(__result
));
4662 * @brief Copy a sequence, removing consecutive values using a predicate.
4663 * @ingroup mutating_algorithms
4664 * @param __first An input iterator.
4665 * @param __last An input iterator.
4666 * @param __result An output iterator.
4667 * @param __binary_pred A binary predicate.
4668 * @return An iterator designating the end of the resulting sequence.
4670 * Copies each element in the range @p [__first,__last) to the range
4671 * beginning at @p __result, except that only the first element is copied
4672 * from groups of consecutive elements for which @p __binary_pred returns
4674 * unique_copy() is stable, so the relative order of elements that are
4675 * copied is unchanged.
4677 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4678 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4680 template<typename _InputIterator
, typename _OutputIterator
,
4681 typename _BinaryPredicate
>
4682 _GLIBCXX20_CONSTEXPR
4683 inline _OutputIterator
4684 unique_copy(_InputIterator __first
, _InputIterator __last
,
4685 _OutputIterator __result
,
4686 _BinaryPredicate __binary_pred
)
4688 // concept requirements -- predicates checked later
4689 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4690 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4691 typename iterator_traits
<_InputIterator
>::value_type
>)
4692 __glibcxx_requires_valid_range(__first
, __last
);
4694 if (__first
== __last
)
4696 return std::__unique_copy(__first
, __last
, __result
,
4697 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4698 std::__iterator_category(__first
),
4699 std::__iterator_category(__result
));
4704 * @brief Randomly shuffle the elements of a sequence.
4705 * @ingroup mutating_algorithms
4706 * @param __first A forward iterator.
4707 * @param __last A forward iterator.
4710 * Reorder the elements in the range @p [__first,__last) using a random
4711 * distribution, so that every possible ordering of the sequence is
4714 template<typename _RandomAccessIterator
>
4716 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4718 // concept requirements
4719 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4720 _RandomAccessIterator
>)
4721 __glibcxx_requires_valid_range(__first
, __last
);
4723 if (__first
!= __last
)
4724 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4726 // XXX rand() % N is not uniformly distributed
4727 _RandomAccessIterator __j
= __first
4728 + std::rand() % ((__i
- __first
) + 1);
4730 std::iter_swap(__i
, __j
);
4736 * @brief Shuffle the elements of a sequence using a random number
4738 * @ingroup mutating_algorithms
4739 * @param __first A forward iterator.
4740 * @param __last A forward iterator.
4741 * @param __rand The RNG functor or function.
4744 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4745 * provide a random distribution. Calling @p __rand(N) for a positive
4746 * integer @p N should return a randomly chosen integer from the
4749 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4751 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4752 #if __cplusplus >= 201103L
4753 _RandomNumberGenerator
&& __rand
)
4755 _RandomNumberGenerator
& __rand
)
4758 // concept requirements
4759 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4760 _RandomAccessIterator
>)
4761 __glibcxx_requires_valid_range(__first
, __last
);
4763 if (__first
== __last
)
4765 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4767 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4769 std::iter_swap(__i
, __j
);
4775 * @brief Move elements for which a predicate is true to the beginning
4777 * @ingroup mutating_algorithms
4778 * @param __first A forward iterator.
4779 * @param __last A forward iterator.
4780 * @param __pred A predicate functor.
4781 * @return An iterator @p middle such that @p __pred(i) is true for each
4782 * iterator @p i in the range @p [__first,middle) and false for each @p i
4783 * in the range @p [middle,__last).
4785 * @p __pred must not modify its operand. @p partition() does not preserve
4786 * the relative ordering of elements in each group, use
4787 * @p stable_partition() if this is needed.
4789 template<typename _ForwardIterator
, typename _Predicate
>
4790 _GLIBCXX20_CONSTEXPR
4791 inline _ForwardIterator
4792 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4795 // concept requirements
4796 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4798 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4799 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4800 __glibcxx_requires_valid_range(__first
, __last
);
4802 return std::__partition(__first
, __last
, __pred
,
4803 std::__iterator_category(__first
));
4808 * @brief Sort the smallest elements of a sequence.
4809 * @ingroup sorting_algorithms
4810 * @param __first An iterator.
4811 * @param __middle Another iterator.
4812 * @param __last Another iterator.
4815 * Sorts the smallest @p (__middle-__first) elements in the range
4816 * @p [first,last) and moves them to the range @p [__first,__middle). The
4817 * order of the remaining elements in the range @p [__middle,__last) is
4819 * After the sort if @e i and @e j are iterators in the range
4820 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4821 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4823 template<typename _RandomAccessIterator
>
4824 _GLIBCXX20_CONSTEXPR
4826 partial_sort(_RandomAccessIterator __first
,
4827 _RandomAccessIterator __middle
,
4828 _RandomAccessIterator __last
)
4830 // concept requirements
4831 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4832 _RandomAccessIterator
>)
4833 __glibcxx_function_requires(_LessThanComparableConcept
<
4834 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4835 __glibcxx_requires_valid_range(__first
, __middle
);
4836 __glibcxx_requires_valid_range(__middle
, __last
);
4837 __glibcxx_requires_irreflexive(__first
, __last
);
4839 std::__partial_sort(__first
, __middle
, __last
,
4840 __gnu_cxx::__ops::__iter_less_iter());
4844 * @brief Sort the smallest elements of a sequence using a predicate
4846 * @ingroup sorting_algorithms
4847 * @param __first An iterator.
4848 * @param __middle Another iterator.
4849 * @param __last Another iterator.
4850 * @param __comp A comparison functor.
4853 * Sorts the smallest @p (__middle-__first) elements in the range
4854 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4855 * order of the remaining elements in the range @p [__middle,__last) is
4857 * After the sort if @e i and @e j are iterators in the range
4858 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4859 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4862 template<typename _RandomAccessIterator
, typename _Compare
>
4863 _GLIBCXX20_CONSTEXPR
4865 partial_sort(_RandomAccessIterator __first
,
4866 _RandomAccessIterator __middle
,
4867 _RandomAccessIterator __last
,
4870 // concept requirements
4871 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4872 _RandomAccessIterator
>)
4873 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4874 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4875 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4876 __glibcxx_requires_valid_range(__first
, __middle
);
4877 __glibcxx_requires_valid_range(__middle
, __last
);
4878 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4880 std::__partial_sort(__first
, __middle
, __last
,
4881 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4885 * @brief Sort a sequence just enough to find a particular position.
4886 * @ingroup sorting_algorithms
4887 * @param __first An iterator.
4888 * @param __nth Another iterator.
4889 * @param __last Another iterator.
4892 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4893 * is the same element that would have been in that position had the
4894 * whole sequence been sorted. The elements either side of @p *__nth are
4895 * not completely sorted, but for any iterator @e i in the range
4896 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4897 * holds that *j < *i is false.
4899 template<typename _RandomAccessIterator
>
4900 _GLIBCXX20_CONSTEXPR
4902 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4903 _RandomAccessIterator __last
)
4905 // concept requirements
4906 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4907 _RandomAccessIterator
>)
4908 __glibcxx_function_requires(_LessThanComparableConcept
<
4909 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4910 __glibcxx_requires_valid_range(__first
, __nth
);
4911 __glibcxx_requires_valid_range(__nth
, __last
);
4912 __glibcxx_requires_irreflexive(__first
, __last
);
4914 if (__first
== __last
|| __nth
== __last
)
4917 std::__introselect(__first
, __nth
, __last
,
4918 std::__lg(__last
- __first
) * 2,
4919 __gnu_cxx::__ops::__iter_less_iter());
4923 * @brief Sort a sequence just enough to find a particular position
4924 * using a predicate for comparison.
4925 * @ingroup sorting_algorithms
4926 * @param __first An iterator.
4927 * @param __nth Another iterator.
4928 * @param __last Another iterator.
4929 * @param __comp A comparison functor.
4932 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4933 * is the same element that would have been in that position had the
4934 * whole sequence been sorted. The elements either side of @p *__nth are
4935 * not completely sorted, but for any iterator @e i in the range
4936 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4937 * holds that @p __comp(*j,*i) is false.
4939 template<typename _RandomAccessIterator
, typename _Compare
>
4940 _GLIBCXX20_CONSTEXPR
4942 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4943 _RandomAccessIterator __last
, _Compare __comp
)
4945 // concept requirements
4946 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4947 _RandomAccessIterator
>)
4948 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4949 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4950 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4951 __glibcxx_requires_valid_range(__first
, __nth
);
4952 __glibcxx_requires_valid_range(__nth
, __last
);
4953 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4955 if (__first
== __last
|| __nth
== __last
)
4958 std::__introselect(__first
, __nth
, __last
,
4959 std::__lg(__last
- __first
) * 2,
4960 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4964 * @brief Sort the elements of a sequence.
4965 * @ingroup sorting_algorithms
4966 * @param __first An iterator.
4967 * @param __last Another iterator.
4970 * Sorts the elements in the range @p [__first,__last) in ascending order,
4971 * such that for each iterator @e i in the range @p [__first,__last-1),
4972 * *(i+1)<*i is false.
4974 * The relative ordering of equivalent elements is not preserved, use
4975 * @p stable_sort() if this is needed.
4977 template<typename _RandomAccessIterator
>
4978 _GLIBCXX20_CONSTEXPR
4980 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4982 // concept requirements
4983 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4984 _RandomAccessIterator
>)
4985 __glibcxx_function_requires(_LessThanComparableConcept
<
4986 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4987 __glibcxx_requires_valid_range(__first
, __last
);
4988 __glibcxx_requires_irreflexive(__first
, __last
);
4990 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4994 * @brief Sort the elements of a sequence using a predicate for comparison.
4995 * @ingroup sorting_algorithms
4996 * @param __first An iterator.
4997 * @param __last Another iterator.
4998 * @param __comp A comparison functor.
5001 * Sorts the elements in the range @p [__first,__last) in ascending order,
5002 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
5003 * range @p [__first,__last-1).
5005 * The relative ordering of equivalent elements is not preserved, use
5006 * @p stable_sort() if this is needed.
5008 template<typename _RandomAccessIterator
, typename _Compare
>
5009 _GLIBCXX20_CONSTEXPR
5011 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5014 // concept requirements
5015 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5016 _RandomAccessIterator
>)
5017 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5018 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5019 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5020 __glibcxx_requires_valid_range(__first
, __last
);
5021 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5023 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5026 template<typename _InputIterator1
, typename _InputIterator2
,
5027 typename _OutputIterator
, typename _Compare
>
5028 _GLIBCXX20_CONSTEXPR
5030 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5031 _InputIterator2 __first2
, _InputIterator2 __last2
,
5032 _OutputIterator __result
, _Compare __comp
)
5034 while (__first1
!= __last1
&& __first2
!= __last2
)
5036 if (__comp(__first2
, __first1
))
5038 *__result
= *__first2
;
5043 *__result
= *__first1
;
5048 return std::copy(__first2
, __last2
,
5049 std::copy(__first1
, __last1
, __result
));
5053 * @brief Merges two sorted ranges.
5054 * @ingroup sorting_algorithms
5055 * @param __first1 An iterator.
5056 * @param __first2 Another iterator.
5057 * @param __last1 Another iterator.
5058 * @param __last2 Another iterator.
5059 * @param __result An iterator pointing to the end of the merged range.
5060 * @return An output iterator equal to @p __result + (__last1 - __first1)
5061 * + (__last2 - __first2).
5063 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5064 * the sorted range @p [__result, __result + (__last1-__first1) +
5065 * (__last2-__first2)). Both input ranges must be sorted, and the
5066 * output range must not overlap with either of the input ranges.
5067 * The sort is @e stable, that is, for equivalent elements in the
5068 * two ranges, elements from the first range will always come
5069 * before elements from the second.
5071 template<typename _InputIterator1
, typename _InputIterator2
,
5072 typename _OutputIterator
>
5073 _GLIBCXX20_CONSTEXPR
5074 inline _OutputIterator
5075 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5076 _InputIterator2 __first2
, _InputIterator2 __last2
,
5077 _OutputIterator __result
)
5079 // concept requirements
5080 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5081 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5082 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5083 typename iterator_traits
<_InputIterator1
>::value_type
>)
5084 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5085 typename iterator_traits
<_InputIterator2
>::value_type
>)
5086 __glibcxx_function_requires(_LessThanOpConcept
<
5087 typename iterator_traits
<_InputIterator2
>::value_type
,
5088 typename iterator_traits
<_InputIterator1
>::value_type
>)
5089 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5090 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5091 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5092 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5094 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5095 __first2
, __last2
, __result
,
5096 __gnu_cxx::__ops::__iter_less_iter());
5100 * @brief Merges two sorted ranges.
5101 * @ingroup sorting_algorithms
5102 * @param __first1 An iterator.
5103 * @param __first2 Another iterator.
5104 * @param __last1 Another iterator.
5105 * @param __last2 Another iterator.
5106 * @param __result An iterator pointing to the end of the merged range.
5107 * @param __comp A functor to use for comparisons.
5108 * @return An output iterator equal to @p __result + (__last1 - __first1)
5109 * + (__last2 - __first2).
5111 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5112 * the sorted range @p [__result, __result + (__last1-__first1) +
5113 * (__last2-__first2)). Both input ranges must be sorted, and the
5114 * output range must not overlap with either of the input ranges.
5115 * The sort is @e stable, that is, for equivalent elements in the
5116 * two ranges, elements from the first range will always come
5117 * before elements from the second.
5119 * The comparison function should have the same effects on ordering as
5120 * the function used for the initial sort.
5122 template<typename _InputIterator1
, typename _InputIterator2
,
5123 typename _OutputIterator
, typename _Compare
>
5124 _GLIBCXX20_CONSTEXPR
5125 inline _OutputIterator
5126 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5127 _InputIterator2 __first2
, _InputIterator2 __last2
,
5128 _OutputIterator __result
, _Compare __comp
)
5130 // concept requirements
5131 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5132 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5133 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5134 typename iterator_traits
<_InputIterator1
>::value_type
>)
5135 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5136 typename iterator_traits
<_InputIterator2
>::value_type
>)
5137 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5138 typename iterator_traits
<_InputIterator2
>::value_type
,
5139 typename iterator_traits
<_InputIterator1
>::value_type
>)
5140 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5141 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5142 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5143 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5145 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5146 __first2
, __last2
, __result
,
5147 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5150 template<typename _RandomAccessIterator
, typename _Compare
>
5152 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5155 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5157 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5160 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5161 _TmpBuf
__buf(__first
, std::distance(__first
, __last
));
5163 if (__buf
.begin() == 0)
5164 std::__inplace_stable_sort(__first
, __last
, __comp
);
5166 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5167 _DistanceType(__buf
.size()), __comp
);
5171 * @brief Sort the elements of a sequence, preserving the relative order
5172 * of equivalent elements.
5173 * @ingroup sorting_algorithms
5174 * @param __first An iterator.
5175 * @param __last Another iterator.
5178 * Sorts the elements in the range @p [__first,__last) in ascending order,
5179 * such that for each iterator @p i in the range @p [__first,__last-1),
5180 * @p *(i+1)<*i is false.
5182 * The relative ordering of equivalent elements is preserved, so any two
5183 * elements @p x and @p y in the range @p [__first,__last) such that
5184 * @p x<y is false and @p y<x is false will have the same relative
5185 * ordering after calling @p stable_sort().
5187 template<typename _RandomAccessIterator
>
5189 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5191 // concept requirements
5192 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5193 _RandomAccessIterator
>)
5194 __glibcxx_function_requires(_LessThanComparableConcept
<
5195 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5196 __glibcxx_requires_valid_range(__first
, __last
);
5197 __glibcxx_requires_irreflexive(__first
, __last
);
5199 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5200 __gnu_cxx::__ops::__iter_less_iter());
5204 * @brief Sort the elements of a sequence using a predicate for comparison,
5205 * preserving the relative order of equivalent elements.
5206 * @ingroup sorting_algorithms
5207 * @param __first An iterator.
5208 * @param __last Another iterator.
5209 * @param __comp A comparison functor.
5212 * Sorts the elements in the range @p [__first,__last) in ascending order,
5213 * such that for each iterator @p i in the range @p [__first,__last-1),
5214 * @p __comp(*(i+1),*i) is false.
5216 * The relative ordering of equivalent elements is preserved, so any two
5217 * elements @p x and @p y in the range @p [__first,__last) such that
5218 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5219 * relative ordering after calling @p stable_sort().
5221 template<typename _RandomAccessIterator
, typename _Compare
>
5223 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5226 // concept requirements
5227 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5228 _RandomAccessIterator
>)
5229 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5230 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5231 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5232 __glibcxx_requires_valid_range(__first
, __last
);
5233 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5235 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5236 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5239 template<typename _InputIterator1
, typename _InputIterator2
,
5240 typename _OutputIterator
,
5242 _GLIBCXX20_CONSTEXPR
5244 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5245 _InputIterator2 __first2
, _InputIterator2 __last2
,
5246 _OutputIterator __result
, _Compare __comp
)
5248 while (__first1
!= __last1
&& __first2
!= __last2
)
5250 if (__comp(__first1
, __first2
))
5252 *__result
= *__first1
;
5255 else if (__comp(__first2
, __first1
))
5257 *__result
= *__first2
;
5262 *__result
= *__first1
;
5268 return std::copy(__first2
, __last2
,
5269 std::copy(__first1
, __last1
, __result
));
5273 * @brief Return the union of two sorted ranges.
5274 * @ingroup set_algorithms
5275 * @param __first1 Start of first range.
5276 * @param __last1 End of first range.
5277 * @param __first2 Start of second range.
5278 * @param __last2 End of second range.
5279 * @param __result Start of output range.
5280 * @return End of the output range.
5281 * @ingroup set_algorithms
5283 * This operation iterates over both ranges, copying elements present in
5284 * each range in order to the output range. Iterators increment for each
5285 * range. When the current element of one range is less than the other,
5286 * that element is copied and the iterator advanced. If an element is
5287 * contained in both ranges, the element from the first range is copied and
5288 * both ranges advance. The output range may not overlap either input
5291 template<typename _InputIterator1
, typename _InputIterator2
,
5292 typename _OutputIterator
>
5293 _GLIBCXX20_CONSTEXPR
5294 inline _OutputIterator
5295 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5296 _InputIterator2 __first2
, _InputIterator2 __last2
,
5297 _OutputIterator __result
)
5299 // concept requirements
5300 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5301 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5302 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5303 typename iterator_traits
<_InputIterator1
>::value_type
>)
5304 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5305 typename iterator_traits
<_InputIterator2
>::value_type
>)
5306 __glibcxx_function_requires(_LessThanOpConcept
<
5307 typename iterator_traits
<_InputIterator1
>::value_type
,
5308 typename iterator_traits
<_InputIterator2
>::value_type
>)
5309 __glibcxx_function_requires(_LessThanOpConcept
<
5310 typename iterator_traits
<_InputIterator2
>::value_type
,
5311 typename iterator_traits
<_InputIterator1
>::value_type
>)
5312 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5313 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5314 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5315 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5317 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5318 __first2
, __last2
, __result
,
5319 __gnu_cxx::__ops::__iter_less_iter());
5323 * @brief Return the union of two sorted ranges using a comparison functor.
5324 * @ingroup set_algorithms
5325 * @param __first1 Start of first range.
5326 * @param __last1 End of first range.
5327 * @param __first2 Start of second range.
5328 * @param __last2 End of second range.
5329 * @param __result Start of output range.
5330 * @param __comp The comparison functor.
5331 * @return End of the output range.
5332 * @ingroup set_algorithms
5334 * This operation iterates over both ranges, copying elements present in
5335 * each range in order to the output range. Iterators increment for each
5336 * range. When the current element of one range is less than the other
5337 * according to @p __comp, that element is copied and the iterator advanced.
5338 * If an equivalent element according to @p __comp is contained in both
5339 * ranges, the element from the first range is copied and both ranges
5340 * advance. The output range may not overlap either input range.
5342 template<typename _InputIterator1
, typename _InputIterator2
,
5343 typename _OutputIterator
, typename _Compare
>
5344 _GLIBCXX20_CONSTEXPR
5345 inline _OutputIterator
5346 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5347 _InputIterator2 __first2
, _InputIterator2 __last2
,
5348 _OutputIterator __result
, _Compare __comp
)
5350 // concept requirements
5351 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5352 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5353 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5354 typename iterator_traits
<_InputIterator1
>::value_type
>)
5355 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5356 typename iterator_traits
<_InputIterator2
>::value_type
>)
5357 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5358 typename iterator_traits
<_InputIterator1
>::value_type
,
5359 typename iterator_traits
<_InputIterator2
>::value_type
>)
5360 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5361 typename iterator_traits
<_InputIterator2
>::value_type
,
5362 typename iterator_traits
<_InputIterator1
>::value_type
>)
5363 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5364 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5365 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5366 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5368 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5369 __first2
, __last2
, __result
,
5370 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5373 template<typename _InputIterator1
, typename _InputIterator2
,
5374 typename _OutputIterator
,
5376 _GLIBCXX20_CONSTEXPR
5378 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5379 _InputIterator2 __first2
, _InputIterator2 __last2
,
5380 _OutputIterator __result
, _Compare __comp
)
5382 while (__first1
!= __last1
&& __first2
!= __last2
)
5383 if (__comp(__first1
, __first2
))
5385 else if (__comp(__first2
, __first1
))
5389 *__result
= *__first1
;
5398 * @brief Return the intersection of two sorted ranges.
5399 * @ingroup set_algorithms
5400 * @param __first1 Start of first range.
5401 * @param __last1 End of first range.
5402 * @param __first2 Start of second range.
5403 * @param __last2 End of second range.
5404 * @param __result Start of output range.
5405 * @return End of the output range.
5406 * @ingroup set_algorithms
5408 * This operation iterates over both ranges, copying elements present in
5409 * both ranges in order to the output range. Iterators increment for each
5410 * range. When the current element of one range is less than the other,
5411 * that iterator advances. If an element is contained in both ranges, the
5412 * element from the first range is copied and both ranges advance. The
5413 * output range may not overlap either input range.
5415 template<typename _InputIterator1
, typename _InputIterator2
,
5416 typename _OutputIterator
>
5417 _GLIBCXX20_CONSTEXPR
5418 inline _OutputIterator
5419 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5420 _InputIterator2 __first2
, _InputIterator2 __last2
,
5421 _OutputIterator __result
)
5423 // concept requirements
5424 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5425 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5426 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5427 typename iterator_traits
<_InputIterator1
>::value_type
>)
5428 __glibcxx_function_requires(_LessThanOpConcept
<
5429 typename iterator_traits
<_InputIterator1
>::value_type
,
5430 typename iterator_traits
<_InputIterator2
>::value_type
>)
5431 __glibcxx_function_requires(_LessThanOpConcept
<
5432 typename iterator_traits
<_InputIterator2
>::value_type
,
5433 typename iterator_traits
<_InputIterator1
>::value_type
>)
5434 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5435 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5436 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5437 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5439 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5440 __first2
, __last2
, __result
,
5441 __gnu_cxx::__ops::__iter_less_iter());
5445 * @brief Return the intersection of two sorted ranges using comparison
5447 * @ingroup set_algorithms
5448 * @param __first1 Start of first range.
5449 * @param __last1 End of first range.
5450 * @param __first2 Start of second range.
5451 * @param __last2 End of second range.
5452 * @param __result Start of output range.
5453 * @param __comp The comparison functor.
5454 * @return End of the output range.
5455 * @ingroup set_algorithms
5457 * This operation iterates over both ranges, copying elements present in
5458 * both ranges in order to the output range. Iterators increment for each
5459 * range. When the current element of one range is less than the other
5460 * according to @p __comp, that iterator advances. If an element is
5461 * contained in both ranges according to @p __comp, the element from the
5462 * first range is copied and both ranges advance. The output range may not
5463 * overlap either input range.
5465 template<typename _InputIterator1
, typename _InputIterator2
,
5466 typename _OutputIterator
, typename _Compare
>
5467 _GLIBCXX20_CONSTEXPR
5468 inline _OutputIterator
5469 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5470 _InputIterator2 __first2
, _InputIterator2 __last2
,
5471 _OutputIterator __result
, _Compare __comp
)
5473 // concept requirements
5474 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5475 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5476 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5477 typename iterator_traits
<_InputIterator1
>::value_type
>)
5478 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5479 typename iterator_traits
<_InputIterator1
>::value_type
,
5480 typename iterator_traits
<_InputIterator2
>::value_type
>)
5481 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5482 typename iterator_traits
<_InputIterator2
>::value_type
,
5483 typename iterator_traits
<_InputIterator1
>::value_type
>)
5484 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5485 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5486 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5487 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5489 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5490 __first2
, __last2
, __result
,
5491 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5494 template<typename _InputIterator1
, typename _InputIterator2
,
5495 typename _OutputIterator
,
5497 _GLIBCXX20_CONSTEXPR
5499 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5500 _InputIterator2 __first2
, _InputIterator2 __last2
,
5501 _OutputIterator __result
, _Compare __comp
)
5503 while (__first1
!= __last1
&& __first2
!= __last2
)
5504 if (__comp(__first1
, __first2
))
5506 *__result
= *__first1
;
5510 else if (__comp(__first2
, __first1
))
5517 return std::copy(__first1
, __last1
, __result
);
5521 * @brief Return the difference of two sorted ranges.
5522 * @ingroup set_algorithms
5523 * @param __first1 Start of first range.
5524 * @param __last1 End of first range.
5525 * @param __first2 Start of second range.
5526 * @param __last2 End of second range.
5527 * @param __result Start of output range.
5528 * @return End of the output range.
5529 * @ingroup set_algorithms
5531 * This operation iterates over both ranges, copying elements present in
5532 * the first range but not the second in order to the output range.
5533 * Iterators increment for each range. When the current element of the
5534 * first range is less than the second, that element is copied and the
5535 * iterator advances. If the current element of the second range is less,
5536 * the iterator advances, but no element is copied. If an element is
5537 * contained in both ranges, no elements are copied and both ranges
5538 * advance. The output range may not overlap either input range.
5540 template<typename _InputIterator1
, typename _InputIterator2
,
5541 typename _OutputIterator
>
5542 _GLIBCXX20_CONSTEXPR
5543 inline _OutputIterator
5544 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5545 _InputIterator2 __first2
, _InputIterator2 __last2
,
5546 _OutputIterator __result
)
5548 // concept requirements
5549 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5550 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5551 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5552 typename iterator_traits
<_InputIterator1
>::value_type
>)
5553 __glibcxx_function_requires(_LessThanOpConcept
<
5554 typename iterator_traits
<_InputIterator1
>::value_type
,
5555 typename iterator_traits
<_InputIterator2
>::value_type
>)
5556 __glibcxx_function_requires(_LessThanOpConcept
<
5557 typename iterator_traits
<_InputIterator2
>::value_type
,
5558 typename iterator_traits
<_InputIterator1
>::value_type
>)
5559 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5560 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5561 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5562 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5564 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5565 __first2
, __last2
, __result
,
5566 __gnu_cxx::__ops::__iter_less_iter());
5570 * @brief Return the difference of two sorted ranges using comparison
5572 * @ingroup set_algorithms
5573 * @param __first1 Start of first range.
5574 * @param __last1 End of first range.
5575 * @param __first2 Start of second range.
5576 * @param __last2 End of second range.
5577 * @param __result Start of output range.
5578 * @param __comp The comparison functor.
5579 * @return End of the output range.
5580 * @ingroup set_algorithms
5582 * This operation iterates over both ranges, copying elements present in
5583 * the first range but not the second in order to the output range.
5584 * Iterators increment for each range. When the current element of the
5585 * first range is less than the second according to @p __comp, that element
5586 * is copied and the iterator advances. If the current element of the
5587 * second range is less, no element is copied and the iterator advances.
5588 * If an element is contained in both ranges according to @p __comp, no
5589 * elements are copied and both ranges advance. The output range may not
5590 * overlap either input range.
5592 template<typename _InputIterator1
, typename _InputIterator2
,
5593 typename _OutputIterator
, typename _Compare
>
5594 _GLIBCXX20_CONSTEXPR
5595 inline _OutputIterator
5596 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5597 _InputIterator2 __first2
, _InputIterator2 __last2
,
5598 _OutputIterator __result
, _Compare __comp
)
5600 // concept requirements
5601 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5602 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5603 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5604 typename iterator_traits
<_InputIterator1
>::value_type
>)
5605 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5606 typename iterator_traits
<_InputIterator1
>::value_type
,
5607 typename iterator_traits
<_InputIterator2
>::value_type
>)
5608 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5609 typename iterator_traits
<_InputIterator2
>::value_type
,
5610 typename iterator_traits
<_InputIterator1
>::value_type
>)
5611 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5612 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5613 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5614 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5616 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5617 __first2
, __last2
, __result
,
5618 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5621 template<typename _InputIterator1
, typename _InputIterator2
,
5622 typename _OutputIterator
,
5624 _GLIBCXX20_CONSTEXPR
5626 __set_symmetric_difference(_InputIterator1 __first1
,
5627 _InputIterator1 __last1
,
5628 _InputIterator2 __first2
,
5629 _InputIterator2 __last2
,
5630 _OutputIterator __result
,
5633 while (__first1
!= __last1
&& __first2
!= __last2
)
5634 if (__comp(__first1
, __first2
))
5636 *__result
= *__first1
;
5640 else if (__comp(__first2
, __first1
))
5642 *__result
= *__first2
;
5651 return std::copy(__first2
, __last2
,
5652 std::copy(__first1
, __last1
, __result
));
5656 * @brief Return the symmetric difference of two sorted ranges.
5657 * @ingroup set_algorithms
5658 * @param __first1 Start of first range.
5659 * @param __last1 End of first range.
5660 * @param __first2 Start of second range.
5661 * @param __last2 End of second range.
5662 * @param __result Start of output range.
5663 * @return End of the output range.
5664 * @ingroup set_algorithms
5666 * This operation iterates over both ranges, copying elements present in
5667 * one range but not the other in order to the output range. Iterators
5668 * increment for each range. When the current element of one range is less
5669 * than the other, that element is copied and the iterator advances. If an
5670 * element is contained in both ranges, no elements are copied and both
5671 * ranges advance. The output range may not overlap either input range.
5673 template<typename _InputIterator1
, typename _InputIterator2
,
5674 typename _OutputIterator
>
5675 _GLIBCXX20_CONSTEXPR
5676 inline _OutputIterator
5677 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5678 _InputIterator2 __first2
, _InputIterator2 __last2
,
5679 _OutputIterator __result
)
5681 // concept requirements
5682 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5683 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5684 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5685 typename iterator_traits
<_InputIterator1
>::value_type
>)
5686 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5687 typename iterator_traits
<_InputIterator2
>::value_type
>)
5688 __glibcxx_function_requires(_LessThanOpConcept
<
5689 typename iterator_traits
<_InputIterator1
>::value_type
,
5690 typename iterator_traits
<_InputIterator2
>::value_type
>)
5691 __glibcxx_function_requires(_LessThanOpConcept
<
5692 typename iterator_traits
<_InputIterator2
>::value_type
,
5693 typename iterator_traits
<_InputIterator1
>::value_type
>)
5694 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5695 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5696 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5697 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5699 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5700 __first2
, __last2
, __result
,
5701 __gnu_cxx::__ops::__iter_less_iter());
5705 * @brief Return the symmetric difference of two sorted ranges using
5706 * comparison functor.
5707 * @ingroup set_algorithms
5708 * @param __first1 Start of first range.
5709 * @param __last1 End of first range.
5710 * @param __first2 Start of second range.
5711 * @param __last2 End of second range.
5712 * @param __result Start of output range.
5713 * @param __comp The comparison functor.
5714 * @return End of the output range.
5715 * @ingroup set_algorithms
5717 * This operation iterates over both ranges, copying elements present in
5718 * one range but not the other in order to the output range. Iterators
5719 * increment for each range. When the current element of one range is less
5720 * than the other according to @p comp, that element is copied and the
5721 * iterator advances. If an element is contained in both ranges according
5722 * to @p __comp, no elements are copied and both ranges advance. The output
5723 * range may not overlap either input range.
5725 template<typename _InputIterator1
, typename _InputIterator2
,
5726 typename _OutputIterator
, typename _Compare
>
5727 _GLIBCXX20_CONSTEXPR
5728 inline _OutputIterator
5729 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5730 _InputIterator2 __first2
, _InputIterator2 __last2
,
5731 _OutputIterator __result
,
5734 // concept requirements
5735 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5736 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5737 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5738 typename iterator_traits
<_InputIterator1
>::value_type
>)
5739 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5740 typename iterator_traits
<_InputIterator2
>::value_type
>)
5741 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5742 typename iterator_traits
<_InputIterator1
>::value_type
,
5743 typename iterator_traits
<_InputIterator2
>::value_type
>)
5744 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5745 typename iterator_traits
<_InputIterator2
>::value_type
,
5746 typename iterator_traits
<_InputIterator1
>::value_type
>)
5747 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5748 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5749 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5750 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5752 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5753 __first2
, __last2
, __result
,
5754 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5757 template<typename _ForwardIterator
, typename _Compare
>
5758 _GLIBCXX14_CONSTEXPR
5760 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5763 if (__first
== __last
)
5765 _ForwardIterator __result
= __first
;
5766 while (++__first
!= __last
)
5767 if (__comp(__first
, __result
))
5773 * @brief Return the minimum element in a range.
5774 * @ingroup sorting_algorithms
5775 * @param __first Start of range.
5776 * @param __last End of range.
5777 * @return Iterator referencing the first instance of the smallest value.
5779 template<typename _ForwardIterator
>
5780 _GLIBCXX14_CONSTEXPR
5782 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5784 // concept requirements
5785 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5786 __glibcxx_function_requires(_LessThanComparableConcept
<
5787 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5788 __glibcxx_requires_valid_range(__first
, __last
);
5789 __glibcxx_requires_irreflexive(__first
, __last
);
5791 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5792 __gnu_cxx::__ops::__iter_less_iter());
5796 * @brief Return the minimum element in a range using comparison functor.
5797 * @ingroup sorting_algorithms
5798 * @param __first Start of range.
5799 * @param __last End of range.
5800 * @param __comp Comparison functor.
5801 * @return Iterator referencing the first instance of the smallest value
5802 * according to __comp.
5804 template<typename _ForwardIterator
, typename _Compare
>
5805 _GLIBCXX14_CONSTEXPR
5806 inline _ForwardIterator
5807 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5810 // concept requirements
5811 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5812 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5813 typename iterator_traits
<_ForwardIterator
>::value_type
,
5814 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5815 __glibcxx_requires_valid_range(__first
, __last
);
5816 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5818 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5819 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5822 template<typename _ForwardIterator
, typename _Compare
>
5823 _GLIBCXX14_CONSTEXPR
5825 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5828 if (__first
== __last
) return __first
;
5829 _ForwardIterator __result
= __first
;
5830 while (++__first
!= __last
)
5831 if (__comp(__result
, __first
))
5837 * @brief Return the maximum element in a range.
5838 * @ingroup sorting_algorithms
5839 * @param __first Start of range.
5840 * @param __last End of range.
5841 * @return Iterator referencing the first instance of the largest value.
5843 template<typename _ForwardIterator
>
5844 _GLIBCXX14_CONSTEXPR
5845 inline _ForwardIterator
5846 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5848 // concept requirements
5849 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5850 __glibcxx_function_requires(_LessThanComparableConcept
<
5851 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5852 __glibcxx_requires_valid_range(__first
, __last
);
5853 __glibcxx_requires_irreflexive(__first
, __last
);
5855 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5856 __gnu_cxx::__ops::__iter_less_iter());
5860 * @brief Return the maximum element in a range using comparison functor.
5861 * @ingroup sorting_algorithms
5862 * @param __first Start of range.
5863 * @param __last End of range.
5864 * @param __comp Comparison functor.
5865 * @return Iterator referencing the first instance of the largest value
5866 * according to __comp.
5868 template<typename _ForwardIterator
, typename _Compare
>
5869 _GLIBCXX14_CONSTEXPR
5870 inline _ForwardIterator
5871 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5874 // concept requirements
5875 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5876 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5877 typename iterator_traits
<_ForwardIterator
>::value_type
,
5878 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5879 __glibcxx_requires_valid_range(__first
, __last
);
5880 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5882 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5883 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5886 #if __cplusplus >= 201402L
5887 /// Reservoir sampling algorithm.
5888 template<typename _InputIterator
, typename _RandomAccessIterator
,
5889 typename _Size
, typename _UniformRandomBitGenerator
>
5890 _RandomAccessIterator
5891 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5892 _RandomAccessIterator __out
, random_access_iterator_tag
,
5893 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5895 using __distrib_type
= uniform_int_distribution
<_Size
>;
5896 using __param_type
= typename
__distrib_type::param_type
;
5897 __distrib_type __d
{};
5898 _Size __sample_sz
= 0;
5899 while (__first
!= __last
&& __sample_sz
!= __n
)
5901 __out
[__sample_sz
++] = *__first
;
5904 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5905 ++__first
, (void) ++__pop_sz
)
5907 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5909 __out
[__k
] = *__first
;
5911 return __out
+ __sample_sz
;
5914 /// Selection sampling algorithm.
5915 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5916 typename _Size
, typename _UniformRandomBitGenerator
>
5918 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5919 forward_iterator_tag
,
5920 _OutputIterator __out
, _Cat
,
5921 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5923 using __distrib_type
= uniform_int_distribution
<_Size
>;
5924 using __param_type
= typename
__distrib_type::param_type
;
5925 using _USize
= make_unsigned_t
<_Size
>;
5926 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5927 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5929 __distrib_type __d
{};
5930 _Size __unsampled_sz
= std::distance(__first
, __last
);
5931 __n
= std::min(__n
, __unsampled_sz
);
5933 // If possible, we use __gen_two_uniform_ints to efficiently produce
5934 // two random numbers using a single distribution invocation:
5936 const __uc_type __urngrange
= __g
.max() - __g
.min();
5937 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5938 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5941 while (__n
!= 0 && __unsampled_sz
>= 2)
5943 const pair
<_Size
, _Size
> __p
=
5944 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5947 if (__p
.first
< __n
)
5949 *__out
++ = *__first
;
5955 if (__n
== 0) break;
5958 if (__p
.second
< __n
)
5960 *__out
++ = *__first
;
5968 // The loop above is otherwise equivalent to this one-at-a-time version:
5970 for (; __n
!= 0; ++__first
)
5971 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5973 *__out
++ = *__first
;
5979 #if __cplusplus > 201402L
5980 #define __cpp_lib_sample 201603
5981 /// Take a random sample from a population.
5982 template<typename _PopulationIterator
, typename _SampleIterator
,
5983 typename _Distance
, typename _UniformRandomBitGenerator
>
5985 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5986 _SampleIterator __out
, _Distance __n
,
5987 _UniformRandomBitGenerator
&& __g
)
5989 using __pop_cat
= typename
5990 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5991 using __samp_cat
= typename
5992 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5995 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5996 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5997 "output range must use a RandomAccessIterator when input range"
5998 " does not meet the ForwardIterator requirements");
6000 static_assert(is_integral
<_Distance
>::value
,
6001 "sample size must be an integer type");
6003 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
6004 return _GLIBCXX_STD_A::
6005 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
6006 std::forward
<_UniformRandomBitGenerator
>(__g
));
6011 _GLIBCXX_END_NAMESPACE_ALGO
6012 _GLIBCXX_END_NAMESPACE_VERSION
6015 #endif /* _STL_ALGO_H */