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
>
78 __move_median_to_first(_Iterator __result
,_Iterator __a
, _Iterator __b
,
79 _Iterator __c
, _Compare __comp
)
84 std::iter_swap(__result
, __b
);
85 else if (__comp(__a
, __c
))
86 std::iter_swap(__result
, __c
);
88 std::iter_swap(__result
, __a
);
90 else if (__comp(__a
, __c
))
91 std::iter_swap(__result
, __a
);
92 else if (__comp(__b
, __c
))
93 std::iter_swap(__result
, __c
);
95 std::iter_swap(__result
, __b
);
98 /// This is an overload used by find algos for the Input Iterator case.
99 template<typename _InputIterator
, typename _Predicate
>
100 inline _InputIterator
101 __find_if(_InputIterator __first
, _InputIterator __last
,
102 _Predicate __pred
, input_iterator_tag
)
104 while (__first
!= __last
&& !__pred(__first
))
109 /// This is an overload used by find algos for the RAI case.
110 template<typename _RandomAccessIterator
, typename _Predicate
>
111 _RandomAccessIterator
112 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
113 _Predicate __pred
, random_access_iterator_tag
)
115 typename iterator_traits
<_RandomAccessIterator
>::difference_type
116 __trip_count
= (__last
- __first
) >> 2;
118 for (; __trip_count
> 0; --__trip_count
)
137 switch (__last
- __first
)
157 template<typename _Iterator
, typename _Predicate
>
159 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
161 return __find_if(__first
, __last
, __pred
,
162 std::__iterator_category(__first
));
165 /// Provided for stable_partition to use.
166 template<typename _InputIterator
, typename _Predicate
>
167 inline _InputIterator
168 __find_if_not(_InputIterator __first
, _InputIterator __last
,
171 return std::__find_if(__first
, __last
,
172 __gnu_cxx::__ops::__negate(__pred
),
173 std::__iterator_category(__first
));
176 /// Like find_if_not(), but uses and updates a count of the
177 /// remaining range length instead of comparing against an end
179 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
181 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
183 for (; __len
; --__len
, (void) ++__first
)
184 if (!__pred(__first
))
191 // set_symmetric_difference
202 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
203 typename _BinaryPredicate
>
205 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
206 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
207 _BinaryPredicate __predicate
)
209 // Test for empty ranges
210 if (__first1
== __last1
|| __first2
== __last2
)
213 // Test for a pattern of length 1.
214 _ForwardIterator2
__p1(__first2
);
215 if (++__p1
== __last2
)
216 return std::__find_if(__first1
, __last1
,
217 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
220 _ForwardIterator2 __p
;
221 _ForwardIterator1 __current
= __first1
;
226 std::__find_if(__first1
, __last1
,
227 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
229 if (__first1
== __last1
)
233 __current
= __first1
;
234 if (++__current
== __last1
)
237 while (__predicate(__current
, __p
))
239 if (++__p
== __last2
)
241 if (++__current
== __last1
)
252 * This is an helper function for search_n overloaded for forward iterators.
254 template<typename _ForwardIterator
, typename _Integer
,
255 typename _UnaryPredicate
>
257 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
258 _Integer __count
, _UnaryPredicate __unary_pred
,
259 std::forward_iterator_tag
)
261 __first
= std::__find_if(__first
, __last
, __unary_pred
);
262 while (__first
!= __last
)
264 typename iterator_traits
<_ForwardIterator
>::difference_type
266 _ForwardIterator __i
= __first
;
268 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
277 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
283 * This is an helper function for search_n overloaded for random access
286 template<typename _RandomAccessIter
, typename _Integer
,
287 typename _UnaryPredicate
>
289 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
290 _Integer __count
, _UnaryPredicate __unary_pred
,
291 std::random_access_iterator_tag
)
293 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
296 _DistanceType __tailSize
= __last
- __first
;
297 _DistanceType __remainder
= __count
;
299 while (__remainder
<= __tailSize
) // the main loop...
301 __first
+= __remainder
;
302 __tailSize
-= __remainder
;
303 // __first here is always pointing to one past the last element of
304 // next possible match.
305 _RandomAccessIter __backTrack
= __first
;
306 while (__unary_pred(--__backTrack
))
308 if (--__remainder
== 0)
309 return (__first
- __count
); // Success
311 __remainder
= __count
+ 1 - (__first
- __backTrack
);
313 return __last
; // Failure
316 template<typename _ForwardIterator
, typename _Integer
,
317 typename _UnaryPredicate
>
319 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
321 _UnaryPredicate __unary_pred
)
327 return std::__find_if(__first
, __last
, __unary_pred
);
329 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
330 std::__iterator_category(__first
));
333 // find_end for forward iterators.
334 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
335 typename _BinaryPredicate
>
337 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
338 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
339 forward_iterator_tag
, forward_iterator_tag
,
340 _BinaryPredicate __comp
)
342 if (__first2
== __last2
)
345 _ForwardIterator1 __result
= __last1
;
348 _ForwardIterator1 __new_result
349 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
350 if (__new_result
== __last1
)
354 __result
= __new_result
;
355 __first1
= __new_result
;
361 // find_end for bidirectional iterators (much faster).
362 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
363 typename _BinaryPredicate
>
364 _BidirectionalIterator1
365 __find_end(_BidirectionalIterator1 __first1
,
366 _BidirectionalIterator1 __last1
,
367 _BidirectionalIterator2 __first2
,
368 _BidirectionalIterator2 __last2
,
369 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
370 _BinaryPredicate __comp
)
372 // concept requirements
373 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
374 _BidirectionalIterator1
>)
375 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
376 _BidirectionalIterator2
>)
378 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
379 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
381 _RevIterator1
__rlast1(__first1
);
382 _RevIterator2
__rlast2(__first2
);
383 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
384 _RevIterator2(__last2
), __rlast2
,
387 if (__rresult
== __rlast1
)
391 _BidirectionalIterator1 __result
= __rresult
.base();
392 std::advance(__result
, -std::distance(__first2
, __last2
));
398 * @brief Find last matching subsequence in a sequence.
399 * @ingroup non_mutating_algorithms
400 * @param __first1 Start of range to search.
401 * @param __last1 End of range to search.
402 * @param __first2 Start of sequence to match.
403 * @param __last2 End of sequence to match.
404 * @return The last iterator @c i in the range
405 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
406 * @p *(__first2+N) for each @c N in the range @p
407 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
409 * Searches the range @p [__first1,__last1) for a sub-sequence that
410 * compares equal value-by-value with the sequence given by @p
411 * [__first2,__last2) and returns an iterator to the __first
412 * element of the sub-sequence, or @p __last1 if the sub-sequence
413 * is not found. The sub-sequence will be the last such
414 * subsequence contained in [__first1,__last1).
416 * Because the sub-sequence must lie completely within the range @p
417 * [__first1,__last1) it must start at a position less than @p
418 * __last1-(__last2-__first2) where @p __last2-__first2 is the
419 * length of the sub-sequence. This means that the returned
420 * iterator @c i will be in the range @p
421 * [__first1,__last1-(__last2-__first2))
423 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
424 inline _ForwardIterator1
425 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
426 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
428 // concept requirements
429 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
430 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
431 __glibcxx_function_requires(_EqualOpConcept
<
432 typename iterator_traits
<_ForwardIterator1
>::value_type
,
433 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
434 __glibcxx_requires_valid_range(__first1
, __last1
);
435 __glibcxx_requires_valid_range(__first2
, __last2
);
437 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
438 std::__iterator_category(__first1
),
439 std::__iterator_category(__first2
),
440 __gnu_cxx::__ops::__iter_equal_to_iter());
444 * @brief Find last matching subsequence in a sequence using a predicate.
445 * @ingroup non_mutating_algorithms
446 * @param __first1 Start of range to search.
447 * @param __last1 End of range to search.
448 * @param __first2 Start of sequence to match.
449 * @param __last2 End of sequence to match.
450 * @param __comp The predicate to use.
451 * @return The last iterator @c i in the range @p
452 * [__first1,__last1-(__last2-__first2)) such that @c
453 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
454 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
457 * Searches the range @p [__first1,__last1) for a sub-sequence that
458 * compares equal value-by-value with the sequence given by @p
459 * [__first2,__last2) using comp as a predicate and returns an
460 * iterator to the first element of the sub-sequence, or @p __last1
461 * if the sub-sequence is not found. The sub-sequence will be the
462 * last such subsequence contained in [__first,__last1).
464 * Because the sub-sequence must lie completely within the range @p
465 * [__first1,__last1) it must start at a position less than @p
466 * __last1-(__last2-__first2) where @p __last2-__first2 is the
467 * length of the sub-sequence. This means that the returned
468 * iterator @c i will be in the range @p
469 * [__first1,__last1-(__last2-__first2))
471 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
472 typename _BinaryPredicate
>
473 inline _ForwardIterator1
474 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
475 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
476 _BinaryPredicate __comp
)
478 // concept requirements
479 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
480 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
481 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
482 typename iterator_traits
<_ForwardIterator1
>::value_type
,
483 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
484 __glibcxx_requires_valid_range(__first1
, __last1
);
485 __glibcxx_requires_valid_range(__first2
, __last2
);
487 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
488 std::__iterator_category(__first1
),
489 std::__iterator_category(__first2
),
490 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
493 #if __cplusplus >= 201103L
495 * @brief Checks that a predicate is true for all the elements
497 * @ingroup non_mutating_algorithms
498 * @param __first An input iterator.
499 * @param __last An input iterator.
500 * @param __pred A predicate.
501 * @return True if the check is true, false otherwise.
503 * Returns true if @p __pred is true for each element in the range
504 * @p [__first,__last), and false otherwise.
506 template<typename _InputIterator
, typename _Predicate
>
508 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
509 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
512 * @brief Checks that a predicate is false for all the elements
514 * @ingroup non_mutating_algorithms
515 * @param __first An input iterator.
516 * @param __last An input iterator.
517 * @param __pred A predicate.
518 * @return True if the check is true, false otherwise.
520 * Returns true if @p __pred is false for each element in the range
521 * @p [__first,__last), and false otherwise.
523 template<typename _InputIterator
, typename _Predicate
>
525 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
526 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
529 * @brief Checks that a predicate is false for at least an element
531 * @ingroup non_mutating_algorithms
532 * @param __first An input iterator.
533 * @param __last An input iterator.
534 * @param __pred A predicate.
535 * @return True if the check is true, false otherwise.
537 * Returns true if an element exists in the range @p
538 * [__first,__last) such that @p __pred is true, and false
541 template<typename _InputIterator
, typename _Predicate
>
543 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
544 { return !std::none_of(__first
, __last
, __pred
); }
547 * @brief Find the first element in a sequence for which a
548 * predicate is false.
549 * @ingroup non_mutating_algorithms
550 * @param __first An input iterator.
551 * @param __last An input iterator.
552 * @param __pred A predicate.
553 * @return The first iterator @c i in the range @p [__first,__last)
554 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
556 template<typename _InputIterator
, typename _Predicate
>
557 inline _InputIterator
558 find_if_not(_InputIterator __first
, _InputIterator __last
,
561 // concept requirements
562 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
563 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
564 typename iterator_traits
<_InputIterator
>::value_type
>)
565 __glibcxx_requires_valid_range(__first
, __last
);
566 return std::__find_if_not(__first
, __last
,
567 __gnu_cxx::__ops::__pred_iter(__pred
));
571 * @brief Checks whether the sequence is partitioned.
572 * @ingroup mutating_algorithms
573 * @param __first An input iterator.
574 * @param __last An input iterator.
575 * @param __pred A predicate.
576 * @return True if the range @p [__first,__last) is partioned by @p __pred,
577 * i.e. if all elements that satisfy @p __pred appear before those that
580 template<typename _InputIterator
, typename _Predicate
>
582 is_partitioned(_InputIterator __first
, _InputIterator __last
,
585 __first
= std::find_if_not(__first
, __last
, __pred
);
586 if (__first
== __last
)
589 return std::none_of(__first
, __last
, __pred
);
593 * @brief Find the partition point of a partitioned range.
594 * @ingroup mutating_algorithms
595 * @param __first An iterator.
596 * @param __last Another iterator.
597 * @param __pred A predicate.
598 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
599 * and @p none_of(mid, __last, __pred) are both true.
601 template<typename _ForwardIterator
, typename _Predicate
>
603 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
606 // concept requirements
607 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
608 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
609 typename iterator_traits
<_ForwardIterator
>::value_type
>)
611 // A specific debug-mode test will be necessary...
612 __glibcxx_requires_valid_range(__first
, __last
);
614 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
617 _DistanceType __len
= std::distance(__first
, __last
);
618 _DistanceType __half
;
619 _ForwardIterator __middle
;
625 std::advance(__middle
, __half
);
626 if (__pred(*__middle
))
630 __len
= __len
- __half
- 1;
639 template<typename _InputIterator
, typename _OutputIterator
,
642 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
643 _OutputIterator __result
, _Predicate __pred
)
645 for (; __first
!= __last
; ++__first
)
646 if (!__pred(__first
))
648 *__result
= *__first
;
655 * @brief Copy a sequence, removing elements of a given value.
656 * @ingroup mutating_algorithms
657 * @param __first An input iterator.
658 * @param __last An input iterator.
659 * @param __result An output iterator.
660 * @param __value The value to be removed.
661 * @return An iterator designating the end of the resulting sequence.
663 * Copies each element in the range @p [__first,__last) not equal
664 * to @p __value to the range beginning at @p __result.
665 * remove_copy() is stable, so the relative order of elements that
666 * are copied is unchanged.
668 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
669 inline _OutputIterator
670 remove_copy(_InputIterator __first
, _InputIterator __last
,
671 _OutputIterator __result
, const _Tp
& __value
)
673 // concept requirements
674 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
675 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
676 typename iterator_traits
<_InputIterator
>::value_type
>)
677 __glibcxx_function_requires(_EqualOpConcept
<
678 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
679 __glibcxx_requires_valid_range(__first
, __last
);
681 return std::__remove_copy_if(__first
, __last
, __result
,
682 __gnu_cxx::__ops::__iter_equals_val(__value
));
686 * @brief Copy a sequence, removing elements for which a predicate is true.
687 * @ingroup mutating_algorithms
688 * @param __first An input iterator.
689 * @param __last An input iterator.
690 * @param __result An output iterator.
691 * @param __pred A predicate.
692 * @return An iterator designating the end of the resulting sequence.
694 * Copies each element in the range @p [__first,__last) for which
695 * @p __pred returns false to the range beginning at @p __result.
697 * remove_copy_if() is stable, so the relative order of elements that are
698 * copied is unchanged.
700 template<typename _InputIterator
, typename _OutputIterator
,
702 inline _OutputIterator
703 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
704 _OutputIterator __result
, _Predicate __pred
)
706 // concept requirements
707 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
708 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
709 typename iterator_traits
<_InputIterator
>::value_type
>)
710 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
711 typename iterator_traits
<_InputIterator
>::value_type
>)
712 __glibcxx_requires_valid_range(__first
, __last
);
714 return std::__remove_copy_if(__first
, __last
, __result
,
715 __gnu_cxx::__ops::__pred_iter(__pred
));
718 #if __cplusplus >= 201103L
720 * @brief Copy the elements of a sequence for which a predicate is true.
721 * @ingroup mutating_algorithms
722 * @param __first An input iterator.
723 * @param __last An input iterator.
724 * @param __result An output iterator.
725 * @param __pred A predicate.
726 * @return An iterator designating the end of the resulting sequence.
728 * Copies each element in the range @p [__first,__last) for which
729 * @p __pred returns true to the range beginning at @p __result.
731 * copy_if() is stable, so the relative order of elements that are
732 * copied is unchanged.
734 template<typename _InputIterator
, typename _OutputIterator
,
737 copy_if(_InputIterator __first
, _InputIterator __last
,
738 _OutputIterator __result
, _Predicate __pred
)
740 // concept requirements
741 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
742 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
743 typename iterator_traits
<_InputIterator
>::value_type
>)
744 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
745 typename iterator_traits
<_InputIterator
>::value_type
>)
746 __glibcxx_requires_valid_range(__first
, __last
);
748 for (; __first
!= __last
; ++__first
)
749 if (__pred(*__first
))
751 *__result
= *__first
;
757 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
759 __copy_n(_InputIterator __first
, _Size __n
,
760 _OutputIterator __result
, input_iterator_tag
)
766 *__result
= *__first
;
777 template<typename _RandomAccessIterator
, typename _Size
,
778 typename _OutputIterator
>
779 inline _OutputIterator
780 __copy_n(_RandomAccessIterator __first
, _Size __n
,
781 _OutputIterator __result
, random_access_iterator_tag
)
782 { return std::copy(__first
, __first
+ __n
, __result
); }
785 * @brief Copies the range [first,first+n) into [result,result+n).
786 * @ingroup mutating_algorithms
787 * @param __first An input iterator.
788 * @param __n The number of elements to copy.
789 * @param __result An output iterator.
792 * This inline function will boil down to a call to @c memmove whenever
793 * possible. Failing that, if random access iterators are passed, then the
794 * loop count will be known (and therefore a candidate for compiler
795 * optimizations such as unrolling).
797 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
798 inline _OutputIterator
799 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
801 // concept requirements
802 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
803 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
804 typename iterator_traits
<_InputIterator
>::value_type
>)
806 return std::__copy_n(__first
, __n
, __result
,
807 std::__iterator_category(__first
));
811 * @brief Copy the elements of a sequence to separate output sequences
812 * depending on the truth value of a predicate.
813 * @ingroup mutating_algorithms
814 * @param __first An input iterator.
815 * @param __last An input iterator.
816 * @param __out_true An output iterator.
817 * @param __out_false An output iterator.
818 * @param __pred A predicate.
819 * @return A pair designating the ends of the resulting sequences.
821 * Copies each element in the range @p [__first,__last) for which
822 * @p __pred returns true to the range beginning at @p out_true
823 * and each element for which @p __pred returns false to @p __out_false.
825 template<typename _InputIterator
, typename _OutputIterator1
,
826 typename _OutputIterator2
, typename _Predicate
>
827 pair
<_OutputIterator1
, _OutputIterator2
>
828 partition_copy(_InputIterator __first
, _InputIterator __last
,
829 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
832 // concept requirements
833 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
834 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
835 typename iterator_traits
<_InputIterator
>::value_type
>)
836 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
837 typename iterator_traits
<_InputIterator
>::value_type
>)
838 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
839 typename iterator_traits
<_InputIterator
>::value_type
>)
840 __glibcxx_requires_valid_range(__first
, __last
);
842 for (; __first
!= __last
; ++__first
)
843 if (__pred(*__first
))
845 *__out_true
= *__first
;
850 *__out_false
= *__first
;
854 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
858 template<typename _ForwardIterator
, typename _Predicate
>
860 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
863 __first
= std::__find_if(__first
, __last
, __pred
);
864 if (__first
== __last
)
866 _ForwardIterator __result
= __first
;
868 for (; __first
!= __last
; ++__first
)
869 if (!__pred(__first
))
871 *__result
= _GLIBCXX_MOVE(*__first
);
878 * @brief Remove elements from a sequence.
879 * @ingroup mutating_algorithms
880 * @param __first An input iterator.
881 * @param __last An input iterator.
882 * @param __value The value to be removed.
883 * @return An iterator designating the end of the resulting sequence.
885 * All elements equal to @p __value are removed from the range
886 * @p [__first,__last).
888 * remove() is stable, so the relative order of elements that are
889 * not removed is unchanged.
891 * Elements between the end of the resulting sequence and @p __last
892 * are still present, but their value is unspecified.
894 template<typename _ForwardIterator
, typename _Tp
>
895 inline _ForwardIterator
896 remove(_ForwardIterator __first
, _ForwardIterator __last
,
899 // concept requirements
900 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
902 __glibcxx_function_requires(_EqualOpConcept
<
903 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
904 __glibcxx_requires_valid_range(__first
, __last
);
906 return std::__remove_if(__first
, __last
,
907 __gnu_cxx::__ops::__iter_equals_val(__value
));
911 * @brief Remove elements from a sequence using a predicate.
912 * @ingroup mutating_algorithms
913 * @param __first A forward iterator.
914 * @param __last A forward iterator.
915 * @param __pred A predicate.
916 * @return An iterator designating the end of the resulting sequence.
918 * All elements for which @p __pred returns true are removed from the range
919 * @p [__first,__last).
921 * remove_if() is stable, so the relative order of elements that are
922 * not removed is unchanged.
924 * Elements between the end of the resulting sequence and @p __last
925 * are still present, but their value is unspecified.
927 template<typename _ForwardIterator
, typename _Predicate
>
928 inline _ForwardIterator
929 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
932 // concept requirements
933 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
935 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
936 typename iterator_traits
<_ForwardIterator
>::value_type
>)
937 __glibcxx_requires_valid_range(__first
, __last
);
939 return std::__remove_if(__first
, __last
,
940 __gnu_cxx::__ops::__pred_iter(__pred
));
943 template<typename _ForwardIterator
, typename _BinaryPredicate
>
945 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
946 _BinaryPredicate __binary_pred
)
948 if (__first
== __last
)
950 _ForwardIterator __next
= __first
;
951 while (++__next
!= __last
)
953 if (__binary_pred(__first
, __next
))
960 template<typename _ForwardIterator
, typename _BinaryPredicate
>
962 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
963 _BinaryPredicate __binary_pred
)
965 // Skip the beginning, if already unique.
966 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
967 if (__first
== __last
)
970 // Do the real copy work.
971 _ForwardIterator __dest
= __first
;
973 while (++__first
!= __last
)
974 if (!__binary_pred(__dest
, __first
))
975 *++__dest
= _GLIBCXX_MOVE(*__first
);
980 * @brief Remove consecutive duplicate values from a sequence.
981 * @ingroup mutating_algorithms
982 * @param __first A forward iterator.
983 * @param __last A forward iterator.
984 * @return An iterator designating the end of the resulting sequence.
986 * Removes all but the first element from each group of consecutive
987 * values that compare equal.
988 * unique() is stable, so the relative order of elements that are
989 * not removed is unchanged.
990 * Elements between the end of the resulting sequence and @p __last
991 * are still present, but their value is unspecified.
993 template<typename _ForwardIterator
>
994 inline _ForwardIterator
995 unique(_ForwardIterator __first
, _ForwardIterator __last
)
997 // concept requirements
998 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1000 __glibcxx_function_requires(_EqualityComparableConcept
<
1001 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1002 __glibcxx_requires_valid_range(__first
, __last
);
1004 return std::__unique(__first
, __last
,
1005 __gnu_cxx::__ops::__iter_equal_to_iter());
1009 * @brief Remove consecutive values from a sequence using a predicate.
1010 * @ingroup mutating_algorithms
1011 * @param __first A forward iterator.
1012 * @param __last A forward iterator.
1013 * @param __binary_pred A binary predicate.
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 for which @p __binary_pred returns true.
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
, typename _BinaryPredicate
>
1024 inline _ForwardIterator
1025 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1026 _BinaryPredicate __binary_pred
)
1028 // concept requirements
1029 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1031 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1032 typename iterator_traits
<_ForwardIterator
>::value_type
,
1033 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1034 __glibcxx_requires_valid_range(__first
, __last
);
1036 return std::__unique(__first
, __last
,
1037 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1041 * This is an uglified
1042 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1044 * overloaded for forward iterators and output iterator as result.
1046 template<typename _ForwardIterator
, typename _OutputIterator
,
1047 typename _BinaryPredicate
>
1049 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1050 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1051 forward_iterator_tag
, output_iterator_tag
)
1053 // concept requirements -- iterators already checked
1054 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1055 typename iterator_traits
<_ForwardIterator
>::value_type
,
1056 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1058 _ForwardIterator __next
= __first
;
1059 *__result
= *__first
;
1060 while (++__next
!= __last
)
1061 if (!__binary_pred(__first
, __next
))
1064 *++__result
= *__first
;
1070 * This is an uglified
1071 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1073 * overloaded for input iterators and output iterator as result.
1075 template<typename _InputIterator
, typename _OutputIterator
,
1076 typename _BinaryPredicate
>
1078 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1079 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1080 input_iterator_tag
, output_iterator_tag
)
1082 // concept requirements -- iterators already checked
1083 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1084 typename iterator_traits
<_InputIterator
>::value_type
,
1085 typename iterator_traits
<_InputIterator
>::value_type
>)
1087 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1088 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1090 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1091 *__result
= __value
;
1092 while (++__first
!= __last
)
1093 if (!__rebound_pred(__first
, __value
))
1096 *++__result
= __value
;
1102 * This is an uglified
1103 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1105 * overloaded for input iterators and forward iterator as result.
1107 template<typename _InputIterator
, typename _ForwardIterator
,
1108 typename _BinaryPredicate
>
1110 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1111 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1112 input_iterator_tag
, forward_iterator_tag
)
1114 // concept requirements -- iterators already checked
1115 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1116 typename iterator_traits
<_ForwardIterator
>::value_type
,
1117 typename iterator_traits
<_InputIterator
>::value_type
>)
1118 *__result
= *__first
;
1119 while (++__first
!= __last
)
1120 if (!__binary_pred(__result
, __first
))
1121 *++__result
= *__first
;
1126 * This is an uglified reverse(_BidirectionalIterator,
1127 * _BidirectionalIterator)
1128 * overloaded for bidirectional iterators.
1130 template<typename _BidirectionalIterator
>
1132 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1133 bidirectional_iterator_tag
)
1136 if (__first
== __last
|| __first
== --__last
)
1140 std::iter_swap(__first
, __last
);
1146 * This is an uglified reverse(_BidirectionalIterator,
1147 * _BidirectionalIterator)
1148 * overloaded for random access iterators.
1150 template<typename _RandomAccessIterator
>
1152 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1153 random_access_iterator_tag
)
1155 if (__first
== __last
)
1158 while (__first
< __last
)
1160 std::iter_swap(__first
, __last
);
1167 * @brief Reverse a sequence.
1168 * @ingroup mutating_algorithms
1169 * @param __first A bidirectional iterator.
1170 * @param __last A bidirectional iterator.
1171 * @return reverse() returns no value.
1173 * Reverses the order of the elements in the range @p [__first,__last),
1174 * so that the first element becomes the last etc.
1175 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1176 * swaps @p *(__first+i) and @p *(__last-(i+1))
1178 template<typename _BidirectionalIterator
>
1180 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1182 // concept requirements
1183 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1184 _BidirectionalIterator
>)
1185 __glibcxx_requires_valid_range(__first
, __last
);
1186 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1190 * @brief Copy a sequence, reversing its elements.
1191 * @ingroup mutating_algorithms
1192 * @param __first A bidirectional iterator.
1193 * @param __last A bidirectional iterator.
1194 * @param __result An output iterator.
1195 * @return An iterator designating the end of the resulting sequence.
1197 * Copies the elements in the range @p [__first,__last) to the
1198 * range @p [__result,__result+(__last-__first)) such that the
1199 * order of the elements is reversed. For every @c i such that @p
1200 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1201 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1202 * The ranges @p [__first,__last) and @p
1203 * [__result,__result+(__last-__first)) must not overlap.
1205 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1207 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1208 _OutputIterator __result
)
1210 // concept requirements
1211 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1212 _BidirectionalIterator
>)
1213 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1214 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1215 __glibcxx_requires_valid_range(__first
, __last
);
1217 while (__first
!= __last
)
1220 *__result
= *__last
;
1227 * This is a helper function for the rotate algorithm specialized on RAIs.
1228 * It returns the greatest common divisor of two integer values.
1230 template<typename _EuclideanRingElement
>
1231 _EuclideanRingElement
1232 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1236 _EuclideanRingElement __t
= __m
% __n
;
1243 inline namespace _V2
1246 /// This is a helper function for the rotate algorithm.
1247 template<typename _ForwardIterator
>
1249 __rotate(_ForwardIterator __first
,
1250 _ForwardIterator __middle
,
1251 _ForwardIterator __last
,
1252 forward_iterator_tag
)
1254 if (__first
== __middle
)
1256 else if (__last
== __middle
)
1259 _ForwardIterator __first2
= __middle
;
1262 std::iter_swap(__first
, __first2
);
1265 if (__first
== __middle
)
1266 __middle
= __first2
;
1268 while (__first2
!= __last
);
1270 _ForwardIterator __ret
= __first
;
1272 __first2
= __middle
;
1274 while (__first2
!= __last
)
1276 std::iter_swap(__first
, __first2
);
1279 if (__first
== __middle
)
1280 __middle
= __first2
;
1281 else if (__first2
== __last
)
1282 __first2
= __middle
;
1287 /// This is a helper function for the rotate algorithm.
1288 template<typename _BidirectionalIterator
>
1289 _BidirectionalIterator
1290 __rotate(_BidirectionalIterator __first
,
1291 _BidirectionalIterator __middle
,
1292 _BidirectionalIterator __last
,
1293 bidirectional_iterator_tag
)
1295 // concept requirements
1296 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1297 _BidirectionalIterator
>)
1299 if (__first
== __middle
)
1301 else if (__last
== __middle
)
1304 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1305 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1307 while (__first
!= __middle
&& __middle
!= __last
)
1309 std::iter_swap(__first
, --__last
);
1313 if (__first
== __middle
)
1315 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1320 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1325 /// This is a helper function for the rotate algorithm.
1326 template<typename _RandomAccessIterator
>
1327 _RandomAccessIterator
1328 __rotate(_RandomAccessIterator __first
,
1329 _RandomAccessIterator __middle
,
1330 _RandomAccessIterator __last
,
1331 random_access_iterator_tag
)
1333 // concept requirements
1334 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1335 _RandomAccessIterator
>)
1337 if (__first
== __middle
)
1339 else if (__last
== __middle
)
1342 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1344 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1347 _Distance __n
= __last
- __first
;
1348 _Distance __k
= __middle
- __first
;
1350 if (__k
== __n
- __k
)
1352 std::swap_ranges(__first
, __middle
, __middle
);
1356 _RandomAccessIterator __p
= __first
;
1357 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1361 if (__k
< __n
- __k
)
1363 if (__is_pod(_ValueType
) && __k
== 1)
1365 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1366 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1367 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1370 _RandomAccessIterator __q
= __p
+ __k
;
1371 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1373 std::iter_swap(__p
, __q
);
1380 std::swap(__n
, __k
);
1386 if (__is_pod(_ValueType
) && __k
== 1)
1388 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1389 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1390 *__p
= _GLIBCXX_MOVE(__t
);
1393 _RandomAccessIterator __q
= __p
+ __n
;
1395 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1399 std::iter_swap(__p
, __q
);
1404 std::swap(__n
, __k
);
1409 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1410 // DR 488. rotate throws away useful information
1412 * @brief Rotate the elements of a sequence.
1413 * @ingroup mutating_algorithms
1414 * @param __first A forward iterator.
1415 * @param __middle A forward iterator.
1416 * @param __last A forward iterator.
1417 * @return first + (last - middle).
1419 * Rotates the elements of the range @p [__first,__last) by
1420 * @p (__middle - __first) positions so that the element at @p __middle
1421 * is moved to @p __first, the element at @p __middle+1 is moved to
1422 * @p __first+1 and so on for each element in the range
1423 * @p [__first,__last).
1425 * This effectively swaps the ranges @p [__first,__middle) and
1426 * @p [__middle,__last).
1429 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1430 * for each @p n in the range @p [0,__last-__first).
1432 template<typename _ForwardIterator
>
1433 inline _ForwardIterator
1434 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1435 _ForwardIterator __last
)
1437 // concept requirements
1438 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1440 __glibcxx_requires_valid_range(__first
, __middle
);
1441 __glibcxx_requires_valid_range(__middle
, __last
);
1443 return std::__rotate(__first
, __middle
, __last
,
1444 std::__iterator_category(__first
));
1450 * @brief Copy a sequence, rotating its elements.
1451 * @ingroup mutating_algorithms
1452 * @param __first A forward iterator.
1453 * @param __middle A forward iterator.
1454 * @param __last A forward iterator.
1455 * @param __result An output iterator.
1456 * @return An iterator designating the end of the resulting sequence.
1458 * Copies the elements of the range @p [__first,__last) to the
1459 * range beginning at @result, rotating the copied elements by
1460 * @p (__middle-__first) positions so that the element at @p __middle
1461 * is moved to @p __result, the element at @p __middle+1 is moved
1462 * to @p __result+1 and so on for each element in the range @p
1466 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1467 * for each @p n in the range @p [0,__last-__first).
1469 template<typename _ForwardIterator
, typename _OutputIterator
>
1470 inline _OutputIterator
1471 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1472 _ForwardIterator __last
, _OutputIterator __result
)
1474 // concept requirements
1475 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1476 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1477 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1478 __glibcxx_requires_valid_range(__first
, __middle
);
1479 __glibcxx_requires_valid_range(__middle
, __last
);
1481 return std::copy(__first
, __middle
,
1482 std::copy(__middle
, __last
, __result
));
1485 /// This is a helper function...
1486 template<typename _ForwardIterator
, typename _Predicate
>
1488 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1489 _Predicate __pred
, forward_iterator_tag
)
1491 if (__first
== __last
)
1494 while (__pred(*__first
))
1495 if (++__first
== __last
)
1498 _ForwardIterator __next
= __first
;
1500 while (++__next
!= __last
)
1501 if (__pred(*__next
))
1503 std::iter_swap(__first
, __next
);
1510 /// This is a helper function...
1511 template<typename _BidirectionalIterator
, typename _Predicate
>
1512 _BidirectionalIterator
1513 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1514 _Predicate __pred
, bidirectional_iterator_tag
)
1519 if (__first
== __last
)
1521 else if (__pred(*__first
))
1527 if (__first
== __last
)
1529 else if (!bool(__pred(*__last
)))
1533 std::iter_swap(__first
, __last
);
1540 /// This is a helper function...
1541 /// Requires __first != __last and !__pred(__first)
1542 /// and __len == distance(__first, __last).
1544 /// !__pred(__first) allows us to guarantee that we don't
1545 /// move-assign an element onto itself.
1546 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1549 __stable_partition_adaptive(_ForwardIterator __first
,
1550 _ForwardIterator __last
,
1551 _Predicate __pred
, _Distance __len
,
1553 _Distance __buffer_size
)
1558 if (__len
<= __buffer_size
)
1560 _ForwardIterator __result1
= __first
;
1561 _Pointer __result2
= __buffer
;
1563 // The precondition guarantees that !__pred(__first), so
1564 // move that element to the buffer before starting the loop.
1565 // This ensures that we only call __pred once per element.
1566 *__result2
= _GLIBCXX_MOVE(*__first
);
1569 for (; __first
!= __last
; ++__first
)
1570 if (__pred(__first
))
1572 *__result1
= _GLIBCXX_MOVE(*__first
);
1577 *__result2
= _GLIBCXX_MOVE(*__first
);
1581 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1585 _ForwardIterator __middle
= __first
;
1586 std::advance(__middle
, __len
/ 2);
1587 _ForwardIterator __left_split
=
1588 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1589 __len
/ 2, __buffer
,
1592 // Advance past true-predicate values to satisfy this
1593 // function's preconditions.
1594 _Distance __right_len
= __len
- __len
/ 2;
1595 _ForwardIterator __right_split
=
1596 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1600 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1602 __buffer
, __buffer_size
);
1604 return std::rotate(__left_split
, __middle
, __right_split
);
1607 template<typename _ForwardIterator
, typename _Predicate
>
1609 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1612 __first
= std::__find_if_not(__first
, __last
, __pred
);
1614 if (__first
== __last
)
1617 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1619 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1622 _Temporary_buffer
<_ForwardIterator
, _ValueType
>
1623 __buf(__first
, std::distance(__first
, __last
));
1625 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1626 _DistanceType(__buf
.requested_size()),
1628 _DistanceType(__buf
.size()));
1632 * @brief Move elements for which a predicate is true to the beginning
1633 * of a sequence, preserving relative ordering.
1634 * @ingroup mutating_algorithms
1635 * @param __first A forward iterator.
1636 * @param __last A forward iterator.
1637 * @param __pred A predicate functor.
1638 * @return An iterator @p middle such that @p __pred(i) is true for each
1639 * iterator @p i in the range @p [first,middle) and false for each @p i
1640 * in the range @p [middle,last).
1642 * Performs the same function as @p partition() with the additional
1643 * guarantee that the relative ordering of elements in each group is
1644 * preserved, so any two elements @p x and @p y in the range
1645 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1646 * relative ordering after calling @p stable_partition().
1648 template<typename _ForwardIterator
, typename _Predicate
>
1649 inline _ForwardIterator
1650 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1653 // concept requirements
1654 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1656 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1657 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1658 __glibcxx_requires_valid_range(__first
, __last
);
1660 return std::__stable_partition(__first
, __last
,
1661 __gnu_cxx::__ops::__pred_iter(__pred
));
1664 /// This is a helper function for the sort routines.
1665 template<typename _RandomAccessIterator
, typename _Compare
>
1667 __heap_select(_RandomAccessIterator __first
,
1668 _RandomAccessIterator __middle
,
1669 _RandomAccessIterator __last
, _Compare __comp
)
1671 std::__make_heap(__first
, __middle
, __comp
);
1672 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1673 if (__comp(__i
, __first
))
1674 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1679 template<typename _InputIterator
, typename _RandomAccessIterator
,
1681 _RandomAccessIterator
1682 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1683 _RandomAccessIterator __result_first
,
1684 _RandomAccessIterator __result_last
,
1687 typedef typename iterator_traits
<_InputIterator
>::value_type
1689 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1690 typedef typename
_RItTraits::difference_type _DistanceType
;
1692 if (__result_first
== __result_last
)
1693 return __result_last
;
1694 _RandomAccessIterator __result_real_last
= __result_first
;
1695 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1697 *__result_real_last
= *__first
;
1698 ++__result_real_last
;
1702 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1703 while (__first
!= __last
)
1705 if (__comp(__first
, __result_first
))
1706 std::__adjust_heap(__result_first
, _DistanceType(0),
1707 _DistanceType(__result_real_last
1709 _InputValueType(*__first
), __comp
);
1712 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1713 return __result_real_last
;
1717 * @brief Copy the smallest elements of a sequence.
1718 * @ingroup sorting_algorithms
1719 * @param __first An iterator.
1720 * @param __last Another iterator.
1721 * @param __result_first A random-access iterator.
1722 * @param __result_last Another random-access iterator.
1723 * @return An iterator indicating the end of the resulting sequence.
1725 * Copies and sorts the smallest N values from the range @p [__first,__last)
1726 * to the range beginning at @p __result_first, where the number of
1727 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1728 * @p (__result_last-__result_first).
1729 * After the sort if @e i and @e j are iterators in the range
1730 * @p [__result_first,__result_first+N) such that i precedes j then
1732 * The value returned is @p __result_first+N.
1734 template<typename _InputIterator
, typename _RandomAccessIterator
>
1735 inline _RandomAccessIterator
1736 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1737 _RandomAccessIterator __result_first
,
1738 _RandomAccessIterator __result_last
)
1740 #ifdef _GLIBCXX_CONCEPT_CHECKS
1741 typedef typename iterator_traits
<_InputIterator
>::value_type
1743 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1747 // concept requirements
1748 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1749 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1751 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1753 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1754 __glibcxx_requires_valid_range(__first
, __last
);
1755 __glibcxx_requires_irreflexive(__first
, __last
);
1756 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1758 return std::__partial_sort_copy(__first
, __last
,
1759 __result_first
, __result_last
,
1760 __gnu_cxx::__ops::__iter_less_iter());
1764 * @brief Copy the smallest elements of a sequence using a predicate for
1766 * @ingroup sorting_algorithms
1767 * @param __first An input iterator.
1768 * @param __last Another input iterator.
1769 * @param __result_first A random-access iterator.
1770 * @param __result_last Another random-access iterator.
1771 * @param __comp A comparison functor.
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
1780 * @p __comp(*j,*i) is false.
1781 * The value returned is @p __result_first+N.
1783 template<typename _InputIterator
, typename _RandomAccessIterator
,
1785 inline _RandomAccessIterator
1786 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1787 _RandomAccessIterator __result_first
,
1788 _RandomAccessIterator __result_last
,
1791 #ifdef _GLIBCXX_CONCEPT_CHECKS
1792 typedef typename iterator_traits
<_InputIterator
>::value_type
1794 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1798 // concept requirements
1799 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1800 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1801 _RandomAccessIterator
>)
1802 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1804 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1805 _InputValueType
, _OutputValueType
>)
1806 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1807 _OutputValueType
, _OutputValueType
>)
1808 __glibcxx_requires_valid_range(__first
, __last
);
1809 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1810 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1812 return std::__partial_sort_copy(__first
, __last
,
1813 __result_first
, __result_last
,
1814 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1817 /// This is a helper function for the sort routine.
1818 template<typename _RandomAccessIterator
, typename _Compare
>
1820 __unguarded_linear_insert(_RandomAccessIterator __last
,
1823 typename iterator_traits
<_RandomAccessIterator
>::value_type
1824 __val
= _GLIBCXX_MOVE(*__last
);
1825 _RandomAccessIterator __next
= __last
;
1827 while (__comp(__val
, __next
))
1829 *__last
= _GLIBCXX_MOVE(*__next
);
1833 *__last
= _GLIBCXX_MOVE(__val
);
1836 /// This is a helper function for the sort routine.
1837 template<typename _RandomAccessIterator
, typename _Compare
>
1839 __insertion_sort(_RandomAccessIterator __first
,
1840 _RandomAccessIterator __last
, _Compare __comp
)
1842 if (__first
== __last
) return;
1844 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1846 if (__comp(__i
, __first
))
1848 typename iterator_traits
<_RandomAccessIterator
>::value_type
1849 __val
= _GLIBCXX_MOVE(*__i
);
1850 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1851 *__first
= _GLIBCXX_MOVE(__val
);
1854 std::__unguarded_linear_insert(__i
,
1855 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1859 /// This is a helper function for the sort routine.
1860 template<typename _RandomAccessIterator
, typename _Compare
>
1862 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1863 _RandomAccessIterator __last
, _Compare __comp
)
1865 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1866 std::__unguarded_linear_insert(__i
,
1867 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1872 * This controls some aspect of the sort routines.
1874 enum { _S_threshold
= 16 };
1876 /// This is a helper function for the sort routine.
1877 template<typename _RandomAccessIterator
, typename _Compare
>
1879 __final_insertion_sort(_RandomAccessIterator __first
,
1880 _RandomAccessIterator __last
, _Compare __comp
)
1882 if (__last
- __first
> int(_S_threshold
))
1884 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1885 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1889 std::__insertion_sort(__first
, __last
, __comp
);
1892 /// This is a helper function...
1893 template<typename _RandomAccessIterator
, typename _Compare
>
1894 _RandomAccessIterator
1895 __unguarded_partition(_RandomAccessIterator __first
,
1896 _RandomAccessIterator __last
,
1897 _RandomAccessIterator __pivot
, _Compare __comp
)
1901 while (__comp(__first
, __pivot
))
1904 while (__comp(__pivot
, __last
))
1906 if (!(__first
< __last
))
1908 std::iter_swap(__first
, __last
);
1913 /// This is a helper function...
1914 template<typename _RandomAccessIterator
, typename _Compare
>
1915 inline _RandomAccessIterator
1916 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1917 _RandomAccessIterator __last
, _Compare __comp
)
1919 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1920 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1922 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1925 template<typename _RandomAccessIterator
, typename _Compare
>
1927 __partial_sort(_RandomAccessIterator __first
,
1928 _RandomAccessIterator __middle
,
1929 _RandomAccessIterator __last
,
1932 std::__heap_select(__first
, __middle
, __last
, __comp
);
1933 std::__sort_heap(__first
, __middle
, __comp
);
1936 /// This is a helper function for the sort routine.
1937 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1939 __introsort_loop(_RandomAccessIterator __first
,
1940 _RandomAccessIterator __last
,
1941 _Size __depth_limit
, _Compare __comp
)
1943 while (__last
- __first
> int(_S_threshold
))
1945 if (__depth_limit
== 0)
1947 std::__partial_sort(__first
, __last
, __last
, __comp
);
1951 _RandomAccessIterator __cut
=
1952 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1953 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1960 template<typename _RandomAccessIterator
, typename _Compare
>
1962 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1965 if (__first
!= __last
)
1967 std::__introsort_loop(__first
, __last
,
1968 std::__lg(__last
- __first
) * 2,
1970 std::__final_insertion_sort(__first
, __last
, __comp
);
1974 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1976 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1977 _RandomAccessIterator __last
, _Size __depth_limit
,
1980 while (__last
- __first
> 3)
1982 if (__depth_limit
== 0)
1984 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1985 // Place the nth largest element in its final position.
1986 std::iter_swap(__first
, __nth
);
1990 _RandomAccessIterator __cut
=
1991 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1997 std::__insertion_sort(__first
, __last
, __comp
);
2002 // lower_bound moved to stl_algobase.h
2005 * @brief Finds the first position in which @p __val could be inserted
2006 * without changing the ordering.
2007 * @ingroup binary_search_algorithms
2008 * @param __first An iterator.
2009 * @param __last Another iterator.
2010 * @param __val The search term.
2011 * @param __comp A functor to use for comparisons.
2012 * @return An iterator pointing to the first element <em>not less
2013 * than</em> @p __val, or end() if every element is less
2015 * @ingroup binary_search_algorithms
2017 * The comparison function should have the same effects on ordering as
2018 * the function used for the initial sort.
2020 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2021 inline _ForwardIterator
2022 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2023 const _Tp
& __val
, _Compare __comp
)
2025 // concept requirements
2026 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2027 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2028 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2029 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2032 return std::__lower_bound(__first
, __last
, __val
,
2033 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2036 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2038 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2039 const _Tp
& __val
, _Compare __comp
)
2041 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2044 _DistanceType __len
= std::distance(__first
, __last
);
2048 _DistanceType __half
= __len
>> 1;
2049 _ForwardIterator __middle
= __first
;
2050 std::advance(__middle
, __half
);
2051 if (__comp(__val
, __middle
))
2057 __len
= __len
- __half
- 1;
2064 * @brief Finds the last position in which @p __val could be inserted
2065 * without changing the ordering.
2066 * @ingroup binary_search_algorithms
2067 * @param __first An iterator.
2068 * @param __last Another iterator.
2069 * @param __val The search term.
2070 * @return An iterator pointing to the first element greater than @p __val,
2071 * or end() if no elements are greater than @p __val.
2072 * @ingroup binary_search_algorithms
2074 template<typename _ForwardIterator
, typename _Tp
>
2075 inline _ForwardIterator
2076 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2079 // concept requirements
2080 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2081 __glibcxx_function_requires(_LessThanOpConcept
<
2082 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2083 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2085 return std::__upper_bound(__first
, __last
, __val
,
2086 __gnu_cxx::__ops::__val_less_iter());
2090 * @brief Finds the last position in which @p __val could be inserted
2091 * without changing the ordering.
2092 * @ingroup binary_search_algorithms
2093 * @param __first An iterator.
2094 * @param __last Another iterator.
2095 * @param __val The search term.
2096 * @param __comp A functor to use for comparisons.
2097 * @return An iterator pointing to the first element greater than @p __val,
2098 * or end() if no elements are greater than @p __val.
2099 * @ingroup binary_search_algorithms
2101 * The comparison function should have the same effects on ordering as
2102 * the function used for the initial sort.
2104 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2105 inline _ForwardIterator
2106 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2107 const _Tp
& __val
, _Compare __comp
)
2109 // concept requirements
2110 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2111 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2112 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2113 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2116 return std::__upper_bound(__first
, __last
, __val
,
2117 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2120 template<typename _ForwardIterator
, typename _Tp
,
2121 typename _CompareItTp
, typename _CompareTpIt
>
2122 pair
<_ForwardIterator
, _ForwardIterator
>
2123 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2125 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2127 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2130 _DistanceType __len
= std::distance(__first
, __last
);
2134 _DistanceType __half
= __len
>> 1;
2135 _ForwardIterator __middle
= __first
;
2136 std::advance(__middle
, __half
);
2137 if (__comp_it_val(__middle
, __val
))
2141 __len
= __len
- __half
- 1;
2143 else if (__comp_val_it(__val
, __middle
))
2147 _ForwardIterator __left
2148 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2149 std::advance(__first
, __len
);
2150 _ForwardIterator __right
2151 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2152 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2155 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2159 * @brief Finds the largest subrange in which @p __val could be inserted
2160 * at any place in it without changing the ordering.
2161 * @ingroup binary_search_algorithms
2162 * @param __first An iterator.
2163 * @param __last Another iterator.
2164 * @param __val The search term.
2165 * @return An pair of iterators defining the subrange.
2166 * @ingroup binary_search_algorithms
2168 * This is equivalent to
2170 * std::make_pair(lower_bound(__first, __last, __val),
2171 * upper_bound(__first, __last, __val))
2173 * but does not actually call those functions.
2175 template<typename _ForwardIterator
, typename _Tp
>
2176 inline pair
<_ForwardIterator
, _ForwardIterator
>
2177 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2180 // concept requirements
2181 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2182 __glibcxx_function_requires(_LessThanOpConcept
<
2183 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2184 __glibcxx_function_requires(_LessThanOpConcept
<
2185 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2186 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2187 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2189 return std::__equal_range(__first
, __last
, __val
,
2190 __gnu_cxx::__ops::__iter_less_val(),
2191 __gnu_cxx::__ops::__val_less_iter());
2195 * @brief Finds the largest subrange in which @p __val could be inserted
2196 * at any place in it without changing the ordering.
2197 * @param __first An iterator.
2198 * @param __last Another iterator.
2199 * @param __val The search term.
2200 * @param __comp A functor to use for comparisons.
2201 * @return An pair of iterators defining the subrange.
2202 * @ingroup binary_search_algorithms
2204 * This is equivalent to
2206 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2207 * upper_bound(__first, __last, __val, __comp))
2209 * but does not actually call those functions.
2211 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2212 inline pair
<_ForwardIterator
, _ForwardIterator
>
2213 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2214 const _Tp
& __val
, _Compare __comp
)
2216 // concept requirements
2217 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2218 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2219 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2220 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2221 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2222 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2224 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2227 return std::__equal_range(__first
, __last
, __val
,
2228 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2229 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2233 * @brief Determines whether an element exists in a range.
2234 * @ingroup binary_search_algorithms
2235 * @param __first An iterator.
2236 * @param __last Another iterator.
2237 * @param __val The search term.
2238 * @return True if @p __val (or its equivalent) is in [@p
2239 * __first,@p __last ].
2241 * Note that this does not actually return an iterator to @p __val. For
2242 * that, use std::find or a container's specialized find member functions.
2244 template<typename _ForwardIterator
, typename _Tp
>
2246 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2249 // concept requirements
2250 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
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 _ForwardIterator __i
2257 = std::__lower_bound(__first
, __last
, __val
,
2258 __gnu_cxx::__ops::__iter_less_val());
2259 return __i
!= __last
&& !(__val
< *__i
);
2263 * @brief Determines whether an element exists in a range.
2264 * @ingroup binary_search_algorithms
2265 * @param __first An iterator.
2266 * @param __last Another iterator.
2267 * @param __val The search term.
2268 * @param __comp A functor to use for comparisons.
2269 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2271 * Note that this does not actually return an iterator to @p __val. For
2272 * that, use std::find or a container's specialized find member functions.
2274 * The comparison function should have the same effects on ordering as
2275 * the function used for the initial sort.
2277 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2279 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2280 const _Tp
& __val
, _Compare __comp
)
2282 // concept requirements
2283 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2284 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2285 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2286 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2288 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2291 _ForwardIterator __i
2292 = std::__lower_bound(__first
, __last
, __val
,
2293 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2294 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2299 /// This is a helper function for the __merge_adaptive routines.
2300 template<typename _InputIterator1
, typename _InputIterator2
,
2301 typename _OutputIterator
, typename _Compare
>
2303 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2304 _InputIterator2 __first2
, _InputIterator2 __last2
,
2305 _OutputIterator __result
, _Compare __comp
)
2307 while (__first1
!= __last1
&& __first2
!= __last2
)
2309 if (__comp(__first2
, __first1
))
2311 *__result
= _GLIBCXX_MOVE(*__first2
);
2316 *__result
= _GLIBCXX_MOVE(*__first1
);
2321 if (__first1
!= __last1
)
2322 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2325 /// This is a helper function for the __merge_adaptive routines.
2326 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2327 typename _BidirectionalIterator3
, typename _Compare
>
2329 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2330 _BidirectionalIterator1 __last1
,
2331 _BidirectionalIterator2 __first2
,
2332 _BidirectionalIterator2 __last2
,
2333 _BidirectionalIterator3 __result
,
2336 if (__first1
== __last1
)
2338 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2341 else if (__first2
== __last2
)
2348 if (__comp(__last2
, __last1
))
2350 *--__result
= _GLIBCXX_MOVE(*__last1
);
2351 if (__first1
== __last1
)
2353 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2360 *--__result
= _GLIBCXX_MOVE(*__last2
);
2361 if (__first2
== __last2
)
2368 /// This is a helper function for the merge routines.
2369 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2371 _BidirectionalIterator1
2372 __rotate_adaptive(_BidirectionalIterator1 __first
,
2373 _BidirectionalIterator1 __middle
,
2374 _BidirectionalIterator1 __last
,
2375 _Distance __len1
, _Distance __len2
,
2376 _BidirectionalIterator2 __buffer
,
2377 _Distance __buffer_size
)
2379 _BidirectionalIterator2 __buffer_end
;
2380 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2384 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2385 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2386 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2391 else if (__len1
<= __buffer_size
)
2395 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2396 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2397 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2403 return std::rotate(__first
, __middle
, __last
);
2406 /// This is a helper function for the merge routines.
2407 template<typename _BidirectionalIterator
, typename _Distance
,
2408 typename _Pointer
, typename _Compare
>
2410 __merge_adaptive(_BidirectionalIterator __first
,
2411 _BidirectionalIterator __middle
,
2412 _BidirectionalIterator __last
,
2413 _Distance __len1
, _Distance __len2
,
2414 _Pointer __buffer
, _Distance __buffer_size
,
2417 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2419 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2420 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2423 else if (__len2
<= __buffer_size
)
2425 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2426 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2427 __buffer_end
, __last
, __comp
);
2431 _BidirectionalIterator __first_cut
= __first
;
2432 _BidirectionalIterator __second_cut
= __middle
;
2433 _Distance __len11
= 0;
2434 _Distance __len22
= 0;
2435 if (__len1
> __len2
)
2437 __len11
= __len1
/ 2;
2438 std::advance(__first_cut
, __len11
);
2440 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2441 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2442 __len22
= std::distance(__middle
, __second_cut
);
2446 __len22
= __len2
/ 2;
2447 std::advance(__second_cut
, __len22
);
2449 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2450 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2451 __len11
= std::distance(__first
, __first_cut
);
2454 _BidirectionalIterator __new_middle
2455 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2456 __len1
- __len11
, __len22
, __buffer
,
2458 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2459 __len22
, __buffer
, __buffer_size
, __comp
);
2460 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2462 __len2
- __len22
, __buffer
,
2463 __buffer_size
, __comp
);
2467 /// This is a helper function for the merge routines.
2468 template<typename _BidirectionalIterator
, typename _Distance
,
2471 __merge_without_buffer(_BidirectionalIterator __first
,
2472 _BidirectionalIterator __middle
,
2473 _BidirectionalIterator __last
,
2474 _Distance __len1
, _Distance __len2
,
2477 if (__len1
== 0 || __len2
== 0)
2480 if (__len1
+ __len2
== 2)
2482 if (__comp(__middle
, __first
))
2483 std::iter_swap(__first
, __middle
);
2487 _BidirectionalIterator __first_cut
= __first
;
2488 _BidirectionalIterator __second_cut
= __middle
;
2489 _Distance __len11
= 0;
2490 _Distance __len22
= 0;
2491 if (__len1
> __len2
)
2493 __len11
= __len1
/ 2;
2494 std::advance(__first_cut
, __len11
);
2496 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2497 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2498 __len22
= std::distance(__middle
, __second_cut
);
2502 __len22
= __len2
/ 2;
2503 std::advance(__second_cut
, __len22
);
2505 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2506 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2507 __len11
= std::distance(__first
, __first_cut
);
2510 _BidirectionalIterator __new_middle
2511 = std::rotate(__first_cut
, __middle
, __second_cut
);
2512 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2513 __len11
, __len22
, __comp
);
2514 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2515 __len1
- __len11
, __len2
- __len22
, __comp
);
2518 template<typename _BidirectionalIterator
, typename _Compare
>
2520 __inplace_merge(_BidirectionalIterator __first
,
2521 _BidirectionalIterator __middle
,
2522 _BidirectionalIterator __last
,
2525 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2527 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2530 if (__first
== __middle
|| __middle
== __last
)
2533 const _DistanceType __len1
= std::distance(__first
, __middle
);
2534 const _DistanceType __len2
= std::distance(__middle
, __last
);
2536 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2537 _TmpBuf
__buf(__first
, __len1
+ __len2
);
2539 if (__buf
.begin() == 0)
2540 std::__merge_without_buffer
2541 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2543 std::__merge_adaptive
2544 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2545 _DistanceType(__buf
.size()), __comp
);
2549 * @brief Merges two sorted ranges in place.
2550 * @ingroup sorting_algorithms
2551 * @param __first An iterator.
2552 * @param __middle Another iterator.
2553 * @param __last Another iterator.
2556 * Merges two sorted and consecutive ranges, [__first,__middle) and
2557 * [__middle,__last), and puts the result in [__first,__last). The
2558 * output will be sorted. The sort is @e stable, that is, for
2559 * equivalent elements in the two ranges, elements from the first
2560 * range will always come before elements from the second.
2562 * If enough additional memory is available, this takes (__last-__first)-1
2563 * comparisons. Otherwise an NlogN algorithm is used, where N is
2564 * distance(__first,__last).
2566 template<typename _BidirectionalIterator
>
2568 inplace_merge(_BidirectionalIterator __first
,
2569 _BidirectionalIterator __middle
,
2570 _BidirectionalIterator __last
)
2572 // concept requirements
2573 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2574 _BidirectionalIterator
>)
2575 __glibcxx_function_requires(_LessThanComparableConcept
<
2576 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2577 __glibcxx_requires_sorted(__first
, __middle
);
2578 __glibcxx_requires_sorted(__middle
, __last
);
2579 __glibcxx_requires_irreflexive(__first
, __last
);
2581 std::__inplace_merge(__first
, __middle
, __last
,
2582 __gnu_cxx::__ops::__iter_less_iter());
2586 * @brief Merges two sorted ranges in place.
2587 * @ingroup sorting_algorithms
2588 * @param __first An iterator.
2589 * @param __middle Another iterator.
2590 * @param __last Another iterator.
2591 * @param __comp A functor to use for comparisons.
2594 * Merges two sorted and consecutive ranges, [__first,__middle) and
2595 * [middle,last), and puts the result in [__first,__last). The output will
2596 * be sorted. The sort is @e stable, that is, for equivalent
2597 * elements in the two ranges, elements from the first range will always
2598 * come before elements from the second.
2600 * If enough additional memory is available, this takes (__last-__first)-1
2601 * comparisons. Otherwise an NlogN algorithm is used, where N is
2602 * distance(__first,__last).
2604 * The comparison function should have the same effects on ordering as
2605 * the function used for the initial sort.
2607 template<typename _BidirectionalIterator
, typename _Compare
>
2609 inplace_merge(_BidirectionalIterator __first
,
2610 _BidirectionalIterator __middle
,
2611 _BidirectionalIterator __last
,
2614 // concept requirements
2615 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2616 _BidirectionalIterator
>)
2617 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2618 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2619 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2620 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2621 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2622 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2624 std::__inplace_merge(__first
, __middle
, __last
,
2625 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2629 /// This is a helper function for the __merge_sort_loop routines.
2630 template<typename _InputIterator
, typename _OutputIterator
,
2633 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2634 _InputIterator __first2
, _InputIterator __last2
,
2635 _OutputIterator __result
, _Compare __comp
)
2637 while (__first1
!= __last1
&& __first2
!= __last2
)
2639 if (__comp(__first2
, __first1
))
2641 *__result
= _GLIBCXX_MOVE(*__first2
);
2646 *__result
= _GLIBCXX_MOVE(*__first1
);
2651 return _GLIBCXX_MOVE3(__first2
, __last2
,
2652 _GLIBCXX_MOVE3(__first1
, __last1
,
2656 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2657 typename _Distance
, typename _Compare
>
2659 __merge_sort_loop(_RandomAccessIterator1 __first
,
2660 _RandomAccessIterator1 __last
,
2661 _RandomAccessIterator2 __result
, _Distance __step_size
,
2664 const _Distance __two_step
= 2 * __step_size
;
2666 while (__last
- __first
>= __two_step
)
2668 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2669 __first
+ __step_size
,
2670 __first
+ __two_step
,
2672 __first
+= __two_step
;
2674 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2676 std::__move_merge(__first
, __first
+ __step_size
,
2677 __first
+ __step_size
, __last
, __result
, __comp
);
2680 template<typename _RandomAccessIterator
, typename _Distance
,
2683 __chunk_insertion_sort(_RandomAccessIterator __first
,
2684 _RandomAccessIterator __last
,
2685 _Distance __chunk_size
, _Compare __comp
)
2687 while (__last
- __first
>= __chunk_size
)
2689 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2690 __first
+= __chunk_size
;
2692 std::__insertion_sort(__first
, __last
, __comp
);
2695 enum { _S_chunk_size
= 7 };
2697 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2699 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2700 _RandomAccessIterator __last
,
2701 _Pointer __buffer
, _Compare __comp
)
2703 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2706 const _Distance __len
= __last
- __first
;
2707 const _Pointer __buffer_last
= __buffer
+ __len
;
2709 _Distance __step_size
= _S_chunk_size
;
2710 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2712 while (__step_size
< __len
)
2714 std::__merge_sort_loop(__first
, __last
, __buffer
,
2715 __step_size
, __comp
);
2717 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2718 __step_size
, __comp
);
2723 template<typename _RandomAccessIterator
, typename _Pointer
,
2724 typename _Distance
, typename _Compare
>
2726 __stable_sort_adaptive(_RandomAccessIterator __first
,
2727 _RandomAccessIterator __last
,
2728 _Pointer __buffer
, _Distance __buffer_size
,
2731 const _Distance __len
= (__last
- __first
+ 1) / 2;
2732 const _RandomAccessIterator __middle
= __first
+ __len
;
2733 if (__len
> __buffer_size
)
2735 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2736 __buffer_size
, __comp
);
2737 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2738 __buffer_size
, __comp
);
2742 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2743 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2745 std::__merge_adaptive(__first
, __middle
, __last
,
2746 _Distance(__middle
- __first
),
2747 _Distance(__last
- __middle
),
2748 __buffer
, __buffer_size
,
2752 /// This is a helper function for the stable sorting routines.
2753 template<typename _RandomAccessIterator
, typename _Compare
>
2755 __inplace_stable_sort(_RandomAccessIterator __first
,
2756 _RandomAccessIterator __last
, _Compare __comp
)
2758 if (__last
- __first
< 15)
2760 std::__insertion_sort(__first
, __last
, __comp
);
2763 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2764 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2765 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2766 std::__merge_without_buffer(__first
, __middle
, __last
,
2774 // Set algorithms: includes, set_union, set_intersection, set_difference,
2775 // set_symmetric_difference. All of these algorithms have the precondition
2776 // that their input ranges are sorted and the postcondition that their output
2777 // ranges are sorted.
2779 template<typename _InputIterator1
, typename _InputIterator2
,
2782 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2783 _InputIterator2 __first2
, _InputIterator2 __last2
,
2786 while (__first1
!= __last1
&& __first2
!= __last2
)
2787 if (__comp(__first2
, __first1
))
2789 else if (__comp(__first1
, __first2
))
2797 return __first2
== __last2
;
2801 * @brief Determines whether all elements of a sequence exists in a range.
2802 * @param __first1 Start of search range.
2803 * @param __last1 End of search range.
2804 * @param __first2 Start of sequence
2805 * @param __last2 End of sequence.
2806 * @return True if each element in [__first2,__last2) is contained in order
2807 * within [__first1,__last1). False otherwise.
2808 * @ingroup set_algorithms
2810 * This operation expects both [__first1,__last1) and
2811 * [__first2,__last2) to be sorted. Searches for the presence of
2812 * each element in [__first2,__last2) within [__first1,__last1).
2813 * The iterators over each range only move forward, so this is a
2814 * linear algorithm. If an element in [__first2,__last2) is not
2815 * found before the search iterator reaches @p __last2, false is
2818 template<typename _InputIterator1
, typename _InputIterator2
>
2820 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2821 _InputIterator2 __first2
, _InputIterator2 __last2
)
2823 // concept requirements
2824 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2825 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2826 __glibcxx_function_requires(_LessThanOpConcept
<
2827 typename iterator_traits
<_InputIterator1
>::value_type
,
2828 typename iterator_traits
<_InputIterator2
>::value_type
>)
2829 __glibcxx_function_requires(_LessThanOpConcept
<
2830 typename iterator_traits
<_InputIterator2
>::value_type
,
2831 typename iterator_traits
<_InputIterator1
>::value_type
>)
2832 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2833 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2834 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2835 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2837 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2838 __gnu_cxx::__ops::__iter_less_iter());
2842 * @brief Determines whether all elements of a sequence exists in a range
2844 * @ingroup set_algorithms
2845 * @param __first1 Start of search range.
2846 * @param __last1 End of search range.
2847 * @param __first2 Start of sequence
2848 * @param __last2 End of sequence.
2849 * @param __comp Comparison function to use.
2850 * @return True if each element in [__first2,__last2) is contained
2851 * in order within [__first1,__last1) according to comp. False
2852 * otherwise. @ingroup set_algorithms
2854 * This operation expects both [__first1,__last1) and
2855 * [__first2,__last2) to be sorted. Searches for the presence of
2856 * each element in [__first2,__last2) within [__first1,__last1),
2857 * using comp to decide. The iterators over each range only move
2858 * forward, so this is a linear algorithm. If an element in
2859 * [__first2,__last2) is not found before the search iterator
2860 * reaches @p __last2, false is returned.
2862 template<typename _InputIterator1
, typename _InputIterator2
,
2865 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2866 _InputIterator2 __first2
, _InputIterator2 __last2
,
2869 // concept requirements
2870 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2871 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2872 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2873 typename iterator_traits
<_InputIterator1
>::value_type
,
2874 typename iterator_traits
<_InputIterator2
>::value_type
>)
2875 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2876 typename iterator_traits
<_InputIterator2
>::value_type
,
2877 typename iterator_traits
<_InputIterator1
>::value_type
>)
2878 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2879 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2880 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2881 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2883 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2884 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2893 // set_symmetric_difference
2897 template<typename _BidirectionalIterator
, typename _Compare
>
2899 __next_permutation(_BidirectionalIterator __first
,
2900 _BidirectionalIterator __last
, _Compare __comp
)
2902 if (__first
== __last
)
2904 _BidirectionalIterator __i
= __first
;
2913 _BidirectionalIterator __ii
= __i
;
2915 if (__comp(__i
, __ii
))
2917 _BidirectionalIterator __j
= __last
;
2918 while (!__comp(__i
, --__j
))
2920 std::iter_swap(__i
, __j
);
2921 std::__reverse(__ii
, __last
,
2922 std::__iterator_category(__first
));
2927 std::__reverse(__first
, __last
,
2928 std::__iterator_category(__first
));
2935 * @brief Permute range into the next @e dictionary ordering.
2936 * @ingroup sorting_algorithms
2937 * @param __first Start of range.
2938 * @param __last End of range.
2939 * @return False if wrapped to first permutation, true otherwise.
2941 * Treats all permutations of the range as a set of @e dictionary sorted
2942 * sequences. Permutes the current sequence into the next one of this set.
2943 * Returns true if there are more sequences to generate. If the sequence
2944 * is the largest of the set, the smallest is generated and false returned.
2946 template<typename _BidirectionalIterator
>
2948 next_permutation(_BidirectionalIterator __first
,
2949 _BidirectionalIterator __last
)
2951 // concept requirements
2952 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2953 _BidirectionalIterator
>)
2954 __glibcxx_function_requires(_LessThanComparableConcept
<
2955 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2956 __glibcxx_requires_valid_range(__first
, __last
);
2957 __glibcxx_requires_irreflexive(__first
, __last
);
2959 return std::__next_permutation
2960 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2964 * @brief Permute range into the next @e dictionary ordering using
2965 * comparison functor.
2966 * @ingroup sorting_algorithms
2967 * @param __first Start of range.
2968 * @param __last End of range.
2969 * @param __comp A comparison functor.
2970 * @return False if wrapped to first permutation, true otherwise.
2972 * Treats all permutations of the range [__first,__last) as a set of
2973 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2974 * sequence into the next one of this set. Returns true if there are more
2975 * sequences to generate. If the sequence is the largest of the set, the
2976 * smallest is generated and false returned.
2978 template<typename _BidirectionalIterator
, typename _Compare
>
2980 next_permutation(_BidirectionalIterator __first
,
2981 _BidirectionalIterator __last
, _Compare __comp
)
2983 // concept requirements
2984 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2985 _BidirectionalIterator
>)
2986 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2987 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2988 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2989 __glibcxx_requires_valid_range(__first
, __last
);
2990 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2992 return std::__next_permutation
2993 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2996 template<typename _BidirectionalIterator
, typename _Compare
>
2998 __prev_permutation(_BidirectionalIterator __first
,
2999 _BidirectionalIterator __last
, _Compare __comp
)
3001 if (__first
== __last
)
3003 _BidirectionalIterator __i
= __first
;
3012 _BidirectionalIterator __ii
= __i
;
3014 if (__comp(__ii
, __i
))
3016 _BidirectionalIterator __j
= __last
;
3017 while (!__comp(--__j
, __i
))
3019 std::iter_swap(__i
, __j
);
3020 std::__reverse(__ii
, __last
,
3021 std::__iterator_category(__first
));
3026 std::__reverse(__first
, __last
,
3027 std::__iterator_category(__first
));
3034 * @brief Permute range into the previous @e dictionary ordering.
3035 * @ingroup sorting_algorithms
3036 * @param __first Start of range.
3037 * @param __last End of range.
3038 * @return False if wrapped to last permutation, true otherwise.
3040 * Treats all permutations of the range as a set of @e dictionary sorted
3041 * sequences. Permutes the current sequence into the previous one of this
3042 * set. Returns true if there are more sequences to generate. If the
3043 * sequence is the smallest of the set, the largest is generated and false
3046 template<typename _BidirectionalIterator
>
3048 prev_permutation(_BidirectionalIterator __first
,
3049 _BidirectionalIterator __last
)
3051 // concept requirements
3052 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3053 _BidirectionalIterator
>)
3054 __glibcxx_function_requires(_LessThanComparableConcept
<
3055 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3056 __glibcxx_requires_valid_range(__first
, __last
);
3057 __glibcxx_requires_irreflexive(__first
, __last
);
3059 return std::__prev_permutation(__first
, __last
,
3060 __gnu_cxx::__ops::__iter_less_iter());
3064 * @brief Permute range into the previous @e dictionary ordering using
3065 * comparison functor.
3066 * @ingroup sorting_algorithms
3067 * @param __first Start of range.
3068 * @param __last End of range.
3069 * @param __comp A comparison functor.
3070 * @return False if wrapped to last permutation, true otherwise.
3072 * Treats all permutations of the range [__first,__last) as a set of
3073 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3074 * sequence into the previous one of this set. Returns true if there are
3075 * more sequences to generate. If the sequence is the smallest of the set,
3076 * the largest is generated and false returned.
3078 template<typename _BidirectionalIterator
, typename _Compare
>
3080 prev_permutation(_BidirectionalIterator __first
,
3081 _BidirectionalIterator __last
, _Compare __comp
)
3083 // concept requirements
3084 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3085 _BidirectionalIterator
>)
3086 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3087 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3088 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3089 __glibcxx_requires_valid_range(__first
, __last
);
3090 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3092 return std::__prev_permutation(__first
, __last
,
3093 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3099 template<typename _InputIterator
, typename _OutputIterator
,
3100 typename _Predicate
, typename _Tp
>
3102 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3103 _OutputIterator __result
,
3104 _Predicate __pred
, const _Tp
& __new_value
)
3106 for (; __first
!= __last
; ++__first
, (void)++__result
)
3107 if (__pred(__first
))
3108 *__result
= __new_value
;
3110 *__result
= *__first
;
3115 * @brief Copy a sequence, replacing each element of one value with another
3117 * @param __first An input iterator.
3118 * @param __last An input iterator.
3119 * @param __result An output iterator.
3120 * @param __old_value The value to be replaced.
3121 * @param __new_value The replacement value.
3122 * @return The end of the output sequence, @p result+(last-first).
3124 * Copies each element in the input range @p [__first,__last) to the
3125 * output range @p [__result,__result+(__last-__first)) replacing elements
3126 * equal to @p __old_value with @p __new_value.
3128 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3129 inline _OutputIterator
3130 replace_copy(_InputIterator __first
, _InputIterator __last
,
3131 _OutputIterator __result
,
3132 const _Tp
& __old_value
, const _Tp
& __new_value
)
3134 // concept requirements
3135 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3136 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3137 typename iterator_traits
<_InputIterator
>::value_type
>)
3138 __glibcxx_function_requires(_EqualOpConcept
<
3139 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3140 __glibcxx_requires_valid_range(__first
, __last
);
3142 return std::__replace_copy_if(__first
, __last
, __result
,
3143 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3148 * @brief Copy a sequence, replacing each value for which a predicate
3149 * returns true with another value.
3150 * @ingroup mutating_algorithms
3151 * @param __first An input iterator.
3152 * @param __last An input iterator.
3153 * @param __result An output iterator.
3154 * @param __pred A predicate.
3155 * @param __new_value The replacement value.
3156 * @return The end of the output sequence, @p __result+(__last-__first).
3158 * Copies each element in the range @p [__first,__last) to the range
3159 * @p [__result,__result+(__last-__first)) replacing elements for which
3160 * @p __pred returns true with @p __new_value.
3162 template<typename _InputIterator
, typename _OutputIterator
,
3163 typename _Predicate
, typename _Tp
>
3164 inline _OutputIterator
3165 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3166 _OutputIterator __result
,
3167 _Predicate __pred
, const _Tp
& __new_value
)
3169 // concept requirements
3170 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3171 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3172 typename iterator_traits
<_InputIterator
>::value_type
>)
3173 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3174 typename iterator_traits
<_InputIterator
>::value_type
>)
3175 __glibcxx_requires_valid_range(__first
, __last
);
3177 return std::__replace_copy_if(__first
, __last
, __result
,
3178 __gnu_cxx::__ops::__pred_iter(__pred
),
3182 template<typename _InputIterator
, typename _Predicate
>
3183 typename iterator_traits
<_InputIterator
>::difference_type
3184 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3186 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3187 for (; __first
!= __last
; ++__first
)
3188 if (__pred(__first
))
3193 #if __cplusplus >= 201103L
3195 * @brief Determines whether the elements of a sequence are sorted.
3196 * @ingroup sorting_algorithms
3197 * @param __first An iterator.
3198 * @param __last Another iterator.
3199 * @return True if the elements are sorted, false otherwise.
3201 template<typename _ForwardIterator
>
3203 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3204 { return std::is_sorted_until(__first
, __last
) == __last
; }
3207 * @brief Determines whether the elements of a sequence are sorted
3208 * according to a comparison functor.
3209 * @ingroup sorting_algorithms
3210 * @param __first An iterator.
3211 * @param __last Another iterator.
3212 * @param __comp A comparison functor.
3213 * @return True if the elements are sorted, false otherwise.
3215 template<typename _ForwardIterator
, typename _Compare
>
3217 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3219 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3221 template<typename _ForwardIterator
, typename _Compare
>
3223 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3226 if (__first
== __last
)
3229 _ForwardIterator __next
= __first
;
3230 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3231 if (__comp(__next
, __first
))
3237 * @brief Determines the end of a sorted sequence.
3238 * @ingroup sorting_algorithms
3239 * @param __first An iterator.
3240 * @param __last Another iterator.
3241 * @return An iterator pointing to the last iterator i in [__first, __last)
3242 * for which the range [__first, i) is sorted.
3244 template<typename _ForwardIterator
>
3245 inline _ForwardIterator
3246 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3248 // concept requirements
3249 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3250 __glibcxx_function_requires(_LessThanComparableConcept
<
3251 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3252 __glibcxx_requires_valid_range(__first
, __last
);
3253 __glibcxx_requires_irreflexive(__first
, __last
);
3255 return std::__is_sorted_until(__first
, __last
,
3256 __gnu_cxx::__ops::__iter_less_iter());
3260 * @brief Determines the end of a sorted sequence using comparison functor.
3261 * @ingroup sorting_algorithms
3262 * @param __first An iterator.
3263 * @param __last Another iterator.
3264 * @param __comp A comparison functor.
3265 * @return An iterator pointing to the last iterator i in [__first, __last)
3266 * for which the range [__first, i) is sorted.
3268 template<typename _ForwardIterator
, typename _Compare
>
3269 inline _ForwardIterator
3270 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3273 // concept requirements
3274 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3275 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3276 typename iterator_traits
<_ForwardIterator
>::value_type
,
3277 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3278 __glibcxx_requires_valid_range(__first
, __last
);
3279 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3281 return std::__is_sorted_until(__first
, __last
,
3282 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3286 * @brief Determines min and max at once as an ordered pair.
3287 * @ingroup sorting_algorithms
3288 * @param __a A thing of arbitrary type.
3289 * @param __b Another thing of arbitrary type.
3290 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3293 template<typename _Tp
>
3294 _GLIBCXX14_CONSTEXPR
3295 inline pair
<const _Tp
&, const _Tp
&>
3296 minmax(const _Tp
& __a
, const _Tp
& __b
)
3298 // concept requirements
3299 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3301 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3302 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3306 * @brief Determines min and max at once as an ordered pair.
3307 * @ingroup sorting_algorithms
3308 * @param __a A thing of arbitrary type.
3309 * @param __b Another thing of arbitrary type.
3310 * @param __comp A @link comparison_functors comparison functor @endlink.
3311 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3314 template<typename _Tp
, typename _Compare
>
3315 _GLIBCXX14_CONSTEXPR
3316 inline pair
<const _Tp
&, const _Tp
&>
3317 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3319 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3320 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3323 template<typename _ForwardIterator
, typename _Compare
>
3324 _GLIBCXX14_CONSTEXPR
3325 pair
<_ForwardIterator
, _ForwardIterator
>
3326 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3329 _ForwardIterator __next
= __first
;
3330 if (__first
== __last
3331 || ++__next
== __last
)
3332 return std::make_pair(__first
, __first
);
3334 _ForwardIterator __min
{}, __max
{};
3335 if (__comp(__next
, __first
))
3349 while (__first
!= __last
)
3352 if (++__next
== __last
)
3354 if (__comp(__first
, __min
))
3356 else if (!__comp(__first
, __max
))
3361 if (__comp(__next
, __first
))
3363 if (__comp(__next
, __min
))
3365 if (!__comp(__first
, __max
))
3370 if (__comp(__first
, __min
))
3372 if (!__comp(__next
, __max
))
3380 return std::make_pair(__min
, __max
);
3384 * @brief Return a pair of iterators pointing to the minimum and maximum
3385 * elements in a range.
3386 * @ingroup sorting_algorithms
3387 * @param __first Start of range.
3388 * @param __last End of range.
3389 * @return make_pair(m, M), where m is the first iterator i in
3390 * [__first, __last) such that no other element in the range is
3391 * smaller, and where M is the last iterator i in [__first, __last)
3392 * such that no other element in the range is larger.
3394 template<typename _ForwardIterator
>
3395 _GLIBCXX14_CONSTEXPR
3396 inline pair
<_ForwardIterator
, _ForwardIterator
>
3397 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3399 // concept requirements
3400 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3401 __glibcxx_function_requires(_LessThanComparableConcept
<
3402 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3403 __glibcxx_requires_valid_range(__first
, __last
);
3404 __glibcxx_requires_irreflexive(__first
, __last
);
3406 return std::__minmax_element(__first
, __last
,
3407 __gnu_cxx::__ops::__iter_less_iter());
3411 * @brief Return a pair of iterators pointing to the minimum and maximum
3412 * elements in a range.
3413 * @ingroup sorting_algorithms
3414 * @param __first Start of range.
3415 * @param __last End of range.
3416 * @param __comp Comparison functor.
3417 * @return make_pair(m, M), where m is the first iterator i in
3418 * [__first, __last) such that no other element in the range is
3419 * smaller, and where M is the last iterator i in [__first, __last)
3420 * such that no other element in the range is larger.
3422 template<typename _ForwardIterator
, typename _Compare
>
3423 _GLIBCXX14_CONSTEXPR
3424 inline pair
<_ForwardIterator
, _ForwardIterator
>
3425 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3428 // concept requirements
3429 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3430 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3431 typename iterator_traits
<_ForwardIterator
>::value_type
,
3432 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3433 __glibcxx_requires_valid_range(__first
, __last
);
3434 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3436 return std::__minmax_element(__first
, __last
,
3437 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3441 template<typename _Tp
>
3442 _GLIBCXX14_CONSTEXPR
3444 min(initializer_list
<_Tp
> __l
)
3445 { return *std::min_element(__l
.begin(), __l
.end()); }
3447 template<typename _Tp
, typename _Compare
>
3448 _GLIBCXX14_CONSTEXPR
3450 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3451 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3453 template<typename _Tp
>
3454 _GLIBCXX14_CONSTEXPR
3456 max(initializer_list
<_Tp
> __l
)
3457 { return *std::max_element(__l
.begin(), __l
.end()); }
3459 template<typename _Tp
, typename _Compare
>
3460 _GLIBCXX14_CONSTEXPR
3462 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3463 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3465 template<typename _Tp
>
3466 _GLIBCXX14_CONSTEXPR
3467 inline pair
<_Tp
, _Tp
>
3468 minmax(initializer_list
<_Tp
> __l
)
3470 pair
<const _Tp
*, const _Tp
*> __p
=
3471 std::minmax_element(__l
.begin(), __l
.end());
3472 return std::make_pair(*__p
.first
, *__p
.second
);
3475 template<typename _Tp
, typename _Compare
>
3476 _GLIBCXX14_CONSTEXPR
3477 inline pair
<_Tp
, _Tp
>
3478 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3480 pair
<const _Tp
*, const _Tp
*> __p
=
3481 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3482 return std::make_pair(*__p
.first
, *__p
.second
);
3485 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3486 typename _BinaryPredicate
>
3488 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3489 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3491 // Efficiently compare identical prefixes: O(N) if sequences
3492 // have the same elements in the same order.
3493 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
3494 if (!__pred(__first1
, __first2
))
3497 if (__first1
== __last1
)
3500 // Establish __last2 assuming equal ranges by iterating over the
3501 // rest of the list.
3502 _ForwardIterator2 __last2
= __first2
;
3503 std::advance(__last2
, std::distance(__first1
, __last1
));
3504 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3506 if (__scan
!= std::__find_if(__first1
, __scan
,
3507 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3508 continue; // We've seen this one before.
3511 = std::__count_if(__first2
, __last2
,
3512 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3513 if (0 == __matches
||
3514 std::__count_if(__scan
, __last1
,
3515 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3523 * @brief Checks whether a permutation of the second sequence is equal
3524 * to the first sequence.
3525 * @ingroup non_mutating_algorithms
3526 * @param __first1 Start of first range.
3527 * @param __last1 End of first range.
3528 * @param __first2 Start of second range.
3529 * @return true if there exists a permutation of the elements in the range
3530 * [__first2, __first2 + (__last1 - __first1)), beginning with
3531 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3532 * returns true; otherwise, returns false.
3534 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3536 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3537 _ForwardIterator2 __first2
)
3539 // concept requirements
3540 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3541 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3542 __glibcxx_function_requires(_EqualOpConcept
<
3543 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3544 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3545 __glibcxx_requires_valid_range(__first1
, __last1
);
3547 return std::__is_permutation(__first1
, __last1
, __first2
,
3548 __gnu_cxx::__ops::__iter_equal_to_iter());
3552 * @brief Checks whether a permutation of the second sequence is equal
3553 * to the first sequence.
3554 * @ingroup non_mutating_algorithms
3555 * @param __first1 Start of first range.
3556 * @param __last1 End of first range.
3557 * @param __first2 Start of second range.
3558 * @param __pred A binary predicate.
3559 * @return true if there exists a permutation of the elements in
3560 * the range [__first2, __first2 + (__last1 - __first1)),
3561 * beginning with ForwardIterator2 begin, such that
3562 * equal(__first1, __last1, __begin, __pred) returns true;
3563 * otherwise, returns false.
3565 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3566 typename _BinaryPredicate
>
3568 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3569 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3571 // concept requirements
3572 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3573 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3574 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3575 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3576 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3577 __glibcxx_requires_valid_range(__first1
, __last1
);
3579 return std::__is_permutation(__first1
, __last1
, __first2
,
3580 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3583 #if __cplusplus > 201103L
3584 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3585 typename _BinaryPredicate
>
3587 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3588 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3589 _BinaryPredicate __pred
)
3592 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3594 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3595 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3596 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3597 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3600 auto __d1
= std::distance(__first1
, __last1
);
3601 auto __d2
= std::distance(__first2
, __last2
);
3606 // Efficiently compare identical prefixes: O(N) if sequences
3607 // have the same elements in the same order.
3608 for (; __first1
!= __last1
&& __first2
!= __last2
;
3609 ++__first1
, (void)++__first2
)
3610 if (!__pred(__first1
, __first2
))
3615 if (__first1
== __last1
)
3620 auto __d1
= std::distance(__first1
, __last1
);
3621 auto __d2
= std::distance(__first2
, __last2
);
3622 if (__d1
== 0 && __d2
== 0)
3628 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3630 if (__scan
!= std::__find_if(__first1
, __scan
,
3631 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3632 continue; // We've seen this one before.
3634 auto __matches
= std::__count_if(__first2
, __last2
,
3635 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3637 || std::__count_if(__scan
, __last1
,
3638 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3646 * @brief Checks whether a permutaion of the second sequence is equal
3647 * to the first sequence.
3648 * @ingroup non_mutating_algorithms
3649 * @param __first1 Start of first range.
3650 * @param __last1 End of first range.
3651 * @param __first2 Start of second range.
3652 * @param __last2 End of first range.
3653 * @return true if there exists a permutation of the elements in the range
3654 * [__first2, __last2), beginning with ForwardIterator2 begin,
3655 * such that equal(__first1, __last1, begin) returns true;
3656 * otherwise, returns false.
3658 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3660 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3661 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3663 __glibcxx_requires_valid_range(__first1
, __last1
);
3664 __glibcxx_requires_valid_range(__first2
, __last2
);
3667 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3668 __gnu_cxx::__ops::__iter_equal_to_iter());
3672 * @brief Checks whether a permutation of the second sequence is equal
3673 * to the first sequence.
3674 * @ingroup non_mutating_algorithms
3675 * @param __first1 Start of first range.
3676 * @param __last1 End of first range.
3677 * @param __first2 Start of second range.
3678 * @param __last2 End of first range.
3679 * @param __pred A binary predicate.
3680 * @return true if there exists a permutation of the elements in the range
3681 * [__first2, __last2), beginning with ForwardIterator2 begin,
3682 * such that equal(__first1, __last1, __begin, __pred) returns true;
3683 * otherwise, returns false.
3685 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3686 typename _BinaryPredicate
>
3688 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3689 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3690 _BinaryPredicate __pred
)
3692 __glibcxx_requires_valid_range(__first1
, __last1
);
3693 __glibcxx_requires_valid_range(__first2
, __last2
);
3695 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3696 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3699 #if __cplusplus > 201402L
3701 #define __cpp_lib_clamp 201603
3704 * @brief Returns the value clamped between lo and hi.
3705 * @ingroup sorting_algorithms
3706 * @param __val A value of arbitrary type.
3707 * @param __lo A lower limit of arbitrary type.
3708 * @param __hi An upper limit of arbitrary type.
3709 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
3711 template<typename _Tp
>
3712 constexpr const _Tp
&
3713 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
)
3715 __glibcxx_assert(!(__hi
< __lo
));
3716 return (__val
< __lo
) ? __lo
: (__hi
< __val
) ? __hi
: __val
;
3720 * @brief Returns the value clamped between lo and hi.
3721 * @ingroup sorting_algorithms
3722 * @param __val A value of arbitrary type.
3723 * @param __lo A lower limit of arbitrary type.
3724 * @param __hi An upper limit of arbitrary type.
3725 * @param __comp A comparison functor.
3726 * @return max(__val, __lo, __comp) if __comp(__val, __hi)
3727 * or min(__val, __hi, __comp) otherwise.
3729 template<typename _Tp
, typename _Compare
>
3730 constexpr const _Tp
&
3731 clamp(const _Tp
& __val
, const _Tp
& __lo
, const _Tp
& __hi
, _Compare __comp
)
3733 __glibcxx_assert(!__comp(__hi
, __lo
));
3734 return __comp(__val
, __lo
) ? __lo
: __comp(__hi
, __val
) ? __hi
: __val
;
3739 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3741 * @brief Generate two uniformly distributed integers using a
3742 * single distribution invocation.
3743 * @param __b0 The upper bound for the first integer.
3744 * @param __b1 The upper bound for the second integer.
3745 * @param __g A UniformRandomBitGenerator.
3746 * @return A pair (i, j) with i and j uniformly distributed
3747 * over [0, __b0) and [0, __b1), respectively.
3749 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3751 * Using uniform_int_distribution with a range that is very
3752 * small relative to the range of the generator ends up wasting
3753 * potentially expensively generated randomness, since
3754 * uniform_int_distribution does not store leftover randomness
3755 * between invocations.
3757 * If we know we want two integers in ranges that are sufficiently
3758 * small, we can compose the ranges, use a single distribution
3759 * invocation, and significantly reduce the waste.
3761 template<typename _IntType
, typename _UniformRandomBitGenerator
>
3762 pair
<_IntType
, _IntType
>
3763 __gen_two_uniform_ints(_IntType __b0
, _IntType __b1
,
3764 _UniformRandomBitGenerator
&& __g
)
3767 = uniform_int_distribution
<_IntType
>{0, (__b0
* __b1
) - 1}(__g
);
3768 return std::make_pair(__x
/ __b1
, __x
% __b1
);
3772 * @brief Shuffle the elements of a sequence using a uniform random
3774 * @ingroup mutating_algorithms
3775 * @param __first A forward iterator.
3776 * @param __last A forward iterator.
3777 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3780 * Reorders the elements in the range @p [__first,__last) using @p __g to
3781 * provide random numbers.
3783 template<typename _RandomAccessIterator
,
3784 typename _UniformRandomNumberGenerator
>
3786 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3787 _UniformRandomNumberGenerator
&& __g
)
3789 // concept requirements
3790 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3791 _RandomAccessIterator
>)
3792 __glibcxx_requires_valid_range(__first
, __last
);
3794 if (__first
== __last
)
3797 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3800 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3801 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3802 typedef typename
__distr_type::param_type __p_type
;
3804 typedef typename remove_reference
<_UniformRandomNumberGenerator
>::type
3806 typedef typename common_type
<typename
_Gen::result_type
, __ud_type
>::type
3809 const __uc_type __urngrange
= __g
.max() - __g
.min();
3810 const __uc_type __urange
= __uc_type(__last
- __first
);
3812 if (__urngrange
/ __urange
>= __urange
)
3813 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3815 _RandomAccessIterator __i
= __first
+ 1;
3817 // Since we know the range isn't empty, an even number of elements
3818 // means an uneven number of elements /to swap/, in which case we
3819 // do the first one up front:
3821 if ((__urange
% 2) == 0)
3823 __distr_type __d
{0, 1};
3824 std::iter_swap(__i
++, __first
+ __d(__g
));
3827 // Now we know that __last - __i is even, so we do the rest in pairs,
3828 // using a single distribution invocation to produce swap positions
3829 // for two successive elements at a time:
3831 while (__i
!= __last
)
3833 const __uc_type __swap_range
= __uc_type(__i
- __first
) + 1;
3835 const pair
<__uc_type
, __uc_type
> __pospos
=
3836 __gen_two_uniform_ints(__swap_range
, __swap_range
+ 1, __g
);
3838 std::iter_swap(__i
++, __first
+ __pospos
.first
);
3839 std::iter_swap(__i
++, __first
+ __pospos
.second
);
3847 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3848 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3854 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3857 * @brief Apply a function to every element of a sequence.
3858 * @ingroup non_mutating_algorithms
3859 * @param __first An input iterator.
3860 * @param __last An input iterator.
3861 * @param __f A unary function object.
3864 * Applies the function object @p __f to each element in the range
3865 * @p [first,last). @p __f must not modify the order of the sequence.
3866 * If @p __f has a return value it is ignored.
3868 template<typename _InputIterator
, typename _Function
>
3870 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3872 // concept requirements
3873 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3874 __glibcxx_requires_valid_range(__first
, __last
);
3875 for (; __first
!= __last
; ++__first
)
3877 return __f
; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3880 #if __cplusplus >= 201703L
3882 * @brief Apply a function to every element of a sequence.
3883 * @ingroup non_mutating_algorithms
3884 * @param __first An input iterator.
3885 * @param __n A value convertible to an integer.
3886 * @param __f A unary function object.
3887 * @return `__first+__n`
3889 * Applies the function object `__f` to each element in the range
3890 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3891 * If `__f` has a return value it is ignored.
3893 template<typename _InputIterator
, typename _Size
, typename _Function
>
3895 for_each_n(_InputIterator __first
, _Size __n
, _Function __f
)
3897 auto __n2
= std::__size_to_integer(__n
);
3898 using _Cat
= typename iterator_traits
<_InputIterator
>::iterator_category
;
3899 if constexpr (is_base_of_v
<random_access_iterator_tag
, _Cat
>)
3900 return std::for_each(__first
, __first
+ __n2
, __f
);
3914 * @brief Find the first occurrence of a value in a sequence.
3915 * @ingroup non_mutating_algorithms
3916 * @param __first An input iterator.
3917 * @param __last An input iterator.
3918 * @param __val The value to find.
3919 * @return The first iterator @c i in the range @p [__first,__last)
3920 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3922 template<typename _InputIterator
, typename _Tp
>
3923 inline _InputIterator
3924 find(_InputIterator __first
, _InputIterator __last
,
3927 // concept requirements
3928 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3929 __glibcxx_function_requires(_EqualOpConcept
<
3930 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3931 __glibcxx_requires_valid_range(__first
, __last
);
3932 return std::__find_if(__first
, __last
,
3933 __gnu_cxx::__ops::__iter_equals_val(__val
));
3937 * @brief Find the first element in a sequence for which a
3938 * predicate is true.
3939 * @ingroup non_mutating_algorithms
3940 * @param __first An input iterator.
3941 * @param __last An input iterator.
3942 * @param __pred A predicate.
3943 * @return The first iterator @c i in the range @p [__first,__last)
3944 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3946 template<typename _InputIterator
, typename _Predicate
>
3947 inline _InputIterator
3948 find_if(_InputIterator __first
, _InputIterator __last
,
3951 // concept requirements
3952 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3953 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3954 typename iterator_traits
<_InputIterator
>::value_type
>)
3955 __glibcxx_requires_valid_range(__first
, __last
);
3957 return std::__find_if(__first
, __last
,
3958 __gnu_cxx::__ops::__pred_iter(__pred
));
3962 * @brief Find element from a set in a sequence.
3963 * @ingroup non_mutating_algorithms
3964 * @param __first1 Start of range to search.
3965 * @param __last1 End of range to search.
3966 * @param __first2 Start of match candidates.
3967 * @param __last2 End of match candidates.
3968 * @return The first iterator @c i in the range
3969 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3970 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3972 * Searches the range @p [__first1,__last1) for an element that is
3973 * equal to some element in the range [__first2,__last2). If
3974 * found, returns an iterator in the range [__first1,__last1),
3975 * otherwise returns @p __last1.
3977 template<typename _InputIterator
, typename _ForwardIterator
>
3979 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3980 _ForwardIterator __first2
, _ForwardIterator __last2
)
3982 // concept requirements
3983 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3984 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3985 __glibcxx_function_requires(_EqualOpConcept
<
3986 typename iterator_traits
<_InputIterator
>::value_type
,
3987 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3988 __glibcxx_requires_valid_range(__first1
, __last1
);
3989 __glibcxx_requires_valid_range(__first2
, __last2
);
3991 for (; __first1
!= __last1
; ++__first1
)
3992 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3993 if (*__first1
== *__iter
)
3999 * @brief Find element from a set in a sequence using a predicate.
4000 * @ingroup non_mutating_algorithms
4001 * @param __first1 Start of range to search.
4002 * @param __last1 End of range to search.
4003 * @param __first2 Start of match candidates.
4004 * @param __last2 End of match candidates.
4005 * @param __comp Predicate to use.
4006 * @return The first iterator @c i in the range
4007 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4008 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4009 * such iterator exists.
4012 * Searches the range @p [__first1,__last1) for an element that is
4013 * equal to some element in the range [__first2,__last2). If
4014 * found, returns an iterator in the range [__first1,__last1),
4015 * otherwise returns @p __last1.
4017 template<typename _InputIterator
, typename _ForwardIterator
,
4018 typename _BinaryPredicate
>
4020 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4021 _ForwardIterator __first2
, _ForwardIterator __last2
,
4022 _BinaryPredicate __comp
)
4024 // concept requirements
4025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4026 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4027 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4028 typename iterator_traits
<_InputIterator
>::value_type
,
4029 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4030 __glibcxx_requires_valid_range(__first1
, __last1
);
4031 __glibcxx_requires_valid_range(__first2
, __last2
);
4033 for (; __first1
!= __last1
; ++__first1
)
4034 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4035 if (__comp(*__first1
, *__iter
))
4041 * @brief Find two adjacent values in a sequence that are equal.
4042 * @ingroup non_mutating_algorithms
4043 * @param __first A forward iterator.
4044 * @param __last A forward iterator.
4045 * @return The first iterator @c i such that @c i and @c i+1 are both
4046 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4047 * or @p __last if no such iterator exists.
4049 template<typename _ForwardIterator
>
4050 inline _ForwardIterator
4051 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4053 // concept requirements
4054 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4055 __glibcxx_function_requires(_EqualityComparableConcept
<
4056 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4057 __glibcxx_requires_valid_range(__first
, __last
);
4059 return std::__adjacent_find(__first
, __last
,
4060 __gnu_cxx::__ops::__iter_equal_to_iter());
4064 * @brief Find two adjacent values in a sequence using a predicate.
4065 * @ingroup non_mutating_algorithms
4066 * @param __first A forward iterator.
4067 * @param __last A forward iterator.
4068 * @param __binary_pred A binary predicate.
4069 * @return The first iterator @c i such that @c i and @c i+1 are both
4070 * valid iterators in @p [__first,__last) and such that
4071 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4074 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4075 inline _ForwardIterator
4076 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4077 _BinaryPredicate __binary_pred
)
4079 // concept requirements
4080 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4081 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4082 typename iterator_traits
<_ForwardIterator
>::value_type
,
4083 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4084 __glibcxx_requires_valid_range(__first
, __last
);
4086 return std::__adjacent_find(__first
, __last
,
4087 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
4091 * @brief Count the number of copies of a value in a sequence.
4092 * @ingroup non_mutating_algorithms
4093 * @param __first An input iterator.
4094 * @param __last An input iterator.
4095 * @param __value The value to be counted.
4096 * @return The number of iterators @c i in the range @p [__first,__last)
4097 * for which @c *i == @p __value
4099 template<typename _InputIterator
, typename _Tp
>
4100 inline typename iterator_traits
<_InputIterator
>::difference_type
4101 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4103 // concept requirements
4104 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4105 __glibcxx_function_requires(_EqualOpConcept
<
4106 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4107 __glibcxx_requires_valid_range(__first
, __last
);
4109 return std::__count_if(__first
, __last
,
4110 __gnu_cxx::__ops::__iter_equals_val(__value
));
4114 * @brief Count the elements of a sequence for which a predicate is true.
4115 * @ingroup non_mutating_algorithms
4116 * @param __first An input iterator.
4117 * @param __last An input iterator.
4118 * @param __pred A predicate.
4119 * @return The number of iterators @c i in the range @p [__first,__last)
4120 * for which @p __pred(*i) is true.
4122 template<typename _InputIterator
, typename _Predicate
>
4123 inline typename iterator_traits
<_InputIterator
>::difference_type
4124 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4126 // concept requirements
4127 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4128 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4129 typename iterator_traits
<_InputIterator
>::value_type
>)
4130 __glibcxx_requires_valid_range(__first
, __last
);
4132 return std::__count_if(__first
, __last
,
4133 __gnu_cxx::__ops::__pred_iter(__pred
));
4137 * @brief Search a sequence for a matching sub-sequence.
4138 * @ingroup non_mutating_algorithms
4139 * @param __first1 A forward iterator.
4140 * @param __last1 A forward iterator.
4141 * @param __first2 A forward iterator.
4142 * @param __last2 A forward iterator.
4143 * @return The first iterator @c i in the range @p
4144 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4145 * *(__first2+N) for each @c N in the range @p
4146 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4148 * Searches the range @p [__first1,__last1) for a sub-sequence that
4149 * compares equal value-by-value with the sequence given by @p
4150 * [__first2,__last2) and returns an iterator to the first element
4151 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4154 * Because the sub-sequence must lie completely within the range @p
4155 * [__first1,__last1) it must start at a position less than @p
4156 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4157 * length of the sub-sequence.
4159 * This means that the returned iterator @c i will be in the range
4160 * @p [__first1,__last1-(__last2-__first2))
4162 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4163 inline _ForwardIterator1
4164 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4165 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4167 // concept requirements
4168 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4169 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4170 __glibcxx_function_requires(_EqualOpConcept
<
4171 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4172 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4173 __glibcxx_requires_valid_range(__first1
, __last1
);
4174 __glibcxx_requires_valid_range(__first2
, __last2
);
4176 return std::__search(__first1
, __last1
, __first2
, __last2
,
4177 __gnu_cxx::__ops::__iter_equal_to_iter());
4181 * @brief Search a sequence for a matching sub-sequence using a predicate.
4182 * @ingroup non_mutating_algorithms
4183 * @param __first1 A forward iterator.
4184 * @param __last1 A forward iterator.
4185 * @param __first2 A forward iterator.
4186 * @param __last2 A forward iterator.
4187 * @param __predicate A binary predicate.
4188 * @return The first iterator @c i in the range
4189 * @p [__first1,__last1-(__last2-__first2)) such that
4190 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4191 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4193 * Searches the range @p [__first1,__last1) for a sub-sequence that
4194 * compares equal value-by-value with the sequence given by @p
4195 * [__first2,__last2), using @p __predicate to determine equality,
4196 * and returns an iterator to the first element of the
4197 * sub-sequence, or @p __last1 if no such iterator exists.
4199 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4201 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4202 typename _BinaryPredicate
>
4203 inline _ForwardIterator1
4204 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4205 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4206 _BinaryPredicate __predicate
)
4208 // concept requirements
4209 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4210 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4211 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4212 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4213 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4214 __glibcxx_requires_valid_range(__first1
, __last1
);
4215 __glibcxx_requires_valid_range(__first2
, __last2
);
4217 return std::__search(__first1
, __last1
, __first2
, __last2
,
4218 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4222 * @brief Search a sequence for a number of consecutive values.
4223 * @ingroup non_mutating_algorithms
4224 * @param __first A forward iterator.
4225 * @param __last A forward iterator.
4226 * @param __count The number of consecutive values.
4227 * @param __val The value to find.
4228 * @return The first iterator @c i in the range @p
4229 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4230 * each @c N in the range @p [0,__count), or @p __last if no such
4233 * Searches the range @p [__first,__last) for @p count consecutive elements
4234 * equal to @p __val.
4236 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4237 inline _ForwardIterator
4238 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4239 _Integer __count
, const _Tp
& __val
)
4241 // concept requirements
4242 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4243 __glibcxx_function_requires(_EqualOpConcept
<
4244 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4245 __glibcxx_requires_valid_range(__first
, __last
);
4247 return std::__search_n(__first
, __last
, __count
,
4248 __gnu_cxx::__ops::__iter_equals_val(__val
));
4253 * @brief Search a sequence for a number of consecutive values using a
4255 * @ingroup non_mutating_algorithms
4256 * @param __first A forward iterator.
4257 * @param __last A forward iterator.
4258 * @param __count The number of consecutive values.
4259 * @param __val The value to find.
4260 * @param __binary_pred A binary predicate.
4261 * @return The first iterator @c i in the range @p
4262 * [__first,__last-__count) such that @p
4263 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4264 * @p [0,__count), or @p __last if no such iterator exists.
4266 * Searches the range @p [__first,__last) for @p __count
4267 * consecutive elements for which the predicate returns true.
4269 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4270 typename _BinaryPredicate
>
4271 inline _ForwardIterator
4272 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4273 _Integer __count
, const _Tp
& __val
,
4274 _BinaryPredicate __binary_pred
)
4276 // concept requirements
4277 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4278 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4279 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4280 __glibcxx_requires_valid_range(__first
, __last
);
4282 return std::__search_n(__first
, __last
, __count
,
4283 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4286 #if __cplusplus > 201402L
4287 /** @brief Search a sequence using a Searcher object.
4289 * @param __first A forward iterator.
4290 * @param __last A forward iterator.
4291 * @param __searcher A callable object.
4292 * @return @p __searcher(__first,__last).first
4294 template<typename _ForwardIterator
, typename _Searcher
>
4295 inline _ForwardIterator
4296 search(_ForwardIterator __first
, _ForwardIterator __last
,
4297 const _Searcher
& __searcher
)
4298 { return __searcher(__first
, __last
).first
; }
4302 * @brief Perform an operation on a sequence.
4303 * @ingroup mutating_algorithms
4304 * @param __first An input iterator.
4305 * @param __last An input iterator.
4306 * @param __result An output iterator.
4307 * @param __unary_op A unary operator.
4308 * @return An output iterator equal to @p __result+(__last-__first).
4310 * Applies the operator to each element in the input range and assigns
4311 * the results to successive elements of the output sequence.
4312 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4313 * range @p [0,__last-__first).
4315 * @p unary_op must not alter its argument.
4317 template<typename _InputIterator
, typename _OutputIterator
,
4318 typename _UnaryOperation
>
4320 transform(_InputIterator __first
, _InputIterator __last
,
4321 _OutputIterator __result
, _UnaryOperation __unary_op
)
4323 // concept requirements
4324 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4325 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4326 // "the type returned by a _UnaryOperation"
4327 __typeof__(__unary_op(*__first
))>)
4328 __glibcxx_requires_valid_range(__first
, __last
);
4330 for (; __first
!= __last
; ++__first
, (void)++__result
)
4331 *__result
= __unary_op(*__first
);
4336 * @brief Perform an operation on corresponding elements of two sequences.
4337 * @ingroup mutating_algorithms
4338 * @param __first1 An input iterator.
4339 * @param __last1 An input iterator.
4340 * @param __first2 An input iterator.
4341 * @param __result An output iterator.
4342 * @param __binary_op A binary operator.
4343 * @return An output iterator equal to @p result+(last-first).
4345 * Applies the operator to the corresponding elements in the two
4346 * input ranges and assigns the results to successive elements of the
4349 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4350 * @c N in the range @p [0,__last1-__first1).
4352 * @p binary_op must not alter either of its arguments.
4354 template<typename _InputIterator1
, typename _InputIterator2
,
4355 typename _OutputIterator
, typename _BinaryOperation
>
4357 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4358 _InputIterator2 __first2
, _OutputIterator __result
,
4359 _BinaryOperation __binary_op
)
4361 // concept requirements
4362 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4363 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4364 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4365 // "the type returned by a _BinaryOperation"
4366 __typeof__(__binary_op(*__first1
,*__first2
))>)
4367 __glibcxx_requires_valid_range(__first1
, __last1
);
4369 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4370 *__result
= __binary_op(*__first1
, *__first2
);
4375 * @brief Replace each occurrence of one value in a sequence with another
4377 * @ingroup mutating_algorithms
4378 * @param __first A forward iterator.
4379 * @param __last A forward iterator.
4380 * @param __old_value The value to be replaced.
4381 * @param __new_value The replacement value.
4382 * @return replace() returns no value.
4384 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4385 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4387 template<typename _ForwardIterator
, typename _Tp
>
4389 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4390 const _Tp
& __old_value
, const _Tp
& __new_value
)
4392 // concept requirements
4393 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4395 __glibcxx_function_requires(_EqualOpConcept
<
4396 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4397 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4398 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4399 __glibcxx_requires_valid_range(__first
, __last
);
4401 for (; __first
!= __last
; ++__first
)
4402 if (*__first
== __old_value
)
4403 *__first
= __new_value
;
4407 * @brief Replace each value in a sequence for which a predicate returns
4408 * true with another value.
4409 * @ingroup mutating_algorithms
4410 * @param __first A forward iterator.
4411 * @param __last A forward iterator.
4412 * @param __pred A predicate.
4413 * @param __new_value The replacement value.
4414 * @return replace_if() returns no value.
4416 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4417 * is true then the assignment @c *i = @p __new_value is performed.
4419 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4421 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4422 _Predicate __pred
, const _Tp
& __new_value
)
4424 // concept requirements
4425 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4427 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4428 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4429 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4430 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4431 __glibcxx_requires_valid_range(__first
, __last
);
4433 for (; __first
!= __last
; ++__first
)
4434 if (__pred(*__first
))
4435 *__first
= __new_value
;
4439 * @brief Assign the result of a function object to each value in a
4441 * @ingroup mutating_algorithms
4442 * @param __first A forward iterator.
4443 * @param __last A forward iterator.
4444 * @param __gen A function object taking no arguments and returning
4445 * std::iterator_traits<_ForwardIterator>::value_type
4446 * @return generate() returns no value.
4448 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4449 * @p [__first,__last).
4451 template<typename _ForwardIterator
, typename _Generator
>
4453 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4456 // concept requirements
4457 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4458 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4459 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4460 __glibcxx_requires_valid_range(__first
, __last
);
4462 for (; __first
!= __last
; ++__first
)
4467 * @brief Assign the result of a function object to each value in a
4469 * @ingroup mutating_algorithms
4470 * @param __first A forward iterator.
4471 * @param __n The length of the sequence.
4472 * @param __gen A function object taking no arguments and returning
4473 * std::iterator_traits<_ForwardIterator>::value_type
4474 * @return The end of the sequence, @p __first+__n
4476 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4477 * @p [__first,__first+__n).
4479 * If @p __n is negative, the function does nothing and returns @p __first.
4481 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4482 // DR 865. More algorithms that throw away information
4483 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4484 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4486 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4488 // concept requirements
4489 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4490 // "the type returned by a _Generator"
4491 __typeof__(__gen())>)
4493 typedef __decltype(std::__size_to_integer(__n
)) _IntSize
;
4494 for (_IntSize __niter
= std::__size_to_integer(__n
);
4495 __niter
> 0; --__niter
, (void) ++__first
)
4501 * @brief Copy a sequence, removing consecutive duplicate values.
4502 * @ingroup mutating_algorithms
4503 * @param __first An input iterator.
4504 * @param __last An input iterator.
4505 * @param __result An output iterator.
4506 * @return An iterator designating the end of the resulting sequence.
4508 * Copies each element in the range @p [__first,__last) to the range
4509 * beginning at @p __result, except that only the first element is copied
4510 * from groups of consecutive elements that compare equal.
4511 * unique_copy() is stable, so the relative order of elements that are
4512 * copied is unchanged.
4514 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4515 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4517 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4518 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4521 template<typename _InputIterator
, typename _OutputIterator
>
4522 inline _OutputIterator
4523 unique_copy(_InputIterator __first
, _InputIterator __last
,
4524 _OutputIterator __result
)
4526 // concept requirements
4527 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4528 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4529 typename iterator_traits
<_InputIterator
>::value_type
>)
4530 __glibcxx_function_requires(_EqualityComparableConcept
<
4531 typename iterator_traits
<_InputIterator
>::value_type
>)
4532 __glibcxx_requires_valid_range(__first
, __last
);
4534 if (__first
== __last
)
4536 return std::__unique_copy(__first
, __last
, __result
,
4537 __gnu_cxx::__ops::__iter_equal_to_iter(),
4538 std::__iterator_category(__first
),
4539 std::__iterator_category(__result
));
4543 * @brief Copy a sequence, removing consecutive values using a predicate.
4544 * @ingroup mutating_algorithms
4545 * @param __first An input iterator.
4546 * @param __last An input iterator.
4547 * @param __result An output iterator.
4548 * @param __binary_pred A binary predicate.
4549 * @return An iterator designating the end of the resulting sequence.
4551 * Copies each element in the range @p [__first,__last) to the range
4552 * beginning at @p __result, except that only the first element is copied
4553 * from groups of consecutive elements for which @p __binary_pred returns
4555 * unique_copy() is stable, so the relative order of elements that are
4556 * copied is unchanged.
4558 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4559 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4561 template<typename _InputIterator
, typename _OutputIterator
,
4562 typename _BinaryPredicate
>
4563 inline _OutputIterator
4564 unique_copy(_InputIterator __first
, _InputIterator __last
,
4565 _OutputIterator __result
,
4566 _BinaryPredicate __binary_pred
)
4568 // concept requirements -- predicates checked later
4569 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4570 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4571 typename iterator_traits
<_InputIterator
>::value_type
>)
4572 __glibcxx_requires_valid_range(__first
, __last
);
4574 if (__first
== __last
)
4576 return std::__unique_copy(__first
, __last
, __result
,
4577 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4578 std::__iterator_category(__first
),
4579 std::__iterator_category(__result
));
4584 * @brief Randomly shuffle the elements of a sequence.
4585 * @ingroup mutating_algorithms
4586 * @param __first A forward iterator.
4587 * @param __last A forward iterator.
4590 * Reorder the elements in the range @p [__first,__last) using a random
4591 * distribution, so that every possible ordering of the sequence is
4594 template<typename _RandomAccessIterator
>
4596 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4598 // concept requirements
4599 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4600 _RandomAccessIterator
>)
4601 __glibcxx_requires_valid_range(__first
, __last
);
4603 if (__first
!= __last
)
4604 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4606 // XXX rand() % N is not uniformly distributed
4607 _RandomAccessIterator __j
= __first
4608 + std::rand() % ((__i
- __first
) + 1);
4610 std::iter_swap(__i
, __j
);
4616 * @brief Shuffle the elements of a sequence using a random number
4618 * @ingroup mutating_algorithms
4619 * @param __first A forward iterator.
4620 * @param __last A forward iterator.
4621 * @param __rand The RNG functor or function.
4624 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4625 * provide a random distribution. Calling @p __rand(N) for a positive
4626 * integer @p N should return a randomly chosen integer from the
4629 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4631 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4632 #if __cplusplus >= 201103L
4633 _RandomNumberGenerator
&& __rand
)
4635 _RandomNumberGenerator
& __rand
)
4638 // concept requirements
4639 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4640 _RandomAccessIterator
>)
4641 __glibcxx_requires_valid_range(__first
, __last
);
4643 if (__first
== __last
)
4645 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4647 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4649 std::iter_swap(__i
, __j
);
4655 * @brief Move elements for which a predicate is true to the beginning
4657 * @ingroup mutating_algorithms
4658 * @param __first A forward iterator.
4659 * @param __last A forward iterator.
4660 * @param __pred A predicate functor.
4661 * @return An iterator @p middle such that @p __pred(i) is true for each
4662 * iterator @p i in the range @p [__first,middle) and false for each @p i
4663 * in the range @p [middle,__last).
4665 * @p __pred must not modify its operand. @p partition() does not preserve
4666 * the relative ordering of elements in each group, use
4667 * @p stable_partition() if this is needed.
4669 template<typename _ForwardIterator
, typename _Predicate
>
4670 inline _ForwardIterator
4671 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4674 // concept requirements
4675 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4677 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4678 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4679 __glibcxx_requires_valid_range(__first
, __last
);
4681 return std::__partition(__first
, __last
, __pred
,
4682 std::__iterator_category(__first
));
4687 * @brief Sort the smallest elements of a sequence.
4688 * @ingroup sorting_algorithms
4689 * @param __first An iterator.
4690 * @param __middle Another iterator.
4691 * @param __last Another iterator.
4694 * Sorts the smallest @p (__middle-__first) elements in the range
4695 * @p [first,last) and moves them to the range @p [__first,__middle). The
4696 * order of the remaining elements in the range @p [__middle,__last) is
4698 * After the sort if @e i and @e j are iterators in the range
4699 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4700 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4702 template<typename _RandomAccessIterator
>
4704 partial_sort(_RandomAccessIterator __first
,
4705 _RandomAccessIterator __middle
,
4706 _RandomAccessIterator __last
)
4708 // concept requirements
4709 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4710 _RandomAccessIterator
>)
4711 __glibcxx_function_requires(_LessThanComparableConcept
<
4712 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4713 __glibcxx_requires_valid_range(__first
, __middle
);
4714 __glibcxx_requires_valid_range(__middle
, __last
);
4715 __glibcxx_requires_irreflexive(__first
, __last
);
4717 std::__partial_sort(__first
, __middle
, __last
,
4718 __gnu_cxx::__ops::__iter_less_iter());
4722 * @brief Sort the smallest elements of a sequence using a predicate
4724 * @ingroup sorting_algorithms
4725 * @param __first An iterator.
4726 * @param __middle Another iterator.
4727 * @param __last Another iterator.
4728 * @param __comp A comparison functor.
4731 * Sorts the smallest @p (__middle-__first) elements in the range
4732 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4733 * order of the remaining elements in the range @p [__middle,__last) is
4735 * After the sort if @e i and @e j are iterators in the range
4736 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4737 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4740 template<typename _RandomAccessIterator
, typename _Compare
>
4742 partial_sort(_RandomAccessIterator __first
,
4743 _RandomAccessIterator __middle
,
4744 _RandomAccessIterator __last
,
4747 // concept requirements
4748 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4749 _RandomAccessIterator
>)
4750 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4751 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4752 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4753 __glibcxx_requires_valid_range(__first
, __middle
);
4754 __glibcxx_requires_valid_range(__middle
, __last
);
4755 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4757 std::__partial_sort(__first
, __middle
, __last
,
4758 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4762 * @brief Sort a sequence just enough to find a particular position.
4763 * @ingroup sorting_algorithms
4764 * @param __first An iterator.
4765 * @param __nth Another iterator.
4766 * @param __last Another iterator.
4769 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4770 * is the same element that would have been in that position had the
4771 * whole sequence been sorted. The elements either side of @p *__nth are
4772 * not completely sorted, but for any iterator @e i in the range
4773 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4774 * holds that *j < *i is false.
4776 template<typename _RandomAccessIterator
>
4778 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4779 _RandomAccessIterator __last
)
4781 // concept requirements
4782 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4783 _RandomAccessIterator
>)
4784 __glibcxx_function_requires(_LessThanComparableConcept
<
4785 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4786 __glibcxx_requires_valid_range(__first
, __nth
);
4787 __glibcxx_requires_valid_range(__nth
, __last
);
4788 __glibcxx_requires_irreflexive(__first
, __last
);
4790 if (__first
== __last
|| __nth
== __last
)
4793 std::__introselect(__first
, __nth
, __last
,
4794 std::__lg(__last
- __first
) * 2,
4795 __gnu_cxx::__ops::__iter_less_iter());
4799 * @brief Sort a sequence just enough to find a particular position
4800 * using a predicate for comparison.
4801 * @ingroup sorting_algorithms
4802 * @param __first An iterator.
4803 * @param __nth Another iterator.
4804 * @param __last Another iterator.
4805 * @param __comp A comparison functor.
4808 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4809 * is the same element that would have been in that position had the
4810 * whole sequence been sorted. The elements either side of @p *__nth are
4811 * not completely sorted, but for any iterator @e i in the range
4812 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4813 * holds that @p __comp(*j,*i) is false.
4815 template<typename _RandomAccessIterator
, typename _Compare
>
4817 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4818 _RandomAccessIterator __last
, _Compare __comp
)
4820 // concept requirements
4821 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4822 _RandomAccessIterator
>)
4823 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4824 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4825 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4826 __glibcxx_requires_valid_range(__first
, __nth
);
4827 __glibcxx_requires_valid_range(__nth
, __last
);
4828 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4830 if (__first
== __last
|| __nth
== __last
)
4833 std::__introselect(__first
, __nth
, __last
,
4834 std::__lg(__last
- __first
) * 2,
4835 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4839 * @brief Sort the elements of a sequence.
4840 * @ingroup sorting_algorithms
4841 * @param __first An iterator.
4842 * @param __last Another iterator.
4845 * Sorts the elements in the range @p [__first,__last) in ascending order,
4846 * such that for each iterator @e i in the range @p [__first,__last-1),
4847 * *(i+1)<*i is false.
4849 * The relative ordering of equivalent elements is not preserved, use
4850 * @p stable_sort() if this is needed.
4852 template<typename _RandomAccessIterator
>
4854 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4856 // concept requirements
4857 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4858 _RandomAccessIterator
>)
4859 __glibcxx_function_requires(_LessThanComparableConcept
<
4860 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4861 __glibcxx_requires_valid_range(__first
, __last
);
4862 __glibcxx_requires_irreflexive(__first
, __last
);
4864 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4868 * @brief Sort the elements of a sequence using a predicate for comparison.
4869 * @ingroup sorting_algorithms
4870 * @param __first An iterator.
4871 * @param __last Another iterator.
4872 * @param __comp A comparison functor.
4875 * Sorts the elements in the range @p [__first,__last) in ascending order,
4876 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4877 * range @p [__first,__last-1).
4879 * The relative ordering of equivalent elements is not preserved, use
4880 * @p stable_sort() if this is needed.
4882 template<typename _RandomAccessIterator
, typename _Compare
>
4884 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4887 // concept requirements
4888 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4889 _RandomAccessIterator
>)
4890 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4891 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4892 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4893 __glibcxx_requires_valid_range(__first
, __last
);
4894 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4896 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4899 template<typename _InputIterator1
, typename _InputIterator2
,
4900 typename _OutputIterator
, typename _Compare
>
4902 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4903 _InputIterator2 __first2
, _InputIterator2 __last2
,
4904 _OutputIterator __result
, _Compare __comp
)
4906 while (__first1
!= __last1
&& __first2
!= __last2
)
4908 if (__comp(__first2
, __first1
))
4910 *__result
= *__first2
;
4915 *__result
= *__first1
;
4920 return std::copy(__first2
, __last2
,
4921 std::copy(__first1
, __last1
, __result
));
4925 * @brief Merges two sorted ranges.
4926 * @ingroup sorting_algorithms
4927 * @param __first1 An iterator.
4928 * @param __first2 Another iterator.
4929 * @param __last1 Another iterator.
4930 * @param __last2 Another iterator.
4931 * @param __result An iterator pointing to the end of the merged range.
4932 * @return An iterator pointing to the first element <em>not less
4935 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4936 * the sorted range @p [__result, __result + (__last1-__first1) +
4937 * (__last2-__first2)). Both input ranges must be sorted, and the
4938 * output range must not overlap with either of the input ranges.
4939 * The sort is @e stable, that is, for equivalent elements in the
4940 * two ranges, elements from the first range will always come
4941 * before elements from the second.
4943 template<typename _InputIterator1
, typename _InputIterator2
,
4944 typename _OutputIterator
>
4945 inline _OutputIterator
4946 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4947 _InputIterator2 __first2
, _InputIterator2 __last2
,
4948 _OutputIterator __result
)
4950 // concept requirements
4951 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4952 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4953 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4954 typename iterator_traits
<_InputIterator1
>::value_type
>)
4955 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4956 typename iterator_traits
<_InputIterator2
>::value_type
>)
4957 __glibcxx_function_requires(_LessThanOpConcept
<
4958 typename iterator_traits
<_InputIterator2
>::value_type
,
4959 typename iterator_traits
<_InputIterator1
>::value_type
>)
4960 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4961 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4962 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4963 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4965 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4966 __first2
, __last2
, __result
,
4967 __gnu_cxx::__ops::__iter_less_iter());
4971 * @brief Merges two sorted ranges.
4972 * @ingroup sorting_algorithms
4973 * @param __first1 An iterator.
4974 * @param __first2 Another iterator.
4975 * @param __last1 Another iterator.
4976 * @param __last2 Another iterator.
4977 * @param __result An iterator pointing to the end of the merged range.
4978 * @param __comp A functor to use for comparisons.
4979 * @return An iterator pointing to the first element "not less
4982 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4983 * the sorted range @p [__result, __result + (__last1-__first1) +
4984 * (__last2-__first2)). Both input ranges must be sorted, and the
4985 * output range must not overlap with either of the input ranges.
4986 * The sort is @e stable, that is, for equivalent elements in the
4987 * two ranges, elements from the first range will always come
4988 * before elements from the second.
4990 * The comparison function should have the same effects on ordering as
4991 * the function used for the initial sort.
4993 template<typename _InputIterator1
, typename _InputIterator2
,
4994 typename _OutputIterator
, typename _Compare
>
4995 inline _OutputIterator
4996 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4997 _InputIterator2 __first2
, _InputIterator2 __last2
,
4998 _OutputIterator __result
, _Compare __comp
)
5000 // concept requirements
5001 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5002 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5003 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5004 typename iterator_traits
<_InputIterator1
>::value_type
>)
5005 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5006 typename iterator_traits
<_InputIterator2
>::value_type
>)
5007 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5008 typename iterator_traits
<_InputIterator2
>::value_type
,
5009 typename iterator_traits
<_InputIterator1
>::value_type
>)
5010 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5011 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5012 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5013 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5015 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
5016 __first2
, __last2
, __result
,
5017 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5020 template<typename _RandomAccessIterator
, typename _Compare
>
5022 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5025 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5027 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5030 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
5031 _TmpBuf
__buf(__first
, std::distance(__first
, __last
));
5033 if (__buf
.begin() == 0)
5034 std::__inplace_stable_sort(__first
, __last
, __comp
);
5036 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5037 _DistanceType(__buf
.size()), __comp
);
5041 * @brief Sort the elements of a sequence, preserving the relative order
5042 * of equivalent elements.
5043 * @ingroup sorting_algorithms
5044 * @param __first An iterator.
5045 * @param __last Another iterator.
5048 * Sorts the elements in the range @p [__first,__last) in ascending order,
5049 * such that for each iterator @p i in the range @p [__first,__last-1),
5050 * @p *(i+1)<*i is false.
5052 * The relative ordering of equivalent elements is preserved, so any two
5053 * elements @p x and @p y in the range @p [__first,__last) such that
5054 * @p x<y is false and @p y<x is false will have the same relative
5055 * ordering after calling @p stable_sort().
5057 template<typename _RandomAccessIterator
>
5059 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5061 // concept requirements
5062 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5063 _RandomAccessIterator
>)
5064 __glibcxx_function_requires(_LessThanComparableConcept
<
5065 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5066 __glibcxx_requires_valid_range(__first
, __last
);
5067 __glibcxx_requires_irreflexive(__first
, __last
);
5069 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5070 __gnu_cxx::__ops::__iter_less_iter());
5074 * @brief Sort the elements of a sequence using a predicate for comparison,
5075 * preserving the relative order of equivalent elements.
5076 * @ingroup sorting_algorithms
5077 * @param __first An iterator.
5078 * @param __last Another iterator.
5079 * @param __comp A comparison functor.
5082 * Sorts the elements in the range @p [__first,__last) in ascending order,
5083 * such that for each iterator @p i in the range @p [__first,__last-1),
5084 * @p __comp(*(i+1),*i) is false.
5086 * The relative ordering of equivalent elements is preserved, so any two
5087 * elements @p x and @p y in the range @p [__first,__last) such that
5088 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5089 * relative ordering after calling @p stable_sort().
5091 template<typename _RandomAccessIterator
, typename _Compare
>
5093 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5096 // concept requirements
5097 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5098 _RandomAccessIterator
>)
5099 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5100 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
5101 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
5102 __glibcxx_requires_valid_range(__first
, __last
);
5103 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5105 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
5106 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5109 template<typename _InputIterator1
, typename _InputIterator2
,
5110 typename _OutputIterator
,
5113 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5114 _InputIterator2 __first2
, _InputIterator2 __last2
,
5115 _OutputIterator __result
, _Compare __comp
)
5117 while (__first1
!= __last1
&& __first2
!= __last2
)
5119 if (__comp(__first1
, __first2
))
5121 *__result
= *__first1
;
5124 else if (__comp(__first2
, __first1
))
5126 *__result
= *__first2
;
5131 *__result
= *__first1
;
5137 return std::copy(__first2
, __last2
,
5138 std::copy(__first1
, __last1
, __result
));
5142 * @brief Return the union of two sorted ranges.
5143 * @ingroup set_algorithms
5144 * @param __first1 Start of first range.
5145 * @param __last1 End of first range.
5146 * @param __first2 Start of second range.
5147 * @param __last2 End of second range.
5148 * @param __result Start of output range.
5149 * @return End of the output range.
5150 * @ingroup set_algorithms
5152 * This operation iterates over both ranges, copying elements present in
5153 * each range in order to the output range. Iterators increment for each
5154 * range. When the current element of one range is less than the other,
5155 * that element is copied and the iterator advanced. If an element is
5156 * contained in both ranges, the element from the first range is copied and
5157 * both ranges advance. The output range may not overlap either input
5160 template<typename _InputIterator1
, typename _InputIterator2
,
5161 typename _OutputIterator
>
5162 inline _OutputIterator
5163 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5164 _InputIterator2 __first2
, _InputIterator2 __last2
,
5165 _OutputIterator __result
)
5167 // concept requirements
5168 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5169 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5170 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5171 typename iterator_traits
<_InputIterator1
>::value_type
>)
5172 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5173 typename iterator_traits
<_InputIterator2
>::value_type
>)
5174 __glibcxx_function_requires(_LessThanOpConcept
<
5175 typename iterator_traits
<_InputIterator1
>::value_type
,
5176 typename iterator_traits
<_InputIterator2
>::value_type
>)
5177 __glibcxx_function_requires(_LessThanOpConcept
<
5178 typename iterator_traits
<_InputIterator2
>::value_type
,
5179 typename iterator_traits
<_InputIterator1
>::value_type
>)
5180 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5181 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5182 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5183 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5185 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5186 __first2
, __last2
, __result
,
5187 __gnu_cxx::__ops::__iter_less_iter());
5191 * @brief Return the union of two sorted ranges using a comparison functor.
5192 * @ingroup set_algorithms
5193 * @param __first1 Start of first range.
5194 * @param __last1 End of first range.
5195 * @param __first2 Start of second range.
5196 * @param __last2 End of second range.
5197 * @param __result Start of output range.
5198 * @param __comp The comparison functor.
5199 * @return End of the output range.
5200 * @ingroup set_algorithms
5202 * This operation iterates over both ranges, copying elements present in
5203 * each range in order to the output range. Iterators increment for each
5204 * range. When the current element of one range is less than the other
5205 * according to @p __comp, that element is copied and the iterator advanced.
5206 * If an equivalent element according to @p __comp is contained in both
5207 * ranges, the element from the first range is copied and both ranges
5208 * advance. The output range may not overlap either input range.
5210 template<typename _InputIterator1
, typename _InputIterator2
,
5211 typename _OutputIterator
, typename _Compare
>
5212 inline _OutputIterator
5213 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5214 _InputIterator2 __first2
, _InputIterator2 __last2
,
5215 _OutputIterator __result
, _Compare __comp
)
5217 // concept requirements
5218 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5219 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5220 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5221 typename iterator_traits
<_InputIterator1
>::value_type
>)
5222 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5223 typename iterator_traits
<_InputIterator2
>::value_type
>)
5224 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5225 typename iterator_traits
<_InputIterator1
>::value_type
,
5226 typename iterator_traits
<_InputIterator2
>::value_type
>)
5227 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5228 typename iterator_traits
<_InputIterator2
>::value_type
,
5229 typename iterator_traits
<_InputIterator1
>::value_type
>)
5230 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5231 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5232 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5233 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5235 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5236 __first2
, __last2
, __result
,
5237 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5240 template<typename _InputIterator1
, typename _InputIterator2
,
5241 typename _OutputIterator
,
5244 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5245 _InputIterator2 __first2
, _InputIterator2 __last2
,
5246 _OutputIterator __result
, _Compare __comp
)
5248 while (__first1
!= __last1
&& __first2
!= __last2
)
5249 if (__comp(__first1
, __first2
))
5251 else if (__comp(__first2
, __first1
))
5255 *__result
= *__first1
;
5264 * @brief Return the intersection of two sorted ranges.
5265 * @ingroup set_algorithms
5266 * @param __first1 Start of first range.
5267 * @param __last1 End of first range.
5268 * @param __first2 Start of second range.
5269 * @param __last2 End of second range.
5270 * @param __result Start of output range.
5271 * @return End of the output range.
5272 * @ingroup set_algorithms
5274 * This operation iterates over both ranges, copying elements present in
5275 * both ranges in order to the output range. Iterators increment for each
5276 * range. When the current element of one range is less than the other,
5277 * that iterator advances. If an element is contained in both ranges, the
5278 * element from the first range is copied and both ranges advance. The
5279 * output range may not overlap either input range.
5281 template<typename _InputIterator1
, typename _InputIterator2
,
5282 typename _OutputIterator
>
5283 inline _OutputIterator
5284 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5285 _InputIterator2 __first2
, _InputIterator2 __last2
,
5286 _OutputIterator __result
)
5288 // concept requirements
5289 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5290 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5291 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5292 typename iterator_traits
<_InputIterator1
>::value_type
>)
5293 __glibcxx_function_requires(_LessThanOpConcept
<
5294 typename iterator_traits
<_InputIterator1
>::value_type
,
5295 typename iterator_traits
<_InputIterator2
>::value_type
>)
5296 __glibcxx_function_requires(_LessThanOpConcept
<
5297 typename iterator_traits
<_InputIterator2
>::value_type
,
5298 typename iterator_traits
<_InputIterator1
>::value_type
>)
5299 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5300 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5301 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5302 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5304 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5305 __first2
, __last2
, __result
,
5306 __gnu_cxx::__ops::__iter_less_iter());
5310 * @brief Return the intersection of two sorted ranges using comparison
5312 * @ingroup set_algorithms
5313 * @param __first1 Start of first range.
5314 * @param __last1 End of first range.
5315 * @param __first2 Start of second range.
5316 * @param __last2 End of second range.
5317 * @param __result Start of output range.
5318 * @param __comp The comparison functor.
5319 * @return End of the output range.
5320 * @ingroup set_algorithms
5322 * This operation iterates over both ranges, copying elements present in
5323 * both ranges in order to the output range. Iterators increment for each
5324 * range. When the current element of one range is less than the other
5325 * according to @p __comp, that iterator advances. If an element is
5326 * contained in both ranges according to @p __comp, the element from the
5327 * first range is copied and both ranges advance. The output range may not
5328 * overlap either input range.
5330 template<typename _InputIterator1
, typename _InputIterator2
,
5331 typename _OutputIterator
, typename _Compare
>
5332 inline _OutputIterator
5333 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5334 _InputIterator2 __first2
, _InputIterator2 __last2
,
5335 _OutputIterator __result
, _Compare __comp
)
5337 // concept requirements
5338 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5339 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5340 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5341 typename iterator_traits
<_InputIterator1
>::value_type
>)
5342 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5343 typename iterator_traits
<_InputIterator1
>::value_type
,
5344 typename iterator_traits
<_InputIterator2
>::value_type
>)
5345 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5346 typename iterator_traits
<_InputIterator2
>::value_type
,
5347 typename iterator_traits
<_InputIterator1
>::value_type
>)
5348 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5349 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5350 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5351 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5353 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5354 __first2
, __last2
, __result
,
5355 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5358 template<typename _InputIterator1
, typename _InputIterator2
,
5359 typename _OutputIterator
,
5362 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5363 _InputIterator2 __first2
, _InputIterator2 __last2
,
5364 _OutputIterator __result
, _Compare __comp
)
5366 while (__first1
!= __last1
&& __first2
!= __last2
)
5367 if (__comp(__first1
, __first2
))
5369 *__result
= *__first1
;
5373 else if (__comp(__first2
, __first1
))
5380 return std::copy(__first1
, __last1
, __result
);
5384 * @brief Return the difference of two sorted ranges.
5385 * @ingroup set_algorithms
5386 * @param __first1 Start of first range.
5387 * @param __last1 End of first range.
5388 * @param __first2 Start of second range.
5389 * @param __last2 End of second range.
5390 * @param __result Start of output range.
5391 * @return End of the output range.
5392 * @ingroup set_algorithms
5394 * This operation iterates over both ranges, copying elements present in
5395 * the first range but not the second in order to the output range.
5396 * Iterators increment for each range. When the current element of the
5397 * first range is less than the second, that element is copied and the
5398 * iterator advances. If the current element of the second range is less,
5399 * the iterator advances, but no element is copied. If an element is
5400 * contained in both ranges, no elements are copied and both ranges
5401 * advance. The output range may not overlap either input range.
5403 template<typename _InputIterator1
, typename _InputIterator2
,
5404 typename _OutputIterator
>
5405 inline _OutputIterator
5406 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5407 _InputIterator2 __first2
, _InputIterator2 __last2
,
5408 _OutputIterator __result
)
5410 // concept requirements
5411 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5412 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5413 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5414 typename iterator_traits
<_InputIterator1
>::value_type
>)
5415 __glibcxx_function_requires(_LessThanOpConcept
<
5416 typename iterator_traits
<_InputIterator1
>::value_type
,
5417 typename iterator_traits
<_InputIterator2
>::value_type
>)
5418 __glibcxx_function_requires(_LessThanOpConcept
<
5419 typename iterator_traits
<_InputIterator2
>::value_type
,
5420 typename iterator_traits
<_InputIterator1
>::value_type
>)
5421 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5422 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5423 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5424 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5426 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5427 __first2
, __last2
, __result
,
5428 __gnu_cxx::__ops::__iter_less_iter());
5432 * @brief Return the difference of two sorted ranges using comparison
5434 * @ingroup set_algorithms
5435 * @param __first1 Start of first range.
5436 * @param __last1 End of first range.
5437 * @param __first2 Start of second range.
5438 * @param __last2 End of second range.
5439 * @param __result Start of output range.
5440 * @param __comp The comparison functor.
5441 * @return End of the output range.
5442 * @ingroup set_algorithms
5444 * This operation iterates over both ranges, copying elements present in
5445 * the first range but not the second in order to the output range.
5446 * Iterators increment for each range. When the current element of the
5447 * first range is less than the second according to @p __comp, that element
5448 * is copied and the iterator advances. If the current element of the
5449 * second range is less, no element is copied and the iterator advances.
5450 * If an element is contained in both ranges according to @p __comp, no
5451 * elements are copied and both ranges advance. The output range may not
5452 * overlap either input range.
5454 template<typename _InputIterator1
, typename _InputIterator2
,
5455 typename _OutputIterator
, typename _Compare
>
5456 inline _OutputIterator
5457 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5458 _InputIterator2 __first2
, _InputIterator2 __last2
,
5459 _OutputIterator __result
, _Compare __comp
)
5461 // concept requirements
5462 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5463 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5464 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5465 typename iterator_traits
<_InputIterator1
>::value_type
>)
5466 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5467 typename iterator_traits
<_InputIterator1
>::value_type
,
5468 typename iterator_traits
<_InputIterator2
>::value_type
>)
5469 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5470 typename iterator_traits
<_InputIterator2
>::value_type
,
5471 typename iterator_traits
<_InputIterator1
>::value_type
>)
5472 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5473 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5474 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5475 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5477 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5478 __first2
, __last2
, __result
,
5479 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5482 template<typename _InputIterator1
, typename _InputIterator2
,
5483 typename _OutputIterator
,
5486 __set_symmetric_difference(_InputIterator1 __first1
,
5487 _InputIterator1 __last1
,
5488 _InputIterator2 __first2
,
5489 _InputIterator2 __last2
,
5490 _OutputIterator __result
,
5493 while (__first1
!= __last1
&& __first2
!= __last2
)
5494 if (__comp(__first1
, __first2
))
5496 *__result
= *__first1
;
5500 else if (__comp(__first2
, __first1
))
5502 *__result
= *__first2
;
5511 return std::copy(__first2
, __last2
,
5512 std::copy(__first1
, __last1
, __result
));
5516 * @brief Return the symmetric difference of two sorted ranges.
5517 * @ingroup set_algorithms
5518 * @param __first1 Start of first range.
5519 * @param __last1 End of first range.
5520 * @param __first2 Start of second range.
5521 * @param __last2 End of second range.
5522 * @param __result Start of output range.
5523 * @return End of the output range.
5524 * @ingroup set_algorithms
5526 * This operation iterates over both ranges, copying elements present in
5527 * one range but not the other in order to the output range. Iterators
5528 * increment for each range. When the current element of one range is less
5529 * than the other, that element is copied and the iterator advances. If an
5530 * element is contained in both ranges, no elements are copied and both
5531 * ranges advance. The output range may not overlap either input range.
5533 template<typename _InputIterator1
, typename _InputIterator2
,
5534 typename _OutputIterator
>
5535 inline _OutputIterator
5536 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5537 _InputIterator2 __first2
, _InputIterator2 __last2
,
5538 _OutputIterator __result
)
5540 // concept requirements
5541 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5542 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5543 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5544 typename iterator_traits
<_InputIterator1
>::value_type
>)
5545 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5546 typename iterator_traits
<_InputIterator2
>::value_type
>)
5547 __glibcxx_function_requires(_LessThanOpConcept
<
5548 typename iterator_traits
<_InputIterator1
>::value_type
,
5549 typename iterator_traits
<_InputIterator2
>::value_type
>)
5550 __glibcxx_function_requires(_LessThanOpConcept
<
5551 typename iterator_traits
<_InputIterator2
>::value_type
,
5552 typename iterator_traits
<_InputIterator1
>::value_type
>)
5553 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5554 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5555 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5556 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5558 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5559 __first2
, __last2
, __result
,
5560 __gnu_cxx::__ops::__iter_less_iter());
5564 * @brief Return the symmetric difference of two sorted ranges using
5565 * comparison functor.
5566 * @ingroup set_algorithms
5567 * @param __first1 Start of first range.
5568 * @param __last1 End of first range.
5569 * @param __first2 Start of second range.
5570 * @param __last2 End of second range.
5571 * @param __result Start of output range.
5572 * @param __comp The comparison functor.
5573 * @return End of the output range.
5574 * @ingroup set_algorithms
5576 * This operation iterates over both ranges, copying elements present in
5577 * one range but not the other in order to the output range. Iterators
5578 * increment for each range. When the current element of one range is less
5579 * than the other according to @p comp, that element is copied and the
5580 * iterator advances. If an element is contained in both ranges according
5581 * to @p __comp, no elements are copied and both ranges advance. The output
5582 * range may not overlap either input range.
5584 template<typename _InputIterator1
, typename _InputIterator2
,
5585 typename _OutputIterator
, typename _Compare
>
5586 inline _OutputIterator
5587 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5588 _InputIterator2 __first2
, _InputIterator2 __last2
,
5589 _OutputIterator __result
,
5592 // concept requirements
5593 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5594 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5595 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5596 typename iterator_traits
<_InputIterator1
>::value_type
>)
5597 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5598 typename iterator_traits
<_InputIterator2
>::value_type
>)
5599 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5600 typename iterator_traits
<_InputIterator1
>::value_type
,
5601 typename iterator_traits
<_InputIterator2
>::value_type
>)
5602 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5603 typename iterator_traits
<_InputIterator2
>::value_type
,
5604 typename iterator_traits
<_InputIterator1
>::value_type
>)
5605 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5606 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5607 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5608 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5610 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5611 __first2
, __last2
, __result
,
5612 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5615 template<typename _ForwardIterator
, typename _Compare
>
5616 _GLIBCXX14_CONSTEXPR
5618 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5621 if (__first
== __last
)
5623 _ForwardIterator __result
= __first
;
5624 while (++__first
!= __last
)
5625 if (__comp(__first
, __result
))
5631 * @brief Return the minimum element in a range.
5632 * @ingroup sorting_algorithms
5633 * @param __first Start of range.
5634 * @param __last End of range.
5635 * @return Iterator referencing the first instance of the smallest value.
5637 template<typename _ForwardIterator
>
5638 _GLIBCXX14_CONSTEXPR
5640 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5642 // concept requirements
5643 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5644 __glibcxx_function_requires(_LessThanComparableConcept
<
5645 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5646 __glibcxx_requires_valid_range(__first
, __last
);
5647 __glibcxx_requires_irreflexive(__first
, __last
);
5649 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5650 __gnu_cxx::__ops::__iter_less_iter());
5654 * @brief Return the minimum element in a range using comparison functor.
5655 * @ingroup sorting_algorithms
5656 * @param __first Start of range.
5657 * @param __last End of range.
5658 * @param __comp Comparison functor.
5659 * @return Iterator referencing the first instance of the smallest value
5660 * according to __comp.
5662 template<typename _ForwardIterator
, typename _Compare
>
5663 _GLIBCXX14_CONSTEXPR
5664 inline _ForwardIterator
5665 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5668 // concept requirements
5669 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5670 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5671 typename iterator_traits
<_ForwardIterator
>::value_type
,
5672 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5673 __glibcxx_requires_valid_range(__first
, __last
);
5674 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5676 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5677 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5680 template<typename _ForwardIterator
, typename _Compare
>
5681 _GLIBCXX14_CONSTEXPR
5683 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5686 if (__first
== __last
) return __first
;
5687 _ForwardIterator __result
= __first
;
5688 while (++__first
!= __last
)
5689 if (__comp(__result
, __first
))
5695 * @brief Return the maximum element in a range.
5696 * @ingroup sorting_algorithms
5697 * @param __first Start of range.
5698 * @param __last End of range.
5699 * @return Iterator referencing the first instance of the largest value.
5701 template<typename _ForwardIterator
>
5702 _GLIBCXX14_CONSTEXPR
5703 inline _ForwardIterator
5704 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5706 // concept requirements
5707 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5708 __glibcxx_function_requires(_LessThanComparableConcept
<
5709 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5710 __glibcxx_requires_valid_range(__first
, __last
);
5711 __glibcxx_requires_irreflexive(__first
, __last
);
5713 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5714 __gnu_cxx::__ops::__iter_less_iter());
5718 * @brief Return the maximum element in a range using comparison functor.
5719 * @ingroup sorting_algorithms
5720 * @param __first Start of range.
5721 * @param __last End of range.
5722 * @param __comp Comparison functor.
5723 * @return Iterator referencing the first instance of the largest value
5724 * according to __comp.
5726 template<typename _ForwardIterator
, typename _Compare
>
5727 _GLIBCXX14_CONSTEXPR
5728 inline _ForwardIterator
5729 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5732 // concept requirements
5733 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5734 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5735 typename iterator_traits
<_ForwardIterator
>::value_type
,
5736 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5737 __glibcxx_requires_valid_range(__first
, __last
);
5738 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5740 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5741 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5744 #if __cplusplus >= 201402L
5745 /// Reservoir sampling algorithm.
5746 template<typename _InputIterator
, typename _RandomAccessIterator
,
5747 typename _Size
, typename _UniformRandomBitGenerator
>
5748 _RandomAccessIterator
5749 __sample(_InputIterator __first
, _InputIterator __last
, input_iterator_tag
,
5750 _RandomAccessIterator __out
, random_access_iterator_tag
,
5751 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5753 using __distrib_type
= uniform_int_distribution
<_Size
>;
5754 using __param_type
= typename
__distrib_type::param_type
;
5755 __distrib_type __d
{};
5756 _Size __sample_sz
= 0;
5757 while (__first
!= __last
&& __sample_sz
!= __n
)
5759 __out
[__sample_sz
++] = *__first
;
5762 for (auto __pop_sz
= __sample_sz
; __first
!= __last
;
5763 ++__first
, (void) ++__pop_sz
)
5765 const auto __k
= __d(__g
, __param_type
{0, __pop_sz
});
5767 __out
[__k
] = *__first
;
5769 return __out
+ __sample_sz
;
5772 /// Selection sampling algorithm.
5773 template<typename _ForwardIterator
, typename _OutputIterator
, typename _Cat
,
5774 typename _Size
, typename _UniformRandomBitGenerator
>
5776 __sample(_ForwardIterator __first
, _ForwardIterator __last
,
5777 forward_iterator_tag
,
5778 _OutputIterator __out
, _Cat
,
5779 _Size __n
, _UniformRandomBitGenerator
&& __g
)
5781 using __distrib_type
= uniform_int_distribution
<_Size
>;
5782 using __param_type
= typename
__distrib_type::param_type
;
5783 using _USize
= make_unsigned_t
<_Size
>;
5784 using _Gen
= remove_reference_t
<_UniformRandomBitGenerator
>;
5785 using __uc_type
= common_type_t
<typename
_Gen::result_type
, _USize
>;
5787 __distrib_type __d
{};
5788 _Size __unsampled_sz
= std::distance(__first
, __last
);
5789 __n
= std::min(__n
, __unsampled_sz
);
5791 // If possible, we use __gen_two_uniform_ints to efficiently produce
5792 // two random numbers using a single distribution invocation:
5794 const __uc_type __urngrange
= __g
.max() - __g
.min();
5795 if (__urngrange
/ __uc_type(__unsampled_sz
) >= __uc_type(__unsampled_sz
))
5796 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5799 while (__n
!= 0 && __unsampled_sz
>= 2)
5801 const pair
<_Size
, _Size
> __p
=
5802 __gen_two_uniform_ints(__unsampled_sz
, __unsampled_sz
- 1, __g
);
5805 if (__p
.first
< __n
)
5807 *__out
++ = *__first
;
5813 if (__n
== 0) break;
5816 if (__p
.second
< __n
)
5818 *__out
++ = *__first
;
5826 // The loop above is otherwise equivalent to this one-at-a-time version:
5828 for (; __n
!= 0; ++__first
)
5829 if (__d(__g
, __param_type
{0, --__unsampled_sz
}) < __n
)
5831 *__out
++ = *__first
;
5837 #if __cplusplus > 201402L
5838 #define __cpp_lib_sample 201603
5839 /// Take a random sample from a population.
5840 template<typename _PopulationIterator
, typename _SampleIterator
,
5841 typename _Distance
, typename _UniformRandomBitGenerator
>
5843 sample(_PopulationIterator __first
, _PopulationIterator __last
,
5844 _SampleIterator __out
, _Distance __n
,
5845 _UniformRandomBitGenerator
&& __g
)
5847 using __pop_cat
= typename
5848 std::iterator_traits
<_PopulationIterator
>::iterator_category
;
5849 using __samp_cat
= typename
5850 std::iterator_traits
<_SampleIterator
>::iterator_category
;
5853 __or_
<is_convertible
<__pop_cat
, forward_iterator_tag
>,
5854 is_convertible
<__samp_cat
, random_access_iterator_tag
>>::value
,
5855 "output range must use a RandomAccessIterator when input range"
5856 " does not meet the ForwardIterator requirements");
5858 static_assert(is_integral
<_Distance
>::value
,
5859 "sample size must be an integer type");
5861 typename iterator_traits
<_PopulationIterator
>::difference_type __d
= __n
;
5862 return _GLIBCXX_STD_A::
5863 __sample(__first
, __last
, __pop_cat
{}, __out
, __samp_cat
{}, __d
,
5864 std::forward
<_UniformRandomBitGenerator
>(__g
));
5869 _GLIBCXX_END_NAMESPACE_ALGO
5870 _GLIBCXX_END_NAMESPACE_VERSION
5873 #endif /* _STL_ALGO_H */