1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_algo.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{algorithm}
61 #include <cstdlib> // for rand
62 #include <bits/algorithmfwd.h>
63 #include <bits/stl_heap.h>
64 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
66 #ifdef __GXX_EXPERIMENTAL_CXX0X__
67 #include <random> // for std::uniform_int_distribution
68 #include <functional> // for std::bind
71 // See concept_check.h for the __glibcxx_*_requires macros.
73 namespace std
_GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 /// Swaps the median value of *__a, *__b and *__c to *__a
78 template<typename _Iterator
>
80 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
)
82 // concept requirements
83 __glibcxx_function_requires(_LessThanComparableConcept
<
84 typename iterator_traits
<_Iterator
>::value_type
>)
89 std::iter_swap(__a
, __b
);
91 std::iter_swap(__a
, __c
);
96 std::iter_swap(__a
, __c
);
98 std::iter_swap(__a
, __b
);
101 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
102 template<typename _Iterator
, typename _Compare
>
104 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
,
107 // concept requirements
108 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
109 typename iterator_traits
<_Iterator
>::value_type
,
110 typename iterator_traits
<_Iterator
>::value_type
>)
112 if (__comp(*__a
, *__b
))
114 if (__comp(*__b
, *__c
))
115 std::iter_swap(__a
, __b
);
116 else if (__comp(*__a
, *__c
))
117 std::iter_swap(__a
, __c
);
119 else if (__comp(*__a
, *__c
))
121 else if (__comp(*__b
, *__c
))
122 std::iter_swap(__a
, __c
);
124 std::iter_swap(__a
, __b
);
129 /// This is an overload used by find() for the Input Iterator case.
130 template<typename _InputIterator
, typename _Tp
>
131 inline _InputIterator
132 __find(_InputIterator __first
, _InputIterator __last
,
133 const _Tp
& __val
, input_iterator_tag
)
135 while (__first
!= __last
&& !(*__first
== __val
))
140 /// This is an overload used by find_if() for the Input Iterator case.
141 template<typename _InputIterator
, typename _Predicate
>
142 inline _InputIterator
143 __find_if(_InputIterator __first
, _InputIterator __last
,
144 _Predicate __pred
, input_iterator_tag
)
146 while (__first
!= __last
&& !bool(__pred(*__first
)))
151 /// This is an overload used by find() for the RAI case.
152 template<typename _RandomAccessIterator
, typename _Tp
>
153 _RandomAccessIterator
154 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
155 const _Tp
& __val
, random_access_iterator_tag
)
157 typename iterator_traits
<_RandomAccessIterator
>::difference_type
158 __trip_count
= (__last
- __first
) >> 2;
160 for (; __trip_count
> 0; --__trip_count
)
162 if (*__first
== __val
)
166 if (*__first
== __val
)
170 if (*__first
== __val
)
174 if (*__first
== __val
)
179 switch (__last
- __first
)
182 if (*__first
== __val
)
186 if (*__first
== __val
)
190 if (*__first
== __val
)
199 /// This is an overload used by find_if() for the RAI case.
200 template<typename _RandomAccessIterator
, typename _Predicate
>
201 _RandomAccessIterator
202 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
203 _Predicate __pred
, random_access_iterator_tag
)
205 typename iterator_traits
<_RandomAccessIterator
>::difference_type
206 __trip_count
= (__last
- __first
) >> 2;
208 for (; __trip_count
> 0; --__trip_count
)
210 if (__pred(*__first
))
214 if (__pred(*__first
))
218 if (__pred(*__first
))
222 if (__pred(*__first
))
227 switch (__last
- __first
)
230 if (__pred(*__first
))
234 if (__pred(*__first
))
238 if (__pred(*__first
))
247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
248 /// This is an overload used by find_if_not() for the Input Iterator case.
249 template<typename _InputIterator
, typename _Predicate
>
250 inline _InputIterator
251 __find_if_not(_InputIterator __first
, _InputIterator __last
,
252 _Predicate __pred
, input_iterator_tag
)
254 while (__first
!= __last
&& bool(__pred(*__first
)))
259 /// This is an overload used by find_if_not() for the RAI case.
260 template<typename _RandomAccessIterator
, typename _Predicate
>
261 _RandomAccessIterator
262 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
263 _Predicate __pred
, random_access_iterator_tag
)
265 typename iterator_traits
<_RandomAccessIterator
>::difference_type
266 __trip_count
= (__last
- __first
) >> 2;
268 for (; __trip_count
> 0; --__trip_count
)
270 if (!bool(__pred(*__first
)))
274 if (!bool(__pred(*__first
)))
278 if (!bool(__pred(*__first
)))
282 if (!bool(__pred(*__first
)))
287 switch (__last
- __first
)
290 if (!bool(__pred(*__first
)))
294 if (!bool(__pred(*__first
)))
298 if (!bool(__pred(*__first
)))
310 // set_symmetric_difference
322 * This is an uglified
323 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
324 * overloaded for forward iterators.
326 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
328 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
329 _Integer __count
, const _Tp
& __val
,
330 std::forward_iterator_tag
)
332 __first
= _GLIBCXX_STD_A::find(__first
, __last
, __val
);
333 while (__first
!= __last
)
335 typename iterator_traits
<_ForwardIterator
>::difference_type
337 _ForwardIterator __i
= __first
;
339 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
348 __first
= _GLIBCXX_STD_A::find(++__i
, __last
, __val
);
354 * This is an uglified
355 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
356 * overloaded for random access iterators.
358 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
360 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
361 _Integer __count
, const _Tp
& __val
,
362 std::random_access_iterator_tag
)
365 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
368 _DistanceType __tailSize
= __last
- __first
;
369 const _DistanceType __pattSize
= __count
;
371 if (__tailSize
< __pattSize
)
374 const _DistanceType __skipOffset
= __pattSize
- 1;
375 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
376 __tailSize
-= __pattSize
;
378 while (1) // the main loop...
380 // __lookAhead here is always pointing to the last element of next
382 while (!(*__lookAhead
== __val
)) // the skip loop...
384 if (__tailSize
< __pattSize
)
385 return __last
; // Failure
386 __lookAhead
+= __pattSize
;
387 __tailSize
-= __pattSize
;
389 _DistanceType __remainder
= __skipOffset
;
390 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
391 *__backTrack
== __val
; --__backTrack
)
393 if (--__remainder
== 0)
394 return (__lookAhead
- __skipOffset
); // Success
396 if (__remainder
> __tailSize
)
397 return __last
; // Failure
398 __lookAhead
+= __remainder
;
399 __tailSize
-= __remainder
;
406 * This is an uglified
407 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
409 * overloaded for forward iterators.
411 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
412 typename _BinaryPredicate
>
414 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
415 _Integer __count
, const _Tp
& __val
,
416 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
418 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
421 while (__first
!= __last
)
423 typename iterator_traits
<_ForwardIterator
>::difference_type
425 _ForwardIterator __i
= __first
;
427 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
437 while (__first
!= __last
438 && !bool(__binary_pred(*__first
, __val
)))
445 * This is an uglified
446 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
448 * overloaded for random access iterators.
450 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
451 typename _BinaryPredicate
>
453 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
454 _Integer __count
, const _Tp
& __val
,
455 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
458 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
461 _DistanceType __tailSize
= __last
- __first
;
462 const _DistanceType __pattSize
= __count
;
464 if (__tailSize
< __pattSize
)
467 const _DistanceType __skipOffset
= __pattSize
- 1;
468 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
469 __tailSize
-= __pattSize
;
471 while (1) // the main loop...
473 // __lookAhead here is always pointing to the last element of next
475 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
477 if (__tailSize
< __pattSize
)
478 return __last
; // Failure
479 __lookAhead
+= __pattSize
;
480 __tailSize
-= __pattSize
;
482 _DistanceType __remainder
= __skipOffset
;
483 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
484 __binary_pred(*__backTrack
, __val
); --__backTrack
)
486 if (--__remainder
== 0)
487 return (__lookAhead
- __skipOffset
); // Success
489 if (__remainder
> __tailSize
)
490 return __last
; // Failure
491 __lookAhead
+= __remainder
;
492 __tailSize
-= __remainder
;
496 // find_end for forward iterators.
497 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
499 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
500 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
501 forward_iterator_tag
, forward_iterator_tag
)
503 if (__first2
== __last2
)
507 _ForwardIterator1 __result
= __last1
;
510 _ForwardIterator1 __new_result
511 = _GLIBCXX_STD_A::search(__first1
, __last1
, __first2
, __last2
);
512 if (__new_result
== __last1
)
516 __result
= __new_result
;
517 __first1
= __new_result
;
524 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
525 typename _BinaryPredicate
>
527 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
528 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
529 forward_iterator_tag
, forward_iterator_tag
,
530 _BinaryPredicate __comp
)
532 if (__first2
== __last2
)
536 _ForwardIterator1 __result
= __last1
;
539 _ForwardIterator1 __new_result
540 = _GLIBCXX_STD_A::search(__first1
, __last1
, __first2
,
542 if (__new_result
== __last1
)
546 __result
= __new_result
;
547 __first1
= __new_result
;
554 // find_end for bidirectional iterators (much faster).
555 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
556 _BidirectionalIterator1
557 __find_end(_BidirectionalIterator1 __first1
,
558 _BidirectionalIterator1 __last1
,
559 _BidirectionalIterator2 __first2
,
560 _BidirectionalIterator2 __last2
,
561 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
563 // concept requirements
564 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
565 _BidirectionalIterator1
>)
566 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
567 _BidirectionalIterator2
>)
569 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
570 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
572 _RevIterator1
__rlast1(__first1
);
573 _RevIterator2
__rlast2(__first2
);
574 _RevIterator1 __rresult
= _GLIBCXX_STD_A::search(_RevIterator1(__last1
),
576 _RevIterator2(__last2
),
579 if (__rresult
== __rlast1
)
583 _BidirectionalIterator1 __result
= __rresult
.base();
584 std::advance(__result
, -std::distance(__first2
, __last2
));
589 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
590 typename _BinaryPredicate
>
591 _BidirectionalIterator1
592 __find_end(_BidirectionalIterator1 __first1
,
593 _BidirectionalIterator1 __last1
,
594 _BidirectionalIterator2 __first2
,
595 _BidirectionalIterator2 __last2
,
596 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
597 _BinaryPredicate __comp
)
599 // concept requirements
600 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
601 _BidirectionalIterator1
>)
602 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
603 _BidirectionalIterator2
>)
605 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
606 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
608 _RevIterator1
__rlast1(__first1
);
609 _RevIterator2
__rlast2(__first2
);
610 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
611 _RevIterator2(__last2
), __rlast2
,
614 if (__rresult
== __rlast1
)
618 _BidirectionalIterator1 __result
= __rresult
.base();
619 std::advance(__result
, -std::distance(__first2
, __last2
));
625 * @brief Find last matching subsequence in a sequence.
626 * @ingroup non_mutating_algorithms
627 * @param __first1 Start of range to search.
628 * @param __last1 End of range to search.
629 * @param __first2 Start of sequence to match.
630 * @param __last2 End of sequence to match.
631 * @return The last iterator @c i in the range
632 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
633 * @p *(__first2+N) for each @c N in the range @p
634 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
636 * Searches the range @p [__first1,__last1) for a sub-sequence that
637 * compares equal value-by-value with the sequence given by @p
638 * [__first2,__last2) and returns an iterator to the __first
639 * element of the sub-sequence, or @p __last1 if the sub-sequence
640 * is not found. The sub-sequence will be the last such
641 * subsequence contained in [__first,__last1).
643 * Because the sub-sequence must lie completely within the range @p
644 * [__first1,__last1) it must start at a position less than @p
645 * __last1-(__last2-__first2) where @p __last2-__first2 is the
646 * length of the sub-sequence. This means that the returned
647 * iterator @c i will be in the range @p
648 * [__first1,__last1-(__last2-__first2))
650 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
651 inline _ForwardIterator1
652 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
653 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
655 // concept requirements
656 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
657 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
658 __glibcxx_function_requires(_EqualOpConcept
<
659 typename iterator_traits
<_ForwardIterator1
>::value_type
,
660 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
661 __glibcxx_requires_valid_range(__first1
, __last1
);
662 __glibcxx_requires_valid_range(__first2
, __last2
);
664 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
665 std::__iterator_category(__first1
),
666 std::__iterator_category(__first2
));
670 * @brief Find last matching subsequence in a sequence using a predicate.
671 * @ingroup non_mutating_algorithms
672 * @param __first1 Start of range to search.
673 * @param __last1 End of range to search.
674 * @param __first2 Start of sequence to match.
675 * @param __last2 End of sequence to match.
676 * @param __comp The predicate to use.
677 * @return The last iterator @c i in the range @p
678 * [__first1,__last1-(__last2-__first2)) such that @c
679 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
680 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
683 * Searches the range @p [__first1,__last1) for a sub-sequence that
684 * compares equal value-by-value with the sequence given by @p
685 * [__first2,__last2) using comp as a predicate and returns an
686 * iterator to the first element of the sub-sequence, or @p __last1
687 * if the sub-sequence is not found. The sub-sequence will be the
688 * last such subsequence contained in [__first,__last1).
690 * Because the sub-sequence must lie completely within the range @p
691 * [__first1,__last1) it must start at a position less than @p
692 * __last1-(__last2-__first2) where @p __last2-__first2 is the
693 * length of the sub-sequence. This means that the returned
694 * iterator @c i will be in the range @p
695 * [__first1,__last1-(__last2-__first2))
697 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
698 typename _BinaryPredicate
>
699 inline _ForwardIterator1
700 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
701 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
702 _BinaryPredicate __comp
)
704 // concept requirements
705 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
706 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
707 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
708 typename iterator_traits
<_ForwardIterator1
>::value_type
,
709 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
710 __glibcxx_requires_valid_range(__first1
, __last1
);
711 __glibcxx_requires_valid_range(__first2
, __last2
);
713 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
714 std::__iterator_category(__first1
),
715 std::__iterator_category(__first2
),
719 #ifdef __GXX_EXPERIMENTAL_CXX0X__
721 * @brief Checks that a predicate is true for all the elements
723 * @ingroup non_mutating_algorithms
724 * @param __first An input iterator.
725 * @param __last An input iterator.
726 * @param __pred A predicate.
727 * @return True if the check is true, false otherwise.
729 * Returns true if @p __pred is true for each element in the range
730 * @p [__first,__last), and false otherwise.
732 template<typename _InputIterator
, typename _Predicate
>
734 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
735 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
738 * @brief Checks that a predicate is false for all the elements
740 * @ingroup non_mutating_algorithms
741 * @param __first An input iterator.
742 * @param __last An input iterator.
743 * @param __pred A predicate.
744 * @return True if the check is true, false otherwise.
746 * Returns true if @p __pred is false for each element in the range
747 * @p [__first,__last), and false otherwise.
749 template<typename _InputIterator
, typename _Predicate
>
751 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
752 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
755 * @brief Checks that a predicate is false for at least an element
757 * @ingroup non_mutating_algorithms
758 * @param __first An input iterator.
759 * @param __last An input iterator.
760 * @param __pred A predicate.
761 * @return True if the check is true, false otherwise.
763 * Returns true if an element exists in the range @p
764 * [__first,__last) such that @p __pred is true, and false
767 template<typename _InputIterator
, typename _Predicate
>
769 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
770 { return !std::none_of(__first
, __last
, __pred
); }
773 * @brief Find the first element in a sequence for which a
774 * predicate is false.
775 * @ingroup non_mutating_algorithms
776 * @param __first An input iterator.
777 * @param __last An input iterator.
778 * @param __pred A predicate.
779 * @return The first iterator @c i in the range @p [__first,__last)
780 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
782 template<typename _InputIterator
, typename _Predicate
>
783 inline _InputIterator
784 find_if_not(_InputIterator __first
, _InputIterator __last
,
787 // concept requirements
788 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
789 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
790 typename iterator_traits
<_InputIterator
>::value_type
>)
791 __glibcxx_requires_valid_range(__first
, __last
);
792 return std::__find_if_not(__first
, __last
, __pred
,
793 std::__iterator_category(__first
));
797 * @brief Checks whether the sequence is partitioned.
798 * @ingroup mutating_algorithms
799 * @param __first An input iterator.
800 * @param __last An input iterator.
801 * @param __pred A predicate.
802 * @return True if the range @p [__first,__last) is partioned by @p __pred,
803 * i.e. if all elements that satisfy @p __pred appear before those that
806 template<typename _InputIterator
, typename _Predicate
>
808 is_partitioned(_InputIterator __first
, _InputIterator __last
,
811 __first
= std::find_if_not(__first
, __last
, __pred
);
812 return std::none_of(__first
, __last
, __pred
);
816 * @brief Find the partition point of a partitioned range.
817 * @ingroup mutating_algorithms
818 * @param __first An iterator.
819 * @param __last Another iterator.
820 * @param __pred A predicate.
821 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
822 * and @p none_of(mid, __last, __pred) are both true.
824 template<typename _ForwardIterator
, typename _Predicate
>
826 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
829 // concept requirements
830 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
831 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
832 typename iterator_traits
<_ForwardIterator
>::value_type
>)
834 // A specific debug-mode test will be necessary...
835 __glibcxx_requires_valid_range(__first
, __last
);
837 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
840 _DistanceType __len
= std::distance(__first
, __last
);
841 _DistanceType __half
;
842 _ForwardIterator __middle
;
848 std::advance(__middle
, __half
);
849 if (__pred(*__middle
))
853 __len
= __len
- __half
- 1;
864 * @brief Copy a sequence, removing elements of a given value.
865 * @ingroup mutating_algorithms
866 * @param __first An input iterator.
867 * @param __last An input iterator.
868 * @param __result An output iterator.
869 * @param __value The value to be removed.
870 * @return An iterator designating the end of the resulting sequence.
872 * Copies each element in the range @p [__first,__last) not equal
873 * to @p __value to the range beginning at @p __result.
874 * remove_copy() is stable, so the relative order of elements that
875 * are copied is unchanged.
877 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
879 remove_copy(_InputIterator __first
, _InputIterator __last
,
880 _OutputIterator __result
, const _Tp
& __value
)
882 // concept requirements
883 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
884 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
885 typename iterator_traits
<_InputIterator
>::value_type
>)
886 __glibcxx_function_requires(_EqualOpConcept
<
887 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
888 __glibcxx_requires_valid_range(__first
, __last
);
890 for (; __first
!= __last
; ++__first
)
891 if (!(*__first
== __value
))
893 *__result
= *__first
;
900 * @brief Copy a sequence, removing elements for which a predicate is true.
901 * @ingroup mutating_algorithms
902 * @param __first An input iterator.
903 * @param __last An input iterator.
904 * @param __result An output iterator.
905 * @param __pred A predicate.
906 * @return An iterator designating the end of the resulting sequence.
908 * Copies each element in the range @p [__first,__last) for which
909 * @p __pred returns false to the range beginning at @p __result.
911 * remove_copy_if() is stable, so the relative order of elements that are
912 * copied is unchanged.
914 template<typename _InputIterator
, typename _OutputIterator
,
917 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
918 _OutputIterator __result
, _Predicate __pred
)
920 // concept requirements
921 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
922 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
923 typename iterator_traits
<_InputIterator
>::value_type
>)
924 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
925 typename iterator_traits
<_InputIterator
>::value_type
>)
926 __glibcxx_requires_valid_range(__first
, __last
);
928 for (; __first
!= __last
; ++__first
)
929 if (!bool(__pred(*__first
)))
931 *__result
= *__first
;
937 #ifdef __GXX_EXPERIMENTAL_CXX0X__
939 * @brief Copy the elements of a sequence for which a predicate is true.
940 * @ingroup mutating_algorithms
941 * @param __first An input iterator.
942 * @param __last An input iterator.
943 * @param __result An output iterator.
944 * @param __pred A predicate.
945 * @return An iterator designating the end of the resulting sequence.
947 * Copies each element in the range @p [__first,__last) for which
948 * @p __pred returns true to the range beginning at @p __result.
950 * copy_if() is stable, so the relative order of elements that are
951 * copied is unchanged.
953 template<typename _InputIterator
, typename _OutputIterator
,
956 copy_if(_InputIterator __first
, _InputIterator __last
,
957 _OutputIterator __result
, _Predicate __pred
)
959 // concept requirements
960 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
961 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
962 typename iterator_traits
<_InputIterator
>::value_type
>)
963 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
964 typename iterator_traits
<_InputIterator
>::value_type
>)
965 __glibcxx_requires_valid_range(__first
, __last
);
967 for (; __first
!= __last
; ++__first
)
968 if (__pred(*__first
))
970 *__result
= *__first
;
977 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
979 __copy_n(_InputIterator __first
, _Size __n
,
980 _OutputIterator __result
, input_iterator_tag
)
982 for (; __n
> 0; --__n
)
984 *__result
= *__first
;
991 template<typename _RandomAccessIterator
, typename _Size
,
992 typename _OutputIterator
>
993 inline _OutputIterator
994 __copy_n(_RandomAccessIterator __first
, _Size __n
,
995 _OutputIterator __result
, random_access_iterator_tag
)
996 { return std::copy(__first
, __first
+ __n
, __result
); }
999 * @brief Copies the range [first,first+n) into [result,result+n).
1000 * @ingroup mutating_algorithms
1001 * @param __first An input iterator.
1002 * @param __n The number of elements to copy.
1003 * @param __result An output iterator.
1006 * This inline function will boil down to a call to @c memmove whenever
1007 * possible. Failing that, if random access iterators are passed, then the
1008 * loop count will be known (and therefore a candidate for compiler
1009 * optimizations such as unrolling).
1011 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1012 inline _OutputIterator
1013 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1015 // concept requirements
1016 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1017 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1018 typename iterator_traits
<_InputIterator
>::value_type
>)
1020 return std::__copy_n(__first
, __n
, __result
,
1021 std::__iterator_category(__first
));
1025 * @brief Copy the elements of a sequence to separate output sequences
1026 * depending on the truth value of a predicate.
1027 * @ingroup mutating_algorithms
1028 * @param __first An input iterator.
1029 * @param __last An input iterator.
1030 * @param __out_true An output iterator.
1031 * @param __out_false An output iterator.
1032 * @param __pred A predicate.
1033 * @return A pair designating the ends of the resulting sequences.
1035 * Copies each element in the range @p [__first,__last) for which
1036 * @p __pred returns true to the range beginning at @p out_true
1037 * and each element for which @p __pred returns false to @p __out_false.
1039 template<typename _InputIterator
, typename _OutputIterator1
,
1040 typename _OutputIterator2
, typename _Predicate
>
1041 pair
<_OutputIterator1
, _OutputIterator2
>
1042 partition_copy(_InputIterator __first
, _InputIterator __last
,
1043 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1046 // concept requirements
1047 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1048 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1049 typename iterator_traits
<_InputIterator
>::value_type
>)
1050 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1051 typename iterator_traits
<_InputIterator
>::value_type
>)
1052 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1053 typename iterator_traits
<_InputIterator
>::value_type
>)
1054 __glibcxx_requires_valid_range(__first
, __last
);
1056 for (; __first
!= __last
; ++__first
)
1057 if (__pred(*__first
))
1059 *__out_true
= *__first
;
1064 *__out_false
= *__first
;
1068 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1073 * @brief Remove elements from a sequence.
1074 * @ingroup mutating_algorithms
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
1081 * @p [__first,__last).
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_A::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 * @ingroup mutating_algorithms
1118 * @param __first A forward iterator.
1119 * @param __last A forward iterator.
1120 * @param __pred A predicate.
1121 * @return An iterator designating the end of the resulting sequence.
1123 * All elements for which @p __pred returns true are removed from the range
1124 * @p [__first,__last).
1126 * remove_if() is stable, so the relative order of elements that are
1127 * not removed is unchanged.
1129 * Elements between the end of the resulting sequence and @p __last
1130 * are still present, but their value is unspecified.
1132 template<typename _ForwardIterator
, typename _Predicate
>
1134 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1137 // concept requirements
1138 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1140 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1141 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1142 __glibcxx_requires_valid_range(__first
, __last
);
1144 __first
= _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
);
1145 if(__first
== __last
)
1147 _ForwardIterator __result
= __first
;
1149 for(; __first
!= __last
; ++__first
)
1150 if(!bool(__pred(*__first
)))
1152 *__result
= _GLIBCXX_MOVE(*__first
);
1159 * @brief Remove consecutive duplicate values from a sequence.
1160 * @ingroup mutating_algorithms
1161 * @param __first A forward iterator.
1162 * @param __last A forward iterator.
1163 * @return An iterator designating the end of the resulting sequence.
1165 * Removes all but the first element from each group of consecutive
1166 * values that compare equal.
1167 * unique() is stable, so the relative order of elements that are
1168 * not removed is unchanged.
1169 * Elements between the end of the resulting sequence and @p __last
1170 * are still present, but their value is unspecified.
1172 template<typename _ForwardIterator
>
1174 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1176 // concept requirements
1177 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1179 __glibcxx_function_requires(_EqualityComparableConcept
<
1180 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1181 __glibcxx_requires_valid_range(__first
, __last
);
1183 // Skip the beginning, if already unique.
1184 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
);
1185 if (__first
== __last
)
1188 // Do the real copy work.
1189 _ForwardIterator __dest
= __first
;
1191 while (++__first
!= __last
)
1192 if (!(*__dest
== *__first
))
1193 *++__dest
= _GLIBCXX_MOVE(*__first
);
1198 * @brief Remove consecutive values from a sequence using a predicate.
1199 * @ingroup mutating_algorithms
1200 * @param __first A forward iterator.
1201 * @param __last A forward iterator.
1202 * @param __binary_pred A binary predicate.
1203 * @return An iterator designating the end of the resulting sequence.
1205 * Removes all but the first element from each group of consecutive
1206 * values for which @p __binary_pred returns true.
1207 * unique() is stable, so the relative order of elements that are
1208 * not removed is unchanged.
1209 * Elements between the end of the resulting sequence and @p __last
1210 * are still present, but their value is unspecified.
1212 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1214 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1215 _BinaryPredicate __binary_pred
)
1217 // concept requirements
1218 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1220 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1221 typename iterator_traits
<_ForwardIterator
>::value_type
,
1222 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1223 __glibcxx_requires_valid_range(__first
, __last
);
1225 // Skip the beginning, if already unique.
1226 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
, __binary_pred
);
1227 if (__first
== __last
)
1230 // Do the real copy work.
1231 _ForwardIterator __dest
= __first
;
1233 while (++__first
!= __last
)
1234 if (!bool(__binary_pred(*__dest
, *__first
)))
1235 *++__dest
= _GLIBCXX_MOVE(*__first
);
1240 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1242 * overloaded for forward iterators and output iterator as result.
1244 template<typename _ForwardIterator
, typename _OutputIterator
>
1246 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1247 _OutputIterator __result
,
1248 forward_iterator_tag
, output_iterator_tag
)
1250 // concept requirements -- taken care of in dispatching function
1251 _ForwardIterator __next
= __first
;
1252 *__result
= *__first
;
1253 while (++__next
!= __last
)
1254 if (!(*__first
== *__next
))
1257 *++__result
= *__first
;
1263 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1265 * overloaded for input iterators and output iterator as result.
1267 template<typename _InputIterator
, typename _OutputIterator
>
1269 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1270 _OutputIterator __result
,
1271 input_iterator_tag
, output_iterator_tag
)
1273 // concept requirements -- taken care of in dispatching function
1274 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1275 *__result
= __value
;
1276 while (++__first
!= __last
)
1277 if (!(__value
== *__first
))
1280 *++__result
= __value
;
1286 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1288 * overloaded for input iterators and forward iterator as result.
1290 template<typename _InputIterator
, typename _ForwardIterator
>
1292 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1293 _ForwardIterator __result
,
1294 input_iterator_tag
, forward_iterator_tag
)
1296 // concept requirements -- taken care of in dispatching function
1297 *__result
= *__first
;
1298 while (++__first
!= __last
)
1299 if (!(*__result
== *__first
))
1300 *++__result
= *__first
;
1305 * This is an uglified
1306 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1308 * overloaded for forward iterators and output iterator as result.
1310 template<typename _ForwardIterator
, typename _OutputIterator
,
1311 typename _BinaryPredicate
>
1313 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1314 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1315 forward_iterator_tag
, output_iterator_tag
)
1317 // concept requirements -- iterators already checked
1318 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1319 typename iterator_traits
<_ForwardIterator
>::value_type
,
1320 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1322 _ForwardIterator __next
= __first
;
1323 *__result
= *__first
;
1324 while (++__next
!= __last
)
1325 if (!bool(__binary_pred(*__first
, *__next
)))
1328 *++__result
= *__first
;
1334 * This is an uglified
1335 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1337 * overloaded for input iterators and output iterator as result.
1339 template<typename _InputIterator
, typename _OutputIterator
,
1340 typename _BinaryPredicate
>
1342 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1343 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1344 input_iterator_tag
, output_iterator_tag
)
1346 // concept requirements -- iterators already checked
1347 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1348 typename iterator_traits
<_InputIterator
>::value_type
,
1349 typename iterator_traits
<_InputIterator
>::value_type
>)
1351 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1352 *__result
= __value
;
1353 while (++__first
!= __last
)
1354 if (!bool(__binary_pred(__value
, *__first
)))
1357 *++__result
= __value
;
1363 * This is an uglified
1364 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1366 * overloaded for input iterators and forward iterator as result.
1368 template<typename _InputIterator
, typename _ForwardIterator
,
1369 typename _BinaryPredicate
>
1371 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1372 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1373 input_iterator_tag
, forward_iterator_tag
)
1375 // concept requirements -- iterators already checked
1376 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1377 typename iterator_traits
<_ForwardIterator
>::value_type
,
1378 typename iterator_traits
<_InputIterator
>::value_type
>)
1380 *__result
= *__first
;
1381 while (++__first
!= __last
)
1382 if (!bool(__binary_pred(*__result
, *__first
)))
1383 *++__result
= *__first
;
1388 * This is an uglified reverse(_BidirectionalIterator,
1389 * _BidirectionalIterator)
1390 * overloaded for bidirectional iterators.
1392 template<typename _BidirectionalIterator
>
1394 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1395 bidirectional_iterator_tag
)
1398 if (__first
== __last
|| __first
== --__last
)
1402 std::iter_swap(__first
, __last
);
1408 * This is an uglified reverse(_BidirectionalIterator,
1409 * _BidirectionalIterator)
1410 * overloaded for random access iterators.
1412 template<typename _RandomAccessIterator
>
1414 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1415 random_access_iterator_tag
)
1417 if (__first
== __last
)
1420 while (__first
< __last
)
1422 std::iter_swap(__first
, __last
);
1429 * @brief Reverse a sequence.
1430 * @ingroup mutating_algorithms
1431 * @param __first A bidirectional iterator.
1432 * @param __last A bidirectional iterator.
1433 * @return reverse() returns no value.
1435 * Reverses the order of the elements in the range @p [__first,__last),
1436 * so that the first element becomes the last etc.
1437 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1438 * swaps @p *(__first+i) and @p *(__last-(i+1))
1440 template<typename _BidirectionalIterator
>
1442 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1444 // concept requirements
1445 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1446 _BidirectionalIterator
>)
1447 __glibcxx_requires_valid_range(__first
, __last
);
1448 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1452 * @brief Copy a sequence, reversing its elements.
1453 * @ingroup mutating_algorithms
1454 * @param __first A bidirectional iterator.
1455 * @param __last A bidirectional iterator.
1456 * @param __result An output iterator.
1457 * @return An iterator designating the end of the resulting sequence.
1459 * Copies the elements in the range @p [__first,__last) to the
1460 * range @p [__result,__result+(__last-__first)) such that the
1461 * order of the elements is reversed. For every @c i such that @p
1462 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1463 * assignment @p *(__result+(__last-__first)-i) = *(__first+i).
1464 * The ranges @p [__first,__last) and @p
1465 * [__result,__result+(__last-__first)) must not overlap.
1467 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1469 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1470 _OutputIterator __result
)
1472 // concept requirements
1473 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1474 _BidirectionalIterator
>)
1475 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1476 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1477 __glibcxx_requires_valid_range(__first
, __last
);
1479 while (__first
!= __last
)
1482 *__result
= *__last
;
1489 * This is a helper function for the rotate algorithm specialized on RAIs.
1490 * It returns the greatest common divisor of two integer values.
1492 template<typename _EuclideanRingElement
>
1493 _EuclideanRingElement
1494 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1498 _EuclideanRingElement __t
= __m
% __n
;
1505 /// This is a helper function for the rotate algorithm.
1506 template<typename _ForwardIterator
>
1508 __rotate(_ForwardIterator __first
,
1509 _ForwardIterator __middle
,
1510 _ForwardIterator __last
,
1511 forward_iterator_tag
)
1513 if (__first
== __middle
|| __last
== __middle
)
1516 _ForwardIterator __first2
= __middle
;
1519 std::iter_swap(__first
, __first2
);
1522 if (__first
== __middle
)
1523 __middle
= __first2
;
1525 while (__first2
!= __last
);
1527 __first2
= __middle
;
1529 while (__first2
!= __last
)
1531 std::iter_swap(__first
, __first2
);
1534 if (__first
== __middle
)
1535 __middle
= __first2
;
1536 else if (__first2
== __last
)
1537 __first2
= __middle
;
1541 /// This is a helper function for the rotate algorithm.
1542 template<typename _BidirectionalIterator
>
1544 __rotate(_BidirectionalIterator __first
,
1545 _BidirectionalIterator __middle
,
1546 _BidirectionalIterator __last
,
1547 bidirectional_iterator_tag
)
1549 // concept requirements
1550 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1551 _BidirectionalIterator
>)
1553 if (__first
== __middle
|| __last
== __middle
)
1556 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1557 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1559 while (__first
!= __middle
&& __middle
!= __last
)
1561 std::iter_swap(__first
, --__last
);
1565 if (__first
== __middle
)
1566 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1568 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1571 /// This is a helper function for the rotate algorithm.
1572 template<typename _RandomAccessIterator
>
1574 __rotate(_RandomAccessIterator __first
,
1575 _RandomAccessIterator __middle
,
1576 _RandomAccessIterator __last
,
1577 random_access_iterator_tag
)
1579 // concept requirements
1580 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1581 _RandomAccessIterator
>)
1583 if (__first
== __middle
|| __last
== __middle
)
1586 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1588 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1591 _Distance __n
= __last
- __first
;
1592 _Distance __k
= __middle
- __first
;
1594 if (__k
== __n
- __k
)
1596 std::swap_ranges(__first
, __middle
, __middle
);
1600 _RandomAccessIterator __p
= __first
;
1604 if (__k
< __n
- __k
)
1606 if (__is_pod(_ValueType
) && __k
== 1)
1608 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1609 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1610 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1613 _RandomAccessIterator __q
= __p
+ __k
;
1614 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1616 std::iter_swap(__p
, __q
);
1623 std::swap(__n
, __k
);
1629 if (__is_pod(_ValueType
) && __k
== 1)
1631 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1632 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1633 *__p
= _GLIBCXX_MOVE(__t
);
1636 _RandomAccessIterator __q
= __p
+ __n
;
1638 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1642 std::iter_swap(__p
, __q
);
1647 std::swap(__n
, __k
);
1653 * @brief Rotate the elements of a sequence.
1654 * @ingroup mutating_algorithms
1655 * @param __first A forward iterator.
1656 * @param __middle A forward iterator.
1657 * @param __last A forward iterator.
1660 * Rotates the elements of the range @p [__first,__last) by
1661 * @p(__middle - __first) positions so that the element at @p __middle
1662 * is moved to @p __first, the element at @p __middle+1 is moved to
1663 * @ __first+1 and so on for each element in the range
1664 * @p [__first,__last).
1666 * This effectively swaps the ranges @p [__first,__middle) and
1667 * @p [__middle,__last).
1670 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1671 * for each @p n in the range @p [0,__last-__first).
1673 template<typename _ForwardIterator
>
1675 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1676 _ForwardIterator __last
)
1678 // concept requirements
1679 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1681 __glibcxx_requires_valid_range(__first
, __middle
);
1682 __glibcxx_requires_valid_range(__middle
, __last
);
1684 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1686 std::__rotate(__first
, __middle
, __last
, _IterType());
1690 * @brief Copy a sequence, rotating its elements.
1691 * @ingroup mutating_algorithms
1692 * @param __first A forward iterator.
1693 * @param __middle A forward iterator.
1694 * @param __last A forward iterator.
1695 * @param __result An output iterator.
1696 * @return An iterator designating the end of the resulting sequence.
1698 * Copies the elements of the range @p [__first,__last) to the
1699 * range beginning at @result, rotating the copied elements by
1700 * @p (__middle-__first) positions so that the element at @p __middle
1701 * is moved to @p __result, the element at @p __middle+1 is moved
1702 * to @__result+1 and so on for each element in the range @p
1706 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1707 * for each @p n in the range @p [0,__last-__first).
1709 template<typename _ForwardIterator
, typename _OutputIterator
>
1711 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1712 _ForwardIterator __last
, _OutputIterator __result
)
1714 // concept requirements
1715 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1716 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1717 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1718 __glibcxx_requires_valid_range(__first
, __middle
);
1719 __glibcxx_requires_valid_range(__middle
, __last
);
1721 return std::copy(__first
, __middle
,
1722 std::copy(__middle
, __last
, __result
));
1725 /// This is a helper function...
1726 template<typename _ForwardIterator
, typename _Predicate
>
1728 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1729 _Predicate __pred
, forward_iterator_tag
)
1731 if (__first
== __last
)
1734 while (__pred(*__first
))
1735 if (++__first
== __last
)
1738 _ForwardIterator __next
= __first
;
1740 while (++__next
!= __last
)
1741 if (__pred(*__next
))
1743 std::iter_swap(__first
, __next
);
1750 /// This is a helper function...
1751 template<typename _BidirectionalIterator
, typename _Predicate
>
1752 _BidirectionalIterator
1753 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1754 _Predicate __pred
, bidirectional_iterator_tag
)
1759 if (__first
== __last
)
1761 else if (__pred(*__first
))
1767 if (__first
== __last
)
1769 else if (!bool(__pred(*__last
)))
1773 std::iter_swap(__first
, __last
);
1780 /// This is a helper function...
1781 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1783 __inplace_stable_partition(_ForwardIterator __first
,
1784 _ForwardIterator __last
,
1785 _Predicate __pred
, _Distance __len
)
1788 return __pred(*__first
) ? __last
: __first
;
1789 _ForwardIterator __middle
= __first
;
1790 std::advance(__middle
, __len
/ 2);
1791 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1795 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1799 std::rotate(__begin
, __middle
, __end
);
1800 std::advance(__begin
, std::distance(__middle
, __end
));
1804 /// This is a helper function...
1805 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1808 __stable_partition_adaptive(_ForwardIterator __first
,
1809 _ForwardIterator __last
,
1810 _Predicate __pred
, _Distance __len
,
1812 _Distance __buffer_size
)
1814 if (__len
<= __buffer_size
)
1816 _ForwardIterator __result1
= __first
;
1817 _Pointer __result2
= __buffer
;
1818 for (; __first
!= __last
; ++__first
)
1819 if (__pred(*__first
))
1821 *__result1
= _GLIBCXX_MOVE(*__first
);
1826 *__result2
= _GLIBCXX_MOVE(*__first
);
1829 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1834 _ForwardIterator __middle
= __first
;
1835 std::advance(__middle
, __len
/ 2);
1836 _ForwardIterator __begin
=
1837 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1838 __len
/ 2, __buffer
,
1840 _ForwardIterator __end
=
1841 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1843 __buffer
, __buffer_size
);
1844 std::rotate(__begin
, __middle
, __end
);
1845 std::advance(__begin
, std::distance(__middle
, __end
));
1851 * @brief Move elements for which a predicate is true to the beginning
1852 * of a sequence, preserving relative ordering.
1853 * @ingroup mutating_algorithms
1854 * @param __first A forward iterator.
1855 * @param __last A forward iterator.
1856 * @param __pred A predicate functor.
1857 * @return An iterator @p middle such that @p __pred(i) is true for each
1858 * iterator @p i in the range @p [first,middle) and false for each @p i
1859 * in the range @p [middle,last).
1861 * Performs the same function as @p partition() with the additional
1862 * guarantee that the relative ordering of elements in each group is
1863 * preserved, so any two elements @p x and @p y in the range
1864 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1865 * relative ordering after calling @p stable_partition().
1867 template<typename _ForwardIterator
, typename _Predicate
>
1869 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1872 // concept requirements
1873 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1875 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1876 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1877 __glibcxx_requires_valid_range(__first
, __last
);
1879 if (__first
== __last
)
1883 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1885 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1888 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1890 if (__buf
.size() > 0)
1892 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1893 _DistanceType(__buf
.requested_size()),
1895 _DistanceType(__buf
.size()));
1898 std::__inplace_stable_partition(__first
, __last
, __pred
,
1899 _DistanceType(__buf
.requested_size()));
1903 /// This is a helper function for the sort routines.
1904 template<typename _RandomAccessIterator
>
1906 __heap_select(_RandomAccessIterator __first
,
1907 _RandomAccessIterator __middle
,
1908 _RandomAccessIterator __last
)
1910 std::make_heap(__first
, __middle
);
1911 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1912 if (*__i
< *__first
)
1913 std::__pop_heap(__first
, __middle
, __i
);
1916 /// This is a helper function for the sort routines.
1917 template<typename _RandomAccessIterator
, typename _Compare
>
1919 __heap_select(_RandomAccessIterator __first
,
1920 _RandomAccessIterator __middle
,
1921 _RandomAccessIterator __last
, _Compare __comp
)
1923 std::make_heap(__first
, __middle
, __comp
);
1924 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1925 if (__comp(*__i
, *__first
))
1926 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1932 * @brief Copy the smallest elements of a sequence.
1933 * @ingroup sorting_algorithms
1934 * @param __first An iterator.
1935 * @param __last Another iterator.
1936 * @param __result_first A random-access iterator.
1937 * @param __result_last Another random-access iterator.
1938 * @return An iterator indicating the end of the resulting sequence.
1940 * Copies and sorts the smallest N values from the range @p [__first,__last)
1941 * to the range beginning at @p __result_first, where the number of
1942 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1943 * @p (__result_last-__result_first).
1944 * After the sort if @p i and @j are iterators in the range
1945 * @p [__result_first,__result_first+N) such that @i precedes @j then
1946 * @p *j<*i is false.
1947 * The value returned is @p __result_first+N.
1949 template<typename _InputIterator
, typename _RandomAccessIterator
>
1950 _RandomAccessIterator
1951 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1952 _RandomAccessIterator __result_first
,
1953 _RandomAccessIterator __result_last
)
1955 typedef typename iterator_traits
<_InputIterator
>::value_type
1957 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1959 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1962 // concept requirements
1963 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1964 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1966 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1968 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1969 __glibcxx_requires_valid_range(__first
, __last
);
1970 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1972 if (__result_first
== __result_last
)
1973 return __result_last
;
1974 _RandomAccessIterator __result_real_last
= __result_first
;
1975 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1977 *__result_real_last
= *__first
;
1978 ++__result_real_last
;
1981 std::make_heap(__result_first
, __result_real_last
);
1982 while (__first
!= __last
)
1984 if (*__first
< *__result_first
)
1985 std::__adjust_heap(__result_first
, _DistanceType(0),
1986 _DistanceType(__result_real_last
1988 _InputValueType(*__first
));
1991 std::sort_heap(__result_first
, __result_real_last
);
1992 return __result_real_last
;
1996 * @brief Copy the smallest elements of a sequence using a predicate for
1998 * @ingroup sorting_algorithms
1999 * @param __first An input iterator.
2000 * @param __last Another input iterator.
2001 * @param __result_first A random-access iterator.
2002 * @param __result_last Another random-access iterator.
2003 * @param __comp A comparison functor.
2004 * @return An iterator indicating the end of the resulting sequence.
2006 * Copies and sorts the smallest N values from the range @p [__first,__last)
2007 * to the range beginning at @p result_first, where the number of
2008 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
2009 * @p (__result_last-__result_first).
2010 * After the sort if @p i and @j are iterators in the range
2011 * @p [__result_first,__result_first+N) such that @i precedes @j then
2012 * @p __comp(*j,*i) is false.
2013 * The value returned is @p __result_first+N.
2015 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2016 _RandomAccessIterator
2017 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2018 _RandomAccessIterator __result_first
,
2019 _RandomAccessIterator __result_last
,
2022 typedef typename iterator_traits
<_InputIterator
>::value_type
2024 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2026 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2029 // concept requirements
2030 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2031 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2032 _RandomAccessIterator
>)
2033 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2035 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2036 _InputValueType
, _OutputValueType
>)
2037 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2038 _OutputValueType
, _OutputValueType
>)
2039 __glibcxx_requires_valid_range(__first
, __last
);
2040 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2042 if (__result_first
== __result_last
)
2043 return __result_last
;
2044 _RandomAccessIterator __result_real_last
= __result_first
;
2045 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2047 *__result_real_last
= *__first
;
2048 ++__result_real_last
;
2051 std::make_heap(__result_first
, __result_real_last
, __comp
);
2052 while (__first
!= __last
)
2054 if (__comp(*__first
, *__result_first
))
2055 std::__adjust_heap(__result_first
, _DistanceType(0),
2056 _DistanceType(__result_real_last
2058 _InputValueType(*__first
),
2062 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2063 return __result_real_last
;
2066 /// This is a helper function for the sort routine.
2067 template<typename _RandomAccessIterator
>
2069 __unguarded_linear_insert(_RandomAccessIterator __last
)
2071 typename iterator_traits
<_RandomAccessIterator
>::value_type
2072 __val
= _GLIBCXX_MOVE(*__last
);
2073 _RandomAccessIterator __next
= __last
;
2075 while (__val
< *__next
)
2077 *__last
= _GLIBCXX_MOVE(*__next
);
2081 *__last
= _GLIBCXX_MOVE(__val
);
2084 /// This is a helper function for the sort routine.
2085 template<typename _RandomAccessIterator
, typename _Compare
>
2087 __unguarded_linear_insert(_RandomAccessIterator __last
,
2090 typename iterator_traits
<_RandomAccessIterator
>::value_type
2091 __val
= _GLIBCXX_MOVE(*__last
);
2092 _RandomAccessIterator __next
= __last
;
2094 while (__comp(__val
, *__next
))
2096 *__last
= _GLIBCXX_MOVE(*__next
);
2100 *__last
= _GLIBCXX_MOVE(__val
);
2103 /// This is a helper function for the sort routine.
2104 template<typename _RandomAccessIterator
>
2106 __insertion_sort(_RandomAccessIterator __first
,
2107 _RandomAccessIterator __last
)
2109 if (__first
== __last
)
2112 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2114 if (*__i
< *__first
)
2116 typename iterator_traits
<_RandomAccessIterator
>::value_type
2117 __val
= _GLIBCXX_MOVE(*__i
);
2118 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2119 *__first
= _GLIBCXX_MOVE(__val
);
2122 std::__unguarded_linear_insert(__i
);
2126 /// This is a helper function for the sort routine.
2127 template<typename _RandomAccessIterator
, typename _Compare
>
2129 __insertion_sort(_RandomAccessIterator __first
,
2130 _RandomAccessIterator __last
, _Compare __comp
)
2132 if (__first
== __last
) return;
2134 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2136 if (__comp(*__i
, *__first
))
2138 typename iterator_traits
<_RandomAccessIterator
>::value_type
2139 __val
= _GLIBCXX_MOVE(*__i
);
2140 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2141 *__first
= _GLIBCXX_MOVE(__val
);
2144 std::__unguarded_linear_insert(__i
, __comp
);
2148 /// This is a helper function for the sort routine.
2149 template<typename _RandomAccessIterator
>
2151 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2152 _RandomAccessIterator __last
)
2154 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2157 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2158 std::__unguarded_linear_insert(__i
);
2161 /// This is a helper function for the sort routine.
2162 template<typename _RandomAccessIterator
, typename _Compare
>
2164 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2165 _RandomAccessIterator __last
, _Compare __comp
)
2167 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2170 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2171 std::__unguarded_linear_insert(__i
, __comp
);
2176 * This controls some aspect of the sort routines.
2178 enum { _S_threshold
= 16 };
2180 /// This is a helper function for the sort routine.
2181 template<typename _RandomAccessIterator
>
2183 __final_insertion_sort(_RandomAccessIterator __first
,
2184 _RandomAccessIterator __last
)
2186 if (__last
- __first
> int(_S_threshold
))
2188 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2189 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2192 std::__insertion_sort(__first
, __last
);
2195 /// This is a helper function for the sort routine.
2196 template<typename _RandomAccessIterator
, typename _Compare
>
2198 __final_insertion_sort(_RandomAccessIterator __first
,
2199 _RandomAccessIterator __last
, _Compare __comp
)
2201 if (__last
- __first
> int(_S_threshold
))
2203 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2204 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2208 std::__insertion_sort(__first
, __last
, __comp
);
2211 /// This is a helper function...
2212 template<typename _RandomAccessIterator
, typename _Tp
>
2213 _RandomAccessIterator
2214 __unguarded_partition(_RandomAccessIterator __first
,
2215 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2219 while (*__first
< __pivot
)
2222 while (__pivot
< *__last
)
2224 if (!(__first
< __last
))
2226 std::iter_swap(__first
, __last
);
2231 /// This is a helper function...
2232 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2233 _RandomAccessIterator
2234 __unguarded_partition(_RandomAccessIterator __first
,
2235 _RandomAccessIterator __last
,
2236 const _Tp
& __pivot
, _Compare __comp
)
2240 while (__comp(*__first
, __pivot
))
2243 while (__comp(__pivot
, *__last
))
2245 if (!(__first
< __last
))
2247 std::iter_swap(__first
, __last
);
2252 /// This is a helper function...
2253 template<typename _RandomAccessIterator
>
2254 inline _RandomAccessIterator
2255 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2256 _RandomAccessIterator __last
)
2258 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2259 std::__move_median_first(__first
, __mid
, (__last
- 1));
2260 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2264 /// This is a helper function...
2265 template<typename _RandomAccessIterator
, typename _Compare
>
2266 inline _RandomAccessIterator
2267 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2268 _RandomAccessIterator __last
, _Compare __comp
)
2270 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2271 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2272 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2275 /// This is a helper function for the sort routine.
2276 template<typename _RandomAccessIterator
, typename _Size
>
2278 __introsort_loop(_RandomAccessIterator __first
,
2279 _RandomAccessIterator __last
,
2280 _Size __depth_limit
)
2282 while (__last
- __first
> int(_S_threshold
))
2284 if (__depth_limit
== 0)
2286 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
);
2290 _RandomAccessIterator __cut
=
2291 std::__unguarded_partition_pivot(__first
, __last
);
2292 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2297 /// This is a helper function for the sort routine.
2298 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2300 __introsort_loop(_RandomAccessIterator __first
,
2301 _RandomAccessIterator __last
,
2302 _Size __depth_limit
, _Compare __comp
)
2304 while (__last
- __first
> int(_S_threshold
))
2306 if (__depth_limit
== 0)
2308 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
, __comp
);
2312 _RandomAccessIterator __cut
=
2313 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2314 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2321 template<typename _RandomAccessIterator
, typename _Size
>
2323 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2324 _RandomAccessIterator __last
, _Size __depth_limit
)
2326 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2329 while (__last
- __first
> 3)
2331 if (__depth_limit
== 0)
2333 std::__heap_select(__first
, __nth
+ 1, __last
);
2335 // Place the nth largest element in its final position.
2336 std::iter_swap(__first
, __nth
);
2340 _RandomAccessIterator __cut
=
2341 std::__unguarded_partition_pivot(__first
, __last
);
2347 std::__insertion_sort(__first
, __last
);
2350 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2352 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2353 _RandomAccessIterator __last
, _Size __depth_limit
,
2356 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2359 while (__last
- __first
> 3)
2361 if (__depth_limit
== 0)
2363 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2364 // Place the nth largest element in its final position.
2365 std::iter_swap(__first
, __nth
);
2369 _RandomAccessIterator __cut
=
2370 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2376 std::__insertion_sort(__first
, __last
, __comp
);
2381 // lower_bound moved to stl_algobase.h
2384 * @brief Finds the first position in which @a val could be inserted
2385 * without changing the ordering.
2386 * @ingroup binary_search_algorithms
2387 * @param __first An iterator.
2388 * @param __last Another iterator.
2389 * @param __val The search term.
2390 * @param __comp A functor to use for comparisons.
2391 * @return An iterator pointing to the first element <em>not less
2392 * than</em> @a __val, or end() if every element is less
2394 * @ingroup binary_search_algorithms
2396 * The comparison function should have the same effects on ordering as
2397 * the function used for the initial sort.
2399 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2401 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2402 const _Tp
& __val
, _Compare __comp
)
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(_BinaryPredicateConcept
<_Compare
,
2413 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2416 _DistanceType __len
= std::distance(__first
, __last
);
2420 _DistanceType __half
= __len
>> 1;
2421 _ForwardIterator __middle
= __first
;
2422 std::advance(__middle
, __half
);
2423 if (__comp(*__middle
, __val
))
2427 __len
= __len
- __half
- 1;
2436 * @brief Finds the last position in which @a val could be inserted
2437 * without changing the ordering.
2438 * @ingroup binary_search_algorithms
2439 * @param __first An iterator.
2440 * @param __last Another iterator.
2441 * @param __val The search term.
2442 * @return An iterator pointing to the first element greater than @a __val,
2443 * or end() if no elements are greater than @a __val.
2444 * @ingroup binary_search_algorithms
2446 template<typename _ForwardIterator
, typename _Tp
>
2448 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2451 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2453 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2456 // concept requirements
2457 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2458 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2459 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2461 _DistanceType __len
= std::distance(__first
, __last
);
2465 _DistanceType __half
= __len
>> 1;
2466 _ForwardIterator __middle
= __first
;
2467 std::advance(__middle
, __half
);
2468 if (__val
< *__middle
)
2474 __len
= __len
- __half
- 1;
2481 * @brief Finds the last position in which @a val could be inserted
2482 * without changing the ordering.
2483 * @ingroup binary_search_algorithms
2484 * @param __first An iterator.
2485 * @param __last Another iterator.
2486 * @param __val The search term.
2487 * @param __comp A functor to use for comparisons.
2488 * @return An iterator pointing to the first element greater than @a __val,
2489 * or end() if no elements are greater than @a __val.
2490 * @ingroup binary_search_algorithms
2492 * The comparison function should have the same effects on ordering as
2493 * the function used for the initial sort.
2495 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2497 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2498 const _Tp
& __val
, _Compare __comp
)
2500 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2502 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2505 // concept requirements
2506 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2507 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2509 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2512 _DistanceType __len
= std::distance(__first
, __last
);
2516 _DistanceType __half
= __len
>> 1;
2517 _ForwardIterator __middle
= __first
;
2518 std::advance(__middle
, __half
);
2519 if (__comp(__val
, *__middle
))
2525 __len
= __len
- __half
- 1;
2532 * @brief Finds the largest subrange in which @a val could be inserted
2533 * at any place in it without changing the ordering.
2534 * @ingroup binary_search_algorithms
2535 * @param __first An iterator.
2536 * @param __last Another iterator.
2537 * @param __val The search term.
2538 * @return An pair of iterators defining the subrange.
2539 * @ingroup binary_search_algorithms
2541 * This is equivalent to
2543 * std::make_pair(lower_bound(__first, __last, __val),
2544 * upper_bound(__first, __last, __val))
2546 * but does not actually call those functions.
2548 template<typename _ForwardIterator
, typename _Tp
>
2549 pair
<_ForwardIterator
, _ForwardIterator
>
2550 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2553 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2555 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2558 // concept requirements
2559 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2560 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2561 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2562 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2563 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2565 _DistanceType __len
= std::distance(__first
, __last
);
2569 _DistanceType __half
= __len
>> 1;
2570 _ForwardIterator __middle
= __first
;
2571 std::advance(__middle
, __half
);
2572 if (*__middle
< __val
)
2576 __len
= __len
- __half
- 1;
2578 else if (__val
< *__middle
)
2582 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2584 std::advance(__first
, __len
);
2585 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2587 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2590 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2594 * @brief Finds the largest subrange in which @a val could be inserted
2595 * at any place in it without changing the ordering.
2596 * @param __first An iterator.
2597 * @param __last Another iterator.
2598 * @param __val The search term.
2599 * @param __comp A functor to use for comparisons.
2600 * @return An pair of iterators defining the subrange.
2601 * @ingroup binary_search_algorithms
2603 * This is equivalent to
2605 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2606 * upper_bound(__first, __last, __val, __comp))
2608 * but does not actually call those functions.
2610 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2611 pair
<_ForwardIterator
, _ForwardIterator
>
2612 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2613 const _Tp
& __val
, _Compare __comp
)
2615 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2617 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2620 // concept requirements
2621 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2622 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2624 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2626 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2628 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2631 _DistanceType __len
= std::distance(__first
, __last
);
2635 _DistanceType __half
= __len
>> 1;
2636 _ForwardIterator __middle
= __first
;
2637 std::advance(__middle
, __half
);
2638 if (__comp(*__middle
, __val
))
2642 __len
= __len
- __half
- 1;
2644 else if (__comp(__val
, *__middle
))
2648 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2650 std::advance(__first
, __len
);
2651 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2653 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2656 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2660 * @brief Determines whether an element exists in a range.
2661 * @ingroup binary_search_algorithms
2662 * @param __first An iterator.
2663 * @param __last Another iterator.
2664 * @param __val The search term.
2665 * @return True if @a __val (or its equivalent) is in [@a
2666 * __first,@a __last ].
2668 * Note that this does not actually return an iterator to @a __val. For
2669 * that, use std::find or a container's specialized find member functions.
2671 template<typename _ForwardIterator
, typename _Tp
>
2673 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2676 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2679 // concept requirements
2680 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2681 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2682 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2683 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2685 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2686 return __i
!= __last
&& !(__val
< *__i
);
2690 * @brief Determines whether an element exists in a range.
2691 * @ingroup binary_search_algorithms
2692 * @param __first An iterator.
2693 * @param __last Another iterator.
2694 * @param __val The search term.
2695 * @param __comp A functor to use for comparisons.
2696 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2698 * Note that this does not actually return an iterator to @a val. For
2699 * that, use std::find or a container's specialized find member functions.
2701 * The comparison function should have the same effects on ordering as
2702 * the function used for the initial sort.
2704 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2706 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2707 const _Tp
& __val
, _Compare __comp
)
2709 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2712 // concept requirements
2713 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2714 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2716 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2718 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2721 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2722 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2727 /// This is a helper function for the __merge_adaptive routines.
2728 template<typename _InputIterator1
, typename _InputIterator2
,
2729 typename _OutputIterator
>
2731 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2732 _InputIterator2 __first2
, _InputIterator2 __last2
,
2733 _OutputIterator __result
)
2735 while (__first1
!= __last1
&& __first2
!= __last2
)
2737 if (*__first2
< *__first1
)
2739 *__result
= _GLIBCXX_MOVE(*__first2
);
2744 *__result
= _GLIBCXX_MOVE(*__first1
);
2749 if (__first1
!= __last1
)
2750 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2753 /// This is a helper function for the __merge_adaptive routines.
2754 template<typename _InputIterator1
, typename _InputIterator2
,
2755 typename _OutputIterator
, typename _Compare
>
2757 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2758 _InputIterator2 __first2
, _InputIterator2 __last2
,
2759 _OutputIterator __result
, _Compare __comp
)
2761 while (__first1
!= __last1
&& __first2
!= __last2
)
2763 if (__comp(*__first2
, *__first1
))
2765 *__result
= _GLIBCXX_MOVE(*__first2
);
2770 *__result
= _GLIBCXX_MOVE(*__first1
);
2775 if (__first1
!= __last1
)
2776 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2779 /// This is a helper function for the __merge_adaptive routines.
2780 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2781 typename _BidirectionalIterator3
>
2783 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2784 _BidirectionalIterator1 __last1
,
2785 _BidirectionalIterator2 __first2
,
2786 _BidirectionalIterator2 __last2
,
2787 _BidirectionalIterator3 __result
)
2789 if (__first1
== __last1
)
2791 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2794 else if (__first2
== __last2
)
2801 if (*__last2
< *__last1
)
2803 *--__result
= _GLIBCXX_MOVE(*__last1
);
2804 if (__first1
== __last1
)
2806 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2813 *--__result
= _GLIBCXX_MOVE(*__last2
);
2814 if (__first2
== __last2
)
2821 /// This is a helper function for the __merge_adaptive routines.
2822 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2823 typename _BidirectionalIterator3
, typename _Compare
>
2825 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2826 _BidirectionalIterator1 __last1
,
2827 _BidirectionalIterator2 __first2
,
2828 _BidirectionalIterator2 __last2
,
2829 _BidirectionalIterator3 __result
,
2832 if (__first1
== __last1
)
2834 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2837 else if (__first2
== __last2
)
2844 if (__comp(*__last2
, *__last1
))
2846 *--__result
= _GLIBCXX_MOVE(*__last1
);
2847 if (__first1
== __last1
)
2849 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2856 *--__result
= _GLIBCXX_MOVE(*__last2
);
2857 if (__first2
== __last2
)
2864 /// This is a helper function for the merge routines.
2865 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2867 _BidirectionalIterator1
2868 __rotate_adaptive(_BidirectionalIterator1 __first
,
2869 _BidirectionalIterator1 __middle
,
2870 _BidirectionalIterator1 __last
,
2871 _Distance __len1
, _Distance __len2
,
2872 _BidirectionalIterator2 __buffer
,
2873 _Distance __buffer_size
)
2875 _BidirectionalIterator2 __buffer_end
;
2876 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2880 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2881 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2882 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2887 else if (__len1
<= __buffer_size
)
2891 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2892 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2893 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2900 std::rotate(__first
, __middle
, __last
);
2901 std::advance(__first
, std::distance(__middle
, __last
));
2906 /// This is a helper function for the merge routines.
2907 template<typename _BidirectionalIterator
, typename _Distance
,
2910 __merge_adaptive(_BidirectionalIterator __first
,
2911 _BidirectionalIterator __middle
,
2912 _BidirectionalIterator __last
,
2913 _Distance __len1
, _Distance __len2
,
2914 _Pointer __buffer
, _Distance __buffer_size
)
2916 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2918 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2919 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2922 else if (__len2
<= __buffer_size
)
2924 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2925 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2926 __buffer_end
, __last
);
2930 _BidirectionalIterator __first_cut
= __first
;
2931 _BidirectionalIterator __second_cut
= __middle
;
2932 _Distance __len11
= 0;
2933 _Distance __len22
= 0;
2934 if (__len1
> __len2
)
2936 __len11
= __len1
/ 2;
2937 std::advance(__first_cut
, __len11
);
2938 __second_cut
= std::lower_bound(__middle
, __last
,
2940 __len22
= std::distance(__middle
, __second_cut
);
2944 __len22
= __len2
/ 2;
2945 std::advance(__second_cut
, __len22
);
2946 __first_cut
= std::upper_bound(__first
, __middle
,
2948 __len11
= std::distance(__first
, __first_cut
);
2950 _BidirectionalIterator __new_middle
=
2951 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2952 __len1
- __len11
, __len22
, __buffer
,
2954 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2955 __len22
, __buffer
, __buffer_size
);
2956 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2958 __len2
- __len22
, __buffer
, __buffer_size
);
2962 /// This is a helper function for the merge routines.
2963 template<typename _BidirectionalIterator
, typename _Distance
,
2964 typename _Pointer
, typename _Compare
>
2966 __merge_adaptive(_BidirectionalIterator __first
,
2967 _BidirectionalIterator __middle
,
2968 _BidirectionalIterator __last
,
2969 _Distance __len1
, _Distance __len2
,
2970 _Pointer __buffer
, _Distance __buffer_size
,
2973 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2975 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2976 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2979 else if (__len2
<= __buffer_size
)
2981 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2982 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2983 __buffer_end
, __last
, __comp
);
2987 _BidirectionalIterator __first_cut
= __first
;
2988 _BidirectionalIterator __second_cut
= __middle
;
2989 _Distance __len11
= 0;
2990 _Distance __len22
= 0;
2991 if (__len1
> __len2
)
2993 __len11
= __len1
/ 2;
2994 std::advance(__first_cut
, __len11
);
2995 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2997 __len22
= std::distance(__middle
, __second_cut
);
3001 __len22
= __len2
/ 2;
3002 std::advance(__second_cut
, __len22
);
3003 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3005 __len11
= std::distance(__first
, __first_cut
);
3007 _BidirectionalIterator __new_middle
=
3008 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3009 __len1
- __len11
, __len22
, __buffer
,
3011 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3012 __len22
, __buffer
, __buffer_size
, __comp
);
3013 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3015 __len2
- __len22
, __buffer
,
3016 __buffer_size
, __comp
);
3020 /// This is a helper function for the merge routines.
3021 template<typename _BidirectionalIterator
, typename _Distance
>
3023 __merge_without_buffer(_BidirectionalIterator __first
,
3024 _BidirectionalIterator __middle
,
3025 _BidirectionalIterator __last
,
3026 _Distance __len1
, _Distance __len2
)
3028 if (__len1
== 0 || __len2
== 0)
3030 if (__len1
+ __len2
== 2)
3032 if (*__middle
< *__first
)
3033 std::iter_swap(__first
, __middle
);
3036 _BidirectionalIterator __first_cut
= __first
;
3037 _BidirectionalIterator __second_cut
= __middle
;
3038 _Distance __len11
= 0;
3039 _Distance __len22
= 0;
3040 if (__len1
> __len2
)
3042 __len11
= __len1
/ 2;
3043 std::advance(__first_cut
, __len11
);
3044 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3045 __len22
= std::distance(__middle
, __second_cut
);
3049 __len22
= __len2
/ 2;
3050 std::advance(__second_cut
, __len22
);
3051 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3052 __len11
= std::distance(__first
, __first_cut
);
3054 std::rotate(__first_cut
, __middle
, __second_cut
);
3055 _BidirectionalIterator __new_middle
= __first_cut
;
3056 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3057 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3059 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3060 __len1
- __len11
, __len2
- __len22
);
3063 /// This is a helper function for the merge routines.
3064 template<typename _BidirectionalIterator
, typename _Distance
,
3067 __merge_without_buffer(_BidirectionalIterator __first
,
3068 _BidirectionalIterator __middle
,
3069 _BidirectionalIterator __last
,
3070 _Distance __len1
, _Distance __len2
,
3073 if (__len1
== 0 || __len2
== 0)
3075 if (__len1
+ __len2
== 2)
3077 if (__comp(*__middle
, *__first
))
3078 std::iter_swap(__first
, __middle
);
3081 _BidirectionalIterator __first_cut
= __first
;
3082 _BidirectionalIterator __second_cut
= __middle
;
3083 _Distance __len11
= 0;
3084 _Distance __len22
= 0;
3085 if (__len1
> __len2
)
3087 __len11
= __len1
/ 2;
3088 std::advance(__first_cut
, __len11
);
3089 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3091 __len22
= std::distance(__middle
, __second_cut
);
3095 __len22
= __len2
/ 2;
3096 std::advance(__second_cut
, __len22
);
3097 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3099 __len11
= std::distance(__first
, __first_cut
);
3101 std::rotate(__first_cut
, __middle
, __second_cut
);
3102 _BidirectionalIterator __new_middle
= __first_cut
;
3103 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3104 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3105 __len11
, __len22
, __comp
);
3106 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3107 __len1
- __len11
, __len2
- __len22
, __comp
);
3111 * @brief Merges two sorted ranges in place.
3112 * @ingroup sorting_algorithms
3113 * @param __first An iterator.
3114 * @param __middle Another iterator.
3115 * @param __last Another iterator.
3118 * Merges two sorted and consecutive ranges, [__first,__middle) and
3119 * [__middle,__last), and puts the result in [__first,__last). The
3120 * output will be sorted. The sort is @e stable, that is, for
3121 * equivalent elements in the two ranges, elements from the first
3122 * range will always come before elements from the second.
3124 * If enough additional memory is available, this takes (__last-__first)-1
3125 * comparisons. Otherwise an NlogN algorithm is used, where N is
3126 * distance(__first,__last).
3128 template<typename _BidirectionalIterator
>
3130 inplace_merge(_BidirectionalIterator __first
,
3131 _BidirectionalIterator __middle
,
3132 _BidirectionalIterator __last
)
3134 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3136 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3139 // concept requirements
3140 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3141 _BidirectionalIterator
>)
3142 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3143 __glibcxx_requires_sorted(__first
, __middle
);
3144 __glibcxx_requires_sorted(__middle
, __last
);
3146 if (__first
== __middle
|| __middle
== __last
)
3149 _DistanceType __len1
= std::distance(__first
, __middle
);
3150 _DistanceType __len2
= std::distance(__middle
, __last
);
3152 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3154 if (__buf
.begin() == 0)
3155 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3157 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3158 __buf
.begin(), _DistanceType(__buf
.size()));
3162 * @brief Merges two sorted ranges in place.
3163 * @ingroup sorting_algorithms
3164 * @param __first An iterator.
3165 * @param __middle Another iterator.
3166 * @param __last Another iterator.
3167 * @param __comp A functor to use for comparisons.
3170 * Merges two sorted and consecutive ranges, [__first,__middle) and
3171 * [middle,last), and puts the result in [__first,__last). The output will
3172 * be sorted. The sort is @e stable, that is, for equivalent
3173 * elements in the two ranges, elements from the first range will always
3174 * come before elements from the second.
3176 * If enough additional memory is available, this takes (__last-__first)-1
3177 * comparisons. Otherwise an NlogN algorithm is used, where N is
3178 * distance(__first,__last).
3180 * The comparison function should have the same effects on ordering as
3181 * the function used for the initial sort.
3183 template<typename _BidirectionalIterator
, typename _Compare
>
3185 inplace_merge(_BidirectionalIterator __first
,
3186 _BidirectionalIterator __middle
,
3187 _BidirectionalIterator __last
,
3190 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3192 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3195 // concept requirements
3196 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3197 _BidirectionalIterator
>)
3198 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3199 _ValueType
, _ValueType
>)
3200 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3201 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3203 if (__first
== __middle
|| __middle
== __last
)
3206 const _DistanceType __len1
= std::distance(__first
, __middle
);
3207 const _DistanceType __len2
= std::distance(__middle
, __last
);
3209 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3211 if (__buf
.begin() == 0)
3212 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3215 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3216 __buf
.begin(), _DistanceType(__buf
.size()),
3221 /// This is a helper function for the __merge_sort_loop routines.
3222 template<typename _InputIterator1
, typename _InputIterator2
,
3223 typename _OutputIterator
>
3225 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3226 _InputIterator2 __first2
, _InputIterator2 __last2
,
3227 _OutputIterator __result
)
3229 while (__first1
!= __last1
&& __first2
!= __last2
)
3231 if (*__first2
< *__first1
)
3233 *__result
= _GLIBCXX_MOVE(*__first2
);
3238 *__result
= _GLIBCXX_MOVE(*__first1
);
3243 return _GLIBCXX_MOVE3(__first2
, __last2
,
3244 _GLIBCXX_MOVE3(__first1
, __last1
,
3248 /// This is a helper function for the __merge_sort_loop routines.
3249 template<typename _InputIterator1
, typename _InputIterator2
,
3250 typename _OutputIterator
, typename _Compare
>
3252 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3253 _InputIterator2 __first2
, _InputIterator2 __last2
,
3254 _OutputIterator __result
, _Compare __comp
)
3256 while (__first1
!= __last1
&& __first2
!= __last2
)
3258 if (__comp(*__first2
, *__first1
))
3260 *__result
= _GLIBCXX_MOVE(*__first2
);
3265 *__result
= _GLIBCXX_MOVE(*__first1
);
3270 return _GLIBCXX_MOVE3(__first2
, __last2
,
3271 _GLIBCXX_MOVE3(__first1
, __last1
,
3275 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3278 __merge_sort_loop(_RandomAccessIterator1 __first
,
3279 _RandomAccessIterator1 __last
,
3280 _RandomAccessIterator2 __result
,
3281 _Distance __step_size
)
3283 const _Distance __two_step
= 2 * __step_size
;
3285 while (__last
- __first
>= __two_step
)
3287 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3288 __first
+ __step_size
,
3289 __first
+ __two_step
, __result
);
3290 __first
+= __two_step
;
3293 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3294 std::__move_merge(__first
, __first
+ __step_size
,
3295 __first
+ __step_size
, __last
, __result
);
3298 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3299 typename _Distance
, typename _Compare
>
3301 __merge_sort_loop(_RandomAccessIterator1 __first
,
3302 _RandomAccessIterator1 __last
,
3303 _RandomAccessIterator2 __result
, _Distance __step_size
,
3306 const _Distance __two_step
= 2 * __step_size
;
3308 while (__last
- __first
>= __two_step
)
3310 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3311 __first
+ __step_size
,
3312 __first
+ __two_step
,
3314 __first
+= __two_step
;
3316 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3318 std::__move_merge(__first
,__first
+ __step_size
,
3319 __first
+ __step_size
, __last
, __result
, __comp
);
3322 template<typename _RandomAccessIterator
, typename _Distance
>
3324 __chunk_insertion_sort(_RandomAccessIterator __first
,
3325 _RandomAccessIterator __last
,
3326 _Distance __chunk_size
)
3328 while (__last
- __first
>= __chunk_size
)
3330 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3331 __first
+= __chunk_size
;
3333 std::__insertion_sort(__first
, __last
);
3336 template<typename _RandomAccessIterator
, typename _Distance
,
3339 __chunk_insertion_sort(_RandomAccessIterator __first
,
3340 _RandomAccessIterator __last
,
3341 _Distance __chunk_size
, _Compare __comp
)
3343 while (__last
- __first
>= __chunk_size
)
3345 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3346 __first
+= __chunk_size
;
3348 std::__insertion_sort(__first
, __last
, __comp
);
3351 enum { _S_chunk_size
= 7 };
3353 template<typename _RandomAccessIterator
, typename _Pointer
>
3355 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3356 _RandomAccessIterator __last
,
3359 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3362 const _Distance __len
= __last
- __first
;
3363 const _Pointer __buffer_last
= __buffer
+ __len
;
3365 _Distance __step_size
= _S_chunk_size
;
3366 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3368 while (__step_size
< __len
)
3370 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3372 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3377 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3379 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3380 _RandomAccessIterator __last
,
3381 _Pointer __buffer
, _Compare __comp
)
3383 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3386 const _Distance __len
= __last
- __first
;
3387 const _Pointer __buffer_last
= __buffer
+ __len
;
3389 _Distance __step_size
= _S_chunk_size
;
3390 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3392 while (__step_size
< __len
)
3394 std::__merge_sort_loop(__first
, __last
, __buffer
,
3395 __step_size
, __comp
);
3397 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3398 __step_size
, __comp
);
3403 template<typename _RandomAccessIterator
, typename _Pointer
,
3406 __stable_sort_adaptive(_RandomAccessIterator __first
,
3407 _RandomAccessIterator __last
,
3408 _Pointer __buffer
, _Distance __buffer_size
)
3410 const _Distance __len
= (__last
- __first
+ 1) / 2;
3411 const _RandomAccessIterator __middle
= __first
+ __len
;
3412 if (__len
> __buffer_size
)
3414 std::__stable_sort_adaptive(__first
, __middle
,
3415 __buffer
, __buffer_size
);
3416 std::__stable_sort_adaptive(__middle
, __last
,
3417 __buffer
, __buffer_size
);
3421 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3422 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3424 std::__merge_adaptive(__first
, __middle
, __last
,
3425 _Distance(__middle
- __first
),
3426 _Distance(__last
- __middle
),
3427 __buffer
, __buffer_size
);
3430 template<typename _RandomAccessIterator
, typename _Pointer
,
3431 typename _Distance
, typename _Compare
>
3433 __stable_sort_adaptive(_RandomAccessIterator __first
,
3434 _RandomAccessIterator __last
,
3435 _Pointer __buffer
, _Distance __buffer_size
,
3438 const _Distance __len
= (__last
- __first
+ 1) / 2;
3439 const _RandomAccessIterator __middle
= __first
+ __len
;
3440 if (__len
> __buffer_size
)
3442 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3443 __buffer_size
, __comp
);
3444 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3445 __buffer_size
, __comp
);
3449 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3450 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3452 std::__merge_adaptive(__first
, __middle
, __last
,
3453 _Distance(__middle
- __first
),
3454 _Distance(__last
- __middle
),
3455 __buffer
, __buffer_size
,
3459 /// This is a helper function for the stable sorting routines.
3460 template<typename _RandomAccessIterator
>
3462 __inplace_stable_sort(_RandomAccessIterator __first
,
3463 _RandomAccessIterator __last
)
3465 if (__last
- __first
< 15)
3467 std::__insertion_sort(__first
, __last
);
3470 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3471 std::__inplace_stable_sort(__first
, __middle
);
3472 std::__inplace_stable_sort(__middle
, __last
);
3473 std::__merge_without_buffer(__first
, __middle
, __last
,
3478 /// This is a helper function for the stable sorting routines.
3479 template<typename _RandomAccessIterator
, typename _Compare
>
3481 __inplace_stable_sort(_RandomAccessIterator __first
,
3482 _RandomAccessIterator __last
, _Compare __comp
)
3484 if (__last
- __first
< 15)
3486 std::__insertion_sort(__first
, __last
, __comp
);
3489 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3490 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3491 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3492 std::__merge_without_buffer(__first
, __middle
, __last
,
3500 // Set algorithms: includes, set_union, set_intersection, set_difference,
3501 // set_symmetric_difference. All of these algorithms have the precondition
3502 // that their input ranges are sorted and the postcondition that their output
3503 // ranges are sorted.
3506 * @brief Determines whether all elements of a sequence exists in a range.
3507 * @param __first1 Start of search range.
3508 * @param __last1 End of search range.
3509 * @param __first2 Start of sequence
3510 * @param __last2 End of sequence.
3511 * @return True if each element in [__first2,__last2) is contained in order
3512 * within [__first1,__last1). False otherwise.
3513 * @ingroup set_algorithms
3515 * This operation expects both [__first1,__last1) and
3516 * [__first2,__last2) to be sorted. Searches for the presence of
3517 * each element in [__first2,__last2) within [__first1,__last1).
3518 * The iterators over each range only move forward, so this is a
3519 * linear algorithm. If an element in [__first2,__last2) is not
3520 * found before the search iterator reaches @a __last2, false is
3523 template<typename _InputIterator1
, typename _InputIterator2
>
3525 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3526 _InputIterator2 __first2
, _InputIterator2 __last2
)
3528 typedef typename iterator_traits
<_InputIterator1
>::value_type
3530 typedef typename iterator_traits
<_InputIterator2
>::value_type
3533 // concept requirements
3534 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3535 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3536 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3537 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3538 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3539 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3541 while (__first1
!= __last1
&& __first2
!= __last2
)
3542 if (*__first2
< *__first1
)
3544 else if(*__first1
< *__first2
)
3547 ++__first1
, ++__first2
;
3549 return __first2
== __last2
;
3553 * @brief Determines whether all elements of a sequence exists in a range
3555 * @ingroup set_algorithms
3556 * @param __first1 Start of search range.
3557 * @param __last1 End of search range.
3558 * @param __first2 Start of sequence
3559 * @param __last2 End of sequence.
3560 * @param __comp Comparison function to use.
3561 * @return True if each element in [__first2,__last2) is contained
3562 * in order within [__first1,__last1) according to comp. False
3563 * otherwise. @ingroup set_algorithms
3565 * This operation expects both [__first1,__last1) and
3566 * [__first2,__last2) to be sorted. Searches for the presence of
3567 * each element in [__first2,__last2) within [__first1,__last1),
3568 * using comp to decide. The iterators over each range only move
3569 * forward, so this is a linear algorithm. If an element in
3570 * [__first2,__last2) is not found before the search iterator
3571 * reaches @a __last2, false is returned.
3573 template<typename _InputIterator1
, typename _InputIterator2
,
3576 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3577 _InputIterator2 __first2
, _InputIterator2 __last2
,
3580 typedef typename iterator_traits
<_InputIterator1
>::value_type
3582 typedef typename iterator_traits
<_InputIterator2
>::value_type
3585 // concept requirements
3586 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3587 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3588 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3589 _ValueType1
, _ValueType2
>)
3590 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3591 _ValueType2
, _ValueType1
>)
3592 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3593 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3595 while (__first1
!= __last1
&& __first2
!= __last2
)
3596 if (__comp(*__first2
, *__first1
))
3598 else if(__comp(*__first1
, *__first2
))
3601 ++__first1
, ++__first2
;
3603 return __first2
== __last2
;
3612 // set_symmetric_difference
3617 * @brief Permute range into the next @a dictionary ordering.
3618 * @ingroup sorting_algorithms
3619 * @param __first Start of range.
3620 * @param __last End of range.
3621 * @return False if wrapped to first permutation, true otherwise.
3623 * Treats all permutations of the range as a set of @a dictionary sorted
3624 * sequences. Permutes the current sequence into the next one of this set.
3625 * Returns true if there are more sequences to generate. If the sequence
3626 * is the largest of the set, the smallest is generated and false returned.
3628 template<typename _BidirectionalIterator
>
3630 next_permutation(_BidirectionalIterator __first
,
3631 _BidirectionalIterator __last
)
3633 // concept requirements
3634 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3635 _BidirectionalIterator
>)
3636 __glibcxx_function_requires(_LessThanComparableConcept
<
3637 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3638 __glibcxx_requires_valid_range(__first
, __last
);
3640 if (__first
== __last
)
3642 _BidirectionalIterator __i
= __first
;
3651 _BidirectionalIterator __ii
= __i
;
3655 _BidirectionalIterator __j
= __last
;
3656 while (!(*__i
< *--__j
))
3658 std::iter_swap(__i
, __j
);
3659 std::reverse(__ii
, __last
);
3664 std::reverse(__first
, __last
);
3671 * @brief Permute range into the next @a dictionary ordering using
3672 * comparison functor.
3673 * @ingroup sorting_algorithms
3674 * @param __first Start of range.
3675 * @param __last End of range.
3676 * @param __comp A comparison functor.
3677 * @return False if wrapped to first permutation, true otherwise.
3679 * Treats all permutations of the range [__first,__last) as a set of
3680 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3681 * sequence into the next one of this set. Returns true if there are more
3682 * sequences to generate. If the sequence is the largest of the set, the
3683 * smallest is generated and false returned.
3685 template<typename _BidirectionalIterator
, typename _Compare
>
3687 next_permutation(_BidirectionalIterator __first
,
3688 _BidirectionalIterator __last
, _Compare __comp
)
3690 // concept requirements
3691 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3692 _BidirectionalIterator
>)
3693 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3694 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3695 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3696 __glibcxx_requires_valid_range(__first
, __last
);
3698 if (__first
== __last
)
3700 _BidirectionalIterator __i
= __first
;
3709 _BidirectionalIterator __ii
= __i
;
3711 if (__comp(*__i
, *__ii
))
3713 _BidirectionalIterator __j
= __last
;
3714 while (!bool(__comp(*__i
, *--__j
)))
3716 std::iter_swap(__i
, __j
);
3717 std::reverse(__ii
, __last
);
3722 std::reverse(__first
, __last
);
3729 * @brief Permute range into the previous @a dictionary ordering.
3730 * @ingroup sorting_algorithms
3731 * @param __first Start of range.
3732 * @param __last End of range.
3733 * @return False if wrapped to last permutation, true otherwise.
3735 * Treats all permutations of the range as a set of @a dictionary sorted
3736 * sequences. Permutes the current sequence into the previous one of this
3737 * set. Returns true if there are more sequences to generate. If the
3738 * sequence is the smallest of the set, the largest is generated and false
3741 template<typename _BidirectionalIterator
>
3743 prev_permutation(_BidirectionalIterator __first
,
3744 _BidirectionalIterator __last
)
3746 // concept requirements
3747 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3748 _BidirectionalIterator
>)
3749 __glibcxx_function_requires(_LessThanComparableConcept
<
3750 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3751 __glibcxx_requires_valid_range(__first
, __last
);
3753 if (__first
== __last
)
3755 _BidirectionalIterator __i
= __first
;
3764 _BidirectionalIterator __ii
= __i
;
3768 _BidirectionalIterator __j
= __last
;
3769 while (!(*--__j
< *__i
))
3771 std::iter_swap(__i
, __j
);
3772 std::reverse(__ii
, __last
);
3777 std::reverse(__first
, __last
);
3784 * @brief Permute range into the previous @a dictionary ordering using
3785 * comparison functor.
3786 * @ingroup sorting_algorithms
3787 * @param __first Start of range.
3788 * @param __last End of range.
3789 * @param __comp A comparison functor.
3790 * @return False if wrapped to last permutation, true otherwise.
3792 * Treats all permutations of the range [__first,__last) as a set of
3793 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3794 * sequence into the previous one of this set. Returns true if there are
3795 * more sequences to generate. If the sequence is the smallest of the set,
3796 * the largest is generated and false returned.
3798 template<typename _BidirectionalIterator
, typename _Compare
>
3800 prev_permutation(_BidirectionalIterator __first
,
3801 _BidirectionalIterator __last
, _Compare __comp
)
3803 // concept requirements
3804 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3805 _BidirectionalIterator
>)
3806 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3807 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3808 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3809 __glibcxx_requires_valid_range(__first
, __last
);
3811 if (__first
== __last
)
3813 _BidirectionalIterator __i
= __first
;
3822 _BidirectionalIterator __ii
= __i
;
3824 if (__comp(*__ii
, *__i
))
3826 _BidirectionalIterator __j
= __last
;
3827 while (!bool(__comp(*--__j
, *__i
)))
3829 std::iter_swap(__i
, __j
);
3830 std::reverse(__ii
, __last
);
3835 std::reverse(__first
, __last
);
3845 * @brief Copy a sequence, replacing each element of one value with another
3847 * @param __first An input iterator.
3848 * @param __last An input iterator.
3849 * @param __result An output iterator.
3850 * @param __old_value The value to be replaced.
3851 * @param __new_value The replacement value.
3852 * @return The end of the output sequence, @p result+(last-first).
3854 * Copies each element in the input range @p [__first,__last) to the
3855 * output range @p [__result,__result+(__last-__first)) replacing elements
3856 * equal to @p __old_value with @p __new_value.
3858 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3860 replace_copy(_InputIterator __first
, _InputIterator __last
,
3861 _OutputIterator __result
,
3862 const _Tp
& __old_value
, const _Tp
& __new_value
)
3864 // concept requirements
3865 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3866 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3867 typename iterator_traits
<_InputIterator
>::value_type
>)
3868 __glibcxx_function_requires(_EqualOpConcept
<
3869 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3870 __glibcxx_requires_valid_range(__first
, __last
);
3872 for (; __first
!= __last
; ++__first
, ++__result
)
3873 if (*__first
== __old_value
)
3874 *__result
= __new_value
;
3876 *__result
= *__first
;
3881 * @brief Copy a sequence, replacing each value for which a predicate
3882 * returns true with another value.
3883 * @ingroup mutating_algorithms
3884 * @param __first An input iterator.
3885 * @param __last An input iterator.
3886 * @param __result An output iterator.
3887 * @param __pred A predicate.
3888 * @param __new_value The replacement value.
3889 * @return The end of the output sequence, @p __result+(__last-__first).
3891 * Copies each element in the range @p [__first,__last) to the range
3892 * @p [__result,__result+(__last-__first)) replacing elements for which
3893 * @p __pred returns true with @p __new_value.
3895 template<typename _InputIterator
, typename _OutputIterator
,
3896 typename _Predicate
, typename _Tp
>
3898 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3899 _OutputIterator __result
,
3900 _Predicate __pred
, const _Tp
& __new_value
)
3902 // concept requirements
3903 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3904 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3905 typename iterator_traits
<_InputIterator
>::value_type
>)
3906 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3907 typename iterator_traits
<_InputIterator
>::value_type
>)
3908 __glibcxx_requires_valid_range(__first
, __last
);
3910 for (; __first
!= __last
; ++__first
, ++__result
)
3911 if (__pred(*__first
))
3912 *__result
= __new_value
;
3914 *__result
= *__first
;
3918 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3920 * @brief Determines whether the elements of a sequence are sorted.
3921 * @ingroup sorting_algorithms
3922 * @param __first An iterator.
3923 * @param __last Another iterator.
3924 * @return True if the elements are sorted, false otherwise.
3926 template<typename _ForwardIterator
>
3928 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3929 { return std::is_sorted_until(__first
, __last
) == __last
; }
3932 * @brief Determines whether the elements of a sequence are sorted
3933 * according to a comparison functor.
3934 * @ingroup sorting_algorithms
3935 * @param __first An iterator.
3936 * @param __last Another iterator.
3937 * @param __comp A comparison functor.
3938 * @return True if the elements are sorted, false otherwise.
3940 template<typename _ForwardIterator
, typename _Compare
>
3942 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3944 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3947 * @brief Determines the end of a sorted sequence.
3948 * @ingroup sorting_algorithms
3949 * @param __first An iterator.
3950 * @param __last Another iterator.
3951 * @return An iterator pointing to the last iterator i in [__first, __last)
3952 * for which the range [__first, i) is sorted.
3954 template<typename _ForwardIterator
>
3956 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3958 // concept requirements
3959 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3960 __glibcxx_function_requires(_LessThanComparableConcept
<
3961 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3962 __glibcxx_requires_valid_range(__first
, __last
);
3964 if (__first
== __last
)
3967 _ForwardIterator __next
= __first
;
3968 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3969 if (*__next
< *__first
)
3975 * @brief Determines the end of a sorted sequence using comparison functor.
3976 * @ingroup sorting_algorithms
3977 * @param __first An iterator.
3978 * @param __last Another iterator.
3979 * @param __comp A comparison functor.
3980 * @return An iterator pointing to the last iterator i in [__first, __last)
3981 * for which the range [__first, i) is sorted.
3983 template<typename _ForwardIterator
, typename _Compare
>
3985 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3988 // concept requirements
3989 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3990 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3991 typename iterator_traits
<_ForwardIterator
>::value_type
,
3992 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3993 __glibcxx_requires_valid_range(__first
, __last
);
3995 if (__first
== __last
)
3998 _ForwardIterator __next
= __first
;
3999 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
4000 if (__comp(*__next
, *__first
))
4006 * @brief Determines min and max at once as an ordered pair.
4007 * @ingroup sorting_algorithms
4008 * @param __a A thing of arbitrary type.
4009 * @param __b Another thing of arbitrary type.
4010 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4013 template<typename _Tp
>
4014 inline pair
<const _Tp
&, const _Tp
&>
4015 minmax(const _Tp
& __a
, const _Tp
& __b
)
4017 // concept requirements
4018 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
4020 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4021 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4025 * @brief Determines min and max at once as an ordered pair.
4026 * @ingroup sorting_algorithms
4027 * @param __a A thing of arbitrary type.
4028 * @param __b Another thing of arbitrary type.
4029 * @param __comp A @link comparison_functor comparison functor @endlink.
4030 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4033 template<typename _Tp
, typename _Compare
>
4034 inline pair
<const _Tp
&, const _Tp
&>
4035 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
4037 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4038 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4042 * @brief Return a pair of iterators pointing to the minimum and maximum
4043 * elements in a range.
4044 * @ingroup sorting_algorithms
4045 * @param __first Start of range.
4046 * @param __last End of range.
4047 * @return make_pair(m, M), where m is the first iterator i in
4048 * [__first, __last) such that no other element in the range is
4049 * smaller, and where M is the last iterator i in [__first, __last)
4050 * such that no other element in the range is larger.
4052 template<typename _ForwardIterator
>
4053 pair
<_ForwardIterator
, _ForwardIterator
>
4054 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4056 // concept requirements
4057 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4058 __glibcxx_function_requires(_LessThanComparableConcept
<
4059 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4060 __glibcxx_requires_valid_range(__first
, __last
);
4062 _ForwardIterator __next
= __first
;
4063 if (__first
== __last
4064 || ++__next
== __last
)
4065 return std::make_pair(__first
, __first
);
4067 _ForwardIterator __min
, __max
;
4068 if (*__next
< *__first
)
4082 while (__first
!= __last
)
4085 if (++__next
== __last
)
4087 if (*__first
< *__min
)
4089 else if (!(*__first
< *__max
))
4094 if (*__next
< *__first
)
4096 if (*__next
< *__min
)
4098 if (!(*__first
< *__max
))
4103 if (*__first
< *__min
)
4105 if (!(*__next
< *__max
))
4113 return std::make_pair(__min
, __max
);
4117 * @brief Return a pair of iterators pointing to the minimum and maximum
4118 * elements in a range.
4119 * @ingroup sorting_algorithms
4120 * @param __first Start of range.
4121 * @param __last End of range.
4122 * @param __comp Comparison functor.
4123 * @return make_pair(m, M), where m is the first iterator i in
4124 * [__first, __last) such that no other element in the range is
4125 * smaller, and where M is the last iterator i in [__first, __last)
4126 * such that no other element in the range is larger.
4128 template<typename _ForwardIterator
, typename _Compare
>
4129 pair
<_ForwardIterator
, _ForwardIterator
>
4130 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4133 // concept requirements
4134 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4135 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4136 typename iterator_traits
<_ForwardIterator
>::value_type
,
4137 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4138 __glibcxx_requires_valid_range(__first
, __last
);
4140 _ForwardIterator __next
= __first
;
4141 if (__first
== __last
4142 || ++__next
== __last
)
4143 return std::make_pair(__first
, __first
);
4145 _ForwardIterator __min
, __max
;
4146 if (__comp(*__next
, *__first
))
4160 while (__first
!= __last
)
4163 if (++__next
== __last
)
4165 if (__comp(*__first
, *__min
))
4167 else if (!__comp(*__first
, *__max
))
4172 if (__comp(*__next
, *__first
))
4174 if (__comp(*__next
, *__min
))
4176 if (!__comp(*__first
, *__max
))
4181 if (__comp(*__first
, *__min
))
4183 if (!__comp(*__next
, *__max
))
4191 return std::make_pair(__min
, __max
);
4195 template<typename _Tp
>
4197 min(initializer_list
<_Tp
> __l
)
4198 { return *std::min_element(__l
.begin(), __l
.end()); }
4200 template<typename _Tp
, typename _Compare
>
4202 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4203 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4205 template<typename _Tp
>
4207 max(initializer_list
<_Tp
> __l
)
4208 { return *std::max_element(__l
.begin(), __l
.end()); }
4210 template<typename _Tp
, typename _Compare
>
4212 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4213 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4215 template<typename _Tp
>
4216 inline pair
<_Tp
, _Tp
>
4217 minmax(initializer_list
<_Tp
> __l
)
4219 pair
<const _Tp
*, const _Tp
*> __p
=
4220 std::minmax_element(__l
.begin(), __l
.end());
4221 return std::make_pair(*__p
.first
, *__p
.second
);
4224 template<typename _Tp
, typename _Compare
>
4225 inline pair
<_Tp
, _Tp
>
4226 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4228 pair
<const _Tp
*, const _Tp
*> __p
=
4229 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4230 return std::make_pair(*__p
.first
, *__p
.second
);
4234 * @brief Checks whether a permutaion of the second sequence is equal
4235 * to the first sequence.
4236 * @ingroup non_mutating_algorithms
4237 * @param __first1 Start of first range.
4238 * @param __last1 End of first range.
4239 * @param __first2 Start of second range.
4240 * @return true if there exists a permutation of the elements in the range
4241 * [__first2, __first2 + (__last1 - __first1)), beginning with
4242 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
4243 * returns true; otherwise, returns false.
4245 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4247 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4248 _ForwardIterator2 __first2
)
4250 // Efficiently compare identical prefixes: O(N) if sequences
4251 // have the same elements in the same order.
4252 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4253 if (!(*__first1
== *__first2
))
4256 if (__first1
== __last1
)
4259 // Establish __last2 assuming equal ranges by iterating over the
4260 // rest of the list.
4261 _ForwardIterator2 __last2
= __first2
;
4262 std::advance(__last2
, std::distance(__first1
, __last1
));
4263 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4265 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4266 continue; // We've seen this one before.
4268 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4270 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4277 * @brief Checks whether a permutation of the second sequence is equal
4278 * to the first sequence.
4279 * @ingroup non_mutating_algorithms
4280 * @param __first1 Start of first range.
4281 * @param __last1 End of first range.
4282 * @param __first2 Start of second range.
4283 * @param __pred A binary predicate.
4284 * @return true if there exists a permutation of the elements in
4285 * the range [__first2, __first2 + (__last1 - __first1)),
4286 * beginning with ForwardIterator2 begin, such that
4287 * equal(__first1, __last1, __begin, __pred) returns true;
4288 * otherwise, returns false.
4290 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4291 typename _BinaryPredicate
>
4293 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4294 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
4296 // Efficiently compare identical prefixes: O(N) if sequences
4297 // have the same elements in the same order.
4298 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4299 if (!bool(__pred(*__first1
, *__first2
)))
4302 if (__first1
== __last1
)
4305 // Establish __last2 assuming equal ranges by iterating over the
4306 // rest of the list.
4307 _ForwardIterator2 __last2
= __first2
;
4308 std::advance(__last2
, std::distance(__first1
, __last1
));
4309 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4311 using std::placeholders::_1
;
4313 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4314 std::bind(__pred
, _1
, *__scan
)))
4315 continue; // We've seen this one before.
4317 auto __matches
= std::count_if(__first2
, __last2
,
4318 std::bind(__pred
, _1
, *__scan
));
4320 || std::count_if(__scan
, __last1
,
4321 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4327 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4329 * @brief Shuffle the elements of a sequence using a uniform random
4331 * @ingroup mutating_algorithms
4332 * @param __first A forward iterator.
4333 * @param __last A forward iterator.
4334 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
4337 * Reorders the elements in the range @p [__first,__last) using @p __g to
4338 * provide random numbers.
4340 template<typename _RandomAccessIterator
,
4341 typename _UniformRandomNumberGenerator
>
4343 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4344 _UniformRandomNumberGenerator
&& __g
)
4346 // concept requirements
4347 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4348 _RandomAccessIterator
>)
4349 __glibcxx_requires_valid_range(__first
, __last
);
4351 if (__first
== __last
)
4354 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4357 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
4358 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
4359 typedef typename
__distr_type::param_type __p_type
;
4362 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4363 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
4367 #endif // __GXX_EXPERIMENTAL_CXX0X__
4369 _GLIBCXX_END_NAMESPACE_VERSION
4371 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4374 * @brief Apply a function to every element of a sequence.
4375 * @ingroup non_mutating_algorithms
4376 * @param __first An input iterator.
4377 * @param __last An input iterator.
4378 * @param __f A unary function object.
4379 * @return @p __f (std::move(@p __f) in C++0x).
4381 * Applies the function object @p __f to each element in the range
4382 * @p [first,last). @p __f must not modify the order of the sequence.
4383 * If @p __f has a return value it is ignored.
4385 template<typename _InputIterator
, typename _Function
>
4387 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4389 // concept requirements
4390 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4391 __glibcxx_requires_valid_range(__first
, __last
);
4392 for (; __first
!= __last
; ++__first
)
4394 return _GLIBCXX_MOVE(__f
);
4398 * @brief Find the first occurrence of a value in a sequence.
4399 * @ingroup non_mutating_algorithms
4400 * @param __first An input iterator.
4401 * @param __last An input iterator.
4402 * @param __val The value to find.
4403 * @return The first iterator @c i in the range @p [__first,__last)
4404 * such that @c *i == @p __val, or @p __last if no such iterator exists.
4406 template<typename _InputIterator
, typename _Tp
>
4407 inline _InputIterator
4408 find(_InputIterator __first
, _InputIterator __last
,
4411 // concept requirements
4412 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4413 __glibcxx_function_requires(_EqualOpConcept
<
4414 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4415 __glibcxx_requires_valid_range(__first
, __last
);
4416 return std::__find(__first
, __last
, __val
,
4417 std::__iterator_category(__first
));
4421 * @brief Find the first element in a sequence for which a
4422 * predicate is true.
4423 * @ingroup non_mutating_algorithms
4424 * @param __first An input iterator.
4425 * @param __last An input iterator.
4426 * @param __pred A predicate.
4427 * @return The first iterator @c i in the range @p [__first,__last)
4428 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
4430 template<typename _InputIterator
, typename _Predicate
>
4431 inline _InputIterator
4432 find_if(_InputIterator __first
, _InputIterator __last
,
4435 // concept requirements
4436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4437 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4438 typename iterator_traits
<_InputIterator
>::value_type
>)
4439 __glibcxx_requires_valid_range(__first
, __last
);
4440 return std::__find_if(__first
, __last
, __pred
,
4441 std::__iterator_category(__first
));
4445 * @brief Find element from a set in a sequence.
4446 * @ingroup non_mutating_algorithms
4447 * @param __first1 Start of range to search.
4448 * @param __last1 End of range to search.
4449 * @param __first2 Start of match candidates.
4450 * @param __last2 End of match candidates.
4451 * @return The first iterator @c i in the range
4452 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
4453 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
4455 * Searches the range @p [__first1,__last1) for an element that is
4456 * equal to some element in the range [__first2,__last2). If
4457 * found, returns an iterator in the range [__first1,__last1),
4458 * otherwise returns @p __last1.
4460 template<typename _InputIterator
, typename _ForwardIterator
>
4462 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4463 _ForwardIterator __first2
, _ForwardIterator __last2
)
4465 // concept requirements
4466 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4467 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4468 __glibcxx_function_requires(_EqualOpConcept
<
4469 typename iterator_traits
<_InputIterator
>::value_type
,
4470 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4471 __glibcxx_requires_valid_range(__first1
, __last1
);
4472 __glibcxx_requires_valid_range(__first2
, __last2
);
4474 for (; __first1
!= __last1
; ++__first1
)
4475 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4476 if (*__first1
== *__iter
)
4482 * @brief Find element from a set in a sequence using a predicate.
4483 * @ingroup non_mutating_algorithms
4484 * @param __first1 Start of range to search.
4485 * @param __last1 End of range to search.
4486 * @param __first2 Start of match candidates.
4487 * @param __last2 End of match candidates.
4488 * @param __comp Predicate to use.
4489 * @return The first iterator @c i in the range
4490 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4491 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4492 * such iterator exists.
4495 * Searches the range @p [__first1,__last1) for an element that is
4496 * equal to some element in the range [__first2,__last2). If
4497 * found, returns an iterator in the range [__first1,__last1),
4498 * otherwise returns @p __last1.
4500 template<typename _InputIterator
, typename _ForwardIterator
,
4501 typename _BinaryPredicate
>
4503 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4504 _ForwardIterator __first2
, _ForwardIterator __last2
,
4505 _BinaryPredicate __comp
)
4507 // concept requirements
4508 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4509 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4510 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4511 typename iterator_traits
<_InputIterator
>::value_type
,
4512 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4513 __glibcxx_requires_valid_range(__first1
, __last1
);
4514 __glibcxx_requires_valid_range(__first2
, __last2
);
4516 for (; __first1
!= __last1
; ++__first1
)
4517 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4518 if (__comp(*__first1
, *__iter
))
4524 * @brief Find two adjacent values in a sequence that are equal.
4525 * @ingroup non_mutating_algorithms
4526 * @param __first A forward iterator.
4527 * @param __last A forward iterator.
4528 * @return The first iterator @c i such that @c i and @c i+1 are both
4529 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4530 * or @p __last if no such iterator exists.
4532 template<typename _ForwardIterator
>
4534 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4536 // concept requirements
4537 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4538 __glibcxx_function_requires(_EqualityComparableConcept
<
4539 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4540 __glibcxx_requires_valid_range(__first
, __last
);
4541 if (__first
== __last
)
4543 _ForwardIterator __next
= __first
;
4544 while(++__next
!= __last
)
4546 if (*__first
== *__next
)
4554 * @brief Find two adjacent values in a sequence using a predicate.
4555 * @ingroup non_mutating_algorithms
4556 * @param __first A forward iterator.
4557 * @param __last A forward iterator.
4558 * @param __binary_pred A binary predicate.
4559 * @return The first iterator @c i such that @c i and @c i+1 are both
4560 * valid iterators in @p [__first,__last) and such that
4561 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4564 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4566 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4567 _BinaryPredicate __binary_pred
)
4569 // concept requirements
4570 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4571 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4572 typename iterator_traits
<_ForwardIterator
>::value_type
,
4573 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4574 __glibcxx_requires_valid_range(__first
, __last
);
4575 if (__first
== __last
)
4577 _ForwardIterator __next
= __first
;
4578 while(++__next
!= __last
)
4580 if (__binary_pred(*__first
, *__next
))
4588 * @brief Count the number of copies of a value in a sequence.
4589 * @ingroup non_mutating_algorithms
4590 * @param __first An input iterator.
4591 * @param __last An input iterator.
4592 * @param __value The value to be counted.
4593 * @return The number of iterators @c i in the range @p [__first,__last)
4594 * for which @c *i == @p __value
4596 template<typename _InputIterator
, typename _Tp
>
4597 typename iterator_traits
<_InputIterator
>::difference_type
4598 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4600 // concept requirements
4601 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4602 __glibcxx_function_requires(_EqualOpConcept
<
4603 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4604 __glibcxx_requires_valid_range(__first
, __last
);
4605 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4606 for (; __first
!= __last
; ++__first
)
4607 if (*__first
== __value
)
4613 * @brief Count the elements of a sequence for which a predicate is true.
4614 * @ingroup non_mutating_algorithms
4615 * @param __first An input iterator.
4616 * @param __last An input iterator.
4617 * @param __pred A predicate.
4618 * @return The number of iterators @c i in the range @p [__first,__last)
4619 * for which @p __pred(*i) is true.
4621 template<typename _InputIterator
, typename _Predicate
>
4622 typename iterator_traits
<_InputIterator
>::difference_type
4623 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4625 // concept requirements
4626 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4627 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4628 typename iterator_traits
<_InputIterator
>::value_type
>)
4629 __glibcxx_requires_valid_range(__first
, __last
);
4630 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4631 for (; __first
!= __last
; ++__first
)
4632 if (__pred(*__first
))
4638 * @brief Search a sequence for a matching sub-sequence.
4639 * @ingroup non_mutating_algorithms
4640 * @param __first1 A forward iterator.
4641 * @param __last1 A forward iterator.
4642 * @param __first2 A forward iterator.
4643 * @param __last2 A forward iterator.
4644 * @return The first iterator @c i in the range @p
4645 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4646 * *(__first2+N) for each @c N in the range @p
4647 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4649 * Searches the range @p [__first1,__last1) for a sub-sequence that
4650 * compares equal value-by-value with the sequence given by @p
4651 * [__first2,__last2) and returns an iterator to the first element
4652 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4655 * Because the sub-sequence must lie completely within the range @p
4656 * [__first1,__last1) it must start at a position less than @p
4657 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4658 * length of the sub-sequence.
4660 * This means that the returned iterator @c i will be in the range
4661 * @p [__first1,__last1-(__last2-__first2))
4663 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4665 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4666 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4668 // concept requirements
4669 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4670 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4671 __glibcxx_function_requires(_EqualOpConcept
<
4672 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4673 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4674 __glibcxx_requires_valid_range(__first1
, __last1
);
4675 __glibcxx_requires_valid_range(__first2
, __last2
);
4677 // Test for empty ranges
4678 if (__first1
== __last1
|| __first2
== __last2
)
4681 // Test for a pattern of length 1.
4682 _ForwardIterator2
__p1(__first2
);
4683 if (++__p1
== __last2
)
4684 return _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4687 _ForwardIterator2 __p
;
4688 _ForwardIterator1 __current
= __first1
;
4692 __first1
= _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4693 if (__first1
== __last1
)
4697 __current
= __first1
;
4698 if (++__current
== __last1
)
4701 while (*__current
== *__p
)
4703 if (++__p
== __last2
)
4705 if (++__current
== __last1
)
4714 * @brief Search a sequence for a matching sub-sequence using a predicate.
4715 * @ingroup non_mutating_algorithms
4716 * @param __first1 A forward iterator.
4717 * @param __last1 A forward iterator.
4718 * @param __first2 A forward iterator.
4719 * @param __last2 A forward iterator.
4720 * @param __predicate A binary predicate.
4721 * @return The first iterator @c i in the range
4722 * @p [__first1,__last1-(__last2-__first2)) such that
4723 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4724 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4726 * Searches the range @p [__first1,__last1) for a sub-sequence that
4727 * compares equal value-by-value with the sequence given by @p
4728 * [__first2,__last2), using @p __predicate to determine equality,
4729 * and returns an iterator to the first element of the
4730 * sub-sequence, or @p __last1 if no such iterator exists.
4732 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4734 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4735 typename _BinaryPredicate
>
4737 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4738 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4739 _BinaryPredicate __predicate
)
4741 // concept requirements
4742 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4743 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4744 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4745 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4746 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4747 __glibcxx_requires_valid_range(__first1
, __last1
);
4748 __glibcxx_requires_valid_range(__first2
, __last2
);
4750 // Test for empty ranges
4751 if (__first1
== __last1
|| __first2
== __last2
)
4754 // Test for a pattern of length 1.
4755 _ForwardIterator2
__p1(__first2
);
4756 if (++__p1
== __last2
)
4758 while (__first1
!= __last1
4759 && !bool(__predicate(*__first1
, *__first2
)))
4765 _ForwardIterator2 __p
;
4766 _ForwardIterator1 __current
= __first1
;
4770 while (__first1
!= __last1
4771 && !bool(__predicate(*__first1
, *__first2
)))
4773 if (__first1
== __last1
)
4777 __current
= __first1
;
4778 if (++__current
== __last1
)
4781 while (__predicate(*__current
, *__p
))
4783 if (++__p
== __last2
)
4785 if (++__current
== __last1
)
4795 * @brief Search a sequence for a number of consecutive values.
4796 * @ingroup non_mutating_algorithms
4797 * @param __first A forward iterator.
4798 * @param __last A forward iterator.
4799 * @param __count The number of consecutive values.
4800 * @param __val The value to find.
4801 * @return The first iterator @c i in the range @p
4802 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4803 * each @c N in the range @p [0,__count), or @p __last if no such
4806 * Searches the range @p [__first,__last) for @p count consecutive elements
4807 * equal to @p __val.
4809 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4811 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4812 _Integer __count
, const _Tp
& __val
)
4814 // concept requirements
4815 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4816 __glibcxx_function_requires(_EqualOpConcept
<
4817 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4818 __glibcxx_requires_valid_range(__first
, __last
);
4823 return _GLIBCXX_STD_A::find(__first
, __last
, __val
);
4824 return std::__search_n(__first
, __last
, __count
, __val
,
4825 std::__iterator_category(__first
));
4830 * @brief Search a sequence for a number of consecutive values using a
4832 * @ingroup non_mutating_algorithms
4833 * @param __first A forward iterator.
4834 * @param __last A forward iterator.
4835 * @param __count The number of consecutive values.
4836 * @param __val The value to find.
4837 * @param __binary_pred A binary predicate.
4838 * @return The first iterator @c i in the range @p
4839 * [__first,__last-__count) such that @p
4840 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4841 * @p [0,__count), or @p __last if no such iterator exists.
4843 * Searches the range @p [__first,__last) for @p __count
4844 * consecutive elements for which the predicate returns true.
4846 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4847 typename _BinaryPredicate
>
4849 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4850 _Integer __count
, const _Tp
& __val
,
4851 _BinaryPredicate __binary_pred
)
4853 // concept requirements
4854 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4855 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4856 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4857 __glibcxx_requires_valid_range(__first
, __last
);
4863 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4867 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4868 std::__iterator_category(__first
));
4873 * @brief Perform an operation on a sequence.
4874 * @ingroup mutating_algorithms
4875 * @param __first An input iterator.
4876 * @param __last An input iterator.
4877 * @param __result An output iterator.
4878 * @param __unary_op A unary operator.
4879 * @return An output iterator equal to @p __result+(__last-__first).
4881 * Applies the operator to each element in the input range and assigns
4882 * the results to successive elements of the output sequence.
4883 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4884 * range @p [0,__last-__first).
4886 * @p unary_op must not alter its argument.
4888 template<typename _InputIterator
, typename _OutputIterator
,
4889 typename _UnaryOperation
>
4891 transform(_InputIterator __first
, _InputIterator __last
,
4892 _OutputIterator __result
, _UnaryOperation __unary_op
)
4894 // concept requirements
4895 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4896 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4897 // "the type returned by a _UnaryOperation"
4898 __typeof__(__unary_op(*__first
))>)
4899 __glibcxx_requires_valid_range(__first
, __last
);
4901 for (; __first
!= __last
; ++__first
, ++__result
)
4902 *__result
= __unary_op(*__first
);
4907 * @brief Perform an operation on corresponding elements of two sequences.
4908 * @ingroup mutating_algorithms
4909 * @param __first1 An input iterator.
4910 * @param __last1 An input iterator.
4911 * @param __first2 An input iterator.
4912 * @param __result An output iterator.
4913 * @param __binary_op A binary operator.
4914 * @return An output iterator equal to @p result+(last-first).
4916 * Applies the operator to the corresponding elements in the two
4917 * input ranges and assigns the results to successive elements of the
4920 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4921 * @c N in the range @p [0,__last1-__first1).
4923 * @p binary_op must not alter either of its arguments.
4925 template<typename _InputIterator1
, typename _InputIterator2
,
4926 typename _OutputIterator
, typename _BinaryOperation
>
4928 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4929 _InputIterator2 __first2
, _OutputIterator __result
,
4930 _BinaryOperation __binary_op
)
4932 // concept requirements
4933 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4934 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4935 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4936 // "the type returned by a _BinaryOperation"
4937 __typeof__(__binary_op(*__first1
,*__first2
))>)
4938 __glibcxx_requires_valid_range(__first1
, __last1
);
4940 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4941 *__result
= __binary_op(*__first1
, *__first2
);
4946 * @brief Replace each occurrence of one value in a sequence with another
4948 * @ingroup mutating_algorithms
4949 * @param __first A forward iterator.
4950 * @param __last A forward iterator.
4951 * @param __old_value The value to be replaced.
4952 * @param __new_value The replacement value.
4953 * @return replace() returns no value.
4955 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4956 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4958 template<typename _ForwardIterator
, typename _Tp
>
4960 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4961 const _Tp
& __old_value
, const _Tp
& __new_value
)
4963 // concept requirements
4964 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4966 __glibcxx_function_requires(_EqualOpConcept
<
4967 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4968 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4969 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4970 __glibcxx_requires_valid_range(__first
, __last
);
4972 for (; __first
!= __last
; ++__first
)
4973 if (*__first
== __old_value
)
4974 *__first
= __new_value
;
4978 * @brief Replace each value in a sequence for which a predicate returns
4979 * true with another value.
4980 * @ingroup mutating_algorithms
4981 * @param __first A forward iterator.
4982 * @param __last A forward iterator.
4983 * @param __pred A predicate.
4984 * @param __new_value The replacement value.
4985 * @return replace_if() returns no value.
4987 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4988 * is true then the assignment @c *i = @p __new_value is performed.
4990 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4992 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4993 _Predicate __pred
, const _Tp
& __new_value
)
4995 // concept requirements
4996 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4998 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4999 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5000 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5001 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5002 __glibcxx_requires_valid_range(__first
, __last
);
5004 for (; __first
!= __last
; ++__first
)
5005 if (__pred(*__first
))
5006 *__first
= __new_value
;
5010 * @brief Assign the result of a function object to each value in a
5012 * @ingroup mutating_algorithms
5013 * @param __first A forward iterator.
5014 * @param __last A forward iterator.
5015 * @param __gen A function object taking no arguments and returning
5016 * std::iterator_traits<_ForwardIterator>::value_type
5017 * @return generate() returns no value.
5019 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5020 * @p [__first,__last).
5022 template<typename _ForwardIterator
, typename _Generator
>
5024 generate(_ForwardIterator __first
, _ForwardIterator __last
,
5027 // concept requirements
5028 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5029 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
5030 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5031 __glibcxx_requires_valid_range(__first
, __last
);
5033 for (; __first
!= __last
; ++__first
)
5038 * @brief Assign the result of a function object to each value in a
5040 * @ingroup mutating_algorithms
5041 * @param __first A forward iterator.
5042 * @param __n The length of the sequence.
5043 * @param __gen A function object taking no arguments and returning
5044 * std::iterator_traits<_ForwardIterator>::value_type
5045 * @return The end of the sequence, @p __first+__n
5047 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5048 * @p [__first,__first+__n).
5050 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5051 * DR 865. More algorithms that throw away information
5053 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
5055 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
5057 // concept requirements
5058 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5059 // "the type returned by a _Generator"
5060 __typeof__(__gen())>)
5062 for (__decltype(__n
+ 0) __niter
= __n
;
5063 __niter
> 0; --__niter
, ++__first
)
5070 * @brief Copy a sequence, removing consecutive duplicate values.
5071 * @ingroup mutating_algorithms
5072 * @param __first An input iterator.
5073 * @param __last An input iterator.
5074 * @param __result An output iterator.
5075 * @return An iterator designating the end of the resulting sequence.
5077 * Copies each element in the range @p [__first,__last) to the range
5078 * beginning at @p __result, except that only the first element is copied
5079 * from groups of consecutive elements that compare equal.
5080 * unique_copy() is stable, so the relative order of elements that are
5081 * copied is unchanged.
5083 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5084 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5086 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5087 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
5090 template<typename _InputIterator
, typename _OutputIterator
>
5091 inline _OutputIterator
5092 unique_copy(_InputIterator __first
, _InputIterator __last
,
5093 _OutputIterator __result
)
5095 // concept requirements
5096 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5097 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5098 typename iterator_traits
<_InputIterator
>::value_type
>)
5099 __glibcxx_function_requires(_EqualityComparableConcept
<
5100 typename iterator_traits
<_InputIterator
>::value_type
>)
5101 __glibcxx_requires_valid_range(__first
, __last
);
5103 if (__first
== __last
)
5105 return std::__unique_copy(__first
, __last
, __result
,
5106 std::__iterator_category(__first
),
5107 std::__iterator_category(__result
));
5111 * @brief Copy a sequence, removing consecutive values using a predicate.
5112 * @ingroup mutating_algorithms
5113 * @param __first An input iterator.
5114 * @param __last An input iterator.
5115 * @param __result An output iterator.
5116 * @param __binary_pred A binary predicate.
5117 * @return An iterator designating the end of the resulting sequence.
5119 * Copies each element in the range @p [__first,__last) to the range
5120 * beginning at @p __result, except that only the first element is copied
5121 * from groups of consecutive elements for which @p __binary_pred returns
5123 * unique_copy() is stable, so the relative order of elements that are
5124 * copied is unchanged.
5126 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5127 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5129 template<typename _InputIterator
, typename _OutputIterator
,
5130 typename _BinaryPredicate
>
5131 inline _OutputIterator
5132 unique_copy(_InputIterator __first
, _InputIterator __last
,
5133 _OutputIterator __result
,
5134 _BinaryPredicate __binary_pred
)
5136 // concept requirements -- predicates checked later
5137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5138 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5139 typename iterator_traits
<_InputIterator
>::value_type
>)
5140 __glibcxx_requires_valid_range(__first
, __last
);
5142 if (__first
== __last
)
5144 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
5145 std::__iterator_category(__first
),
5146 std::__iterator_category(__result
));
5151 * @brief Randomly shuffle the elements of a sequence.
5152 * @ingroup mutating_algorithms
5153 * @param __first A forward iterator.
5154 * @param __last A forward iterator.
5157 * Reorder the elements in the range @p [__first,__last) using a random
5158 * distribution, so that every possible ordering of the sequence is
5161 template<typename _RandomAccessIterator
>
5163 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5165 // concept requirements
5166 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5167 _RandomAccessIterator
>)
5168 __glibcxx_requires_valid_range(__first
, __last
);
5170 if (__first
!= __last
)
5171 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5172 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
5176 * @brief Shuffle the elements of a sequence using a random number
5178 * @ingroup mutating_algorithms
5179 * @param __first A forward iterator.
5180 * @param __last A forward iterator.
5181 * @param __rand The RNG functor or function.
5184 * Reorders the elements in the range @p [__first,__last) using @p __rand to
5185 * provide a random distribution. Calling @p __rand(N) for a positive
5186 * integer @p N should return a randomly chosen integer from the
5189 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
5191 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5192 #ifdef __GXX_EXPERIMENTAL_CXX0X__
5193 _RandomNumberGenerator
&& __rand
)
5195 _RandomNumberGenerator
& __rand
)
5198 // concept requirements
5199 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5200 _RandomAccessIterator
>)
5201 __glibcxx_requires_valid_range(__first
, __last
);
5203 if (__first
== __last
)
5205 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5206 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5211 * @brief Move elements for which a predicate is true to the beginning
5213 * @ingroup mutating_algorithms
5214 * @param __first A forward iterator.
5215 * @param __last A forward iterator.
5216 * @param __pred A predicate functor.
5217 * @return An iterator @p middle such that @p __pred(i) is true for each
5218 * iterator @p i in the range @p [__first,middle) and false for each @p i
5219 * in the range @p [middle,__last).
5221 * @p __pred must not modify its operand. @p partition() does not preserve
5222 * the relative ordering of elements in each group, use
5223 * @p stable_partition() if this is needed.
5225 template<typename _ForwardIterator
, typename _Predicate
>
5226 inline _ForwardIterator
5227 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5230 // concept requirements
5231 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5233 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5234 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5235 __glibcxx_requires_valid_range(__first
, __last
);
5237 return std::__partition(__first
, __last
, __pred
,
5238 std::__iterator_category(__first
));
5244 * @brief Sort the smallest elements of a sequence.
5245 * @ingroup sorting_algorithms
5246 * @param __first An iterator.
5247 * @param __middle Another iterator.
5248 * @param __last Another iterator.
5251 * Sorts the smallest @p (__middle-__first) elements in the range
5252 * @p [first,last) and moves them to the range @p [__first,__middle). The
5253 * order of the remaining elements in the range @p [__middle,__last) is
5255 * After the sort if @p i and @j are iterators in the range
5256 * @p [__first,__middle) such that @i precedes @j and @k is an iterator in
5257 * the range @p [__middle,__last) then @p *j<*i and @p *k<*i are both false.
5259 template<typename _RandomAccessIterator
>
5261 partial_sort(_RandomAccessIterator __first
,
5262 _RandomAccessIterator __middle
,
5263 _RandomAccessIterator __last
)
5265 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5268 // concept requirements
5269 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5270 _RandomAccessIterator
>)
5271 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5272 __glibcxx_requires_valid_range(__first
, __middle
);
5273 __glibcxx_requires_valid_range(__middle
, __last
);
5275 std::__heap_select(__first
, __middle
, __last
);
5276 std::sort_heap(__first
, __middle
);
5280 * @brief Sort the smallest elements of a sequence using a predicate
5282 * @ingroup sorting_algorithms
5283 * @param __first An iterator.
5284 * @param __middle Another iterator.
5285 * @param __last Another iterator.
5286 * @param __comp A comparison functor.
5289 * Sorts the smallest @p (__middle-__first) elements in the range
5290 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
5291 * order of the remaining elements in the range @p [__middle,__last) is
5293 * After the sort if @p i and @j are iterators in the range
5294 * @p [__first,__middle) such that @i precedes @j and @k is an iterator in
5295 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
5298 template<typename _RandomAccessIterator
, typename _Compare
>
5300 partial_sort(_RandomAccessIterator __first
,
5301 _RandomAccessIterator __middle
,
5302 _RandomAccessIterator __last
,
5305 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5308 // concept requirements
5309 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5310 _RandomAccessIterator
>)
5311 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5312 _ValueType
, _ValueType
>)
5313 __glibcxx_requires_valid_range(__first
, __middle
);
5314 __glibcxx_requires_valid_range(__middle
, __last
);
5316 std::__heap_select(__first
, __middle
, __last
, __comp
);
5317 std::sort_heap(__first
, __middle
, __comp
);
5321 * @brief Sort a sequence just enough to find a particular position.
5322 * @ingroup sorting_algorithms
5323 * @param __first An iterator.
5324 * @param __nth Another iterator.
5325 * @param __last Another iterator.
5328 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5329 * is the same element that would have been in that position had the
5330 * whole sequence been sorted.
5331 * whole sequence been sorted. The elements either side of @p *__nth are
5332 * not completely sorted, but for any iterator @i in the range
5333 * @p [__first,__nth) and any iterator @j in the range @p [__nth,__last) it
5334 * holds that @p *j<*i is false.
5336 template<typename _RandomAccessIterator
>
5338 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5339 _RandomAccessIterator __last
)
5341 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5344 // concept requirements
5345 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5346 _RandomAccessIterator
>)
5347 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5348 __glibcxx_requires_valid_range(__first
, __nth
);
5349 __glibcxx_requires_valid_range(__nth
, __last
);
5351 if (__first
== __last
|| __nth
== __last
)
5354 std::__introselect(__first
, __nth
, __last
,
5355 std::__lg(__last
- __first
) * 2);
5359 * @brief Sort a sequence just enough to find a particular position
5360 * using a predicate for comparison.
5361 * @ingroup sorting_algorithms
5362 * @param __first An iterator.
5363 * @param __nth Another iterator.
5364 * @param __last Another iterator.
5365 * @param __comp A comparison functor.
5368 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5369 * is the same element that would have been in that position had the
5370 * whole sequence been sorted. The elements either side of @p *__nth are
5371 * not completely sorted, but for any iterator @i in the range
5372 * @p [__first,__nth) and any iterator @j in the range @p [__nth,__last) it
5373 * holds that @p __comp(*j,*i) is false.
5375 template<typename _RandomAccessIterator
, typename _Compare
>
5377 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5378 _RandomAccessIterator __last
, _Compare __comp
)
5380 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5383 // concept requirements
5384 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5385 _RandomAccessIterator
>)
5386 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5387 _ValueType
, _ValueType
>)
5388 __glibcxx_requires_valid_range(__first
, __nth
);
5389 __glibcxx_requires_valid_range(__nth
, __last
);
5391 if (__first
== __last
|| __nth
== __last
)
5394 std::__introselect(__first
, __nth
, __last
,
5395 std::__lg(__last
- __first
) * 2, __comp
);
5400 * @brief Sort the elements of a sequence.
5401 * @ingroup sorting_algorithms
5402 * @param __first An iterator.
5403 * @param __last Another iterator.
5406 * Sorts the elements in the range @p [__first,__last) in ascending order,
5407 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5408 * @p [__first,__last-1).
5410 * The relative ordering of equivalent elements is not preserved, use
5411 * @p stable_sort() if this is needed.
5413 template<typename _RandomAccessIterator
>
5415 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5417 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5420 // concept requirements
5421 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5422 _RandomAccessIterator
>)
5423 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5424 __glibcxx_requires_valid_range(__first
, __last
);
5426 if (__first
!= __last
)
5428 std::__introsort_loop(__first
, __last
,
5429 std::__lg(__last
- __first
) * 2);
5430 std::__final_insertion_sort(__first
, __last
);
5435 * @brief Sort the elements of a sequence using a predicate for comparison.
5436 * @ingroup sorting_algorithms
5437 * @param __first An iterator.
5438 * @param __last Another iterator.
5439 * @param __comp A comparison functor.
5442 * Sorts the elements in the range @p [__first,__last) in ascending order,
5443 * such that @p __comp(*(i+1),*i) is false for every iterator @p i in the
5444 * range @p [__first,__last-1).
5446 * The relative ordering of equivalent elements is not preserved, use
5447 * @p stable_sort() if this is needed.
5449 template<typename _RandomAccessIterator
, typename _Compare
>
5451 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5454 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5457 // concept requirements
5458 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5459 _RandomAccessIterator
>)
5460 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5462 __glibcxx_requires_valid_range(__first
, __last
);
5464 if (__first
!= __last
)
5466 std::__introsort_loop(__first
, __last
,
5467 std::__lg(__last
- __first
) * 2, __comp
);
5468 std::__final_insertion_sort(__first
, __last
, __comp
);
5473 * @brief Merges two sorted ranges.
5474 * @ingroup sorting_algorithms
5475 * @param __first1 An iterator.
5476 * @param __first2 Another iterator.
5477 * @param __last1 Another iterator.
5478 * @param __last2 Another iterator.
5479 * @param __result An iterator pointing to the end of the merged range.
5480 * @return An iterator pointing to the first element <em>not less
5483 * Merges the ranges [__first1,__last1) and [__first2,__last2) into
5484 * the sorted range [__result, __result + (__last1-__first1) +
5485 * (__last2-__first2)). Both input ranges must be sorted, and the
5486 * output range must not overlap with either of the input ranges.
5487 * The sort is @e stable, that is, for equivalent elements in the
5488 * two ranges, elements from the first range will always come
5489 * before elements from the second.
5491 template<typename _InputIterator1
, typename _InputIterator2
,
5492 typename _OutputIterator
>
5494 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5495 _InputIterator2 __first2
, _InputIterator2 __last2
,
5496 _OutputIterator __result
)
5498 typedef typename iterator_traits
<_InputIterator1
>::value_type
5500 typedef typename iterator_traits
<_InputIterator2
>::value_type
5503 // concept requirements
5504 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5505 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5506 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5508 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5510 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5511 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5512 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5514 while (__first1
!= __last1
&& __first2
!= __last2
)
5516 if (*__first2
< *__first1
)
5518 *__result
= *__first2
;
5523 *__result
= *__first1
;
5528 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5533 * @brief Merges two sorted ranges.
5534 * @ingroup sorting_algorithms
5535 * @param __first1 An iterator.
5536 * @param __first2 Another iterator.
5537 * @param __last1 Another iterator.
5538 * @param __last2 Another iterator.
5539 * @param __result An iterator pointing to the end of the merged range.
5540 * @param __comp A functor to use for comparisons.
5541 * @return An iterator pointing to the first element "not less
5544 * Merges the ranges [__first1,__last1) and [__first2,__last2) into
5545 * the sorted range [__result, __result + (__last1-__first1) +
5546 * (__last2-__first2)). Both input ranges must be sorted, and the
5547 * output range must not overlap with either of the input ranges.
5548 * The sort is @e stable, that is, for equivalent elements in the
5549 * two ranges, elements from the first range will always come
5550 * before elements from the second.
5552 * The comparison function should have the same effects on ordering as
5553 * the function used for the initial sort.
5555 template<typename _InputIterator1
, typename _InputIterator2
,
5556 typename _OutputIterator
, typename _Compare
>
5558 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5559 _InputIterator2 __first2
, _InputIterator2 __last2
,
5560 _OutputIterator __result
, _Compare __comp
)
5562 typedef typename iterator_traits
<_InputIterator1
>::value_type
5564 typedef typename iterator_traits
<_InputIterator2
>::value_type
5567 // concept requirements
5568 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5569 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5570 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5572 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5574 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5575 _ValueType2
, _ValueType1
>)
5576 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5577 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5579 while (__first1
!= __last1
&& __first2
!= __last2
)
5581 if (__comp(*__first2
, *__first1
))
5583 *__result
= *__first2
;
5588 *__result
= *__first1
;
5593 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5599 * @brief Sort the elements of a sequence, preserving the relative order
5600 * of equivalent elements.
5601 * @ingroup sorting_algorithms
5602 * @param __first An iterator.
5603 * @param __last Another iterator.
5606 * Sorts the elements in the range @p [__first,__last) in ascending order,
5607 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5608 * @p [__first,__last-1).
5610 * The relative ordering of equivalent elements is preserved, so any two
5611 * elements @p x and @p y in the range @p [__first,__last) such that
5612 * @p x<y is false and @p y<x is false will have the same relative
5613 * ordering after calling @p stable_sort().
5615 template<typename _RandomAccessIterator
>
5617 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5619 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5621 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5624 // concept requirements
5625 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5626 _RandomAccessIterator
>)
5627 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5628 __glibcxx_requires_valid_range(__first
, __last
);
5630 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5632 if (__buf
.begin() == 0)
5633 std::__inplace_stable_sort(__first
, __last
);
5635 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5636 _DistanceType(__buf
.size()));
5640 * @brief Sort the elements of a sequence using a predicate for comparison,
5641 * preserving the relative order of equivalent elements.
5642 * @ingroup sorting_algorithms
5643 * @param __first An iterator.
5644 * @param __last Another iterator.
5645 * @param __comp A comparison functor.
5648 * Sorts the elements in the range @p [__first,__last) in ascending order,
5649 * such that @p __comp(*(i+1),*i) is false for each iterator @p i in the
5650 * range @p [__first,__last-1).
5652 * The relative ordering of equivalent elements is preserved, so any two
5653 * elements @p x and @p y in the range @p [__first,__last) such that
5654 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5655 * relative ordering after calling @p stable_sort().
5657 template<typename _RandomAccessIterator
, typename _Compare
>
5659 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5662 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5664 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5667 // concept requirements
5668 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5669 _RandomAccessIterator
>)
5670 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5673 __glibcxx_requires_valid_range(__first
, __last
);
5675 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5677 if (__buf
.begin() == 0)
5678 std::__inplace_stable_sort(__first
, __last
, __comp
);
5680 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5681 _DistanceType(__buf
.size()), __comp
);
5686 * @brief Return the union of two sorted ranges.
5687 * @ingroup set_algorithms
5688 * @param __first1 Start of first range.
5689 * @param __last1 End of first range.
5690 * @param __first2 Start of second range.
5691 * @param __last2 End of second range.
5692 * @return End of the output range.
5693 * @ingroup set_algorithms
5695 * This operation iterates over both ranges, copying elements present in
5696 * each range in order to the output range. Iterators increment for each
5697 * range. When the current element of one range is less than the other,
5698 * that element is copied and the iterator advanced. If an element is
5699 * contained in both ranges, the element from the first range is copied and
5700 * both ranges advance. The output range may not overlap either input
5703 template<typename _InputIterator1
, typename _InputIterator2
,
5704 typename _OutputIterator
>
5706 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5707 _InputIterator2 __first2
, _InputIterator2 __last2
,
5708 _OutputIterator __result
)
5710 typedef typename iterator_traits
<_InputIterator1
>::value_type
5712 typedef typename iterator_traits
<_InputIterator2
>::value_type
5715 // concept requirements
5716 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5717 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5718 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5720 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5722 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5723 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5724 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5725 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5727 while (__first1
!= __last1
&& __first2
!= __last2
)
5729 if (*__first1
< *__first2
)
5731 *__result
= *__first1
;
5734 else if (*__first2
< *__first1
)
5736 *__result
= *__first2
;
5741 *__result
= *__first1
;
5747 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5752 * @brief Return the union of two sorted ranges using a comparison functor.
5753 * @ingroup set_algorithms
5754 * @param __first1 Start of first range.
5755 * @param __last1 End of first range.
5756 * @param __first2 Start of second range.
5757 * @param __last2 End of second range.
5758 * @param __comp The comparison functor.
5759 * @return End of the output range.
5760 * @ingroup set_algorithms
5762 * This operation iterates over both ranges, copying elements present in
5763 * each range in order to the output range. Iterators increment for each
5764 * range. When the current element of one range is less than the other
5765 * according to @a __comp, that element is copied and the iterator advanced.
5766 * If an equivalent element according to @a __comp is contained in both
5767 * ranges, the element from the first range is copied and both ranges
5768 * advance. The output range may not overlap either input range.
5770 template<typename _InputIterator1
, typename _InputIterator2
,
5771 typename _OutputIterator
, typename _Compare
>
5773 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5774 _InputIterator2 __first2
, _InputIterator2 __last2
,
5775 _OutputIterator __result
, _Compare __comp
)
5777 typedef typename iterator_traits
<_InputIterator1
>::value_type
5779 typedef typename iterator_traits
<_InputIterator2
>::value_type
5782 // concept requirements
5783 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5784 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5785 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5787 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5789 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5790 _ValueType1
, _ValueType2
>)
5791 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5792 _ValueType2
, _ValueType1
>)
5793 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5794 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5796 while (__first1
!= __last1
&& __first2
!= __last2
)
5798 if (__comp(*__first1
, *__first2
))
5800 *__result
= *__first1
;
5803 else if (__comp(*__first2
, *__first1
))
5805 *__result
= *__first2
;
5810 *__result
= *__first1
;
5816 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5821 * @brief Return the intersection of two sorted ranges.
5822 * @ingroup set_algorithms
5823 * @param __first1 Start of first range.
5824 * @param __last1 End of first range.
5825 * @param __first2 Start of second range.
5826 * @param __last2 End of second range.
5827 * @return End of the output range.
5828 * @ingroup set_algorithms
5830 * This operation iterates over both ranges, copying elements present in
5831 * both ranges in order to the output range. Iterators increment for each
5832 * range. When the current element of one range is less than the other,
5833 * that iterator advances. If an element is contained in both ranges, the
5834 * element from the first range is copied and both ranges advance. The
5835 * output range may not overlap either input range.
5837 template<typename _InputIterator1
, typename _InputIterator2
,
5838 typename _OutputIterator
>
5840 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5841 _InputIterator2 __first2
, _InputIterator2 __last2
,
5842 _OutputIterator __result
)
5844 typedef typename iterator_traits
<_InputIterator1
>::value_type
5846 typedef typename iterator_traits
<_InputIterator2
>::value_type
5849 // concept requirements
5850 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5851 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5852 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5854 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5855 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5856 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5857 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5859 while (__first1
!= __last1
&& __first2
!= __last2
)
5860 if (*__first1
< *__first2
)
5862 else if (*__first2
< *__first1
)
5866 *__result
= *__first1
;
5875 * @brief Return the intersection of two sorted ranges using comparison
5877 * @ingroup set_algorithms
5878 * @param __first1 Start of first range.
5879 * @param __last1 End of first range.
5880 * @param __first2 Start of second range.
5881 * @param __last2 End of second range.
5882 * @param __comp The comparison functor.
5883 * @return End of the output range.
5884 * @ingroup set_algorithms
5886 * This operation iterates over both ranges, copying elements present in
5887 * both ranges in order to the output range. Iterators increment for each
5888 * range. When the current element of one range is less than the other
5889 * according to @a __comp, that iterator advances. If an element is
5890 * contained in both ranges according to @a __comp, the element from the
5891 * first range is copied and both ranges advance. The output range may not
5892 * overlap either input range.
5894 template<typename _InputIterator1
, typename _InputIterator2
,
5895 typename _OutputIterator
, typename _Compare
>
5897 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5898 _InputIterator2 __first2
, _InputIterator2 __last2
,
5899 _OutputIterator __result
, _Compare __comp
)
5901 typedef typename iterator_traits
<_InputIterator1
>::value_type
5903 typedef typename iterator_traits
<_InputIterator2
>::value_type
5906 // concept requirements
5907 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5908 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5909 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5911 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5912 _ValueType1
, _ValueType2
>)
5913 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5914 _ValueType2
, _ValueType1
>)
5915 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5916 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5918 while (__first1
!= __last1
&& __first2
!= __last2
)
5919 if (__comp(*__first1
, *__first2
))
5921 else if (__comp(*__first2
, *__first1
))
5925 *__result
= *__first1
;
5934 * @brief Return the difference of two sorted ranges.
5935 * @ingroup set_algorithms
5936 * @param __first1 Start of first range.
5937 * @param __last1 End of first range.
5938 * @param __first2 Start of second range.
5939 * @param __last2 End of second range.
5940 * @return End of the output range.
5941 * @ingroup set_algorithms
5943 * This operation iterates over both ranges, copying elements present in
5944 * the first range but not the second in order to the output range.
5945 * Iterators increment for each range. When the current element of the
5946 * first range is less than the second, that element is copied and the
5947 * iterator advances. If the current element of the second range is less,
5948 * the iterator advances, but no element is copied. If an element is
5949 * contained in both ranges, no elements are copied and both ranges
5950 * advance. The output range may not overlap either input range.
5952 template<typename _InputIterator1
, typename _InputIterator2
,
5953 typename _OutputIterator
>
5955 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5956 _InputIterator2 __first2
, _InputIterator2 __last2
,
5957 _OutputIterator __result
)
5959 typedef typename iterator_traits
<_InputIterator1
>::value_type
5961 typedef typename iterator_traits
<_InputIterator2
>::value_type
5964 // concept requirements
5965 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5966 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5967 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5969 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5970 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5971 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5972 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5974 while (__first1
!= __last1
&& __first2
!= __last2
)
5975 if (*__first1
< *__first2
)
5977 *__result
= *__first1
;
5981 else if (*__first2
< *__first1
)
5988 return std::copy(__first1
, __last1
, __result
);
5992 * @brief Return the difference of two sorted ranges using comparison
5994 * @ingroup set_algorithms
5995 * @param __first1 Start of first range.
5996 * @param __last1 End of first range.
5997 * @param __first2 Start of second range.
5998 * @param __last2 End of second range.
5999 * @param __comp The comparison functor.
6000 * @return End of the output range.
6001 * @ingroup set_algorithms
6003 * This operation iterates over both ranges, copying elements present in
6004 * the first range but not the second in order to the output range.
6005 * Iterators increment for each range. When the current element of the
6006 * first range is less than the second according to @a comp, that element
6007 * is copied and the iterator advances. If the current element of the
6008 * second range is less, no element is copied and the iterator advances.
6009 * If an element is contained in both ranges according to @a comp, no
6010 * elements are copied and both ranges advance. The output range may not
6011 * overlap either input range.
6013 template<typename _InputIterator1
, typename _InputIterator2
,
6014 typename _OutputIterator
, typename _Compare
>
6016 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6017 _InputIterator2 __first2
, _InputIterator2 __last2
,
6018 _OutputIterator __result
, _Compare __comp
)
6020 typedef typename iterator_traits
<_InputIterator1
>::value_type
6022 typedef typename iterator_traits
<_InputIterator2
>::value_type
6025 // concept requirements
6026 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6027 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6028 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6030 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6031 _ValueType1
, _ValueType2
>)
6032 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6033 _ValueType2
, _ValueType1
>)
6034 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6035 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6037 while (__first1
!= __last1
&& __first2
!= __last2
)
6038 if (__comp(*__first1
, *__first2
))
6040 *__result
= *__first1
;
6044 else if (__comp(*__first2
, *__first1
))
6051 return std::copy(__first1
, __last1
, __result
);
6055 * @brief Return the symmetric difference of two sorted ranges.
6056 * @ingroup set_algorithms
6057 * @param __first1 Start of first range.
6058 * @param __last1 End of first range.
6059 * @param __first2 Start of second range.
6060 * @param __last2 End of second range.
6061 * @return End of the output range.
6062 * @ingroup set_algorithms
6064 * This operation iterates over both ranges, copying elements present in
6065 * one range but not the other in order to the output range. Iterators
6066 * increment for each range. When the current element of one range is less
6067 * than the other, that element is copied and the iterator advances. If an
6068 * element is contained in both ranges, no elements are copied and both
6069 * ranges advance. The output range may not overlap either input range.
6071 template<typename _InputIterator1
, typename _InputIterator2
,
6072 typename _OutputIterator
>
6074 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6075 _InputIterator2 __first2
, _InputIterator2 __last2
,
6076 _OutputIterator __result
)
6078 typedef typename iterator_traits
<_InputIterator1
>::value_type
6080 typedef typename iterator_traits
<_InputIterator2
>::value_type
6083 // concept requirements
6084 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6085 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6086 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6088 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6090 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6091 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6092 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6093 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6095 while (__first1
!= __last1
&& __first2
!= __last2
)
6096 if (*__first1
< *__first2
)
6098 *__result
= *__first1
;
6102 else if (*__first2
< *__first1
)
6104 *__result
= *__first2
;
6113 return std::copy(__first2
, __last2
, std::copy(__first1
,
6114 __last1
, __result
));
6118 * @brief Return the symmetric difference of two sorted ranges using
6119 * comparison functor.
6120 * @ingroup set_algorithms
6121 * @param __first1 Start of first range.
6122 * @param __last1 End of first range.
6123 * @param __first2 Start of second range.
6124 * @param __last2 End of second range.
6125 * @param __comp The comparison functor.
6126 * @return End of the output range.
6127 * @ingroup set_algorithms
6129 * This operation iterates over both ranges, copying elements present in
6130 * one range but not the other in order to the output range. Iterators
6131 * increment for each range. When the current element of one range is less
6132 * than the other according to @a comp, that element is copied and the
6133 * iterator advances. If an element is contained in both ranges according
6134 * to @a comp, no elements are copied and both ranges advance. The output
6135 * range may not overlap either input range.
6137 template<typename _InputIterator1
, typename _InputIterator2
,
6138 typename _OutputIterator
, typename _Compare
>
6140 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6141 _InputIterator2 __first2
, _InputIterator2 __last2
,
6142 _OutputIterator __result
,
6145 typedef typename iterator_traits
<_InputIterator1
>::value_type
6147 typedef typename iterator_traits
<_InputIterator2
>::value_type
6150 // concept requirements
6151 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6152 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6153 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6155 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6157 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6158 _ValueType1
, _ValueType2
>)
6159 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6160 _ValueType2
, _ValueType1
>)
6161 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6162 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6164 while (__first1
!= __last1
&& __first2
!= __last2
)
6165 if (__comp(*__first1
, *__first2
))
6167 *__result
= *__first1
;
6171 else if (__comp(*__first2
, *__first1
))
6173 *__result
= *__first2
;
6182 return std::copy(__first2
, __last2
,
6183 std::copy(__first1
, __last1
, __result
));
6188 * @brief Return the minimum element in a range.
6189 * @ingroup sorting_algorithms
6190 * @param __first Start of range.
6191 * @param __last End of range.
6192 * @return Iterator referencing the first instance of the smallest value.
6194 template<typename _ForwardIterator
>
6196 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
6198 // concept requirements
6199 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6200 __glibcxx_function_requires(_LessThanComparableConcept
<
6201 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6202 __glibcxx_requires_valid_range(__first
, __last
);
6204 if (__first
== __last
)
6206 _ForwardIterator __result
= __first
;
6207 while (++__first
!= __last
)
6208 if (*__first
< *__result
)
6214 * @brief Return the minimum element in a range using comparison functor.
6215 * @ingroup sorting_algorithms
6216 * @param __first Start of range.
6217 * @param __last End of range.
6218 * @param __comp Comparison functor.
6219 * @return Iterator referencing the first instance of the smallest value
6220 * according to comp.
6222 template<typename _ForwardIterator
, typename _Compare
>
6224 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6227 // concept requirements
6228 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6229 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6230 typename iterator_traits
<_ForwardIterator
>::value_type
,
6231 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6232 __glibcxx_requires_valid_range(__first
, __last
);
6234 if (__first
== __last
)
6236 _ForwardIterator __result
= __first
;
6237 while (++__first
!= __last
)
6238 if (__comp(*__first
, *__result
))
6244 * @brief Return the maximum element in a range.
6245 * @ingroup sorting_algorithms
6246 * @param __first Start of range.
6247 * @param __last End of range.
6248 * @return Iterator referencing the first instance of the largest value.
6250 template<typename _ForwardIterator
>
6252 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6254 // concept requirements
6255 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6256 __glibcxx_function_requires(_LessThanComparableConcept
<
6257 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6258 __glibcxx_requires_valid_range(__first
, __last
);
6260 if (__first
== __last
)
6262 _ForwardIterator __result
= __first
;
6263 while (++__first
!= __last
)
6264 if (*__result
< *__first
)
6270 * @brief Return the maximum element in a range using comparison functor.
6271 * @ingroup sorting_algorithms
6272 * @param __first Start of range.
6273 * @param __last End of range.
6274 * @param __comp Comparison functor.
6275 * @return Iterator referencing the first instance of the largest value
6276 * according to __comp.
6278 template<typename _ForwardIterator
, typename _Compare
>
6280 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6283 // concept requirements
6284 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6285 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6286 typename iterator_traits
<_ForwardIterator
>::value_type
,
6287 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6288 __glibcxx_requires_valid_range(__first
, __last
);
6290 if (__first
== __last
) return __first
;
6291 _ForwardIterator __result
= __first
;
6292 while (++__first
!= __last
)
6293 if (__comp(*__result
, *__first
))
6298 _GLIBCXX_END_NAMESPACE_ALGO
6301 #endif /* _STL_ALGO_H */