1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2013 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 <random> // for std::uniform_int_distribution
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 *__a
76 template<typename _Iterator
, typename _Compare
>
78 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
,
84 std::iter_swap(__a
, __b
);
85 else if (__comp(__a
, __c
))
86 std::iter_swap(__a
, __c
);
88 else if (__comp(__a
, __c
))
90 else if (__comp(__b
, __c
))
91 std::iter_swap(__a
, __c
);
93 std::iter_swap(__a
, __b
);
96 /// This is an overload used by find algos for the Input Iterator case.
97 template<typename _InputIterator
, typename _Predicate
>
99 __find_if(_InputIterator __first
, _InputIterator __last
,
100 _Predicate __pred
, input_iterator_tag
)
102 while (__first
!= __last
&& !__pred(__first
))
107 /// This is an overload used by find algos for the RAI case.
108 template<typename _RandomAccessIterator
, typename _Predicate
>
109 _RandomAccessIterator
110 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
111 _Predicate __pred
, random_access_iterator_tag
)
113 typename iterator_traits
<_RandomAccessIterator
>::difference_type
114 __trip_count
= (__last
- __first
) >> 2;
116 for (; __trip_count
> 0; --__trip_count
)
135 switch (__last
- __first
)
155 template<typename _Iterator
, typename _Predicate
>
157 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
159 return __find_if(__first
, __last
, __pred
,
160 std::__iterator_category(__first
));
163 /// Provided for stable_partition to use.
164 template<typename _InputIterator
, typename _Predicate
>
165 inline _InputIterator
166 __find_if_not(_InputIterator __first
, _InputIterator __last
,
169 return std::__find_if(__first
, __last
,
170 __gnu_cxx::__ops::__negate(__pred
),
171 std::__iterator_category(__first
));
174 /// Like find_if_not(), but uses and updates a count of the
175 /// remaining range length instead of comparing against an end
177 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
179 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
181 for (; __len
; --__len
, ++__first
)
182 if (!__pred(__first
))
189 // set_symmetric_difference
200 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
201 typename _BinaryPredicate
>
203 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
204 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
205 _BinaryPredicate __predicate
)
207 // Test for empty ranges
208 if (__first1
== __last1
|| __first2
== __last2
)
211 // Test for a pattern of length 1.
212 _ForwardIterator2
__p1(__first2
);
213 if (++__p1
== __last2
)
214 return std::__find_if(__first1
, __last1
,
215 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
218 _ForwardIterator2 __p
;
219 _ForwardIterator1 __current
= __first1
;
224 std::__find_if(__first1
, __last1
,
225 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
227 if (__first1
== __last1
)
231 __current
= __first1
;
232 if (++__current
== __last1
)
235 while (__predicate(__current
, __p
))
237 if (++__p
== __last2
)
239 if (++__current
== __last1
)
250 * This is an helper function for search_n overloaded for forward iterators.
252 template<typename _ForwardIterator
, typename _Integer
,
253 typename _UnaryPredicate
>
255 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
256 _Integer __count
, _UnaryPredicate __unary_pred
,
257 std::forward_iterator_tag
)
259 __first
= std::__find_if(__first
, __last
, __unary_pred
);
260 while (__first
!= __last
)
262 typename iterator_traits
<_ForwardIterator
>::difference_type
264 _ForwardIterator __i
= __first
;
266 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
275 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
281 * This is an helper function for search_n overloaded for random access
284 template<typename _RandomAccessIter
, typename _Integer
,
285 typename _UnaryPredicate
>
287 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
288 _Integer __count
, _UnaryPredicate __unary_pred
,
289 std::random_access_iterator_tag
)
291 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
294 _DistanceType __tailSize
= __last
- __first
;
295 _DistanceType __remainder
= __count
;
297 while (__remainder
<= __tailSize
) // the main loop...
299 __first
+= __remainder
;
300 __tailSize
-= __remainder
;
301 // __first here is always pointing to one past the last element of
302 // next possible match.
303 _RandomAccessIter __backTrack
= __first
;
304 while (__unary_pred(--__backTrack
))
306 if (--__remainder
== 0)
307 return (__first
- __count
); // Success
309 __remainder
= __count
+ 1 - (__first
- __backTrack
);
311 return __last
; // Failure
314 template<typename _ForwardIterator
, typename _Integer
,
315 typename _UnaryPredicate
>
317 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
319 _UnaryPredicate __unary_pred
)
325 return std::__find_if(__first
, __last
, __unary_pred
);
327 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
328 std::__iterator_category(__first
));
331 // find_end for forward iterators.
332 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
333 typename _BinaryPredicate
>
335 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
336 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
337 forward_iterator_tag
, forward_iterator_tag
,
338 _BinaryPredicate __comp
)
340 if (__first2
== __last2
)
343 _ForwardIterator1 __result
= __last1
;
346 _ForwardIterator1 __new_result
347 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
348 if (__new_result
== __last1
)
352 __result
= __new_result
;
353 __first1
= __new_result
;
359 // find_end for bidirectional iterators (much faster).
360 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
361 typename _BinaryPredicate
>
362 _BidirectionalIterator1
363 __find_end(_BidirectionalIterator1 __first1
,
364 _BidirectionalIterator1 __last1
,
365 _BidirectionalIterator2 __first2
,
366 _BidirectionalIterator2 __last2
,
367 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
368 _BinaryPredicate __comp
)
370 // concept requirements
371 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
372 _BidirectionalIterator1
>)
373 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
374 _BidirectionalIterator2
>)
376 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
377 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
379 _RevIterator1
__rlast1(__first1
);
380 _RevIterator2
__rlast2(__first2
);
381 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
382 _RevIterator2(__last2
), __rlast2
,
385 if (__rresult
== __rlast1
)
389 _BidirectionalIterator1 __result
= __rresult
.base();
390 std::advance(__result
, -std::distance(__first2
, __last2
));
396 * @brief Find last matching subsequence in a sequence.
397 * @ingroup non_mutating_algorithms
398 * @param __first1 Start of range to search.
399 * @param __last1 End of range to search.
400 * @param __first2 Start of sequence to match.
401 * @param __last2 End of sequence to match.
402 * @return The last iterator @c i in the range
403 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
404 * @p *(__first2+N) for each @c N in the range @p
405 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
407 * Searches the range @p [__first1,__last1) for a sub-sequence that
408 * compares equal value-by-value with the sequence given by @p
409 * [__first2,__last2) and returns an iterator to the __first
410 * element of the sub-sequence, or @p __last1 if the sub-sequence
411 * is not found. The sub-sequence will be the last such
412 * subsequence contained in [__first1,__last1).
414 * Because the sub-sequence must lie completely within the range @p
415 * [__first1,__last1) it must start at a position less than @p
416 * __last1-(__last2-__first2) where @p __last2-__first2 is the
417 * length of the sub-sequence. This means that the returned
418 * iterator @c i will be in the range @p
419 * [__first1,__last1-(__last2-__first2))
421 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
422 inline _ForwardIterator1
423 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
424 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
426 // concept requirements
427 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
428 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
429 __glibcxx_function_requires(_EqualOpConcept
<
430 typename iterator_traits
<_ForwardIterator1
>::value_type
,
431 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
432 __glibcxx_requires_valid_range(__first1
, __last1
);
433 __glibcxx_requires_valid_range(__first2
, __last2
);
435 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
436 std::__iterator_category(__first1
),
437 std::__iterator_category(__first2
),
438 __gnu_cxx::__ops::__iter_equal_to_iter());
442 * @brief Find last matching subsequence in a sequence using a predicate.
443 * @ingroup non_mutating_algorithms
444 * @param __first1 Start of range to search.
445 * @param __last1 End of range to search.
446 * @param __first2 Start of sequence to match.
447 * @param __last2 End of sequence to match.
448 * @param __comp The predicate to use.
449 * @return The last iterator @c i in the range @p
450 * [__first1,__last1-(__last2-__first2)) such that @c
451 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
452 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
455 * Searches the range @p [__first1,__last1) for a sub-sequence that
456 * compares equal value-by-value with the sequence given by @p
457 * [__first2,__last2) using comp as a predicate and returns an
458 * iterator to the first element of the sub-sequence, or @p __last1
459 * if the sub-sequence is not found. The sub-sequence will be the
460 * last such subsequence contained in [__first,__last1).
462 * Because the sub-sequence must lie completely within the range @p
463 * [__first1,__last1) it must start at a position less than @p
464 * __last1-(__last2-__first2) where @p __last2-__first2 is the
465 * length of the sub-sequence. This means that the returned
466 * iterator @c i will be in the range @p
467 * [__first1,__last1-(__last2-__first2))
469 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
470 typename _BinaryPredicate
>
471 inline _ForwardIterator1
472 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
473 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
474 _BinaryPredicate __comp
)
476 // concept requirements
477 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
478 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
479 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
480 typename iterator_traits
<_ForwardIterator1
>::value_type
,
481 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
482 __glibcxx_requires_valid_range(__first1
, __last1
);
483 __glibcxx_requires_valid_range(__first2
, __last2
);
485 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
486 std::__iterator_category(__first1
),
487 std::__iterator_category(__first2
),
488 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
491 #if __cplusplus >= 201103L
493 * @brief Checks that a predicate is true for all the elements
495 * @ingroup non_mutating_algorithms
496 * @param __first An input iterator.
497 * @param __last An input iterator.
498 * @param __pred A predicate.
499 * @return True if the check is true, false otherwise.
501 * Returns true if @p __pred is true for each element in the range
502 * @p [__first,__last), and false otherwise.
504 template<typename _InputIterator
, typename _Predicate
>
506 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
507 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
510 * @brief Checks that a predicate is false for all the elements
512 * @ingroup non_mutating_algorithms
513 * @param __first An input iterator.
514 * @param __last An input iterator.
515 * @param __pred A predicate.
516 * @return True if the check is true, false otherwise.
518 * Returns true if @p __pred is false for each element in the range
519 * @p [__first,__last), and false otherwise.
521 template<typename _InputIterator
, typename _Predicate
>
523 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
524 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
527 * @brief Checks that a predicate is false for at least an element
529 * @ingroup non_mutating_algorithms
530 * @param __first An input iterator.
531 * @param __last An input iterator.
532 * @param __pred A predicate.
533 * @return True if the check is true, false otherwise.
535 * Returns true if an element exists in the range @p
536 * [__first,__last) such that @p __pred is true, and false
539 template<typename _InputIterator
, typename _Predicate
>
541 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
542 { return !std::none_of(__first
, __last
, __pred
); }
545 * @brief Find the first element in a sequence for which a
546 * predicate is false.
547 * @ingroup non_mutating_algorithms
548 * @param __first An input iterator.
549 * @param __last An input iterator.
550 * @param __pred A predicate.
551 * @return The first iterator @c i in the range @p [__first,__last)
552 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
554 template<typename _InputIterator
, typename _Predicate
>
555 inline _InputIterator
556 find_if_not(_InputIterator __first
, _InputIterator __last
,
559 // concept requirements
560 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
561 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
562 typename iterator_traits
<_InputIterator
>::value_type
>)
563 __glibcxx_requires_valid_range(__first
, __last
);
564 return std::__find_if_not(__first
, __last
,
565 __gnu_cxx::__ops::__pred_iter(__pred
));
569 * @brief Checks whether the sequence is partitioned.
570 * @ingroup mutating_algorithms
571 * @param __first An input iterator.
572 * @param __last An input iterator.
573 * @param __pred A predicate.
574 * @return True if the range @p [__first,__last) is partioned by @p __pred,
575 * i.e. if all elements that satisfy @p __pred appear before those that
578 template<typename _InputIterator
, typename _Predicate
>
580 is_partitioned(_InputIterator __first
, _InputIterator __last
,
583 __first
= std::find_if_not(__first
, __last
, __pred
);
584 return std::none_of(__first
, __last
, __pred
);
588 * @brief Find the partition point of a partitioned range.
589 * @ingroup mutating_algorithms
590 * @param __first An iterator.
591 * @param __last Another iterator.
592 * @param __pred A predicate.
593 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
594 * and @p none_of(mid, __last, __pred) are both true.
596 template<typename _ForwardIterator
, typename _Predicate
>
598 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
601 // concept requirements
602 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
603 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
604 typename iterator_traits
<_ForwardIterator
>::value_type
>)
606 // A specific debug-mode test will be necessary...
607 __glibcxx_requires_valid_range(__first
, __last
);
609 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
612 _DistanceType __len
= std::distance(__first
, __last
);
613 _DistanceType __half
;
614 _ForwardIterator __middle
;
620 std::advance(__middle
, __half
);
621 if (__pred(*__middle
))
625 __len
= __len
- __half
- 1;
634 template<typename _InputIterator
, typename _OutputIterator
,
637 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
638 _OutputIterator __result
, _Predicate __pred
)
640 for (; __first
!= __last
; ++__first
)
641 if (!__pred(__first
))
643 *__result
= *__first
;
650 * @brief Copy a sequence, removing elements of a given value.
651 * @ingroup mutating_algorithms
652 * @param __first An input iterator.
653 * @param __last An input iterator.
654 * @param __result An output iterator.
655 * @param __value The value to be removed.
656 * @return An iterator designating the end of the resulting sequence.
658 * Copies each element in the range @p [__first,__last) not equal
659 * to @p __value to the range beginning at @p __result.
660 * remove_copy() is stable, so the relative order of elements that
661 * are copied is unchanged.
663 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
665 remove_copy(_InputIterator __first
, _InputIterator __last
,
666 _OutputIterator __result
, const _Tp
& __value
)
668 // concept requirements
669 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
670 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
671 typename iterator_traits
<_InputIterator
>::value_type
>)
672 __glibcxx_function_requires(_EqualOpConcept
<
673 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
674 __glibcxx_requires_valid_range(__first
, __last
);
676 return std::__remove_copy_if(__first
, __last
, __result
,
677 __gnu_cxx::__ops::__iter_equals_val(__value
));
681 * @brief Copy a sequence, removing elements for which a predicate is true.
682 * @ingroup mutating_algorithms
683 * @param __first An input iterator.
684 * @param __last An input iterator.
685 * @param __result An output iterator.
686 * @param __pred A predicate.
687 * @return An iterator designating the end of the resulting sequence.
689 * Copies each element in the range @p [__first,__last) for which
690 * @p __pred returns false to the range beginning at @p __result.
692 * remove_copy_if() is stable, so the relative order of elements that are
693 * copied is unchanged.
695 template<typename _InputIterator
, typename _OutputIterator
,
698 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
699 _OutputIterator __result
, _Predicate __pred
)
701 // concept requirements
702 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
703 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
704 typename iterator_traits
<_InputIterator
>::value_type
>)
705 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
706 typename iterator_traits
<_InputIterator
>::value_type
>)
707 __glibcxx_requires_valid_range(__first
, __last
);
709 return std::__remove_copy_if(__first
, __last
, __result
,
710 __gnu_cxx::__ops::__pred_iter(__pred
));
713 #if __cplusplus >= 201103L
715 * @brief Copy the elements of a sequence for which a predicate is true.
716 * @ingroup mutating_algorithms
717 * @param __first An input iterator.
718 * @param __last An input iterator.
719 * @param __result An output iterator.
720 * @param __pred A predicate.
721 * @return An iterator designating the end of the resulting sequence.
723 * Copies each element in the range @p [__first,__last) for which
724 * @p __pred returns true to the range beginning at @p __result.
726 * copy_if() is stable, so the relative order of elements that are
727 * copied is unchanged.
729 template<typename _InputIterator
, typename _OutputIterator
,
732 copy_if(_InputIterator __first
, _InputIterator __last
,
733 _OutputIterator __result
, _Predicate __pred
)
735 // concept requirements
736 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
737 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
738 typename iterator_traits
<_InputIterator
>::value_type
>)
739 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
740 typename iterator_traits
<_InputIterator
>::value_type
>)
741 __glibcxx_requires_valid_range(__first
, __last
);
743 for (; __first
!= __last
; ++__first
)
744 if (__pred(*__first
))
746 *__result
= *__first
;
752 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
754 __copy_n(_InputIterator __first
, _Size __n
,
755 _OutputIterator __result
, input_iterator_tag
)
761 *__result
= *__first
;
772 template<typename _RandomAccessIterator
, typename _Size
,
773 typename _OutputIterator
>
774 inline _OutputIterator
775 __copy_n(_RandomAccessIterator __first
, _Size __n
,
776 _OutputIterator __result
, random_access_iterator_tag
)
777 { return std::copy(__first
, __first
+ __n
, __result
); }
780 * @brief Copies the range [first,first+n) into [result,result+n).
781 * @ingroup mutating_algorithms
782 * @param __first An input iterator.
783 * @param __n The number of elements to copy.
784 * @param __result An output iterator.
787 * This inline function will boil down to a call to @c memmove whenever
788 * possible. Failing that, if random access iterators are passed, then the
789 * loop count will be known (and therefore a candidate for compiler
790 * optimizations such as unrolling).
792 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
793 inline _OutputIterator
794 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
796 // concept requirements
797 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
798 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
799 typename iterator_traits
<_InputIterator
>::value_type
>)
801 return std::__copy_n(__first
, __n
, __result
,
802 std::__iterator_category(__first
));
806 * @brief Copy the elements of a sequence to separate output sequences
807 * depending on the truth value of a predicate.
808 * @ingroup mutating_algorithms
809 * @param __first An input iterator.
810 * @param __last An input iterator.
811 * @param __out_true An output iterator.
812 * @param __out_false An output iterator.
813 * @param __pred A predicate.
814 * @return A pair designating the ends of the resulting sequences.
816 * Copies each element in the range @p [__first,__last) for which
817 * @p __pred returns true to the range beginning at @p out_true
818 * and each element for which @p __pred returns false to @p __out_false.
820 template<typename _InputIterator
, typename _OutputIterator1
,
821 typename _OutputIterator2
, typename _Predicate
>
822 pair
<_OutputIterator1
, _OutputIterator2
>
823 partition_copy(_InputIterator __first
, _InputIterator __last
,
824 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
827 // concept requirements
828 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
829 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
830 typename iterator_traits
<_InputIterator
>::value_type
>)
831 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
832 typename iterator_traits
<_InputIterator
>::value_type
>)
833 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
834 typename iterator_traits
<_InputIterator
>::value_type
>)
835 __glibcxx_requires_valid_range(__first
, __last
);
837 for (; __first
!= __last
; ++__first
)
838 if (__pred(*__first
))
840 *__out_true
= *__first
;
845 *__out_false
= *__first
;
849 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
853 template<typename _ForwardIterator
, typename _Predicate
>
855 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
858 __first
= std::__find_if(__first
, __last
, __pred
);
859 if (__first
== __last
)
861 _ForwardIterator __result
= __first
;
863 for (; __first
!= __last
; ++__first
)
864 if (!__pred(__first
))
866 *__result
= _GLIBCXX_MOVE(*__first
);
873 * @brief Remove elements from a sequence.
874 * @ingroup mutating_algorithms
875 * @param __first An input iterator.
876 * @param __last An input iterator.
877 * @param __value The value to be removed.
878 * @return An iterator designating the end of the resulting sequence.
880 * All elements equal to @p __value are removed from the range
881 * @p [__first,__last).
883 * remove() is stable, so the relative order of elements that are
884 * not removed is unchanged.
886 * Elements between the end of the resulting sequence and @p __last
887 * are still present, but their value is unspecified.
889 template<typename _ForwardIterator
, typename _Tp
>
890 inline _ForwardIterator
891 remove(_ForwardIterator __first
, _ForwardIterator __last
,
894 // concept requirements
895 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
897 __glibcxx_function_requires(_EqualOpConcept
<
898 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
899 __glibcxx_requires_valid_range(__first
, __last
);
901 return std::__remove_if(__first
, __last
,
902 __gnu_cxx::__ops::__iter_equals_val(__value
));
906 * @brief Remove elements from a sequence using a predicate.
907 * @ingroup mutating_algorithms
908 * @param __first A forward iterator.
909 * @param __last A forward iterator.
910 * @param __pred A predicate.
911 * @return An iterator designating the end of the resulting sequence.
913 * All elements for which @p __pred returns true are removed from the range
914 * @p [__first,__last).
916 * remove_if() is stable, so the relative order of elements that are
917 * not removed is unchanged.
919 * Elements between the end of the resulting sequence and @p __last
920 * are still present, but their value is unspecified.
922 template<typename _ForwardIterator
, typename _Predicate
>
923 inline _ForwardIterator
924 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
927 // concept requirements
928 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
930 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
931 typename iterator_traits
<_ForwardIterator
>::value_type
>)
932 __glibcxx_requires_valid_range(__first
, __last
);
934 return std::__remove_if(__first
, __last
,
935 __gnu_cxx::__ops::__pred_iter(__pred
));
938 template<typename _ForwardIterator
, typename _BinaryPredicate
>
940 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
941 _BinaryPredicate __binary_pred
)
943 if (__first
== __last
)
945 _ForwardIterator __next
= __first
;
946 while (++__next
!= __last
)
948 if (__binary_pred(__first
, __next
))
955 template<typename _ForwardIterator
, typename _BinaryPredicate
>
957 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
958 _BinaryPredicate __binary_pred
)
960 // Skip the beginning, if already unique.
961 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
962 if (__first
== __last
)
965 // Do the real copy work.
966 _ForwardIterator __dest
= __first
;
968 while (++__first
!= __last
)
969 if (!__binary_pred(__dest
, __first
))
970 *++__dest
= _GLIBCXX_MOVE(*__first
);
975 * @brief Remove consecutive duplicate values from a sequence.
976 * @ingroup mutating_algorithms
977 * @param __first A forward iterator.
978 * @param __last A forward iterator.
979 * @return An iterator designating the end of the resulting sequence.
981 * Removes all but the first element from each group of consecutive
982 * values that compare equal.
983 * unique() is stable, so the relative order of elements that are
984 * not removed is unchanged.
985 * Elements between the end of the resulting sequence and @p __last
986 * are still present, but their value is unspecified.
988 template<typename _ForwardIterator
>
989 inline _ForwardIterator
990 unique(_ForwardIterator __first
, _ForwardIterator __last
)
992 // concept requirements
993 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
995 __glibcxx_function_requires(_EqualityComparableConcept
<
996 typename iterator_traits
<_ForwardIterator
>::value_type
>)
997 __glibcxx_requires_valid_range(__first
, __last
);
999 return std::__unique(__first
, __last
,
1000 __gnu_cxx::__ops::__iter_equal_to_iter());
1004 * @brief Remove consecutive values from a sequence using a predicate.
1005 * @ingroup mutating_algorithms
1006 * @param __first A forward iterator.
1007 * @param __last A forward iterator.
1008 * @param __binary_pred A binary predicate.
1009 * @return An iterator designating the end of the resulting sequence.
1011 * Removes all but the first element from each group of consecutive
1012 * values for which @p __binary_pred returns true.
1013 * unique() is stable, so the relative order of elements that are
1014 * not removed is unchanged.
1015 * Elements between the end of the resulting sequence and @p __last
1016 * are still present, but their value is unspecified.
1018 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1019 inline _ForwardIterator
1020 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1021 _BinaryPredicate __binary_pred
)
1023 // concept requirements
1024 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1026 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1027 typename iterator_traits
<_ForwardIterator
>::value_type
,
1028 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1029 __glibcxx_requires_valid_range(__first
, __last
);
1031 return std::__unique(__first
, __last
,
1032 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1036 * This is an uglified
1037 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1039 * overloaded for forward iterators and output iterator as result.
1041 template<typename _ForwardIterator
, typename _OutputIterator
,
1042 typename _BinaryPredicate
>
1044 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1045 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1046 forward_iterator_tag
, output_iterator_tag
)
1048 // concept requirements -- iterators already checked
1049 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1050 typename iterator_traits
<_ForwardIterator
>::value_type
,
1051 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1053 _ForwardIterator __next
= __first
;
1054 *__result
= *__first
;
1055 while (++__next
!= __last
)
1056 if (!__binary_pred(__first
, __next
))
1059 *++__result
= *__first
;
1065 * This is an uglified
1066 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1068 * overloaded for input iterators and output iterator as result.
1070 template<typename _InputIterator
, typename _OutputIterator
,
1071 typename _BinaryPredicate
>
1073 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1074 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1075 input_iterator_tag
, output_iterator_tag
)
1077 // concept requirements -- iterators already checked
1078 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1079 typename iterator_traits
<_InputIterator
>::value_type
,
1080 typename iterator_traits
<_InputIterator
>::value_type
>)
1082 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1083 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1085 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1086 *__result
= __value
;
1087 while (++__first
!= __last
)
1088 if (!__rebound_pred(__first
, __value
))
1091 *++__result
= __value
;
1097 * This is an uglified
1098 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1100 * overloaded for input iterators and forward iterator as result.
1102 template<typename _InputIterator
, typename _ForwardIterator
,
1103 typename _BinaryPredicate
>
1105 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1106 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1107 input_iterator_tag
, forward_iterator_tag
)
1109 // concept requirements -- iterators already checked
1110 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1111 typename iterator_traits
<_ForwardIterator
>::value_type
,
1112 typename iterator_traits
<_InputIterator
>::value_type
>)
1113 *__result
= *__first
;
1114 while (++__first
!= __last
)
1115 if (!__binary_pred(__result
, __first
))
1116 *++__result
= *__first
;
1121 * This is an uglified reverse(_BidirectionalIterator,
1122 * _BidirectionalIterator)
1123 * overloaded for bidirectional iterators.
1125 template<typename _BidirectionalIterator
>
1127 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1128 bidirectional_iterator_tag
)
1131 if (__first
== __last
|| __first
== --__last
)
1135 std::iter_swap(__first
, __last
);
1141 * This is an uglified reverse(_BidirectionalIterator,
1142 * _BidirectionalIterator)
1143 * overloaded for random access iterators.
1145 template<typename _RandomAccessIterator
>
1147 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1148 random_access_iterator_tag
)
1150 if (__first
== __last
)
1153 while (__first
< __last
)
1155 std::iter_swap(__first
, __last
);
1162 * @brief Reverse a sequence.
1163 * @ingroup mutating_algorithms
1164 * @param __first A bidirectional iterator.
1165 * @param __last A bidirectional iterator.
1166 * @return reverse() returns no value.
1168 * Reverses the order of the elements in the range @p [__first,__last),
1169 * so that the first element becomes the last etc.
1170 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1171 * swaps @p *(__first+i) and @p *(__last-(i+1))
1173 template<typename _BidirectionalIterator
>
1175 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1177 // concept requirements
1178 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1179 _BidirectionalIterator
>)
1180 __glibcxx_requires_valid_range(__first
, __last
);
1181 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1185 * @brief Copy a sequence, reversing its elements.
1186 * @ingroup mutating_algorithms
1187 * @param __first A bidirectional iterator.
1188 * @param __last A bidirectional iterator.
1189 * @param __result An output iterator.
1190 * @return An iterator designating the end of the resulting sequence.
1192 * Copies the elements in the range @p [__first,__last) to the
1193 * range @p [__result,__result+(__last-__first)) such that the
1194 * order of the elements is reversed. For every @c i such that @p
1195 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1196 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1197 * The ranges @p [__first,__last) and @p
1198 * [__result,__result+(__last-__first)) must not overlap.
1200 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1202 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1203 _OutputIterator __result
)
1205 // concept requirements
1206 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1207 _BidirectionalIterator
>)
1208 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1209 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1210 __glibcxx_requires_valid_range(__first
, __last
);
1212 while (__first
!= __last
)
1215 *__result
= *__last
;
1222 * This is a helper function for the rotate algorithm specialized on RAIs.
1223 * It returns the greatest common divisor of two integer values.
1225 template<typename _EuclideanRingElement
>
1226 _EuclideanRingElement
1227 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1231 _EuclideanRingElement __t
= __m
% __n
;
1238 /// This is a helper function for the rotate algorithm.
1239 template<typename _ForwardIterator
>
1241 __rotate(_ForwardIterator __first
,
1242 _ForwardIterator __middle
,
1243 _ForwardIterator __last
,
1244 forward_iterator_tag
)
1246 if (__first
== __middle
|| __last
== __middle
)
1249 _ForwardIterator __first2
= __middle
;
1252 std::iter_swap(__first
, __first2
);
1255 if (__first
== __middle
)
1256 __middle
= __first2
;
1258 while (__first2
!= __last
);
1260 __first2
= __middle
;
1262 while (__first2
!= __last
)
1264 std::iter_swap(__first
, __first2
);
1267 if (__first
== __middle
)
1268 __middle
= __first2
;
1269 else if (__first2
== __last
)
1270 __first2
= __middle
;
1274 /// This is a helper function for the rotate algorithm.
1275 template<typename _BidirectionalIterator
>
1277 __rotate(_BidirectionalIterator __first
,
1278 _BidirectionalIterator __middle
,
1279 _BidirectionalIterator __last
,
1280 bidirectional_iterator_tag
)
1282 // concept requirements
1283 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1284 _BidirectionalIterator
>)
1286 if (__first
== __middle
|| __last
== __middle
)
1289 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1290 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1292 while (__first
!= __middle
&& __middle
!= __last
)
1294 std::iter_swap(__first
, --__last
);
1298 if (__first
== __middle
)
1299 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1301 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1304 /// This is a helper function for the rotate algorithm.
1305 template<typename _RandomAccessIterator
>
1307 __rotate(_RandomAccessIterator __first
,
1308 _RandomAccessIterator __middle
,
1309 _RandomAccessIterator __last
,
1310 random_access_iterator_tag
)
1312 // concept requirements
1313 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1314 _RandomAccessIterator
>)
1316 if (__first
== __middle
|| __last
== __middle
)
1319 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1321 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1324 _Distance __n
= __last
- __first
;
1325 _Distance __k
= __middle
- __first
;
1327 if (__k
== __n
- __k
)
1329 std::swap_ranges(__first
, __middle
, __middle
);
1333 _RandomAccessIterator __p
= __first
;
1337 if (__k
< __n
- __k
)
1339 if (__is_pod(_ValueType
) && __k
== 1)
1341 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1342 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1343 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1346 _RandomAccessIterator __q
= __p
+ __k
;
1347 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1349 std::iter_swap(__p
, __q
);
1356 std::swap(__n
, __k
);
1362 if (__is_pod(_ValueType
) && __k
== 1)
1364 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1365 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1366 *__p
= _GLIBCXX_MOVE(__t
);
1369 _RandomAccessIterator __q
= __p
+ __n
;
1371 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1375 std::iter_swap(__p
, __q
);
1380 std::swap(__n
, __k
);
1386 * @brief Rotate the elements of a sequence.
1387 * @ingroup mutating_algorithms
1388 * @param __first A forward iterator.
1389 * @param __middle A forward iterator.
1390 * @param __last A forward iterator.
1393 * Rotates the elements of the range @p [__first,__last) by
1394 * @p (__middle - __first) positions so that the element at @p __middle
1395 * is moved to @p __first, the element at @p __middle+1 is moved to
1396 * @p __first+1 and so on for each element in the range
1397 * @p [__first,__last).
1399 * This effectively swaps the ranges @p [__first,__middle) and
1400 * @p [__middle,__last).
1403 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1404 * for each @p n in the range @p [0,__last-__first).
1406 template<typename _ForwardIterator
>
1408 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1409 _ForwardIterator __last
)
1411 // concept requirements
1412 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1414 __glibcxx_requires_valid_range(__first
, __middle
);
1415 __glibcxx_requires_valid_range(__middle
, __last
);
1417 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1419 std::__rotate(__first
, __middle
, __last
, _IterType());
1423 * @brief Copy a sequence, rotating its elements.
1424 * @ingroup mutating_algorithms
1425 * @param __first A forward iterator.
1426 * @param __middle A forward iterator.
1427 * @param __last A forward iterator.
1428 * @param __result An output iterator.
1429 * @return An iterator designating the end of the resulting sequence.
1431 * Copies the elements of the range @p [__first,__last) to the
1432 * range beginning at @result, rotating the copied elements by
1433 * @p (__middle-__first) positions so that the element at @p __middle
1434 * is moved to @p __result, the element at @p __middle+1 is moved
1435 * to @p __result+1 and so on for each element in the range @p
1439 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1440 * for each @p n in the range @p [0,__last-__first).
1442 template<typename _ForwardIterator
, typename _OutputIterator
>
1444 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1445 _ForwardIterator __last
, _OutputIterator __result
)
1447 // concept requirements
1448 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1449 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1450 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1451 __glibcxx_requires_valid_range(__first
, __middle
);
1452 __glibcxx_requires_valid_range(__middle
, __last
);
1454 return std::copy(__first
, __middle
,
1455 std::copy(__middle
, __last
, __result
));
1458 /// This is a helper function...
1459 template<typename _ForwardIterator
, typename _Predicate
>
1461 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1462 _Predicate __pred
, forward_iterator_tag
)
1464 if (__first
== __last
)
1467 while (__pred(*__first
))
1468 if (++__first
== __last
)
1471 _ForwardIterator __next
= __first
;
1473 while (++__next
!= __last
)
1474 if (__pred(*__next
))
1476 std::iter_swap(__first
, __next
);
1483 /// This is a helper function...
1484 template<typename _BidirectionalIterator
, typename _Predicate
>
1485 _BidirectionalIterator
1486 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1487 _Predicate __pred
, bidirectional_iterator_tag
)
1492 if (__first
== __last
)
1494 else if (__pred(*__first
))
1500 if (__first
== __last
)
1502 else if (!bool(__pred(*__last
)))
1506 std::iter_swap(__first
, __last
);
1513 /// This is a helper function...
1514 /// Requires __len != 0 and !__pred(*__first),
1515 /// same as __stable_partition_adaptive.
1516 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1518 __inplace_stable_partition(_ForwardIterator __first
,
1519 _Predicate __pred
, _Distance __len
)
1523 _ForwardIterator __middle
= __first
;
1524 std::advance(__middle
, __len
/ 2);
1525 _ForwardIterator __left_split
=
1526 std::__inplace_stable_partition(__first
, __pred
, __len
/ 2);
1527 // Advance past true-predicate values to satisfy this
1528 // function's preconditions.
1529 _Distance __right_len
= __len
- __len
/ 2;
1530 _ForwardIterator __right_split
=
1531 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1533 __right_split
= std::__inplace_stable_partition(__middle
,
1536 std::rotate(__left_split
, __middle
, __right_split
);
1537 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1538 return __left_split
;
1541 /// This is a helper function...
1542 /// Requires __first != __last and !__pred(__first)
1543 /// and __len == distance(__first, __last).
1545 /// !__pred(__first) allows us to guarantee that we don't
1546 /// move-assign an element onto itself.
1547 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1550 __stable_partition_adaptive(_ForwardIterator __first
,
1551 _ForwardIterator __last
,
1552 _Predicate __pred
, _Distance __len
,
1554 _Distance __buffer_size
)
1556 if (__len
<= __buffer_size
)
1558 _ForwardIterator __result1
= __first
;
1559 _Pointer __result2
= __buffer
;
1560 // The precondition guarantees that !__pred(__first), so
1561 // move that element to the buffer before starting the loop.
1562 // This ensures that we only call __pred once per element.
1563 *__result2
= _GLIBCXX_MOVE(*__first
);
1566 for (; __first
!= __last
; ++__first
)
1567 if (__pred(__first
))
1569 *__result1
= _GLIBCXX_MOVE(*__first
);
1574 *__result2
= _GLIBCXX_MOVE(*__first
);
1577 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1582 _ForwardIterator __middle
= __first
;
1583 std::advance(__middle
, __len
/ 2);
1584 _ForwardIterator __left_split
=
1585 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1586 __len
/ 2, __buffer
,
1588 // Advance past true-predicate values to satisfy this
1589 // function's preconditions.
1590 _Distance __right_len
= __len
- __len
/ 2;
1591 _ForwardIterator __right_split
=
1592 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1595 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1597 __buffer
, __buffer_size
);
1598 std::rotate(__left_split
, __middle
, __right_split
);
1599 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1600 return __left_split
;
1604 template<typename _ForwardIterator
, typename _Predicate
>
1606 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1609 __first
= std::__find_if_not(__first
, __last
, __pred
);
1611 if (__first
== __last
)
1614 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1616 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1619 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
, __last
);
1620 if (__buf
.size() > 0)
1622 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1623 _DistanceType(__buf
.requested_size()),
1625 _DistanceType(__buf
.size()));
1628 std::__inplace_stable_partition(__first
, __pred
,
1629 _DistanceType(__buf
.requested_size()));
1633 * @brief Move elements for which a predicate is true to the beginning
1634 * of a sequence, preserving relative ordering.
1635 * @ingroup mutating_algorithms
1636 * @param __first A forward iterator.
1637 * @param __last A forward iterator.
1638 * @param __pred A predicate functor.
1639 * @return An iterator @p middle such that @p __pred(i) is true for each
1640 * iterator @p i in the range @p [first,middle) and false for each @p i
1641 * in the range @p [middle,last).
1643 * Performs the same function as @p partition() with the additional
1644 * guarantee that the relative ordering of elements in each group is
1645 * preserved, so any two elements @p x and @p y in the range
1646 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1647 * relative ordering after calling @p stable_partition().
1649 template<typename _ForwardIterator
, typename _Predicate
>
1651 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1654 // concept requirements
1655 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1657 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1658 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1659 __glibcxx_requires_valid_range(__first
, __last
);
1661 return std::__stable_partition(__first
, __last
,
1662 __gnu_cxx::__ops::__pred_iter(__pred
));
1665 /// This is a helper function for the sort routines.
1666 template<typename _RandomAccessIterator
, typename _Compare
>
1668 __heap_select(_RandomAccessIterator __first
,
1669 _RandomAccessIterator __middle
,
1670 _RandomAccessIterator __last
, _Compare __comp
)
1672 std::__make_heap(__first
, __middle
, __comp
);
1673 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1674 if (__comp(__i
, __first
))
1675 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1680 template<typename _InputIterator
, typename _RandomAccessIterator
,
1682 _RandomAccessIterator
1683 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1684 _RandomAccessIterator __result_first
,
1685 _RandomAccessIterator __result_last
,
1688 typedef typename iterator_traits
<_InputIterator
>::value_type
1690 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1691 typedef typename
_RItTraits::difference_type _DistanceType
;
1693 if (__result_first
== __result_last
)
1694 return __result_last
;
1695 _RandomAccessIterator __result_real_last
= __result_first
;
1696 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1698 *__result_real_last
= *__first
;
1699 ++__result_real_last
;
1703 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1704 while (__first
!= __last
)
1706 if (__comp(__first
, __result_first
))
1707 std::__adjust_heap(__result_first
, _DistanceType(0),
1708 _DistanceType(__result_real_last
1710 _InputValueType(*__first
), __comp
);
1713 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1714 return __result_real_last
;
1718 * @brief Copy the smallest elements of a sequence.
1719 * @ingroup sorting_algorithms
1720 * @param __first An iterator.
1721 * @param __last Another iterator.
1722 * @param __result_first A random-access iterator.
1723 * @param __result_last Another random-access iterator.
1724 * @return An iterator indicating the end of the resulting sequence.
1726 * Copies and sorts the smallest N values from the range @p [__first,__last)
1727 * to the range beginning at @p __result_first, where the number of
1728 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1729 * @p (__result_last-__result_first).
1730 * After the sort if @e i and @e j are iterators in the range
1731 * @p [__result_first,__result_first+N) such that i precedes j then
1733 * The value returned is @p __result_first+N.
1735 template<typename _InputIterator
, typename _RandomAccessIterator
>
1736 _RandomAccessIterator
1737 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1738 _RandomAccessIterator __result_first
,
1739 _RandomAccessIterator __result_last
)
1741 typedef typename iterator_traits
<_InputIterator
>::value_type
1743 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1745 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1748 // concept requirements
1749 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1750 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1752 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1754 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1755 __glibcxx_requires_valid_range(__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 _RandomAccessIterator
1786 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1787 _RandomAccessIterator __result_first
,
1788 _RandomAccessIterator __result_last
,
1791 typedef typename iterator_traits
<_InputIterator
>::value_type
1793 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1795 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_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_valid_range(__result_first
, __result_last
);
1811 return std::__partial_sort_copy(__first
, __last
,
1812 __result_first
, __result_last
,
1813 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1816 /// This is a helper function for the sort routine.
1817 template<typename _RandomAccessIterator
, typename _Compare
>
1819 __unguarded_linear_insert(_RandomAccessIterator __last
,
1822 typename iterator_traits
<_RandomAccessIterator
>::value_type
1823 __val
= _GLIBCXX_MOVE(*__last
);
1824 _RandomAccessIterator __next
= __last
;
1826 while (__comp(__val
, __next
))
1828 *__last
= _GLIBCXX_MOVE(*__next
);
1832 *__last
= _GLIBCXX_MOVE(__val
);
1835 /// This is a helper function for the sort routine.
1836 template<typename _RandomAccessIterator
, typename _Compare
>
1838 __insertion_sort(_RandomAccessIterator __first
,
1839 _RandomAccessIterator __last
, _Compare __comp
)
1841 if (__first
== __last
) return;
1843 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1845 if (__comp(__i
, __first
))
1847 typename iterator_traits
<_RandomAccessIterator
>::value_type
1848 __val
= _GLIBCXX_MOVE(*__i
);
1849 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1850 *__first
= _GLIBCXX_MOVE(__val
);
1853 std::__unguarded_linear_insert(__i
,
1854 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1858 /// This is a helper function for the sort routine.
1859 template<typename _RandomAccessIterator
, typename _Compare
>
1861 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1862 _RandomAccessIterator __last
, _Compare __comp
)
1864 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1865 std::__unguarded_linear_insert(__i
,
1866 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1871 * This controls some aspect of the sort routines.
1873 enum { _S_threshold
= 16 };
1875 /// This is a helper function for the sort routine.
1876 template<typename _RandomAccessIterator
, typename _Compare
>
1878 __final_insertion_sort(_RandomAccessIterator __first
,
1879 _RandomAccessIterator __last
, _Compare __comp
)
1881 if (__last
- __first
> int(_S_threshold
))
1883 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1884 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1888 std::__insertion_sort(__first
, __last
, __comp
);
1891 /// This is a helper function...
1892 template<typename _RandomAccessIterator
, typename _Compare
>
1893 _RandomAccessIterator
1894 __unguarded_partition(_RandomAccessIterator __first
,
1895 _RandomAccessIterator __last
,
1896 _RandomAccessIterator __pivot
, _Compare __comp
)
1900 while (__comp(__first
, __pivot
))
1903 while (__comp(__pivot
, __last
))
1905 if (!(__first
< __last
))
1907 std::iter_swap(__first
, __last
);
1912 /// This is a helper function...
1913 template<typename _RandomAccessIterator
, typename _Compare
>
1914 inline _RandomAccessIterator
1915 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1916 _RandomAccessIterator __last
, _Compare __comp
)
1918 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1919 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
1920 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1923 template<typename _RandomAccessIterator
, typename _Compare
>
1925 __partial_sort(_RandomAccessIterator __first
,
1926 _RandomAccessIterator __middle
,
1927 _RandomAccessIterator __last
,
1930 std::__heap_select(__first
, __middle
, __last
, __comp
);
1931 std::__sort_heap(__first
, __middle
, __comp
);
1934 /// This is a helper function for the sort routine.
1935 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1937 __introsort_loop(_RandomAccessIterator __first
,
1938 _RandomAccessIterator __last
,
1939 _Size __depth_limit
, _Compare __comp
)
1941 while (__last
- __first
> int(_S_threshold
))
1943 if (__depth_limit
== 0)
1945 std::__partial_sort(__first
, __last
, __last
, __comp
);
1949 _RandomAccessIterator __cut
=
1950 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1951 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1958 template<typename _RandomAccessIterator
, typename _Compare
>
1960 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1963 if (__first
!= __last
)
1965 std::__introsort_loop(__first
, __last
,
1966 std::__lg(__last
- __first
) * 2,
1968 std::__final_insertion_sort(__first
, __last
, __comp
);
1972 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1974 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1975 _RandomAccessIterator __last
, _Size __depth_limit
,
1978 while (__last
- __first
> 3)
1980 if (__depth_limit
== 0)
1982 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1983 // Place the nth largest element in its final position.
1984 std::iter_swap(__first
, __nth
);
1988 _RandomAccessIterator __cut
=
1989 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1995 std::__insertion_sort(__first
, __last
, __comp
);
2000 // lower_bound moved to stl_algobase.h
2003 * @brief Finds the first position in which @p __val could be inserted
2004 * without changing the ordering.
2005 * @ingroup binary_search_algorithms
2006 * @param __first An iterator.
2007 * @param __last Another iterator.
2008 * @param __val The search term.
2009 * @param __comp A functor to use for comparisons.
2010 * @return An iterator pointing to the first element <em>not less
2011 * than</em> @p __val, or end() if every element is less
2013 * @ingroup binary_search_algorithms
2015 * The comparison function should have the same effects on ordering as
2016 * the function used for the initial sort.
2018 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2020 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2021 const _Tp
& __val
, _Compare __comp
)
2023 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2026 // concept requirements
2027 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2028 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2030 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2033 return std::__lower_bound(__first
, __last
, __val
,
2034 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2037 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2039 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2040 const _Tp
& __val
, _Compare __comp
)
2042 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2045 _DistanceType __len
= std::distance(__first
, __last
);
2049 _DistanceType __half
= __len
>> 1;
2050 _ForwardIterator __middle
= __first
;
2051 std::advance(__middle
, __half
);
2052 if (__comp(__val
, __middle
))
2058 __len
= __len
- __half
- 1;
2065 * @brief Finds the last position in which @p __val could be inserted
2066 * without changing the ordering.
2067 * @ingroup binary_search_algorithms
2068 * @param __first An iterator.
2069 * @param __last Another iterator.
2070 * @param __val The search term.
2071 * @return An iterator pointing to the first element greater than @p __val,
2072 * or end() if no elements are greater than @p __val.
2073 * @ingroup binary_search_algorithms
2075 template<typename _ForwardIterator
, typename _Tp
>
2077 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2080 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2083 // concept requirements
2084 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2085 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2086 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2088 return std::__upper_bound(__first
, __last
, __val
,
2089 __gnu_cxx::__ops::__val_less_iter());
2093 * @brief Finds the last position in which @p __val could be inserted
2094 * without changing the ordering.
2095 * @ingroup binary_search_algorithms
2096 * @param __first An iterator.
2097 * @param __last Another iterator.
2098 * @param __val The search term.
2099 * @param __comp A functor to use for comparisons.
2100 * @return An iterator pointing to the first element greater than @p __val,
2101 * or end() if no elements are greater than @p __val.
2102 * @ingroup binary_search_algorithms
2104 * The comparison function should have the same effects on ordering as
2105 * the function used for the initial sort.
2107 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2109 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2110 const _Tp
& __val
, _Compare __comp
)
2112 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2115 // concept requirements
2116 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2117 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2119 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2122 return std::__upper_bound(__first
, __last
, __val
,
2123 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2126 template<typename _ForwardIterator
, typename _Tp
,
2127 typename _CompareItTp
, typename _CompareTpIt
>
2128 pair
<_ForwardIterator
, _ForwardIterator
>
2129 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2131 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2133 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2136 _DistanceType __len
= std::distance(__first
, __last
);
2140 _DistanceType __half
= __len
>> 1;
2141 _ForwardIterator __middle
= __first
;
2142 std::advance(__middle
, __half
);
2143 if (__comp_it_val(__middle
, __val
))
2147 __len
= __len
- __half
- 1;
2149 else if (__comp_val_it(__val
, __middle
))
2153 _ForwardIterator __left
2154 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2155 std::advance(__first
, __len
);
2156 _ForwardIterator __right
2157 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2158 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2161 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2165 * @brief Finds the largest subrange in which @p __val could be inserted
2166 * at any place in it without changing the ordering.
2167 * @ingroup binary_search_algorithms
2168 * @param __first An iterator.
2169 * @param __last Another iterator.
2170 * @param __val The search term.
2171 * @return An pair of iterators defining the subrange.
2172 * @ingroup binary_search_algorithms
2174 * This is equivalent to
2176 * std::make_pair(lower_bound(__first, __last, __val),
2177 * upper_bound(__first, __last, __val))
2179 * but does not actually call those functions.
2181 template<typename _ForwardIterator
, typename _Tp
>
2182 pair
<_ForwardIterator
, _ForwardIterator
>
2183 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2186 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2189 // concept requirements
2190 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2191 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2192 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2193 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2194 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2196 return std::__equal_range(__first
, __last
, __val
,
2197 __gnu_cxx::__ops::__iter_less_val(),
2198 __gnu_cxx::__ops::__val_less_iter());
2202 * @brief Finds the largest subrange in which @p __val could be inserted
2203 * at any place in it without changing the ordering.
2204 * @param __first An iterator.
2205 * @param __last Another iterator.
2206 * @param __val The search term.
2207 * @param __comp A functor to use for comparisons.
2208 * @return An pair of iterators defining the subrange.
2209 * @ingroup binary_search_algorithms
2211 * This is equivalent to
2213 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2214 * upper_bound(__first, __last, __val, __comp))
2216 * but does not actually call those functions.
2218 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2219 pair
<_ForwardIterator
, _ForwardIterator
>
2220 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2221 const _Tp
& __val
, _Compare __comp
)
2223 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2226 // concept requirements
2227 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2228 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2230 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2232 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2234 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2237 return std::__equal_range(__first
, __last
, __val
,
2238 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2239 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2243 * @brief Determines whether an element exists in a range.
2244 * @ingroup binary_search_algorithms
2245 * @param __first An iterator.
2246 * @param __last Another iterator.
2247 * @param __val The search term.
2248 * @return True if @p __val (or its equivalent) is in [@p
2249 * __first,@p __last ].
2251 * Note that this does not actually return an iterator to @p __val. For
2252 * that, use std::find or a container's specialized find member functions.
2254 template<typename _ForwardIterator
, typename _Tp
>
2256 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2259 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2262 // concept requirements
2263 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2264 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2265 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2266 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2268 _ForwardIterator __i
2269 = std::__lower_bound(__first
, __last
, __val
,
2270 __gnu_cxx::__ops::__iter_less_val());
2271 return __i
!= __last
&& !(__val
< *__i
);
2275 * @brief Determines whether an element exists in a range.
2276 * @ingroup binary_search_algorithms
2277 * @param __first An iterator.
2278 * @param __last Another iterator.
2279 * @param __val The search term.
2280 * @param __comp A functor to use for comparisons.
2281 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2283 * Note that this does not actually return an iterator to @p __val. For
2284 * that, use std::find or a container's specialized find member functions.
2286 * The comparison function should have the same effects on ordering as
2287 * the function used for the initial sort.
2289 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2291 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2292 const _Tp
& __val
, _Compare __comp
)
2294 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2297 // concept requirements
2298 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2299 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2301 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2303 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2306 _ForwardIterator __i
2307 = std::__lower_bound(__first
, __last
, __val
,
2308 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2309 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2314 /// This is a helper function for the __merge_adaptive routines.
2315 template<typename _InputIterator1
, typename _InputIterator2
,
2316 typename _OutputIterator
, typename _Compare
>
2318 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2319 _InputIterator2 __first2
, _InputIterator2 __last2
,
2320 _OutputIterator __result
, _Compare __comp
)
2322 while (__first1
!= __last1
&& __first2
!= __last2
)
2324 if (__comp(__first2
, __first1
))
2326 *__result
= _GLIBCXX_MOVE(*__first2
);
2331 *__result
= _GLIBCXX_MOVE(*__first1
);
2336 if (__first1
!= __last1
)
2337 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2340 /// This is a helper function for the __merge_adaptive routines.
2341 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2342 typename _BidirectionalIterator3
, typename _Compare
>
2344 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2345 _BidirectionalIterator1 __last1
,
2346 _BidirectionalIterator2 __first2
,
2347 _BidirectionalIterator2 __last2
,
2348 _BidirectionalIterator3 __result
,
2351 if (__first1
== __last1
)
2353 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2356 else if (__first2
== __last2
)
2363 if (__comp(__last2
, __last1
))
2365 *--__result
= _GLIBCXX_MOVE(*__last1
);
2366 if (__first1
== __last1
)
2368 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2375 *--__result
= _GLIBCXX_MOVE(*__last2
);
2376 if (__first2
== __last2
)
2383 /// This is a helper function for the merge routines.
2384 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2386 _BidirectionalIterator1
2387 __rotate_adaptive(_BidirectionalIterator1 __first
,
2388 _BidirectionalIterator1 __middle
,
2389 _BidirectionalIterator1 __last
,
2390 _Distance __len1
, _Distance __len2
,
2391 _BidirectionalIterator2 __buffer
,
2392 _Distance __buffer_size
)
2394 _BidirectionalIterator2 __buffer_end
;
2395 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2399 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2400 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2401 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2406 else if (__len1
<= __buffer_size
)
2410 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2411 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2412 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2419 std::rotate(__first
, __middle
, __last
);
2420 std::advance(__first
, std::distance(__middle
, __last
));
2425 /// This is a helper function for the merge routines.
2426 template<typename _BidirectionalIterator
, typename _Distance
,
2427 typename _Pointer
, typename _Compare
>
2429 __merge_adaptive(_BidirectionalIterator __first
,
2430 _BidirectionalIterator __middle
,
2431 _BidirectionalIterator __last
,
2432 _Distance __len1
, _Distance __len2
,
2433 _Pointer __buffer
, _Distance __buffer_size
,
2436 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2438 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2439 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2442 else if (__len2
<= __buffer_size
)
2444 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2445 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2446 __buffer_end
, __last
, __comp
);
2450 _BidirectionalIterator __first_cut
= __first
;
2451 _BidirectionalIterator __second_cut
= __middle
;
2452 _Distance __len11
= 0;
2453 _Distance __len22
= 0;
2454 if (__len1
> __len2
)
2456 __len11
= __len1
/ 2;
2457 std::advance(__first_cut
, __len11
);
2459 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2460 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2461 __len22
= std::distance(__middle
, __second_cut
);
2465 __len22
= __len2
/ 2;
2466 std::advance(__second_cut
, __len22
);
2468 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2469 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2470 __len11
= std::distance(__first
, __first_cut
);
2472 _BidirectionalIterator __new_middle
2473 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2474 __len1
- __len11
, __len22
, __buffer
,
2476 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2477 __len22
, __buffer
, __buffer_size
, __comp
);
2478 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2480 __len2
- __len22
, __buffer
,
2481 __buffer_size
, __comp
);
2485 /// This is a helper function for the merge routines.
2486 template<typename _BidirectionalIterator
, typename _Distance
,
2489 __merge_without_buffer(_BidirectionalIterator __first
,
2490 _BidirectionalIterator __middle
,
2491 _BidirectionalIterator __last
,
2492 _Distance __len1
, _Distance __len2
,
2495 if (__len1
== 0 || __len2
== 0)
2497 if (__len1
+ __len2
== 2)
2499 if (__comp(__middle
, __first
))
2500 std::iter_swap(__first
, __middle
);
2503 _BidirectionalIterator __first_cut
= __first
;
2504 _BidirectionalIterator __second_cut
= __middle
;
2505 _Distance __len11
= 0;
2506 _Distance __len22
= 0;
2507 if (__len1
> __len2
)
2509 __len11
= __len1
/ 2;
2510 std::advance(__first_cut
, __len11
);
2512 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2513 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2514 __len22
= std::distance(__middle
, __second_cut
);
2518 __len22
= __len2
/ 2;
2519 std::advance(__second_cut
, __len22
);
2521 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2522 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2523 __len11
= std::distance(__first
, __first_cut
);
2525 std::rotate(__first_cut
, __middle
, __second_cut
);
2526 _BidirectionalIterator __new_middle
= __first_cut
;
2527 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2528 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2529 __len11
, __len22
, __comp
);
2530 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2531 __len1
- __len11
, __len2
- __len22
, __comp
);
2534 template<typename _BidirectionalIterator
, typename _Compare
>
2536 __inplace_merge(_BidirectionalIterator __first
,
2537 _BidirectionalIterator __middle
,
2538 _BidirectionalIterator __last
,
2541 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2543 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2546 if (__first
== __middle
|| __middle
== __last
)
2549 const _DistanceType __len1
= std::distance(__first
, __middle
);
2550 const _DistanceType __len2
= std::distance(__middle
, __last
);
2552 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2553 _TmpBuf
__buf(__first
, __last
);
2555 if (__buf
.begin() == 0)
2556 std::__merge_without_buffer
2557 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2559 std::__merge_adaptive
2560 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2561 _DistanceType(__buf
.size()), __comp
);
2565 * @brief Merges two sorted ranges in place.
2566 * @ingroup sorting_algorithms
2567 * @param __first An iterator.
2568 * @param __middle Another iterator.
2569 * @param __last Another iterator.
2572 * Merges two sorted and consecutive ranges, [__first,__middle) and
2573 * [__middle,__last), and puts the result in [__first,__last). The
2574 * output will be sorted. The sort is @e stable, that is, for
2575 * equivalent elements in the two ranges, elements from the first
2576 * range will always come before elements from the second.
2578 * If enough additional memory is available, this takes (__last-__first)-1
2579 * comparisons. Otherwise an NlogN algorithm is used, where N is
2580 * distance(__first,__last).
2582 template<typename _BidirectionalIterator
>
2584 inplace_merge(_BidirectionalIterator __first
,
2585 _BidirectionalIterator __middle
,
2586 _BidirectionalIterator __last
)
2588 // concept requirements
2589 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2590 _BidirectionalIterator
>)
2591 __glibcxx_function_requires(_LessThanComparableConcept
<
2592 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2593 __glibcxx_requires_sorted(__first
, __middle
);
2594 __glibcxx_requires_sorted(__middle
, __last
);
2596 std::__inplace_merge(__first
, __middle
, __last
,
2597 __gnu_cxx::__ops::__iter_less_iter());
2601 * @brief Merges two sorted ranges in place.
2602 * @ingroup sorting_algorithms
2603 * @param __first An iterator.
2604 * @param __middle Another iterator.
2605 * @param __last Another iterator.
2606 * @param __comp A functor to use for comparisons.
2609 * Merges two sorted and consecutive ranges, [__first,__middle) and
2610 * [middle,last), and puts the result in [__first,__last). The output will
2611 * be sorted. The sort is @e stable, that is, for equivalent
2612 * elements in the two ranges, elements from the first range will always
2613 * come before elements from the second.
2615 * If enough additional memory is available, this takes (__last-__first)-1
2616 * comparisons. Otherwise an NlogN algorithm is used, where N is
2617 * distance(__first,__last).
2619 * The comparison function should have the same effects on ordering as
2620 * the function used for the initial sort.
2622 template<typename _BidirectionalIterator
, typename _Compare
>
2624 inplace_merge(_BidirectionalIterator __first
,
2625 _BidirectionalIterator __middle
,
2626 _BidirectionalIterator __last
,
2629 // concept requirements
2630 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2631 _BidirectionalIterator
>)
2632 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2633 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2634 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2635 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2636 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2638 std::__inplace_merge(__first
, __middle
, __last
,
2639 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2643 /// This is a helper function for the __merge_sort_loop routines.
2644 template<typename _InputIterator
, typename _OutputIterator
,
2647 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2648 _InputIterator __first2
, _InputIterator __last2
,
2649 _OutputIterator __result
, _Compare __comp
)
2651 while (__first1
!= __last1
&& __first2
!= __last2
)
2653 if (__comp(__first2
, __first1
))
2655 *__result
= _GLIBCXX_MOVE(*__first2
);
2660 *__result
= _GLIBCXX_MOVE(*__first1
);
2665 return _GLIBCXX_MOVE3(__first2
, __last2
,
2666 _GLIBCXX_MOVE3(__first1
, __last1
,
2670 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2671 typename _Distance
, typename _Compare
>
2673 __merge_sort_loop(_RandomAccessIterator1 __first
,
2674 _RandomAccessIterator1 __last
,
2675 _RandomAccessIterator2 __result
, _Distance __step_size
,
2678 const _Distance __two_step
= 2 * __step_size
;
2680 while (__last
- __first
>= __two_step
)
2682 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2683 __first
+ __step_size
,
2684 __first
+ __two_step
,
2686 __first
+= __two_step
;
2688 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2690 std::__move_merge(__first
, __first
+ __step_size
,
2691 __first
+ __step_size
, __last
, __result
, __comp
);
2694 template<typename _RandomAccessIterator
, typename _Distance
,
2697 __chunk_insertion_sort(_RandomAccessIterator __first
,
2698 _RandomAccessIterator __last
,
2699 _Distance __chunk_size
, _Compare __comp
)
2701 while (__last
- __first
>= __chunk_size
)
2703 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2704 __first
+= __chunk_size
;
2706 std::__insertion_sort(__first
, __last
, __comp
);
2709 enum { _S_chunk_size
= 7 };
2711 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2713 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2714 _RandomAccessIterator __last
,
2715 _Pointer __buffer
, _Compare __comp
)
2717 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2720 const _Distance __len
= __last
- __first
;
2721 const _Pointer __buffer_last
= __buffer
+ __len
;
2723 _Distance __step_size
= _S_chunk_size
;
2724 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2726 while (__step_size
< __len
)
2728 std::__merge_sort_loop(__first
, __last
, __buffer
,
2729 __step_size
, __comp
);
2731 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2732 __step_size
, __comp
);
2737 template<typename _RandomAccessIterator
, typename _Pointer
,
2738 typename _Distance
, typename _Compare
>
2740 __stable_sort_adaptive(_RandomAccessIterator __first
,
2741 _RandomAccessIterator __last
,
2742 _Pointer __buffer
, _Distance __buffer_size
,
2745 const _Distance __len
= (__last
- __first
+ 1) / 2;
2746 const _RandomAccessIterator __middle
= __first
+ __len
;
2747 if (__len
> __buffer_size
)
2749 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2750 __buffer_size
, __comp
);
2751 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2752 __buffer_size
, __comp
);
2756 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2757 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2759 std::__merge_adaptive(__first
, __middle
, __last
,
2760 _Distance(__middle
- __first
),
2761 _Distance(__last
- __middle
),
2762 __buffer
, __buffer_size
,
2766 /// This is a helper function for the stable sorting routines.
2767 template<typename _RandomAccessIterator
, typename _Compare
>
2769 __inplace_stable_sort(_RandomAccessIterator __first
,
2770 _RandomAccessIterator __last
, _Compare __comp
)
2772 if (__last
- __first
< 15)
2774 std::__insertion_sort(__first
, __last
, __comp
);
2777 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2778 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2779 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2780 std::__merge_without_buffer(__first
, __middle
, __last
,
2788 // Set algorithms: includes, set_union, set_intersection, set_difference,
2789 // set_symmetric_difference. All of these algorithms have the precondition
2790 // that their input ranges are sorted and the postcondition that their output
2791 // ranges are sorted.
2793 template<typename _InputIterator1
, typename _InputIterator2
,
2796 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2797 _InputIterator2 __first2
, _InputIterator2 __last2
,
2800 while (__first1
!= __last1
&& __first2
!= __last2
)
2801 if (__comp(__first2
, __first1
))
2803 else if (__comp(__first1
, __first2
))
2806 ++__first1
, ++__first2
;
2808 return __first2
== __last2
;
2812 * @brief Determines whether all elements of a sequence exists in a range.
2813 * @param __first1 Start of search range.
2814 * @param __last1 End of search range.
2815 * @param __first2 Start of sequence
2816 * @param __last2 End of sequence.
2817 * @return True if each element in [__first2,__last2) is contained in order
2818 * within [__first1,__last1). False otherwise.
2819 * @ingroup set_algorithms
2821 * This operation expects both [__first1,__last1) and
2822 * [__first2,__last2) to be sorted. Searches for the presence of
2823 * each element in [__first2,__last2) within [__first1,__last1).
2824 * The iterators over each range only move forward, so this is a
2825 * linear algorithm. If an element in [__first2,__last2) is not
2826 * found before the search iterator reaches @p __last2, false is
2829 template<typename _InputIterator1
, typename _InputIterator2
>
2831 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2832 _InputIterator2 __first2
, _InputIterator2 __last2
)
2834 // concept requirements
2835 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2836 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2837 __glibcxx_function_requires(_LessThanOpConcept
<
2838 typename iterator_traits
<_InputIterator1
>::value_type
,
2839 typename iterator_traits
<_InputIterator2
>::value_type
>)
2840 __glibcxx_function_requires(_LessThanOpConcept
<
2841 typename iterator_traits
<_InputIterator2
>::value_type
,
2842 typename iterator_traits
<_InputIterator1
>::value_type
>)
2843 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2844 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2846 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2847 __gnu_cxx::__ops::__iter_less_iter());
2851 * @brief Determines whether all elements of a sequence exists in a range
2853 * @ingroup set_algorithms
2854 * @param __first1 Start of search range.
2855 * @param __last1 End of search range.
2856 * @param __first2 Start of sequence
2857 * @param __last2 End of sequence.
2858 * @param __comp Comparison function to use.
2859 * @return True if each element in [__first2,__last2) is contained
2860 * in order within [__first1,__last1) according to comp. False
2861 * otherwise. @ingroup set_algorithms
2863 * This operation expects both [__first1,__last1) and
2864 * [__first2,__last2) to be sorted. Searches for the presence of
2865 * each element in [__first2,__last2) within [__first1,__last1),
2866 * using comp to decide. The iterators over each range only move
2867 * forward, so this is a linear algorithm. If an element in
2868 * [__first2,__last2) is not found before the search iterator
2869 * reaches @p __last2, false is returned.
2871 template<typename _InputIterator1
, typename _InputIterator2
,
2874 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2875 _InputIterator2 __first2
, _InputIterator2 __last2
,
2878 // concept requirements
2879 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2880 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2881 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2882 typename iterator_traits
<_InputIterator1
>::value_type
,
2883 typename iterator_traits
<_InputIterator2
>::value_type
>)
2884 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2885 typename iterator_traits
<_InputIterator2
>::value_type
,
2886 typename iterator_traits
<_InputIterator1
>::value_type
>)
2887 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2888 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2890 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2891 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2900 // set_symmetric_difference
2904 template<typename _BidirectionalIterator
, typename _Compare
>
2906 __next_permutation(_BidirectionalIterator __first
,
2907 _BidirectionalIterator __last
, _Compare __comp
)
2909 if (__first
== __last
)
2911 _BidirectionalIterator __i
= __first
;
2920 _BidirectionalIterator __ii
= __i
;
2922 if (__comp(__i
, __ii
))
2924 _BidirectionalIterator __j
= __last
;
2925 while (!__comp(__i
, --__j
))
2927 std::iter_swap(__i
, __j
);
2928 std::__reverse(__ii
, __last
,
2929 std::__iterator_category(__first
));
2934 std::__reverse(__first
, __last
,
2935 std::__iterator_category(__first
));
2942 * @brief Permute range into the next @e dictionary ordering.
2943 * @ingroup sorting_algorithms
2944 * @param __first Start of range.
2945 * @param __last End of range.
2946 * @return False if wrapped to first permutation, true otherwise.
2948 * Treats all permutations of the range as a set of @e dictionary sorted
2949 * sequences. Permutes the current sequence into the next one of this set.
2950 * Returns true if there are more sequences to generate. If the sequence
2951 * is the largest of the set, the smallest is generated and false returned.
2953 template<typename _BidirectionalIterator
>
2955 next_permutation(_BidirectionalIterator __first
,
2956 _BidirectionalIterator __last
)
2958 // concept requirements
2959 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2960 _BidirectionalIterator
>)
2961 __glibcxx_function_requires(_LessThanComparableConcept
<
2962 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2963 __glibcxx_requires_valid_range(__first
, __last
);
2965 return std::__next_permutation
2966 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2970 * @brief Permute range into the next @e dictionary ordering using
2971 * comparison functor.
2972 * @ingroup sorting_algorithms
2973 * @param __first Start of range.
2974 * @param __last End of range.
2975 * @param __comp A comparison functor.
2976 * @return False if wrapped to first permutation, true otherwise.
2978 * Treats all permutations of the range [__first,__last) as a set of
2979 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2980 * sequence into the next one of this set. Returns true if there are more
2981 * sequences to generate. If the sequence is the largest of the set, the
2982 * smallest is generated and false returned.
2984 template<typename _BidirectionalIterator
, typename _Compare
>
2986 next_permutation(_BidirectionalIterator __first
,
2987 _BidirectionalIterator __last
, _Compare __comp
)
2989 // concept requirements
2990 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2991 _BidirectionalIterator
>)
2992 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2993 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2994 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2995 __glibcxx_requires_valid_range(__first
, __last
);
2997 return std::__next_permutation
2998 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3001 template<typename _BidirectionalIterator
, typename _Compare
>
3003 __prev_permutation(_BidirectionalIterator __first
,
3004 _BidirectionalIterator __last
, _Compare __comp
)
3006 if (__first
== __last
)
3008 _BidirectionalIterator __i
= __first
;
3017 _BidirectionalIterator __ii
= __i
;
3019 if (__comp(__ii
, __i
))
3021 _BidirectionalIterator __j
= __last
;
3022 while (!__comp(--__j
, __i
))
3024 std::iter_swap(__i
, __j
);
3025 std::__reverse(__ii
, __last
,
3026 std::__iterator_category(__first
));
3031 std::__reverse(__first
, __last
,
3032 std::__iterator_category(__first
));
3039 * @brief Permute range into the previous @e dictionary ordering.
3040 * @ingroup sorting_algorithms
3041 * @param __first Start of range.
3042 * @param __last End of range.
3043 * @return False if wrapped to last permutation, true otherwise.
3045 * Treats all permutations of the range as a set of @e dictionary sorted
3046 * sequences. Permutes the current sequence into the previous one of this
3047 * set. Returns true if there are more sequences to generate. If the
3048 * sequence is the smallest of the set, the largest is generated and false
3051 template<typename _BidirectionalIterator
>
3053 prev_permutation(_BidirectionalIterator __first
,
3054 _BidirectionalIterator __last
)
3056 // concept requirements
3057 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3058 _BidirectionalIterator
>)
3059 __glibcxx_function_requires(_LessThanComparableConcept
<
3060 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3061 __glibcxx_requires_valid_range(__first
, __last
);
3063 return std::__prev_permutation(__first
, __last
,
3064 __gnu_cxx::__ops::__iter_less_iter());
3068 * @brief Permute range into the previous @e dictionary ordering using
3069 * comparison functor.
3070 * @ingroup sorting_algorithms
3071 * @param __first Start of range.
3072 * @param __last End of range.
3073 * @param __comp A comparison functor.
3074 * @return False if wrapped to last permutation, true otherwise.
3076 * Treats all permutations of the range [__first,__last) as a set of
3077 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3078 * sequence into the previous one of this set. Returns true if there are
3079 * more sequences to generate. If the sequence is the smallest of the set,
3080 * the largest is generated and false returned.
3082 template<typename _BidirectionalIterator
, typename _Compare
>
3084 prev_permutation(_BidirectionalIterator __first
,
3085 _BidirectionalIterator __last
, _Compare __comp
)
3087 // concept requirements
3088 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3089 _BidirectionalIterator
>)
3090 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3091 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3092 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3093 __glibcxx_requires_valid_range(__first
, __last
);
3095 return std::__prev_permutation(__first
, __last
,
3096 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3102 template<typename _InputIterator
, typename _OutputIterator
,
3103 typename _Predicate
, typename _Tp
>
3105 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3106 _OutputIterator __result
,
3107 _Predicate __pred
, const _Tp
& __new_value
)
3109 for (; __first
!= __last
; ++__first
, ++__result
)
3110 if (__pred(__first
))
3111 *__result
= __new_value
;
3113 *__result
= *__first
;
3118 * @brief Copy a sequence, replacing each element of one value with another
3120 * @param __first An input iterator.
3121 * @param __last An input iterator.
3122 * @param __result An output iterator.
3123 * @param __old_value The value to be replaced.
3124 * @param __new_value The replacement value.
3125 * @return The end of the output sequence, @p result+(last-first).
3127 * Copies each element in the input range @p [__first,__last) to the
3128 * output range @p [__result,__result+(__last-__first)) replacing elements
3129 * equal to @p __old_value with @p __new_value.
3131 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3133 replace_copy(_InputIterator __first
, _InputIterator __last
,
3134 _OutputIterator __result
,
3135 const _Tp
& __old_value
, const _Tp
& __new_value
)
3137 // concept requirements
3138 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3139 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3140 typename iterator_traits
<_InputIterator
>::value_type
>)
3141 __glibcxx_function_requires(_EqualOpConcept
<
3142 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3143 __glibcxx_requires_valid_range(__first
, __last
);
3145 return std::__replace_copy_if(__first
, __last
, __result
,
3146 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3151 * @brief Copy a sequence, replacing each value for which a predicate
3152 * returns true with another value.
3153 * @ingroup mutating_algorithms
3154 * @param __first An input iterator.
3155 * @param __last An input iterator.
3156 * @param __result An output iterator.
3157 * @param __pred A predicate.
3158 * @param __new_value The replacement value.
3159 * @return The end of the output sequence, @p __result+(__last-__first).
3161 * Copies each element in the range @p [__first,__last) to the range
3162 * @p [__result,__result+(__last-__first)) replacing elements for which
3163 * @p __pred returns true with @p __new_value.
3165 template<typename _InputIterator
, typename _OutputIterator
,
3166 typename _Predicate
, typename _Tp
>
3168 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3169 _OutputIterator __result
,
3170 _Predicate __pred
, const _Tp
& __new_value
)
3172 // concept requirements
3173 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3174 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3175 typename iterator_traits
<_InputIterator
>::value_type
>)
3176 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3177 typename iterator_traits
<_InputIterator
>::value_type
>)
3178 __glibcxx_requires_valid_range(__first
, __last
);
3180 return std::__replace_copy_if(__first
, __last
, __result
,
3181 __gnu_cxx::__ops::__pred_iter(__pred
),
3185 template<typename _InputIterator
, typename _Predicate
>
3186 typename iterator_traits
<_InputIterator
>::difference_type
3187 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3189 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3190 for (; __first
!= __last
; ++__first
)
3191 if (__pred(__first
))
3196 #if __cplusplus >= 201103L
3198 * @brief Determines whether the elements of a sequence are sorted.
3199 * @ingroup sorting_algorithms
3200 * @param __first An iterator.
3201 * @param __last Another iterator.
3202 * @return True if the elements are sorted, false otherwise.
3204 template<typename _ForwardIterator
>
3206 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3207 { return std::is_sorted_until(__first
, __last
) == __last
; }
3210 * @brief Determines whether the elements of a sequence are sorted
3211 * according to a comparison functor.
3212 * @ingroup sorting_algorithms
3213 * @param __first An iterator.
3214 * @param __last Another iterator.
3215 * @param __comp A comparison functor.
3216 * @return True if the elements are sorted, false otherwise.
3218 template<typename _ForwardIterator
, typename _Compare
>
3220 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3222 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3224 template<typename _ForwardIterator
, typename _Compare
>
3226 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3229 if (__first
== __last
)
3232 _ForwardIterator __next
= __first
;
3233 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3234 if (__comp(__next
, __first
))
3240 * @brief Determines the end of a sorted sequence.
3241 * @ingroup sorting_algorithms
3242 * @param __first An iterator.
3243 * @param __last Another iterator.
3244 * @return An iterator pointing to the last iterator i in [__first, __last)
3245 * for which the range [__first, i) is sorted.
3247 template<typename _ForwardIterator
>
3249 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3251 // concept requirements
3252 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3253 __glibcxx_function_requires(_LessThanComparableConcept
<
3254 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3255 __glibcxx_requires_valid_range(__first
, __last
);
3257 return std::__is_sorted_until(__first
, __last
,
3258 __gnu_cxx::__ops::__iter_less_iter());
3262 * @brief Determines the end of a sorted sequence using comparison functor.
3263 * @ingroup sorting_algorithms
3264 * @param __first An iterator.
3265 * @param __last Another iterator.
3266 * @param __comp A comparison functor.
3267 * @return An iterator pointing to the last iterator i in [__first, __last)
3268 * for which the range [__first, i) is sorted.
3270 template<typename _ForwardIterator
, typename _Compare
>
3272 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3275 // concept requirements
3276 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3277 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3278 typename iterator_traits
<_ForwardIterator
>::value_type
,
3279 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3280 __glibcxx_requires_valid_range(__first
, __last
);
3282 return std::__is_sorted_until(__first
, __last
,
3283 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3287 * @brief Determines min and max at once as an ordered pair.
3288 * @ingroup sorting_algorithms
3289 * @param __a A thing of arbitrary type.
3290 * @param __b Another thing of arbitrary type.
3291 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3294 template<typename _Tp
>
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 inline pair
<const _Tp
&, const _Tp
&>
3316 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3318 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3319 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3322 template<typename _ForwardIterator
, typename _Compare
>
3323 pair
<_ForwardIterator
, _ForwardIterator
>
3324 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3327 _ForwardIterator __next
= __first
;
3328 if (__first
== __last
3329 || ++__next
== __last
)
3330 return std::make_pair(__first
, __first
);
3332 _ForwardIterator __min
, __max
;
3333 if (__comp(__next
, __first
))
3347 while (__first
!= __last
)
3350 if (++__next
== __last
)
3352 if (__comp(__first
, __min
))
3354 else if (!__comp(__first
, __max
))
3359 if (__comp(__next
, __first
))
3361 if (__comp(__next
, __min
))
3363 if (!__comp(__first
, __max
))
3368 if (__comp(__first
, __min
))
3370 if (!__comp(__next
, __max
))
3378 return std::make_pair(__min
, __max
);
3382 * @brief Return a pair of iterators pointing to the minimum and maximum
3383 * elements in a range.
3384 * @ingroup sorting_algorithms
3385 * @param __first Start of range.
3386 * @param __last End of range.
3387 * @return make_pair(m, M), where m is the first iterator i in
3388 * [__first, __last) such that no other element in the range is
3389 * smaller, and where M is the last iterator i in [__first, __last)
3390 * such that no other element in the range is larger.
3392 template<typename _ForwardIterator
>
3393 pair
<_ForwardIterator
, _ForwardIterator
>
3394 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3396 // concept requirements
3397 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3398 __glibcxx_function_requires(_LessThanComparableConcept
<
3399 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3400 __glibcxx_requires_valid_range(__first
, __last
);
3402 return std::__minmax_element(__first
, __last
,
3403 __gnu_cxx::__ops::__iter_less_iter());
3407 * @brief Return a pair of iterators pointing to the minimum and maximum
3408 * elements in a range.
3409 * @ingroup sorting_algorithms
3410 * @param __first Start of range.
3411 * @param __last End of range.
3412 * @param __comp Comparison functor.
3413 * @return make_pair(m, M), where m is the first iterator i in
3414 * [__first, __last) such that no other element in the range is
3415 * smaller, and where M is the last iterator i in [__first, __last)
3416 * such that no other element in the range is larger.
3418 template<typename _ForwardIterator
, typename _Compare
>
3419 pair
<_ForwardIterator
, _ForwardIterator
>
3420 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3423 // concept requirements
3424 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3425 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3426 typename iterator_traits
<_ForwardIterator
>::value_type
,
3427 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3428 __glibcxx_requires_valid_range(__first
, __last
);
3430 return std::__minmax_element(__first
, __last
,
3431 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3435 template<typename _Tp
>
3437 min(initializer_list
<_Tp
> __l
)
3438 { return *std::min_element(__l
.begin(), __l
.end()); }
3440 template<typename _Tp
, typename _Compare
>
3442 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3443 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3445 template<typename _Tp
>
3447 max(initializer_list
<_Tp
> __l
)
3448 { return *std::max_element(__l
.begin(), __l
.end()); }
3450 template<typename _Tp
, typename _Compare
>
3452 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3453 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3455 template<typename _Tp
>
3456 inline pair
<_Tp
, _Tp
>
3457 minmax(initializer_list
<_Tp
> __l
)
3459 pair
<const _Tp
*, const _Tp
*> __p
=
3460 std::minmax_element(__l
.begin(), __l
.end());
3461 return std::make_pair(*__p
.first
, *__p
.second
);
3464 template<typename _Tp
, typename _Compare
>
3465 inline pair
<_Tp
, _Tp
>
3466 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3468 pair
<const _Tp
*, const _Tp
*> __p
=
3469 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3470 return std::make_pair(*__p
.first
, *__p
.second
);
3473 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3474 typename _BinaryPredicate
>
3476 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3477 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3479 // Efficiently compare identical prefixes: O(N) if sequences
3480 // have the same elements in the same order.
3481 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
3482 if (!__pred(__first1
, __first2
))
3485 if (__first1
== __last1
)
3488 // Establish __last2 assuming equal ranges by iterating over the
3489 // rest of the list.
3490 _ForwardIterator2 __last2
= __first2
;
3491 std::advance(__last2
, std::distance(__first1
, __last1
));
3492 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3494 if (__scan
!= std::__find_if(__first1
, __scan
,
3495 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3496 continue; // We've seen this one before.
3499 = std::__count_if(__first2
, __last2
,
3500 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3501 if (0 == __matches
||
3502 std::__count_if(__scan
, __last1
,
3503 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3511 * @brief Checks whether a permutation of the second sequence is equal
3512 * to the first sequence.
3513 * @ingroup non_mutating_algorithms
3514 * @param __first1 Start of first range.
3515 * @param __last1 End of first range.
3516 * @param __first2 Start of second range.
3517 * @return true if there exists a permutation of the elements in the range
3518 * [__first2, __first2 + (__last1 - __first1)), beginning with
3519 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3520 * returns true; otherwise, returns false.
3522 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3524 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3525 _ForwardIterator2 __first2
)
3527 // concept requirements
3528 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3529 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3530 __glibcxx_function_requires(_EqualOpConcept
<
3531 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3532 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3533 __glibcxx_requires_valid_range(__first1
, __last1
);
3535 return std::__is_permutation(__first1
, __last1
, __first2
,
3536 __gnu_cxx::__ops::__iter_equal_to_iter());
3540 * @brief Checks whether a permutation of the second sequence is equal
3541 * to the first sequence.
3542 * @ingroup non_mutating_algorithms
3543 * @param __first1 Start of first range.
3544 * @param __last1 End of first range.
3545 * @param __first2 Start of second range.
3546 * @param __pred A binary predicate.
3547 * @return true if there exists a permutation of the elements in
3548 * the range [__first2, __first2 + (__last1 - __first1)),
3549 * beginning with ForwardIterator2 begin, such that
3550 * equal(__first1, __last1, __begin, __pred) returns true;
3551 * otherwise, returns false.
3553 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3554 typename _BinaryPredicate
>
3556 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3557 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3559 // concept requirements
3560 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3561 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3562 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3563 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3564 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3565 __glibcxx_requires_valid_range(__first1
, __last1
);
3567 return std::__is_permutation(__first1
, __last1
, __first2
,
3568 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3571 #if __cplusplus > 201103L
3572 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3573 typename _BinaryPredicate
>
3575 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3576 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3577 _BinaryPredicate __pred
)
3580 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3582 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3583 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3584 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3585 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3588 auto __d1
= std::distance(__first1
, __last1
);
3589 auto __d2
= std::distance(__first2
, __last2
);
3594 // Efficiently compare identical prefixes: O(N) if sequences
3595 // have the same elements in the same order.
3596 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
3597 if (!__pred(__first1
, __first2
))
3602 if (__first1
== __last1
)
3607 auto __d1
= std::distance(__first1
, __last1
);
3608 auto __d2
= std::distance(__first2
, __last2
);
3609 if (__d1
== 0 && __d2
== 0)
3615 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3617 if (__scan
!= std::__find_if(__first1
, __scan
,
3618 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3619 continue; // We've seen this one before.
3621 auto __matches
= std::__count_if(__first2
, __last2
,
3622 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3624 || std::__count_if(__scan
, __last1
,
3625 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3633 * @brief Checks whether a permutaion of the second sequence is equal
3634 * to the first sequence.
3635 * @ingroup non_mutating_algorithms
3636 * @param __first1 Start of first range.
3637 * @param __last1 End of first range.
3638 * @param __first2 Start of second range.
3639 * @param __last2 End of first range.
3640 * @return true if there exists a permutation of the elements in the range
3641 * [__first2, __last2), beginning with ForwardIterator2 begin,
3642 * such that equal(__first1, __last1, begin) returns true;
3643 * otherwise, returns false.
3645 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3647 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3648 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3650 __glibcxx_requires_valid_range(__first1
, __last1
);
3651 __glibcxx_requires_valid_range(__first2
, __last2
);
3654 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3655 __gnu_cxx::__ops::__iter_equal_to_iter());
3659 * @brief Checks whether a permutation of the second sequence is equal
3660 * to the first sequence.
3661 * @ingroup non_mutating_algorithms
3662 * @param __first1 Start of first range.
3663 * @param __last1 End of first range.
3664 * @param __first2 Start of second range.
3665 * @param __last2 End of first range.
3666 * @param __pred A binary predicate.
3667 * @return true if there exists a permutation of the elements in the range
3668 * [__first2, __last2), beginning with ForwardIterator2 begin,
3669 * such that equal(__first1, __last1, __begin, __pred) returns true;
3670 * otherwise, returns false.
3672 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3673 typename _BinaryPredicate
>
3675 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3676 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3677 _BinaryPredicate __pred
)
3679 __glibcxx_requires_valid_range(__first1
, __last1
);
3680 __glibcxx_requires_valid_range(__first2
, __last2
);
3682 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3683 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3687 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3689 * @brief Shuffle the elements of a sequence using a uniform random
3691 * @ingroup mutating_algorithms
3692 * @param __first A forward iterator.
3693 * @param __last A forward iterator.
3694 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3697 * Reorders the elements in the range @p [__first,__last) using @p __g to
3698 * provide random numbers.
3700 template<typename _RandomAccessIterator
,
3701 typename _UniformRandomNumberGenerator
>
3703 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3704 _UniformRandomNumberGenerator
&& __g
)
3706 // concept requirements
3707 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3708 _RandomAccessIterator
>)
3709 __glibcxx_requires_valid_range(__first
, __last
);
3711 if (__first
== __last
)
3714 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3717 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3718 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3719 typedef typename
__distr_type::param_type __p_type
;
3722 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3723 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3729 _GLIBCXX_END_NAMESPACE_VERSION
3731 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3734 * @brief Apply a function to every element of a sequence.
3735 * @ingroup non_mutating_algorithms
3736 * @param __first An input iterator.
3737 * @param __last An input iterator.
3738 * @param __f A unary function object.
3739 * @return @p __f (std::move(@p __f) in C++0x).
3741 * Applies the function object @p __f to each element in the range
3742 * @p [first,last). @p __f must not modify the order of the sequence.
3743 * If @p __f has a return value it is ignored.
3745 template<typename _InputIterator
, typename _Function
>
3747 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3749 // concept requirements
3750 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3751 __glibcxx_requires_valid_range(__first
, __last
);
3752 for (; __first
!= __last
; ++__first
)
3754 return _GLIBCXX_MOVE(__f
);
3758 * @brief Find the first occurrence of a value in a sequence.
3759 * @ingroup non_mutating_algorithms
3760 * @param __first An input iterator.
3761 * @param __last An input iterator.
3762 * @param __val The value to find.
3763 * @return The first iterator @c i in the range @p [__first,__last)
3764 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3766 template<typename _InputIterator
, typename _Tp
>
3767 inline _InputIterator
3768 find(_InputIterator __first
, _InputIterator __last
,
3771 // concept requirements
3772 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3773 __glibcxx_function_requires(_EqualOpConcept
<
3774 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3775 __glibcxx_requires_valid_range(__first
, __last
);
3776 return std::__find_if(__first
, __last
,
3777 __gnu_cxx::__ops::__iter_equals_val(__val
));
3781 * @brief Find the first element in a sequence for which a
3782 * predicate is true.
3783 * @ingroup non_mutating_algorithms
3784 * @param __first An input iterator.
3785 * @param __last An input iterator.
3786 * @param __pred A predicate.
3787 * @return The first iterator @c i in the range @p [__first,__last)
3788 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3790 template<typename _InputIterator
, typename _Predicate
>
3791 inline _InputIterator
3792 find_if(_InputIterator __first
, _InputIterator __last
,
3795 // concept requirements
3796 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3797 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3798 typename iterator_traits
<_InputIterator
>::value_type
>)
3799 __glibcxx_requires_valid_range(__first
, __last
);
3801 return std::__find_if(__first
, __last
,
3802 __gnu_cxx::__ops::__pred_iter(__pred
));
3806 * @brief Find element from a set in a sequence.
3807 * @ingroup non_mutating_algorithms
3808 * @param __first1 Start of range to search.
3809 * @param __last1 End of range to search.
3810 * @param __first2 Start of match candidates.
3811 * @param __last2 End of match candidates.
3812 * @return The first iterator @c i in the range
3813 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3814 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3816 * Searches the range @p [__first1,__last1) for an element that is
3817 * equal to some element in the range [__first2,__last2). If
3818 * found, returns an iterator in the range [__first1,__last1),
3819 * otherwise returns @p __last1.
3821 template<typename _InputIterator
, typename _ForwardIterator
>
3823 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3824 _ForwardIterator __first2
, _ForwardIterator __last2
)
3826 // concept requirements
3827 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3828 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3829 __glibcxx_function_requires(_EqualOpConcept
<
3830 typename iterator_traits
<_InputIterator
>::value_type
,
3831 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3832 __glibcxx_requires_valid_range(__first1
, __last1
);
3833 __glibcxx_requires_valid_range(__first2
, __last2
);
3835 for (; __first1
!= __last1
; ++__first1
)
3836 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3837 if (*__first1
== *__iter
)
3843 * @brief Find element from a set in a sequence using a predicate.
3844 * @ingroup non_mutating_algorithms
3845 * @param __first1 Start of range to search.
3846 * @param __last1 End of range to search.
3847 * @param __first2 Start of match candidates.
3848 * @param __last2 End of match candidates.
3849 * @param __comp Predicate to use.
3850 * @return The first iterator @c i in the range
3851 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3852 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3853 * such iterator exists.
3856 * Searches the range @p [__first1,__last1) for an element that is
3857 * equal to some element in the range [__first2,__last2). If
3858 * found, returns an iterator in the range [__first1,__last1),
3859 * otherwise returns @p __last1.
3861 template<typename _InputIterator
, typename _ForwardIterator
,
3862 typename _BinaryPredicate
>
3864 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3865 _ForwardIterator __first2
, _ForwardIterator __last2
,
3866 _BinaryPredicate __comp
)
3868 // concept requirements
3869 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3870 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3871 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3872 typename iterator_traits
<_InputIterator
>::value_type
,
3873 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3874 __glibcxx_requires_valid_range(__first1
, __last1
);
3875 __glibcxx_requires_valid_range(__first2
, __last2
);
3877 for (; __first1
!= __last1
; ++__first1
)
3878 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3879 if (__comp(*__first1
, *__iter
))
3885 * @brief Find two adjacent values in a sequence that are equal.
3886 * @ingroup non_mutating_algorithms
3887 * @param __first A forward iterator.
3888 * @param __last A forward iterator.
3889 * @return The first iterator @c i such that @c i and @c i+1 are both
3890 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3891 * or @p __last if no such iterator exists.
3893 template<typename _ForwardIterator
>
3895 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3897 // concept requirements
3898 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3899 __glibcxx_function_requires(_EqualityComparableConcept
<
3900 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3901 __glibcxx_requires_valid_range(__first
, __last
);
3903 return std::__adjacent_find(__first
, __last
,
3904 __gnu_cxx::__ops::__iter_equal_to_iter());
3908 * @brief Find two adjacent values in a sequence using a predicate.
3909 * @ingroup non_mutating_algorithms
3910 * @param __first A forward iterator.
3911 * @param __last A forward iterator.
3912 * @param __binary_pred A binary predicate.
3913 * @return The first iterator @c i such that @c i and @c i+1 are both
3914 * valid iterators in @p [__first,__last) and such that
3915 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3918 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3920 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3921 _BinaryPredicate __binary_pred
)
3923 // concept requirements
3924 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3925 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3926 typename iterator_traits
<_ForwardIterator
>::value_type
,
3927 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3928 __glibcxx_requires_valid_range(__first
, __last
);
3930 return std::__adjacent_find(__first
, __last
,
3931 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
3935 * @brief Count the number of copies of a value in a sequence.
3936 * @ingroup non_mutating_algorithms
3937 * @param __first An input iterator.
3938 * @param __last An input iterator.
3939 * @param __value The value to be counted.
3940 * @return The number of iterators @c i in the range @p [__first,__last)
3941 * for which @c *i == @p __value
3943 template<typename _InputIterator
, typename _Tp
>
3944 typename iterator_traits
<_InputIterator
>::difference_type
3945 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
3947 // concept requirements
3948 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3949 __glibcxx_function_requires(_EqualOpConcept
<
3950 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3951 __glibcxx_requires_valid_range(__first
, __last
);
3953 return std::__count_if(__first
, __last
,
3954 __gnu_cxx::__ops::__iter_equals_val(__value
));
3958 * @brief Count the elements of a sequence for which a predicate is true.
3959 * @ingroup non_mutating_algorithms
3960 * @param __first An input iterator.
3961 * @param __last An input iterator.
3962 * @param __pred A predicate.
3963 * @return The number of iterators @c i in the range @p [__first,__last)
3964 * for which @p __pred(*i) is true.
3966 template<typename _InputIterator
, typename _Predicate
>
3967 typename iterator_traits
<_InputIterator
>::difference_type
3968 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3970 // concept requirements
3971 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3972 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3973 typename iterator_traits
<_InputIterator
>::value_type
>)
3974 __glibcxx_requires_valid_range(__first
, __last
);
3976 return std::__count_if(__first
, __last
,
3977 __gnu_cxx::__ops::__pred_iter(__pred
));
3981 * @brief Search a sequence for a matching sub-sequence.
3982 * @ingroup non_mutating_algorithms
3983 * @param __first1 A forward iterator.
3984 * @param __last1 A forward iterator.
3985 * @param __first2 A forward iterator.
3986 * @param __last2 A forward iterator.
3987 * @return The first iterator @c i in the range @p
3988 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
3989 * *(__first2+N) for each @c N in the range @p
3990 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
3992 * Searches the range @p [__first1,__last1) for a sub-sequence that
3993 * compares equal value-by-value with the sequence given by @p
3994 * [__first2,__last2) and returns an iterator to the first element
3995 * of the sub-sequence, or @p __last1 if the sub-sequence is not
3998 * Because the sub-sequence must lie completely within the range @p
3999 * [__first1,__last1) it must start at a position less than @p
4000 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4001 * length of the sub-sequence.
4003 * This means that the returned iterator @c i will be in the range
4004 * @p [__first1,__last1-(__last2-__first2))
4006 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4008 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4009 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4011 // concept requirements
4012 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4013 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4014 __glibcxx_function_requires(_EqualOpConcept
<
4015 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4016 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4017 __glibcxx_requires_valid_range(__first1
, __last1
);
4018 __glibcxx_requires_valid_range(__first2
, __last2
);
4020 return std::__search(__first1
, __last1
, __first2
, __last2
,
4021 __gnu_cxx::__ops::__iter_equal_to_iter());
4025 * @brief Search a sequence for a matching sub-sequence using a predicate.
4026 * @ingroup non_mutating_algorithms
4027 * @param __first1 A forward iterator.
4028 * @param __last1 A forward iterator.
4029 * @param __first2 A forward iterator.
4030 * @param __last2 A forward iterator.
4031 * @param __predicate A binary predicate.
4032 * @return The first iterator @c i in the range
4033 * @p [__first1,__last1-(__last2-__first2)) such that
4034 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4035 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4037 * Searches the range @p [__first1,__last1) for a sub-sequence that
4038 * compares equal value-by-value with the sequence given by @p
4039 * [__first2,__last2), using @p __predicate to determine equality,
4040 * and returns an iterator to the first element of the
4041 * sub-sequence, or @p __last1 if no such iterator exists.
4043 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4045 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4046 typename _BinaryPredicate
>
4048 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4049 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4050 _BinaryPredicate __predicate
)
4052 // concept requirements
4053 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4054 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4055 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4056 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4057 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4058 __glibcxx_requires_valid_range(__first1
, __last1
);
4059 __glibcxx_requires_valid_range(__first2
, __last2
);
4061 return std::__search(__first1
, __last1
, __first2
, __last2
,
4062 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4066 * @brief Search a sequence for a number of consecutive values.
4067 * @ingroup non_mutating_algorithms
4068 * @param __first A forward iterator.
4069 * @param __last A forward iterator.
4070 * @param __count The number of consecutive values.
4071 * @param __val The value to find.
4072 * @return The first iterator @c i in the range @p
4073 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4074 * each @c N in the range @p [0,__count), or @p __last if no such
4077 * Searches the range @p [__first,__last) for @p count consecutive elements
4078 * equal to @p __val.
4080 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4082 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4083 _Integer __count
, const _Tp
& __val
)
4085 // concept requirements
4086 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4087 __glibcxx_function_requires(_EqualOpConcept
<
4088 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4089 __glibcxx_requires_valid_range(__first
, __last
);
4091 return std::__search_n(__first
, __last
, __count
,
4092 __gnu_cxx::__ops::__iter_equals_val(__val
));
4097 * @brief Search a sequence for a number of consecutive values using a
4099 * @ingroup non_mutating_algorithms
4100 * @param __first A forward iterator.
4101 * @param __last A forward iterator.
4102 * @param __count The number of consecutive values.
4103 * @param __val The value to find.
4104 * @param __binary_pred A binary predicate.
4105 * @return The first iterator @c i in the range @p
4106 * [__first,__last-__count) such that @p
4107 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4108 * @p [0,__count), or @p __last if no such iterator exists.
4110 * Searches the range @p [__first,__last) for @p __count
4111 * consecutive elements for which the predicate returns true.
4113 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4114 typename _BinaryPredicate
>
4116 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4117 _Integer __count
, const _Tp
& __val
,
4118 _BinaryPredicate __binary_pred
)
4120 // concept requirements
4121 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4122 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4123 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4124 __glibcxx_requires_valid_range(__first
, __last
);
4126 return std::__search_n(__first
, __last
, __count
,
4127 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4132 * @brief Perform an operation on a sequence.
4133 * @ingroup mutating_algorithms
4134 * @param __first An input iterator.
4135 * @param __last An input iterator.
4136 * @param __result An output iterator.
4137 * @param __unary_op A unary operator.
4138 * @return An output iterator equal to @p __result+(__last-__first).
4140 * Applies the operator to each element in the input range and assigns
4141 * the results to successive elements of the output sequence.
4142 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4143 * range @p [0,__last-__first).
4145 * @p unary_op must not alter its argument.
4147 template<typename _InputIterator
, typename _OutputIterator
,
4148 typename _UnaryOperation
>
4150 transform(_InputIterator __first
, _InputIterator __last
,
4151 _OutputIterator __result
, _UnaryOperation __unary_op
)
4153 // concept requirements
4154 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4155 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4156 // "the type returned by a _UnaryOperation"
4157 __typeof__(__unary_op(*__first
))>)
4158 __glibcxx_requires_valid_range(__first
, __last
);
4160 for (; __first
!= __last
; ++__first
, ++__result
)
4161 *__result
= __unary_op(*__first
);
4166 * @brief Perform an operation on corresponding elements of two sequences.
4167 * @ingroup mutating_algorithms
4168 * @param __first1 An input iterator.
4169 * @param __last1 An input iterator.
4170 * @param __first2 An input iterator.
4171 * @param __result An output iterator.
4172 * @param __binary_op A binary operator.
4173 * @return An output iterator equal to @p result+(last-first).
4175 * Applies the operator to the corresponding elements in the two
4176 * input ranges and assigns the results to successive elements of the
4179 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4180 * @c N in the range @p [0,__last1-__first1).
4182 * @p binary_op must not alter either of its arguments.
4184 template<typename _InputIterator1
, typename _InputIterator2
,
4185 typename _OutputIterator
, typename _BinaryOperation
>
4187 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4188 _InputIterator2 __first2
, _OutputIterator __result
,
4189 _BinaryOperation __binary_op
)
4191 // concept requirements
4192 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4193 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4194 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4195 // "the type returned by a _BinaryOperation"
4196 __typeof__(__binary_op(*__first1
,*__first2
))>)
4197 __glibcxx_requires_valid_range(__first1
, __last1
);
4199 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4200 *__result
= __binary_op(*__first1
, *__first2
);
4205 * @brief Replace each occurrence of one value in a sequence with another
4207 * @ingroup mutating_algorithms
4208 * @param __first A forward iterator.
4209 * @param __last A forward iterator.
4210 * @param __old_value The value to be replaced.
4211 * @param __new_value The replacement value.
4212 * @return replace() returns no value.
4214 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4215 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4217 template<typename _ForwardIterator
, typename _Tp
>
4219 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4220 const _Tp
& __old_value
, const _Tp
& __new_value
)
4222 // concept requirements
4223 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4225 __glibcxx_function_requires(_EqualOpConcept
<
4226 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4227 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4228 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4229 __glibcxx_requires_valid_range(__first
, __last
);
4231 for (; __first
!= __last
; ++__first
)
4232 if (*__first
== __old_value
)
4233 *__first
= __new_value
;
4237 * @brief Replace each value in a sequence for which a predicate returns
4238 * true with another value.
4239 * @ingroup mutating_algorithms
4240 * @param __first A forward iterator.
4241 * @param __last A forward iterator.
4242 * @param __pred A predicate.
4243 * @param __new_value The replacement value.
4244 * @return replace_if() returns no value.
4246 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4247 * is true then the assignment @c *i = @p __new_value is performed.
4249 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4251 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4252 _Predicate __pred
, const _Tp
& __new_value
)
4254 // concept requirements
4255 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4257 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4258 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4259 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4260 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4261 __glibcxx_requires_valid_range(__first
, __last
);
4263 for (; __first
!= __last
; ++__first
)
4264 if (__pred(*__first
))
4265 *__first
= __new_value
;
4269 * @brief Assign the result of a function object to each value in a
4271 * @ingroup mutating_algorithms
4272 * @param __first A forward iterator.
4273 * @param __last A forward iterator.
4274 * @param __gen A function object taking no arguments and returning
4275 * std::iterator_traits<_ForwardIterator>::value_type
4276 * @return generate() returns no value.
4278 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4279 * @p [__first,__last).
4281 template<typename _ForwardIterator
, typename _Generator
>
4283 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4286 // concept requirements
4287 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4288 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4289 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4290 __glibcxx_requires_valid_range(__first
, __last
);
4292 for (; __first
!= __last
; ++__first
)
4297 * @brief Assign the result of a function object to each value in a
4299 * @ingroup mutating_algorithms
4300 * @param __first A forward iterator.
4301 * @param __n The length of the sequence.
4302 * @param __gen A function object taking no arguments and returning
4303 * std::iterator_traits<_ForwardIterator>::value_type
4304 * @return The end of the sequence, @p __first+__n
4306 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4307 * @p [__first,__first+__n).
4309 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4310 * DR 865. More algorithms that throw away information
4312 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4314 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4316 // concept requirements
4317 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4318 // "the type returned by a _Generator"
4319 __typeof__(__gen())>)
4321 for (__decltype(__n
+ 0) __niter
= __n
;
4322 __niter
> 0; --__niter
, ++__first
)
4328 * @brief Copy a sequence, removing consecutive duplicate values.
4329 * @ingroup mutating_algorithms
4330 * @param __first An input iterator.
4331 * @param __last An input iterator.
4332 * @param __result An output iterator.
4333 * @return An iterator designating the end of the resulting sequence.
4335 * Copies each element in the range @p [__first,__last) to the range
4336 * beginning at @p __result, except that only the first element is copied
4337 * from groups of consecutive elements that compare equal.
4338 * unique_copy() is stable, so the relative order of elements that are
4339 * copied is unchanged.
4341 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4342 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4344 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4345 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4348 template<typename _InputIterator
, typename _OutputIterator
>
4349 inline _OutputIterator
4350 unique_copy(_InputIterator __first
, _InputIterator __last
,
4351 _OutputIterator __result
)
4353 // concept requirements
4354 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4355 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4356 typename iterator_traits
<_InputIterator
>::value_type
>)
4357 __glibcxx_function_requires(_EqualityComparableConcept
<
4358 typename iterator_traits
<_InputIterator
>::value_type
>)
4359 __glibcxx_requires_valid_range(__first
, __last
);
4361 if (__first
== __last
)
4363 return std::__unique_copy(__first
, __last
, __result
,
4364 __gnu_cxx::__ops::__iter_equal_to_iter(),
4365 std::__iterator_category(__first
),
4366 std::__iterator_category(__result
));
4370 * @brief Copy a sequence, removing consecutive values using a predicate.
4371 * @ingroup mutating_algorithms
4372 * @param __first An input iterator.
4373 * @param __last An input iterator.
4374 * @param __result An output iterator.
4375 * @param __binary_pred A binary predicate.
4376 * @return An iterator designating the end of the resulting sequence.
4378 * Copies each element in the range @p [__first,__last) to the range
4379 * beginning at @p __result, except that only the first element is copied
4380 * from groups of consecutive elements for which @p __binary_pred returns
4382 * unique_copy() is stable, so the relative order of elements that are
4383 * copied is unchanged.
4385 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4386 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4388 template<typename _InputIterator
, typename _OutputIterator
,
4389 typename _BinaryPredicate
>
4390 inline _OutputIterator
4391 unique_copy(_InputIterator __first
, _InputIterator __last
,
4392 _OutputIterator __result
,
4393 _BinaryPredicate __binary_pred
)
4395 // concept requirements -- predicates checked later
4396 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4397 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4398 typename iterator_traits
<_InputIterator
>::value_type
>)
4399 __glibcxx_requires_valid_range(__first
, __last
);
4401 if (__first
== __last
)
4403 return std::__unique_copy(__first
, __last
, __result
,
4404 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4405 std::__iterator_category(__first
),
4406 std::__iterator_category(__result
));
4410 * @brief Randomly shuffle the elements of a sequence.
4411 * @ingroup mutating_algorithms
4412 * @param __first A forward iterator.
4413 * @param __last A forward iterator.
4416 * Reorder the elements in the range @p [__first,__last) using a random
4417 * distribution, so that every possible ordering of the sequence is
4420 template<typename _RandomAccessIterator
>
4422 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4424 // concept requirements
4425 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4426 _RandomAccessIterator
>)
4427 __glibcxx_requires_valid_range(__first
, __last
);
4429 if (__first
!= __last
)
4430 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4431 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4435 * @brief Shuffle the elements of a sequence using a random number
4437 * @ingroup mutating_algorithms
4438 * @param __first A forward iterator.
4439 * @param __last A forward iterator.
4440 * @param __rand The RNG functor or function.
4443 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4444 * provide a random distribution. Calling @p __rand(N) for a positive
4445 * integer @p N should return a randomly chosen integer from the
4448 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4450 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4451 #if __cplusplus >= 201103L
4452 _RandomNumberGenerator
&& __rand
)
4454 _RandomNumberGenerator
& __rand
)
4457 // concept requirements
4458 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4459 _RandomAccessIterator
>)
4460 __glibcxx_requires_valid_range(__first
, __last
);
4462 if (__first
== __last
)
4464 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4465 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4470 * @brief Move elements for which a predicate is true to the beginning
4472 * @ingroup mutating_algorithms
4473 * @param __first A forward iterator.
4474 * @param __last A forward iterator.
4475 * @param __pred A predicate functor.
4476 * @return An iterator @p middle such that @p __pred(i) is true for each
4477 * iterator @p i in the range @p [__first,middle) and false for each @p i
4478 * in the range @p [middle,__last).
4480 * @p __pred must not modify its operand. @p partition() does not preserve
4481 * the relative ordering of elements in each group, use
4482 * @p stable_partition() if this is needed.
4484 template<typename _ForwardIterator
, typename _Predicate
>
4485 inline _ForwardIterator
4486 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4489 // concept requirements
4490 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4492 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4493 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4494 __glibcxx_requires_valid_range(__first
, __last
);
4496 return std::__partition(__first
, __last
, __pred
,
4497 std::__iterator_category(__first
));
4502 * @brief Sort the smallest elements of a sequence.
4503 * @ingroup sorting_algorithms
4504 * @param __first An iterator.
4505 * @param __middle Another iterator.
4506 * @param __last Another iterator.
4509 * Sorts the smallest @p (__middle-__first) elements in the range
4510 * @p [first,last) and moves them to the range @p [__first,__middle). The
4511 * order of the remaining elements in the range @p [__middle,__last) is
4513 * After the sort if @e i and @e j are iterators in the range
4514 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4515 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4517 template<typename _RandomAccessIterator
>
4519 partial_sort(_RandomAccessIterator __first
,
4520 _RandomAccessIterator __middle
,
4521 _RandomAccessIterator __last
)
4523 // concept requirements
4524 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4525 _RandomAccessIterator
>)
4526 __glibcxx_function_requires(_LessThanComparableConcept
<
4527 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4528 __glibcxx_requires_valid_range(__first
, __middle
);
4529 __glibcxx_requires_valid_range(__middle
, __last
);
4531 std::__partial_sort(__first
, __middle
, __last
,
4532 __gnu_cxx::__ops::__iter_less_iter());
4536 * @brief Sort the smallest elements of a sequence using a predicate
4538 * @ingroup sorting_algorithms
4539 * @param __first An iterator.
4540 * @param __middle Another iterator.
4541 * @param __last Another iterator.
4542 * @param __comp A comparison functor.
4545 * Sorts the smallest @p (__middle-__first) elements in the range
4546 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4547 * order of the remaining elements in the range @p [__middle,__last) is
4549 * After the sort if @e i and @e j are iterators in the range
4550 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4551 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4554 template<typename _RandomAccessIterator
, typename _Compare
>
4556 partial_sort(_RandomAccessIterator __first
,
4557 _RandomAccessIterator __middle
,
4558 _RandomAccessIterator __last
,
4561 // concept requirements
4562 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4563 _RandomAccessIterator
>)
4564 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4565 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4566 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4567 __glibcxx_requires_valid_range(__first
, __middle
);
4568 __glibcxx_requires_valid_range(__middle
, __last
);
4570 std::__partial_sort(__first
, __middle
, __last
,
4571 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4575 * @brief Sort a sequence just enough to find a particular position.
4576 * @ingroup sorting_algorithms
4577 * @param __first An iterator.
4578 * @param __nth Another iterator.
4579 * @param __last Another iterator.
4582 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4583 * is the same element that would have been in that position had the
4584 * whole sequence been sorted. The elements either side of @p *__nth are
4585 * not completely sorted, but for any iterator @e i in the range
4586 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4587 * holds that *j < *i is false.
4589 template<typename _RandomAccessIterator
>
4591 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4592 _RandomAccessIterator __last
)
4594 // concept requirements
4595 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4596 _RandomAccessIterator
>)
4597 __glibcxx_function_requires(_LessThanComparableConcept
<
4598 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4599 __glibcxx_requires_valid_range(__first
, __nth
);
4600 __glibcxx_requires_valid_range(__nth
, __last
);
4602 if (__first
== __last
|| __nth
== __last
)
4605 std::__introselect(__first
, __nth
, __last
,
4606 std::__lg(__last
- __first
) * 2,
4607 __gnu_cxx::__ops::__iter_less_iter());
4611 * @brief Sort a sequence just enough to find a particular position
4612 * using a predicate for comparison.
4613 * @ingroup sorting_algorithms
4614 * @param __first An iterator.
4615 * @param __nth Another iterator.
4616 * @param __last Another iterator.
4617 * @param __comp A comparison functor.
4620 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4621 * is the same element that would have been in that position had the
4622 * whole sequence been sorted. The elements either side of @p *__nth are
4623 * not completely sorted, but for any iterator @e i in the range
4624 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4625 * holds that @p __comp(*j,*i) is false.
4627 template<typename _RandomAccessIterator
, typename _Compare
>
4629 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4630 _RandomAccessIterator __last
, _Compare __comp
)
4632 // concept requirements
4633 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4634 _RandomAccessIterator
>)
4635 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4636 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4637 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4638 __glibcxx_requires_valid_range(__first
, __nth
);
4639 __glibcxx_requires_valid_range(__nth
, __last
);
4641 if (__first
== __last
|| __nth
== __last
)
4644 std::__introselect(__first
, __nth
, __last
,
4645 std::__lg(__last
- __first
) * 2,
4646 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4650 * @brief Sort the elements of a sequence.
4651 * @ingroup sorting_algorithms
4652 * @param __first An iterator.
4653 * @param __last Another iterator.
4656 * Sorts the elements in the range @p [__first,__last) in ascending order,
4657 * such that for each iterator @e i in the range @p [__first,__last-1),
4658 * *(i+1)<*i is false.
4660 * The relative ordering of equivalent elements is not preserved, use
4661 * @p stable_sort() if this is needed.
4663 template<typename _RandomAccessIterator
>
4665 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4667 // concept requirements
4668 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4669 _RandomAccessIterator
>)
4670 __glibcxx_function_requires(_LessThanComparableConcept
<
4671 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4672 __glibcxx_requires_valid_range(__first
, __last
);
4674 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4678 * @brief Sort the elements of a sequence using a predicate for comparison.
4679 * @ingroup sorting_algorithms
4680 * @param __first An iterator.
4681 * @param __last Another iterator.
4682 * @param __comp A comparison functor.
4685 * Sorts the elements in the range @p [__first,__last) in ascending order,
4686 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4687 * range @p [__first,__last-1).
4689 * The relative ordering of equivalent elements is not preserved, use
4690 * @p stable_sort() if this is needed.
4692 template<typename _RandomAccessIterator
, typename _Compare
>
4694 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4697 // concept requirements
4698 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4699 _RandomAccessIterator
>)
4700 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4701 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4702 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4703 __glibcxx_requires_valid_range(__first
, __last
);
4705 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4708 template<typename _InputIterator1
, typename _InputIterator2
,
4709 typename _OutputIterator
, typename _Compare
>
4711 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4712 _InputIterator2 __first2
, _InputIterator2 __last2
,
4713 _OutputIterator __result
, _Compare __comp
)
4715 while (__first1
!= __last1
&& __first2
!= __last2
)
4717 if (__comp(__first2
, __first1
))
4719 *__result
= *__first2
;
4724 *__result
= *__first1
;
4729 return std::copy(__first2
, __last2
,
4730 std::copy(__first1
, __last1
, __result
));
4734 * @brief Merges two sorted ranges.
4735 * @ingroup sorting_algorithms
4736 * @param __first1 An iterator.
4737 * @param __first2 Another iterator.
4738 * @param __last1 Another iterator.
4739 * @param __last2 Another iterator.
4740 * @param __result An iterator pointing to the end of the merged range.
4741 * @return An iterator pointing to the first element <em>not less
4744 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4745 * the sorted range @p [__result, __result + (__last1-__first1) +
4746 * (__last2-__first2)). Both input ranges must be sorted, and the
4747 * output range must not overlap with either of the input ranges.
4748 * The sort is @e stable, that is, for equivalent elements in the
4749 * two ranges, elements from the first range will always come
4750 * before elements from the second.
4752 template<typename _InputIterator1
, typename _InputIterator2
,
4753 typename _OutputIterator
>
4755 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4756 _InputIterator2 __first2
, _InputIterator2 __last2
,
4757 _OutputIterator __result
)
4759 // concept requirements
4760 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4761 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4762 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4763 typename iterator_traits
<_InputIterator1
>::value_type
>)
4764 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4765 typename iterator_traits
<_InputIterator2
>::value_type
>)
4766 __glibcxx_function_requires(_LessThanOpConcept
<
4767 typename iterator_traits
<_InputIterator2
>::value_type
,
4768 typename iterator_traits
<_InputIterator1
>::value_type
>)
4769 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4770 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4772 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4773 __first2
, __last2
, __result
,
4774 __gnu_cxx::__ops::__iter_less_iter());
4778 * @brief Merges two sorted ranges.
4779 * @ingroup sorting_algorithms
4780 * @param __first1 An iterator.
4781 * @param __first2 Another iterator.
4782 * @param __last1 Another iterator.
4783 * @param __last2 Another iterator.
4784 * @param __result An iterator pointing to the end of the merged range.
4785 * @param __comp A functor to use for comparisons.
4786 * @return An iterator pointing to the first element "not less
4789 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4790 * the sorted range @p [__result, __result + (__last1-__first1) +
4791 * (__last2-__first2)). Both input ranges must be sorted, and the
4792 * output range must not overlap with either of the input ranges.
4793 * The sort is @e stable, that is, for equivalent elements in the
4794 * two ranges, elements from the first range will always come
4795 * before elements from the second.
4797 * The comparison function should have the same effects on ordering as
4798 * the function used for the initial sort.
4800 template<typename _InputIterator1
, typename _InputIterator2
,
4801 typename _OutputIterator
, typename _Compare
>
4803 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4804 _InputIterator2 __first2
, _InputIterator2 __last2
,
4805 _OutputIterator __result
, _Compare __comp
)
4807 // concept requirements
4808 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4809 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4810 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4811 typename iterator_traits
<_InputIterator1
>::value_type
>)
4812 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4813 typename iterator_traits
<_InputIterator2
>::value_type
>)
4814 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4815 typename iterator_traits
<_InputIterator2
>::value_type
,
4816 typename iterator_traits
<_InputIterator1
>::value_type
>)
4817 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4818 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4820 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4821 __first2
, __last2
, __result
,
4822 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4825 template<typename _RandomAccessIterator
, typename _Compare
>
4827 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4830 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4832 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4835 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
4836 _TmpBuf
__buf(__first
, __last
);
4838 if (__buf
.begin() == 0)
4839 std::__inplace_stable_sort(__first
, __last
, __comp
);
4841 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4842 _DistanceType(__buf
.size()), __comp
);
4846 * @brief Sort the elements of a sequence, preserving the relative order
4847 * of equivalent elements.
4848 * @ingroup sorting_algorithms
4849 * @param __first An iterator.
4850 * @param __last Another iterator.
4853 * Sorts the elements in the range @p [__first,__last) in ascending order,
4854 * such that for each iterator @p i in the range @p [__first,__last-1),
4855 * @p *(i+1)<*i is false.
4857 * The relative ordering of equivalent elements is preserved, so any two
4858 * elements @p x and @p y in the range @p [__first,__last) such that
4859 * @p x<y is false and @p y<x is false will have the same relative
4860 * ordering after calling @p stable_sort().
4862 template<typename _RandomAccessIterator
>
4864 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4866 // concept requirements
4867 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4868 _RandomAccessIterator
>)
4869 __glibcxx_function_requires(_LessThanComparableConcept
<
4870 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4871 __glibcxx_requires_valid_range(__first
, __last
);
4873 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4874 __gnu_cxx::__ops::__iter_less_iter());
4878 * @brief Sort the elements of a sequence using a predicate for comparison,
4879 * preserving the relative order of equivalent elements.
4880 * @ingroup sorting_algorithms
4881 * @param __first An iterator.
4882 * @param __last Another iterator.
4883 * @param __comp A comparison functor.
4886 * Sorts the elements in the range @p [__first,__last) in ascending order,
4887 * such that for each iterator @p i in the range @p [__first,__last-1),
4888 * @p __comp(*(i+1),*i) is false.
4890 * The relative ordering of equivalent elements is preserved, so any two
4891 * elements @p x and @p y in the range @p [__first,__last) such that
4892 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
4893 * relative ordering after calling @p stable_sort().
4895 template<typename _RandomAccessIterator
, typename _Compare
>
4897 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4900 // concept requirements
4901 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4902 _RandomAccessIterator
>)
4903 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4904 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4905 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4906 __glibcxx_requires_valid_range(__first
, __last
);
4908 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4909 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4912 template<typename _InputIterator1
, typename _InputIterator2
,
4913 typename _OutputIterator
,
4916 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4917 _InputIterator2 __first2
, _InputIterator2 __last2
,
4918 _OutputIterator __result
, _Compare __comp
)
4920 while (__first1
!= __last1
&& __first2
!= __last2
)
4922 if (__comp(__first1
, __first2
))
4924 *__result
= *__first1
;
4927 else if (__comp(__first2
, __first1
))
4929 *__result
= *__first2
;
4934 *__result
= *__first1
;
4940 return std::copy(__first2
, __last2
,
4941 std::copy(__first1
, __last1
, __result
));
4945 * @brief Return the union of two sorted ranges.
4946 * @ingroup set_algorithms
4947 * @param __first1 Start of first range.
4948 * @param __last1 End of first range.
4949 * @param __first2 Start of second range.
4950 * @param __last2 End of second range.
4951 * @return End of the output range.
4952 * @ingroup set_algorithms
4954 * This operation iterates over both ranges, copying elements present in
4955 * each range in order to the output range. Iterators increment for each
4956 * range. When the current element of one range is less than the other,
4957 * that element is copied and the iterator advanced. If an element is
4958 * contained in both ranges, the element from the first range is copied and
4959 * both ranges advance. The output range may not overlap either input
4962 template<typename _InputIterator1
, typename _InputIterator2
,
4963 typename _OutputIterator
>
4965 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4966 _InputIterator2 __first2
, _InputIterator2 __last2
,
4967 _OutputIterator __result
)
4969 // concept requirements
4970 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4971 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4972 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4973 typename iterator_traits
<_InputIterator1
>::value_type
>)
4974 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4975 typename iterator_traits
<_InputIterator2
>::value_type
>)
4976 __glibcxx_function_requires(_LessThanOpConcept
<
4977 typename iterator_traits
<_InputIterator1
>::value_type
,
4978 typename iterator_traits
<_InputIterator2
>::value_type
>)
4979 __glibcxx_function_requires(_LessThanOpConcept
<
4980 typename iterator_traits
<_InputIterator2
>::value_type
,
4981 typename iterator_traits
<_InputIterator1
>::value_type
>)
4982 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4983 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4985 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
4986 __first2
, __last2
, __result
,
4987 __gnu_cxx::__ops::__iter_less_iter());
4991 * @brief Return the union of two sorted ranges using a comparison functor.
4992 * @ingroup set_algorithms
4993 * @param __first1 Start of first range.
4994 * @param __last1 End of first range.
4995 * @param __first2 Start of second range.
4996 * @param __last2 End of second range.
4997 * @param __comp The comparison functor.
4998 * @return End of the output range.
4999 * @ingroup set_algorithms
5001 * This operation iterates over both ranges, copying elements present in
5002 * each range in order to the output range. Iterators increment for each
5003 * range. When the current element of one range is less than the other
5004 * according to @p __comp, that element is copied and the iterator advanced.
5005 * If an equivalent element according to @p __comp is contained in both
5006 * ranges, the element from the first range is copied and both ranges
5007 * advance. The output range may not overlap either input range.
5009 template<typename _InputIterator1
, typename _InputIterator2
,
5010 typename _OutputIterator
, typename _Compare
>
5012 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5013 _InputIterator2 __first2
, _InputIterator2 __last2
,
5014 _OutputIterator __result
, _Compare __comp
)
5016 // concept requirements
5017 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5018 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5019 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5020 typename iterator_traits
<_InputIterator1
>::value_type
>)
5021 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5022 typename iterator_traits
<_InputIterator2
>::value_type
>)
5023 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5024 typename iterator_traits
<_InputIterator1
>::value_type
,
5025 typename iterator_traits
<_InputIterator2
>::value_type
>)
5026 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5027 typename iterator_traits
<_InputIterator2
>::value_type
,
5028 typename iterator_traits
<_InputIterator1
>::value_type
>)
5029 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5030 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5032 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5033 __first2
, __last2
, __result
,
5034 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5037 template<typename _InputIterator1
, typename _InputIterator2
,
5038 typename _OutputIterator
,
5041 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5042 _InputIterator2 __first2
, _InputIterator2 __last2
,
5043 _OutputIterator __result
, _Compare __comp
)
5045 while (__first1
!= __last1
&& __first2
!= __last2
)
5046 if (__comp(__first1
, __first2
))
5048 else if (__comp(__first2
, __first1
))
5052 *__result
= *__first1
;
5061 * @brief Return the intersection of two sorted ranges.
5062 * @ingroup set_algorithms
5063 * @param __first1 Start of first range.
5064 * @param __last1 End of first range.
5065 * @param __first2 Start of second range.
5066 * @param __last2 End of second range.
5067 * @return End of the output range.
5068 * @ingroup set_algorithms
5070 * This operation iterates over both ranges, copying elements present in
5071 * both ranges in order to the output range. Iterators increment for each
5072 * range. When the current element of one range is less than the other,
5073 * that iterator advances. If an element is contained in both ranges, the
5074 * element from the first range is copied and both ranges advance. The
5075 * output range may not overlap either input range.
5077 template<typename _InputIterator1
, typename _InputIterator2
,
5078 typename _OutputIterator
>
5080 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5081 _InputIterator2 __first2
, _InputIterator2 __last2
,
5082 _OutputIterator __result
)
5084 // concept requirements
5085 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5086 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5087 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5088 typename iterator_traits
<_InputIterator1
>::value_type
>)
5089 __glibcxx_function_requires(_LessThanOpConcept
<
5090 typename iterator_traits
<_InputIterator1
>::value_type
,
5091 typename iterator_traits
<_InputIterator2
>::value_type
>)
5092 __glibcxx_function_requires(_LessThanOpConcept
<
5093 typename iterator_traits
<_InputIterator2
>::value_type
,
5094 typename iterator_traits
<_InputIterator1
>::value_type
>)
5095 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5096 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5098 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5099 __first2
, __last2
, __result
,
5100 __gnu_cxx::__ops::__iter_less_iter());
5104 * @brief Return the intersection of two sorted ranges using comparison
5106 * @ingroup set_algorithms
5107 * @param __first1 Start of first range.
5108 * @param __last1 End of first range.
5109 * @param __first2 Start of second range.
5110 * @param __last2 End of second range.
5111 * @param __comp The comparison functor.
5112 * @return End of the output range.
5113 * @ingroup set_algorithms
5115 * This operation iterates over both ranges, copying elements present in
5116 * both ranges in order to the output range. Iterators increment for each
5117 * range. When the current element of one range is less than the other
5118 * according to @p __comp, that iterator advances. If an element is
5119 * contained in both ranges according to @p __comp, the element from the
5120 * first range is copied and both ranges advance. The output range may not
5121 * overlap either input range.
5123 template<typename _InputIterator1
, typename _InputIterator2
,
5124 typename _OutputIterator
, typename _Compare
>
5126 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5127 _InputIterator2 __first2
, _InputIterator2 __last2
,
5128 _OutputIterator __result
, _Compare __comp
)
5130 // concept requirements
5131 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5132 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5133 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5134 typename iterator_traits
<_InputIterator1
>::value_type
>)
5135 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5136 typename iterator_traits
<_InputIterator1
>::value_type
,
5137 typename iterator_traits
<_InputIterator2
>::value_type
>)
5138 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5139 typename iterator_traits
<_InputIterator2
>::value_type
,
5140 typename iterator_traits
<_InputIterator1
>::value_type
>)
5141 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5142 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5144 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5145 __first2
, __last2
, __result
,
5146 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5149 template<typename _InputIterator1
, typename _InputIterator2
,
5150 typename _OutputIterator
,
5153 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5154 _InputIterator2 __first2
, _InputIterator2 __last2
,
5155 _OutputIterator __result
, _Compare __comp
)
5157 while (__first1
!= __last1
&& __first2
!= __last2
)
5158 if (__comp(__first1
, __first2
))
5160 *__result
= *__first1
;
5164 else if (__comp(__first2
, __first1
))
5171 return std::copy(__first1
, __last1
, __result
);
5175 * @brief Return the difference of two sorted ranges.
5176 * @ingroup set_algorithms
5177 * @param __first1 Start of first range.
5178 * @param __last1 End of first range.
5179 * @param __first2 Start of second range.
5180 * @param __last2 End of second range.
5181 * @return End of the output range.
5182 * @ingroup set_algorithms
5184 * This operation iterates over both ranges, copying elements present in
5185 * the first range but not the second in order to the output range.
5186 * Iterators increment for each range. When the current element of the
5187 * first range is less than the second, that element is copied and the
5188 * iterator advances. If the current element of the second range is less,
5189 * the iterator advances, but no element is copied. If an element is
5190 * contained in both ranges, no elements are copied and both ranges
5191 * advance. The output range may not overlap either input range.
5193 template<typename _InputIterator1
, typename _InputIterator2
,
5194 typename _OutputIterator
>
5196 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5197 _InputIterator2 __first2
, _InputIterator2 __last2
,
5198 _OutputIterator __result
)
5200 // concept requirements
5201 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5202 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5203 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5204 typename iterator_traits
<_InputIterator1
>::value_type
>)
5205 __glibcxx_function_requires(_LessThanOpConcept
<
5206 typename iterator_traits
<_InputIterator1
>::value_type
,
5207 typename iterator_traits
<_InputIterator2
>::value_type
>)
5208 __glibcxx_function_requires(_LessThanOpConcept
<
5209 typename iterator_traits
<_InputIterator2
>::value_type
,
5210 typename iterator_traits
<_InputIterator1
>::value_type
>)
5211 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5212 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5214 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5215 __first2
, __last2
, __result
,
5216 __gnu_cxx::__ops::__iter_less_iter());
5220 * @brief Return the difference of two sorted ranges using comparison
5222 * @ingroup set_algorithms
5223 * @param __first1 Start of first range.
5224 * @param __last1 End of first range.
5225 * @param __first2 Start of second range.
5226 * @param __last2 End of second range.
5227 * @param __comp The comparison functor.
5228 * @return End of the output range.
5229 * @ingroup set_algorithms
5231 * This operation iterates over both ranges, copying elements present in
5232 * the first range but not the second in order to the output range.
5233 * Iterators increment for each range. When the current element of the
5234 * first range is less than the second according to @p __comp, that element
5235 * is copied and the iterator advances. If the current element of the
5236 * second range is less, no element is copied and the iterator advances.
5237 * If an element is contained in both ranges according to @p __comp, no
5238 * elements are copied and both ranges advance. The output range may not
5239 * overlap either input range.
5241 template<typename _InputIterator1
, typename _InputIterator2
,
5242 typename _OutputIterator
, typename _Compare
>
5244 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5245 _InputIterator2 __first2
, _InputIterator2 __last2
,
5246 _OutputIterator __result
, _Compare __comp
)
5248 // concept requirements
5249 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5250 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5251 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5252 typename iterator_traits
<_InputIterator1
>::value_type
>)
5253 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5254 typename iterator_traits
<_InputIterator1
>::value_type
,
5255 typename iterator_traits
<_InputIterator2
>::value_type
>)
5256 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5257 typename iterator_traits
<_InputIterator2
>::value_type
,
5258 typename iterator_traits
<_InputIterator1
>::value_type
>)
5259 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5260 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5262 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5263 __first2
, __last2
, __result
,
5264 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5267 template<typename _InputIterator1
, typename _InputIterator2
,
5268 typename _OutputIterator
,
5271 __set_symmetric_difference(_InputIterator1 __first1
,
5272 _InputIterator1 __last1
,
5273 _InputIterator2 __first2
,
5274 _InputIterator2 __last2
,
5275 _OutputIterator __result
,
5278 while (__first1
!= __last1
&& __first2
!= __last2
)
5279 if (__comp(__first1
, __first2
))
5281 *__result
= *__first1
;
5285 else if (__comp(__first2
, __first1
))
5287 *__result
= *__first2
;
5296 return std::copy(__first2
, __last2
,
5297 std::copy(__first1
, __last1
, __result
));
5301 * @brief Return the symmetric difference of two sorted ranges.
5302 * @ingroup set_algorithms
5303 * @param __first1 Start of first range.
5304 * @param __last1 End of first range.
5305 * @param __first2 Start of second range.
5306 * @param __last2 End of second range.
5307 * @return End of the output range.
5308 * @ingroup set_algorithms
5310 * This operation iterates over both ranges, copying elements present in
5311 * one range but not the other in order to the output range. Iterators
5312 * increment for each range. When the current element of one range is less
5313 * than the other, that element is copied and the iterator advances. If an
5314 * element is contained in both ranges, no elements are copied and both
5315 * ranges advance. The output range may not overlap either input range.
5317 template<typename _InputIterator1
, typename _InputIterator2
,
5318 typename _OutputIterator
>
5320 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5321 _InputIterator2 __first2
, _InputIterator2 __last2
,
5322 _OutputIterator __result
)
5324 // concept requirements
5325 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5326 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5327 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5328 typename iterator_traits
<_InputIterator1
>::value_type
>)
5329 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5330 typename iterator_traits
<_InputIterator2
>::value_type
>)
5331 __glibcxx_function_requires(_LessThanOpConcept
<
5332 typename iterator_traits
<_InputIterator1
>::value_type
,
5333 typename iterator_traits
<_InputIterator2
>::value_type
>)
5334 __glibcxx_function_requires(_LessThanOpConcept
<
5335 typename iterator_traits
<_InputIterator2
>::value_type
,
5336 typename iterator_traits
<_InputIterator1
>::value_type
>)
5337 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5338 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5340 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5341 __first2
, __last2
, __result
,
5342 __gnu_cxx::__ops::__iter_less_iter());
5346 * @brief Return the symmetric difference of two sorted ranges using
5347 * comparison functor.
5348 * @ingroup set_algorithms
5349 * @param __first1 Start of first range.
5350 * @param __last1 End of first range.
5351 * @param __first2 Start of second range.
5352 * @param __last2 End of second range.
5353 * @param __comp The comparison functor.
5354 * @return End of the output range.
5355 * @ingroup set_algorithms
5357 * This operation iterates over both ranges, copying elements present in
5358 * one range but not the other in order to the output range. Iterators
5359 * increment for each range. When the current element of one range is less
5360 * than the other according to @p comp, that element is copied and the
5361 * iterator advances. If an element is contained in both ranges according
5362 * to @p __comp, no elements are copied and both ranges advance. The output
5363 * range may not overlap either input range.
5365 template<typename _InputIterator1
, typename _InputIterator2
,
5366 typename _OutputIterator
, typename _Compare
>
5368 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5369 _InputIterator2 __first2
, _InputIterator2 __last2
,
5370 _OutputIterator __result
,
5373 // concept requirements
5374 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5375 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5376 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5377 typename iterator_traits
<_InputIterator1
>::value_type
>)
5378 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5379 typename iterator_traits
<_InputIterator2
>::value_type
>)
5380 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5381 typename iterator_traits
<_InputIterator1
>::value_type
,
5382 typename iterator_traits
<_InputIterator2
>::value_type
>)
5383 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5384 typename iterator_traits
<_InputIterator2
>::value_type
,
5385 typename iterator_traits
<_InputIterator1
>::value_type
>)
5386 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5387 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5389 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5390 __first2
, __last2
, __result
,
5391 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5394 template<typename _ForwardIterator
, typename _Compare
>
5396 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5399 if (__first
== __last
)
5401 _ForwardIterator __result
= __first
;
5402 while (++__first
!= __last
)
5403 if (__comp(__first
, __result
))
5409 * @brief Return the minimum element in a range.
5410 * @ingroup sorting_algorithms
5411 * @param __first Start of range.
5412 * @param __last End of range.
5413 * @return Iterator referencing the first instance of the smallest value.
5415 template<typename _ForwardIterator
>
5417 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5419 // concept requirements
5420 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5421 __glibcxx_function_requires(_LessThanComparableConcept
<
5422 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5423 __glibcxx_requires_valid_range(__first
, __last
);
5425 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5426 __gnu_cxx::__ops::__iter_less_iter());
5430 * @brief Return the minimum element in a range using comparison functor.
5431 * @ingroup sorting_algorithms
5432 * @param __first Start of range.
5433 * @param __last End of range.
5434 * @param __comp Comparison functor.
5435 * @return Iterator referencing the first instance of the smallest value
5436 * according to __comp.
5438 template<typename _ForwardIterator
, typename _Compare
>
5440 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5443 // concept requirements
5444 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5445 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5446 typename iterator_traits
<_ForwardIterator
>::value_type
,
5447 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5448 __glibcxx_requires_valid_range(__first
, __last
);
5450 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5451 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5454 template<typename _ForwardIterator
, typename _Compare
>
5456 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5459 if (__first
== __last
) return __first
;
5460 _ForwardIterator __result
= __first
;
5461 while (++__first
!= __last
)
5462 if (__comp(__result
, __first
))
5468 * @brief Return the maximum element in a range.
5469 * @ingroup sorting_algorithms
5470 * @param __first Start of range.
5471 * @param __last End of range.
5472 * @return Iterator referencing the first instance of the largest value.
5474 template<typename _ForwardIterator
>
5476 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5478 // concept requirements
5479 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5480 __glibcxx_function_requires(_LessThanComparableConcept
<
5481 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5482 __glibcxx_requires_valid_range(__first
, __last
);
5484 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5485 __gnu_cxx::__ops::__iter_less_iter());
5489 * @brief Return the maximum element in a range using comparison functor.
5490 * @ingroup sorting_algorithms
5491 * @param __first Start of range.
5492 * @param __last End of range.
5493 * @param __comp Comparison functor.
5494 * @return Iterator referencing the first instance of the largest value
5495 * according to __comp.
5497 template<typename _ForwardIterator
, typename _Compare
>
5499 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5502 // concept requirements
5503 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5504 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5505 typename iterator_traits
<_ForwardIterator
>::value_type
,
5506 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5507 __glibcxx_requires_valid_range(__first
, __last
);
5509 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5510 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5513 _GLIBCXX_END_NAMESPACE_ALGO
5516 #endif /* _STL_ALGO_H */