1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 * @param first1 Start of range to search.
644 * @param last1 End of range to search.
645 * @param first2 Start of sequence to match.
646 * @param last2 End of sequence to match.
647 * @return The last iterator @c i in the range
648 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
649 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
650 * such iterator exists.
652 * Searches the range @p [first1,last1) for a sub-sequence that compares
653 * equal value-by-value with the sequence given by @p [first2,last2) and
654 * returns an iterator to the first element of the sub-sequence, or
655 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
656 * last such subsequence contained in [first,last1).
658 * Because the sub-sequence must lie completely within the range
659 * @p [first1,last1) it must start at a position less than
660 * @p last1-(last2-first2) where @p last2-first2 is the length of the
662 * This means that the returned iterator @c i will be in the range
663 * @p [first1,last1-(last2-first2))
665 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
666 inline _ForwardIterator1
667 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
668 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
670 // concept requirements
671 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
672 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
673 __glibcxx_function_requires(_EqualOpConcept
<
674 typename iterator_traits
<_ForwardIterator1
>::value_type
,
675 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
676 __glibcxx_requires_valid_range(__first1
, __last1
);
677 __glibcxx_requires_valid_range(__first2
, __last2
);
679 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
680 std::__iterator_category(__first1
),
681 std::__iterator_category(__first2
));
685 * @brief Find last matching subsequence in a sequence using a predicate.
686 * @param first1 Start of range to search.
687 * @param last1 End of range to search.
688 * @param first2 Start of sequence to match.
689 * @param last2 End of sequence to match.
690 * @param comp The predicate to use.
691 * @return The last iterator @c i in the range
692 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
693 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
694 * @p last1 if no such iterator exists.
696 * Searches the range @p [first1,last1) for a sub-sequence that compares
697 * equal value-by-value with the sequence given by @p [first2,last2) using
698 * comp as a predicate and returns an iterator to the first element of the
699 * sub-sequence, or @p last1 if the sub-sequence is not found. The
700 * sub-sequence will be the last such subsequence contained in
703 * Because the sub-sequence must lie completely within the range
704 * @p [first1,last1) it must start at a position less than
705 * @p last1-(last2-first2) where @p last2-first2 is the length of the
707 * This means that the returned iterator @c i will be in the range
708 * @p [first1,last1-(last2-first2))
710 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
711 typename _BinaryPredicate
>
712 inline _ForwardIterator1
713 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
714 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
715 _BinaryPredicate __comp
)
717 // concept requirements
718 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
719 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
720 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
721 typename iterator_traits
<_ForwardIterator1
>::value_type
,
722 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
723 __glibcxx_requires_valid_range(__first1
, __last1
);
724 __glibcxx_requires_valid_range(__first2
, __last2
);
726 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
727 std::__iterator_category(__first1
),
728 std::__iterator_category(__first2
),
732 #ifdef __GXX_EXPERIMENTAL_CXX0X__
734 * @brief Checks that a predicate is true for all the elements
736 * @param first An input iterator.
737 * @param last An input iterator.
738 * @param pred A predicate.
739 * @return True if the check is true, false otherwise.
741 * Returns true if @p pred is true for each element in the range
742 * @p [first,last), and false otherwise.
744 template<typename _InputIterator
, typename _Predicate
>
746 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
747 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
750 * @brief Checks that a predicate is false for all the elements
752 * @param first An input iterator.
753 * @param last An input iterator.
754 * @param pred A predicate.
755 * @return True if the check is true, false otherwise.
757 * Returns true if @p pred is false for each element in the range
758 * @p [first,last), and false otherwise.
760 template<typename _InputIterator
, typename _Predicate
>
762 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
763 { return __last
== _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
); }
766 * @brief Checks that a predicate is false for at least an element
768 * @param first An input iterator.
769 * @param last An input iterator.
770 * @param pred A predicate.
771 * @return True if the check is true, false otherwise.
773 * Returns true if an element exists in the range @p [first,last) such that
774 * @p pred is true, and false otherwise.
776 template<typename _InputIterator
, typename _Predicate
>
778 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
779 { return !std::none_of(__first
, __last
, __pred
); }
782 * @brief Find the first element in a sequence for which a
783 * predicate is false.
784 * @param first An input iterator.
785 * @param last An input iterator.
786 * @param pred A predicate.
787 * @return The first iterator @c i in the range @p [first,last)
788 * such that @p pred(*i) is false, or @p last if no such iterator exists.
790 template<typename _InputIterator
, typename _Predicate
>
791 inline _InputIterator
792 find_if_not(_InputIterator __first
, _InputIterator __last
,
795 // concept requirements
796 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
797 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
798 typename iterator_traits
<_InputIterator
>::value_type
>)
799 __glibcxx_requires_valid_range(__first
, __last
);
800 return std::__find_if_not(__first
, __last
, __pred
,
801 std::__iterator_category(__first
));
805 * @brief Checks whether the sequence is partitioned.
806 * @param first An input iterator.
807 * @param last An input iterator.
808 * @param pred A predicate.
809 * @return True if the range @p [first,last) is partioned by @p pred,
810 * i.e. if all elements that satisfy @p pred appear before those that
813 template<typename _InputIterator
, typename _Predicate
>
815 is_partitioned(_InputIterator __first
, _InputIterator __last
,
818 __first
= std::find_if_not(__first
, __last
, __pred
);
819 return std::none_of(__first
, __last
, __pred
);
823 * @brief Find the partition point of a partitioned range.
824 * @param first An iterator.
825 * @param last Another iterator.
826 * @param pred A predicate.
827 * @return An iterator @p mid such that @p all_of(first, mid, pred)
828 * and @p none_of(mid, last, pred) are both true.
830 template<typename _ForwardIterator
, typename _Predicate
>
832 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
835 // concept requirements
836 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
837 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
838 typename iterator_traits
<_ForwardIterator
>::value_type
>)
840 // A specific debug-mode test will be necessary...
841 __glibcxx_requires_valid_range(__first
, __last
);
843 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
846 _DistanceType __len
= std::distance(__first
, __last
);
847 _DistanceType __half
;
848 _ForwardIterator __middle
;
854 std::advance(__middle
, __half
);
855 if (__pred(*__middle
))
859 __len
= __len
- __half
- 1;
870 * @brief Copy a sequence, removing elements of a given value.
871 * @param first An input iterator.
872 * @param last An input iterator.
873 * @param result An output iterator.
874 * @param value The value to be removed.
875 * @return An iterator designating the end of the resulting sequence.
877 * Copies each element in the range @p [first,last) not equal to @p value
878 * to the range beginning at @p result.
879 * remove_copy() is stable, so the relative order of elements that are
880 * copied is unchanged.
882 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
884 remove_copy(_InputIterator __first
, _InputIterator __last
,
885 _OutputIterator __result
, const _Tp
& __value
)
887 // concept requirements
888 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
889 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
890 typename iterator_traits
<_InputIterator
>::value_type
>)
891 __glibcxx_function_requires(_EqualOpConcept
<
892 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
893 __glibcxx_requires_valid_range(__first
, __last
);
895 for (; __first
!= __last
; ++__first
)
896 if (!(*__first
== __value
))
898 *__result
= *__first
;
905 * @brief Copy a sequence, removing elements for which a predicate is true.
906 * @param first An input iterator.
907 * @param last An input iterator.
908 * @param result An output iterator.
909 * @param pred A predicate.
910 * @return An iterator designating the end of the resulting sequence.
912 * Copies each element in the range @p [first,last) for which
913 * @p pred returns false to the range beginning at @p result.
915 * remove_copy_if() is stable, so the relative order of elements that are
916 * copied is unchanged.
918 template<typename _InputIterator
, typename _OutputIterator
,
921 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
922 _OutputIterator __result
, _Predicate __pred
)
924 // concept requirements
925 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
926 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
927 typename iterator_traits
<_InputIterator
>::value_type
>)
928 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
929 typename iterator_traits
<_InputIterator
>::value_type
>)
930 __glibcxx_requires_valid_range(__first
, __last
);
932 for (; __first
!= __last
; ++__first
)
933 if (!bool(__pred(*__first
)))
935 *__result
= *__first
;
941 #ifdef __GXX_EXPERIMENTAL_CXX0X__
943 * @brief Copy the elements of a sequence for which a predicate is true.
944 * @param first An input iterator.
945 * @param last An input iterator.
946 * @param result An output iterator.
947 * @param pred A predicate.
948 * @return An iterator designating the end of the resulting sequence.
950 * Copies each element in the range @p [first,last) for which
951 * @p pred returns true to the range beginning at @p result.
953 * copy_if() is stable, so the relative order of elements that are
954 * copied is unchanged.
956 template<typename _InputIterator
, typename _OutputIterator
,
959 copy_if(_InputIterator __first
, _InputIterator __last
,
960 _OutputIterator __result
, _Predicate __pred
)
962 // concept requirements
963 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
964 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
965 typename iterator_traits
<_InputIterator
>::value_type
>)
966 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
967 typename iterator_traits
<_InputIterator
>::value_type
>)
968 __glibcxx_requires_valid_range(__first
, __last
);
970 for (; __first
!= __last
; ++__first
)
971 if (__pred(*__first
))
973 *__result
= *__first
;
980 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
982 __copy_n(_InputIterator __first
, _Size __n
,
983 _OutputIterator __result
, input_iterator_tag
)
985 for (; __n
> 0; --__n
)
987 *__result
= *__first
;
994 template<typename _RandomAccessIterator
, typename _Size
,
995 typename _OutputIterator
>
996 inline _OutputIterator
997 __copy_n(_RandomAccessIterator __first
, _Size __n
,
998 _OutputIterator __result
, random_access_iterator_tag
)
999 { return std::copy(__first
, __first
+ __n
, __result
); }
1002 * @brief Copies the range [first,first+n) into [result,result+n).
1003 * @param first An input iterator.
1004 * @param n The number of elements to copy.
1005 * @param result An output iterator.
1008 * This inline function will boil down to a call to @c memmove whenever
1009 * possible. Failing that, if random access iterators are passed, then the
1010 * loop count will be known (and therefore a candidate for compiler
1011 * optimizations such as unrolling).
1013 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1014 inline _OutputIterator
1015 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1017 // concept requirements
1018 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1019 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1020 typename iterator_traits
<_InputIterator
>::value_type
>)
1022 return std::__copy_n(__first
, __n
, __result
,
1023 std::__iterator_category(__first
));
1027 * @brief Copy the elements of a sequence to separate output sequences
1028 * depending on the truth value of a predicate.
1029 * @param first An input iterator.
1030 * @param last An input iterator.
1031 * @param out_true An output iterator.
1032 * @param out_false An output iterator.
1033 * @param pred A predicate.
1034 * @return A pair designating the ends of the resulting sequences.
1036 * Copies each element in the range @p [first,last) for which
1037 * @p pred returns true to the range beginning at @p out_true
1038 * and each element for which @p pred returns false to @p out_false.
1040 template<typename _InputIterator
, typename _OutputIterator1
,
1041 typename _OutputIterator2
, typename _Predicate
>
1042 pair
<_OutputIterator1
, _OutputIterator2
>
1043 partition_copy(_InputIterator __first
, _InputIterator __last
,
1044 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1047 // concept requirements
1048 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1049 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1050 typename iterator_traits
<_InputIterator
>::value_type
>)
1051 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1052 typename iterator_traits
<_InputIterator
>::value_type
>)
1053 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1054 typename iterator_traits
<_InputIterator
>::value_type
>)
1055 __glibcxx_requires_valid_range(__first
, __last
);
1057 for (; __first
!= __last
; ++__first
)
1058 if (__pred(*__first
))
1060 *__out_true
= *__first
;
1065 *__out_false
= *__first
;
1069 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1074 * @brief Remove elements from a sequence.
1075 * @param first An input iterator.
1076 * @param last An input iterator.
1077 * @param value The value to be removed.
1078 * @return An iterator designating the end of the resulting sequence.
1080 * All elements equal to @p value are removed from the range
1083 * remove() is stable, so the relative order of elements that are
1084 * not removed is unchanged.
1086 * Elements between the end of the resulting sequence and @p last
1087 * are still present, but their value is unspecified.
1089 template<typename _ForwardIterator
, typename _Tp
>
1091 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1094 // concept requirements
1095 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1097 __glibcxx_function_requires(_EqualOpConcept
<
1098 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1099 __glibcxx_requires_valid_range(__first
, __last
);
1101 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1102 if(__first
== __last
)
1104 _ForwardIterator __result
= __first
;
1106 for(; __first
!= __last
; ++__first
)
1107 if(!(*__first
== __value
))
1109 *__result
= _GLIBCXX_MOVE(*__first
);
1116 * @brief Remove elements from a sequence using a predicate.
1117 * @param first A forward iterator.
1118 * @param last A forward iterator.
1119 * @param pred A predicate.
1120 * @return An iterator designating the end of the resulting sequence.
1122 * All elements for which @p pred returns true are removed from the range
1125 * remove_if() is stable, so the relative order of elements that are
1126 * not removed is unchanged.
1128 * Elements between the end of the resulting sequence and @p last
1129 * are still present, but their value is unspecified.
1131 template<typename _ForwardIterator
, typename _Predicate
>
1133 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1136 // concept requirements
1137 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1139 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1140 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1141 __glibcxx_requires_valid_range(__first
, __last
);
1143 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1144 if(__first
== __last
)
1146 _ForwardIterator __result
= __first
;
1148 for(; __first
!= __last
; ++__first
)
1149 if(!bool(__pred(*__first
)))
1151 *__result
= _GLIBCXX_MOVE(*__first
);
1158 * @brief Remove consecutive duplicate values from a sequence.
1159 * @param first A forward iterator.
1160 * @param last A forward iterator.
1161 * @return An iterator designating the end of the resulting sequence.
1163 * Removes all but the first element from each group of consecutive
1164 * values that compare equal.
1165 * unique() is stable, so the relative order of elements that are
1166 * not removed is unchanged.
1167 * Elements between the end of the resulting sequence and @p last
1168 * are still present, but their value is unspecified.
1170 template<typename _ForwardIterator
>
1172 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1174 // concept requirements
1175 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1177 __glibcxx_function_requires(_EqualityComparableConcept
<
1178 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1179 __glibcxx_requires_valid_range(__first
, __last
);
1181 // Skip the beginning, if already unique.
1182 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1183 if (__first
== __last
)
1186 // Do the real copy work.
1187 _ForwardIterator __dest
= __first
;
1189 while (++__first
!= __last
)
1190 if (!(*__dest
== *__first
))
1191 *++__dest
= _GLIBCXX_MOVE(*__first
);
1196 * @brief Remove consecutive values from a sequence using a predicate.
1197 * @param first A forward iterator.
1198 * @param last A forward iterator.
1199 * @param binary_pred A binary predicate.
1200 * @return An iterator designating the end of the resulting sequence.
1202 * Removes all but the first element from each group of consecutive
1203 * values for which @p binary_pred returns true.
1204 * unique() is stable, so the relative order of elements that are
1205 * not removed is unchanged.
1206 * Elements between the end of the resulting sequence and @p last
1207 * are still present, but their value is unspecified.
1209 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1211 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1212 _BinaryPredicate __binary_pred
)
1214 // concept requirements
1215 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1217 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1218 typename iterator_traits
<_ForwardIterator
>::value_type
,
1219 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1220 __glibcxx_requires_valid_range(__first
, __last
);
1222 // Skip the beginning, if already unique.
1223 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1224 if (__first
== __last
)
1227 // Do the real copy work.
1228 _ForwardIterator __dest
= __first
;
1230 while (++__first
!= __last
)
1231 if (!bool(__binary_pred(*__dest
, *__first
)))
1232 *++__dest
= _GLIBCXX_MOVE(*__first
);
1237 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1239 * overloaded for forward iterators and output iterator as result.
1241 template<typename _ForwardIterator
, typename _OutputIterator
>
1243 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1244 _OutputIterator __result
,
1245 forward_iterator_tag
, output_iterator_tag
)
1247 // concept requirements -- taken care of in dispatching function
1248 _ForwardIterator __next
= __first
;
1249 *__result
= *__first
;
1250 while (++__next
!= __last
)
1251 if (!(*__first
== *__next
))
1254 *++__result
= *__first
;
1260 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1262 * overloaded for input iterators and output iterator as result.
1264 template<typename _InputIterator
, typename _OutputIterator
>
1266 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1267 _OutputIterator __result
,
1268 input_iterator_tag
, output_iterator_tag
)
1270 // concept requirements -- taken care of in dispatching function
1271 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1272 *__result
= __value
;
1273 while (++__first
!= __last
)
1274 if (!(__value
== *__first
))
1277 *++__result
= __value
;
1283 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1285 * overloaded for input iterators and forward iterator as result.
1287 template<typename _InputIterator
, typename _ForwardIterator
>
1289 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1290 _ForwardIterator __result
,
1291 input_iterator_tag
, forward_iterator_tag
)
1293 // concept requirements -- taken care of in dispatching function
1294 *__result
= *__first
;
1295 while (++__first
!= __last
)
1296 if (!(*__result
== *__first
))
1297 *++__result
= *__first
;
1302 * This is an uglified
1303 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1305 * overloaded for forward iterators and output iterator as result.
1307 template<typename _ForwardIterator
, typename _OutputIterator
,
1308 typename _BinaryPredicate
>
1310 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1311 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1312 forward_iterator_tag
, output_iterator_tag
)
1314 // concept requirements -- iterators already checked
1315 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1316 typename iterator_traits
<_ForwardIterator
>::value_type
,
1317 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1319 _ForwardIterator __next
= __first
;
1320 *__result
= *__first
;
1321 while (++__next
!= __last
)
1322 if (!bool(__binary_pred(*__first
, *__next
)))
1325 *++__result
= *__first
;
1331 * This is an uglified
1332 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1334 * overloaded for input iterators and output iterator as result.
1336 template<typename _InputIterator
, typename _OutputIterator
,
1337 typename _BinaryPredicate
>
1339 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1340 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1341 input_iterator_tag
, output_iterator_tag
)
1343 // concept requirements -- iterators already checked
1344 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1345 typename iterator_traits
<_InputIterator
>::value_type
,
1346 typename iterator_traits
<_InputIterator
>::value_type
>)
1348 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1349 *__result
= __value
;
1350 while (++__first
!= __last
)
1351 if (!bool(__binary_pred(__value
, *__first
)))
1354 *++__result
= __value
;
1360 * This is an uglified
1361 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1363 * overloaded for input iterators and forward iterator as result.
1365 template<typename _InputIterator
, typename _ForwardIterator
,
1366 typename _BinaryPredicate
>
1368 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1369 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1370 input_iterator_tag
, forward_iterator_tag
)
1372 // concept requirements -- iterators already checked
1373 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1374 typename iterator_traits
<_ForwardIterator
>::value_type
,
1375 typename iterator_traits
<_InputIterator
>::value_type
>)
1377 *__result
= *__first
;
1378 while (++__first
!= __last
)
1379 if (!bool(__binary_pred(*__result
, *__first
)))
1380 *++__result
= *__first
;
1385 * This is an uglified reverse(_BidirectionalIterator,
1386 * _BidirectionalIterator)
1387 * overloaded for bidirectional iterators.
1389 template<typename _BidirectionalIterator
>
1391 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1392 bidirectional_iterator_tag
)
1395 if (__first
== __last
|| __first
== --__last
)
1399 std::iter_swap(__first
, __last
);
1405 * This is an uglified reverse(_BidirectionalIterator,
1406 * _BidirectionalIterator)
1407 * overloaded for random access iterators.
1409 template<typename _RandomAccessIterator
>
1411 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1412 random_access_iterator_tag
)
1414 if (__first
== __last
)
1417 while (__first
< __last
)
1419 std::iter_swap(__first
, __last
);
1426 * @brief Reverse a sequence.
1427 * @param first A bidirectional iterator.
1428 * @param last A bidirectional iterator.
1429 * @return reverse() returns no value.
1431 * Reverses the order of the elements in the range @p [first,last),
1432 * so that the first element becomes the last etc.
1433 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1434 * swaps @p *(first+i) and @p *(last-(i+1))
1436 template<typename _BidirectionalIterator
>
1438 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1440 // concept requirements
1441 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1442 _BidirectionalIterator
>)
1443 __glibcxx_requires_valid_range(__first
, __last
);
1444 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1448 * @brief Copy a sequence, reversing its elements.
1449 * @param first A bidirectional iterator.
1450 * @param last A bidirectional iterator.
1451 * @param result An output iterator.
1452 * @return An iterator designating the end of the resulting sequence.
1454 * Copies the elements in the range @p [first,last) to the range
1455 * @p [result,result+(last-first)) such that the order of the
1456 * elements is reversed.
1457 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1458 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1459 * The ranges @p [first,last) and @p [result,result+(last-first))
1462 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1464 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1465 _OutputIterator __result
)
1467 // concept requirements
1468 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1469 _BidirectionalIterator
>)
1470 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1471 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1472 __glibcxx_requires_valid_range(__first
, __last
);
1474 while (__first
!= __last
)
1477 *__result
= *__last
;
1484 * This is a helper function for the rotate algorithm specialized on RAIs.
1485 * It returns the greatest common divisor of two integer values.
1487 template<typename _EuclideanRingElement
>
1488 _EuclideanRingElement
1489 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1493 _EuclideanRingElement __t
= __m
% __n
;
1500 /// This is a helper function for the rotate algorithm.
1501 template<typename _ForwardIterator
>
1503 __rotate(_ForwardIterator __first
,
1504 _ForwardIterator __middle
,
1505 _ForwardIterator __last
,
1506 forward_iterator_tag
)
1508 if (__first
== __middle
|| __last
== __middle
)
1511 _ForwardIterator __first2
= __middle
;
1514 std::iter_swap(__first
, __first2
);
1517 if (__first
== __middle
)
1518 __middle
= __first2
;
1520 while (__first2
!= __last
);
1522 __first2
= __middle
;
1524 while (__first2
!= __last
)
1526 std::iter_swap(__first
, __first2
);
1529 if (__first
== __middle
)
1530 __middle
= __first2
;
1531 else if (__first2
== __last
)
1532 __first2
= __middle
;
1536 /// This is a helper function for the rotate algorithm.
1537 template<typename _BidirectionalIterator
>
1539 __rotate(_BidirectionalIterator __first
,
1540 _BidirectionalIterator __middle
,
1541 _BidirectionalIterator __last
,
1542 bidirectional_iterator_tag
)
1544 // concept requirements
1545 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1546 _BidirectionalIterator
>)
1548 if (__first
== __middle
|| __last
== __middle
)
1551 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1552 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1554 while (__first
!= __middle
&& __middle
!= __last
)
1556 std::iter_swap(__first
, --__last
);
1560 if (__first
== __middle
)
1561 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1563 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1566 /// This is a helper function for the rotate algorithm.
1567 template<typename _RandomAccessIterator
>
1569 __rotate(_RandomAccessIterator __first
,
1570 _RandomAccessIterator __middle
,
1571 _RandomAccessIterator __last
,
1572 random_access_iterator_tag
)
1574 // concept requirements
1575 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1576 _RandomAccessIterator
>)
1578 if (__first
== __middle
|| __last
== __middle
)
1581 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1583 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1586 const _Distance __n
= __last
- __first
;
1587 const _Distance __k
= __middle
- __first
;
1588 const _Distance __l
= __n
- __k
;
1592 std::swap_ranges(__first
, __middle
, __middle
);
1596 const _Distance __d
= std::__gcd(__n
, __k
);
1598 for (_Distance __i
= 0; __i
< __d
; __i
++)
1600 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1601 _RandomAccessIterator __p
= __first
;
1605 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1607 if (__p
> __first
+ __l
)
1609 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1613 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1619 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1621 if (__p
< __last
- __k
)
1623 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1626 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1631 *__p
= _GLIBCXX_MOVE(__tmp
);
1637 * @brief Rotate the elements of a sequence.
1638 * @param first A forward iterator.
1639 * @param middle A forward iterator.
1640 * @param last A forward iterator.
1643 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1644 * positions so that the element at @p middle is moved to @p first, the
1645 * element at @p middle+1 is moved to @first+1 and so on for each element
1646 * in the range @p [first,last).
1648 * This effectively swaps the ranges @p [first,middle) and
1651 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1652 * each @p n in the range @p [0,last-first).
1654 template<typename _ForwardIterator
>
1656 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1657 _ForwardIterator __last
)
1659 // concept requirements
1660 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1662 __glibcxx_requires_valid_range(__first
, __middle
);
1663 __glibcxx_requires_valid_range(__middle
, __last
);
1665 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1667 std::__rotate(__first
, __middle
, __last
, _IterType());
1671 * @brief Copy a sequence, rotating its elements.
1672 * @param first A forward iterator.
1673 * @param middle A forward iterator.
1674 * @param last A forward iterator.
1675 * @param result An output iterator.
1676 * @return An iterator designating the end of the resulting sequence.
1678 * Copies the elements of the range @p [first,last) to the range
1679 * beginning at @result, rotating the copied elements by @p (middle-first)
1680 * positions so that the element at @p middle is moved to @p result, the
1681 * element at @p middle+1 is moved to @result+1 and so on for each element
1682 * in the range @p [first,last).
1684 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1685 * each @p n in the range @p [0,last-first).
1687 template<typename _ForwardIterator
, typename _OutputIterator
>
1689 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1690 _ForwardIterator __last
, _OutputIterator __result
)
1692 // concept requirements
1693 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1694 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1695 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1696 __glibcxx_requires_valid_range(__first
, __middle
);
1697 __glibcxx_requires_valid_range(__middle
, __last
);
1699 return std::copy(__first
, __middle
,
1700 std::copy(__middle
, __last
, __result
));
1703 /// This is a helper function...
1704 template<typename _ForwardIterator
, typename _Predicate
>
1706 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1707 _Predicate __pred
, forward_iterator_tag
)
1709 if (__first
== __last
)
1712 while (__pred(*__first
))
1713 if (++__first
== __last
)
1716 _ForwardIterator __next
= __first
;
1718 while (++__next
!= __last
)
1719 if (__pred(*__next
))
1721 std::iter_swap(__first
, __next
);
1728 /// This is a helper function...
1729 template<typename _BidirectionalIterator
, typename _Predicate
>
1730 _BidirectionalIterator
1731 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1732 _Predicate __pred
, bidirectional_iterator_tag
)
1737 if (__first
== __last
)
1739 else if (__pred(*__first
))
1745 if (__first
== __last
)
1747 else if (!bool(__pred(*__last
)))
1751 std::iter_swap(__first
, __last
);
1758 /// This is a helper function...
1759 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1761 __inplace_stable_partition(_ForwardIterator __first
,
1762 _ForwardIterator __last
,
1763 _Predicate __pred
, _Distance __len
)
1766 return __pred(*__first
) ? __last
: __first
;
1767 _ForwardIterator __middle
= __first
;
1768 std::advance(__middle
, __len
/ 2);
1769 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1773 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1777 std::rotate(__begin
, __middle
, __end
);
1778 std::advance(__begin
, std::distance(__middle
, __end
));
1782 /// This is a helper function...
1783 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1786 __stable_partition_adaptive(_ForwardIterator __first
,
1787 _ForwardIterator __last
,
1788 _Predicate __pred
, _Distance __len
,
1790 _Distance __buffer_size
)
1792 if (__len
<= __buffer_size
)
1794 _ForwardIterator __result1
= __first
;
1795 _Pointer __result2
= __buffer
;
1796 for (; __first
!= __last
; ++__first
)
1797 if (__pred(*__first
))
1799 *__result1
= *__first
;
1804 *__result2
= *__first
;
1807 std::copy(__buffer
, __result2
, __result1
);
1812 _ForwardIterator __middle
= __first
;
1813 std::advance(__middle
, __len
/ 2);
1814 _ForwardIterator __begin
=
1815 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1816 __len
/ 2, __buffer
,
1818 _ForwardIterator __end
=
1819 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1821 __buffer
, __buffer_size
);
1822 std::rotate(__begin
, __middle
, __end
);
1823 std::advance(__begin
, std::distance(__middle
, __end
));
1829 * @brief Move elements for which a predicate is true to the beginning
1830 * of a sequence, preserving relative ordering.
1831 * @param first A forward iterator.
1832 * @param last A forward iterator.
1833 * @param pred A predicate functor.
1834 * @return An iterator @p middle such that @p pred(i) is true for each
1835 * iterator @p i in the range @p [first,middle) and false for each @p i
1836 * in the range @p [middle,last).
1838 * Performs the same function as @p partition() with the additional
1839 * guarantee that the relative ordering of elements in each group is
1840 * preserved, so any two elements @p x and @p y in the range
1841 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1842 * relative ordering after calling @p stable_partition().
1844 template<typename _ForwardIterator
, typename _Predicate
>
1846 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1849 // concept requirements
1850 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1852 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1853 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1854 __glibcxx_requires_valid_range(__first
, __last
);
1856 if (__first
== __last
)
1860 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1862 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1865 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1867 if (__buf
.size() > 0)
1869 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1870 _DistanceType(__buf
.requested_size()),
1872 _DistanceType(__buf
.size()));
1875 std::__inplace_stable_partition(__first
, __last
, __pred
,
1876 _DistanceType(__buf
.requested_size()));
1880 /// This is a helper function for the sort routines.
1881 template<typename _RandomAccessIterator
>
1883 __heap_select(_RandomAccessIterator __first
,
1884 _RandomAccessIterator __middle
,
1885 _RandomAccessIterator __last
)
1887 std::make_heap(__first
, __middle
);
1888 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1889 if (*__i
< *__first
)
1890 std::__pop_heap(__first
, __middle
, __i
);
1893 /// This is a helper function for the sort routines.
1894 template<typename _RandomAccessIterator
, typename _Compare
>
1896 __heap_select(_RandomAccessIterator __first
,
1897 _RandomAccessIterator __middle
,
1898 _RandomAccessIterator __last
, _Compare __comp
)
1900 std::make_heap(__first
, __middle
, __comp
);
1901 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1902 if (__comp(*__i
, *__first
))
1903 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1909 * @brief Copy the smallest elements of a sequence.
1910 * @param first An iterator.
1911 * @param last Another iterator.
1912 * @param result_first A random-access iterator.
1913 * @param result_last Another random-access iterator.
1914 * @return An iterator indicating the end of the resulting sequence.
1916 * Copies and sorts the smallest N values from the range @p [first,last)
1917 * to the range beginning at @p result_first, where the number of
1918 * elements to be copied, @p N, is the smaller of @p (last-first) and
1919 * @p (result_last-result_first).
1920 * After the sort if @p i and @j are iterators in the range
1921 * @p [result_first,result_first+N) such that @i precedes @j then
1922 * @p *j<*i is false.
1923 * The value returned is @p result_first+N.
1925 template<typename _InputIterator
, typename _RandomAccessIterator
>
1926 _RandomAccessIterator
1927 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1928 _RandomAccessIterator __result_first
,
1929 _RandomAccessIterator __result_last
)
1931 typedef typename iterator_traits
<_InputIterator
>::value_type
1933 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1935 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1938 // concept requirements
1939 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1940 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1942 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1944 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1945 __glibcxx_requires_valid_range(__first
, __last
);
1946 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1948 if (__result_first
== __result_last
)
1949 return __result_last
;
1950 _RandomAccessIterator __result_real_last
= __result_first
;
1951 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1953 *__result_real_last
= *__first
;
1954 ++__result_real_last
;
1957 std::make_heap(__result_first
, __result_real_last
);
1958 while (__first
!= __last
)
1960 if (*__first
< *__result_first
)
1961 std::__adjust_heap(__result_first
, _DistanceType(0),
1962 _DistanceType(__result_real_last
1964 _InputValueType(*__first
));
1967 std::sort_heap(__result_first
, __result_real_last
);
1968 return __result_real_last
;
1972 * @brief Copy the smallest elements of a sequence using a predicate for
1974 * @param first An input iterator.
1975 * @param last Another input iterator.
1976 * @param result_first A random-access iterator.
1977 * @param result_last Another random-access iterator.
1978 * @param comp A comparison functor.
1979 * @return An iterator indicating the end of the resulting sequence.
1981 * Copies and sorts the smallest N values from the range @p [first,last)
1982 * to the range beginning at @p result_first, where the number of
1983 * elements to be copied, @p N, is the smaller of @p (last-first) and
1984 * @p (result_last-result_first).
1985 * After the sort if @p i and @j are iterators in the range
1986 * @p [result_first,result_first+N) such that @i precedes @j then
1987 * @p comp(*j,*i) is false.
1988 * The value returned is @p result_first+N.
1990 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1991 _RandomAccessIterator
1992 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1993 _RandomAccessIterator __result_first
,
1994 _RandomAccessIterator __result_last
,
1997 typedef typename iterator_traits
<_InputIterator
>::value_type
1999 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2001 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2004 // concept requirements
2005 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2006 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2007 _RandomAccessIterator
>)
2008 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2010 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2011 _InputValueType
, _OutputValueType
>)
2012 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2013 _OutputValueType
, _OutputValueType
>)
2014 __glibcxx_requires_valid_range(__first
, __last
);
2015 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2017 if (__result_first
== __result_last
)
2018 return __result_last
;
2019 _RandomAccessIterator __result_real_last
= __result_first
;
2020 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2022 *__result_real_last
= *__first
;
2023 ++__result_real_last
;
2026 std::make_heap(__result_first
, __result_real_last
, __comp
);
2027 while (__first
!= __last
)
2029 if (__comp(*__first
, *__result_first
))
2030 std::__adjust_heap(__result_first
, _DistanceType(0),
2031 _DistanceType(__result_real_last
2033 _InputValueType(*__first
),
2037 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2038 return __result_real_last
;
2041 /// This is a helper function for the sort routine.
2042 template<typename _RandomAccessIterator
, typename _Tp
>
2044 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2046 _RandomAccessIterator __next
= __last
;
2048 while (__val
< *__next
)
2057 /// This is a helper function for the sort routine.
2058 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2060 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2063 _RandomAccessIterator __next
= __last
;
2065 while (__comp(__val
, *__next
))
2074 /// This is a helper function for the sort routine.
2075 template<typename _RandomAccessIterator
>
2077 __insertion_sort(_RandomAccessIterator __first
,
2078 _RandomAccessIterator __last
)
2080 if (__first
== __last
)
2083 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2085 typename iterator_traits
<_RandomAccessIterator
>::value_type
2087 if (__val
< *__first
)
2089 std::copy_backward(__first
, __i
, __i
+ 1);
2093 std::__unguarded_linear_insert(__i
, __val
);
2097 /// This is a helper function for the sort routine.
2098 template<typename _RandomAccessIterator
, typename _Compare
>
2100 __insertion_sort(_RandomAccessIterator __first
,
2101 _RandomAccessIterator __last
, _Compare __comp
)
2103 if (__first
== __last
) return;
2105 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2107 typename iterator_traits
<_RandomAccessIterator
>::value_type
2109 if (__comp(__val
, *__first
))
2111 std::copy_backward(__first
, __i
, __i
+ 1);
2115 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2119 /// This is a helper function for the sort routine.
2120 template<typename _RandomAccessIterator
>
2122 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2123 _RandomAccessIterator __last
)
2125 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2128 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2129 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2132 /// This is a helper function for the sort routine.
2133 template<typename _RandomAccessIterator
, typename _Compare
>
2135 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2136 _RandomAccessIterator __last
, _Compare __comp
)
2138 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2141 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2142 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2147 * This controls some aspect of the sort routines.
2149 enum { _S_threshold
= 16 };
2151 /// This is a helper function for the sort routine.
2152 template<typename _RandomAccessIterator
>
2154 __final_insertion_sort(_RandomAccessIterator __first
,
2155 _RandomAccessIterator __last
)
2157 if (__last
- __first
> int(_S_threshold
))
2159 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2160 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2163 std::__insertion_sort(__first
, __last
);
2166 /// This is a helper function for the sort routine.
2167 template<typename _RandomAccessIterator
, typename _Compare
>
2169 __final_insertion_sort(_RandomAccessIterator __first
,
2170 _RandomAccessIterator __last
, _Compare __comp
)
2172 if (__last
- __first
> int(_S_threshold
))
2174 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2175 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2179 std::__insertion_sort(__first
, __last
, __comp
);
2182 /// This is a helper function...
2183 template<typename _RandomAccessIterator
, typename _Tp
>
2184 _RandomAccessIterator
2185 __unguarded_partition(_RandomAccessIterator __first
,
2186 _RandomAccessIterator __last
, _Tp __pivot
)
2190 while (*__first
< __pivot
)
2193 while (__pivot
< *__last
)
2195 if (!(__first
< __last
))
2197 std::iter_swap(__first
, __last
);
2202 /// This is a helper function...
2203 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2204 _RandomAccessIterator
2205 __unguarded_partition(_RandomAccessIterator __first
,
2206 _RandomAccessIterator __last
,
2207 _Tp __pivot
, _Compare __comp
)
2211 while (__comp(*__first
, __pivot
))
2214 while (__comp(__pivot
, *__last
))
2216 if (!(__first
< __last
))
2218 std::iter_swap(__first
, __last
);
2223 /// This is a helper function for the sort routine.
2224 template<typename _RandomAccessIterator
, typename _Size
>
2226 __introsort_loop(_RandomAccessIterator __first
,
2227 _RandomAccessIterator __last
,
2228 _Size __depth_limit
)
2230 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2233 while (__last
- __first
> int(_S_threshold
))
2235 if (__depth_limit
== 0)
2237 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2241 _RandomAccessIterator __cut
=
2242 std::__unguarded_partition(__first
, __last
,
2243 _ValueType(std::__median(*__first
,
2250 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2255 /// This is a helper function for the sort routine.
2256 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2258 __introsort_loop(_RandomAccessIterator __first
,
2259 _RandomAccessIterator __last
,
2260 _Size __depth_limit
, _Compare __comp
)
2262 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2265 while (__last
- __first
> int(_S_threshold
))
2267 if (__depth_limit
== 0)
2269 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2273 _RandomAccessIterator __cut
=
2274 std::__unguarded_partition(__first
, __last
,
2275 _ValueType(std::__median(*__first
,
2283 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2288 /// This is a helper function for the sort routines. Precondition: __n > 0.
2289 template<typename _Size
>
2294 for (__k
= 0; __n
!= 0; __n
>>= 1)
2301 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2305 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2309 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2313 template<typename _RandomAccessIterator
, typename _Size
>
2315 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2316 _RandomAccessIterator __last
, _Size __depth_limit
)
2318 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2321 while (__last
- __first
> 3)
2323 if (__depth_limit
== 0)
2325 std::__heap_select(__first
, __nth
+ 1, __last
);
2327 // Place the nth largest element in its final position.
2328 std::iter_swap(__first
, __nth
);
2332 _RandomAccessIterator __cut
=
2333 std::__unguarded_partition(__first
, __last
,
2334 _ValueType(std::__median(*__first
,
2346 std::__insertion_sort(__first
, __last
);
2349 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2351 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2352 _RandomAccessIterator __last
, _Size __depth_limit
,
2355 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2358 while (__last
- __first
> 3)
2360 if (__depth_limit
== 0)
2362 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2363 // Place the nth largest element in its final position.
2364 std::iter_swap(__first
, __nth
);
2368 _RandomAccessIterator __cut
=
2369 std::__unguarded_partition(__first
, __last
,
2370 _ValueType(std::__median(*__first
,
2383 std::__insertion_sort(__first
, __last
, __comp
);
2389 * @brief Finds the first position in which @a val could be inserted
2390 * without changing the ordering.
2391 * @param first An iterator.
2392 * @param last Another iterator.
2393 * @param val The search term.
2394 * @return An iterator pointing to the first element "not less
2395 * than" @a val, or end() if every element is less than
2397 * @ingroup binarysearch
2399 template<typename _ForwardIterator
, typename _Tp
>
2401 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2404 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2406 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2409 // concept requirements
2410 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2411 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2412 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2414 _DistanceType __len
= std::distance(__first
, __last
);
2415 _DistanceType __half
;
2416 _ForwardIterator __middle
;
2420 __half
= __len
>> 1;
2422 std::advance(__middle
, __half
);
2423 if (*__middle
< __val
)
2427 __len
= __len
- __half
- 1;
2436 * @brief Finds the first position in which @a val could be inserted
2437 * without changing the ordering.
2438 * @param first An iterator.
2439 * @param last Another iterator.
2440 * @param val The search term.
2441 * @param comp A functor to use for comparisons.
2442 * @return An iterator pointing to the first element "not less than" @a val,
2443 * or end() if every element is less than @a val.
2444 * @ingroup binarysearch
2446 * The comparison function should have the same effects on ordering as
2447 * the function used for the initial sort.
2449 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2451 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2452 const _Tp
& __val
, _Compare __comp
)
2454 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2456 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2459 // concept requirements
2460 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2461 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2463 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2466 _DistanceType __len
= std::distance(__first
, __last
);
2467 _DistanceType __half
;
2468 _ForwardIterator __middle
;
2472 __half
= __len
>> 1;
2474 std::advance(__middle
, __half
);
2475 if (__comp(*__middle
, __val
))
2479 __len
= __len
- __half
- 1;
2488 * @brief Finds the last position in which @a val could be inserted
2489 * without changing the ordering.
2490 * @param first An iterator.
2491 * @param last Another iterator.
2492 * @param val The search term.
2493 * @return An iterator pointing to the first element greater than @a val,
2494 * or end() if no elements are greater than @a val.
2495 * @ingroup binarysearch
2497 template<typename _ForwardIterator
, typename _Tp
>
2499 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2502 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2504 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2507 // concept requirements
2508 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2509 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2510 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2512 _DistanceType __len
= std::distance(__first
, __last
);
2513 _DistanceType __half
;
2514 _ForwardIterator __middle
;
2518 __half
= __len
>> 1;
2520 std::advance(__middle
, __half
);
2521 if (__val
< *__middle
)
2527 __len
= __len
- __half
- 1;
2534 * @brief Finds the last position in which @a val could be inserted
2535 * without changing the ordering.
2536 * @param first An iterator.
2537 * @param last Another iterator.
2538 * @param val The search term.
2539 * @param comp A functor to use for comparisons.
2540 * @return An iterator pointing to the first element greater than @a val,
2541 * or end() if no elements are greater than @a val.
2542 * @ingroup binarysearch
2544 * The comparison function should have the same effects on ordering as
2545 * the function used for the initial sort.
2547 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2549 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2550 const _Tp
& __val
, _Compare __comp
)
2552 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2554 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2557 // concept requirements
2558 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2559 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2561 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2564 _DistanceType __len
= std::distance(__first
, __last
);
2565 _DistanceType __half
;
2566 _ForwardIterator __middle
;
2570 __half
= __len
>> 1;
2572 std::advance(__middle
, __half
);
2573 if (__comp(__val
, *__middle
))
2579 __len
= __len
- __half
- 1;
2586 * @brief Finds the largest subrange in which @a val could be inserted
2587 * at any place in it without changing the ordering.
2588 * @param first An iterator.
2589 * @param last Another iterator.
2590 * @param val The search term.
2591 * @return An pair of iterators defining the subrange.
2592 * @ingroup binarysearch
2594 * This is equivalent to
2596 * std::make_pair(lower_bound(first, last, val),
2597 * upper_bound(first, last, val))
2599 * but does not actually call those functions.
2601 template<typename _ForwardIterator
, typename _Tp
>
2602 pair
<_ForwardIterator
, _ForwardIterator
>
2603 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2606 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2608 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2611 // concept requirements
2612 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2613 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2614 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2615 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2616 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2618 _DistanceType __len
= std::distance(__first
, __last
);
2619 _DistanceType __half
;
2620 _ForwardIterator __middle
, __left
, __right
;
2624 __half
= __len
>> 1;
2626 std::advance(__middle
, __half
);
2627 if (*__middle
< __val
)
2631 __len
= __len
- __half
- 1;
2633 else if (__val
< *__middle
)
2637 __left
= std::lower_bound(__first
, __middle
, __val
);
2638 std::advance(__first
, __len
);
2639 __right
= std::upper_bound(++__middle
, __first
, __val
);
2640 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2643 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2647 * @brief Finds the largest subrange in which @a val could be inserted
2648 * at any place in it without changing the ordering.
2649 * @param first An iterator.
2650 * @param last Another iterator.
2651 * @param val The search term.
2652 * @param comp A functor to use for comparisons.
2653 * @return An pair of iterators defining the subrange.
2654 * @ingroup binarysearch
2656 * This is equivalent to
2658 * std::make_pair(lower_bound(first, last, val, comp),
2659 * upper_bound(first, last, val, comp))
2661 * but does not actually call those functions.
2663 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2664 pair
<_ForwardIterator
, _ForwardIterator
>
2665 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2669 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2671 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2674 // concept requirements
2675 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2676 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2678 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2680 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2682 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2685 _DistanceType __len
= std::distance(__first
, __last
);
2686 _DistanceType __half
;
2687 _ForwardIterator __middle
, __left
, __right
;
2691 __half
= __len
>> 1;
2693 std::advance(__middle
, __half
);
2694 if (__comp(*__middle
, __val
))
2698 __len
= __len
- __half
- 1;
2700 else if (__comp(__val
, *__middle
))
2704 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2705 std::advance(__first
, __len
);
2706 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2707 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2710 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2714 * @brief Determines whether an element exists in a range.
2715 * @param first An iterator.
2716 * @param last Another iterator.
2717 * @param val The search term.
2718 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2719 * @ingroup binarysearch
2721 * Note that this does not actually return an iterator to @a val. For
2722 * that, use std::find or a container's specialized find member functions.
2724 template<typename _ForwardIterator
, typename _Tp
>
2726 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2729 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2732 // concept requirements
2733 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2734 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2735 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2736 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2738 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2739 return __i
!= __last
&& !(__val
< *__i
);
2743 * @brief Determines whether an element exists in a range.
2744 * @param first An iterator.
2745 * @param last Another iterator.
2746 * @param val The search term.
2747 * @param comp A functor to use for comparisons.
2748 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2749 * @ingroup binarysearch
2751 * Note that this does not actually return an iterator to @a val. For
2752 * that, use std::find or a container's specialized find member functions.
2754 * The comparison function should have the same effects on ordering as
2755 * the function used for the initial sort.
2757 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2759 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2760 const _Tp
& __val
, _Compare __comp
)
2762 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2765 // concept requirements
2766 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2767 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2769 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2771 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2774 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2775 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2780 /// This is a helper function for the merge routines.
2781 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2782 typename _BidirectionalIterator3
>
2783 _BidirectionalIterator3
2784 __merge_backward(_BidirectionalIterator1 __first1
,
2785 _BidirectionalIterator1 __last1
,
2786 _BidirectionalIterator2 __first2
,
2787 _BidirectionalIterator2 __last2
,
2788 _BidirectionalIterator3 __result
)
2790 if (__first1
== __last1
)
2791 return std::copy_backward(__first2
, __last2
, __result
);
2792 if (__first2
== __last2
)
2793 return std::copy_backward(__first1
, __last1
, __result
);
2798 if (*__last2
< *__last1
)
2800 *--__result
= *__last1
;
2801 if (__first1
== __last1
)
2802 return std::copy_backward(__first2
, ++__last2
, __result
);
2807 *--__result
= *__last2
;
2808 if (__first2
== __last2
)
2809 return std::copy_backward(__first1
, ++__last1
, __result
);
2815 /// This is a helper function for the merge routines.
2816 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2817 typename _BidirectionalIterator3
, typename _Compare
>
2818 _BidirectionalIterator3
2819 __merge_backward(_BidirectionalIterator1 __first1
,
2820 _BidirectionalIterator1 __last1
,
2821 _BidirectionalIterator2 __first2
,
2822 _BidirectionalIterator2 __last2
,
2823 _BidirectionalIterator3 __result
,
2826 if (__first1
== __last1
)
2827 return std::copy_backward(__first2
, __last2
, __result
);
2828 if (__first2
== __last2
)
2829 return std::copy_backward(__first1
, __last1
, __result
);
2834 if (__comp(*__last2
, *__last1
))
2836 *--__result
= *__last1
;
2837 if (__first1
== __last1
)
2838 return std::copy_backward(__first2
, ++__last2
, __result
);
2843 *--__result
= *__last2
;
2844 if (__first2
== __last2
)
2845 return std::copy_backward(__first1
, ++__last1
, __result
);
2851 /// This is a helper function for the merge routines.
2852 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2854 _BidirectionalIterator1
2855 __rotate_adaptive(_BidirectionalIterator1 __first
,
2856 _BidirectionalIterator1 __middle
,
2857 _BidirectionalIterator1 __last
,
2858 _Distance __len1
, _Distance __len2
,
2859 _BidirectionalIterator2 __buffer
,
2860 _Distance __buffer_size
)
2862 _BidirectionalIterator2 __buffer_end
;
2863 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2865 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2866 std::copy_backward(__first
, __middle
, __last
);
2867 return std::copy(__buffer
, __buffer_end
, __first
);
2869 else if (__len1
<= __buffer_size
)
2871 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2872 std::copy(__middle
, __last
, __first
);
2873 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2877 std::rotate(__first
, __middle
, __last
);
2878 std::advance(__first
, std::distance(__middle
, __last
));
2883 /// This is a helper function for the merge routines.
2884 template<typename _BidirectionalIterator
, typename _Distance
,
2887 __merge_adaptive(_BidirectionalIterator __first
,
2888 _BidirectionalIterator __middle
,
2889 _BidirectionalIterator __last
,
2890 _Distance __len1
, _Distance __len2
,
2891 _Pointer __buffer
, _Distance __buffer_size
)
2893 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2895 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2896 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2899 else if (__len2
<= __buffer_size
)
2901 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2902 std::__merge_backward(__first
, __middle
, __buffer
,
2903 __buffer_end
, __last
);
2907 _BidirectionalIterator __first_cut
= __first
;
2908 _BidirectionalIterator __second_cut
= __middle
;
2909 _Distance __len11
= 0;
2910 _Distance __len22
= 0;
2911 if (__len1
> __len2
)
2913 __len11
= __len1
/ 2;
2914 std::advance(__first_cut
, __len11
);
2915 __second_cut
= std::lower_bound(__middle
, __last
,
2917 __len22
= std::distance(__middle
, __second_cut
);
2921 __len22
= __len2
/ 2;
2922 std::advance(__second_cut
, __len22
);
2923 __first_cut
= std::upper_bound(__first
, __middle
,
2925 __len11
= std::distance(__first
, __first_cut
);
2927 _BidirectionalIterator __new_middle
=
2928 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2929 __len1
- __len11
, __len22
, __buffer
,
2931 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2932 __len22
, __buffer
, __buffer_size
);
2933 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2935 __len2
- __len22
, __buffer
, __buffer_size
);
2939 /// This is a helper function for the merge routines.
2940 template<typename _BidirectionalIterator
, typename _Distance
,
2941 typename _Pointer
, typename _Compare
>
2943 __merge_adaptive(_BidirectionalIterator __first
,
2944 _BidirectionalIterator __middle
,
2945 _BidirectionalIterator __last
,
2946 _Distance __len1
, _Distance __len2
,
2947 _Pointer __buffer
, _Distance __buffer_size
,
2950 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2952 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2953 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2956 else if (__len2
<= __buffer_size
)
2958 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2959 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2964 _BidirectionalIterator __first_cut
= __first
;
2965 _BidirectionalIterator __second_cut
= __middle
;
2966 _Distance __len11
= 0;
2967 _Distance __len22
= 0;
2968 if (__len1
> __len2
)
2970 __len11
= __len1
/ 2;
2971 std::advance(__first_cut
, __len11
);
2972 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2974 __len22
= std::distance(__middle
, __second_cut
);
2978 __len22
= __len2
/ 2;
2979 std::advance(__second_cut
, __len22
);
2980 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2982 __len11
= std::distance(__first
, __first_cut
);
2984 _BidirectionalIterator __new_middle
=
2985 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2986 __len1
- __len11
, __len22
, __buffer
,
2988 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2989 __len22
, __buffer
, __buffer_size
, __comp
);
2990 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2992 __len2
- __len22
, __buffer
,
2993 __buffer_size
, __comp
);
2997 /// This is a helper function for the merge routines.
2998 template<typename _BidirectionalIterator
, typename _Distance
>
3000 __merge_without_buffer(_BidirectionalIterator __first
,
3001 _BidirectionalIterator __middle
,
3002 _BidirectionalIterator __last
,
3003 _Distance __len1
, _Distance __len2
)
3005 if (__len1
== 0 || __len2
== 0)
3007 if (__len1
+ __len2
== 2)
3009 if (*__middle
< *__first
)
3010 std::iter_swap(__first
, __middle
);
3013 _BidirectionalIterator __first_cut
= __first
;
3014 _BidirectionalIterator __second_cut
= __middle
;
3015 _Distance __len11
= 0;
3016 _Distance __len22
= 0;
3017 if (__len1
> __len2
)
3019 __len11
= __len1
/ 2;
3020 std::advance(__first_cut
, __len11
);
3021 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3022 __len22
= std::distance(__middle
, __second_cut
);
3026 __len22
= __len2
/ 2;
3027 std::advance(__second_cut
, __len22
);
3028 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3029 __len11
= std::distance(__first
, __first_cut
);
3031 std::rotate(__first_cut
, __middle
, __second_cut
);
3032 _BidirectionalIterator __new_middle
= __first_cut
;
3033 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3034 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3036 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3037 __len1
- __len11
, __len2
- __len22
);
3040 /// This is a helper function for the merge routines.
3041 template<typename _BidirectionalIterator
, typename _Distance
,
3044 __merge_without_buffer(_BidirectionalIterator __first
,
3045 _BidirectionalIterator __middle
,
3046 _BidirectionalIterator __last
,
3047 _Distance __len1
, _Distance __len2
,
3050 if (__len1
== 0 || __len2
== 0)
3052 if (__len1
+ __len2
== 2)
3054 if (__comp(*__middle
, *__first
))
3055 std::iter_swap(__first
, __middle
);
3058 _BidirectionalIterator __first_cut
= __first
;
3059 _BidirectionalIterator __second_cut
= __middle
;
3060 _Distance __len11
= 0;
3061 _Distance __len22
= 0;
3062 if (__len1
> __len2
)
3064 __len11
= __len1
/ 2;
3065 std::advance(__first_cut
, __len11
);
3066 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3068 __len22
= std::distance(__middle
, __second_cut
);
3072 __len22
= __len2
/ 2;
3073 std::advance(__second_cut
, __len22
);
3074 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3076 __len11
= std::distance(__first
, __first_cut
);
3078 std::rotate(__first_cut
, __middle
, __second_cut
);
3079 _BidirectionalIterator __new_middle
= __first_cut
;
3080 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3081 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3082 __len11
, __len22
, __comp
);
3083 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3084 __len1
- __len11
, __len2
- __len22
, __comp
);
3088 * @brief Merges two sorted ranges in place.
3089 * @param first An iterator.
3090 * @param middle Another iterator.
3091 * @param last Another iterator.
3094 * Merges two sorted and consecutive ranges, [first,middle) and
3095 * [middle,last), and puts the result in [first,last). The output will
3096 * be sorted. The sort is @e stable, that is, for equivalent
3097 * elements in the two ranges, elements from the first range will always
3098 * come before elements from the second.
3100 * If enough additional memory is available, this takes (last-first)-1
3101 * comparisons. Otherwise an NlogN algorithm is used, where N is
3102 * distance(first,last).
3104 template<typename _BidirectionalIterator
>
3106 inplace_merge(_BidirectionalIterator __first
,
3107 _BidirectionalIterator __middle
,
3108 _BidirectionalIterator __last
)
3110 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3112 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3115 // concept requirements
3116 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3117 _BidirectionalIterator
>)
3118 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3119 __glibcxx_requires_sorted(__first
, __middle
);
3120 __glibcxx_requires_sorted(__middle
, __last
);
3122 if (__first
== __middle
|| __middle
== __last
)
3125 _DistanceType __len1
= std::distance(__first
, __middle
);
3126 _DistanceType __len2
= std::distance(__middle
, __last
);
3128 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3130 if (__buf
.begin() == 0)
3131 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3133 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3134 __buf
.begin(), _DistanceType(__buf
.size()));
3138 * @brief Merges two sorted ranges in place.
3139 * @param first An iterator.
3140 * @param middle Another iterator.
3141 * @param last Another iterator.
3142 * @param comp A functor to use for comparisons.
3145 * Merges two sorted and consecutive ranges, [first,middle) and
3146 * [middle,last), and puts the result in [first,last). The output will
3147 * be sorted. The sort is @e stable, that is, for equivalent
3148 * elements in the two ranges, elements from the first range will always
3149 * come before elements from the second.
3151 * If enough additional memory is available, this takes (last-first)-1
3152 * comparisons. Otherwise an NlogN algorithm is used, where N is
3153 * distance(first,last).
3155 * The comparison function should have the same effects on ordering as
3156 * the function used for the initial sort.
3158 template<typename _BidirectionalIterator
, typename _Compare
>
3160 inplace_merge(_BidirectionalIterator __first
,
3161 _BidirectionalIterator __middle
,
3162 _BidirectionalIterator __last
,
3165 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3167 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3170 // concept requirements
3171 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3172 _BidirectionalIterator
>)
3173 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3174 _ValueType
, _ValueType
>)
3175 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3176 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3178 if (__first
== __middle
|| __middle
== __last
)
3181 const _DistanceType __len1
= std::distance(__first
, __middle
);
3182 const _DistanceType __len2
= std::distance(__middle
, __last
);
3184 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3186 if (__buf
.begin() == 0)
3187 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3190 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3191 __buf
.begin(), _DistanceType(__buf
.size()),
3195 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3198 __merge_sort_loop(_RandomAccessIterator1 __first
,
3199 _RandomAccessIterator1 __last
,
3200 _RandomAccessIterator2 __result
,
3201 _Distance __step_size
)
3203 const _Distance __two_step
= 2 * __step_size
;
3205 while (__last
- __first
>= __two_step
)
3207 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3208 __first
+ __step_size
,
3209 __first
+ __two_step
,
3211 __first
+= __two_step
;
3214 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3215 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3216 __first
+ __step_size
, __last
,
3220 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3221 typename _Distance
, typename _Compare
>
3223 __merge_sort_loop(_RandomAccessIterator1 __first
,
3224 _RandomAccessIterator1 __last
,
3225 _RandomAccessIterator2 __result
, _Distance __step_size
,
3228 const _Distance __two_step
= 2 * __step_size
;
3230 while (__last
- __first
>= __two_step
)
3232 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3233 __first
+ __step_size
, __first
+ __two_step
,
3236 __first
+= __two_step
;
3238 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3240 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3241 __first
+ __step_size
, __last
, __result
, __comp
);
3244 template<typename _RandomAccessIterator
, typename _Distance
>
3246 __chunk_insertion_sort(_RandomAccessIterator __first
,
3247 _RandomAccessIterator __last
,
3248 _Distance __chunk_size
)
3250 while (__last
- __first
>= __chunk_size
)
3252 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3253 __first
+= __chunk_size
;
3255 std::__insertion_sort(__first
, __last
);
3258 template<typename _RandomAccessIterator
, typename _Distance
,
3261 __chunk_insertion_sort(_RandomAccessIterator __first
,
3262 _RandomAccessIterator __last
,
3263 _Distance __chunk_size
, _Compare __comp
)
3265 while (__last
- __first
>= __chunk_size
)
3267 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3268 __first
+= __chunk_size
;
3270 std::__insertion_sort(__first
, __last
, __comp
);
3273 enum { _S_chunk_size
= 7 };
3275 template<typename _RandomAccessIterator
, typename _Pointer
>
3277 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3278 _RandomAccessIterator __last
,
3281 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3284 const _Distance __len
= __last
- __first
;
3285 const _Pointer __buffer_last
= __buffer
+ __len
;
3287 _Distance __step_size
= _S_chunk_size
;
3288 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3290 while (__step_size
< __len
)
3292 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3294 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3299 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3301 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3302 _RandomAccessIterator __last
,
3303 _Pointer __buffer
, _Compare __comp
)
3305 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3308 const _Distance __len
= __last
- __first
;
3309 const _Pointer __buffer_last
= __buffer
+ __len
;
3311 _Distance __step_size
= _S_chunk_size
;
3312 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3314 while (__step_size
< __len
)
3316 std::__merge_sort_loop(__first
, __last
, __buffer
,
3317 __step_size
, __comp
);
3319 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3320 __step_size
, __comp
);
3325 template<typename _RandomAccessIterator
, typename _Pointer
,
3328 __stable_sort_adaptive(_RandomAccessIterator __first
,
3329 _RandomAccessIterator __last
,
3330 _Pointer __buffer
, _Distance __buffer_size
)
3332 const _Distance __len
= (__last
- __first
+ 1) / 2;
3333 const _RandomAccessIterator __middle
= __first
+ __len
;
3334 if (__len
> __buffer_size
)
3336 std::__stable_sort_adaptive(__first
, __middle
,
3337 __buffer
, __buffer_size
);
3338 std::__stable_sort_adaptive(__middle
, __last
,
3339 __buffer
, __buffer_size
);
3343 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3344 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3346 std::__merge_adaptive(__first
, __middle
, __last
,
3347 _Distance(__middle
- __first
),
3348 _Distance(__last
- __middle
),
3349 __buffer
, __buffer_size
);
3352 template<typename _RandomAccessIterator
, typename _Pointer
,
3353 typename _Distance
, typename _Compare
>
3355 __stable_sort_adaptive(_RandomAccessIterator __first
,
3356 _RandomAccessIterator __last
,
3357 _Pointer __buffer
, _Distance __buffer_size
,
3360 const _Distance __len
= (__last
- __first
+ 1) / 2;
3361 const _RandomAccessIterator __middle
= __first
+ __len
;
3362 if (__len
> __buffer_size
)
3364 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3365 __buffer_size
, __comp
);
3366 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3367 __buffer_size
, __comp
);
3371 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3372 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3374 std::__merge_adaptive(__first
, __middle
, __last
,
3375 _Distance(__middle
- __first
),
3376 _Distance(__last
- __middle
),
3377 __buffer
, __buffer_size
,
3381 /// This is a helper function for the stable sorting routines.
3382 template<typename _RandomAccessIterator
>
3384 __inplace_stable_sort(_RandomAccessIterator __first
,
3385 _RandomAccessIterator __last
)
3387 if (__last
- __first
< 15)
3389 std::__insertion_sort(__first
, __last
);
3392 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3393 std::__inplace_stable_sort(__first
, __middle
);
3394 std::__inplace_stable_sort(__middle
, __last
);
3395 std::__merge_without_buffer(__first
, __middle
, __last
,
3400 /// This is a helper function for the stable sorting routines.
3401 template<typename _RandomAccessIterator
, typename _Compare
>
3403 __inplace_stable_sort(_RandomAccessIterator __first
,
3404 _RandomAccessIterator __last
, _Compare __comp
)
3406 if (__last
- __first
< 15)
3408 std::__insertion_sort(__first
, __last
, __comp
);
3411 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3412 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3413 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3414 std::__merge_without_buffer(__first
, __middle
, __last
,
3422 // Set algorithms: includes, set_union, set_intersection, set_difference,
3423 // set_symmetric_difference. All of these algorithms have the precondition
3424 // that their input ranges are sorted and the postcondition that their output
3425 // ranges are sorted.
3428 * @brief Determines whether all elements of a sequence exists in a range.
3429 * @param first1 Start of search range.
3430 * @param last1 End of search range.
3431 * @param first2 Start of sequence
3432 * @param last2 End of sequence.
3433 * @return True if each element in [first2,last2) is contained in order
3434 * within [first1,last1). False otherwise.
3435 * @ingroup setoperations
3437 * This operation expects both [first1,last1) and [first2,last2) to be
3438 * sorted. Searches for the presence of each element in [first2,last2)
3439 * within [first1,last1). The iterators over each range only move forward,
3440 * so this is a linear algorithm. If an element in [first2,last2) is not
3441 * found before the search iterator reaches @a last2, false is returned.
3443 template<typename _InputIterator1
, typename _InputIterator2
>
3445 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3446 _InputIterator2 __first2
, _InputIterator2 __last2
)
3448 typedef typename iterator_traits
<_InputIterator1
>::value_type
3450 typedef typename iterator_traits
<_InputIterator2
>::value_type
3453 // concept requirements
3454 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3455 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3456 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3457 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3458 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3459 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3461 while (__first1
!= __last1
&& __first2
!= __last2
)
3462 if (*__first2
< *__first1
)
3464 else if(*__first1
< *__first2
)
3467 ++__first1
, ++__first2
;
3469 return __first2
== __last2
;
3473 * @brief Determines whether all elements of a sequence exists in a range
3475 * @param first1 Start of search range.
3476 * @param last1 End of search range.
3477 * @param first2 Start of sequence
3478 * @param last2 End of sequence.
3479 * @param comp Comparison function to use.
3480 * @return True if each element in [first2,last2) is contained in order
3481 * within [first1,last1) according to comp. False otherwise.
3482 * @ingroup setoperations
3484 * This operation expects both [first1,last1) and [first2,last2) to be
3485 * sorted. Searches for the presence of each element in [first2,last2)
3486 * within [first1,last1), using comp to decide. The iterators over each
3487 * range only move forward, so this is a linear algorithm. If an element
3488 * in [first2,last2) is not found before the search iterator reaches @a
3489 * last2, false is returned.
3491 template<typename _InputIterator1
, typename _InputIterator2
,
3494 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3495 _InputIterator2 __first2
, _InputIterator2 __last2
,
3498 typedef typename iterator_traits
<_InputIterator1
>::value_type
3500 typedef typename iterator_traits
<_InputIterator2
>::value_type
3503 // concept requirements
3504 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3505 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3506 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3507 _ValueType1
, _ValueType2
>)
3508 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3509 _ValueType2
, _ValueType1
>)
3510 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3511 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3513 while (__first1
!= __last1
&& __first2
!= __last2
)
3514 if (__comp(*__first2
, *__first1
))
3516 else if(__comp(*__first1
, *__first2
))
3519 ++__first1
, ++__first2
;
3521 return __first2
== __last2
;
3530 // set_symmetric_difference
3535 * @brief Permute range into the next "dictionary" ordering.
3536 * @param first Start of range.
3537 * @param last End of range.
3538 * @return False if wrapped to first permutation, true otherwise.
3540 * Treats all permutations of the range as a set of "dictionary" sorted
3541 * sequences. Permutes the current sequence into the next one of this set.
3542 * Returns true if there are more sequences to generate. If the sequence
3543 * is the largest of the set, the smallest is generated and false returned.
3545 template<typename _BidirectionalIterator
>
3547 next_permutation(_BidirectionalIterator __first
,
3548 _BidirectionalIterator __last
)
3550 // concept requirements
3551 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3552 _BidirectionalIterator
>)
3553 __glibcxx_function_requires(_LessThanComparableConcept
<
3554 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3555 __glibcxx_requires_valid_range(__first
, __last
);
3557 if (__first
== __last
)
3559 _BidirectionalIterator __i
= __first
;
3568 _BidirectionalIterator __ii
= __i
;
3572 _BidirectionalIterator __j
= __last
;
3573 while (!(*__i
< *--__j
))
3575 std::iter_swap(__i
, __j
);
3576 std::reverse(__ii
, __last
);
3581 std::reverse(__first
, __last
);
3588 * @brief Permute range into the next "dictionary" ordering using
3589 * comparison functor.
3590 * @param first Start of range.
3591 * @param last End of range.
3592 * @param comp A comparison functor.
3593 * @return False if wrapped to first permutation, true otherwise.
3595 * Treats all permutations of the range [first,last) as a set of
3596 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3597 * sequence into the next one of this set. Returns true if there are more
3598 * sequences to generate. If the sequence is the largest of the set, the
3599 * smallest is generated and false returned.
3601 template<typename _BidirectionalIterator
, typename _Compare
>
3603 next_permutation(_BidirectionalIterator __first
,
3604 _BidirectionalIterator __last
, _Compare __comp
)
3606 // concept requirements
3607 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3608 _BidirectionalIterator
>)
3609 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3610 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3611 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3612 __glibcxx_requires_valid_range(__first
, __last
);
3614 if (__first
== __last
)
3616 _BidirectionalIterator __i
= __first
;
3625 _BidirectionalIterator __ii
= __i
;
3627 if (__comp(*__i
, *__ii
))
3629 _BidirectionalIterator __j
= __last
;
3630 while (!bool(__comp(*__i
, *--__j
)))
3632 std::iter_swap(__i
, __j
);
3633 std::reverse(__ii
, __last
);
3638 std::reverse(__first
, __last
);
3645 * @brief Permute range into the previous "dictionary" ordering.
3646 * @param first Start of range.
3647 * @param last End of range.
3648 * @return False if wrapped to last permutation, true otherwise.
3650 * Treats all permutations of the range as a set of "dictionary" sorted
3651 * sequences. Permutes the current sequence into the previous one of this
3652 * set. Returns true if there are more sequences to generate. If the
3653 * sequence is the smallest of the set, the largest is generated and false
3656 template<typename _BidirectionalIterator
>
3658 prev_permutation(_BidirectionalIterator __first
,
3659 _BidirectionalIterator __last
)
3661 // concept requirements
3662 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3663 _BidirectionalIterator
>)
3664 __glibcxx_function_requires(_LessThanComparableConcept
<
3665 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3666 __glibcxx_requires_valid_range(__first
, __last
);
3668 if (__first
== __last
)
3670 _BidirectionalIterator __i
= __first
;
3679 _BidirectionalIterator __ii
= __i
;
3683 _BidirectionalIterator __j
= __last
;
3684 while (!(*--__j
< *__i
))
3686 std::iter_swap(__i
, __j
);
3687 std::reverse(__ii
, __last
);
3692 std::reverse(__first
, __last
);
3699 * @brief Permute range into the previous "dictionary" ordering using
3700 * comparison functor.
3701 * @param first Start of range.
3702 * @param last End of range.
3703 * @param comp A comparison functor.
3704 * @return False if wrapped to last permutation, true otherwise.
3706 * Treats all permutations of the range [first,last) as a set of
3707 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3708 * sequence into the previous one of this set. Returns true if there are
3709 * more sequences to generate. If the sequence is the smallest of the set,
3710 * the largest is generated and false returned.
3712 template<typename _BidirectionalIterator
, typename _Compare
>
3714 prev_permutation(_BidirectionalIterator __first
,
3715 _BidirectionalIterator __last
, _Compare __comp
)
3717 // concept requirements
3718 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3719 _BidirectionalIterator
>)
3720 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3721 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3722 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3723 __glibcxx_requires_valid_range(__first
, __last
);
3725 if (__first
== __last
)
3727 _BidirectionalIterator __i
= __first
;
3736 _BidirectionalIterator __ii
= __i
;
3738 if (__comp(*__ii
, *__i
))
3740 _BidirectionalIterator __j
= __last
;
3741 while (!bool(__comp(*--__j
, *__i
)))
3743 std::iter_swap(__i
, __j
);
3744 std::reverse(__ii
, __last
);
3749 std::reverse(__first
, __last
);
3759 * @brief Copy a sequence, replacing each element of one value with another
3761 * @param first An input iterator.
3762 * @param last An input iterator.
3763 * @param result An output iterator.
3764 * @param old_value The value to be replaced.
3765 * @param new_value The replacement value.
3766 * @return The end of the output sequence, @p result+(last-first).
3768 * Copies each element in the input range @p [first,last) to the
3769 * output range @p [result,result+(last-first)) replacing elements
3770 * equal to @p old_value with @p new_value.
3772 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3774 replace_copy(_InputIterator __first
, _InputIterator __last
,
3775 _OutputIterator __result
,
3776 const _Tp
& __old_value
, const _Tp
& __new_value
)
3778 // concept requirements
3779 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3780 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3781 typename iterator_traits
<_InputIterator
>::value_type
>)
3782 __glibcxx_function_requires(_EqualOpConcept
<
3783 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3784 __glibcxx_requires_valid_range(__first
, __last
);
3786 for (; __first
!= __last
; ++__first
, ++__result
)
3787 if (*__first
== __old_value
)
3788 *__result
= __new_value
;
3790 *__result
= *__first
;
3795 * @brief Copy a sequence, replacing each value for which a predicate
3796 * returns true with another value.
3797 * @param first An input iterator.
3798 * @param last An input iterator.
3799 * @param result An output iterator.
3800 * @param pred A predicate.
3801 * @param new_value The replacement value.
3802 * @return The end of the output sequence, @p result+(last-first).
3804 * Copies each element in the range @p [first,last) to the range
3805 * @p [result,result+(last-first)) replacing elements for which
3806 * @p pred returns true with @p new_value.
3808 template<typename _InputIterator
, typename _OutputIterator
,
3809 typename _Predicate
, typename _Tp
>
3811 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3812 _OutputIterator __result
,
3813 _Predicate __pred
, const _Tp
& __new_value
)
3815 // concept requirements
3816 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3817 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3818 typename iterator_traits
<_InputIterator
>::value_type
>)
3819 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3820 typename iterator_traits
<_InputIterator
>::value_type
>)
3821 __glibcxx_requires_valid_range(__first
, __last
);
3823 for (; __first
!= __last
; ++__first
, ++__result
)
3824 if (__pred(*__first
))
3825 *__result
= __new_value
;
3827 *__result
= *__first
;
3831 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3833 * @brief Determines whether the elements of a sequence are sorted.
3834 * @param first An iterator.
3835 * @param last Another iterator.
3836 * @return True if the elements are sorted, false otherwise.
3838 template<typename _ForwardIterator
>
3840 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3841 { return std::is_sorted_until(__first
, __last
) == __last
; }
3844 * @brief Determines whether the elements of a sequence are sorted
3845 * according to a comparison functor.
3846 * @param first An iterator.
3847 * @param last Another iterator.
3848 * @param comp A comparison functor.
3849 * @return True if the elements are sorted, false otherwise.
3851 template<typename _ForwardIterator
, typename _Compare
>
3853 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3855 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3858 * @brief Determines the end of a sorted sequence.
3859 * @param first An iterator.
3860 * @param last Another iterator.
3861 * @return An iterator pointing to the last iterator i in [first, last)
3862 * for which the range [first, i) is sorted.
3864 template<typename _ForwardIterator
>
3866 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3868 // concept requirements
3869 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3870 __glibcxx_function_requires(_LessThanComparableConcept
<
3871 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3872 __glibcxx_requires_valid_range(__first
, __last
);
3874 if (__first
== __last
)
3877 _ForwardIterator __next
= __first
;
3878 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3879 if (*__next
< *__first
)
3885 * @brief Determines the end of a sorted sequence using comparison functor.
3886 * @param first An iterator.
3887 * @param last Another iterator.
3888 * @param comp A comparison functor.
3889 * @return An iterator pointing to the last iterator i in [first, last)
3890 * for which the range [first, i) is sorted.
3892 template<typename _ForwardIterator
, typename _Compare
>
3894 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3897 // concept requirements
3898 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3899 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3900 typename iterator_traits
<_ForwardIterator
>::value_type
,
3901 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3902 __glibcxx_requires_valid_range(__first
, __last
);
3904 if (__first
== __last
)
3907 _ForwardIterator __next
= __first
;
3908 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3909 if (__comp(*__next
, *__first
))
3915 * @brief Determines min and max at once as an ordered pair.
3916 * @param a A thing of arbitrary type.
3917 * @param b Another thing of arbitrary type.
3918 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3920 template<typename _Tp
>
3921 inline pair
<const _Tp
&, const _Tp
&>
3922 minmax(const _Tp
& __a
, const _Tp
& __b
)
3924 // concept requirements
3925 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3927 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3928 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3932 * @brief Determines min and max at once as an ordered pair.
3933 * @param a A thing of arbitrary type.
3934 * @param b Another thing of arbitrary type.
3935 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3936 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3938 template<typename _Tp
, typename _Compare
>
3939 inline pair
<const _Tp
&, const _Tp
&>
3940 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3942 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3943 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3947 * @brief Return a pair of iterators pointing to the minimum and maximum
3948 * elements in a range.
3949 * @param first Start of range.
3950 * @param last End of range.
3951 * @return make_pair(m, M), where m is the first iterator i in
3952 * [first, last) such that no other element in the range is
3953 * smaller, and where M is the last iterator i in [first, last)
3954 * such that no other element in the range is larger.
3956 template<typename _ForwardIterator
>
3957 pair
<_ForwardIterator
, _ForwardIterator
>
3958 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3960 // concept requirements
3961 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3962 __glibcxx_function_requires(_LessThanComparableConcept
<
3963 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3964 __glibcxx_requires_valid_range(__first
, __last
);
3966 _ForwardIterator __next
= __first
;
3967 if (__first
== __last
3968 || ++__next
== __last
)
3969 return std::make_pair(__first
, __first
);
3971 _ForwardIterator __min
, __max
;
3972 if (*__next
< *__first
)
3986 while (__first
!= __last
)
3989 if (++__next
== __last
)
3991 if (*__first
< *__min
)
3993 else if (!(*__first
< *__max
))
3998 if (*__next
< *__first
)
4000 if (*__next
< *__min
)
4002 if (!(*__first
< *__max
))
4007 if (*__first
< *__min
)
4009 if (!(*__next
< *__max
))
4017 return std::make_pair(__min
, __max
);
4021 * @brief Return a pair of iterators pointing to the minimum and maximum
4022 * elements in a range.
4023 * @param first Start of range.
4024 * @param last End of range.
4025 * @param comp Comparison functor.
4026 * @return make_pair(m, M), where m is the first iterator i in
4027 * [first, last) such that no other element in the range is
4028 * smaller, and where M is the last iterator i in [first, last)
4029 * such that no other element in the range is larger.
4031 template<typename _ForwardIterator
, typename _Compare
>
4032 pair
<_ForwardIterator
, _ForwardIterator
>
4033 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4036 // concept requirements
4037 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4038 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4039 typename iterator_traits
<_ForwardIterator
>::value_type
,
4040 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4041 __glibcxx_requires_valid_range(__first
, __last
);
4043 _ForwardIterator __next
= __first
;
4044 if (__first
== __last
4045 || ++__next
== __last
)
4046 return std::make_pair(__first
, __first
);
4048 _ForwardIterator __min
, __max
;
4049 if (__comp(*__next
, *__first
))
4063 while (__first
!= __last
)
4066 if (++__next
== __last
)
4068 if (__comp(*__first
, *__min
))
4070 else if (!__comp(*__first
, *__max
))
4075 if (__comp(*__next
, *__first
))
4077 if (__comp(*__next
, *__min
))
4079 if (!__comp(*__first
, *__max
))
4084 if (__comp(*__first
, *__min
))
4086 if (!__comp(*__next
, *__max
))
4094 return std::make_pair(__min
, __max
);
4098 template<typename _Tp
>
4100 min(initializer_list
<_Tp
> __l
)
4101 { return *std::min_element(__l
.begin(), __l
.end()); }
4103 template<typename _Tp
, typename _Compare
>
4105 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4106 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4108 template<typename _Tp
>
4110 max(initializer_list
<_Tp
> __l
)
4111 { return *std::max_element(__l
.begin(), __l
.end()); }
4113 template<typename _Tp
, typename _Compare
>
4115 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4116 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4118 template<typename _Tp
>
4119 inline pair
<_Tp
, _Tp
>
4120 minmax(initializer_list
<_Tp
> __l
)
4122 pair
<const _Tp
*, const _Tp
*> __p
=
4123 std::minmax_element(__l
.begin(), __l
.end());
4124 return std::pair
<_Tp
, _Tp
>(*__p
.first
, *__p
.second
);
4127 template<typename _Tp
, typename _Compare
>
4128 inline pair
<_Tp
, _Tp
>
4129 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4131 pair
<const _Tp
*, const _Tp
*> __p
=
4132 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4133 return std::make_pair(*__p
.first
, *__p
.second
);
4135 #endif // __GXX_EXPERIMENTAL_CXX0X__
4137 _GLIBCXX_END_NAMESPACE
4139 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4142 * @brief Apply a function to every element of a sequence.
4143 * @param first An input iterator.
4144 * @param last An input iterator.
4145 * @param f A unary function object.
4148 * Applies the function object @p f to each element in the range
4149 * @p [first,last). @p f must not modify the order of the sequence.
4150 * If @p f has a return value it is ignored.
4152 template<typename _InputIterator
, typename _Function
>
4154 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4156 // concept requirements
4157 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4158 __glibcxx_requires_valid_range(__first
, __last
);
4159 for (; __first
!= __last
; ++__first
)
4165 * @brief Find the first occurrence of a value in a sequence.
4166 * @param first An input iterator.
4167 * @param last An input iterator.
4168 * @param val The value to find.
4169 * @return The first iterator @c i in the range @p [first,last)
4170 * such that @c *i == @p val, or @p last if no such iterator exists.
4172 template<typename _InputIterator
, typename _Tp
>
4173 inline _InputIterator
4174 find(_InputIterator __first
, _InputIterator __last
,
4177 // concept requirements
4178 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4179 __glibcxx_function_requires(_EqualOpConcept
<
4180 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4181 __glibcxx_requires_valid_range(__first
, __last
);
4182 return std::__find(__first
, __last
, __val
,
4183 std::__iterator_category(__first
));
4187 * @brief Find the first element in a sequence for which a
4188 * predicate is true.
4189 * @param first An input iterator.
4190 * @param last An input iterator.
4191 * @param pred A predicate.
4192 * @return The first iterator @c i in the range @p [first,last)
4193 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4195 template<typename _InputIterator
, typename _Predicate
>
4196 inline _InputIterator
4197 find_if(_InputIterator __first
, _InputIterator __last
,
4200 // concept requirements
4201 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4202 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4203 typename iterator_traits
<_InputIterator
>::value_type
>)
4204 __glibcxx_requires_valid_range(__first
, __last
);
4205 return std::__find_if(__first
, __last
, __pred
,
4206 std::__iterator_category(__first
));
4210 * @brief Find element from a set in a sequence.
4211 * @param first1 Start of range to search.
4212 * @param last1 End of range to search.
4213 * @param first2 Start of match candidates.
4214 * @param last2 End of match candidates.
4215 * @return The first iterator @c i in the range
4216 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4217 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4219 * Searches the range @p [first1,last1) for an element that is equal to
4220 * some element in the range [first2,last2). If found, returns an iterator
4221 * in the range [first1,last1), otherwise returns @p last1.
4223 template<typename _InputIterator
, typename _ForwardIterator
>
4225 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4226 _ForwardIterator __first2
, _ForwardIterator __last2
)
4228 // concept requirements
4229 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4230 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4231 __glibcxx_function_requires(_EqualOpConcept
<
4232 typename iterator_traits
<_InputIterator
>::value_type
,
4233 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4234 __glibcxx_requires_valid_range(__first1
, __last1
);
4235 __glibcxx_requires_valid_range(__first2
, __last2
);
4237 for (; __first1
!= __last1
; ++__first1
)
4238 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4239 if (*__first1
== *__iter
)
4245 * @brief Find element from a set in a sequence using a predicate.
4246 * @param first1 Start of range to search.
4247 * @param last1 End of range to search.
4248 * @param first2 Start of match candidates.
4249 * @param last2 End of match candidates.
4250 * @param comp Predicate to use.
4251 * @return The first iterator @c i in the range
4252 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4253 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4256 * Searches the range @p [first1,last1) for an element that is
4257 * equal to some element in the range [first2,last2). If found,
4258 * returns an iterator in the range [first1,last1), otherwise
4261 template<typename _InputIterator
, typename _ForwardIterator
,
4262 typename _BinaryPredicate
>
4264 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4265 _ForwardIterator __first2
, _ForwardIterator __last2
,
4266 _BinaryPredicate __comp
)
4268 // concept requirements
4269 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4270 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4271 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4272 typename iterator_traits
<_InputIterator
>::value_type
,
4273 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4274 __glibcxx_requires_valid_range(__first1
, __last1
);
4275 __glibcxx_requires_valid_range(__first2
, __last2
);
4277 for (; __first1
!= __last1
; ++__first1
)
4278 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4279 if (__comp(*__first1
, *__iter
))
4285 * @brief Find two adjacent values in a sequence that are equal.
4286 * @param first A forward iterator.
4287 * @param last A forward iterator.
4288 * @return The first iterator @c i such that @c i and @c i+1 are both
4289 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4290 * or @p last if no such iterator exists.
4292 template<typename _ForwardIterator
>
4294 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4296 // concept requirements
4297 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4298 __glibcxx_function_requires(_EqualityComparableConcept
<
4299 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4300 __glibcxx_requires_valid_range(__first
, __last
);
4301 if (__first
== __last
)
4303 _ForwardIterator __next
= __first
;
4304 while(++__next
!= __last
)
4306 if (*__first
== *__next
)
4314 * @brief Find two adjacent values in a sequence using a predicate.
4315 * @param first A forward iterator.
4316 * @param last A forward iterator.
4317 * @param binary_pred A binary predicate.
4318 * @return The first iterator @c i such that @c i and @c i+1 are both
4319 * valid iterators in @p [first,last) and such that
4320 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4323 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4325 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4326 _BinaryPredicate __binary_pred
)
4328 // concept requirements
4329 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4330 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4331 typename iterator_traits
<_ForwardIterator
>::value_type
,
4332 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4333 __glibcxx_requires_valid_range(__first
, __last
);
4334 if (__first
== __last
)
4336 _ForwardIterator __next
= __first
;
4337 while(++__next
!= __last
)
4339 if (__binary_pred(*__first
, *__next
))
4347 * @brief Count the number of copies of a value in a sequence.
4348 * @param first An input iterator.
4349 * @param last An input iterator.
4350 * @param value The value to be counted.
4351 * @return The number of iterators @c i in the range @p [first,last)
4352 * for which @c *i == @p value
4354 template<typename _InputIterator
, typename _Tp
>
4355 typename iterator_traits
<_InputIterator
>::difference_type
4356 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4358 // concept requirements
4359 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4360 __glibcxx_function_requires(_EqualOpConcept
<
4361 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4362 __glibcxx_requires_valid_range(__first
, __last
);
4363 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4364 for (; __first
!= __last
; ++__first
)
4365 if (*__first
== __value
)
4371 * @brief Count the elements of a sequence for which a predicate is true.
4372 * @param first An input iterator.
4373 * @param last An input iterator.
4374 * @param pred A predicate.
4375 * @return The number of iterators @c i in the range @p [first,last)
4376 * for which @p pred(*i) is true.
4378 template<typename _InputIterator
, typename _Predicate
>
4379 typename iterator_traits
<_InputIterator
>::difference_type
4380 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4382 // concept requirements
4383 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4384 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4385 typename iterator_traits
<_InputIterator
>::value_type
>)
4386 __glibcxx_requires_valid_range(__first
, __last
);
4387 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4388 for (; __first
!= __last
; ++__first
)
4389 if (__pred(*__first
))
4395 * @brief Search a sequence for a matching sub-sequence.
4396 * @param first1 A forward iterator.
4397 * @param last1 A forward iterator.
4398 * @param first2 A forward iterator.
4399 * @param last2 A forward iterator.
4400 * @return The first iterator @c i in the range
4401 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4402 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4403 * such iterator exists.
4405 * Searches the range @p [first1,last1) for a sub-sequence that compares
4406 * equal value-by-value with the sequence given by @p [first2,last2) and
4407 * returns an iterator to the first element of the sub-sequence, or
4408 * @p last1 if the sub-sequence is not found.
4410 * Because the sub-sequence must lie completely within the range
4411 * @p [first1,last1) it must start at a position less than
4412 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4414 * This means that the returned iterator @c i will be in the range
4415 * @p [first1,last1-(last2-first2))
4417 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4419 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4420 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4422 // concept requirements
4423 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4424 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4425 __glibcxx_function_requires(_EqualOpConcept
<
4426 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4427 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4428 __glibcxx_requires_valid_range(__first1
, __last1
);
4429 __glibcxx_requires_valid_range(__first2
, __last2
);
4431 // Test for empty ranges
4432 if (__first1
== __last1
|| __first2
== __last2
)
4435 // Test for a pattern of length 1.
4436 _ForwardIterator2
__p1(__first2
);
4437 if (++__p1
== __last2
)
4438 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4441 _ForwardIterator2 __p
;
4442 _ForwardIterator1 __current
= __first1
;
4446 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4447 if (__first1
== __last1
)
4451 __current
= __first1
;
4452 if (++__current
== __last1
)
4455 while (*__current
== *__p
)
4457 if (++__p
== __last2
)
4459 if (++__current
== __last1
)
4468 * @brief Search a sequence for a matching sub-sequence using a predicate.
4469 * @param first1 A forward iterator.
4470 * @param last1 A forward iterator.
4471 * @param first2 A forward iterator.
4472 * @param last2 A forward iterator.
4473 * @param predicate A binary predicate.
4474 * @return The first iterator @c i in the range
4475 * @p [first1,last1-(last2-first2)) such that
4476 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4477 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4479 * Searches the range @p [first1,last1) for a sub-sequence that compares
4480 * equal value-by-value with the sequence given by @p [first2,last2),
4481 * using @p predicate to determine equality, and returns an iterator
4482 * to the first element of the sub-sequence, or @p last1 if no such
4485 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4487 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4488 typename _BinaryPredicate
>
4490 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4491 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4492 _BinaryPredicate __predicate
)
4494 // concept requirements
4495 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4496 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4497 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4498 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4499 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4500 __glibcxx_requires_valid_range(__first1
, __last1
);
4501 __glibcxx_requires_valid_range(__first2
, __last2
);
4503 // Test for empty ranges
4504 if (__first1
== __last1
|| __first2
== __last2
)
4507 // Test for a pattern of length 1.
4508 _ForwardIterator2
__p1(__first2
);
4509 if (++__p1
== __last2
)
4511 while (__first1
!= __last1
4512 && !bool(__predicate(*__first1
, *__first2
)))
4518 _ForwardIterator2 __p
;
4519 _ForwardIterator1 __current
= __first1
;
4523 while (__first1
!= __last1
4524 && !bool(__predicate(*__first1
, *__first2
)))
4526 if (__first1
== __last1
)
4530 __current
= __first1
;
4531 if (++__current
== __last1
)
4534 while (__predicate(*__current
, *__p
))
4536 if (++__p
== __last2
)
4538 if (++__current
== __last1
)
4548 * @brief Search a sequence for a number of consecutive values.
4549 * @param first A forward iterator.
4550 * @param last A forward iterator.
4551 * @param count The number of consecutive values.
4552 * @param val The value to find.
4553 * @return The first iterator @c i in the range @p [first,last-count)
4554 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4555 * or @p last if no such iterator exists.
4557 * Searches the range @p [first,last) for @p count consecutive elements
4560 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4562 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4563 _Integer __count
, const _Tp
& __val
)
4565 // concept requirements
4566 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4567 __glibcxx_function_requires(_EqualOpConcept
<
4568 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4569 __glibcxx_requires_valid_range(__first
, __last
);
4574 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4575 return std::__search_n(__first
, __last
, __count
, __val
,
4576 std::__iterator_category(__first
));
4581 * @brief Search a sequence for a number of consecutive values using a
4583 * @param first A forward iterator.
4584 * @param last A forward iterator.
4585 * @param count The number of consecutive values.
4586 * @param val The value to find.
4587 * @param binary_pred A binary predicate.
4588 * @return The first iterator @c i in the range @p [first,last-count)
4589 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4590 * range @p [0,count), or @p last if no such iterator exists.
4592 * Searches the range @p [first,last) for @p count consecutive elements
4593 * for which the predicate returns true.
4595 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4596 typename _BinaryPredicate
>
4598 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4599 _Integer __count
, const _Tp
& __val
,
4600 _BinaryPredicate __binary_pred
)
4602 // concept requirements
4603 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4604 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4605 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4606 __glibcxx_requires_valid_range(__first
, __last
);
4612 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4616 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4617 std::__iterator_category(__first
));
4622 * @brief Perform an operation on a sequence.
4623 * @param first An input iterator.
4624 * @param last An input iterator.
4625 * @param result An output iterator.
4626 * @param unary_op A unary operator.
4627 * @return An output iterator equal to @p result+(last-first).
4629 * Applies the operator to each element in the input range and assigns
4630 * the results to successive elements of the output sequence.
4631 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4632 * range @p [0,last-first).
4634 * @p unary_op must not alter its argument.
4636 template<typename _InputIterator
, typename _OutputIterator
,
4637 typename _UnaryOperation
>
4639 transform(_InputIterator __first
, _InputIterator __last
,
4640 _OutputIterator __result
, _UnaryOperation __unary_op
)
4642 // concept requirements
4643 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4644 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4645 // "the type returned by a _UnaryOperation"
4646 __typeof__(__unary_op(*__first
))>)
4647 __glibcxx_requires_valid_range(__first
, __last
);
4649 for (; __first
!= __last
; ++__first
, ++__result
)
4650 *__result
= __unary_op(*__first
);
4655 * @brief Perform an operation on corresponding elements of two sequences.
4656 * @param first1 An input iterator.
4657 * @param last1 An input iterator.
4658 * @param first2 An input iterator.
4659 * @param result An output iterator.
4660 * @param binary_op A binary operator.
4661 * @return An output iterator equal to @p result+(last-first).
4663 * Applies the operator to the corresponding elements in the two
4664 * input ranges and assigns the results to successive elements of the
4666 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4667 * @c N in the range @p [0,last1-first1).
4669 * @p binary_op must not alter either of its arguments.
4671 template<typename _InputIterator1
, typename _InputIterator2
,
4672 typename _OutputIterator
, typename _BinaryOperation
>
4674 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4675 _InputIterator2 __first2
, _OutputIterator __result
,
4676 _BinaryOperation __binary_op
)
4678 // concept requirements
4679 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4680 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4681 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4682 // "the type returned by a _BinaryOperation"
4683 __typeof__(__binary_op(*__first1
,*__first2
))>)
4684 __glibcxx_requires_valid_range(__first1
, __last1
);
4686 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4687 *__result
= __binary_op(*__first1
, *__first2
);
4692 * @brief Replace each occurrence of one value in a sequence with another
4694 * @param first A forward iterator.
4695 * @param last A forward iterator.
4696 * @param old_value The value to be replaced.
4697 * @param new_value The replacement value.
4698 * @return replace() returns no value.
4700 * For each iterator @c i in the range @p [first,last) if @c *i ==
4701 * @p old_value then the assignment @c *i = @p new_value is performed.
4703 template<typename _ForwardIterator
, typename _Tp
>
4705 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4706 const _Tp
& __old_value
, const _Tp
& __new_value
)
4708 // concept requirements
4709 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4711 __glibcxx_function_requires(_EqualOpConcept
<
4712 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4713 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4714 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4715 __glibcxx_requires_valid_range(__first
, __last
);
4717 for (; __first
!= __last
; ++__first
)
4718 if (*__first
== __old_value
)
4719 *__first
= __new_value
;
4723 * @brief Replace each value in a sequence for which a predicate returns
4724 * true with another value.
4725 * @param first A forward iterator.
4726 * @param last A forward iterator.
4727 * @param pred A predicate.
4728 * @param new_value The replacement value.
4729 * @return replace_if() returns no value.
4731 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4732 * is true then the assignment @c *i = @p new_value is performed.
4734 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4736 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4737 _Predicate __pred
, const _Tp
& __new_value
)
4739 // concept requirements
4740 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4742 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4743 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4744 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4745 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4746 __glibcxx_requires_valid_range(__first
, __last
);
4748 for (; __first
!= __last
; ++__first
)
4749 if (__pred(*__first
))
4750 *__first
= __new_value
;
4754 * @brief Assign the result of a function object to each value in a
4756 * @param first A forward iterator.
4757 * @param last A forward iterator.
4758 * @param gen A function object taking no arguments and returning
4759 * std::iterator_traits<_ForwardIterator>::value_type
4760 * @return generate() returns no value.
4762 * Performs the assignment @c *i = @p gen() for each @c i in the range
4765 template<typename _ForwardIterator
, typename _Generator
>
4767 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4770 // concept requirements
4771 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4772 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4773 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4774 __glibcxx_requires_valid_range(__first
, __last
);
4776 for (; __first
!= __last
; ++__first
)
4781 * @brief Assign the result of a function object to each value in a
4783 * @param first A forward iterator.
4784 * @param n The length of the sequence.
4785 * @param gen A function object taking no arguments and returning
4786 * std::iterator_traits<_ForwardIterator>::value_type
4787 * @return The end of the sequence, @p first+n
4789 * Performs the assignment @c *i = @p gen() for each @c i in the range
4790 * @p [first,first+n).
4792 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4794 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4796 // concept requirements
4797 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4798 // "the type returned by a _Generator"
4799 __typeof__(__gen())>)
4801 for (; __n
> 0; --__n
, ++__first
)
4808 * @brief Copy a sequence, removing consecutive duplicate values.
4809 * @param first An input iterator.
4810 * @param last An input iterator.
4811 * @param result An output iterator.
4812 * @return An iterator designating the end of the resulting sequence.
4814 * Copies each element in the range @p [first,last) to the range
4815 * beginning at @p result, except that only the first element is copied
4816 * from groups of consecutive elements that compare equal.
4817 * unique_copy() is stable, so the relative order of elements that are
4818 * copied is unchanged.
4820 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4821 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4823 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4824 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4827 template<typename _InputIterator
, typename _OutputIterator
>
4828 inline _OutputIterator
4829 unique_copy(_InputIterator __first
, _InputIterator __last
,
4830 _OutputIterator __result
)
4832 // concept requirements
4833 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4834 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4835 typename iterator_traits
<_InputIterator
>::value_type
>)
4836 __glibcxx_function_requires(_EqualityComparableConcept
<
4837 typename iterator_traits
<_InputIterator
>::value_type
>)
4838 __glibcxx_requires_valid_range(__first
, __last
);
4840 if (__first
== __last
)
4842 return std::__unique_copy(__first
, __last
, __result
,
4843 std::__iterator_category(__first
),
4844 std::__iterator_category(__result
));
4848 * @brief Copy a sequence, removing consecutive values using a predicate.
4849 * @param first An input iterator.
4850 * @param last An input iterator.
4851 * @param result An output iterator.
4852 * @param binary_pred A binary predicate.
4853 * @return An iterator designating the end of the resulting sequence.
4855 * Copies each element in the range @p [first,last) to the range
4856 * beginning at @p result, except that only the first element is copied
4857 * from groups of consecutive elements for which @p binary_pred returns
4859 * unique_copy() is stable, so the relative order of elements that are
4860 * copied is unchanged.
4862 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4863 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4865 template<typename _InputIterator
, typename _OutputIterator
,
4866 typename _BinaryPredicate
>
4867 inline _OutputIterator
4868 unique_copy(_InputIterator __first
, _InputIterator __last
,
4869 _OutputIterator __result
,
4870 _BinaryPredicate __binary_pred
)
4872 // concept requirements -- predicates checked later
4873 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4874 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4875 typename iterator_traits
<_InputIterator
>::value_type
>)
4876 __glibcxx_requires_valid_range(__first
, __last
);
4878 if (__first
== __last
)
4880 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4881 std::__iterator_category(__first
),
4882 std::__iterator_category(__result
));
4887 * @brief Randomly shuffle the elements of a sequence.
4888 * @param first A forward iterator.
4889 * @param last A forward iterator.
4892 * Reorder the elements in the range @p [first,last) using a random
4893 * distribution, so that every possible ordering of the sequence is
4896 template<typename _RandomAccessIterator
>
4898 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4900 // concept requirements
4901 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4902 _RandomAccessIterator
>)
4903 __glibcxx_requires_valid_range(__first
, __last
);
4905 if (__first
!= __last
)
4906 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4907 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4911 * @brief Shuffle the elements of a sequence using a random number
4913 * @param first A forward iterator.
4914 * @param last A forward iterator.
4915 * @param rand The RNG functor or function.
4918 * Reorders the elements in the range @p [first,last) using @p rand to
4919 * provide a random distribution. Calling @p rand(N) for a positive
4920 * integer @p N should return a randomly chosen integer from the
4923 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4925 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4926 _RandomNumberGenerator
& __rand
)
4928 // concept requirements
4929 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4930 _RandomAccessIterator
>)
4931 __glibcxx_requires_valid_range(__first
, __last
);
4933 if (__first
== __last
)
4935 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4936 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4941 * @brief Move elements for which a predicate is true to the beginning
4943 * @param first A forward iterator.
4944 * @param last A forward iterator.
4945 * @param pred A predicate functor.
4946 * @return An iterator @p middle such that @p pred(i) is true for each
4947 * iterator @p i in the range @p [first,middle) and false for each @p i
4948 * in the range @p [middle,last).
4950 * @p pred must not modify its operand. @p partition() does not preserve
4951 * the relative ordering of elements in each group, use
4952 * @p stable_partition() if this is needed.
4954 template<typename _ForwardIterator
, typename _Predicate
>
4955 inline _ForwardIterator
4956 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4959 // concept requirements
4960 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4962 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4963 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4964 __glibcxx_requires_valid_range(__first
, __last
);
4966 return std::__partition(__first
, __last
, __pred
,
4967 std::__iterator_category(__first
));
4973 * @brief Sort the smallest elements of a sequence.
4974 * @param first An iterator.
4975 * @param middle Another iterator.
4976 * @param last Another iterator.
4979 * Sorts the smallest @p (middle-first) elements in the range
4980 * @p [first,last) and moves them to the range @p [first,middle). The
4981 * order of the remaining elements in the range @p [middle,last) is
4983 * After the sort if @p i and @j are iterators in the range
4984 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4985 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4987 template<typename _RandomAccessIterator
>
4989 partial_sort(_RandomAccessIterator __first
,
4990 _RandomAccessIterator __middle
,
4991 _RandomAccessIterator __last
)
4993 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4996 // concept requirements
4997 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4998 _RandomAccessIterator
>)
4999 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5000 __glibcxx_requires_valid_range(__first
, __middle
);
5001 __glibcxx_requires_valid_range(__middle
, __last
);
5003 std::__heap_select(__first
, __middle
, __last
);
5004 std::sort_heap(__first
, __middle
);
5008 * @brief Sort the smallest elements of a sequence using a predicate
5010 * @param first An iterator.
5011 * @param middle Another iterator.
5012 * @param last Another iterator.
5013 * @param comp A comparison functor.
5016 * Sorts the smallest @p (middle-first) elements in the range
5017 * @p [first,last) and moves them to the range @p [first,middle). The
5018 * order of the remaining elements in the range @p [middle,last) is
5020 * After the sort if @p i and @j are iterators in the range
5021 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5022 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5025 template<typename _RandomAccessIterator
, typename _Compare
>
5027 partial_sort(_RandomAccessIterator __first
,
5028 _RandomAccessIterator __middle
,
5029 _RandomAccessIterator __last
,
5032 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5035 // concept requirements
5036 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5037 _RandomAccessIterator
>)
5038 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5039 _ValueType
, _ValueType
>)
5040 __glibcxx_requires_valid_range(__first
, __middle
);
5041 __glibcxx_requires_valid_range(__middle
, __last
);
5043 std::__heap_select(__first
, __middle
, __last
, __comp
);
5044 std::sort_heap(__first
, __middle
, __comp
);
5048 * @brief Sort a sequence just enough to find a particular position.
5049 * @param first An iterator.
5050 * @param nth Another iterator.
5051 * @param last Another iterator.
5054 * Rearranges the elements in the range @p [first,last) so that @p *nth
5055 * is the same element that would have been in that position had the
5056 * whole sequence been sorted.
5057 * whole sequence been sorted. The elements either side of @p *nth are
5058 * not completely sorted, but for any iterator @i in the range
5059 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5060 * holds that @p *j<*i is false.
5062 template<typename _RandomAccessIterator
>
5064 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5065 _RandomAccessIterator __last
)
5067 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5070 // concept requirements
5071 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5072 _RandomAccessIterator
>)
5073 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5074 __glibcxx_requires_valid_range(__first
, __nth
);
5075 __glibcxx_requires_valid_range(__nth
, __last
);
5077 if (__first
== __last
|| __nth
== __last
)
5080 std::__introselect(__first
, __nth
, __last
,
5081 std::__lg(__last
- __first
) * 2);
5085 * @brief Sort a sequence just enough to find a particular position
5086 * using a predicate for comparison.
5087 * @param first An iterator.
5088 * @param nth Another iterator.
5089 * @param last Another iterator.
5090 * @param comp A comparison functor.
5093 * Rearranges the elements in the range @p [first,last) so that @p *nth
5094 * is the same element that would have been in that position had the
5095 * whole sequence been sorted. The elements either side of @p *nth are
5096 * not completely sorted, but for any iterator @i in the range
5097 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5098 * holds that @p comp(*j,*i) is false.
5100 template<typename _RandomAccessIterator
, typename _Compare
>
5102 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5103 _RandomAccessIterator __last
, _Compare __comp
)
5105 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5108 // concept requirements
5109 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5110 _RandomAccessIterator
>)
5111 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5112 _ValueType
, _ValueType
>)
5113 __glibcxx_requires_valid_range(__first
, __nth
);
5114 __glibcxx_requires_valid_range(__nth
, __last
);
5116 if (__first
== __last
|| __nth
== __last
)
5119 std::__introselect(__first
, __nth
, __last
,
5120 std::__lg(__last
- __first
) * 2, __comp
);
5125 * @brief Sort the elements of a sequence.
5126 * @param first An iterator.
5127 * @param last Another iterator.
5130 * Sorts the elements in the range @p [first,last) in ascending order,
5131 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5132 * @p [first,last-1).
5134 * The relative ordering of equivalent elements is not preserved, use
5135 * @p stable_sort() if this is needed.
5137 template<typename _RandomAccessIterator
>
5139 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5141 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5144 // concept requirements
5145 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5146 _RandomAccessIterator
>)
5147 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5148 __glibcxx_requires_valid_range(__first
, __last
);
5150 if (__first
!= __last
)
5152 std::__introsort_loop(__first
, __last
,
5153 std::__lg(__last
- __first
) * 2);
5154 std::__final_insertion_sort(__first
, __last
);
5159 * @brief Sort the elements of a sequence using a predicate for comparison.
5160 * @param first An iterator.
5161 * @param last Another iterator.
5162 * @param comp A comparison functor.
5165 * Sorts the elements in the range @p [first,last) in ascending order,
5166 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5167 * range @p [first,last-1).
5169 * The relative ordering of equivalent elements is not preserved, use
5170 * @p stable_sort() if this is needed.
5172 template<typename _RandomAccessIterator
, typename _Compare
>
5174 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
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
, _ValueType
,
5185 __glibcxx_requires_valid_range(__first
, __last
);
5187 if (__first
!= __last
)
5189 std::__introsort_loop(__first
, __last
,
5190 std::__lg(__last
- __first
) * 2, __comp
);
5191 std::__final_insertion_sort(__first
, __last
, __comp
);
5196 * @brief Merges two sorted ranges.
5197 * @param first1 An iterator.
5198 * @param first2 Another iterator.
5199 * @param last1 Another iterator.
5200 * @param last2 Another iterator.
5201 * @param result An iterator pointing to the end of the merged range.
5202 * @return An iterator pointing to the first element "not less
5205 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5206 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5207 * must be sorted, and the output range must not overlap with either of
5208 * the input ranges. The sort is @e stable, that is, for equivalent
5209 * elements in the two ranges, elements from the first range will always
5210 * come before elements from the second.
5212 template<typename _InputIterator1
, typename _InputIterator2
,
5213 typename _OutputIterator
>
5215 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5216 _InputIterator2 __first2
, _InputIterator2 __last2
,
5217 _OutputIterator __result
)
5219 typedef typename iterator_traits
<_InputIterator1
>::value_type
5221 typedef typename iterator_traits
<_InputIterator2
>::value_type
5224 // concept requirements
5225 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5226 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5227 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5229 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5231 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5232 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5233 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5235 while (__first1
!= __last1
&& __first2
!= __last2
)
5237 if (*__first2
< *__first1
)
5239 *__result
= *__first2
;
5244 *__result
= *__first1
;
5249 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5254 * @brief Merges two sorted ranges.
5255 * @param first1 An iterator.
5256 * @param first2 Another iterator.
5257 * @param last1 Another iterator.
5258 * @param last2 Another iterator.
5259 * @param result An iterator pointing to the end of the merged range.
5260 * @param comp A functor to use for comparisons.
5261 * @return An iterator pointing to the first element "not less
5264 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5265 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5266 * must be sorted, and the output range must not overlap with either of
5267 * the input ranges. The sort is @e stable, that is, for equivalent
5268 * elements in the two ranges, elements from the first range will always
5269 * come before elements from the second.
5271 * The comparison function should have the same effects on ordering as
5272 * the function used for the initial sort.
5274 template<typename _InputIterator1
, typename _InputIterator2
,
5275 typename _OutputIterator
, typename _Compare
>
5277 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5278 _InputIterator2 __first2
, _InputIterator2 __last2
,
5279 _OutputIterator __result
, _Compare __comp
)
5281 typedef typename iterator_traits
<_InputIterator1
>::value_type
5283 typedef typename iterator_traits
<_InputIterator2
>::value_type
5286 // concept requirements
5287 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5288 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5289 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5291 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5293 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5294 _ValueType2
, _ValueType1
>)
5295 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5296 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5298 while (__first1
!= __last1
&& __first2
!= __last2
)
5300 if (__comp(*__first2
, *__first1
))
5302 *__result
= *__first2
;
5307 *__result
= *__first1
;
5312 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5318 * @brief Sort the elements of a sequence, preserving the relative order
5319 * of equivalent elements.
5320 * @param first An iterator.
5321 * @param last Another iterator.
5324 * Sorts the elements in the range @p [first,last) in ascending order,
5325 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5326 * @p [first,last-1).
5328 * The relative ordering of equivalent elements is preserved, so any two
5329 * elements @p x and @p y in the range @p [first,last) such that
5330 * @p x<y is false and @p y<x is false will have the same relative
5331 * ordering after calling @p stable_sort().
5333 template<typename _RandomAccessIterator
>
5335 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5337 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5339 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5342 // concept requirements
5343 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5344 _RandomAccessIterator
>)
5345 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5346 __glibcxx_requires_valid_range(__first
, __last
);
5348 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5350 if (__buf
.begin() == 0)
5351 std::__inplace_stable_sort(__first
, __last
);
5353 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5354 _DistanceType(__buf
.size()));
5358 * @brief Sort the elements of a sequence using a predicate for comparison,
5359 * preserving the relative order of equivalent elements.
5360 * @param first An iterator.
5361 * @param last Another iterator.
5362 * @param comp A comparison functor.
5365 * Sorts the elements in the range @p [first,last) in ascending order,
5366 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5367 * range @p [first,last-1).
5369 * The relative ordering of equivalent elements is preserved, so any two
5370 * elements @p x and @p y in the range @p [first,last) such that
5371 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5372 * relative ordering after calling @p stable_sort().
5374 template<typename _RandomAccessIterator
, typename _Compare
>
5376 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5379 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5381 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5384 // concept requirements
5385 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5386 _RandomAccessIterator
>)
5387 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5390 __glibcxx_requires_valid_range(__first
, __last
);
5392 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5394 if (__buf
.begin() == 0)
5395 std::__inplace_stable_sort(__first
, __last
, __comp
);
5397 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5398 _DistanceType(__buf
.size()), __comp
);
5403 * @brief Return the union of two sorted ranges.
5404 * @param first1 Start of first range.
5405 * @param last1 End of first range.
5406 * @param first2 Start of second range.
5407 * @param last2 End of second range.
5408 * @return End of the output range.
5409 * @ingroup setoperations
5411 * This operation iterates over both ranges, copying elements present in
5412 * each range in order to the output range. Iterators increment for each
5413 * range. When the current element of one range is less than the other,
5414 * that element is copied and the iterator advanced. If an element is
5415 * contained in both ranges, the element from the first range is copied and
5416 * both ranges advance. The output range may not overlap either input
5419 template<typename _InputIterator1
, typename _InputIterator2
,
5420 typename _OutputIterator
>
5422 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5423 _InputIterator2 __first2
, _InputIterator2 __last2
,
5424 _OutputIterator __result
)
5426 typedef typename iterator_traits
<_InputIterator1
>::value_type
5428 typedef typename iterator_traits
<_InputIterator2
>::value_type
5431 // concept requirements
5432 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5433 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5434 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5436 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5438 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5439 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5440 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5441 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5443 while (__first1
!= __last1
&& __first2
!= __last2
)
5445 if (*__first1
< *__first2
)
5447 *__result
= *__first1
;
5450 else if (*__first2
< *__first1
)
5452 *__result
= *__first2
;
5457 *__result
= *__first1
;
5463 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5468 * @brief Return the union of two sorted ranges using a comparison functor.
5469 * @param first1 Start of first range.
5470 * @param last1 End of first range.
5471 * @param first2 Start of second range.
5472 * @param last2 End of second range.
5473 * @param comp The comparison functor.
5474 * @return End of the output range.
5475 * @ingroup setoperations
5477 * This operation iterates over both ranges, copying elements present in
5478 * each range in order to the output range. Iterators increment for each
5479 * range. When the current element of one range is less than the other
5480 * according to @a comp, that element is copied and the iterator advanced.
5481 * If an equivalent element according to @a comp is contained in both
5482 * ranges, the element from the first range is copied and both ranges
5483 * advance. The output range may not overlap either input range.
5485 template<typename _InputIterator1
, typename _InputIterator2
,
5486 typename _OutputIterator
, typename _Compare
>
5488 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5489 _InputIterator2 __first2
, _InputIterator2 __last2
,
5490 _OutputIterator __result
, _Compare __comp
)
5492 typedef typename iterator_traits
<_InputIterator1
>::value_type
5494 typedef typename iterator_traits
<_InputIterator2
>::value_type
5497 // concept requirements
5498 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5499 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5500 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5502 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5504 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5505 _ValueType1
, _ValueType2
>)
5506 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5507 _ValueType2
, _ValueType1
>)
5508 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5509 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5511 while (__first1
!= __last1
&& __first2
!= __last2
)
5513 if (__comp(*__first1
, *__first2
))
5515 *__result
= *__first1
;
5518 else if (__comp(*__first2
, *__first1
))
5520 *__result
= *__first2
;
5525 *__result
= *__first1
;
5531 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5536 * @brief Return the intersection of two sorted ranges.
5537 * @param first1 Start of first range.
5538 * @param last1 End of first range.
5539 * @param first2 Start of second range.
5540 * @param last2 End of second range.
5541 * @return End of the output range.
5542 * @ingroup setoperations
5544 * This operation iterates over both ranges, copying elements present in
5545 * both ranges in order to the output range. Iterators increment for each
5546 * range. When the current element of one range is less than the other,
5547 * that iterator advances. If an element is contained in both ranges, the
5548 * element from the first range is copied and both ranges advance. The
5549 * output range may not overlap either input range.
5551 template<typename _InputIterator1
, typename _InputIterator2
,
5552 typename _OutputIterator
>
5554 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5555 _InputIterator2 __first2
, _InputIterator2 __last2
,
5556 _OutputIterator __result
)
5558 typedef typename iterator_traits
<_InputIterator1
>::value_type
5560 typedef typename iterator_traits
<_InputIterator2
>::value_type
5563 // concept requirements
5564 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5565 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5566 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5568 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5569 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5570 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5571 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5573 while (__first1
!= __last1
&& __first2
!= __last2
)
5574 if (*__first1
< *__first2
)
5576 else if (*__first2
< *__first1
)
5580 *__result
= *__first1
;
5589 * @brief Return the intersection of two sorted ranges using comparison
5591 * @param first1 Start of first range.
5592 * @param last1 End of first range.
5593 * @param first2 Start of second range.
5594 * @param last2 End of second range.
5595 * @param comp The comparison functor.
5596 * @return End of the output range.
5597 * @ingroup setoperations
5599 * This operation iterates over both ranges, copying elements present in
5600 * both ranges in order to the output range. Iterators increment for each
5601 * range. When the current element of one range is less than the other
5602 * according to @a comp, that iterator advances. If an element is
5603 * contained in both ranges according to @a comp, the element from the
5604 * first range is copied and both ranges advance. The output range may not
5605 * overlap either input range.
5607 template<typename _InputIterator1
, typename _InputIterator2
,
5608 typename _OutputIterator
, typename _Compare
>
5610 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5611 _InputIterator2 __first2
, _InputIterator2 __last2
,
5612 _OutputIterator __result
, _Compare __comp
)
5614 typedef typename iterator_traits
<_InputIterator1
>::value_type
5616 typedef typename iterator_traits
<_InputIterator2
>::value_type
5619 // concept requirements
5620 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5621 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5622 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5624 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5625 _ValueType1
, _ValueType2
>)
5626 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5627 _ValueType2
, _ValueType1
>)
5628 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5629 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5631 while (__first1
!= __last1
&& __first2
!= __last2
)
5632 if (__comp(*__first1
, *__first2
))
5634 else if (__comp(*__first2
, *__first1
))
5638 *__result
= *__first1
;
5647 * @brief Return the difference of two sorted ranges.
5648 * @param first1 Start of first range.
5649 * @param last1 End of first range.
5650 * @param first2 Start of second range.
5651 * @param last2 End of second range.
5652 * @return End of the output range.
5653 * @ingroup setoperations
5655 * This operation iterates over both ranges, copying elements present in
5656 * the first range but not the second in order to the output range.
5657 * Iterators increment for each range. When the current element of the
5658 * first range is less than the second, that element is copied and the
5659 * iterator advances. If the current element of the second range is less,
5660 * the iterator advances, but no element is copied. If an element is
5661 * contained in both ranges, no elements are copied and both ranges
5662 * advance. The output range may not overlap either input range.
5664 template<typename _InputIterator1
, typename _InputIterator2
,
5665 typename _OutputIterator
>
5667 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5668 _InputIterator2 __first2
, _InputIterator2 __last2
,
5669 _OutputIterator __result
)
5671 typedef typename iterator_traits
<_InputIterator1
>::value_type
5673 typedef typename iterator_traits
<_InputIterator2
>::value_type
5676 // concept requirements
5677 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5678 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5679 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5681 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5682 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5683 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5684 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5686 while (__first1
!= __last1
&& __first2
!= __last2
)
5687 if (*__first1
< *__first2
)
5689 *__result
= *__first1
;
5693 else if (*__first2
< *__first1
)
5700 return std::copy(__first1
, __last1
, __result
);
5704 * @brief Return the difference of two sorted ranges using comparison
5706 * @param first1 Start of first range.
5707 * @param last1 End of first range.
5708 * @param first2 Start of second range.
5709 * @param last2 End of second range.
5710 * @param comp The comparison functor.
5711 * @return End of the output range.
5712 * @ingroup setoperations
5714 * This operation iterates over both ranges, copying elements present in
5715 * the first range but not the second in order to the output range.
5716 * Iterators increment for each range. When the current element of the
5717 * first range is less than the second according to @a comp, that element
5718 * is copied and the iterator advances. If the current element of the
5719 * second range is less, no element is copied and the iterator advances.
5720 * If an element is contained in both ranges according to @a comp, no
5721 * elements are copied and both ranges advance. The output range may not
5722 * overlap either input range.
5724 template<typename _InputIterator1
, typename _InputIterator2
,
5725 typename _OutputIterator
, typename _Compare
>
5727 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5728 _InputIterator2 __first2
, _InputIterator2 __last2
,
5729 _OutputIterator __result
, _Compare __comp
)
5731 typedef typename iterator_traits
<_InputIterator1
>::value_type
5733 typedef typename iterator_traits
<_InputIterator2
>::value_type
5736 // concept requirements
5737 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5738 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5739 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5741 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5742 _ValueType1
, _ValueType2
>)
5743 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5744 _ValueType2
, _ValueType1
>)
5745 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5746 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5748 while (__first1
!= __last1
&& __first2
!= __last2
)
5749 if (__comp(*__first1
, *__first2
))
5751 *__result
= *__first1
;
5755 else if (__comp(*__first2
, *__first1
))
5762 return std::copy(__first1
, __last1
, __result
);
5766 * @brief Return the symmetric difference of two sorted ranges.
5767 * @param first1 Start of first range.
5768 * @param last1 End of first range.
5769 * @param first2 Start of second range.
5770 * @param last2 End of second range.
5771 * @return End of the output range.
5772 * @ingroup setoperations
5774 * This operation iterates over both ranges, copying elements present in
5775 * one range but not the other in order to the output range. Iterators
5776 * increment for each range. When the current element of one range is less
5777 * than the other, that element is copied and the iterator advances. If an
5778 * element is contained in both ranges, no elements are copied and both
5779 * ranges advance. The output range may not overlap either input range.
5781 template<typename _InputIterator1
, typename _InputIterator2
,
5782 typename _OutputIterator
>
5784 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5785 _InputIterator2 __first2
, _InputIterator2 __last2
,
5786 _OutputIterator __result
)
5788 typedef typename iterator_traits
<_InputIterator1
>::value_type
5790 typedef typename iterator_traits
<_InputIterator2
>::value_type
5793 // concept requirements
5794 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5795 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5796 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5798 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5800 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5801 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5802 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5803 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5805 while (__first1
!= __last1
&& __first2
!= __last2
)
5806 if (*__first1
< *__first2
)
5808 *__result
= *__first1
;
5812 else if (*__first2
< *__first1
)
5814 *__result
= *__first2
;
5823 return std::copy(__first2
, __last2
, std::copy(__first1
,
5824 __last1
, __result
));
5828 * @brief Return the symmetric difference of two sorted ranges using
5829 * comparison functor.
5830 * @param first1 Start of first range.
5831 * @param last1 End of first range.
5832 * @param first2 Start of second range.
5833 * @param last2 End of second range.
5834 * @param comp The comparison functor.
5835 * @return End of the output range.
5836 * @ingroup setoperations
5838 * This operation iterates over both ranges, copying elements present in
5839 * one range but not the other in order to the output range. Iterators
5840 * increment for each range. When the current element of one range is less
5841 * than the other according to @a comp, that element is copied and the
5842 * iterator advances. If an element is contained in both ranges according
5843 * to @a comp, no elements are copied and both ranges advance. The output
5844 * range may not overlap either input range.
5846 template<typename _InputIterator1
, typename _InputIterator2
,
5847 typename _OutputIterator
, typename _Compare
>
5849 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5850 _InputIterator2 __first2
, _InputIterator2 __last2
,
5851 _OutputIterator __result
,
5854 typedef typename iterator_traits
<_InputIterator1
>::value_type
5856 typedef typename iterator_traits
<_InputIterator2
>::value_type
5859 // concept requirements
5860 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5861 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5862 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5864 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5866 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5867 _ValueType1
, _ValueType2
>)
5868 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5869 _ValueType2
, _ValueType1
>)
5870 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5871 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5873 while (__first1
!= __last1
&& __first2
!= __last2
)
5874 if (__comp(*__first1
, *__first2
))
5876 *__result
= *__first1
;
5880 else if (__comp(*__first2
, *__first1
))
5882 *__result
= *__first2
;
5891 return std::copy(__first2
, __last2
,
5892 std::copy(__first1
, __last1
, __result
));
5897 * @brief Return the minimum element in a range.
5898 * @param first Start of range.
5899 * @param last End of range.
5900 * @return Iterator referencing the first instance of the smallest value.
5902 template<typename _ForwardIterator
>
5904 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5906 // concept requirements
5907 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5908 __glibcxx_function_requires(_LessThanComparableConcept
<
5909 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5910 __glibcxx_requires_valid_range(__first
, __last
);
5912 if (__first
== __last
)
5914 _ForwardIterator __result
= __first
;
5915 while (++__first
!= __last
)
5916 if (*__first
< *__result
)
5922 * @brief Return the minimum element in a range using comparison functor.
5923 * @param first Start of range.
5924 * @param last End of range.
5925 * @param comp Comparison functor.
5926 * @return Iterator referencing the first instance of the smallest value
5927 * according to comp.
5929 template<typename _ForwardIterator
, typename _Compare
>
5931 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5934 // concept requirements
5935 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5936 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5937 typename iterator_traits
<_ForwardIterator
>::value_type
,
5938 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5939 __glibcxx_requires_valid_range(__first
, __last
);
5941 if (__first
== __last
)
5943 _ForwardIterator __result
= __first
;
5944 while (++__first
!= __last
)
5945 if (__comp(*__first
, *__result
))
5951 * @brief Return the maximum element in a range.
5952 * @param first Start of range.
5953 * @param last End of range.
5954 * @return Iterator referencing the first instance of the largest value.
5956 template<typename _ForwardIterator
>
5958 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5960 // concept requirements
5961 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5962 __glibcxx_function_requires(_LessThanComparableConcept
<
5963 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5964 __glibcxx_requires_valid_range(__first
, __last
);
5966 if (__first
== __last
)
5968 _ForwardIterator __result
= __first
;
5969 while (++__first
!= __last
)
5970 if (*__result
< *__first
)
5976 * @brief Return the maximum element in a range using comparison functor.
5977 * @param first Start of range.
5978 * @param last End of range.
5979 * @param comp Comparison functor.
5980 * @return Iterator referencing the first instance of the largest value
5981 * according to comp.
5983 template<typename _ForwardIterator
, typename _Compare
>
5985 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5988 // concept requirements
5989 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5990 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5991 typename iterator_traits
<_ForwardIterator
>::value_type
,
5992 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5993 __glibcxx_requires_valid_range(__first
, __last
);
5995 if (__first
== __last
) return __first
;
5996 _ForwardIterator __result
= __first
;
5997 while (++__first
!= __last
)
5998 if (__comp(*__result
, *__first
))
6003 _GLIBCXX_END_NESTED_NAMESPACE
6005 #endif /* _STL_ALGO_H */