1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <cstdlib> // for rand
66 #include <bits/algorithmfwd.h>
67 #include <bits/stl_heap.h>
68 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
69 #include <debug/debug.h>
70 #include <initializer_list>
72 // See concept_check.h for the __glibcxx_*_requires macros.
74 _GLIBCXX_BEGIN_NAMESPACE(std
)
77 * @brief Find the median of three values.
81 * @return One of @p a, @p b or @p c.
83 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
84 * then the value returned will be @c m.
85 * This is an SGI extension.
86 * @ingroup SGIextensions
88 template<typename _Tp
>
90 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
92 // concept requirements
93 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
110 * @brief Find the median of three values using a predicate for comparison.
114 * @param comp A binary predicate.
115 * @return One of @p a, @p b or @p c.
117 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
118 * and @p comp(m,n) are both true then the value returned will be @c m.
119 * This is an SGI extension.
120 * @ingroup SGIextensions
122 template<typename _Tp
, typename _Compare
>
124 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
126 // concept requirements
127 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
129 if (__comp(__a
, __b
))
130 if (__comp(__b
, __c
))
132 else if (__comp(__a
, __c
))
136 else if (__comp(__a
, __c
))
138 else if (__comp(__b
, __c
))
146 /// This is an overload used by find() for the Input Iterator case.
147 template<typename _InputIterator
, typename _Tp
>
148 inline _InputIterator
149 __find(_InputIterator __first
, _InputIterator __last
,
150 const _Tp
& __val
, input_iterator_tag
)
152 while (__first
!= __last
&& !(*__first
== __val
))
157 /// This is an overload used by find_if() for the Input Iterator case.
158 template<typename _InputIterator
, typename _Predicate
>
159 inline _InputIterator
160 __find_if(_InputIterator __first
, _InputIterator __last
,
161 _Predicate __pred
, input_iterator_tag
)
163 while (__first
!= __last
&& !bool(__pred(*__first
)))
168 /// This is an overload used by find() for the RAI case.
169 template<typename _RandomAccessIterator
, typename _Tp
>
170 _RandomAccessIterator
171 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
172 const _Tp
& __val
, random_access_iterator_tag
)
174 typename iterator_traits
<_RandomAccessIterator
>::difference_type
175 __trip_count
= (__last
- __first
) >> 2;
177 for (; __trip_count
> 0; --__trip_count
)
179 if (*__first
== __val
)
183 if (*__first
== __val
)
187 if (*__first
== __val
)
191 if (*__first
== __val
)
196 switch (__last
- __first
)
199 if (*__first
== __val
)
203 if (*__first
== __val
)
207 if (*__first
== __val
)
216 /// This is an overload used by find_if() for the RAI case.
217 template<typename _RandomAccessIterator
, typename _Predicate
>
218 _RandomAccessIterator
219 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
220 _Predicate __pred
, random_access_iterator_tag
)
222 typename iterator_traits
<_RandomAccessIterator
>::difference_type
223 __trip_count
= (__last
- __first
) >> 2;
225 for (; __trip_count
> 0; --__trip_count
)
227 if (__pred(*__first
))
231 if (__pred(*__first
))
235 if (__pred(*__first
))
239 if (__pred(*__first
))
244 switch (__last
- __first
)
247 if (__pred(*__first
))
251 if (__pred(*__first
))
255 if (__pred(*__first
))
264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
265 /// This is an overload used by find_if_not() for the Input Iterator case.
266 template<typename _InputIterator
, typename _Predicate
>
267 inline _InputIterator
268 __find_if_not(_InputIterator __first
, _InputIterator __last
,
269 _Predicate __pred
, input_iterator_tag
)
271 while (__first
!= __last
&& bool(__pred(*__first
)))
276 /// This is an overload used by find_if_not() for the RAI case.
277 template<typename _RandomAccessIterator
, typename _Predicate
>
278 _RandomAccessIterator
279 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
280 _Predicate __pred
, random_access_iterator_tag
)
282 typename iterator_traits
<_RandomAccessIterator
>::difference_type
283 __trip_count
= (__last
- __first
) >> 2;
285 for (; __trip_count
> 0; --__trip_count
)
287 if (!bool(__pred(*__first
)))
291 if (!bool(__pred(*__first
)))
295 if (!bool(__pred(*__first
)))
299 if (!bool(__pred(*__first
)))
304 switch (__last
- __first
)
307 if (!bool(__pred(*__first
)))
311 if (!bool(__pred(*__first
)))
315 if (!bool(__pred(*__first
)))
327 // set_symmetric_difference
339 * This is an uglified
340 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
341 * overloaded for forward iterators.
343 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
345 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
346 _Integer __count
, const _Tp
& __val
,
347 std::forward_iterator_tag
)
349 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
350 while (__first
!= __last
)
352 typename iterator_traits
<_ForwardIterator
>::difference_type
354 _ForwardIterator __i
= __first
;
356 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
365 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
371 * This is an uglified
372 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
373 * overloaded for random access iterators.
375 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
377 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
378 _Integer __count
, const _Tp
& __val
,
379 std::random_access_iterator_tag
)
382 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
385 _DistanceType __tailSize
= __last
- __first
;
386 const _DistanceType __pattSize
= __count
;
388 if (__tailSize
< __pattSize
)
391 const _DistanceType __skipOffset
= __pattSize
- 1;
392 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
393 __tailSize
-= __pattSize
;
395 while (1) // the main loop...
397 // __lookAhead here is always pointing to the last element of next
399 while (!(*__lookAhead
== __val
)) // the skip loop...
401 if (__tailSize
< __pattSize
)
402 return __last
; // Failure
403 __lookAhead
+= __pattSize
;
404 __tailSize
-= __pattSize
;
406 _DistanceType __remainder
= __skipOffset
;
407 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
408 *__backTrack
== __val
; --__backTrack
)
410 if (--__remainder
== 0)
411 return (__lookAhead
- __skipOffset
); // Success
413 if (__remainder
> __tailSize
)
414 return __last
; // Failure
415 __lookAhead
+= __remainder
;
416 __tailSize
-= __remainder
;
423 * This is an uglified
424 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
426 * overloaded for forward iterators.
428 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
429 typename _BinaryPredicate
>
431 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
432 _Integer __count
, const _Tp
& __val
,
433 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
435 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
438 while (__first
!= __last
)
440 typename iterator_traits
<_ForwardIterator
>::difference_type
442 _ForwardIterator __i
= __first
;
444 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
454 while (__first
!= __last
455 && !bool(__binary_pred(*__first
, __val
)))
462 * This is an uglified
463 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
465 * overloaded for random access iterators.
467 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
468 typename _BinaryPredicate
>
470 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
471 _Integer __count
, const _Tp
& __val
,
472 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
475 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
478 _DistanceType __tailSize
= __last
- __first
;
479 const _DistanceType __pattSize
= __count
;
481 if (__tailSize
< __pattSize
)
484 const _DistanceType __skipOffset
= __pattSize
- 1;
485 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
486 __tailSize
-= __pattSize
;
488 while (1) // the main loop...
490 // __lookAhead here is always pointing to the last element of next
492 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
494 if (__tailSize
< __pattSize
)
495 return __last
; // Failure
496 __lookAhead
+= __pattSize
;
497 __tailSize
-= __pattSize
;
499 _DistanceType __remainder
= __skipOffset
;
500 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
501 __binary_pred(*__backTrack
, __val
); --__backTrack
)
503 if (--__remainder
== 0)
504 return (__lookAhead
- __skipOffset
); // Success
506 if (__remainder
> __tailSize
)
507 return __last
; // Failure
508 __lookAhead
+= __remainder
;
509 __tailSize
-= __remainder
;
513 // find_end for forward iterators.
514 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
516 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
517 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
518 forward_iterator_tag
, forward_iterator_tag
)
520 if (__first2
== __last2
)
524 _ForwardIterator1 __result
= __last1
;
527 _ForwardIterator1 __new_result
528 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
529 if (__new_result
== __last1
)
533 __result
= __new_result
;
534 __first1
= __new_result
;
541 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
542 typename _BinaryPredicate
>
544 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
545 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
546 forward_iterator_tag
, forward_iterator_tag
,
547 _BinaryPredicate __comp
)
549 if (__first2
== __last2
)
553 _ForwardIterator1 __result
= __last1
;
556 _ForwardIterator1 __new_result
557 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
559 if (__new_result
== __last1
)
563 __result
= __new_result
;
564 __first1
= __new_result
;
571 // find_end for bidirectional iterators (much faster).
572 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
573 _BidirectionalIterator1
574 __find_end(_BidirectionalIterator1 __first1
,
575 _BidirectionalIterator1 __last1
,
576 _BidirectionalIterator2 __first2
,
577 _BidirectionalIterator2 __last2
,
578 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
580 // concept requirements
581 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
582 _BidirectionalIterator1
>)
583 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
584 _BidirectionalIterator2
>)
586 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
587 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
589 _RevIterator1
__rlast1(__first1
);
590 _RevIterator2
__rlast2(__first2
);
591 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
593 _RevIterator2(__last2
),
596 if (__rresult
== __rlast1
)
600 _BidirectionalIterator1 __result
= __rresult
.base();
601 std::advance(__result
, -std::distance(__first2
, __last2
));
606 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
607 typename _BinaryPredicate
>
608 _BidirectionalIterator1
609 __find_end(_BidirectionalIterator1 __first1
,
610 _BidirectionalIterator1 __last1
,
611 _BidirectionalIterator2 __first2
,
612 _BidirectionalIterator2 __last2
,
613 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
614 _BinaryPredicate __comp
)
616 // concept requirements
617 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
618 _BidirectionalIterator1
>)
619 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
620 _BidirectionalIterator2
>)
622 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
623 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
625 _RevIterator1
__rlast1(__first1
);
626 _RevIterator2
__rlast2(__first2
);
627 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
628 _RevIterator2(__last2
), __rlast2
,
631 if (__rresult
== __rlast1
)
635 _BidirectionalIterator1 __result
= __rresult
.base();
636 std::advance(__result
, -std::distance(__first2
, __last2
));
642 * @brief Find last matching subsequence in a sequence.
643 * @ingroup non_mutating_algorithms
644 * @param first1 Start of range to search.
645 * @param last1 End of range to search.
646 * @param first2 Start of sequence to match.
647 * @param last2 End of sequence to match.
648 * @return The last iterator @c i in the range
649 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
650 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
651 * such iterator exists.
653 * Searches the range @p [first1,last1) for a sub-sequence that compares
654 * equal value-by-value with the sequence given by @p [first2,last2) and
655 * returns an iterator to the first element of the sub-sequence, or
656 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
657 * last such subsequence contained in [first,last1).
659 * Because the sub-sequence must lie completely within the range
660 * @p [first1,last1) it must start at a position less than
661 * @p last1-(last2-first2) where @p last2-first2 is the length of the
663 * This means that the returned iterator @c i will be in the range
664 * @p [first1,last1-(last2-first2))
666 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
667 inline _ForwardIterator1
668 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
669 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
671 // concept requirements
672 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
673 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
674 __glibcxx_function_requires(_EqualOpConcept
<
675 typename iterator_traits
<_ForwardIterator1
>::value_type
,
676 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
677 __glibcxx_requires_valid_range(__first1
, __last1
);
678 __glibcxx_requires_valid_range(__first2
, __last2
);
680 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
681 std::__iterator_category(__first1
),
682 std::__iterator_category(__first2
));
686 * @brief Find last matching subsequence in a sequence using a predicate.
687 * @ingroup non_mutating_algorithms
688 * @param first1 Start of range to search.
689 * @param last1 End of range to search.
690 * @param first2 Start of sequence to match.
691 * @param last2 End of sequence to match.
692 * @param comp The predicate to use.
693 * @return The last iterator @c i in the range
694 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
695 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
696 * @p last1 if no such iterator exists.
698 * Searches the range @p [first1,last1) for a sub-sequence that compares
699 * equal value-by-value with the sequence given by @p [first2,last2) using
700 * comp as a predicate and returns an iterator to the first element of the
701 * sub-sequence, or @p last1 if the sub-sequence is not found. The
702 * sub-sequence will be the last such subsequence contained in
705 * Because the sub-sequence must lie completely within the range
706 * @p [first1,last1) it must start at a position less than
707 * @p last1-(last2-first2) where @p last2-first2 is the length of the
709 * This means that the returned iterator @c i will be in the range
710 * @p [first1,last1-(last2-first2))
712 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
713 typename _BinaryPredicate
>
714 inline _ForwardIterator1
715 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
716 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
717 _BinaryPredicate __comp
)
719 // concept requirements
720 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
721 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
722 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
723 typename iterator_traits
<_ForwardIterator1
>::value_type
,
724 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
725 __glibcxx_requires_valid_range(__first1
, __last1
);
726 __glibcxx_requires_valid_range(__first2
, __last2
);
728 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
729 std::__iterator_category(__first1
),
730 std::__iterator_category(__first2
),
734 #ifdef __GXX_EXPERIMENTAL_CXX0X__
736 * @brief Checks that a predicate is true for all the elements
738 * @ingroup non_mutating_algorithms
739 * @param first An input iterator.
740 * @param last An input iterator.
741 * @param pred A predicate.
742 * @return True if the check is true, false otherwise.
744 * Returns true if @p pred is true for each element in the range
745 * @p [first,last), and false otherwise.
747 template<typename _InputIterator
, typename _Predicate
>
749 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
750 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
753 * @brief Checks that a predicate is false for all the elements
755 * @ingroup non_mutating_algorithms
756 * @param first An input iterator.
757 * @param last An input iterator.
758 * @param pred A predicate.
759 * @return True if the check is true, false otherwise.
761 * Returns true if @p pred is false for each element in the range
762 * @p [first,last), and false otherwise.
764 template<typename _InputIterator
, typename _Predicate
>
766 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
767 { return __last
== _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
); }
770 * @brief Checks that a predicate is false for at least an element
772 * @ingroup non_mutating_algorithms
773 * @param first An input iterator.
774 * @param last An input iterator.
775 * @param pred A predicate.
776 * @return True if the check is true, false otherwise.
778 * Returns true if an element exists in the range @p [first,last) such that
779 * @p pred is true, and false otherwise.
781 template<typename _InputIterator
, typename _Predicate
>
783 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
784 { return !std::none_of(__first
, __last
, __pred
); }
787 * @brief Find the first element in a sequence for which a
788 * predicate is false.
789 * @ingroup non_mutating_algorithms
790 * @param first An input iterator.
791 * @param last An input iterator.
792 * @param pred A predicate.
793 * @return The first iterator @c i in the range @p [first,last)
794 * such that @p pred(*i) is false, or @p last if no such iterator exists.
796 template<typename _InputIterator
, typename _Predicate
>
797 inline _InputIterator
798 find_if_not(_InputIterator __first
, _InputIterator __last
,
801 // concept requirements
802 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
803 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
804 typename iterator_traits
<_InputIterator
>::value_type
>)
805 __glibcxx_requires_valid_range(__first
, __last
);
806 return std::__find_if_not(__first
, __last
, __pred
,
807 std::__iterator_category(__first
));
811 * @brief Checks whether the sequence is partitioned.
812 * @ingroup mutating_algorithms
813 * @param first An input iterator.
814 * @param last An input iterator.
815 * @param pred A predicate.
816 * @return True if the range @p [first,last) is partioned by @p pred,
817 * i.e. if all elements that satisfy @p pred appear before those that
820 template<typename _InputIterator
, typename _Predicate
>
822 is_partitioned(_InputIterator __first
, _InputIterator __last
,
825 __first
= std::find_if_not(__first
, __last
, __pred
);
826 return std::none_of(__first
, __last
, __pred
);
830 * @brief Find the partition point of a partitioned range.
831 * @ingroup mutating_algorithms
832 * @param first An iterator.
833 * @param last Another iterator.
834 * @param pred A predicate.
835 * @return An iterator @p mid such that @p all_of(first, mid, pred)
836 * and @p none_of(mid, last, pred) are both true.
838 template<typename _ForwardIterator
, typename _Predicate
>
840 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
843 // concept requirements
844 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
845 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
846 typename iterator_traits
<_ForwardIterator
>::value_type
>)
848 // A specific debug-mode test will be necessary...
849 __glibcxx_requires_valid_range(__first
, __last
);
851 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
854 _DistanceType __len
= std::distance(__first
, __last
);
855 _DistanceType __half
;
856 _ForwardIterator __middle
;
862 std::advance(__middle
, __half
);
863 if (__pred(*__middle
))
867 __len
= __len
- __half
- 1;
878 * @brief Copy a sequence, removing elements of a given value.
879 * @ingroup mutating_algorithms
880 * @param first An input iterator.
881 * @param last An input iterator.
882 * @param result An output iterator.
883 * @param value The value to be removed.
884 * @return An iterator designating the end of the resulting sequence.
886 * Copies each element in the range @p [first,last) not equal to @p value
887 * to the range beginning at @p result.
888 * remove_copy() is stable, so the relative order of elements that are
889 * copied is unchanged.
891 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
893 remove_copy(_InputIterator __first
, _InputIterator __last
,
894 _OutputIterator __result
, const _Tp
& __value
)
896 // concept requirements
897 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
898 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
899 typename iterator_traits
<_InputIterator
>::value_type
>)
900 __glibcxx_function_requires(_EqualOpConcept
<
901 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
902 __glibcxx_requires_valid_range(__first
, __last
);
904 for (; __first
!= __last
; ++__first
)
905 if (!(*__first
== __value
))
907 *__result
= *__first
;
914 * @brief Copy a sequence, removing elements for which a predicate is true.
915 * @ingroup mutating_algorithms
916 * @param first An input iterator.
917 * @param last An input iterator.
918 * @param result An output iterator.
919 * @param pred A predicate.
920 * @return An iterator designating the end of the resulting sequence.
922 * Copies each element in the range @p [first,last) for which
923 * @p pred returns false to the range beginning at @p result.
925 * remove_copy_if() is stable, so the relative order of elements that are
926 * copied is unchanged.
928 template<typename _InputIterator
, typename _OutputIterator
,
931 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
932 _OutputIterator __result
, _Predicate __pred
)
934 // concept requirements
935 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
936 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
937 typename iterator_traits
<_InputIterator
>::value_type
>)
938 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
939 typename iterator_traits
<_InputIterator
>::value_type
>)
940 __glibcxx_requires_valid_range(__first
, __last
);
942 for (; __first
!= __last
; ++__first
)
943 if (!bool(__pred(*__first
)))
945 *__result
= *__first
;
951 #ifdef __GXX_EXPERIMENTAL_CXX0X__
953 * @brief Copy the elements of a sequence for which a predicate is true.
954 * @ingroup mutating_algorithms
955 * @param first An input iterator.
956 * @param last An input iterator.
957 * @param result An output iterator.
958 * @param pred A predicate.
959 * @return An iterator designating the end of the resulting sequence.
961 * Copies each element in the range @p [first,last) for which
962 * @p pred returns true to the range beginning at @p result.
964 * copy_if() is stable, so the relative order of elements that are
965 * copied is unchanged.
967 template<typename _InputIterator
, typename _OutputIterator
,
970 copy_if(_InputIterator __first
, _InputIterator __last
,
971 _OutputIterator __result
, _Predicate __pred
)
973 // concept requirements
974 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
975 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
976 typename iterator_traits
<_InputIterator
>::value_type
>)
977 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
978 typename iterator_traits
<_InputIterator
>::value_type
>)
979 __glibcxx_requires_valid_range(__first
, __last
);
981 for (; __first
!= __last
; ++__first
)
982 if (__pred(*__first
))
984 *__result
= *__first
;
991 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
993 __copy_n(_InputIterator __first
, _Size __n
,
994 _OutputIterator __result
, input_iterator_tag
)
996 for (; __n
> 0; --__n
)
998 *__result
= *__first
;
1005 template<typename _RandomAccessIterator
, typename _Size
,
1006 typename _OutputIterator
>
1007 inline _OutputIterator
1008 __copy_n(_RandomAccessIterator __first
, _Size __n
,
1009 _OutputIterator __result
, random_access_iterator_tag
)
1010 { return std::copy(__first
, __first
+ __n
, __result
); }
1013 * @brief Copies the range [first,first+n) into [result,result+n).
1014 * @ingroup mutating_algorithms
1015 * @param first An input iterator.
1016 * @param n The number of elements to copy.
1017 * @param result An output iterator.
1020 * This inline function will boil down to a call to @c memmove whenever
1021 * possible. Failing that, if random access iterators are passed, then the
1022 * loop count will be known (and therefore a candidate for compiler
1023 * optimizations such as unrolling).
1025 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1026 inline _OutputIterator
1027 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1029 // concept requirements
1030 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1031 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1032 typename iterator_traits
<_InputIterator
>::value_type
>)
1034 return std::__copy_n(__first
, __n
, __result
,
1035 std::__iterator_category(__first
));
1039 * @brief Copy the elements of a sequence to separate output sequences
1040 * depending on the truth value of a predicate.
1041 * @ingroup mutating_algorithms
1042 * @param first An input iterator.
1043 * @param last An input iterator.
1044 * @param out_true An output iterator.
1045 * @param out_false An output iterator.
1046 * @param pred A predicate.
1047 * @return A pair designating the ends of the resulting sequences.
1049 * Copies each element in the range @p [first,last) for which
1050 * @p pred returns true to the range beginning at @p out_true
1051 * and each element for which @p pred returns false to @p out_false.
1053 template<typename _InputIterator
, typename _OutputIterator1
,
1054 typename _OutputIterator2
, typename _Predicate
>
1055 pair
<_OutputIterator1
, _OutputIterator2
>
1056 partition_copy(_InputIterator __first
, _InputIterator __last
,
1057 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1060 // concept requirements
1061 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1062 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1063 typename iterator_traits
<_InputIterator
>::value_type
>)
1064 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1065 typename iterator_traits
<_InputIterator
>::value_type
>)
1066 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1067 typename iterator_traits
<_InputIterator
>::value_type
>)
1068 __glibcxx_requires_valid_range(__first
, __last
);
1070 for (; __first
!= __last
; ++__first
)
1071 if (__pred(*__first
))
1073 *__out_true
= *__first
;
1078 *__out_false
= *__first
;
1082 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1087 * @brief Remove elements from a sequence.
1088 * @ingroup mutating_algorithms
1089 * @param first An input iterator.
1090 * @param last An input iterator.
1091 * @param value The value to be removed.
1092 * @return An iterator designating the end of the resulting sequence.
1094 * All elements equal to @p value are removed from the range
1097 * remove() is stable, so the relative order of elements that are
1098 * not removed is unchanged.
1100 * Elements between the end of the resulting sequence and @p last
1101 * are still present, but their value is unspecified.
1103 template<typename _ForwardIterator
, typename _Tp
>
1105 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1108 // concept requirements
1109 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1111 __glibcxx_function_requires(_EqualOpConcept
<
1112 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1113 __glibcxx_requires_valid_range(__first
, __last
);
1115 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1116 if(__first
== __last
)
1118 _ForwardIterator __result
= __first
;
1120 for(; __first
!= __last
; ++__first
)
1121 if(!(*__first
== __value
))
1123 *__result
= _GLIBCXX_MOVE(*__first
);
1130 * @brief Remove elements from a sequence using a predicate.
1131 * @ingroup mutating_algorithms
1132 * @param first A forward iterator.
1133 * @param last A forward iterator.
1134 * @param pred A predicate.
1135 * @return An iterator designating the end of the resulting sequence.
1137 * All elements for which @p pred returns true are removed from the range
1140 * remove_if() is stable, so the relative order of elements that are
1141 * not removed is unchanged.
1143 * Elements between the end of the resulting sequence and @p last
1144 * are still present, but their value is unspecified.
1146 template<typename _ForwardIterator
, typename _Predicate
>
1148 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1151 // concept requirements
1152 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1154 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1155 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1156 __glibcxx_requires_valid_range(__first
, __last
);
1158 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1159 if(__first
== __last
)
1161 _ForwardIterator __result
= __first
;
1163 for(; __first
!= __last
; ++__first
)
1164 if(!bool(__pred(*__first
)))
1166 *__result
= _GLIBCXX_MOVE(*__first
);
1173 * @brief Remove consecutive duplicate values from a sequence.
1174 * @ingroup mutating_algorithms
1175 * @param first A forward iterator.
1176 * @param last A forward iterator.
1177 * @return An iterator designating the end of the resulting sequence.
1179 * Removes all but the first element from each group of consecutive
1180 * values that compare equal.
1181 * unique() is stable, so the relative order of elements that are
1182 * not removed is unchanged.
1183 * Elements between the end of the resulting sequence and @p last
1184 * are still present, but their value is unspecified.
1186 template<typename _ForwardIterator
>
1188 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1190 // concept requirements
1191 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1193 __glibcxx_function_requires(_EqualityComparableConcept
<
1194 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1195 __glibcxx_requires_valid_range(__first
, __last
);
1197 // Skip the beginning, if already unique.
1198 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1199 if (__first
== __last
)
1202 // Do the real copy work.
1203 _ForwardIterator __dest
= __first
;
1205 while (++__first
!= __last
)
1206 if (!(*__dest
== *__first
))
1207 *++__dest
= _GLIBCXX_MOVE(*__first
);
1212 * @brief Remove consecutive values from a sequence using a predicate.
1213 * @ingroup mutating_algorithms
1214 * @param first A forward iterator.
1215 * @param last A forward iterator.
1216 * @param binary_pred A binary predicate.
1217 * @return An iterator designating the end of the resulting sequence.
1219 * Removes all but the first element from each group of consecutive
1220 * values for which @p binary_pred returns true.
1221 * unique() is stable, so the relative order of elements that are
1222 * not removed is unchanged.
1223 * Elements between the end of the resulting sequence and @p last
1224 * are still present, but their value is unspecified.
1226 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1228 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1229 _BinaryPredicate __binary_pred
)
1231 // concept requirements
1232 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1234 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1235 typename iterator_traits
<_ForwardIterator
>::value_type
,
1236 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1237 __glibcxx_requires_valid_range(__first
, __last
);
1239 // Skip the beginning, if already unique.
1240 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1241 if (__first
== __last
)
1244 // Do the real copy work.
1245 _ForwardIterator __dest
= __first
;
1247 while (++__first
!= __last
)
1248 if (!bool(__binary_pred(*__dest
, *__first
)))
1249 *++__dest
= _GLIBCXX_MOVE(*__first
);
1254 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1256 * overloaded for forward iterators and output iterator as result.
1258 template<typename _ForwardIterator
, typename _OutputIterator
>
1260 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1261 _OutputIterator __result
,
1262 forward_iterator_tag
, output_iterator_tag
)
1264 // concept requirements -- taken care of in dispatching function
1265 _ForwardIterator __next
= __first
;
1266 *__result
= *__first
;
1267 while (++__next
!= __last
)
1268 if (!(*__first
== *__next
))
1271 *++__result
= *__first
;
1277 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1279 * overloaded for input iterators and output iterator as result.
1281 template<typename _InputIterator
, typename _OutputIterator
>
1283 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1284 _OutputIterator __result
,
1285 input_iterator_tag
, output_iterator_tag
)
1287 // concept requirements -- taken care of in dispatching function
1288 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1289 *__result
= __value
;
1290 while (++__first
!= __last
)
1291 if (!(__value
== *__first
))
1294 *++__result
= __value
;
1300 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1302 * overloaded for input iterators and forward iterator as result.
1304 template<typename _InputIterator
, typename _ForwardIterator
>
1306 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1307 _ForwardIterator __result
,
1308 input_iterator_tag
, forward_iterator_tag
)
1310 // concept requirements -- taken care of in dispatching function
1311 *__result
= *__first
;
1312 while (++__first
!= __last
)
1313 if (!(*__result
== *__first
))
1314 *++__result
= *__first
;
1319 * This is an uglified
1320 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1322 * overloaded for forward iterators and output iterator as result.
1324 template<typename _ForwardIterator
, typename _OutputIterator
,
1325 typename _BinaryPredicate
>
1327 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1328 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1329 forward_iterator_tag
, output_iterator_tag
)
1331 // concept requirements -- iterators already checked
1332 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1333 typename iterator_traits
<_ForwardIterator
>::value_type
,
1334 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1336 _ForwardIterator __next
= __first
;
1337 *__result
= *__first
;
1338 while (++__next
!= __last
)
1339 if (!bool(__binary_pred(*__first
, *__next
)))
1342 *++__result
= *__first
;
1348 * This is an uglified
1349 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1351 * overloaded for input iterators and output iterator as result.
1353 template<typename _InputIterator
, typename _OutputIterator
,
1354 typename _BinaryPredicate
>
1356 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1357 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1358 input_iterator_tag
, output_iterator_tag
)
1360 // concept requirements -- iterators already checked
1361 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1362 typename iterator_traits
<_InputIterator
>::value_type
,
1363 typename iterator_traits
<_InputIterator
>::value_type
>)
1365 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1366 *__result
= __value
;
1367 while (++__first
!= __last
)
1368 if (!bool(__binary_pred(__value
, *__first
)))
1371 *++__result
= __value
;
1377 * This is an uglified
1378 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1380 * overloaded for input iterators and forward iterator as result.
1382 template<typename _InputIterator
, typename _ForwardIterator
,
1383 typename _BinaryPredicate
>
1385 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1386 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1387 input_iterator_tag
, forward_iterator_tag
)
1389 // concept requirements -- iterators already checked
1390 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1391 typename iterator_traits
<_ForwardIterator
>::value_type
,
1392 typename iterator_traits
<_InputIterator
>::value_type
>)
1394 *__result
= *__first
;
1395 while (++__first
!= __last
)
1396 if (!bool(__binary_pred(*__result
, *__first
)))
1397 *++__result
= *__first
;
1402 * This is an uglified reverse(_BidirectionalIterator,
1403 * _BidirectionalIterator)
1404 * overloaded for bidirectional iterators.
1406 template<typename _BidirectionalIterator
>
1408 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1409 bidirectional_iterator_tag
)
1412 if (__first
== __last
|| __first
== --__last
)
1416 std::iter_swap(__first
, __last
);
1422 * This is an uglified reverse(_BidirectionalIterator,
1423 * _BidirectionalIterator)
1424 * overloaded for random access iterators.
1426 template<typename _RandomAccessIterator
>
1428 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1429 random_access_iterator_tag
)
1431 if (__first
== __last
)
1434 while (__first
< __last
)
1436 std::iter_swap(__first
, __last
);
1443 * @brief Reverse a sequence.
1444 * @ingroup mutating_algorithms
1445 * @param first A bidirectional iterator.
1446 * @param last A bidirectional iterator.
1447 * @return reverse() returns no value.
1449 * Reverses the order of the elements in the range @p [first,last),
1450 * so that the first element becomes the last etc.
1451 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1452 * swaps @p *(first+i) and @p *(last-(i+1))
1454 template<typename _BidirectionalIterator
>
1456 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1458 // concept requirements
1459 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1460 _BidirectionalIterator
>)
1461 __glibcxx_requires_valid_range(__first
, __last
);
1462 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1466 * @brief Copy a sequence, reversing its elements.
1467 * @ingroup mutating_algorithms
1468 * @param first A bidirectional iterator.
1469 * @param last A bidirectional iterator.
1470 * @param result An output iterator.
1471 * @return An iterator designating the end of the resulting sequence.
1473 * Copies the elements in the range @p [first,last) to the range
1474 * @p [result,result+(last-first)) such that the order of the
1475 * elements is reversed.
1476 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1477 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1478 * The ranges @p [first,last) and @p [result,result+(last-first))
1481 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1483 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1484 _OutputIterator __result
)
1486 // concept requirements
1487 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1488 _BidirectionalIterator
>)
1489 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1490 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1491 __glibcxx_requires_valid_range(__first
, __last
);
1493 while (__first
!= __last
)
1496 *__result
= *__last
;
1503 * This is a helper function for the rotate algorithm specialized on RAIs.
1504 * It returns the greatest common divisor of two integer values.
1506 template<typename _EuclideanRingElement
>
1507 _EuclideanRingElement
1508 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1512 _EuclideanRingElement __t
= __m
% __n
;
1519 /// This is a helper function for the rotate algorithm.
1520 template<typename _ForwardIterator
>
1522 __rotate(_ForwardIterator __first
,
1523 _ForwardIterator __middle
,
1524 _ForwardIterator __last
,
1525 forward_iterator_tag
)
1527 if (__first
== __middle
|| __last
== __middle
)
1530 _ForwardIterator __first2
= __middle
;
1533 std::iter_swap(__first
, __first2
);
1536 if (__first
== __middle
)
1537 __middle
= __first2
;
1539 while (__first2
!= __last
);
1541 __first2
= __middle
;
1543 while (__first2
!= __last
)
1545 std::iter_swap(__first
, __first2
);
1548 if (__first
== __middle
)
1549 __middle
= __first2
;
1550 else if (__first2
== __last
)
1551 __first2
= __middle
;
1555 /// This is a helper function for the rotate algorithm.
1556 template<typename _BidirectionalIterator
>
1558 __rotate(_BidirectionalIterator __first
,
1559 _BidirectionalIterator __middle
,
1560 _BidirectionalIterator __last
,
1561 bidirectional_iterator_tag
)
1563 // concept requirements
1564 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1565 _BidirectionalIterator
>)
1567 if (__first
== __middle
|| __last
== __middle
)
1570 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1571 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1573 while (__first
!= __middle
&& __middle
!= __last
)
1575 std::iter_swap(__first
, --__last
);
1579 if (__first
== __middle
)
1580 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1582 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1585 /// This is a helper function for the rotate algorithm.
1586 template<typename _RandomAccessIterator
>
1588 __rotate(_RandomAccessIterator __first
,
1589 _RandomAccessIterator __middle
,
1590 _RandomAccessIterator __last
,
1591 random_access_iterator_tag
)
1593 // concept requirements
1594 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1595 _RandomAccessIterator
>)
1597 if (__first
== __middle
|| __last
== __middle
)
1600 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1602 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1605 const _Distance __n
= __last
- __first
;
1606 const _Distance __k
= __middle
- __first
;
1607 const _Distance __l
= __n
- __k
;
1611 std::swap_ranges(__first
, __middle
, __middle
);
1615 const _Distance __d
= std::__gcd(__n
, __k
);
1617 for (_Distance __i
= 0; __i
< __d
; __i
++)
1619 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1620 _RandomAccessIterator __p
= __first
;
1624 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1626 if (__p
> __first
+ __l
)
1628 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1632 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1638 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1640 if (__p
< __last
- __k
)
1642 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1645 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1650 *__p
= _GLIBCXX_MOVE(__tmp
);
1656 * @brief Rotate the elements of a sequence.
1657 * @ingroup mutating_algorithms
1658 * @param first A forward iterator.
1659 * @param middle A forward iterator.
1660 * @param last A forward iterator.
1663 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1664 * positions so that the element at @p middle is moved to @p first, the
1665 * element at @p middle+1 is moved to @first+1 and so on for each element
1666 * in the range @p [first,last).
1668 * This effectively swaps the ranges @p [first,middle) and
1671 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1672 * each @p n in the range @p [0,last-first).
1674 template<typename _ForwardIterator
>
1676 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1677 _ForwardIterator __last
)
1679 // concept requirements
1680 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1682 __glibcxx_requires_valid_range(__first
, __middle
);
1683 __glibcxx_requires_valid_range(__middle
, __last
);
1685 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1687 std::__rotate(__first
, __middle
, __last
, _IterType());
1691 * @brief Copy a sequence, rotating its elements.
1692 * @ingroup mutating_algorithms
1693 * @param first A forward iterator.
1694 * @param middle A forward iterator.
1695 * @param last A forward iterator.
1696 * @param result An output iterator.
1697 * @return An iterator designating the end of the resulting sequence.
1699 * Copies the elements of the range @p [first,last) to the range
1700 * beginning at @result, rotating the copied elements by @p (middle-first)
1701 * positions so that the element at @p middle is moved to @p result, the
1702 * element at @p middle+1 is moved to @result+1 and so on for each element
1703 * in the range @p [first,last).
1705 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1706 * each @p n in the range @p [0,last-first).
1708 template<typename _ForwardIterator
, typename _OutputIterator
>
1710 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1711 _ForwardIterator __last
, _OutputIterator __result
)
1713 // concept requirements
1714 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1715 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1716 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1717 __glibcxx_requires_valid_range(__first
, __middle
);
1718 __glibcxx_requires_valid_range(__middle
, __last
);
1720 return std::copy(__first
, __middle
,
1721 std::copy(__middle
, __last
, __result
));
1724 /// This is a helper function...
1725 template<typename _ForwardIterator
, typename _Predicate
>
1727 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1728 _Predicate __pred
, forward_iterator_tag
)
1730 if (__first
== __last
)
1733 while (__pred(*__first
))
1734 if (++__first
== __last
)
1737 _ForwardIterator __next
= __first
;
1739 while (++__next
!= __last
)
1740 if (__pred(*__next
))
1742 std::iter_swap(__first
, __next
);
1749 /// This is a helper function...
1750 template<typename _BidirectionalIterator
, typename _Predicate
>
1751 _BidirectionalIterator
1752 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1753 _Predicate __pred
, bidirectional_iterator_tag
)
1758 if (__first
== __last
)
1760 else if (__pred(*__first
))
1766 if (__first
== __last
)
1768 else if (!bool(__pred(*__last
)))
1772 std::iter_swap(__first
, __last
);
1779 /// This is a helper function...
1780 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1782 __inplace_stable_partition(_ForwardIterator __first
,
1783 _ForwardIterator __last
,
1784 _Predicate __pred
, _Distance __len
)
1787 return __pred(*__first
) ? __last
: __first
;
1788 _ForwardIterator __middle
= __first
;
1789 std::advance(__middle
, __len
/ 2);
1790 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1794 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1798 std::rotate(__begin
, __middle
, __end
);
1799 std::advance(__begin
, std::distance(__middle
, __end
));
1803 /// This is a helper function...
1804 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1807 __stable_partition_adaptive(_ForwardIterator __first
,
1808 _ForwardIterator __last
,
1809 _Predicate __pred
, _Distance __len
,
1811 _Distance __buffer_size
)
1813 if (__len
<= __buffer_size
)
1815 _ForwardIterator __result1
= __first
;
1816 _Pointer __result2
= __buffer
;
1817 for (; __first
!= __last
; ++__first
)
1818 if (__pred(*__first
))
1820 *__result1
= *__first
;
1825 *__result2
= *__first
;
1828 std::copy(__buffer
, __result2
, __result1
);
1833 _ForwardIterator __middle
= __first
;
1834 std::advance(__middle
, __len
/ 2);
1835 _ForwardIterator __begin
=
1836 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1837 __len
/ 2, __buffer
,
1839 _ForwardIterator __end
=
1840 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1842 __buffer
, __buffer_size
);
1843 std::rotate(__begin
, __middle
, __end
);
1844 std::advance(__begin
, std::distance(__middle
, __end
));
1850 * @brief Move elements for which a predicate is true to the beginning
1851 * of a sequence, preserving relative ordering.
1852 * @ingroup mutating_algorithms
1853 * @param first A forward iterator.
1854 * @param last A forward iterator.
1855 * @param pred A predicate functor.
1856 * @return An iterator @p middle such that @p pred(i) is true for each
1857 * iterator @p i in the range @p [first,middle) and false for each @p i
1858 * in the range @p [middle,last).
1860 * Performs the same function as @p partition() with the additional
1861 * guarantee that the relative ordering of elements in each group is
1862 * preserved, so any two elements @p x and @p y in the range
1863 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1864 * relative ordering after calling @p stable_partition().
1866 template<typename _ForwardIterator
, typename _Predicate
>
1868 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1871 // concept requirements
1872 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1874 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1875 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1876 __glibcxx_requires_valid_range(__first
, __last
);
1878 if (__first
== __last
)
1882 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1884 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1887 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1889 if (__buf
.size() > 0)
1891 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1892 _DistanceType(__buf
.requested_size()),
1894 _DistanceType(__buf
.size()));
1897 std::__inplace_stable_partition(__first
, __last
, __pred
,
1898 _DistanceType(__buf
.requested_size()));
1902 /// This is a helper function for the sort routines.
1903 template<typename _RandomAccessIterator
>
1905 __heap_select(_RandomAccessIterator __first
,
1906 _RandomAccessIterator __middle
,
1907 _RandomAccessIterator __last
)
1909 std::make_heap(__first
, __middle
);
1910 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1911 if (*__i
< *__first
)
1912 std::__pop_heap(__first
, __middle
, __i
);
1915 /// This is a helper function for the sort routines.
1916 template<typename _RandomAccessIterator
, typename _Compare
>
1918 __heap_select(_RandomAccessIterator __first
,
1919 _RandomAccessIterator __middle
,
1920 _RandomAccessIterator __last
, _Compare __comp
)
1922 std::make_heap(__first
, __middle
, __comp
);
1923 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1924 if (__comp(*__i
, *__first
))
1925 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1931 * @brief Copy the smallest elements of a sequence.
1932 * @ingroup sorting_algorithms
1933 * @param first An iterator.
1934 * @param last Another iterator.
1935 * @param result_first A random-access iterator.
1936 * @param result_last Another random-access iterator.
1937 * @return An iterator indicating the end of the resulting sequence.
1939 * Copies and sorts the smallest N values from the range @p [first,last)
1940 * to the range beginning at @p result_first, where the number of
1941 * elements to be copied, @p N, is the smaller of @p (last-first) and
1942 * @p (result_last-result_first).
1943 * After the sort if @p i and @j are iterators in the range
1944 * @p [result_first,result_first+N) such that @i precedes @j then
1945 * @p *j<*i is false.
1946 * The value returned is @p result_first+N.
1948 template<typename _InputIterator
, typename _RandomAccessIterator
>
1949 _RandomAccessIterator
1950 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1951 _RandomAccessIterator __result_first
,
1952 _RandomAccessIterator __result_last
)
1954 typedef typename iterator_traits
<_InputIterator
>::value_type
1956 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1958 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1961 // concept requirements
1962 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1963 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1965 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1967 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1968 __glibcxx_requires_valid_range(__first
, __last
);
1969 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1971 if (__result_first
== __result_last
)
1972 return __result_last
;
1973 _RandomAccessIterator __result_real_last
= __result_first
;
1974 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1976 *__result_real_last
= *__first
;
1977 ++__result_real_last
;
1980 std::make_heap(__result_first
, __result_real_last
);
1981 while (__first
!= __last
)
1983 if (*__first
< *__result_first
)
1984 std::__adjust_heap(__result_first
, _DistanceType(0),
1985 _DistanceType(__result_real_last
1987 _InputValueType(*__first
));
1990 std::sort_heap(__result_first
, __result_real_last
);
1991 return __result_real_last
;
1995 * @brief Copy the smallest elements of a sequence using a predicate for
1997 * @ingroup sorting_algorithms
1998 * @param first An input iterator.
1999 * @param last Another input iterator.
2000 * @param result_first A random-access iterator.
2001 * @param result_last Another random-access iterator.
2002 * @param comp A comparison functor.
2003 * @return An iterator indicating the end of the resulting sequence.
2005 * Copies and sorts the smallest N values from the range @p [first,last)
2006 * to the range beginning at @p result_first, where the number of
2007 * elements to be copied, @p N, is the smaller of @p (last-first) and
2008 * @p (result_last-result_first).
2009 * After the sort if @p i and @j are iterators in the range
2010 * @p [result_first,result_first+N) such that @i precedes @j then
2011 * @p comp(*j,*i) is false.
2012 * The value returned is @p result_first+N.
2014 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2015 _RandomAccessIterator
2016 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2017 _RandomAccessIterator __result_first
,
2018 _RandomAccessIterator __result_last
,
2021 typedef typename iterator_traits
<_InputIterator
>::value_type
2023 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2025 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2028 // concept requirements
2029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2030 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2031 _RandomAccessIterator
>)
2032 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2034 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2035 _InputValueType
, _OutputValueType
>)
2036 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2037 _OutputValueType
, _OutputValueType
>)
2038 __glibcxx_requires_valid_range(__first
, __last
);
2039 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2041 if (__result_first
== __result_last
)
2042 return __result_last
;
2043 _RandomAccessIterator __result_real_last
= __result_first
;
2044 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2046 *__result_real_last
= *__first
;
2047 ++__result_real_last
;
2050 std::make_heap(__result_first
, __result_real_last
, __comp
);
2051 while (__first
!= __last
)
2053 if (__comp(*__first
, *__result_first
))
2054 std::__adjust_heap(__result_first
, _DistanceType(0),
2055 _DistanceType(__result_real_last
2057 _InputValueType(*__first
),
2061 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2062 return __result_real_last
;
2065 /// This is a helper function for the sort routine.
2066 template<typename _RandomAccessIterator
, typename _Tp
>
2068 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2070 _RandomAccessIterator __next
= __last
;
2072 while (__val
< *__next
)
2081 /// This is a helper function for the sort routine.
2082 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2084 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2087 _RandomAccessIterator __next
= __last
;
2089 while (__comp(__val
, *__next
))
2098 /// This is a helper function for the sort routine.
2099 template<typename _RandomAccessIterator
>
2101 __insertion_sort(_RandomAccessIterator __first
,
2102 _RandomAccessIterator __last
)
2104 if (__first
== __last
)
2107 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2109 typename iterator_traits
<_RandomAccessIterator
>::value_type
2111 if (__val
< *__first
)
2113 std::copy_backward(__first
, __i
, __i
+ 1);
2117 std::__unguarded_linear_insert(__i
, __val
);
2121 /// This is a helper function for the sort routine.
2122 template<typename _RandomAccessIterator
, typename _Compare
>
2124 __insertion_sort(_RandomAccessIterator __first
,
2125 _RandomAccessIterator __last
, _Compare __comp
)
2127 if (__first
== __last
) return;
2129 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2131 typename iterator_traits
<_RandomAccessIterator
>::value_type
2133 if (__comp(__val
, *__first
))
2135 std::copy_backward(__first
, __i
, __i
+ 1);
2139 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2143 /// This is a helper function for the sort routine.
2144 template<typename _RandomAccessIterator
>
2146 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2147 _RandomAccessIterator __last
)
2149 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2152 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2153 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2156 /// This is a helper function for the sort routine.
2157 template<typename _RandomAccessIterator
, typename _Compare
>
2159 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2160 _RandomAccessIterator __last
, _Compare __comp
)
2162 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2165 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2166 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2171 * This controls some aspect of the sort routines.
2173 enum { _S_threshold
= 16 };
2175 /// This is a helper function for the sort routine.
2176 template<typename _RandomAccessIterator
>
2178 __final_insertion_sort(_RandomAccessIterator __first
,
2179 _RandomAccessIterator __last
)
2181 if (__last
- __first
> int(_S_threshold
))
2183 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2184 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2187 std::__insertion_sort(__first
, __last
);
2190 /// This is a helper function for the sort routine.
2191 template<typename _RandomAccessIterator
, typename _Compare
>
2193 __final_insertion_sort(_RandomAccessIterator __first
,
2194 _RandomAccessIterator __last
, _Compare __comp
)
2196 if (__last
- __first
> int(_S_threshold
))
2198 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2199 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2203 std::__insertion_sort(__first
, __last
, __comp
);
2206 /// This is a helper function...
2207 template<typename _RandomAccessIterator
, typename _Tp
>
2208 _RandomAccessIterator
2209 __unguarded_partition(_RandomAccessIterator __first
,
2210 _RandomAccessIterator __last
, _Tp __pivot
)
2214 while (*__first
< __pivot
)
2217 while (__pivot
< *__last
)
2219 if (!(__first
< __last
))
2221 std::iter_swap(__first
, __last
);
2226 /// This is a helper function...
2227 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2228 _RandomAccessIterator
2229 __unguarded_partition(_RandomAccessIterator __first
,
2230 _RandomAccessIterator __last
,
2231 _Tp __pivot
, _Compare __comp
)
2235 while (__comp(*__first
, __pivot
))
2238 while (__comp(__pivot
, *__last
))
2240 if (!(__first
< __last
))
2242 std::iter_swap(__first
, __last
);
2247 /// This is a helper function for the sort routine.
2248 template<typename _RandomAccessIterator
, typename _Size
>
2250 __introsort_loop(_RandomAccessIterator __first
,
2251 _RandomAccessIterator __last
,
2252 _Size __depth_limit
)
2254 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2257 while (__last
- __first
> int(_S_threshold
))
2259 if (__depth_limit
== 0)
2261 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2265 _RandomAccessIterator __cut
=
2266 std::__unguarded_partition(__first
, __last
,
2267 _ValueType(std::__median(*__first
,
2274 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2279 /// This is a helper function for the sort routine.
2280 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2282 __introsort_loop(_RandomAccessIterator __first
,
2283 _RandomAccessIterator __last
,
2284 _Size __depth_limit
, _Compare __comp
)
2286 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2289 while (__last
- __first
> int(_S_threshold
))
2291 if (__depth_limit
== 0)
2293 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2297 _RandomAccessIterator __cut
=
2298 std::__unguarded_partition(__first
, __last
,
2299 _ValueType(std::__median(*__first
,
2307 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2312 /// This is a helper function for the sort routines. Precondition: __n > 0.
2313 template<typename _Size
>
2318 for (__k
= 0; __n
!= 0; __n
>>= 1)
2325 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2329 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2333 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2337 template<typename _RandomAccessIterator
, typename _Size
>
2339 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2340 _RandomAccessIterator __last
, _Size __depth_limit
)
2342 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2345 while (__last
- __first
> 3)
2347 if (__depth_limit
== 0)
2349 std::__heap_select(__first
, __nth
+ 1, __last
);
2351 // Place the nth largest element in its final position.
2352 std::iter_swap(__first
, __nth
);
2356 _RandomAccessIterator __cut
=
2357 std::__unguarded_partition(__first
, __last
,
2358 _ValueType(std::__median(*__first
,
2370 std::__insertion_sort(__first
, __last
);
2373 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2375 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2376 _RandomAccessIterator __last
, _Size __depth_limit
,
2379 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2382 while (__last
- __first
> 3)
2384 if (__depth_limit
== 0)
2386 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2387 // Place the nth largest element in its final position.
2388 std::iter_swap(__first
, __nth
);
2392 _RandomAccessIterator __cut
=
2393 std::__unguarded_partition(__first
, __last
,
2394 _ValueType(std::__median(*__first
,
2407 std::__insertion_sort(__first
, __last
, __comp
);
2413 * @brief Finds the first position in which @a val could be inserted
2414 * without changing the ordering.
2415 * @param first An iterator.
2416 * @param last Another iterator.
2417 * @param val The search term.
2418 * @return An iterator pointing to the first element "not less
2419 * than" @a val, or end() if every element is less than
2421 * @ingroup binary_search_algorithms
2423 template<typename _ForwardIterator
, typename _Tp
>
2425 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2428 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2430 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2433 // concept requirements
2434 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2435 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2436 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2438 _DistanceType __len
= std::distance(__first
, __last
);
2439 _DistanceType __half
;
2440 _ForwardIterator __middle
;
2444 __half
= __len
>> 1;
2446 std::advance(__middle
, __half
);
2447 if (*__middle
< __val
)
2451 __len
= __len
- __half
- 1;
2460 * @brief Finds the first position in which @a val could be inserted
2461 * without changing the ordering.
2462 * @ingroup binary_search_algorithms
2463 * @param first An iterator.
2464 * @param last Another iterator.
2465 * @param val The search term.
2466 * @param comp A functor to use for comparisons.
2467 * @return An iterator pointing to the first element "not less than" @a val,
2468 * or end() if every element is less than @a val.
2469 * @ingroup binary_search_algorithms
2471 * The comparison function should have the same effects on ordering as
2472 * the function used for the initial sort.
2474 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2476 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2477 const _Tp
& __val
, _Compare __comp
)
2479 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2481 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2484 // concept requirements
2485 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2486 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2488 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2491 _DistanceType __len
= std::distance(__first
, __last
);
2492 _DistanceType __half
;
2493 _ForwardIterator __middle
;
2497 __half
= __len
>> 1;
2499 std::advance(__middle
, __half
);
2500 if (__comp(*__middle
, __val
))
2504 __len
= __len
- __half
- 1;
2513 * @brief Finds the last position in which @a val could be inserted
2514 * without changing the ordering.
2515 * @ingroup binary_search_algorithms
2516 * @param first An iterator.
2517 * @param last Another iterator.
2518 * @param val The search term.
2519 * @return An iterator pointing to the first element greater than @a val,
2520 * or end() if no elements are greater than @a val.
2521 * @ingroup binary_search_algorithms
2523 template<typename _ForwardIterator
, typename _Tp
>
2525 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2528 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2530 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2533 // concept requirements
2534 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2535 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2536 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2538 _DistanceType __len
= std::distance(__first
, __last
);
2539 _DistanceType __half
;
2540 _ForwardIterator __middle
;
2544 __half
= __len
>> 1;
2546 std::advance(__middle
, __half
);
2547 if (__val
< *__middle
)
2553 __len
= __len
- __half
- 1;
2560 * @brief Finds the last position in which @a val could be inserted
2561 * without changing the ordering.
2562 * @ingroup binary_search_algorithms
2563 * @param first An iterator.
2564 * @param last Another iterator.
2565 * @param val The search term.
2566 * @param comp A functor to use for comparisons.
2567 * @return An iterator pointing to the first element greater than @a val,
2568 * or end() if no elements are greater than @a val.
2569 * @ingroup binary_search_algorithms
2571 * The comparison function should have the same effects on ordering as
2572 * the function used for the initial sort.
2574 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2576 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2577 const _Tp
& __val
, _Compare __comp
)
2579 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2581 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2584 // concept requirements
2585 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2586 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2588 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2591 _DistanceType __len
= std::distance(__first
, __last
);
2592 _DistanceType __half
;
2593 _ForwardIterator __middle
;
2597 __half
= __len
>> 1;
2599 std::advance(__middle
, __half
);
2600 if (__comp(__val
, *__middle
))
2606 __len
= __len
- __half
- 1;
2613 * @brief Finds the largest subrange in which @a val could be inserted
2614 * at any place in it without changing the ordering.
2615 * @ingroup binary_search_algorithms
2616 * @param first An iterator.
2617 * @param last Another iterator.
2618 * @param val The search term.
2619 * @return An pair of iterators defining the subrange.
2620 * @ingroup binary_search_algorithms
2622 * This is equivalent to
2624 * std::make_pair(lower_bound(first, last, val),
2625 * upper_bound(first, last, val))
2627 * but does not actually call those functions.
2629 template<typename _ForwardIterator
, typename _Tp
>
2630 pair
<_ForwardIterator
, _ForwardIterator
>
2631 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2634 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2636 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2639 // concept requirements
2640 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2641 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2642 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2643 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2644 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2646 _DistanceType __len
= std::distance(__first
, __last
);
2647 _DistanceType __half
;
2648 _ForwardIterator __middle
, __left
, __right
;
2652 __half
= __len
>> 1;
2654 std::advance(__middle
, __half
);
2655 if (*__middle
< __val
)
2659 __len
= __len
- __half
- 1;
2661 else if (__val
< *__middle
)
2665 __left
= std::lower_bound(__first
, __middle
, __val
);
2666 std::advance(__first
, __len
);
2667 __right
= std::upper_bound(++__middle
, __first
, __val
);
2668 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2671 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2675 * @brief Finds the largest subrange in which @a val could be inserted
2676 * at any place in it without changing the ordering.
2677 * @param first An iterator.
2678 * @param last Another iterator.
2679 * @param val The search term.
2680 * @param comp A functor to use for comparisons.
2681 * @return An pair of iterators defining the subrange.
2682 * @ingroup binary_search_algorithms
2684 * This is equivalent to
2686 * std::make_pair(lower_bound(first, last, val, comp),
2687 * upper_bound(first, last, val, comp))
2689 * but does not actually call those functions.
2691 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2692 pair
<_ForwardIterator
, _ForwardIterator
>
2693 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2697 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2699 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2702 // concept requirements
2703 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2704 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2706 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2708 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2710 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2713 _DistanceType __len
= std::distance(__first
, __last
);
2714 _DistanceType __half
;
2715 _ForwardIterator __middle
, __left
, __right
;
2719 __half
= __len
>> 1;
2721 std::advance(__middle
, __half
);
2722 if (__comp(*__middle
, __val
))
2726 __len
= __len
- __half
- 1;
2728 else if (__comp(__val
, *__middle
))
2732 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2733 std::advance(__first
, __len
);
2734 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2735 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2738 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2742 * @brief Determines whether an element exists in a range.
2743 * @ingroup binary_search_algorithms
2744 * @param first An iterator.
2745 * @param last Another iterator.
2746 * @param val The search term.
2747 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2749 * Note that this does not actually return an iterator to @a val. For
2750 * that, use std::find or a container's specialized find member functions.
2752 template<typename _ForwardIterator
, typename _Tp
>
2754 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2757 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2760 // concept requirements
2761 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2762 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2763 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2764 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2766 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2767 return __i
!= __last
&& !(__val
< *__i
);
2771 * @brief Determines whether an element exists in a range.
2772 * @ingroup binary_search_algorithms
2773 * @param first An iterator.
2774 * @param last Another iterator.
2775 * @param val The search term.
2776 * @param comp A functor to use for comparisons.
2777 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2779 * Note that this does not actually return an iterator to @a val. For
2780 * that, use std::find or a container's specialized find member functions.
2782 * The comparison function should have the same effects on ordering as
2783 * the function used for the initial sort.
2785 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2787 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2788 const _Tp
& __val
, _Compare __comp
)
2790 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2793 // concept requirements
2794 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2795 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2797 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2799 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2802 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2803 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2808 /// This is a helper function for the merge routines.
2809 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2810 typename _BidirectionalIterator3
>
2811 _BidirectionalIterator3
2812 __merge_backward(_BidirectionalIterator1 __first1
,
2813 _BidirectionalIterator1 __last1
,
2814 _BidirectionalIterator2 __first2
,
2815 _BidirectionalIterator2 __last2
,
2816 _BidirectionalIterator3 __result
)
2818 if (__first1
== __last1
)
2819 return std::copy_backward(__first2
, __last2
, __result
);
2820 if (__first2
== __last2
)
2821 return std::copy_backward(__first1
, __last1
, __result
);
2826 if (*__last2
< *__last1
)
2828 *--__result
= *__last1
;
2829 if (__first1
== __last1
)
2830 return std::copy_backward(__first2
, ++__last2
, __result
);
2835 *--__result
= *__last2
;
2836 if (__first2
== __last2
)
2837 return std::copy_backward(__first1
, ++__last1
, __result
);
2843 /// This is a helper function for the merge routines.
2844 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2845 typename _BidirectionalIterator3
, typename _Compare
>
2846 _BidirectionalIterator3
2847 __merge_backward(_BidirectionalIterator1 __first1
,
2848 _BidirectionalIterator1 __last1
,
2849 _BidirectionalIterator2 __first2
,
2850 _BidirectionalIterator2 __last2
,
2851 _BidirectionalIterator3 __result
,
2854 if (__first1
== __last1
)
2855 return std::copy_backward(__first2
, __last2
, __result
);
2856 if (__first2
== __last2
)
2857 return std::copy_backward(__first1
, __last1
, __result
);
2862 if (__comp(*__last2
, *__last1
))
2864 *--__result
= *__last1
;
2865 if (__first1
== __last1
)
2866 return std::copy_backward(__first2
, ++__last2
, __result
);
2871 *--__result
= *__last2
;
2872 if (__first2
== __last2
)
2873 return std::copy_backward(__first1
, ++__last1
, __result
);
2879 /// This is a helper function for the merge routines.
2880 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2882 _BidirectionalIterator1
2883 __rotate_adaptive(_BidirectionalIterator1 __first
,
2884 _BidirectionalIterator1 __middle
,
2885 _BidirectionalIterator1 __last
,
2886 _Distance __len1
, _Distance __len2
,
2887 _BidirectionalIterator2 __buffer
,
2888 _Distance __buffer_size
)
2890 _BidirectionalIterator2 __buffer_end
;
2891 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2893 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2894 std::copy_backward(__first
, __middle
, __last
);
2895 return std::copy(__buffer
, __buffer_end
, __first
);
2897 else if (__len1
<= __buffer_size
)
2899 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2900 std::copy(__middle
, __last
, __first
);
2901 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2905 std::rotate(__first
, __middle
, __last
);
2906 std::advance(__first
, std::distance(__middle
, __last
));
2911 /// This is a helper function for the merge routines.
2912 template<typename _BidirectionalIterator
, typename _Distance
,
2915 __merge_adaptive(_BidirectionalIterator __first
,
2916 _BidirectionalIterator __middle
,
2917 _BidirectionalIterator __last
,
2918 _Distance __len1
, _Distance __len2
,
2919 _Pointer __buffer
, _Distance __buffer_size
)
2921 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2923 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2924 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2927 else if (__len2
<= __buffer_size
)
2929 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2930 std::__merge_backward(__first
, __middle
, __buffer
,
2931 __buffer_end
, __last
);
2935 _BidirectionalIterator __first_cut
= __first
;
2936 _BidirectionalIterator __second_cut
= __middle
;
2937 _Distance __len11
= 0;
2938 _Distance __len22
= 0;
2939 if (__len1
> __len2
)
2941 __len11
= __len1
/ 2;
2942 std::advance(__first_cut
, __len11
);
2943 __second_cut
= std::lower_bound(__middle
, __last
,
2945 __len22
= std::distance(__middle
, __second_cut
);
2949 __len22
= __len2
/ 2;
2950 std::advance(__second_cut
, __len22
);
2951 __first_cut
= std::upper_bound(__first
, __middle
,
2953 __len11
= std::distance(__first
, __first_cut
);
2955 _BidirectionalIterator __new_middle
=
2956 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2957 __len1
- __len11
, __len22
, __buffer
,
2959 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2960 __len22
, __buffer
, __buffer_size
);
2961 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2963 __len2
- __len22
, __buffer
, __buffer_size
);
2967 /// This is a helper function for the merge routines.
2968 template<typename _BidirectionalIterator
, typename _Distance
,
2969 typename _Pointer
, typename _Compare
>
2971 __merge_adaptive(_BidirectionalIterator __first
,
2972 _BidirectionalIterator __middle
,
2973 _BidirectionalIterator __last
,
2974 _Distance __len1
, _Distance __len2
,
2975 _Pointer __buffer
, _Distance __buffer_size
,
2978 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2980 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2981 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2984 else if (__len2
<= __buffer_size
)
2986 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2987 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2992 _BidirectionalIterator __first_cut
= __first
;
2993 _BidirectionalIterator __second_cut
= __middle
;
2994 _Distance __len11
= 0;
2995 _Distance __len22
= 0;
2996 if (__len1
> __len2
)
2998 __len11
= __len1
/ 2;
2999 std::advance(__first_cut
, __len11
);
3000 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3002 __len22
= std::distance(__middle
, __second_cut
);
3006 __len22
= __len2
/ 2;
3007 std::advance(__second_cut
, __len22
);
3008 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3010 __len11
= std::distance(__first
, __first_cut
);
3012 _BidirectionalIterator __new_middle
=
3013 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3014 __len1
- __len11
, __len22
, __buffer
,
3016 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3017 __len22
, __buffer
, __buffer_size
, __comp
);
3018 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3020 __len2
- __len22
, __buffer
,
3021 __buffer_size
, __comp
);
3025 /// This is a helper function for the merge routines.
3026 template<typename _BidirectionalIterator
, typename _Distance
>
3028 __merge_without_buffer(_BidirectionalIterator __first
,
3029 _BidirectionalIterator __middle
,
3030 _BidirectionalIterator __last
,
3031 _Distance __len1
, _Distance __len2
)
3033 if (__len1
== 0 || __len2
== 0)
3035 if (__len1
+ __len2
== 2)
3037 if (*__middle
< *__first
)
3038 std::iter_swap(__first
, __middle
);
3041 _BidirectionalIterator __first_cut
= __first
;
3042 _BidirectionalIterator __second_cut
= __middle
;
3043 _Distance __len11
= 0;
3044 _Distance __len22
= 0;
3045 if (__len1
> __len2
)
3047 __len11
= __len1
/ 2;
3048 std::advance(__first_cut
, __len11
);
3049 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3050 __len22
= std::distance(__middle
, __second_cut
);
3054 __len22
= __len2
/ 2;
3055 std::advance(__second_cut
, __len22
);
3056 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3057 __len11
= std::distance(__first
, __first_cut
);
3059 std::rotate(__first_cut
, __middle
, __second_cut
);
3060 _BidirectionalIterator __new_middle
= __first_cut
;
3061 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3062 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3064 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3065 __len1
- __len11
, __len2
- __len22
);
3068 /// This is a helper function for the merge routines.
3069 template<typename _BidirectionalIterator
, typename _Distance
,
3072 __merge_without_buffer(_BidirectionalIterator __first
,
3073 _BidirectionalIterator __middle
,
3074 _BidirectionalIterator __last
,
3075 _Distance __len1
, _Distance __len2
,
3078 if (__len1
== 0 || __len2
== 0)
3080 if (__len1
+ __len2
== 2)
3082 if (__comp(*__middle
, *__first
))
3083 std::iter_swap(__first
, __middle
);
3086 _BidirectionalIterator __first_cut
= __first
;
3087 _BidirectionalIterator __second_cut
= __middle
;
3088 _Distance __len11
= 0;
3089 _Distance __len22
= 0;
3090 if (__len1
> __len2
)
3092 __len11
= __len1
/ 2;
3093 std::advance(__first_cut
, __len11
);
3094 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3096 __len22
= std::distance(__middle
, __second_cut
);
3100 __len22
= __len2
/ 2;
3101 std::advance(__second_cut
, __len22
);
3102 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3104 __len11
= std::distance(__first
, __first_cut
);
3106 std::rotate(__first_cut
, __middle
, __second_cut
);
3107 _BidirectionalIterator __new_middle
= __first_cut
;
3108 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3109 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3110 __len11
, __len22
, __comp
);
3111 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3112 __len1
- __len11
, __len2
- __len22
, __comp
);
3116 * @brief Merges two sorted ranges in place.
3117 * @ingroup sorting_algorithms
3118 * @param first An iterator.
3119 * @param middle Another iterator.
3120 * @param last Another iterator.
3123 * Merges two sorted and consecutive ranges, [first,middle) and
3124 * [middle,last), and puts the result in [first,last). The output will
3125 * be sorted. The sort is @e stable, that is, for equivalent
3126 * elements in the two ranges, elements from the first range will always
3127 * come before elements from the second.
3129 * If enough additional memory is available, this takes (last-first)-1
3130 * comparisons. Otherwise an NlogN algorithm is used, where N is
3131 * distance(first,last).
3133 template<typename _BidirectionalIterator
>
3135 inplace_merge(_BidirectionalIterator __first
,
3136 _BidirectionalIterator __middle
,
3137 _BidirectionalIterator __last
)
3139 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3141 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3144 // concept requirements
3145 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3146 _BidirectionalIterator
>)
3147 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3148 __glibcxx_requires_sorted(__first
, __middle
);
3149 __glibcxx_requires_sorted(__middle
, __last
);
3151 if (__first
== __middle
|| __middle
== __last
)
3154 _DistanceType __len1
= std::distance(__first
, __middle
);
3155 _DistanceType __len2
= std::distance(__middle
, __last
);
3157 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3159 if (__buf
.begin() == 0)
3160 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3162 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3163 __buf
.begin(), _DistanceType(__buf
.size()));
3167 * @brief Merges two sorted ranges in place.
3168 * @ingroup sorting_algorithms
3169 * @param first An iterator.
3170 * @param middle Another iterator.
3171 * @param last Another iterator.
3172 * @param comp A functor to use for comparisons.
3175 * Merges two sorted and consecutive ranges, [first,middle) and
3176 * [middle,last), and puts the result in [first,last). The output will
3177 * be sorted. The sort is @e stable, that is, for equivalent
3178 * elements in the two ranges, elements from the first range will always
3179 * come before elements from the second.
3181 * If enough additional memory is available, this takes (last-first)-1
3182 * comparisons. Otherwise an NlogN algorithm is used, where N is
3183 * distance(first,last).
3185 * The comparison function should have the same effects on ordering as
3186 * the function used for the initial sort.
3188 template<typename _BidirectionalIterator
, typename _Compare
>
3190 inplace_merge(_BidirectionalIterator __first
,
3191 _BidirectionalIterator __middle
,
3192 _BidirectionalIterator __last
,
3195 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3197 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3200 // concept requirements
3201 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3202 _BidirectionalIterator
>)
3203 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3204 _ValueType
, _ValueType
>)
3205 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3206 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3208 if (__first
== __middle
|| __middle
== __last
)
3211 const _DistanceType __len1
= std::distance(__first
, __middle
);
3212 const _DistanceType __len2
= std::distance(__middle
, __last
);
3214 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3216 if (__buf
.begin() == 0)
3217 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3220 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3221 __buf
.begin(), _DistanceType(__buf
.size()),
3225 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3228 __merge_sort_loop(_RandomAccessIterator1 __first
,
3229 _RandomAccessIterator1 __last
,
3230 _RandomAccessIterator2 __result
,
3231 _Distance __step_size
)
3233 const _Distance __two_step
= 2 * __step_size
;
3235 while (__last
- __first
>= __two_step
)
3237 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3238 __first
+ __step_size
,
3239 __first
+ __two_step
,
3241 __first
+= __two_step
;
3244 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3245 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3246 __first
+ __step_size
, __last
,
3250 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3251 typename _Distance
, typename _Compare
>
3253 __merge_sort_loop(_RandomAccessIterator1 __first
,
3254 _RandomAccessIterator1 __last
,
3255 _RandomAccessIterator2 __result
, _Distance __step_size
,
3258 const _Distance __two_step
= 2 * __step_size
;
3260 while (__last
- __first
>= __two_step
)
3262 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3263 __first
+ __step_size
, __first
+ __two_step
,
3266 __first
+= __two_step
;
3268 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3270 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3271 __first
+ __step_size
, __last
, __result
, __comp
);
3274 template<typename _RandomAccessIterator
, typename _Distance
>
3276 __chunk_insertion_sort(_RandomAccessIterator __first
,
3277 _RandomAccessIterator __last
,
3278 _Distance __chunk_size
)
3280 while (__last
- __first
>= __chunk_size
)
3282 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3283 __first
+= __chunk_size
;
3285 std::__insertion_sort(__first
, __last
);
3288 template<typename _RandomAccessIterator
, typename _Distance
,
3291 __chunk_insertion_sort(_RandomAccessIterator __first
,
3292 _RandomAccessIterator __last
,
3293 _Distance __chunk_size
, _Compare __comp
)
3295 while (__last
- __first
>= __chunk_size
)
3297 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3298 __first
+= __chunk_size
;
3300 std::__insertion_sort(__first
, __last
, __comp
);
3303 enum { _S_chunk_size
= 7 };
3305 template<typename _RandomAccessIterator
, typename _Pointer
>
3307 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3308 _RandomAccessIterator __last
,
3311 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3314 const _Distance __len
= __last
- __first
;
3315 const _Pointer __buffer_last
= __buffer
+ __len
;
3317 _Distance __step_size
= _S_chunk_size
;
3318 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3320 while (__step_size
< __len
)
3322 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3324 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3329 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3331 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3332 _RandomAccessIterator __last
,
3333 _Pointer __buffer
, _Compare __comp
)
3335 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3338 const _Distance __len
= __last
- __first
;
3339 const _Pointer __buffer_last
= __buffer
+ __len
;
3341 _Distance __step_size
= _S_chunk_size
;
3342 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3344 while (__step_size
< __len
)
3346 std::__merge_sort_loop(__first
, __last
, __buffer
,
3347 __step_size
, __comp
);
3349 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3350 __step_size
, __comp
);
3355 template<typename _RandomAccessIterator
, typename _Pointer
,
3358 __stable_sort_adaptive(_RandomAccessIterator __first
,
3359 _RandomAccessIterator __last
,
3360 _Pointer __buffer
, _Distance __buffer_size
)
3362 const _Distance __len
= (__last
- __first
+ 1) / 2;
3363 const _RandomAccessIterator __middle
= __first
+ __len
;
3364 if (__len
> __buffer_size
)
3366 std::__stable_sort_adaptive(__first
, __middle
,
3367 __buffer
, __buffer_size
);
3368 std::__stable_sort_adaptive(__middle
, __last
,
3369 __buffer
, __buffer_size
);
3373 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3374 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3376 std::__merge_adaptive(__first
, __middle
, __last
,
3377 _Distance(__middle
- __first
),
3378 _Distance(__last
- __middle
),
3379 __buffer
, __buffer_size
);
3382 template<typename _RandomAccessIterator
, typename _Pointer
,
3383 typename _Distance
, typename _Compare
>
3385 __stable_sort_adaptive(_RandomAccessIterator __first
,
3386 _RandomAccessIterator __last
,
3387 _Pointer __buffer
, _Distance __buffer_size
,
3390 const _Distance __len
= (__last
- __first
+ 1) / 2;
3391 const _RandomAccessIterator __middle
= __first
+ __len
;
3392 if (__len
> __buffer_size
)
3394 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3395 __buffer_size
, __comp
);
3396 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3397 __buffer_size
, __comp
);
3401 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3402 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3404 std::__merge_adaptive(__first
, __middle
, __last
,
3405 _Distance(__middle
- __first
),
3406 _Distance(__last
- __middle
),
3407 __buffer
, __buffer_size
,
3411 /// This is a helper function for the stable sorting routines.
3412 template<typename _RandomAccessIterator
>
3414 __inplace_stable_sort(_RandomAccessIterator __first
,
3415 _RandomAccessIterator __last
)
3417 if (__last
- __first
< 15)
3419 std::__insertion_sort(__first
, __last
);
3422 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3423 std::__inplace_stable_sort(__first
, __middle
);
3424 std::__inplace_stable_sort(__middle
, __last
);
3425 std::__merge_without_buffer(__first
, __middle
, __last
,
3430 /// This is a helper function for the stable sorting routines.
3431 template<typename _RandomAccessIterator
, typename _Compare
>
3433 __inplace_stable_sort(_RandomAccessIterator __first
,
3434 _RandomAccessIterator __last
, _Compare __comp
)
3436 if (__last
- __first
< 15)
3438 std::__insertion_sort(__first
, __last
, __comp
);
3441 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3442 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3443 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3444 std::__merge_without_buffer(__first
, __middle
, __last
,
3452 // Set algorithms: includes, set_union, set_intersection, set_difference,
3453 // set_symmetric_difference. All of these algorithms have the precondition
3454 // that their input ranges are sorted and the postcondition that their output
3455 // ranges are sorted.
3458 * @brief Determines whether all elements of a sequence exists in a range.
3459 * @param first1 Start of search range.
3460 * @param last1 End of search range.
3461 * @param first2 Start of sequence
3462 * @param last2 End of sequence.
3463 * @return True if each element in [first2,last2) is contained in order
3464 * within [first1,last1). False otherwise.
3465 * @ingroup set_algorithms
3467 * This operation expects both [first1,last1) and [first2,last2) to be
3468 * sorted. Searches for the presence of each element in [first2,last2)
3469 * within [first1,last1). The iterators over each range only move forward,
3470 * so this is a linear algorithm. If an element in [first2,last2) is not
3471 * found before the search iterator reaches @a last2, false is returned.
3473 template<typename _InputIterator1
, typename _InputIterator2
>
3475 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3476 _InputIterator2 __first2
, _InputIterator2 __last2
)
3478 typedef typename iterator_traits
<_InputIterator1
>::value_type
3480 typedef typename iterator_traits
<_InputIterator2
>::value_type
3483 // concept requirements
3484 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3485 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3486 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3487 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3488 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3489 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3491 while (__first1
!= __last1
&& __first2
!= __last2
)
3492 if (*__first2
< *__first1
)
3494 else if(*__first1
< *__first2
)
3497 ++__first1
, ++__first2
;
3499 return __first2
== __last2
;
3503 * @brief Determines whether all elements of a sequence exists in a range
3505 * @ingroup set_algorithms
3506 * @param first1 Start of search range.
3507 * @param last1 End of search range.
3508 * @param first2 Start of sequence
3509 * @param last2 End of sequence.
3510 * @param comp Comparison function to use.
3511 * @return True if each element in [first2,last2) is contained in order
3512 * within [first1,last1) according to comp. False otherwise.
3513 * @ingroup set_algorithms
3515 * This operation expects both [first1,last1) and [first2,last2) to be
3516 * sorted. Searches for the presence of each element in [first2,last2)
3517 * within [first1,last1), using comp to decide. The iterators over each
3518 * range only move forward, so this is a linear algorithm. If an element
3519 * in [first2,last2) is not found before the search iterator reaches @a
3520 * last2, false is returned.
3522 template<typename _InputIterator1
, typename _InputIterator2
,
3525 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3526 _InputIterator2 __first2
, _InputIterator2 __last2
,
3529 typedef typename iterator_traits
<_InputIterator1
>::value_type
3531 typedef typename iterator_traits
<_InputIterator2
>::value_type
3534 // concept requirements
3535 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3536 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3537 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3538 _ValueType1
, _ValueType2
>)
3539 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3540 _ValueType2
, _ValueType1
>)
3541 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3542 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3544 while (__first1
!= __last1
&& __first2
!= __last2
)
3545 if (__comp(*__first2
, *__first1
))
3547 else if(__comp(*__first1
, *__first2
))
3550 ++__first1
, ++__first2
;
3552 return __first2
== __last2
;
3561 // set_symmetric_difference
3566 * @brief Permute range into the next "dictionary" ordering.
3567 * @ingroup sorting_algorithms
3568 * @param first Start of range.
3569 * @param last End of range.
3570 * @return False if wrapped to first permutation, true otherwise.
3572 * Treats all permutations of the range as a set of "dictionary" sorted
3573 * sequences. Permutes the current sequence into the next one of this set.
3574 * Returns true if there are more sequences to generate. If the sequence
3575 * is the largest of the set, the smallest is generated and false returned.
3577 template<typename _BidirectionalIterator
>
3579 next_permutation(_BidirectionalIterator __first
,
3580 _BidirectionalIterator __last
)
3582 // concept requirements
3583 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3584 _BidirectionalIterator
>)
3585 __glibcxx_function_requires(_LessThanComparableConcept
<
3586 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3587 __glibcxx_requires_valid_range(__first
, __last
);
3589 if (__first
== __last
)
3591 _BidirectionalIterator __i
= __first
;
3600 _BidirectionalIterator __ii
= __i
;
3604 _BidirectionalIterator __j
= __last
;
3605 while (!(*__i
< *--__j
))
3607 std::iter_swap(__i
, __j
);
3608 std::reverse(__ii
, __last
);
3613 std::reverse(__first
, __last
);
3620 * @brief Permute range into the next "dictionary" ordering using
3621 * comparison functor.
3622 * @ingroup sorting_algorithms
3623 * @param first Start of range.
3624 * @param last End of range.
3625 * @param comp A comparison functor.
3626 * @return False if wrapped to first permutation, true otherwise.
3628 * Treats all permutations of the range [first,last) as a set of
3629 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3630 * sequence into the next one of this set. Returns true if there are more
3631 * sequences to generate. If the sequence is the largest of the set, the
3632 * smallest is generated and false returned.
3634 template<typename _BidirectionalIterator
, typename _Compare
>
3636 next_permutation(_BidirectionalIterator __first
,
3637 _BidirectionalIterator __last
, _Compare __comp
)
3639 // concept requirements
3640 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3641 _BidirectionalIterator
>)
3642 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3643 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3644 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3645 __glibcxx_requires_valid_range(__first
, __last
);
3647 if (__first
== __last
)
3649 _BidirectionalIterator __i
= __first
;
3658 _BidirectionalIterator __ii
= __i
;
3660 if (__comp(*__i
, *__ii
))
3662 _BidirectionalIterator __j
= __last
;
3663 while (!bool(__comp(*__i
, *--__j
)))
3665 std::iter_swap(__i
, __j
);
3666 std::reverse(__ii
, __last
);
3671 std::reverse(__first
, __last
);
3678 * @brief Permute range into the previous "dictionary" ordering.
3679 * @ingroup sorting_algorithms
3680 * @param first Start of range.
3681 * @param last End of range.
3682 * @return False if wrapped to last permutation, true otherwise.
3684 * Treats all permutations of the range as a set of "dictionary" sorted
3685 * sequences. Permutes the current sequence into the previous one of this
3686 * set. Returns true if there are more sequences to generate. If the
3687 * sequence is the smallest of the set, the largest is generated and false
3690 template<typename _BidirectionalIterator
>
3692 prev_permutation(_BidirectionalIterator __first
,
3693 _BidirectionalIterator __last
)
3695 // concept requirements
3696 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3697 _BidirectionalIterator
>)
3698 __glibcxx_function_requires(_LessThanComparableConcept
<
3699 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3700 __glibcxx_requires_valid_range(__first
, __last
);
3702 if (__first
== __last
)
3704 _BidirectionalIterator __i
= __first
;
3713 _BidirectionalIterator __ii
= __i
;
3717 _BidirectionalIterator __j
= __last
;
3718 while (!(*--__j
< *__i
))
3720 std::iter_swap(__i
, __j
);
3721 std::reverse(__ii
, __last
);
3726 std::reverse(__first
, __last
);
3733 * @brief Permute range into the previous "dictionary" ordering using
3734 * comparison functor.
3735 * @ingroup sorting_algorithms
3736 * @param first Start of range.
3737 * @param last End of range.
3738 * @param comp A comparison functor.
3739 * @return False if wrapped to last permutation, true otherwise.
3741 * Treats all permutations of the range [first,last) as a set of
3742 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3743 * sequence into the previous one of this set. Returns true if there are
3744 * more sequences to generate. If the sequence is the smallest of the set,
3745 * the largest is generated and false returned.
3747 template<typename _BidirectionalIterator
, typename _Compare
>
3749 prev_permutation(_BidirectionalIterator __first
,
3750 _BidirectionalIterator __last
, _Compare __comp
)
3752 // concept requirements
3753 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3754 _BidirectionalIterator
>)
3755 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3756 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3757 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3758 __glibcxx_requires_valid_range(__first
, __last
);
3760 if (__first
== __last
)
3762 _BidirectionalIterator __i
= __first
;
3771 _BidirectionalIterator __ii
= __i
;
3773 if (__comp(*__ii
, *__i
))
3775 _BidirectionalIterator __j
= __last
;
3776 while (!bool(__comp(*--__j
, *__i
)))
3778 std::iter_swap(__i
, __j
);
3779 std::reverse(__ii
, __last
);
3784 std::reverse(__first
, __last
);
3794 * @brief Copy a sequence, replacing each element of one value with another
3796 * @param first An input iterator.
3797 * @param last An input iterator.
3798 * @param result An output iterator.
3799 * @param old_value The value to be replaced.
3800 * @param new_value The replacement value.
3801 * @return The end of the output sequence, @p result+(last-first).
3803 * Copies each element in the input range @p [first,last) to the
3804 * output range @p [result,result+(last-first)) replacing elements
3805 * equal to @p old_value with @p new_value.
3807 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3809 replace_copy(_InputIterator __first
, _InputIterator __last
,
3810 _OutputIterator __result
,
3811 const _Tp
& __old_value
, const _Tp
& __new_value
)
3813 // concept requirements
3814 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3815 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3816 typename iterator_traits
<_InputIterator
>::value_type
>)
3817 __glibcxx_function_requires(_EqualOpConcept
<
3818 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3819 __glibcxx_requires_valid_range(__first
, __last
);
3821 for (; __first
!= __last
; ++__first
, ++__result
)
3822 if (*__first
== __old_value
)
3823 *__result
= __new_value
;
3825 *__result
= *__first
;
3830 * @brief Copy a sequence, replacing each value for which a predicate
3831 * returns true with another value.
3832 * @ingroup mutating_algorithms
3833 * @param first An input iterator.
3834 * @param last An input iterator.
3835 * @param result An output iterator.
3836 * @param pred A predicate.
3837 * @param new_value The replacement value.
3838 * @return The end of the output sequence, @p result+(last-first).
3840 * Copies each element in the range @p [first,last) to the range
3841 * @p [result,result+(last-first)) replacing elements for which
3842 * @p pred returns true with @p new_value.
3844 template<typename _InputIterator
, typename _OutputIterator
,
3845 typename _Predicate
, typename _Tp
>
3847 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3848 _OutputIterator __result
,
3849 _Predicate __pred
, const _Tp
& __new_value
)
3851 // concept requirements
3852 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3853 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3854 typename iterator_traits
<_InputIterator
>::value_type
>)
3855 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3856 typename iterator_traits
<_InputIterator
>::value_type
>)
3857 __glibcxx_requires_valid_range(__first
, __last
);
3859 for (; __first
!= __last
; ++__first
, ++__result
)
3860 if (__pred(*__first
))
3861 *__result
= __new_value
;
3863 *__result
= *__first
;
3867 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3869 * @brief Determines whether the elements of a sequence are sorted.
3870 * @ingroup sorting_algorithms
3871 * @param first An iterator.
3872 * @param last Another iterator.
3873 * @return True if the elements are sorted, false otherwise.
3875 template<typename _ForwardIterator
>
3877 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3878 { return std::is_sorted_until(__first
, __last
) == __last
; }
3881 * @brief Determines whether the elements of a sequence are sorted
3882 * according to a comparison functor.
3883 * @ingroup sorting_algorithms
3884 * @param first An iterator.
3885 * @param last Another iterator.
3886 * @param comp A comparison functor.
3887 * @return True if the elements are sorted, false otherwise.
3889 template<typename _ForwardIterator
, typename _Compare
>
3891 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3893 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3896 * @brief Determines the end of a sorted sequence.
3897 * @ingroup sorting_algorithms
3898 * @param first An iterator.
3899 * @param last Another iterator.
3900 * @return An iterator pointing to the last iterator i in [first, last)
3901 * for which the range [first, i) is sorted.
3903 template<typename _ForwardIterator
>
3905 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3907 // concept requirements
3908 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3909 __glibcxx_function_requires(_LessThanComparableConcept
<
3910 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3911 __glibcxx_requires_valid_range(__first
, __last
);
3913 if (__first
== __last
)
3916 _ForwardIterator __next
= __first
;
3917 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3918 if (*__next
< *__first
)
3924 * @brief Determines the end of a sorted sequence using comparison functor.
3925 * @ingroup sorting_algorithms
3926 * @param first An iterator.
3927 * @param last Another iterator.
3928 * @param comp A comparison functor.
3929 * @return An iterator pointing to the last iterator i in [first, last)
3930 * for which the range [first, i) is sorted.
3932 template<typename _ForwardIterator
, typename _Compare
>
3934 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3937 // concept requirements
3938 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3939 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3940 typename iterator_traits
<_ForwardIterator
>::value_type
,
3941 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3942 __glibcxx_requires_valid_range(__first
, __last
);
3944 if (__first
== __last
)
3947 _ForwardIterator __next
= __first
;
3948 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3949 if (__comp(*__next
, *__first
))
3955 * @brief Determines min and max at once as an ordered pair.
3956 * @ingroup sorting_algorithms
3957 * @param a A thing of arbitrary type.
3958 * @param b Another thing of arbitrary type.
3959 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3961 template<typename _Tp
>
3962 inline pair
<const _Tp
&, const _Tp
&>
3963 minmax(const _Tp
& __a
, const _Tp
& __b
)
3965 // concept requirements
3966 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3968 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3969 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3973 * @brief Determines min and max at once as an ordered pair.
3974 * @ingroup sorting_algorithms
3975 * @param a A thing of arbitrary type.
3976 * @param b Another thing of arbitrary type.
3977 * @param comp A @link comparison_functor comparison functor@endlink.
3978 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3980 template<typename _Tp
, typename _Compare
>
3981 inline pair
<const _Tp
&, const _Tp
&>
3982 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3984 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3985 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3989 * @brief Return a pair of iterators pointing to the minimum and maximum
3990 * elements in a range.
3991 * @ingroup sorting_algorithms
3992 * @param first Start of range.
3993 * @param last End of range.
3994 * @return make_pair(m, M), where m is the first iterator i in
3995 * [first, last) such that no other element in the range is
3996 * smaller, and where M is the last iterator i in [first, last)
3997 * such that no other element in the range is larger.
3999 template<typename _ForwardIterator
>
4000 pair
<_ForwardIterator
, _ForwardIterator
>
4001 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4003 // concept requirements
4004 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4005 __glibcxx_function_requires(_LessThanComparableConcept
<
4006 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4007 __glibcxx_requires_valid_range(__first
, __last
);
4009 _ForwardIterator __next
= __first
;
4010 if (__first
== __last
4011 || ++__next
== __last
)
4012 return std::make_pair(__first
, __first
);
4014 _ForwardIterator __min
, __max
;
4015 if (*__next
< *__first
)
4029 while (__first
!= __last
)
4032 if (++__next
== __last
)
4034 if (*__first
< *__min
)
4036 else if (!(*__first
< *__max
))
4041 if (*__next
< *__first
)
4043 if (*__next
< *__min
)
4045 if (!(*__first
< *__max
))
4050 if (*__first
< *__min
)
4052 if (!(*__next
< *__max
))
4060 return std::make_pair(__min
, __max
);
4064 * @brief Return a pair of iterators pointing to the minimum and maximum
4065 * elements in a range.
4066 * @ingroup sorting_algorithms
4067 * @param first Start of range.
4068 * @param last End of range.
4069 * @param comp Comparison functor.
4070 * @return make_pair(m, M), where m is the first iterator i in
4071 * [first, last) such that no other element in the range is
4072 * smaller, and where M is the last iterator i in [first, last)
4073 * such that no other element in the range is larger.
4075 template<typename _ForwardIterator
, typename _Compare
>
4076 pair
<_ForwardIterator
, _ForwardIterator
>
4077 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4080 // concept requirements
4081 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4082 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4083 typename iterator_traits
<_ForwardIterator
>::value_type
,
4084 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4085 __glibcxx_requires_valid_range(__first
, __last
);
4087 _ForwardIterator __next
= __first
;
4088 if (__first
== __last
4089 || ++__next
== __last
)
4090 return std::make_pair(__first
, __first
);
4092 _ForwardIterator __min
, __max
;
4093 if (__comp(*__next
, *__first
))
4107 while (__first
!= __last
)
4110 if (++__next
== __last
)
4112 if (__comp(*__first
, *__min
))
4114 else if (!__comp(*__first
, *__max
))
4119 if (__comp(*__next
, *__first
))
4121 if (__comp(*__next
, *__min
))
4123 if (!__comp(*__first
, *__max
))
4128 if (__comp(*__first
, *__min
))
4130 if (!__comp(*__next
, *__max
))
4138 return std::make_pair(__min
, __max
);
4142 template<typename _Tp
>
4144 min(initializer_list
<_Tp
> __l
)
4145 { return *std::min_element(__l
.begin(), __l
.end()); }
4147 template<typename _Tp
, typename _Compare
>
4149 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4150 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4152 template<typename _Tp
>
4154 max(initializer_list
<_Tp
> __l
)
4155 { return *std::max_element(__l
.begin(), __l
.end()); }
4157 template<typename _Tp
, typename _Compare
>
4159 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4160 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4162 template<typename _Tp
>
4163 inline pair
<_Tp
, _Tp
>
4164 minmax(initializer_list
<_Tp
> __l
)
4166 pair
<const _Tp
*, const _Tp
*> __p
=
4167 std::minmax_element(__l
.begin(), __l
.end());
4168 return std::make_pair(*__p
.first
, *__p
.second
);
4171 template<typename _Tp
, typename _Compare
>
4172 inline pair
<_Tp
, _Tp
>
4173 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4175 pair
<const _Tp
*, const _Tp
*> __p
=
4176 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4177 return std::make_pair(*__p
.first
, *__p
.second
);
4179 #endif // __GXX_EXPERIMENTAL_CXX0X__
4181 _GLIBCXX_END_NAMESPACE
4183 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4186 * @brief Apply a function to every element of a sequence.
4187 * @ingroup non_mutating_algorithms
4188 * @param first An input iterator.
4189 * @param last An input iterator.
4190 * @param f A unary function object.
4193 * Applies the function object @p f to each element in the range
4194 * @p [first,last). @p f must not modify the order of the sequence.
4195 * If @p f has a return value it is ignored.
4197 template<typename _InputIterator
, typename _Function
>
4199 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4201 // concept requirements
4202 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4203 __glibcxx_requires_valid_range(__first
, __last
);
4204 for (; __first
!= __last
; ++__first
)
4210 * @brief Find the first occurrence of a value in a sequence.
4211 * @ingroup non_mutating_algorithms
4212 * @param first An input iterator.
4213 * @param last An input iterator.
4214 * @param val The value to find.
4215 * @return The first iterator @c i in the range @p [first,last)
4216 * such that @c *i == @p val, or @p last if no such iterator exists.
4218 template<typename _InputIterator
, typename _Tp
>
4219 inline _InputIterator
4220 find(_InputIterator __first
, _InputIterator __last
,
4223 // concept requirements
4224 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4225 __glibcxx_function_requires(_EqualOpConcept
<
4226 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4227 __glibcxx_requires_valid_range(__first
, __last
);
4228 return std::__find(__first
, __last
, __val
,
4229 std::__iterator_category(__first
));
4233 * @brief Find the first element in a sequence for which a
4234 * predicate is true.
4235 * @ingroup non_mutating_algorithms
4236 * @param first An input iterator.
4237 * @param last An input iterator.
4238 * @param pred A predicate.
4239 * @return The first iterator @c i in the range @p [first,last)
4240 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4242 template<typename _InputIterator
, typename _Predicate
>
4243 inline _InputIterator
4244 find_if(_InputIterator __first
, _InputIterator __last
,
4247 // concept requirements
4248 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4249 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4250 typename iterator_traits
<_InputIterator
>::value_type
>)
4251 __glibcxx_requires_valid_range(__first
, __last
);
4252 return std::__find_if(__first
, __last
, __pred
,
4253 std::__iterator_category(__first
));
4257 * @brief Find element from a set in a sequence.
4258 * @ingroup non_mutating_algorithms
4259 * @param first1 Start of range to search.
4260 * @param last1 End of range to search.
4261 * @param first2 Start of match candidates.
4262 * @param last2 End of match candidates.
4263 * @return The first iterator @c i in the range
4264 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4265 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4267 * Searches the range @p [first1,last1) for an element that is equal to
4268 * some element in the range [first2,last2). If found, returns an iterator
4269 * in the range [first1,last1), otherwise returns @p last1.
4271 template<typename _InputIterator
, typename _ForwardIterator
>
4273 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4274 _ForwardIterator __first2
, _ForwardIterator __last2
)
4276 // concept requirements
4277 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4278 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4279 __glibcxx_function_requires(_EqualOpConcept
<
4280 typename iterator_traits
<_InputIterator
>::value_type
,
4281 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4282 __glibcxx_requires_valid_range(__first1
, __last1
);
4283 __glibcxx_requires_valid_range(__first2
, __last2
);
4285 for (; __first1
!= __last1
; ++__first1
)
4286 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4287 if (*__first1
== *__iter
)
4293 * @brief Find element from a set in a sequence using a predicate.
4294 * @ingroup non_mutating_algorithms
4295 * @param first1 Start of range to search.
4296 * @param last1 End of range to search.
4297 * @param first2 Start of match candidates.
4298 * @param last2 End of match candidates.
4299 * @param comp Predicate to use.
4300 * @return The first iterator @c i in the range
4301 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4302 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4305 * Searches the range @p [first1,last1) for an element that is
4306 * equal to some element in the range [first2,last2). If found,
4307 * returns an iterator in the range [first1,last1), otherwise
4310 template<typename _InputIterator
, typename _ForwardIterator
,
4311 typename _BinaryPredicate
>
4313 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4314 _ForwardIterator __first2
, _ForwardIterator __last2
,
4315 _BinaryPredicate __comp
)
4317 // concept requirements
4318 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4319 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4320 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4321 typename iterator_traits
<_InputIterator
>::value_type
,
4322 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4323 __glibcxx_requires_valid_range(__first1
, __last1
);
4324 __glibcxx_requires_valid_range(__first2
, __last2
);
4326 for (; __first1
!= __last1
; ++__first1
)
4327 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4328 if (__comp(*__first1
, *__iter
))
4334 * @brief Find two adjacent values in a sequence that are equal.
4335 * @ingroup non_mutating_algorithms
4336 * @param first A forward iterator.
4337 * @param last A forward iterator.
4338 * @return The first iterator @c i such that @c i and @c i+1 are both
4339 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4340 * or @p last if no such iterator exists.
4342 template<typename _ForwardIterator
>
4344 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4346 // concept requirements
4347 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4348 __glibcxx_function_requires(_EqualityComparableConcept
<
4349 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4350 __glibcxx_requires_valid_range(__first
, __last
);
4351 if (__first
== __last
)
4353 _ForwardIterator __next
= __first
;
4354 while(++__next
!= __last
)
4356 if (*__first
== *__next
)
4364 * @brief Find two adjacent values in a sequence using a predicate.
4365 * @ingroup non_mutating_algorithms
4366 * @param first A forward iterator.
4367 * @param last A forward iterator.
4368 * @param binary_pred A binary predicate.
4369 * @return The first iterator @c i such that @c i and @c i+1 are both
4370 * valid iterators in @p [first,last) and such that
4371 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4374 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4376 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4377 _BinaryPredicate __binary_pred
)
4379 // concept requirements
4380 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4381 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4382 typename iterator_traits
<_ForwardIterator
>::value_type
,
4383 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4384 __glibcxx_requires_valid_range(__first
, __last
);
4385 if (__first
== __last
)
4387 _ForwardIterator __next
= __first
;
4388 while(++__next
!= __last
)
4390 if (__binary_pred(*__first
, *__next
))
4398 * @brief Count the number of copies of a value in a sequence.
4399 * @ingroup non_mutating_algorithms
4400 * @param first An input iterator.
4401 * @param last An input iterator.
4402 * @param value The value to be counted.
4403 * @return The number of iterators @c i in the range @p [first,last)
4404 * for which @c *i == @p value
4406 template<typename _InputIterator
, typename _Tp
>
4407 typename iterator_traits
<_InputIterator
>::difference_type
4408 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4410 // concept requirements
4411 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4412 __glibcxx_function_requires(_EqualOpConcept
<
4413 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4414 __glibcxx_requires_valid_range(__first
, __last
);
4415 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4416 for (; __first
!= __last
; ++__first
)
4417 if (*__first
== __value
)
4423 * @brief Count the elements of a sequence for which a predicate is true.
4424 * @ingroup non_mutating_algorithms
4425 * @param first An input iterator.
4426 * @param last An input iterator.
4427 * @param pred A predicate.
4428 * @return The number of iterators @c i in the range @p [first,last)
4429 * for which @p pred(*i) is true.
4431 template<typename _InputIterator
, typename _Predicate
>
4432 typename iterator_traits
<_InputIterator
>::difference_type
4433 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4435 // concept requirements
4436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4437 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4438 typename iterator_traits
<_InputIterator
>::value_type
>)
4439 __glibcxx_requires_valid_range(__first
, __last
);
4440 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4441 for (; __first
!= __last
; ++__first
)
4442 if (__pred(*__first
))
4448 * @brief Search a sequence for a matching sub-sequence.
4449 * @ingroup non_mutating_algorithms
4450 * @param first1 A forward iterator.
4451 * @param last1 A forward iterator.
4452 * @param first2 A forward iterator.
4453 * @param last2 A forward iterator.
4454 * @return The first iterator @c i in the range
4455 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4456 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4457 * such iterator exists.
4459 * Searches the range @p [first1,last1) for a sub-sequence that compares
4460 * equal value-by-value with the sequence given by @p [first2,last2) and
4461 * returns an iterator to the first element of the sub-sequence, or
4462 * @p last1 if the sub-sequence is not found.
4464 * Because the sub-sequence must lie completely within the range
4465 * @p [first1,last1) it must start at a position less than
4466 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4468 * This means that the returned iterator @c i will be in the range
4469 * @p [first1,last1-(last2-first2))
4471 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4473 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4474 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4476 // concept requirements
4477 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4478 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4479 __glibcxx_function_requires(_EqualOpConcept
<
4480 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4481 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4482 __glibcxx_requires_valid_range(__first1
, __last1
);
4483 __glibcxx_requires_valid_range(__first2
, __last2
);
4485 // Test for empty ranges
4486 if (__first1
== __last1
|| __first2
== __last2
)
4489 // Test for a pattern of length 1.
4490 _ForwardIterator2
__p1(__first2
);
4491 if (++__p1
== __last2
)
4492 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4495 _ForwardIterator2 __p
;
4496 _ForwardIterator1 __current
= __first1
;
4500 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4501 if (__first1
== __last1
)
4505 __current
= __first1
;
4506 if (++__current
== __last1
)
4509 while (*__current
== *__p
)
4511 if (++__p
== __last2
)
4513 if (++__current
== __last1
)
4522 * @brief Search a sequence for a matching sub-sequence using a predicate.
4523 * @ingroup non_mutating_algorithms
4524 * @param first1 A forward iterator.
4525 * @param last1 A forward iterator.
4526 * @param first2 A forward iterator.
4527 * @param last2 A forward iterator.
4528 * @param predicate A binary predicate.
4529 * @return The first iterator @c i in the range
4530 * @p [first1,last1-(last2-first2)) such that
4531 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4532 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4534 * Searches the range @p [first1,last1) for a sub-sequence that compares
4535 * equal value-by-value with the sequence given by @p [first2,last2),
4536 * using @p predicate to determine equality, and returns an iterator
4537 * to the first element of the sub-sequence, or @p last1 if no such
4540 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4542 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4543 typename _BinaryPredicate
>
4545 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4546 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4547 _BinaryPredicate __predicate
)
4549 // concept requirements
4550 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4551 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4552 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4553 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4554 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4555 __glibcxx_requires_valid_range(__first1
, __last1
);
4556 __glibcxx_requires_valid_range(__first2
, __last2
);
4558 // Test for empty ranges
4559 if (__first1
== __last1
|| __first2
== __last2
)
4562 // Test for a pattern of length 1.
4563 _ForwardIterator2
__p1(__first2
);
4564 if (++__p1
== __last2
)
4566 while (__first1
!= __last1
4567 && !bool(__predicate(*__first1
, *__first2
)))
4573 _ForwardIterator2 __p
;
4574 _ForwardIterator1 __current
= __first1
;
4578 while (__first1
!= __last1
4579 && !bool(__predicate(*__first1
, *__first2
)))
4581 if (__first1
== __last1
)
4585 __current
= __first1
;
4586 if (++__current
== __last1
)
4589 while (__predicate(*__current
, *__p
))
4591 if (++__p
== __last2
)
4593 if (++__current
== __last1
)
4603 * @brief Search a sequence for a number of consecutive values.
4604 * @ingroup non_mutating_algorithms
4605 * @param first A forward iterator.
4606 * @param last A forward iterator.
4607 * @param count The number of consecutive values.
4608 * @param val The value to find.
4609 * @return The first iterator @c i in the range @p [first,last-count)
4610 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4611 * or @p last if no such iterator exists.
4613 * Searches the range @p [first,last) for @p count consecutive elements
4616 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4618 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4619 _Integer __count
, const _Tp
& __val
)
4621 // concept requirements
4622 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4623 __glibcxx_function_requires(_EqualOpConcept
<
4624 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4625 __glibcxx_requires_valid_range(__first
, __last
);
4630 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4631 return std::__search_n(__first
, __last
, __count
, __val
,
4632 std::__iterator_category(__first
));
4637 * @brief Search a sequence for a number of consecutive values using a
4639 * @ingroup non_mutating_algorithms
4640 * @param first A forward iterator.
4641 * @param last A forward iterator.
4642 * @param count The number of consecutive values.
4643 * @param val The value to find.
4644 * @param binary_pred A binary predicate.
4645 * @return The first iterator @c i in the range @p [first,last-count)
4646 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4647 * range @p [0,count), or @p last if no such iterator exists.
4649 * Searches the range @p [first,last) for @p count consecutive elements
4650 * for which the predicate returns true.
4652 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4653 typename _BinaryPredicate
>
4655 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4656 _Integer __count
, const _Tp
& __val
,
4657 _BinaryPredicate __binary_pred
)
4659 // concept requirements
4660 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4661 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4662 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4663 __glibcxx_requires_valid_range(__first
, __last
);
4669 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4673 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4674 std::__iterator_category(__first
));
4679 * @brief Perform an operation on a sequence.
4680 * @ingroup mutating_algorithms
4681 * @param first An input iterator.
4682 * @param last An input iterator.
4683 * @param result An output iterator.
4684 * @param unary_op A unary operator.
4685 * @return An output iterator equal to @p result+(last-first).
4687 * Applies the operator to each element in the input range and assigns
4688 * the results to successive elements of the output sequence.
4689 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4690 * range @p [0,last-first).
4692 * @p unary_op must not alter its argument.
4694 template<typename _InputIterator
, typename _OutputIterator
,
4695 typename _UnaryOperation
>
4697 transform(_InputIterator __first
, _InputIterator __last
,
4698 _OutputIterator __result
, _UnaryOperation __unary_op
)
4700 // concept requirements
4701 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4702 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4703 // "the type returned by a _UnaryOperation"
4704 __typeof__(__unary_op(*__first
))>)
4705 __glibcxx_requires_valid_range(__first
, __last
);
4707 for (; __first
!= __last
; ++__first
, ++__result
)
4708 *__result
= __unary_op(*__first
);
4713 * @brief Perform an operation on corresponding elements of two sequences.
4714 * @ingroup mutating_algorithms
4715 * @param first1 An input iterator.
4716 * @param last1 An input iterator.
4717 * @param first2 An input iterator.
4718 * @param result An output iterator.
4719 * @param binary_op A binary operator.
4720 * @return An output iterator equal to @p result+(last-first).
4722 * Applies the operator to the corresponding elements in the two
4723 * input ranges and assigns the results to successive elements of the
4725 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4726 * @c N in the range @p [0,last1-first1).
4728 * @p binary_op must not alter either of its arguments.
4730 template<typename _InputIterator1
, typename _InputIterator2
,
4731 typename _OutputIterator
, typename _BinaryOperation
>
4733 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4734 _InputIterator2 __first2
, _OutputIterator __result
,
4735 _BinaryOperation __binary_op
)
4737 // concept requirements
4738 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4739 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4740 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4741 // "the type returned by a _BinaryOperation"
4742 __typeof__(__binary_op(*__first1
,*__first2
))>)
4743 __glibcxx_requires_valid_range(__first1
, __last1
);
4745 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4746 *__result
= __binary_op(*__first1
, *__first2
);
4751 * @brief Replace each occurrence of one value in a sequence with another
4753 * @ingroup mutating_algorithms
4754 * @param first A forward iterator.
4755 * @param last A forward iterator.
4756 * @param old_value The value to be replaced.
4757 * @param new_value The replacement value.
4758 * @return replace() returns no value.
4760 * For each iterator @c i in the range @p [first,last) if @c *i ==
4761 * @p old_value then the assignment @c *i = @p new_value is performed.
4763 template<typename _ForwardIterator
, typename _Tp
>
4765 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4766 const _Tp
& __old_value
, const _Tp
& __new_value
)
4768 // concept requirements
4769 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4771 __glibcxx_function_requires(_EqualOpConcept
<
4772 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4773 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4774 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4775 __glibcxx_requires_valid_range(__first
, __last
);
4777 for (; __first
!= __last
; ++__first
)
4778 if (*__first
== __old_value
)
4779 *__first
= __new_value
;
4783 * @brief Replace each value in a sequence for which a predicate returns
4784 * true with another value.
4785 * @ingroup mutating_algorithms
4786 * @param first A forward iterator.
4787 * @param last A forward iterator.
4788 * @param pred A predicate.
4789 * @param new_value The replacement value.
4790 * @return replace_if() returns no value.
4792 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4793 * is true then the assignment @c *i = @p new_value is performed.
4795 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4797 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4798 _Predicate __pred
, const _Tp
& __new_value
)
4800 // concept requirements
4801 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4803 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4804 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4805 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4806 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4807 __glibcxx_requires_valid_range(__first
, __last
);
4809 for (; __first
!= __last
; ++__first
)
4810 if (__pred(*__first
))
4811 *__first
= __new_value
;
4815 * @brief Assign the result of a function object to each value in a
4817 * @ingroup mutating_algorithms
4818 * @param first A forward iterator.
4819 * @param last A forward iterator.
4820 * @param gen A function object taking no arguments and returning
4821 * std::iterator_traits<_ForwardIterator>::value_type
4822 * @return generate() returns no value.
4824 * Performs the assignment @c *i = @p gen() for each @c i in the range
4827 template<typename _ForwardIterator
, typename _Generator
>
4829 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4832 // concept requirements
4833 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4834 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4835 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4836 __glibcxx_requires_valid_range(__first
, __last
);
4838 for (; __first
!= __last
; ++__first
)
4843 * @brief Assign the result of a function object to each value in a
4845 * @ingroup mutating_algorithms
4846 * @param first A forward iterator.
4847 * @param n The length of the sequence.
4848 * @param gen A function object taking no arguments and returning
4849 * std::iterator_traits<_ForwardIterator>::value_type
4850 * @return The end of the sequence, @p first+n
4852 * Performs the assignment @c *i = @p gen() for each @c i in the range
4853 * @p [first,first+n).
4855 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4857 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4859 // concept requirements
4860 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4861 // "the type returned by a _Generator"
4862 __typeof__(__gen())>)
4864 for (; __n
> 0; --__n
, ++__first
)
4871 * @brief Copy a sequence, removing consecutive duplicate values.
4872 * @ingroup mutating_algorithms
4873 * @param first An input iterator.
4874 * @param last An input iterator.
4875 * @param result An output iterator.
4876 * @return An iterator designating the end of the resulting sequence.
4878 * Copies each element in the range @p [first,last) to the range
4879 * beginning at @p result, except that only the first element is copied
4880 * from groups of consecutive elements that compare equal.
4881 * unique_copy() is stable, so the relative order of elements that are
4882 * copied is unchanged.
4884 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4885 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4887 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4888 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4891 template<typename _InputIterator
, typename _OutputIterator
>
4892 inline _OutputIterator
4893 unique_copy(_InputIterator __first
, _InputIterator __last
,
4894 _OutputIterator __result
)
4896 // concept requirements
4897 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4898 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4899 typename iterator_traits
<_InputIterator
>::value_type
>)
4900 __glibcxx_function_requires(_EqualityComparableConcept
<
4901 typename iterator_traits
<_InputIterator
>::value_type
>)
4902 __glibcxx_requires_valid_range(__first
, __last
);
4904 if (__first
== __last
)
4906 return std::__unique_copy(__first
, __last
, __result
,
4907 std::__iterator_category(__first
),
4908 std::__iterator_category(__result
));
4912 * @brief Copy a sequence, removing consecutive values using a predicate.
4913 * @ingroup mutating_algorithms
4914 * @param first An input iterator.
4915 * @param last An input iterator.
4916 * @param result An output iterator.
4917 * @param binary_pred A binary predicate.
4918 * @return An iterator designating the end of the resulting sequence.
4920 * Copies each element in the range @p [first,last) to the range
4921 * beginning at @p result, except that only the first element is copied
4922 * from groups of consecutive elements for which @p binary_pred returns
4924 * unique_copy() is stable, so the relative order of elements that are
4925 * copied is unchanged.
4927 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4928 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4930 template<typename _InputIterator
, typename _OutputIterator
,
4931 typename _BinaryPredicate
>
4932 inline _OutputIterator
4933 unique_copy(_InputIterator __first
, _InputIterator __last
,
4934 _OutputIterator __result
,
4935 _BinaryPredicate __binary_pred
)
4937 // concept requirements -- predicates checked later
4938 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4939 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4940 typename iterator_traits
<_InputIterator
>::value_type
>)
4941 __glibcxx_requires_valid_range(__first
, __last
);
4943 if (__first
== __last
)
4945 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4946 std::__iterator_category(__first
),
4947 std::__iterator_category(__result
));
4952 * @brief Randomly shuffle the elements of a sequence.
4953 * @ingroup mutating_algorithms
4954 * @param first A forward iterator.
4955 * @param last A forward iterator.
4958 * Reorder the elements in the range @p [first,last) using a random
4959 * distribution, so that every possible ordering of the sequence is
4962 template<typename _RandomAccessIterator
>
4964 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4966 // concept requirements
4967 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4968 _RandomAccessIterator
>)
4969 __glibcxx_requires_valid_range(__first
, __last
);
4971 if (__first
!= __last
)
4972 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4973 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4977 * @brief Shuffle the elements of a sequence using a random number
4979 * @ingroup mutating_algorithms
4980 * @param first A forward iterator.
4981 * @param last A forward iterator.
4982 * @param rand The RNG functor or function.
4985 * Reorders the elements in the range @p [first,last) using @p rand to
4986 * provide a random distribution. Calling @p rand(N) for a positive
4987 * integer @p N should return a randomly chosen integer from the
4990 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4992 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4993 _RandomNumberGenerator
& __rand
)
4995 // concept requirements
4996 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4997 _RandomAccessIterator
>)
4998 __glibcxx_requires_valid_range(__first
, __last
);
5000 if (__first
== __last
)
5002 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5003 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5008 * @brief Move elements for which a predicate is true to the beginning
5010 * @ingroup mutating_algorithms
5011 * @param first A forward iterator.
5012 * @param last A forward iterator.
5013 * @param pred A predicate functor.
5014 * @return An iterator @p middle such that @p pred(i) is true for each
5015 * iterator @p i in the range @p [first,middle) and false for each @p i
5016 * in the range @p [middle,last).
5018 * @p pred must not modify its operand. @p partition() does not preserve
5019 * the relative ordering of elements in each group, use
5020 * @p stable_partition() if this is needed.
5022 template<typename _ForwardIterator
, typename _Predicate
>
5023 inline _ForwardIterator
5024 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5027 // concept requirements
5028 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5030 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5031 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5032 __glibcxx_requires_valid_range(__first
, __last
);
5034 return std::__partition(__first
, __last
, __pred
,
5035 std::__iterator_category(__first
));
5041 * @brief Sort the smallest elements of a sequence.
5042 * @ingroup sorting_algorithms
5043 * @param first An iterator.
5044 * @param middle Another iterator.
5045 * @param last Another iterator.
5048 * Sorts the smallest @p (middle-first) elements in the range
5049 * @p [first,last) and moves them to the range @p [first,middle). The
5050 * order of the remaining elements in the range @p [middle,last) is
5052 * After the sort if @p i and @j are iterators in the range
5053 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5054 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5056 template<typename _RandomAccessIterator
>
5058 partial_sort(_RandomAccessIterator __first
,
5059 _RandomAccessIterator __middle
,
5060 _RandomAccessIterator __last
)
5062 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5065 // concept requirements
5066 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5067 _RandomAccessIterator
>)
5068 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5069 __glibcxx_requires_valid_range(__first
, __middle
);
5070 __glibcxx_requires_valid_range(__middle
, __last
);
5072 std::__heap_select(__first
, __middle
, __last
);
5073 std::sort_heap(__first
, __middle
);
5077 * @brief Sort the smallest elements of a sequence using a predicate
5079 * @ingroup sorting_algorithms
5080 * @param first An iterator.
5081 * @param middle Another iterator.
5082 * @param last Another iterator.
5083 * @param comp A comparison functor.
5086 * Sorts the smallest @p (middle-first) elements in the range
5087 * @p [first,last) and moves them to the range @p [first,middle). The
5088 * order of the remaining elements in the range @p [middle,last) is
5090 * After the sort if @p i and @j are iterators in the range
5091 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5092 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5095 template<typename _RandomAccessIterator
, typename _Compare
>
5097 partial_sort(_RandomAccessIterator __first
,
5098 _RandomAccessIterator __middle
,
5099 _RandomAccessIterator __last
,
5102 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5105 // concept requirements
5106 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5107 _RandomAccessIterator
>)
5108 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5109 _ValueType
, _ValueType
>)
5110 __glibcxx_requires_valid_range(__first
, __middle
);
5111 __glibcxx_requires_valid_range(__middle
, __last
);
5113 std::__heap_select(__first
, __middle
, __last
, __comp
);
5114 std::sort_heap(__first
, __middle
, __comp
);
5118 * @brief Sort a sequence just enough to find a particular position.
5119 * @ingroup sorting_algorithms
5120 * @param first An iterator.
5121 * @param nth Another iterator.
5122 * @param last Another iterator.
5125 * Rearranges the elements in the range @p [first,last) so that @p *nth
5126 * is the same element that would have been in that position had the
5127 * whole sequence been sorted.
5128 * whole sequence been sorted. The elements either side of @p *nth are
5129 * not completely sorted, but for any iterator @i in the range
5130 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5131 * holds that @p *j<*i is false.
5133 template<typename _RandomAccessIterator
>
5135 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5136 _RandomAccessIterator __last
)
5138 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5141 // concept requirements
5142 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5143 _RandomAccessIterator
>)
5144 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5145 __glibcxx_requires_valid_range(__first
, __nth
);
5146 __glibcxx_requires_valid_range(__nth
, __last
);
5148 if (__first
== __last
|| __nth
== __last
)
5151 std::__introselect(__first
, __nth
, __last
,
5152 std::__lg(__last
- __first
) * 2);
5156 * @brief Sort a sequence just enough to find a particular position
5157 * using a predicate for comparison.
5158 * @ingroup sorting_algorithms
5159 * @param first An iterator.
5160 * @param nth Another iterator.
5161 * @param last Another iterator.
5162 * @param comp A comparison functor.
5165 * Rearranges the elements in the range @p [first,last) so that @p *nth
5166 * is the same element that would have been in that position had the
5167 * whole sequence been sorted. The elements either side of @p *nth are
5168 * not completely sorted, but for any iterator @i in the range
5169 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5170 * holds that @p comp(*j,*i) is false.
5172 template<typename _RandomAccessIterator
, typename _Compare
>
5174 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5175 _RandomAccessIterator __last
, _Compare __comp
)
5177 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5180 // concept requirements
5181 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5182 _RandomAccessIterator
>)
5183 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5184 _ValueType
, _ValueType
>)
5185 __glibcxx_requires_valid_range(__first
, __nth
);
5186 __glibcxx_requires_valid_range(__nth
, __last
);
5188 if (__first
== __last
|| __nth
== __last
)
5191 std::__introselect(__first
, __nth
, __last
,
5192 std::__lg(__last
- __first
) * 2, __comp
);
5197 * @brief Sort the elements of a sequence.
5198 * @ingroup sorting_algorithms
5199 * @param first An iterator.
5200 * @param last Another iterator.
5203 * Sorts the elements in the range @p [first,last) in ascending order,
5204 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5205 * @p [first,last-1).
5207 * The relative ordering of equivalent elements is not preserved, use
5208 * @p stable_sort() if this is needed.
5210 template<typename _RandomAccessIterator
>
5212 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5214 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5217 // concept requirements
5218 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5219 _RandomAccessIterator
>)
5220 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5221 __glibcxx_requires_valid_range(__first
, __last
);
5223 if (__first
!= __last
)
5225 std::__introsort_loop(__first
, __last
,
5226 std::__lg(__last
- __first
) * 2);
5227 std::__final_insertion_sort(__first
, __last
);
5232 * @brief Sort the elements of a sequence using a predicate for comparison.
5233 * @ingroup sorting_algorithms
5234 * @param first An iterator.
5235 * @param last Another iterator.
5236 * @param comp A comparison functor.
5239 * Sorts the elements in the range @p [first,last) in ascending order,
5240 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5241 * range @p [first,last-1).
5243 * The relative ordering of equivalent elements is not preserved, use
5244 * @p stable_sort() if this is needed.
5246 template<typename _RandomAccessIterator
, typename _Compare
>
5248 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5251 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5254 // concept requirements
5255 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5256 _RandomAccessIterator
>)
5257 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5259 __glibcxx_requires_valid_range(__first
, __last
);
5261 if (__first
!= __last
)
5263 std::__introsort_loop(__first
, __last
,
5264 std::__lg(__last
- __first
) * 2, __comp
);
5265 std::__final_insertion_sort(__first
, __last
, __comp
);
5270 * @brief Merges two sorted ranges.
5271 * @ingroup sorting_algorithms
5272 * @param first1 An iterator.
5273 * @param first2 Another iterator.
5274 * @param last1 Another iterator.
5275 * @param last2 Another iterator.
5276 * @param result An iterator pointing to the end of the merged range.
5277 * @return An iterator pointing to the first element "not less
5280 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5281 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5282 * must be sorted, and the output range must not overlap with either of
5283 * the input ranges. The sort is @e stable, that is, for equivalent
5284 * elements in the two ranges, elements from the first range will always
5285 * come before elements from the second.
5287 template<typename _InputIterator1
, typename _InputIterator2
,
5288 typename _OutputIterator
>
5290 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5291 _InputIterator2 __first2
, _InputIterator2 __last2
,
5292 _OutputIterator __result
)
5294 typedef typename iterator_traits
<_InputIterator1
>::value_type
5296 typedef typename iterator_traits
<_InputIterator2
>::value_type
5299 // concept requirements
5300 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5301 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5302 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5304 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5306 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5307 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5308 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5310 while (__first1
!= __last1
&& __first2
!= __last2
)
5312 if (*__first2
< *__first1
)
5314 *__result
= *__first2
;
5319 *__result
= *__first1
;
5324 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5329 * @brief Merges two sorted ranges.
5330 * @ingroup sorting_algorithms
5331 * @param first1 An iterator.
5332 * @param first2 Another iterator.
5333 * @param last1 Another iterator.
5334 * @param last2 Another iterator.
5335 * @param result An iterator pointing to the end of the merged range.
5336 * @param comp A functor to use for comparisons.
5337 * @return An iterator pointing to the first element "not less
5340 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5341 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5342 * must be sorted, and the output range must not overlap with either of
5343 * the input ranges. The sort is @e stable, that is, for equivalent
5344 * elements in the two ranges, elements from the first range will always
5345 * come before elements from the second.
5347 * The comparison function should have the same effects on ordering as
5348 * the function used for the initial sort.
5350 template<typename _InputIterator1
, typename _InputIterator2
,
5351 typename _OutputIterator
, typename _Compare
>
5353 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5354 _InputIterator2 __first2
, _InputIterator2 __last2
,
5355 _OutputIterator __result
, _Compare __comp
)
5357 typedef typename iterator_traits
<_InputIterator1
>::value_type
5359 typedef typename iterator_traits
<_InputIterator2
>::value_type
5362 // concept requirements
5363 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5364 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5365 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5367 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5369 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5370 _ValueType2
, _ValueType1
>)
5371 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5372 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5374 while (__first1
!= __last1
&& __first2
!= __last2
)
5376 if (__comp(*__first2
, *__first1
))
5378 *__result
= *__first2
;
5383 *__result
= *__first1
;
5388 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5394 * @brief Sort the elements of a sequence, preserving the relative order
5395 * of equivalent elements.
5396 * @ingroup sorting_algorithms
5397 * @param first An iterator.
5398 * @param last Another iterator.
5401 * Sorts the elements in the range @p [first,last) in ascending order,
5402 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5403 * @p [first,last-1).
5405 * The relative ordering of equivalent elements is preserved, so any two
5406 * elements @p x and @p y in the range @p [first,last) such that
5407 * @p x<y is false and @p y<x is false will have the same relative
5408 * ordering after calling @p stable_sort().
5410 template<typename _RandomAccessIterator
>
5412 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5414 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5416 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5419 // concept requirements
5420 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5421 _RandomAccessIterator
>)
5422 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5423 __glibcxx_requires_valid_range(__first
, __last
);
5425 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5427 if (__buf
.begin() == 0)
5428 std::__inplace_stable_sort(__first
, __last
);
5430 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5431 _DistanceType(__buf
.size()));
5435 * @brief Sort the elements of a sequence using a predicate for comparison,
5436 * preserving the relative order of equivalent elements.
5437 * @ingroup sorting_algorithms
5438 * @param first An iterator.
5439 * @param last Another iterator.
5440 * @param comp A comparison functor.
5443 * Sorts the elements in the range @p [first,last) in ascending order,
5444 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5445 * range @p [first,last-1).
5447 * The relative ordering of equivalent elements is preserved, so any two
5448 * elements @p x and @p y in the range @p [first,last) such that
5449 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5450 * relative ordering after calling @p stable_sort().
5452 template<typename _RandomAccessIterator
, typename _Compare
>
5454 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5457 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5459 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5462 // concept requirements
5463 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5464 _RandomAccessIterator
>)
5465 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5468 __glibcxx_requires_valid_range(__first
, __last
);
5470 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5472 if (__buf
.begin() == 0)
5473 std::__inplace_stable_sort(__first
, __last
, __comp
);
5475 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5476 _DistanceType(__buf
.size()), __comp
);
5481 * @brief Return the union of two sorted ranges.
5482 * @ingroup set_algorithms
5483 * @param first1 Start of first range.
5484 * @param last1 End of first range.
5485 * @param first2 Start of second range.
5486 * @param last2 End of second range.
5487 * @return End of the output range.
5488 * @ingroup set_algorithms
5490 * This operation iterates over both ranges, copying elements present in
5491 * each range in order to the output range. Iterators increment for each
5492 * range. When the current element of one range is less than the other,
5493 * that element is copied and the iterator advanced. If an element is
5494 * contained in both ranges, the element from the first range is copied and
5495 * both ranges advance. The output range may not overlap either input
5498 template<typename _InputIterator1
, typename _InputIterator2
,
5499 typename _OutputIterator
>
5501 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5502 _InputIterator2 __first2
, _InputIterator2 __last2
,
5503 _OutputIterator __result
)
5505 typedef typename iterator_traits
<_InputIterator1
>::value_type
5507 typedef typename iterator_traits
<_InputIterator2
>::value_type
5510 // concept requirements
5511 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5512 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5513 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5515 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5517 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5518 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5519 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5520 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5522 while (__first1
!= __last1
&& __first2
!= __last2
)
5524 if (*__first1
< *__first2
)
5526 *__result
= *__first1
;
5529 else if (*__first2
< *__first1
)
5531 *__result
= *__first2
;
5536 *__result
= *__first1
;
5542 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5547 * @brief Return the union of two sorted ranges using a comparison functor.
5548 * @ingroup set_algorithms
5549 * @param first1 Start of first range.
5550 * @param last1 End of first range.
5551 * @param first2 Start of second range.
5552 * @param last2 End of second range.
5553 * @param comp The comparison functor.
5554 * @return End of the output range.
5555 * @ingroup set_algorithms
5557 * This operation iterates over both ranges, copying elements present in
5558 * each range in order to the output range. Iterators increment for each
5559 * range. When the current element of one range is less than the other
5560 * according to @a comp, that element is copied and the iterator advanced.
5561 * If an equivalent element according to @a comp is contained in both
5562 * ranges, the element from the first range is copied and both ranges
5563 * advance. The output range may not overlap either input range.
5565 template<typename _InputIterator1
, typename _InputIterator2
,
5566 typename _OutputIterator
, typename _Compare
>
5568 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5569 _InputIterator2 __first2
, _InputIterator2 __last2
,
5570 _OutputIterator __result
, _Compare __comp
)
5572 typedef typename iterator_traits
<_InputIterator1
>::value_type
5574 typedef typename iterator_traits
<_InputIterator2
>::value_type
5577 // concept requirements
5578 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5579 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5580 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5582 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5584 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5585 _ValueType1
, _ValueType2
>)
5586 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5587 _ValueType2
, _ValueType1
>)
5588 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5589 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5591 while (__first1
!= __last1
&& __first2
!= __last2
)
5593 if (__comp(*__first1
, *__first2
))
5595 *__result
= *__first1
;
5598 else if (__comp(*__first2
, *__first1
))
5600 *__result
= *__first2
;
5605 *__result
= *__first1
;
5611 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5616 * @brief Return the intersection of two sorted ranges.
5617 * @ingroup set_algorithms
5618 * @param first1 Start of first range.
5619 * @param last1 End of first range.
5620 * @param first2 Start of second range.
5621 * @param last2 End of second range.
5622 * @return End of the output range.
5623 * @ingroup set_algorithms
5625 * This operation iterates over both ranges, copying elements present in
5626 * both ranges in order to the output range. Iterators increment for each
5627 * range. When the current element of one range is less than the other,
5628 * that iterator advances. If an element is contained in both ranges, the
5629 * element from the first range is copied and both ranges advance. The
5630 * output range may not overlap either input range.
5632 template<typename _InputIterator1
, typename _InputIterator2
,
5633 typename _OutputIterator
>
5635 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5636 _InputIterator2 __first2
, _InputIterator2 __last2
,
5637 _OutputIterator __result
)
5639 typedef typename iterator_traits
<_InputIterator1
>::value_type
5641 typedef typename iterator_traits
<_InputIterator2
>::value_type
5644 // concept requirements
5645 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5646 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5647 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5649 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5650 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5651 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5652 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5654 while (__first1
!= __last1
&& __first2
!= __last2
)
5655 if (*__first1
< *__first2
)
5657 else if (*__first2
< *__first1
)
5661 *__result
= *__first1
;
5670 * @brief Return the intersection of two sorted ranges using comparison
5672 * @ingroup set_algorithms
5673 * @param first1 Start of first range.
5674 * @param last1 End of first range.
5675 * @param first2 Start of second range.
5676 * @param last2 End of second range.
5677 * @param comp The comparison functor.
5678 * @return End of the output range.
5679 * @ingroup set_algorithms
5681 * This operation iterates over both ranges, copying elements present in
5682 * both ranges in order to the output range. Iterators increment for each
5683 * range. When the current element of one range is less than the other
5684 * according to @a comp, that iterator advances. If an element is
5685 * contained in both ranges according to @a comp, the element from the
5686 * first range is copied and both ranges advance. The output range may not
5687 * overlap either input range.
5689 template<typename _InputIterator1
, typename _InputIterator2
,
5690 typename _OutputIterator
, typename _Compare
>
5692 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5693 _InputIterator2 __first2
, _InputIterator2 __last2
,
5694 _OutputIterator __result
, _Compare __comp
)
5696 typedef typename iterator_traits
<_InputIterator1
>::value_type
5698 typedef typename iterator_traits
<_InputIterator2
>::value_type
5701 // concept requirements
5702 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5703 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5704 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5706 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5707 _ValueType1
, _ValueType2
>)
5708 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5709 _ValueType2
, _ValueType1
>)
5710 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5711 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5713 while (__first1
!= __last1
&& __first2
!= __last2
)
5714 if (__comp(*__first1
, *__first2
))
5716 else if (__comp(*__first2
, *__first1
))
5720 *__result
= *__first1
;
5729 * @brief Return the difference of two sorted ranges.
5730 * @ingroup set_algorithms
5731 * @param first1 Start of first range.
5732 * @param last1 End of first range.
5733 * @param first2 Start of second range.
5734 * @param last2 End of second range.
5735 * @return End of the output range.
5736 * @ingroup set_algorithms
5738 * This operation iterates over both ranges, copying elements present in
5739 * the first range but not the second in order to the output range.
5740 * Iterators increment for each range. When the current element of the
5741 * first range is less than the second, that element is copied and the
5742 * iterator advances. If the current element of the second range is less,
5743 * the iterator advances, but no element is copied. If an element is
5744 * contained in both ranges, no elements are copied and both ranges
5745 * advance. The output range may not overlap either input range.
5747 template<typename _InputIterator1
, typename _InputIterator2
,
5748 typename _OutputIterator
>
5750 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5751 _InputIterator2 __first2
, _InputIterator2 __last2
,
5752 _OutputIterator __result
)
5754 typedef typename iterator_traits
<_InputIterator1
>::value_type
5756 typedef typename iterator_traits
<_InputIterator2
>::value_type
5759 // concept requirements
5760 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5761 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5762 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5764 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5765 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5766 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5767 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5769 while (__first1
!= __last1
&& __first2
!= __last2
)
5770 if (*__first1
< *__first2
)
5772 *__result
= *__first1
;
5776 else if (*__first2
< *__first1
)
5783 return std::copy(__first1
, __last1
, __result
);
5787 * @brief Return the difference of two sorted ranges using comparison
5789 * @ingroup set_algorithms
5790 * @param first1 Start of first range.
5791 * @param last1 End of first range.
5792 * @param first2 Start of second range.
5793 * @param last2 End of second range.
5794 * @param comp The comparison functor.
5795 * @return End of the output range.
5796 * @ingroup set_algorithms
5798 * This operation iterates over both ranges, copying elements present in
5799 * the first range but not the second in order to the output range.
5800 * Iterators increment for each range. When the current element of the
5801 * first range is less than the second according to @a comp, that element
5802 * is copied and the iterator advances. If the current element of the
5803 * second range is less, no element is copied and the iterator advances.
5804 * If an element is contained in both ranges according to @a comp, no
5805 * elements are copied and both ranges advance. The output range may not
5806 * overlap either input range.
5808 template<typename _InputIterator1
, typename _InputIterator2
,
5809 typename _OutputIterator
, typename _Compare
>
5811 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5812 _InputIterator2 __first2
, _InputIterator2 __last2
,
5813 _OutputIterator __result
, _Compare __comp
)
5815 typedef typename iterator_traits
<_InputIterator1
>::value_type
5817 typedef typename iterator_traits
<_InputIterator2
>::value_type
5820 // concept requirements
5821 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5822 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5823 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5825 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5826 _ValueType1
, _ValueType2
>)
5827 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5828 _ValueType2
, _ValueType1
>)
5829 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5830 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5832 while (__first1
!= __last1
&& __first2
!= __last2
)
5833 if (__comp(*__first1
, *__first2
))
5835 *__result
= *__first1
;
5839 else if (__comp(*__first2
, *__first1
))
5846 return std::copy(__first1
, __last1
, __result
);
5850 * @brief Return the symmetric difference of two sorted ranges.
5851 * @ingroup set_algorithms
5852 * @param first1 Start of first range.
5853 * @param last1 End of first range.
5854 * @param first2 Start of second range.
5855 * @param last2 End of second range.
5856 * @return End of the output range.
5857 * @ingroup set_algorithms
5859 * This operation iterates over both ranges, copying elements present in
5860 * one range but not the other in order to the output range. Iterators
5861 * increment for each range. When the current element of one range is less
5862 * than the other, that element is copied and the iterator advances. If an
5863 * element is contained in both ranges, no elements are copied and both
5864 * ranges advance. The output range may not overlap either input range.
5866 template<typename _InputIterator1
, typename _InputIterator2
,
5867 typename _OutputIterator
>
5869 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5870 _InputIterator2 __first2
, _InputIterator2 __last2
,
5871 _OutputIterator __result
)
5873 typedef typename iterator_traits
<_InputIterator1
>::value_type
5875 typedef typename iterator_traits
<_InputIterator2
>::value_type
5878 // concept requirements
5879 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5880 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5881 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5883 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5885 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5886 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5887 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5888 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5890 while (__first1
!= __last1
&& __first2
!= __last2
)
5891 if (*__first1
< *__first2
)
5893 *__result
= *__first1
;
5897 else if (*__first2
< *__first1
)
5899 *__result
= *__first2
;
5908 return std::copy(__first2
, __last2
, std::copy(__first1
,
5909 __last1
, __result
));
5913 * @brief Return the symmetric difference of two sorted ranges using
5914 * comparison functor.
5915 * @ingroup set_algorithms
5916 * @param first1 Start of first range.
5917 * @param last1 End of first range.
5918 * @param first2 Start of second range.
5919 * @param last2 End of second range.
5920 * @param comp The comparison functor.
5921 * @return End of the output range.
5922 * @ingroup set_algorithms
5924 * This operation iterates over both ranges, copying elements present in
5925 * one range but not the other in order to the output range. Iterators
5926 * increment for each range. When the current element of one range is less
5927 * than the other according to @a comp, that element is copied and the
5928 * iterator advances. If an element is contained in both ranges according
5929 * to @a comp, no elements are copied and both ranges advance. The output
5930 * range may not overlap either input range.
5932 template<typename _InputIterator1
, typename _InputIterator2
,
5933 typename _OutputIterator
, typename _Compare
>
5935 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5936 _InputIterator2 __first2
, _InputIterator2 __last2
,
5937 _OutputIterator __result
,
5940 typedef typename iterator_traits
<_InputIterator1
>::value_type
5942 typedef typename iterator_traits
<_InputIterator2
>::value_type
5945 // concept requirements
5946 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5947 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5948 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5950 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5952 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5953 _ValueType1
, _ValueType2
>)
5954 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5955 _ValueType2
, _ValueType1
>)
5956 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5957 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5959 while (__first1
!= __last1
&& __first2
!= __last2
)
5960 if (__comp(*__first1
, *__first2
))
5962 *__result
= *__first1
;
5966 else if (__comp(*__first2
, *__first1
))
5968 *__result
= *__first2
;
5977 return std::copy(__first2
, __last2
,
5978 std::copy(__first1
, __last1
, __result
));
5983 * @brief Return the minimum element in a range.
5984 * @ingroup sorting_algorithms
5985 * @param first Start of range.
5986 * @param last End of range.
5987 * @return Iterator referencing the first instance of the smallest value.
5989 template<typename _ForwardIterator
>
5991 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5993 // concept requirements
5994 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5995 __glibcxx_function_requires(_LessThanComparableConcept
<
5996 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5997 __glibcxx_requires_valid_range(__first
, __last
);
5999 if (__first
== __last
)
6001 _ForwardIterator __result
= __first
;
6002 while (++__first
!= __last
)
6003 if (*__first
< *__result
)
6009 * @brief Return the minimum element in a range using comparison functor.
6010 * @ingroup sorting_algorithms
6011 * @param first Start of range.
6012 * @param last End of range.
6013 * @param comp Comparison functor.
6014 * @return Iterator referencing the first instance of the smallest value
6015 * according to comp.
6017 template<typename _ForwardIterator
, typename _Compare
>
6019 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6022 // concept requirements
6023 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6024 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6025 typename iterator_traits
<_ForwardIterator
>::value_type
,
6026 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6027 __glibcxx_requires_valid_range(__first
, __last
);
6029 if (__first
== __last
)
6031 _ForwardIterator __result
= __first
;
6032 while (++__first
!= __last
)
6033 if (__comp(*__first
, *__result
))
6039 * @brief Return the maximum element in a range.
6040 * @ingroup sorting_algorithms
6041 * @param first Start of range.
6042 * @param last End of range.
6043 * @return Iterator referencing the first instance of the largest value.
6045 template<typename _ForwardIterator
>
6047 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6049 // concept requirements
6050 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6051 __glibcxx_function_requires(_LessThanComparableConcept
<
6052 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6053 __glibcxx_requires_valid_range(__first
, __last
);
6055 if (__first
== __last
)
6057 _ForwardIterator __result
= __first
;
6058 while (++__first
!= __last
)
6059 if (*__result
< *__first
)
6065 * @brief Return the maximum element in a range using comparison functor.
6066 * @ingroup sorting_algorithms
6067 * @param first Start of range.
6068 * @param last End of range.
6069 * @param comp Comparison functor.
6070 * @return Iterator referencing the first instance of the largest value
6071 * according to comp.
6073 template<typename _ForwardIterator
, typename _Compare
>
6075 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6078 // concept requirements
6079 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6080 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6081 typename iterator_traits
<_ForwardIterator
>::value_type
,
6082 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6083 __glibcxx_requires_valid_range(__first
, __last
);
6085 if (__first
== __last
) return __first
;
6086 _ForwardIterator __result
= __first
;
6087 while (++__first
!= __last
)
6088 if (__comp(*__result
, *__first
))
6093 _GLIBCXX_END_NESTED_NAMESPACE
6095 #endif /* _STL_ALGO_H */