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
)
986 *__result
= *__first
;
997 template<typename _RandomAccessIterator
, typename _Size
,
998 typename _OutputIterator
>
999 inline _OutputIterator
1000 __copy_n(_RandomAccessIterator __first
, _Size __n
,
1001 _OutputIterator __result
, random_access_iterator_tag
)
1002 { return std::copy(__first
, __first
+ __n
, __result
); }
1005 * @brief Copies the range [first,first+n) into [result,result+n).
1006 * @ingroup mutating_algorithms
1007 * @param __first An input iterator.
1008 * @param __n The number of elements to copy.
1009 * @param __result An output iterator.
1012 * This inline function will boil down to a call to @c memmove whenever
1013 * possible. Failing that, if random access iterators are passed, then the
1014 * loop count will be known (and therefore a candidate for compiler
1015 * optimizations such as unrolling).
1017 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1018 inline _OutputIterator
1019 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1021 // concept requirements
1022 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1023 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1024 typename iterator_traits
<_InputIterator
>::value_type
>)
1026 return std::__copy_n(__first
, __n
, __result
,
1027 std::__iterator_category(__first
));
1031 * @brief Copy the elements of a sequence to separate output sequences
1032 * depending on the truth value of a predicate.
1033 * @ingroup mutating_algorithms
1034 * @param __first An input iterator.
1035 * @param __last An input iterator.
1036 * @param __out_true An output iterator.
1037 * @param __out_false An output iterator.
1038 * @param __pred A predicate.
1039 * @return A pair designating the ends of the resulting sequences.
1041 * Copies each element in the range @p [__first,__last) for which
1042 * @p __pred returns true to the range beginning at @p out_true
1043 * and each element for which @p __pred returns false to @p __out_false.
1045 template<typename _InputIterator
, typename _OutputIterator1
,
1046 typename _OutputIterator2
, typename _Predicate
>
1047 pair
<_OutputIterator1
, _OutputIterator2
>
1048 partition_copy(_InputIterator __first
, _InputIterator __last
,
1049 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1052 // concept requirements
1053 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1054 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1055 typename iterator_traits
<_InputIterator
>::value_type
>)
1056 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1057 typename iterator_traits
<_InputIterator
>::value_type
>)
1058 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1059 typename iterator_traits
<_InputIterator
>::value_type
>)
1060 __glibcxx_requires_valid_range(__first
, __last
);
1062 for (; __first
!= __last
; ++__first
)
1063 if (__pred(*__first
))
1065 *__out_true
= *__first
;
1070 *__out_false
= *__first
;
1074 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1079 * @brief Remove elements from a sequence.
1080 * @ingroup mutating_algorithms
1081 * @param __first An input iterator.
1082 * @param __last An input iterator.
1083 * @param __value The value to be removed.
1084 * @return An iterator designating the end of the resulting sequence.
1086 * All elements equal to @p __value are removed from the range
1087 * @p [__first,__last).
1089 * remove() is stable, so the relative order of elements that are
1090 * not removed is unchanged.
1092 * Elements between the end of the resulting sequence and @p __last
1093 * are still present, but their value is unspecified.
1095 template<typename _ForwardIterator
, typename _Tp
>
1097 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1100 // concept requirements
1101 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1103 __glibcxx_function_requires(_EqualOpConcept
<
1104 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1105 __glibcxx_requires_valid_range(__first
, __last
);
1107 __first
= _GLIBCXX_STD_A::find(__first
, __last
, __value
);
1108 if(__first
== __last
)
1110 _ForwardIterator __result
= __first
;
1112 for(; __first
!= __last
; ++__first
)
1113 if(!(*__first
== __value
))
1115 *__result
= _GLIBCXX_MOVE(*__first
);
1122 * @brief Remove elements from a sequence using a predicate.
1123 * @ingroup mutating_algorithms
1124 * @param __first A forward iterator.
1125 * @param __last A forward iterator.
1126 * @param __pred A predicate.
1127 * @return An iterator designating the end of the resulting sequence.
1129 * All elements for which @p __pred returns true are removed from the range
1130 * @p [__first,__last).
1132 * remove_if() is stable, so the relative order of elements that are
1133 * not removed is unchanged.
1135 * Elements between the end of the resulting sequence and @p __last
1136 * are still present, but their value is unspecified.
1138 template<typename _ForwardIterator
, typename _Predicate
>
1140 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1143 // concept requirements
1144 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1146 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1147 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1148 __glibcxx_requires_valid_range(__first
, __last
);
1150 __first
= _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
);
1151 if(__first
== __last
)
1153 _ForwardIterator __result
= __first
;
1155 for(; __first
!= __last
; ++__first
)
1156 if(!bool(__pred(*__first
)))
1158 *__result
= _GLIBCXX_MOVE(*__first
);
1165 * @brief Remove consecutive duplicate values from a sequence.
1166 * @ingroup mutating_algorithms
1167 * @param __first A forward iterator.
1168 * @param __last A forward iterator.
1169 * @return An iterator designating the end of the resulting sequence.
1171 * Removes all but the first element from each group of consecutive
1172 * values that compare equal.
1173 * unique() is stable, so the relative order of elements that are
1174 * not removed is unchanged.
1175 * Elements between the end of the resulting sequence and @p __last
1176 * are still present, but their value is unspecified.
1178 template<typename _ForwardIterator
>
1180 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1182 // concept requirements
1183 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1185 __glibcxx_function_requires(_EqualityComparableConcept
<
1186 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1187 __glibcxx_requires_valid_range(__first
, __last
);
1189 // Skip the beginning, if already unique.
1190 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
);
1191 if (__first
== __last
)
1194 // Do the real copy work.
1195 _ForwardIterator __dest
= __first
;
1197 while (++__first
!= __last
)
1198 if (!(*__dest
== *__first
))
1199 *++__dest
= _GLIBCXX_MOVE(*__first
);
1204 * @brief Remove consecutive values from a sequence using a predicate.
1205 * @ingroup mutating_algorithms
1206 * @param __first A forward iterator.
1207 * @param __last A forward iterator.
1208 * @param __binary_pred A binary predicate.
1209 * @return An iterator designating the end of the resulting sequence.
1211 * Removes all but the first element from each group of consecutive
1212 * values for which @p __binary_pred returns true.
1213 * unique() is stable, so the relative order of elements that are
1214 * not removed is unchanged.
1215 * Elements between the end of the resulting sequence and @p __last
1216 * are still present, but their value is unspecified.
1218 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1220 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1221 _BinaryPredicate __binary_pred
)
1223 // concept requirements
1224 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1226 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1227 typename iterator_traits
<_ForwardIterator
>::value_type
,
1228 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1229 __glibcxx_requires_valid_range(__first
, __last
);
1231 // Skip the beginning, if already unique.
1232 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
, __binary_pred
);
1233 if (__first
== __last
)
1236 // Do the real copy work.
1237 _ForwardIterator __dest
= __first
;
1239 while (++__first
!= __last
)
1240 if (!bool(__binary_pred(*__dest
, *__first
)))
1241 *++__dest
= _GLIBCXX_MOVE(*__first
);
1246 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1248 * overloaded for forward iterators and output iterator as result.
1250 template<typename _ForwardIterator
, typename _OutputIterator
>
1252 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1253 _OutputIterator __result
,
1254 forward_iterator_tag
, output_iterator_tag
)
1256 // concept requirements -- taken care of in dispatching function
1257 _ForwardIterator __next
= __first
;
1258 *__result
= *__first
;
1259 while (++__next
!= __last
)
1260 if (!(*__first
== *__next
))
1263 *++__result
= *__first
;
1269 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1271 * overloaded for input iterators and output iterator as result.
1273 template<typename _InputIterator
, typename _OutputIterator
>
1275 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1276 _OutputIterator __result
,
1277 input_iterator_tag
, output_iterator_tag
)
1279 // concept requirements -- taken care of in dispatching function
1280 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1281 *__result
= __value
;
1282 while (++__first
!= __last
)
1283 if (!(__value
== *__first
))
1286 *++__result
= __value
;
1292 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1294 * overloaded for input iterators and forward iterator as result.
1296 template<typename _InputIterator
, typename _ForwardIterator
>
1298 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1299 _ForwardIterator __result
,
1300 input_iterator_tag
, forward_iterator_tag
)
1302 // concept requirements -- taken care of in dispatching function
1303 *__result
= *__first
;
1304 while (++__first
!= __last
)
1305 if (!(*__result
== *__first
))
1306 *++__result
= *__first
;
1311 * This is an uglified
1312 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1314 * overloaded for forward iterators and output iterator as result.
1316 template<typename _ForwardIterator
, typename _OutputIterator
,
1317 typename _BinaryPredicate
>
1319 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1320 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1321 forward_iterator_tag
, output_iterator_tag
)
1323 // concept requirements -- iterators already checked
1324 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1325 typename iterator_traits
<_ForwardIterator
>::value_type
,
1326 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1328 _ForwardIterator __next
= __first
;
1329 *__result
= *__first
;
1330 while (++__next
!= __last
)
1331 if (!bool(__binary_pred(*__first
, *__next
)))
1334 *++__result
= *__first
;
1340 * This is an uglified
1341 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1343 * overloaded for input iterators and output iterator as result.
1345 template<typename _InputIterator
, typename _OutputIterator
,
1346 typename _BinaryPredicate
>
1348 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1349 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1350 input_iterator_tag
, output_iterator_tag
)
1352 // concept requirements -- iterators already checked
1353 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1354 typename iterator_traits
<_InputIterator
>::value_type
,
1355 typename iterator_traits
<_InputIterator
>::value_type
>)
1357 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1358 *__result
= __value
;
1359 while (++__first
!= __last
)
1360 if (!bool(__binary_pred(__value
, *__first
)))
1363 *++__result
= __value
;
1369 * This is an uglified
1370 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1372 * overloaded for input iterators and forward iterator as result.
1374 template<typename _InputIterator
, typename _ForwardIterator
,
1375 typename _BinaryPredicate
>
1377 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1378 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1379 input_iterator_tag
, forward_iterator_tag
)
1381 // concept requirements -- iterators already checked
1382 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1383 typename iterator_traits
<_ForwardIterator
>::value_type
,
1384 typename iterator_traits
<_InputIterator
>::value_type
>)
1386 *__result
= *__first
;
1387 while (++__first
!= __last
)
1388 if (!bool(__binary_pred(*__result
, *__first
)))
1389 *++__result
= *__first
;
1394 * This is an uglified reverse(_BidirectionalIterator,
1395 * _BidirectionalIterator)
1396 * overloaded for bidirectional iterators.
1398 template<typename _BidirectionalIterator
>
1400 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1401 bidirectional_iterator_tag
)
1404 if (__first
== __last
|| __first
== --__last
)
1408 std::iter_swap(__first
, __last
);
1414 * This is an uglified reverse(_BidirectionalIterator,
1415 * _BidirectionalIterator)
1416 * overloaded for random access iterators.
1418 template<typename _RandomAccessIterator
>
1420 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1421 random_access_iterator_tag
)
1423 if (__first
== __last
)
1426 while (__first
< __last
)
1428 std::iter_swap(__first
, __last
);
1435 * @brief Reverse a sequence.
1436 * @ingroup mutating_algorithms
1437 * @param __first A bidirectional iterator.
1438 * @param __last A bidirectional iterator.
1439 * @return reverse() returns no value.
1441 * Reverses the order of the elements in the range @p [__first,__last),
1442 * so that the first element becomes the last etc.
1443 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1444 * swaps @p *(__first+i) and @p *(__last-(i+1))
1446 template<typename _BidirectionalIterator
>
1448 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1450 // concept requirements
1451 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1452 _BidirectionalIterator
>)
1453 __glibcxx_requires_valid_range(__first
, __last
);
1454 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1458 * @brief Copy a sequence, reversing its elements.
1459 * @ingroup mutating_algorithms
1460 * @param __first A bidirectional iterator.
1461 * @param __last A bidirectional iterator.
1462 * @param __result An output iterator.
1463 * @return An iterator designating the end of the resulting sequence.
1465 * Copies the elements in the range @p [__first,__last) to the
1466 * range @p [__result,__result+(__last-__first)) such that the
1467 * order of the elements is reversed. For every @c i such that @p
1468 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1469 * assignment @p *(__result+(__last-__first)-i) = *(__first+i).
1470 * The ranges @p [__first,__last) and @p
1471 * [__result,__result+(__last-__first)) must not overlap.
1473 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1475 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1476 _OutputIterator __result
)
1478 // concept requirements
1479 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1480 _BidirectionalIterator
>)
1481 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1482 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1483 __glibcxx_requires_valid_range(__first
, __last
);
1485 while (__first
!= __last
)
1488 *__result
= *__last
;
1495 * This is a helper function for the rotate algorithm specialized on RAIs.
1496 * It returns the greatest common divisor of two integer values.
1498 template<typename _EuclideanRingElement
>
1499 _EuclideanRingElement
1500 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1504 _EuclideanRingElement __t
= __m
% __n
;
1511 /// This is a helper function for the rotate algorithm.
1512 template<typename _ForwardIterator
>
1514 __rotate(_ForwardIterator __first
,
1515 _ForwardIterator __middle
,
1516 _ForwardIterator __last
,
1517 forward_iterator_tag
)
1519 if (__first
== __middle
|| __last
== __middle
)
1522 _ForwardIterator __first2
= __middle
;
1525 std::iter_swap(__first
, __first2
);
1528 if (__first
== __middle
)
1529 __middle
= __first2
;
1531 while (__first2
!= __last
);
1533 __first2
= __middle
;
1535 while (__first2
!= __last
)
1537 std::iter_swap(__first
, __first2
);
1540 if (__first
== __middle
)
1541 __middle
= __first2
;
1542 else if (__first2
== __last
)
1543 __first2
= __middle
;
1547 /// This is a helper function for the rotate algorithm.
1548 template<typename _BidirectionalIterator
>
1550 __rotate(_BidirectionalIterator __first
,
1551 _BidirectionalIterator __middle
,
1552 _BidirectionalIterator __last
,
1553 bidirectional_iterator_tag
)
1555 // concept requirements
1556 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1557 _BidirectionalIterator
>)
1559 if (__first
== __middle
|| __last
== __middle
)
1562 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1563 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1565 while (__first
!= __middle
&& __middle
!= __last
)
1567 std::iter_swap(__first
, --__last
);
1571 if (__first
== __middle
)
1572 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1574 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1577 /// This is a helper function for the rotate algorithm.
1578 template<typename _RandomAccessIterator
>
1580 __rotate(_RandomAccessIterator __first
,
1581 _RandomAccessIterator __middle
,
1582 _RandomAccessIterator __last
,
1583 random_access_iterator_tag
)
1585 // concept requirements
1586 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1587 _RandomAccessIterator
>)
1589 if (__first
== __middle
|| __last
== __middle
)
1592 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1594 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1597 _Distance __n
= __last
- __first
;
1598 _Distance __k
= __middle
- __first
;
1600 if (__k
== __n
- __k
)
1602 std::swap_ranges(__first
, __middle
, __middle
);
1606 _RandomAccessIterator __p
= __first
;
1610 if (__k
< __n
- __k
)
1612 if (__is_pod(_ValueType
) && __k
== 1)
1614 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1615 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1616 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1619 _RandomAccessIterator __q
= __p
+ __k
;
1620 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1622 std::iter_swap(__p
, __q
);
1629 std::swap(__n
, __k
);
1635 if (__is_pod(_ValueType
) && __k
== 1)
1637 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1638 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1639 *__p
= _GLIBCXX_MOVE(__t
);
1642 _RandomAccessIterator __q
= __p
+ __n
;
1644 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1648 std::iter_swap(__p
, __q
);
1653 std::swap(__n
, __k
);
1659 * @brief Rotate the elements of a sequence.
1660 * @ingroup mutating_algorithms
1661 * @param __first A forward iterator.
1662 * @param __middle A forward iterator.
1663 * @param __last A forward iterator.
1666 * Rotates the elements of the range @p [__first,__last) by
1667 * @p(__middle - __first) positions so that the element at @p __middle
1668 * is moved to @p __first, the element at @p __middle+1 is moved to
1669 * @ __first+1 and so on for each element in the range
1670 * @p [__first,__last).
1672 * This effectively swaps the ranges @p [__first,__middle) and
1673 * @p [__middle,__last).
1676 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1677 * for each @p n in the range @p [0,__last-__first).
1679 template<typename _ForwardIterator
>
1681 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1682 _ForwardIterator __last
)
1684 // concept requirements
1685 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1687 __glibcxx_requires_valid_range(__first
, __middle
);
1688 __glibcxx_requires_valid_range(__middle
, __last
);
1690 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1692 std::__rotate(__first
, __middle
, __last
, _IterType());
1696 * @brief Copy a sequence, rotating its elements.
1697 * @ingroup mutating_algorithms
1698 * @param __first A forward iterator.
1699 * @param __middle A forward iterator.
1700 * @param __last A forward iterator.
1701 * @param __result An output iterator.
1702 * @return An iterator designating the end of the resulting sequence.
1704 * Copies the elements of the range @p [__first,__last) to the
1705 * range beginning at @result, rotating the copied elements by
1706 * @p (__middle-__first) positions so that the element at @p __middle
1707 * is moved to @p __result, the element at @p __middle+1 is moved
1708 * to @__result+1 and so on for each element in the range @p
1712 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1713 * for each @p n in the range @p [0,__last-__first).
1715 template<typename _ForwardIterator
, typename _OutputIterator
>
1717 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1718 _ForwardIterator __last
, _OutputIterator __result
)
1720 // concept requirements
1721 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1722 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1723 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1724 __glibcxx_requires_valid_range(__first
, __middle
);
1725 __glibcxx_requires_valid_range(__middle
, __last
);
1727 return std::copy(__first
, __middle
,
1728 std::copy(__middle
, __last
, __result
));
1731 /// This is a helper function...
1732 template<typename _ForwardIterator
, typename _Predicate
>
1734 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1735 _Predicate __pred
, forward_iterator_tag
)
1737 if (__first
== __last
)
1740 while (__pred(*__first
))
1741 if (++__first
== __last
)
1744 _ForwardIterator __next
= __first
;
1746 while (++__next
!= __last
)
1747 if (__pred(*__next
))
1749 std::iter_swap(__first
, __next
);
1756 /// This is a helper function...
1757 template<typename _BidirectionalIterator
, typename _Predicate
>
1758 _BidirectionalIterator
1759 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1760 _Predicate __pred
, bidirectional_iterator_tag
)
1765 if (__first
== __last
)
1767 else if (__pred(*__first
))
1773 if (__first
== __last
)
1775 else if (!bool(__pred(*__last
)))
1779 std::iter_swap(__first
, __last
);
1786 /// This is a helper function...
1787 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1789 __inplace_stable_partition(_ForwardIterator __first
,
1790 _ForwardIterator __last
,
1791 _Predicate __pred
, _Distance __len
)
1794 return __pred(*__first
) ? __last
: __first
;
1795 _ForwardIterator __middle
= __first
;
1796 std::advance(__middle
, __len
/ 2);
1797 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1801 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1805 std::rotate(__begin
, __middle
, __end
);
1806 std::advance(__begin
, std::distance(__middle
, __end
));
1810 /// This is a helper function...
1811 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1814 __stable_partition_adaptive(_ForwardIterator __first
,
1815 _ForwardIterator __last
,
1816 _Predicate __pred
, _Distance __len
,
1818 _Distance __buffer_size
)
1820 if (__len
<= __buffer_size
)
1822 _ForwardIterator __result1
= __first
;
1823 _Pointer __result2
= __buffer
;
1824 for (; __first
!= __last
; ++__first
)
1825 if (__pred(*__first
))
1827 *__result1
= _GLIBCXX_MOVE(*__first
);
1832 *__result2
= _GLIBCXX_MOVE(*__first
);
1835 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1840 _ForwardIterator __middle
= __first
;
1841 std::advance(__middle
, __len
/ 2);
1842 _ForwardIterator __begin
=
1843 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1844 __len
/ 2, __buffer
,
1846 _ForwardIterator __end
=
1847 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1849 __buffer
, __buffer_size
);
1850 std::rotate(__begin
, __middle
, __end
);
1851 std::advance(__begin
, std::distance(__middle
, __end
));
1857 * @brief Move elements for which a predicate is true to the beginning
1858 * of a sequence, preserving relative ordering.
1859 * @ingroup mutating_algorithms
1860 * @param __first A forward iterator.
1861 * @param __last A forward iterator.
1862 * @param __pred A predicate functor.
1863 * @return An iterator @p middle such that @p __pred(i) is true for each
1864 * iterator @p i in the range @p [first,middle) and false for each @p i
1865 * in the range @p [middle,last).
1867 * Performs the same function as @p partition() with the additional
1868 * guarantee that the relative ordering of elements in each group is
1869 * preserved, so any two elements @p x and @p y in the range
1870 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1871 * relative ordering after calling @p stable_partition().
1873 template<typename _ForwardIterator
, typename _Predicate
>
1875 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1878 // concept requirements
1879 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1881 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1882 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1883 __glibcxx_requires_valid_range(__first
, __last
);
1885 if (__first
== __last
)
1889 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1891 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1894 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1896 if (__buf
.size() > 0)
1898 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1899 _DistanceType(__buf
.requested_size()),
1901 _DistanceType(__buf
.size()));
1904 std::__inplace_stable_partition(__first
, __last
, __pred
,
1905 _DistanceType(__buf
.requested_size()));
1909 /// This is a helper function for the sort routines.
1910 template<typename _RandomAccessIterator
>
1912 __heap_select(_RandomAccessIterator __first
,
1913 _RandomAccessIterator __middle
,
1914 _RandomAccessIterator __last
)
1916 std::make_heap(__first
, __middle
);
1917 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1918 if (*__i
< *__first
)
1919 std::__pop_heap(__first
, __middle
, __i
);
1922 /// This is a helper function for the sort routines.
1923 template<typename _RandomAccessIterator
, typename _Compare
>
1925 __heap_select(_RandomAccessIterator __first
,
1926 _RandomAccessIterator __middle
,
1927 _RandomAccessIterator __last
, _Compare __comp
)
1929 std::make_heap(__first
, __middle
, __comp
);
1930 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1931 if (__comp(*__i
, *__first
))
1932 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1938 * @brief Copy the smallest elements of a sequence.
1939 * @ingroup sorting_algorithms
1940 * @param __first An iterator.
1941 * @param __last Another iterator.
1942 * @param __result_first A random-access iterator.
1943 * @param __result_last Another random-access iterator.
1944 * @return An iterator indicating the end of the resulting sequence.
1946 * Copies and sorts the smallest N values from the range @p [__first,__last)
1947 * to the range beginning at @p __result_first, where the number of
1948 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1949 * @p (__result_last-__result_first).
1950 * After the sort if @p i and @j are iterators in the range
1951 * @p [__result_first,__result_first+N) such that @i precedes @j then
1952 * @p *j<*i is false.
1953 * The value returned is @p __result_first+N.
1955 template<typename _InputIterator
, typename _RandomAccessIterator
>
1956 _RandomAccessIterator
1957 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1958 _RandomAccessIterator __result_first
,
1959 _RandomAccessIterator __result_last
)
1961 typedef typename iterator_traits
<_InputIterator
>::value_type
1963 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1965 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1968 // concept requirements
1969 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1970 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1972 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1974 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1975 __glibcxx_requires_valid_range(__first
, __last
);
1976 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1978 if (__result_first
== __result_last
)
1979 return __result_last
;
1980 _RandomAccessIterator __result_real_last
= __result_first
;
1981 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1983 *__result_real_last
= *__first
;
1984 ++__result_real_last
;
1987 std::make_heap(__result_first
, __result_real_last
);
1988 while (__first
!= __last
)
1990 if (*__first
< *__result_first
)
1991 std::__adjust_heap(__result_first
, _DistanceType(0),
1992 _DistanceType(__result_real_last
1994 _InputValueType(*__first
));
1997 std::sort_heap(__result_first
, __result_real_last
);
1998 return __result_real_last
;
2002 * @brief Copy the smallest elements of a sequence using a predicate for
2004 * @ingroup sorting_algorithms
2005 * @param __first An input iterator.
2006 * @param __last Another input iterator.
2007 * @param __result_first A random-access iterator.
2008 * @param __result_last Another random-access iterator.
2009 * @param __comp A comparison functor.
2010 * @return An iterator indicating the end of the resulting sequence.
2012 * Copies and sorts the smallest N values from the range @p [__first,__last)
2013 * to the range beginning at @p result_first, where the number of
2014 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
2015 * @p (__result_last-__result_first).
2016 * After the sort if @p i and @j are iterators in the range
2017 * @p [__result_first,__result_first+N) such that @i precedes @j then
2018 * @p __comp(*j,*i) is false.
2019 * The value returned is @p __result_first+N.
2021 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2022 _RandomAccessIterator
2023 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2024 _RandomAccessIterator __result_first
,
2025 _RandomAccessIterator __result_last
,
2028 typedef typename iterator_traits
<_InputIterator
>::value_type
2030 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2032 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2035 // concept requirements
2036 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2037 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2038 _RandomAccessIterator
>)
2039 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2041 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2042 _InputValueType
, _OutputValueType
>)
2043 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2044 _OutputValueType
, _OutputValueType
>)
2045 __glibcxx_requires_valid_range(__first
, __last
);
2046 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2048 if (__result_first
== __result_last
)
2049 return __result_last
;
2050 _RandomAccessIterator __result_real_last
= __result_first
;
2051 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2053 *__result_real_last
= *__first
;
2054 ++__result_real_last
;
2057 std::make_heap(__result_first
, __result_real_last
, __comp
);
2058 while (__first
!= __last
)
2060 if (__comp(*__first
, *__result_first
))
2061 std::__adjust_heap(__result_first
, _DistanceType(0),
2062 _DistanceType(__result_real_last
2064 _InputValueType(*__first
),
2068 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2069 return __result_real_last
;
2072 /// This is a helper function for the sort routine.
2073 template<typename _RandomAccessIterator
>
2075 __unguarded_linear_insert(_RandomAccessIterator __last
)
2077 typename iterator_traits
<_RandomAccessIterator
>::value_type
2078 __val
= _GLIBCXX_MOVE(*__last
);
2079 _RandomAccessIterator __next
= __last
;
2081 while (__val
< *__next
)
2083 *__last
= _GLIBCXX_MOVE(*__next
);
2087 *__last
= _GLIBCXX_MOVE(__val
);
2090 /// This is a helper function for the sort routine.
2091 template<typename _RandomAccessIterator
, typename _Compare
>
2093 __unguarded_linear_insert(_RandomAccessIterator __last
,
2096 typename iterator_traits
<_RandomAccessIterator
>::value_type
2097 __val
= _GLIBCXX_MOVE(*__last
);
2098 _RandomAccessIterator __next
= __last
;
2100 while (__comp(__val
, *__next
))
2102 *__last
= _GLIBCXX_MOVE(*__next
);
2106 *__last
= _GLIBCXX_MOVE(__val
);
2109 /// This is a helper function for the sort routine.
2110 template<typename _RandomAccessIterator
>
2112 __insertion_sort(_RandomAccessIterator __first
,
2113 _RandomAccessIterator __last
)
2115 if (__first
== __last
)
2118 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2120 if (*__i
< *__first
)
2122 typename iterator_traits
<_RandomAccessIterator
>::value_type
2123 __val
= _GLIBCXX_MOVE(*__i
);
2124 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2125 *__first
= _GLIBCXX_MOVE(__val
);
2128 std::__unguarded_linear_insert(__i
);
2132 /// This is a helper function for the sort routine.
2133 template<typename _RandomAccessIterator
, typename _Compare
>
2135 __insertion_sort(_RandomAccessIterator __first
,
2136 _RandomAccessIterator __last
, _Compare __comp
)
2138 if (__first
== __last
) return;
2140 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2142 if (__comp(*__i
, *__first
))
2144 typename iterator_traits
<_RandomAccessIterator
>::value_type
2145 __val
= _GLIBCXX_MOVE(*__i
);
2146 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2147 *__first
= _GLIBCXX_MOVE(__val
);
2150 std::__unguarded_linear_insert(__i
, __comp
);
2154 /// This is a helper function for the sort routine.
2155 template<typename _RandomAccessIterator
>
2157 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2158 _RandomAccessIterator __last
)
2160 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2163 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2164 std::__unguarded_linear_insert(__i
);
2167 /// This is a helper function for the sort routine.
2168 template<typename _RandomAccessIterator
, typename _Compare
>
2170 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2171 _RandomAccessIterator __last
, _Compare __comp
)
2173 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2176 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2177 std::__unguarded_linear_insert(__i
, __comp
);
2182 * This controls some aspect of the sort routines.
2184 enum { _S_threshold
= 16 };
2186 /// This is a helper function for the sort routine.
2187 template<typename _RandomAccessIterator
>
2189 __final_insertion_sort(_RandomAccessIterator __first
,
2190 _RandomAccessIterator __last
)
2192 if (__last
- __first
> int(_S_threshold
))
2194 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2195 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2198 std::__insertion_sort(__first
, __last
);
2201 /// This is a helper function for the sort routine.
2202 template<typename _RandomAccessIterator
, typename _Compare
>
2204 __final_insertion_sort(_RandomAccessIterator __first
,
2205 _RandomAccessIterator __last
, _Compare __comp
)
2207 if (__last
- __first
> int(_S_threshold
))
2209 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2210 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2214 std::__insertion_sort(__first
, __last
, __comp
);
2217 /// This is a helper function...
2218 template<typename _RandomAccessIterator
, typename _Tp
>
2219 _RandomAccessIterator
2220 __unguarded_partition(_RandomAccessIterator __first
,
2221 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2225 while (*__first
< __pivot
)
2228 while (__pivot
< *__last
)
2230 if (!(__first
< __last
))
2232 std::iter_swap(__first
, __last
);
2237 /// This is a helper function...
2238 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2239 _RandomAccessIterator
2240 __unguarded_partition(_RandomAccessIterator __first
,
2241 _RandomAccessIterator __last
,
2242 const _Tp
& __pivot
, _Compare __comp
)
2246 while (__comp(*__first
, __pivot
))
2249 while (__comp(__pivot
, *__last
))
2251 if (!(__first
< __last
))
2253 std::iter_swap(__first
, __last
);
2258 /// This is a helper function...
2259 template<typename _RandomAccessIterator
>
2260 inline _RandomAccessIterator
2261 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2262 _RandomAccessIterator __last
)
2264 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2265 std::__move_median_first(__first
, __mid
, (__last
- 1));
2266 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2270 /// This is a helper function...
2271 template<typename _RandomAccessIterator
, typename _Compare
>
2272 inline _RandomAccessIterator
2273 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2274 _RandomAccessIterator __last
, _Compare __comp
)
2276 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2277 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2278 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2281 /// This is a helper function for the sort routine.
2282 template<typename _RandomAccessIterator
, typename _Size
>
2284 __introsort_loop(_RandomAccessIterator __first
,
2285 _RandomAccessIterator __last
,
2286 _Size __depth_limit
)
2288 while (__last
- __first
> int(_S_threshold
))
2290 if (__depth_limit
== 0)
2292 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
);
2296 _RandomAccessIterator __cut
=
2297 std::__unguarded_partition_pivot(__first
, __last
);
2298 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2303 /// This is a helper function for the sort routine.
2304 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2306 __introsort_loop(_RandomAccessIterator __first
,
2307 _RandomAccessIterator __last
,
2308 _Size __depth_limit
, _Compare __comp
)
2310 while (__last
- __first
> int(_S_threshold
))
2312 if (__depth_limit
== 0)
2314 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
, __comp
);
2318 _RandomAccessIterator __cut
=
2319 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2320 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2327 template<typename _RandomAccessIterator
, typename _Size
>
2329 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2330 _RandomAccessIterator __last
, _Size __depth_limit
)
2332 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2335 while (__last
- __first
> 3)
2337 if (__depth_limit
== 0)
2339 std::__heap_select(__first
, __nth
+ 1, __last
);
2341 // Place the nth largest element in its final position.
2342 std::iter_swap(__first
, __nth
);
2346 _RandomAccessIterator __cut
=
2347 std::__unguarded_partition_pivot(__first
, __last
);
2353 std::__insertion_sort(__first
, __last
);
2356 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2358 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2359 _RandomAccessIterator __last
, _Size __depth_limit
,
2362 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2365 while (__last
- __first
> 3)
2367 if (__depth_limit
== 0)
2369 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2370 // Place the nth largest element in its final position.
2371 std::iter_swap(__first
, __nth
);
2375 _RandomAccessIterator __cut
=
2376 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2382 std::__insertion_sort(__first
, __last
, __comp
);
2387 // lower_bound moved to stl_algobase.h
2390 * @brief Finds the first position in which @a val could be inserted
2391 * without changing the ordering.
2392 * @ingroup binary_search_algorithms
2393 * @param __first An iterator.
2394 * @param __last Another iterator.
2395 * @param __val The search term.
2396 * @param __comp A functor to use for comparisons.
2397 * @return An iterator pointing to the first element <em>not less
2398 * than</em> @a __val, or end() if every element is less
2400 * @ingroup binary_search_algorithms
2402 * The comparison function should have the same effects on ordering as
2403 * the function used for the initial sort.
2405 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2407 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2408 const _Tp
& __val
, _Compare __comp
)
2410 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2412 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2415 // concept requirements
2416 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2417 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2419 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2422 _DistanceType __len
= std::distance(__first
, __last
);
2426 _DistanceType __half
= __len
>> 1;
2427 _ForwardIterator __middle
= __first
;
2428 std::advance(__middle
, __half
);
2429 if (__comp(*__middle
, __val
))
2433 __len
= __len
- __half
- 1;
2442 * @brief Finds the last position in which @a val could be inserted
2443 * without changing the ordering.
2444 * @ingroup binary_search_algorithms
2445 * @param __first An iterator.
2446 * @param __last Another iterator.
2447 * @param __val The search term.
2448 * @return An iterator pointing to the first element greater than @a __val,
2449 * or end() if no elements are greater than @a __val.
2450 * @ingroup binary_search_algorithms
2452 template<typename _ForwardIterator
, typename _Tp
>
2454 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2457 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2459 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2462 // concept requirements
2463 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2464 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2465 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2467 _DistanceType __len
= std::distance(__first
, __last
);
2471 _DistanceType __half
= __len
>> 1;
2472 _ForwardIterator __middle
= __first
;
2473 std::advance(__middle
, __half
);
2474 if (__val
< *__middle
)
2480 __len
= __len
- __half
- 1;
2487 * @brief Finds the last position in which @a val could be inserted
2488 * without changing the ordering.
2489 * @ingroup binary_search_algorithms
2490 * @param __first An iterator.
2491 * @param __last Another iterator.
2492 * @param __val The search term.
2493 * @param __comp A functor to use for comparisons.
2494 * @return An iterator pointing to the first element greater than @a __val,
2495 * or end() if no elements are greater than @a __val.
2496 * @ingroup binary_search_algorithms
2498 * The comparison function should have the same effects on ordering as
2499 * the function used for the initial sort.
2501 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2503 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2504 const _Tp
& __val
, _Compare __comp
)
2506 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2508 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2511 // concept requirements
2512 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2513 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2515 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2518 _DistanceType __len
= std::distance(__first
, __last
);
2522 _DistanceType __half
= __len
>> 1;
2523 _ForwardIterator __middle
= __first
;
2524 std::advance(__middle
, __half
);
2525 if (__comp(__val
, *__middle
))
2531 __len
= __len
- __half
- 1;
2538 * @brief Finds the largest subrange in which @a val could be inserted
2539 * at any place in it without changing the ordering.
2540 * @ingroup binary_search_algorithms
2541 * @param __first An iterator.
2542 * @param __last Another iterator.
2543 * @param __val The search term.
2544 * @return An pair of iterators defining the subrange.
2545 * @ingroup binary_search_algorithms
2547 * This is equivalent to
2549 * std::make_pair(lower_bound(__first, __last, __val),
2550 * upper_bound(__first, __last, __val))
2552 * but does not actually call those functions.
2554 template<typename _ForwardIterator
, typename _Tp
>
2555 pair
<_ForwardIterator
, _ForwardIterator
>
2556 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2559 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2561 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2564 // concept requirements
2565 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2566 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2567 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2568 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2569 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2571 _DistanceType __len
= std::distance(__first
, __last
);
2575 _DistanceType __half
= __len
>> 1;
2576 _ForwardIterator __middle
= __first
;
2577 std::advance(__middle
, __half
);
2578 if (*__middle
< __val
)
2582 __len
= __len
- __half
- 1;
2584 else if (__val
< *__middle
)
2588 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2590 std::advance(__first
, __len
);
2591 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2593 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2596 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2600 * @brief Finds the largest subrange in which @a val could be inserted
2601 * at any place in it without changing the ordering.
2602 * @param __first An iterator.
2603 * @param __last Another iterator.
2604 * @param __val The search term.
2605 * @param __comp A functor to use for comparisons.
2606 * @return An pair of iterators defining the subrange.
2607 * @ingroup binary_search_algorithms
2609 * This is equivalent to
2611 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2612 * upper_bound(__first, __last, __val, __comp))
2614 * but does not actually call those functions.
2616 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2617 pair
<_ForwardIterator
, _ForwardIterator
>
2618 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2619 const _Tp
& __val
, _Compare __comp
)
2621 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2623 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2626 // concept requirements
2627 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2628 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2630 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2632 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2634 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2637 _DistanceType __len
= std::distance(__first
, __last
);
2641 _DistanceType __half
= __len
>> 1;
2642 _ForwardIterator __middle
= __first
;
2643 std::advance(__middle
, __half
);
2644 if (__comp(*__middle
, __val
))
2648 __len
= __len
- __half
- 1;
2650 else if (__comp(__val
, *__middle
))
2654 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2656 std::advance(__first
, __len
);
2657 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2659 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2662 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2666 * @brief Determines whether an element exists in a range.
2667 * @ingroup binary_search_algorithms
2668 * @param __first An iterator.
2669 * @param __last Another iterator.
2670 * @param __val The search term.
2671 * @return True if @a __val (or its equivalent) is in [@a
2672 * __first,@a __last ].
2674 * Note that this does not actually return an iterator to @a __val. For
2675 * that, use std::find or a container's specialized find member functions.
2677 template<typename _ForwardIterator
, typename _Tp
>
2679 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2682 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2685 // concept requirements
2686 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2687 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2688 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2689 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2691 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2692 return __i
!= __last
&& !(__val
< *__i
);
2696 * @brief Determines whether an element exists in a range.
2697 * @ingroup binary_search_algorithms
2698 * @param __first An iterator.
2699 * @param __last Another iterator.
2700 * @param __val The search term.
2701 * @param __comp A functor to use for comparisons.
2702 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2704 * Note that this does not actually return an iterator to @a val. For
2705 * that, use std::find or a container's specialized find member functions.
2707 * The comparison function should have the same effects on ordering as
2708 * the function used for the initial sort.
2710 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2712 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2713 const _Tp
& __val
, _Compare __comp
)
2715 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2718 // concept requirements
2719 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2720 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2722 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2724 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2727 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2728 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2733 /// This is a helper function for the __merge_adaptive routines.
2734 template<typename _InputIterator1
, typename _InputIterator2
,
2735 typename _OutputIterator
>
2737 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2738 _InputIterator2 __first2
, _InputIterator2 __last2
,
2739 _OutputIterator __result
)
2741 while (__first1
!= __last1
&& __first2
!= __last2
)
2743 if (*__first2
< *__first1
)
2745 *__result
= _GLIBCXX_MOVE(*__first2
);
2750 *__result
= _GLIBCXX_MOVE(*__first1
);
2755 if (__first1
!= __last1
)
2756 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2759 /// This is a helper function for the __merge_adaptive routines.
2760 template<typename _InputIterator1
, typename _InputIterator2
,
2761 typename _OutputIterator
, typename _Compare
>
2763 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2764 _InputIterator2 __first2
, _InputIterator2 __last2
,
2765 _OutputIterator __result
, _Compare __comp
)
2767 while (__first1
!= __last1
&& __first2
!= __last2
)
2769 if (__comp(*__first2
, *__first1
))
2771 *__result
= _GLIBCXX_MOVE(*__first2
);
2776 *__result
= _GLIBCXX_MOVE(*__first1
);
2781 if (__first1
!= __last1
)
2782 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2785 /// This is a helper function for the __merge_adaptive routines.
2786 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2787 typename _BidirectionalIterator3
>
2789 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2790 _BidirectionalIterator1 __last1
,
2791 _BidirectionalIterator2 __first2
,
2792 _BidirectionalIterator2 __last2
,
2793 _BidirectionalIterator3 __result
)
2795 if (__first1
== __last1
)
2797 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2800 else if (__first2
== __last2
)
2807 if (*__last2
< *__last1
)
2809 *--__result
= _GLIBCXX_MOVE(*__last1
);
2810 if (__first1
== __last1
)
2812 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2819 *--__result
= _GLIBCXX_MOVE(*__last2
);
2820 if (__first2
== __last2
)
2827 /// This is a helper function for the __merge_adaptive routines.
2828 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2829 typename _BidirectionalIterator3
, typename _Compare
>
2831 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2832 _BidirectionalIterator1 __last1
,
2833 _BidirectionalIterator2 __first2
,
2834 _BidirectionalIterator2 __last2
,
2835 _BidirectionalIterator3 __result
,
2838 if (__first1
== __last1
)
2840 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2843 else if (__first2
== __last2
)
2850 if (__comp(*__last2
, *__last1
))
2852 *--__result
= _GLIBCXX_MOVE(*__last1
);
2853 if (__first1
== __last1
)
2855 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2862 *--__result
= _GLIBCXX_MOVE(*__last2
);
2863 if (__first2
== __last2
)
2870 /// This is a helper function for the merge routines.
2871 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2873 _BidirectionalIterator1
2874 __rotate_adaptive(_BidirectionalIterator1 __first
,
2875 _BidirectionalIterator1 __middle
,
2876 _BidirectionalIterator1 __last
,
2877 _Distance __len1
, _Distance __len2
,
2878 _BidirectionalIterator2 __buffer
,
2879 _Distance __buffer_size
)
2881 _BidirectionalIterator2 __buffer_end
;
2882 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2886 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2887 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2888 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2893 else if (__len1
<= __buffer_size
)
2897 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2898 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2899 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2906 std::rotate(__first
, __middle
, __last
);
2907 std::advance(__first
, std::distance(__middle
, __last
));
2912 /// This is a helper function for the merge routines.
2913 template<typename _BidirectionalIterator
, typename _Distance
,
2916 __merge_adaptive(_BidirectionalIterator __first
,
2917 _BidirectionalIterator __middle
,
2918 _BidirectionalIterator __last
,
2919 _Distance __len1
, _Distance __len2
,
2920 _Pointer __buffer
, _Distance __buffer_size
)
2922 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2924 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2925 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2928 else if (__len2
<= __buffer_size
)
2930 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2931 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2932 __buffer_end
, __last
);
2936 _BidirectionalIterator __first_cut
= __first
;
2937 _BidirectionalIterator __second_cut
= __middle
;
2938 _Distance __len11
= 0;
2939 _Distance __len22
= 0;
2940 if (__len1
> __len2
)
2942 __len11
= __len1
/ 2;
2943 std::advance(__first_cut
, __len11
);
2944 __second_cut
= std::lower_bound(__middle
, __last
,
2946 __len22
= std::distance(__middle
, __second_cut
);
2950 __len22
= __len2
/ 2;
2951 std::advance(__second_cut
, __len22
);
2952 __first_cut
= std::upper_bound(__first
, __middle
,
2954 __len11
= std::distance(__first
, __first_cut
);
2956 _BidirectionalIterator __new_middle
=
2957 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2958 __len1
- __len11
, __len22
, __buffer
,
2960 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2961 __len22
, __buffer
, __buffer_size
);
2962 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2964 __len2
- __len22
, __buffer
, __buffer_size
);
2968 /// This is a helper function for the merge routines.
2969 template<typename _BidirectionalIterator
, typename _Distance
,
2970 typename _Pointer
, typename _Compare
>
2972 __merge_adaptive(_BidirectionalIterator __first
,
2973 _BidirectionalIterator __middle
,
2974 _BidirectionalIterator __last
,
2975 _Distance __len1
, _Distance __len2
,
2976 _Pointer __buffer
, _Distance __buffer_size
,
2979 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2981 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2982 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2985 else if (__len2
<= __buffer_size
)
2987 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2988 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2989 __buffer_end
, __last
, __comp
);
2993 _BidirectionalIterator __first_cut
= __first
;
2994 _BidirectionalIterator __second_cut
= __middle
;
2995 _Distance __len11
= 0;
2996 _Distance __len22
= 0;
2997 if (__len1
> __len2
)
2999 __len11
= __len1
/ 2;
3000 std::advance(__first_cut
, __len11
);
3001 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3003 __len22
= std::distance(__middle
, __second_cut
);
3007 __len22
= __len2
/ 2;
3008 std::advance(__second_cut
, __len22
);
3009 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3011 __len11
= std::distance(__first
, __first_cut
);
3013 _BidirectionalIterator __new_middle
=
3014 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3015 __len1
- __len11
, __len22
, __buffer
,
3017 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3018 __len22
, __buffer
, __buffer_size
, __comp
);
3019 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3021 __len2
- __len22
, __buffer
,
3022 __buffer_size
, __comp
);
3026 /// This is a helper function for the merge routines.
3027 template<typename _BidirectionalIterator
, typename _Distance
>
3029 __merge_without_buffer(_BidirectionalIterator __first
,
3030 _BidirectionalIterator __middle
,
3031 _BidirectionalIterator __last
,
3032 _Distance __len1
, _Distance __len2
)
3034 if (__len1
== 0 || __len2
== 0)
3036 if (__len1
+ __len2
== 2)
3038 if (*__middle
< *__first
)
3039 std::iter_swap(__first
, __middle
);
3042 _BidirectionalIterator __first_cut
= __first
;
3043 _BidirectionalIterator __second_cut
= __middle
;
3044 _Distance __len11
= 0;
3045 _Distance __len22
= 0;
3046 if (__len1
> __len2
)
3048 __len11
= __len1
/ 2;
3049 std::advance(__first_cut
, __len11
);
3050 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3051 __len22
= std::distance(__middle
, __second_cut
);
3055 __len22
= __len2
/ 2;
3056 std::advance(__second_cut
, __len22
);
3057 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3058 __len11
= std::distance(__first
, __first_cut
);
3060 std::rotate(__first_cut
, __middle
, __second_cut
);
3061 _BidirectionalIterator __new_middle
= __first_cut
;
3062 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3063 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3065 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3066 __len1
- __len11
, __len2
- __len22
);
3069 /// This is a helper function for the merge routines.
3070 template<typename _BidirectionalIterator
, typename _Distance
,
3073 __merge_without_buffer(_BidirectionalIterator __first
,
3074 _BidirectionalIterator __middle
,
3075 _BidirectionalIterator __last
,
3076 _Distance __len1
, _Distance __len2
,
3079 if (__len1
== 0 || __len2
== 0)
3081 if (__len1
+ __len2
== 2)
3083 if (__comp(*__middle
, *__first
))
3084 std::iter_swap(__first
, __middle
);
3087 _BidirectionalIterator __first_cut
= __first
;
3088 _BidirectionalIterator __second_cut
= __middle
;
3089 _Distance __len11
= 0;
3090 _Distance __len22
= 0;
3091 if (__len1
> __len2
)
3093 __len11
= __len1
/ 2;
3094 std::advance(__first_cut
, __len11
);
3095 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3097 __len22
= std::distance(__middle
, __second_cut
);
3101 __len22
= __len2
/ 2;
3102 std::advance(__second_cut
, __len22
);
3103 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3105 __len11
= std::distance(__first
, __first_cut
);
3107 std::rotate(__first_cut
, __middle
, __second_cut
);
3108 _BidirectionalIterator __new_middle
= __first_cut
;
3109 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3110 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3111 __len11
, __len22
, __comp
);
3112 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3113 __len1
- __len11
, __len2
- __len22
, __comp
);
3117 * @brief Merges two sorted ranges in place.
3118 * @ingroup sorting_algorithms
3119 * @param __first An iterator.
3120 * @param __middle Another iterator.
3121 * @param __last Another iterator.
3124 * Merges two sorted and consecutive ranges, [__first,__middle) and
3125 * [__middle,__last), and puts the result in [__first,__last). The
3126 * output will be sorted. The sort is @e stable, that is, for
3127 * equivalent elements in the two ranges, elements from the first
3128 * range will always come before elements from the second.
3130 * If enough additional memory is available, this takes (__last-__first)-1
3131 * comparisons. Otherwise an NlogN algorithm is used, where N is
3132 * distance(__first,__last).
3134 template<typename _BidirectionalIterator
>
3136 inplace_merge(_BidirectionalIterator __first
,
3137 _BidirectionalIterator __middle
,
3138 _BidirectionalIterator __last
)
3140 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3142 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3145 // concept requirements
3146 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3147 _BidirectionalIterator
>)
3148 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3149 __glibcxx_requires_sorted(__first
, __middle
);
3150 __glibcxx_requires_sorted(__middle
, __last
);
3152 if (__first
== __middle
|| __middle
== __last
)
3155 _DistanceType __len1
= std::distance(__first
, __middle
);
3156 _DistanceType __len2
= std::distance(__middle
, __last
);
3158 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3160 if (__buf
.begin() == 0)
3161 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3163 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3164 __buf
.begin(), _DistanceType(__buf
.size()));
3168 * @brief Merges two sorted ranges in place.
3169 * @ingroup sorting_algorithms
3170 * @param __first An iterator.
3171 * @param __middle Another iterator.
3172 * @param __last Another iterator.
3173 * @param __comp A functor to use for comparisons.
3176 * Merges two sorted and consecutive ranges, [__first,__middle) and
3177 * [middle,last), and puts the result in [__first,__last). The output will
3178 * be sorted. The sort is @e stable, that is, for equivalent
3179 * elements in the two ranges, elements from the first range will always
3180 * come before elements from the second.
3182 * If enough additional memory is available, this takes (__last-__first)-1
3183 * comparisons. Otherwise an NlogN algorithm is used, where N is
3184 * distance(__first,__last).
3186 * The comparison function should have the same effects on ordering as
3187 * the function used for the initial sort.
3189 template<typename _BidirectionalIterator
, typename _Compare
>
3191 inplace_merge(_BidirectionalIterator __first
,
3192 _BidirectionalIterator __middle
,
3193 _BidirectionalIterator __last
,
3196 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3198 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3201 // concept requirements
3202 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3203 _BidirectionalIterator
>)
3204 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3205 _ValueType
, _ValueType
>)
3206 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3207 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3209 if (__first
== __middle
|| __middle
== __last
)
3212 const _DistanceType __len1
= std::distance(__first
, __middle
);
3213 const _DistanceType __len2
= std::distance(__middle
, __last
);
3215 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3217 if (__buf
.begin() == 0)
3218 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3221 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3222 __buf
.begin(), _DistanceType(__buf
.size()),
3227 /// This is a helper function for the __merge_sort_loop routines.
3228 template<typename _InputIterator1
, typename _InputIterator2
,
3229 typename _OutputIterator
>
3231 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3232 _InputIterator2 __first2
, _InputIterator2 __last2
,
3233 _OutputIterator __result
)
3235 while (__first1
!= __last1
&& __first2
!= __last2
)
3237 if (*__first2
< *__first1
)
3239 *__result
= _GLIBCXX_MOVE(*__first2
);
3244 *__result
= _GLIBCXX_MOVE(*__first1
);
3249 return _GLIBCXX_MOVE3(__first2
, __last2
,
3250 _GLIBCXX_MOVE3(__first1
, __last1
,
3254 /// This is a helper function for the __merge_sort_loop routines.
3255 template<typename _InputIterator1
, typename _InputIterator2
,
3256 typename _OutputIterator
, typename _Compare
>
3258 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3259 _InputIterator2 __first2
, _InputIterator2 __last2
,
3260 _OutputIterator __result
, _Compare __comp
)
3262 while (__first1
!= __last1
&& __first2
!= __last2
)
3264 if (__comp(*__first2
, *__first1
))
3266 *__result
= _GLIBCXX_MOVE(*__first2
);
3271 *__result
= _GLIBCXX_MOVE(*__first1
);
3276 return _GLIBCXX_MOVE3(__first2
, __last2
,
3277 _GLIBCXX_MOVE3(__first1
, __last1
,
3281 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3284 __merge_sort_loop(_RandomAccessIterator1 __first
,
3285 _RandomAccessIterator1 __last
,
3286 _RandomAccessIterator2 __result
,
3287 _Distance __step_size
)
3289 const _Distance __two_step
= 2 * __step_size
;
3291 while (__last
- __first
>= __two_step
)
3293 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3294 __first
+ __step_size
,
3295 __first
+ __two_step
, __result
);
3296 __first
+= __two_step
;
3299 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3300 std::__move_merge(__first
, __first
+ __step_size
,
3301 __first
+ __step_size
, __last
, __result
);
3304 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3305 typename _Distance
, typename _Compare
>
3307 __merge_sort_loop(_RandomAccessIterator1 __first
,
3308 _RandomAccessIterator1 __last
,
3309 _RandomAccessIterator2 __result
, _Distance __step_size
,
3312 const _Distance __two_step
= 2 * __step_size
;
3314 while (__last
- __first
>= __two_step
)
3316 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3317 __first
+ __step_size
,
3318 __first
+ __two_step
,
3320 __first
+= __two_step
;
3322 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3324 std::__move_merge(__first
,__first
+ __step_size
,
3325 __first
+ __step_size
, __last
, __result
, __comp
);
3328 template<typename _RandomAccessIterator
, typename _Distance
>
3330 __chunk_insertion_sort(_RandomAccessIterator __first
,
3331 _RandomAccessIterator __last
,
3332 _Distance __chunk_size
)
3334 while (__last
- __first
>= __chunk_size
)
3336 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3337 __first
+= __chunk_size
;
3339 std::__insertion_sort(__first
, __last
);
3342 template<typename _RandomAccessIterator
, typename _Distance
,
3345 __chunk_insertion_sort(_RandomAccessIterator __first
,
3346 _RandomAccessIterator __last
,
3347 _Distance __chunk_size
, _Compare __comp
)
3349 while (__last
- __first
>= __chunk_size
)
3351 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3352 __first
+= __chunk_size
;
3354 std::__insertion_sort(__first
, __last
, __comp
);
3357 enum { _S_chunk_size
= 7 };
3359 template<typename _RandomAccessIterator
, typename _Pointer
>
3361 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3362 _RandomAccessIterator __last
,
3365 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3368 const _Distance __len
= __last
- __first
;
3369 const _Pointer __buffer_last
= __buffer
+ __len
;
3371 _Distance __step_size
= _S_chunk_size
;
3372 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3374 while (__step_size
< __len
)
3376 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3378 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3383 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3385 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3386 _RandomAccessIterator __last
,
3387 _Pointer __buffer
, _Compare __comp
)
3389 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3392 const _Distance __len
= __last
- __first
;
3393 const _Pointer __buffer_last
= __buffer
+ __len
;
3395 _Distance __step_size
= _S_chunk_size
;
3396 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3398 while (__step_size
< __len
)
3400 std::__merge_sort_loop(__first
, __last
, __buffer
,
3401 __step_size
, __comp
);
3403 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3404 __step_size
, __comp
);
3409 template<typename _RandomAccessIterator
, typename _Pointer
,
3412 __stable_sort_adaptive(_RandomAccessIterator __first
,
3413 _RandomAccessIterator __last
,
3414 _Pointer __buffer
, _Distance __buffer_size
)
3416 const _Distance __len
= (__last
- __first
+ 1) / 2;
3417 const _RandomAccessIterator __middle
= __first
+ __len
;
3418 if (__len
> __buffer_size
)
3420 std::__stable_sort_adaptive(__first
, __middle
,
3421 __buffer
, __buffer_size
);
3422 std::__stable_sort_adaptive(__middle
, __last
,
3423 __buffer
, __buffer_size
);
3427 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3428 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3430 std::__merge_adaptive(__first
, __middle
, __last
,
3431 _Distance(__middle
- __first
),
3432 _Distance(__last
- __middle
),
3433 __buffer
, __buffer_size
);
3436 template<typename _RandomAccessIterator
, typename _Pointer
,
3437 typename _Distance
, typename _Compare
>
3439 __stable_sort_adaptive(_RandomAccessIterator __first
,
3440 _RandomAccessIterator __last
,
3441 _Pointer __buffer
, _Distance __buffer_size
,
3444 const _Distance __len
= (__last
- __first
+ 1) / 2;
3445 const _RandomAccessIterator __middle
= __first
+ __len
;
3446 if (__len
> __buffer_size
)
3448 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3449 __buffer_size
, __comp
);
3450 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3451 __buffer_size
, __comp
);
3455 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3456 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3458 std::__merge_adaptive(__first
, __middle
, __last
,
3459 _Distance(__middle
- __first
),
3460 _Distance(__last
- __middle
),
3461 __buffer
, __buffer_size
,
3465 /// This is a helper function for the stable sorting routines.
3466 template<typename _RandomAccessIterator
>
3468 __inplace_stable_sort(_RandomAccessIterator __first
,
3469 _RandomAccessIterator __last
)
3471 if (__last
- __first
< 15)
3473 std::__insertion_sort(__first
, __last
);
3476 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3477 std::__inplace_stable_sort(__first
, __middle
);
3478 std::__inplace_stable_sort(__middle
, __last
);
3479 std::__merge_without_buffer(__first
, __middle
, __last
,
3484 /// This is a helper function for the stable sorting routines.
3485 template<typename _RandomAccessIterator
, typename _Compare
>
3487 __inplace_stable_sort(_RandomAccessIterator __first
,
3488 _RandomAccessIterator __last
, _Compare __comp
)
3490 if (__last
- __first
< 15)
3492 std::__insertion_sort(__first
, __last
, __comp
);
3495 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3496 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3497 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3498 std::__merge_without_buffer(__first
, __middle
, __last
,
3506 // Set algorithms: includes, set_union, set_intersection, set_difference,
3507 // set_symmetric_difference. All of these algorithms have the precondition
3508 // that their input ranges are sorted and the postcondition that their output
3509 // ranges are sorted.
3512 * @brief Determines whether all elements of a sequence exists in a range.
3513 * @param __first1 Start of search range.
3514 * @param __last1 End of search range.
3515 * @param __first2 Start of sequence
3516 * @param __last2 End of sequence.
3517 * @return True if each element in [__first2,__last2) is contained in order
3518 * within [__first1,__last1). False otherwise.
3519 * @ingroup set_algorithms
3521 * This operation expects both [__first1,__last1) and
3522 * [__first2,__last2) to be sorted. Searches for the presence of
3523 * each element in [__first2,__last2) within [__first1,__last1).
3524 * The iterators over each range only move forward, so this is a
3525 * linear algorithm. If an element in [__first2,__last2) is not
3526 * found before the search iterator reaches @a __last2, false is
3529 template<typename _InputIterator1
, typename _InputIterator2
>
3531 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3532 _InputIterator2 __first2
, _InputIterator2 __last2
)
3534 typedef typename iterator_traits
<_InputIterator1
>::value_type
3536 typedef typename iterator_traits
<_InputIterator2
>::value_type
3539 // concept requirements
3540 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3541 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3542 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3543 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3544 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3545 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3547 while (__first1
!= __last1
&& __first2
!= __last2
)
3548 if (*__first2
< *__first1
)
3550 else if(*__first1
< *__first2
)
3553 ++__first1
, ++__first2
;
3555 return __first2
== __last2
;
3559 * @brief Determines whether all elements of a sequence exists in a range
3561 * @ingroup set_algorithms
3562 * @param __first1 Start of search range.
3563 * @param __last1 End of search range.
3564 * @param __first2 Start of sequence
3565 * @param __last2 End of sequence.
3566 * @param __comp Comparison function to use.
3567 * @return True if each element in [__first2,__last2) is contained
3568 * in order within [__first1,__last1) according to comp. False
3569 * otherwise. @ingroup set_algorithms
3571 * This operation expects both [__first1,__last1) and
3572 * [__first2,__last2) to be sorted. Searches for the presence of
3573 * each element in [__first2,__last2) within [__first1,__last1),
3574 * using comp to decide. The iterators over each range only move
3575 * forward, so this is a linear algorithm. If an element in
3576 * [__first2,__last2) is not found before the search iterator
3577 * reaches @a __last2, false is returned.
3579 template<typename _InputIterator1
, typename _InputIterator2
,
3582 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3583 _InputIterator2 __first2
, _InputIterator2 __last2
,
3586 typedef typename iterator_traits
<_InputIterator1
>::value_type
3588 typedef typename iterator_traits
<_InputIterator2
>::value_type
3591 // concept requirements
3592 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3593 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3594 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3595 _ValueType1
, _ValueType2
>)
3596 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3597 _ValueType2
, _ValueType1
>)
3598 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3599 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3601 while (__first1
!= __last1
&& __first2
!= __last2
)
3602 if (__comp(*__first2
, *__first1
))
3604 else if(__comp(*__first1
, *__first2
))
3607 ++__first1
, ++__first2
;
3609 return __first2
== __last2
;
3618 // set_symmetric_difference
3623 * @brief Permute range into the next @a dictionary ordering.
3624 * @ingroup sorting_algorithms
3625 * @param __first Start of range.
3626 * @param __last End of range.
3627 * @return False if wrapped to first permutation, true otherwise.
3629 * Treats all permutations of the range as a set of @a dictionary sorted
3630 * sequences. Permutes the current sequence into the next one of this set.
3631 * Returns true if there are more sequences to generate. If the sequence
3632 * is the largest of the set, the smallest is generated and false returned.
3634 template<typename _BidirectionalIterator
>
3636 next_permutation(_BidirectionalIterator __first
,
3637 _BidirectionalIterator __last
)
3639 // concept requirements
3640 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3641 _BidirectionalIterator
>)
3642 __glibcxx_function_requires(_LessThanComparableConcept
<
3643 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3644 __glibcxx_requires_valid_range(__first
, __last
);
3646 if (__first
== __last
)
3648 _BidirectionalIterator __i
= __first
;
3657 _BidirectionalIterator __ii
= __i
;
3661 _BidirectionalIterator __j
= __last
;
3662 while (!(*__i
< *--__j
))
3664 std::iter_swap(__i
, __j
);
3665 std::reverse(__ii
, __last
);
3670 std::reverse(__first
, __last
);
3677 * @brief Permute range into the next @a dictionary ordering using
3678 * comparison functor.
3679 * @ingroup sorting_algorithms
3680 * @param __first Start of range.
3681 * @param __last End of range.
3682 * @param __comp A comparison functor.
3683 * @return False if wrapped to first permutation, true otherwise.
3685 * Treats all permutations of the range [__first,__last) as a set of
3686 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3687 * sequence into the next one of this set. Returns true if there are more
3688 * sequences to generate. If the sequence is the largest of the set, the
3689 * smallest is generated and false returned.
3691 template<typename _BidirectionalIterator
, typename _Compare
>
3693 next_permutation(_BidirectionalIterator __first
,
3694 _BidirectionalIterator __last
, _Compare __comp
)
3696 // concept requirements
3697 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3698 _BidirectionalIterator
>)
3699 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3700 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3701 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3702 __glibcxx_requires_valid_range(__first
, __last
);
3704 if (__first
== __last
)
3706 _BidirectionalIterator __i
= __first
;
3715 _BidirectionalIterator __ii
= __i
;
3717 if (__comp(*__i
, *__ii
))
3719 _BidirectionalIterator __j
= __last
;
3720 while (!bool(__comp(*__i
, *--__j
)))
3722 std::iter_swap(__i
, __j
);
3723 std::reverse(__ii
, __last
);
3728 std::reverse(__first
, __last
);
3735 * @brief Permute range into the previous @a dictionary ordering.
3736 * @ingroup sorting_algorithms
3737 * @param __first Start of range.
3738 * @param __last End of range.
3739 * @return False if wrapped to last permutation, true otherwise.
3741 * Treats all permutations of the range as a set of @a dictionary sorted
3742 * sequences. Permutes the current sequence into the previous one of this
3743 * set. Returns true if there are more sequences to generate. If the
3744 * sequence is the smallest of the set, the largest is generated and false
3747 template<typename _BidirectionalIterator
>
3749 prev_permutation(_BidirectionalIterator __first
,
3750 _BidirectionalIterator __last
)
3752 // concept requirements
3753 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3754 _BidirectionalIterator
>)
3755 __glibcxx_function_requires(_LessThanComparableConcept
<
3756 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3757 __glibcxx_requires_valid_range(__first
, __last
);
3759 if (__first
== __last
)
3761 _BidirectionalIterator __i
= __first
;
3770 _BidirectionalIterator __ii
= __i
;
3774 _BidirectionalIterator __j
= __last
;
3775 while (!(*--__j
< *__i
))
3777 std::iter_swap(__i
, __j
);
3778 std::reverse(__ii
, __last
);
3783 std::reverse(__first
, __last
);
3790 * @brief Permute range into the previous @a dictionary ordering using
3791 * comparison functor.
3792 * @ingroup sorting_algorithms
3793 * @param __first Start of range.
3794 * @param __last End of range.
3795 * @param __comp A comparison functor.
3796 * @return False if wrapped to last permutation, true otherwise.
3798 * Treats all permutations of the range [__first,__last) as a set of
3799 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3800 * sequence into the previous one of this set. Returns true if there are
3801 * more sequences to generate. If the sequence is the smallest of the set,
3802 * the largest is generated and false returned.
3804 template<typename _BidirectionalIterator
, typename _Compare
>
3806 prev_permutation(_BidirectionalIterator __first
,
3807 _BidirectionalIterator __last
, _Compare __comp
)
3809 // concept requirements
3810 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3811 _BidirectionalIterator
>)
3812 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3813 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3814 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3815 __glibcxx_requires_valid_range(__first
, __last
);
3817 if (__first
== __last
)
3819 _BidirectionalIterator __i
= __first
;
3828 _BidirectionalIterator __ii
= __i
;
3830 if (__comp(*__ii
, *__i
))
3832 _BidirectionalIterator __j
= __last
;
3833 while (!bool(__comp(*--__j
, *__i
)))
3835 std::iter_swap(__i
, __j
);
3836 std::reverse(__ii
, __last
);
3841 std::reverse(__first
, __last
);
3851 * @brief Copy a sequence, replacing each element of one value with another
3853 * @param __first An input iterator.
3854 * @param __last An input iterator.
3855 * @param __result An output iterator.
3856 * @param __old_value The value to be replaced.
3857 * @param __new_value The replacement value.
3858 * @return The end of the output sequence, @p result+(last-first).
3860 * Copies each element in the input range @p [__first,__last) to the
3861 * output range @p [__result,__result+(__last-__first)) replacing elements
3862 * equal to @p __old_value with @p __new_value.
3864 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3866 replace_copy(_InputIterator __first
, _InputIterator __last
,
3867 _OutputIterator __result
,
3868 const _Tp
& __old_value
, const _Tp
& __new_value
)
3870 // concept requirements
3871 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3872 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3873 typename iterator_traits
<_InputIterator
>::value_type
>)
3874 __glibcxx_function_requires(_EqualOpConcept
<
3875 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3876 __glibcxx_requires_valid_range(__first
, __last
);
3878 for (; __first
!= __last
; ++__first
, ++__result
)
3879 if (*__first
== __old_value
)
3880 *__result
= __new_value
;
3882 *__result
= *__first
;
3887 * @brief Copy a sequence, replacing each value for which a predicate
3888 * returns true with another value.
3889 * @ingroup mutating_algorithms
3890 * @param __first An input iterator.
3891 * @param __last An input iterator.
3892 * @param __result An output iterator.
3893 * @param __pred A predicate.
3894 * @param __new_value The replacement value.
3895 * @return The end of the output sequence, @p __result+(__last-__first).
3897 * Copies each element in the range @p [__first,__last) to the range
3898 * @p [__result,__result+(__last-__first)) replacing elements for which
3899 * @p __pred returns true with @p __new_value.
3901 template<typename _InputIterator
, typename _OutputIterator
,
3902 typename _Predicate
, typename _Tp
>
3904 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3905 _OutputIterator __result
,
3906 _Predicate __pred
, const _Tp
& __new_value
)
3908 // concept requirements
3909 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3910 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3911 typename iterator_traits
<_InputIterator
>::value_type
>)
3912 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3913 typename iterator_traits
<_InputIterator
>::value_type
>)
3914 __glibcxx_requires_valid_range(__first
, __last
);
3916 for (; __first
!= __last
; ++__first
, ++__result
)
3917 if (__pred(*__first
))
3918 *__result
= __new_value
;
3920 *__result
= *__first
;
3924 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3926 * @brief Determines whether the elements of a sequence are sorted.
3927 * @ingroup sorting_algorithms
3928 * @param __first An iterator.
3929 * @param __last Another iterator.
3930 * @return True if the elements are sorted, false otherwise.
3932 template<typename _ForwardIterator
>
3934 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3935 { return std::is_sorted_until(__first
, __last
) == __last
; }
3938 * @brief Determines whether the elements of a sequence are sorted
3939 * according to a comparison functor.
3940 * @ingroup sorting_algorithms
3941 * @param __first An iterator.
3942 * @param __last Another iterator.
3943 * @param __comp A comparison functor.
3944 * @return True if the elements are sorted, false otherwise.
3946 template<typename _ForwardIterator
, typename _Compare
>
3948 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3950 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3953 * @brief Determines the end of a sorted sequence.
3954 * @ingroup sorting_algorithms
3955 * @param __first An iterator.
3956 * @param __last Another iterator.
3957 * @return An iterator pointing to the last iterator i in [__first, __last)
3958 * for which the range [__first, i) is sorted.
3960 template<typename _ForwardIterator
>
3962 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3964 // concept requirements
3965 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3966 __glibcxx_function_requires(_LessThanComparableConcept
<
3967 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3968 __glibcxx_requires_valid_range(__first
, __last
);
3970 if (__first
== __last
)
3973 _ForwardIterator __next
= __first
;
3974 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3975 if (*__next
< *__first
)
3981 * @brief Determines the end of a sorted sequence using comparison functor.
3982 * @ingroup sorting_algorithms
3983 * @param __first An iterator.
3984 * @param __last Another iterator.
3985 * @param __comp A comparison functor.
3986 * @return An iterator pointing to the last iterator i in [__first, __last)
3987 * for which the range [__first, i) is sorted.
3989 template<typename _ForwardIterator
, typename _Compare
>
3991 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3994 // concept requirements
3995 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3996 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3997 typename iterator_traits
<_ForwardIterator
>::value_type
,
3998 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3999 __glibcxx_requires_valid_range(__first
, __last
);
4001 if (__first
== __last
)
4004 _ForwardIterator __next
= __first
;
4005 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
4006 if (__comp(*__next
, *__first
))
4012 * @brief Determines min and max at once as an ordered pair.
4013 * @ingroup sorting_algorithms
4014 * @param __a A thing of arbitrary type.
4015 * @param __b Another thing of arbitrary type.
4016 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4019 template<typename _Tp
>
4020 inline pair
<const _Tp
&, const _Tp
&>
4021 minmax(const _Tp
& __a
, const _Tp
& __b
)
4023 // concept requirements
4024 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
4026 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4027 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4031 * @brief Determines min and max at once as an ordered pair.
4032 * @ingroup sorting_algorithms
4033 * @param __a A thing of arbitrary type.
4034 * @param __b Another thing of arbitrary type.
4035 * @param __comp A @link comparison_functor comparison functor @endlink.
4036 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4039 template<typename _Tp
, typename _Compare
>
4040 inline pair
<const _Tp
&, const _Tp
&>
4041 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
4043 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4044 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4048 * @brief Return a pair of iterators pointing to the minimum and maximum
4049 * elements in a range.
4050 * @ingroup sorting_algorithms
4051 * @param __first Start of range.
4052 * @param __last End of range.
4053 * @return make_pair(m, M), where m is the first iterator i in
4054 * [__first, __last) such that no other element in the range is
4055 * smaller, and where M is the last iterator i in [__first, __last)
4056 * such that no other element in the range is larger.
4058 template<typename _ForwardIterator
>
4059 pair
<_ForwardIterator
, _ForwardIterator
>
4060 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4062 // concept requirements
4063 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4064 __glibcxx_function_requires(_LessThanComparableConcept
<
4065 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4066 __glibcxx_requires_valid_range(__first
, __last
);
4068 _ForwardIterator __next
= __first
;
4069 if (__first
== __last
4070 || ++__next
== __last
)
4071 return std::make_pair(__first
, __first
);
4073 _ForwardIterator __min
, __max
;
4074 if (*__next
< *__first
)
4088 while (__first
!= __last
)
4091 if (++__next
== __last
)
4093 if (*__first
< *__min
)
4095 else if (!(*__first
< *__max
))
4100 if (*__next
< *__first
)
4102 if (*__next
< *__min
)
4104 if (!(*__first
< *__max
))
4109 if (*__first
< *__min
)
4111 if (!(*__next
< *__max
))
4119 return std::make_pair(__min
, __max
);
4123 * @brief Return a pair of iterators pointing to the minimum and maximum
4124 * elements in a range.
4125 * @ingroup sorting_algorithms
4126 * @param __first Start of range.
4127 * @param __last End of range.
4128 * @param __comp Comparison functor.
4129 * @return make_pair(m, M), where m is the first iterator i in
4130 * [__first, __last) such that no other element in the range is
4131 * smaller, and where M is the last iterator i in [__first, __last)
4132 * such that no other element in the range is larger.
4134 template<typename _ForwardIterator
, typename _Compare
>
4135 pair
<_ForwardIterator
, _ForwardIterator
>
4136 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4139 // concept requirements
4140 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4141 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4142 typename iterator_traits
<_ForwardIterator
>::value_type
,
4143 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4144 __glibcxx_requires_valid_range(__first
, __last
);
4146 _ForwardIterator __next
= __first
;
4147 if (__first
== __last
4148 || ++__next
== __last
)
4149 return std::make_pair(__first
, __first
);
4151 _ForwardIterator __min
, __max
;
4152 if (__comp(*__next
, *__first
))
4166 while (__first
!= __last
)
4169 if (++__next
== __last
)
4171 if (__comp(*__first
, *__min
))
4173 else if (!__comp(*__first
, *__max
))
4178 if (__comp(*__next
, *__first
))
4180 if (__comp(*__next
, *__min
))
4182 if (!__comp(*__first
, *__max
))
4187 if (__comp(*__first
, *__min
))
4189 if (!__comp(*__next
, *__max
))
4197 return std::make_pair(__min
, __max
);
4201 template<typename _Tp
>
4203 min(initializer_list
<_Tp
> __l
)
4204 { return *std::min_element(__l
.begin(), __l
.end()); }
4206 template<typename _Tp
, typename _Compare
>
4208 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4209 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4211 template<typename _Tp
>
4213 max(initializer_list
<_Tp
> __l
)
4214 { return *std::max_element(__l
.begin(), __l
.end()); }
4216 template<typename _Tp
, typename _Compare
>
4218 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4219 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4221 template<typename _Tp
>
4222 inline pair
<_Tp
, _Tp
>
4223 minmax(initializer_list
<_Tp
> __l
)
4225 pair
<const _Tp
*, const _Tp
*> __p
=
4226 std::minmax_element(__l
.begin(), __l
.end());
4227 return std::make_pair(*__p
.first
, *__p
.second
);
4230 template<typename _Tp
, typename _Compare
>
4231 inline pair
<_Tp
, _Tp
>
4232 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4234 pair
<const _Tp
*, const _Tp
*> __p
=
4235 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4236 return std::make_pair(*__p
.first
, *__p
.second
);
4240 * @brief Checks whether a permutaion of the second sequence is equal
4241 * to the first sequence.
4242 * @ingroup non_mutating_algorithms
4243 * @param __first1 Start of first range.
4244 * @param __last1 End of first range.
4245 * @param __first2 Start of second range.
4246 * @return true if there exists a permutation of the elements in the range
4247 * [__first2, __first2 + (__last1 - __first1)), beginning with
4248 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
4249 * returns true; otherwise, returns false.
4251 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4253 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4254 _ForwardIterator2 __first2
)
4256 // Efficiently compare identical prefixes: O(N) if sequences
4257 // have the same elements in the same order.
4258 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4259 if (!(*__first1
== *__first2
))
4262 if (__first1
== __last1
)
4265 // Establish __last2 assuming equal ranges by iterating over the
4266 // rest of the list.
4267 _ForwardIterator2 __last2
= __first2
;
4268 std::advance(__last2
, std::distance(__first1
, __last1
));
4269 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4271 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4272 continue; // We've seen this one before.
4274 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4276 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4283 * @brief Checks whether a permutation of the second sequence is equal
4284 * to the first sequence.
4285 * @ingroup non_mutating_algorithms
4286 * @param __first1 Start of first range.
4287 * @param __last1 End of first range.
4288 * @param __first2 Start of second range.
4289 * @param __pred A binary predicate.
4290 * @return true if there exists a permutation of the elements in
4291 * the range [__first2, __first2 + (__last1 - __first1)),
4292 * beginning with ForwardIterator2 begin, such that
4293 * equal(__first1, __last1, __begin, __pred) returns true;
4294 * otherwise, returns false.
4296 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4297 typename _BinaryPredicate
>
4299 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4300 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
4302 // Efficiently compare identical prefixes: O(N) if sequences
4303 // have the same elements in the same order.
4304 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4305 if (!bool(__pred(*__first1
, *__first2
)))
4308 if (__first1
== __last1
)
4311 // Establish __last2 assuming equal ranges by iterating over the
4312 // rest of the list.
4313 _ForwardIterator2 __last2
= __first2
;
4314 std::advance(__last2
, std::distance(__first1
, __last1
));
4315 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4317 using std::placeholders::_1
;
4319 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4320 std::bind(__pred
, _1
, *__scan
)))
4321 continue; // We've seen this one before.
4323 auto __matches
= std::count_if(__first2
, __last2
,
4324 std::bind(__pred
, _1
, *__scan
));
4326 || std::count_if(__scan
, __last1
,
4327 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4333 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4335 * @brief Shuffle the elements of a sequence using a uniform random
4337 * @ingroup mutating_algorithms
4338 * @param __first A forward iterator.
4339 * @param __last A forward iterator.
4340 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
4343 * Reorders the elements in the range @p [__first,__last) using @p __g to
4344 * provide random numbers.
4346 template<typename _RandomAccessIterator
,
4347 typename _UniformRandomNumberGenerator
>
4349 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4350 _UniformRandomNumberGenerator
&& __g
)
4352 // concept requirements
4353 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4354 _RandomAccessIterator
>)
4355 __glibcxx_requires_valid_range(__first
, __last
);
4357 if (__first
== __last
)
4360 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4363 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
4364 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
4365 typedef typename
__distr_type::param_type __p_type
;
4368 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4369 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
4373 #endif // __GXX_EXPERIMENTAL_CXX0X__
4375 _GLIBCXX_END_NAMESPACE_VERSION
4377 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4380 * @brief Apply a function to every element of a sequence.
4381 * @ingroup non_mutating_algorithms
4382 * @param __first An input iterator.
4383 * @param __last An input iterator.
4384 * @param __f A unary function object.
4385 * @return @p __f (std::move(@p __f) in C++0x).
4387 * Applies the function object @p __f to each element in the range
4388 * @p [first,last). @p __f must not modify the order of the sequence.
4389 * If @p __f has a return value it is ignored.
4391 template<typename _InputIterator
, typename _Function
>
4393 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4395 // concept requirements
4396 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4397 __glibcxx_requires_valid_range(__first
, __last
);
4398 for (; __first
!= __last
; ++__first
)
4400 return _GLIBCXX_MOVE(__f
);
4404 * @brief Find the first occurrence of a value in a sequence.
4405 * @ingroup non_mutating_algorithms
4406 * @param __first An input iterator.
4407 * @param __last An input iterator.
4408 * @param __val The value to find.
4409 * @return The first iterator @c i in the range @p [__first,__last)
4410 * such that @c *i == @p __val, or @p __last if no such iterator exists.
4412 template<typename _InputIterator
, typename _Tp
>
4413 inline _InputIterator
4414 find(_InputIterator __first
, _InputIterator __last
,
4417 // concept requirements
4418 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4419 __glibcxx_function_requires(_EqualOpConcept
<
4420 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4421 __glibcxx_requires_valid_range(__first
, __last
);
4422 return std::__find(__first
, __last
, __val
,
4423 std::__iterator_category(__first
));
4427 * @brief Find the first element in a sequence for which a
4428 * predicate is true.
4429 * @ingroup non_mutating_algorithms
4430 * @param __first An input iterator.
4431 * @param __last An input iterator.
4432 * @param __pred A predicate.
4433 * @return The first iterator @c i in the range @p [__first,__last)
4434 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
4436 template<typename _InputIterator
, typename _Predicate
>
4437 inline _InputIterator
4438 find_if(_InputIterator __first
, _InputIterator __last
,
4441 // concept requirements
4442 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4443 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4444 typename iterator_traits
<_InputIterator
>::value_type
>)
4445 __glibcxx_requires_valid_range(__first
, __last
);
4446 return std::__find_if(__first
, __last
, __pred
,
4447 std::__iterator_category(__first
));
4451 * @brief Find element from a set in a sequence.
4452 * @ingroup non_mutating_algorithms
4453 * @param __first1 Start of range to search.
4454 * @param __last1 End of range to search.
4455 * @param __first2 Start of match candidates.
4456 * @param __last2 End of match candidates.
4457 * @return The first iterator @c i in the range
4458 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
4459 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
4461 * Searches the range @p [__first1,__last1) for an element that is
4462 * equal to some element in the range [__first2,__last2). If
4463 * found, returns an iterator in the range [__first1,__last1),
4464 * otherwise returns @p __last1.
4466 template<typename _InputIterator
, typename _ForwardIterator
>
4468 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4469 _ForwardIterator __first2
, _ForwardIterator __last2
)
4471 // concept requirements
4472 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4473 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4474 __glibcxx_function_requires(_EqualOpConcept
<
4475 typename iterator_traits
<_InputIterator
>::value_type
,
4476 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4477 __glibcxx_requires_valid_range(__first1
, __last1
);
4478 __glibcxx_requires_valid_range(__first2
, __last2
);
4480 for (; __first1
!= __last1
; ++__first1
)
4481 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4482 if (*__first1
== *__iter
)
4488 * @brief Find element from a set in a sequence using a predicate.
4489 * @ingroup non_mutating_algorithms
4490 * @param __first1 Start of range to search.
4491 * @param __last1 End of range to search.
4492 * @param __first2 Start of match candidates.
4493 * @param __last2 End of match candidates.
4494 * @param __comp Predicate to use.
4495 * @return The first iterator @c i in the range
4496 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4497 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4498 * such iterator exists.
4501 * Searches the range @p [__first1,__last1) for an element that is
4502 * equal to some element in the range [__first2,__last2). If
4503 * found, returns an iterator in the range [__first1,__last1),
4504 * otherwise returns @p __last1.
4506 template<typename _InputIterator
, typename _ForwardIterator
,
4507 typename _BinaryPredicate
>
4509 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4510 _ForwardIterator __first2
, _ForwardIterator __last2
,
4511 _BinaryPredicate __comp
)
4513 // concept requirements
4514 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4515 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4516 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4517 typename iterator_traits
<_InputIterator
>::value_type
,
4518 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4519 __glibcxx_requires_valid_range(__first1
, __last1
);
4520 __glibcxx_requires_valid_range(__first2
, __last2
);
4522 for (; __first1
!= __last1
; ++__first1
)
4523 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4524 if (__comp(*__first1
, *__iter
))
4530 * @brief Find two adjacent values in a sequence that are equal.
4531 * @ingroup non_mutating_algorithms
4532 * @param __first A forward iterator.
4533 * @param __last A forward iterator.
4534 * @return The first iterator @c i such that @c i and @c i+1 are both
4535 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4536 * or @p __last if no such iterator exists.
4538 template<typename _ForwardIterator
>
4540 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4542 // concept requirements
4543 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4544 __glibcxx_function_requires(_EqualityComparableConcept
<
4545 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4546 __glibcxx_requires_valid_range(__first
, __last
);
4547 if (__first
== __last
)
4549 _ForwardIterator __next
= __first
;
4550 while(++__next
!= __last
)
4552 if (*__first
== *__next
)
4560 * @brief Find two adjacent values in a sequence using a predicate.
4561 * @ingroup non_mutating_algorithms
4562 * @param __first A forward iterator.
4563 * @param __last A forward iterator.
4564 * @param __binary_pred A binary predicate.
4565 * @return The first iterator @c i such that @c i and @c i+1 are both
4566 * valid iterators in @p [__first,__last) and such that
4567 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4570 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4572 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4573 _BinaryPredicate __binary_pred
)
4575 // concept requirements
4576 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4577 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4578 typename iterator_traits
<_ForwardIterator
>::value_type
,
4579 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4580 __glibcxx_requires_valid_range(__first
, __last
);
4581 if (__first
== __last
)
4583 _ForwardIterator __next
= __first
;
4584 while(++__next
!= __last
)
4586 if (__binary_pred(*__first
, *__next
))
4594 * @brief Count the number of copies of a value in a sequence.
4595 * @ingroup non_mutating_algorithms
4596 * @param __first An input iterator.
4597 * @param __last An input iterator.
4598 * @param __value The value to be counted.
4599 * @return The number of iterators @c i in the range @p [__first,__last)
4600 * for which @c *i == @p __value
4602 template<typename _InputIterator
, typename _Tp
>
4603 typename iterator_traits
<_InputIterator
>::difference_type
4604 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4606 // concept requirements
4607 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4608 __glibcxx_function_requires(_EqualOpConcept
<
4609 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4610 __glibcxx_requires_valid_range(__first
, __last
);
4611 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4612 for (; __first
!= __last
; ++__first
)
4613 if (*__first
== __value
)
4619 * @brief Count the elements of a sequence for which a predicate is true.
4620 * @ingroup non_mutating_algorithms
4621 * @param __first An input iterator.
4622 * @param __last An input iterator.
4623 * @param __pred A predicate.
4624 * @return The number of iterators @c i in the range @p [__first,__last)
4625 * for which @p __pred(*i) is true.
4627 template<typename _InputIterator
, typename _Predicate
>
4628 typename iterator_traits
<_InputIterator
>::difference_type
4629 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4631 // concept requirements
4632 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4633 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4634 typename iterator_traits
<_InputIterator
>::value_type
>)
4635 __glibcxx_requires_valid_range(__first
, __last
);
4636 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4637 for (; __first
!= __last
; ++__first
)
4638 if (__pred(*__first
))
4644 * @brief Search a sequence for a matching sub-sequence.
4645 * @ingroup non_mutating_algorithms
4646 * @param __first1 A forward iterator.
4647 * @param __last1 A forward iterator.
4648 * @param __first2 A forward iterator.
4649 * @param __last2 A forward iterator.
4650 * @return The first iterator @c i in the range @p
4651 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4652 * *(__first2+N) for each @c N in the range @p
4653 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4655 * Searches the range @p [__first1,__last1) for a sub-sequence that
4656 * compares equal value-by-value with the sequence given by @p
4657 * [__first2,__last2) and returns an iterator to the first element
4658 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4661 * Because the sub-sequence must lie completely within the range @p
4662 * [__first1,__last1) it must start at a position less than @p
4663 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4664 * length of the sub-sequence.
4666 * This means that the returned iterator @c i will be in the range
4667 * @p [__first1,__last1-(__last2-__first2))
4669 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4671 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4672 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4674 // concept requirements
4675 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4676 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4677 __glibcxx_function_requires(_EqualOpConcept
<
4678 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4679 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4680 __glibcxx_requires_valid_range(__first1
, __last1
);
4681 __glibcxx_requires_valid_range(__first2
, __last2
);
4683 // Test for empty ranges
4684 if (__first1
== __last1
|| __first2
== __last2
)
4687 // Test for a pattern of length 1.
4688 _ForwardIterator2
__p1(__first2
);
4689 if (++__p1
== __last2
)
4690 return _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4693 _ForwardIterator2 __p
;
4694 _ForwardIterator1 __current
= __first1
;
4698 __first1
= _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4699 if (__first1
== __last1
)
4703 __current
= __first1
;
4704 if (++__current
== __last1
)
4707 while (*__current
== *__p
)
4709 if (++__p
== __last2
)
4711 if (++__current
== __last1
)
4720 * @brief Search a sequence for a matching sub-sequence using a predicate.
4721 * @ingroup non_mutating_algorithms
4722 * @param __first1 A forward iterator.
4723 * @param __last1 A forward iterator.
4724 * @param __first2 A forward iterator.
4725 * @param __last2 A forward iterator.
4726 * @param __predicate A binary predicate.
4727 * @return The first iterator @c i in the range
4728 * @p [__first1,__last1-(__last2-__first2)) such that
4729 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4730 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4732 * Searches the range @p [__first1,__last1) for a sub-sequence that
4733 * compares equal value-by-value with the sequence given by @p
4734 * [__first2,__last2), using @p __predicate to determine equality,
4735 * and returns an iterator to the first element of the
4736 * sub-sequence, or @p __last1 if no such iterator exists.
4738 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4740 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4741 typename _BinaryPredicate
>
4743 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4744 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4745 _BinaryPredicate __predicate
)
4747 // concept requirements
4748 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4749 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4750 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4751 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4752 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4753 __glibcxx_requires_valid_range(__first1
, __last1
);
4754 __glibcxx_requires_valid_range(__first2
, __last2
);
4756 // Test for empty ranges
4757 if (__first1
== __last1
|| __first2
== __last2
)
4760 // Test for a pattern of length 1.
4761 _ForwardIterator2
__p1(__first2
);
4762 if (++__p1
== __last2
)
4764 while (__first1
!= __last1
4765 && !bool(__predicate(*__first1
, *__first2
)))
4771 _ForwardIterator2 __p
;
4772 _ForwardIterator1 __current
= __first1
;
4776 while (__first1
!= __last1
4777 && !bool(__predicate(*__first1
, *__first2
)))
4779 if (__first1
== __last1
)
4783 __current
= __first1
;
4784 if (++__current
== __last1
)
4787 while (__predicate(*__current
, *__p
))
4789 if (++__p
== __last2
)
4791 if (++__current
== __last1
)
4801 * @brief Search a sequence for a number of consecutive values.
4802 * @ingroup non_mutating_algorithms
4803 * @param __first A forward iterator.
4804 * @param __last A forward iterator.
4805 * @param __count The number of consecutive values.
4806 * @param __val The value to find.
4807 * @return The first iterator @c i in the range @p
4808 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4809 * each @c N in the range @p [0,__count), or @p __last if no such
4812 * Searches the range @p [__first,__last) for @p count consecutive elements
4813 * equal to @p __val.
4815 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4817 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4818 _Integer __count
, const _Tp
& __val
)
4820 // concept requirements
4821 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4822 __glibcxx_function_requires(_EqualOpConcept
<
4823 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4824 __glibcxx_requires_valid_range(__first
, __last
);
4829 return _GLIBCXX_STD_A::find(__first
, __last
, __val
);
4830 return std::__search_n(__first
, __last
, __count
, __val
,
4831 std::__iterator_category(__first
));
4836 * @brief Search a sequence for a number of consecutive values using a
4838 * @ingroup non_mutating_algorithms
4839 * @param __first A forward iterator.
4840 * @param __last A forward iterator.
4841 * @param __count The number of consecutive values.
4842 * @param __val The value to find.
4843 * @param __binary_pred A binary predicate.
4844 * @return The first iterator @c i in the range @p
4845 * [__first,__last-__count) such that @p
4846 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4847 * @p [0,__count), or @p __last if no such iterator exists.
4849 * Searches the range @p [__first,__last) for @p __count
4850 * consecutive elements for which the predicate returns true.
4852 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4853 typename _BinaryPredicate
>
4855 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4856 _Integer __count
, const _Tp
& __val
,
4857 _BinaryPredicate __binary_pred
)
4859 // concept requirements
4860 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4861 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4862 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4863 __glibcxx_requires_valid_range(__first
, __last
);
4869 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4873 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4874 std::__iterator_category(__first
));
4879 * @brief Perform an operation on a sequence.
4880 * @ingroup mutating_algorithms
4881 * @param __first An input iterator.
4882 * @param __last An input iterator.
4883 * @param __result An output iterator.
4884 * @param __unary_op A unary operator.
4885 * @return An output iterator equal to @p __result+(__last-__first).
4887 * Applies the operator to each element in the input range and assigns
4888 * the results to successive elements of the output sequence.
4889 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4890 * range @p [0,__last-__first).
4892 * @p unary_op must not alter its argument.
4894 template<typename _InputIterator
, typename _OutputIterator
,
4895 typename _UnaryOperation
>
4897 transform(_InputIterator __first
, _InputIterator __last
,
4898 _OutputIterator __result
, _UnaryOperation __unary_op
)
4900 // concept requirements
4901 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4902 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4903 // "the type returned by a _UnaryOperation"
4904 __typeof__(__unary_op(*__first
))>)
4905 __glibcxx_requires_valid_range(__first
, __last
);
4907 for (; __first
!= __last
; ++__first
, ++__result
)
4908 *__result
= __unary_op(*__first
);
4913 * @brief Perform an operation on corresponding elements of two sequences.
4914 * @ingroup mutating_algorithms
4915 * @param __first1 An input iterator.
4916 * @param __last1 An input iterator.
4917 * @param __first2 An input iterator.
4918 * @param __result An output iterator.
4919 * @param __binary_op A binary operator.
4920 * @return An output iterator equal to @p result+(last-first).
4922 * Applies the operator to the corresponding elements in the two
4923 * input ranges and assigns the results to successive elements of the
4926 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4927 * @c N in the range @p [0,__last1-__first1).
4929 * @p binary_op must not alter either of its arguments.
4931 template<typename _InputIterator1
, typename _InputIterator2
,
4932 typename _OutputIterator
, typename _BinaryOperation
>
4934 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4935 _InputIterator2 __first2
, _OutputIterator __result
,
4936 _BinaryOperation __binary_op
)
4938 // concept requirements
4939 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4940 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4941 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4942 // "the type returned by a _BinaryOperation"
4943 __typeof__(__binary_op(*__first1
,*__first2
))>)
4944 __glibcxx_requires_valid_range(__first1
, __last1
);
4946 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4947 *__result
= __binary_op(*__first1
, *__first2
);
4952 * @brief Replace each occurrence of one value in a sequence with another
4954 * @ingroup mutating_algorithms
4955 * @param __first A forward iterator.
4956 * @param __last A forward iterator.
4957 * @param __old_value The value to be replaced.
4958 * @param __new_value The replacement value.
4959 * @return replace() returns no value.
4961 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4962 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4964 template<typename _ForwardIterator
, typename _Tp
>
4966 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4967 const _Tp
& __old_value
, const _Tp
& __new_value
)
4969 // concept requirements
4970 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4972 __glibcxx_function_requires(_EqualOpConcept
<
4973 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4974 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4975 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4976 __glibcxx_requires_valid_range(__first
, __last
);
4978 for (; __first
!= __last
; ++__first
)
4979 if (*__first
== __old_value
)
4980 *__first
= __new_value
;
4984 * @brief Replace each value in a sequence for which a predicate returns
4985 * true with another value.
4986 * @ingroup mutating_algorithms
4987 * @param __first A forward iterator.
4988 * @param __last A forward iterator.
4989 * @param __pred A predicate.
4990 * @param __new_value The replacement value.
4991 * @return replace_if() returns no value.
4993 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4994 * is true then the assignment @c *i = @p __new_value is performed.
4996 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4998 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4999 _Predicate __pred
, const _Tp
& __new_value
)
5001 // concept requirements
5002 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5004 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
5005 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5006 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5007 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5008 __glibcxx_requires_valid_range(__first
, __last
);
5010 for (; __first
!= __last
; ++__first
)
5011 if (__pred(*__first
))
5012 *__first
= __new_value
;
5016 * @brief Assign the result of a function object to each value in a
5018 * @ingroup mutating_algorithms
5019 * @param __first A forward iterator.
5020 * @param __last A forward iterator.
5021 * @param __gen A function object taking no arguments and returning
5022 * std::iterator_traits<_ForwardIterator>::value_type
5023 * @return generate() returns no value.
5025 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5026 * @p [__first,__last).
5028 template<typename _ForwardIterator
, typename _Generator
>
5030 generate(_ForwardIterator __first
, _ForwardIterator __last
,
5033 // concept requirements
5034 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5035 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
5036 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5037 __glibcxx_requires_valid_range(__first
, __last
);
5039 for (; __first
!= __last
; ++__first
)
5044 * @brief Assign the result of a function object to each value in a
5046 * @ingroup mutating_algorithms
5047 * @param __first A forward iterator.
5048 * @param __n The length of the sequence.
5049 * @param __gen A function object taking no arguments and returning
5050 * std::iterator_traits<_ForwardIterator>::value_type
5051 * @return The end of the sequence, @p __first+__n
5053 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5054 * @p [__first,__first+__n).
5056 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5057 * DR 865. More algorithms that throw away information
5059 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
5061 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
5063 // concept requirements
5064 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5065 // "the type returned by a _Generator"
5066 __typeof__(__gen())>)
5068 for (__decltype(__n
+ 0) __niter
= __n
;
5069 __niter
> 0; --__niter
, ++__first
)
5076 * @brief Copy a sequence, removing consecutive duplicate values.
5077 * @ingroup mutating_algorithms
5078 * @param __first An input iterator.
5079 * @param __last An input iterator.
5080 * @param __result An output iterator.
5081 * @return An iterator designating the end of the resulting sequence.
5083 * Copies each element in the range @p [__first,__last) to the range
5084 * beginning at @p __result, except that only the first element is copied
5085 * from groups of consecutive elements that compare equal.
5086 * unique_copy() is stable, so the relative order of elements that are
5087 * copied is unchanged.
5089 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5090 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5092 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5093 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
5096 template<typename _InputIterator
, typename _OutputIterator
>
5097 inline _OutputIterator
5098 unique_copy(_InputIterator __first
, _InputIterator __last
,
5099 _OutputIterator __result
)
5101 // concept requirements
5102 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5103 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5104 typename iterator_traits
<_InputIterator
>::value_type
>)
5105 __glibcxx_function_requires(_EqualityComparableConcept
<
5106 typename iterator_traits
<_InputIterator
>::value_type
>)
5107 __glibcxx_requires_valid_range(__first
, __last
);
5109 if (__first
== __last
)
5111 return std::__unique_copy(__first
, __last
, __result
,
5112 std::__iterator_category(__first
),
5113 std::__iterator_category(__result
));
5117 * @brief Copy a sequence, removing consecutive values using a predicate.
5118 * @ingroup mutating_algorithms
5119 * @param __first An input iterator.
5120 * @param __last An input iterator.
5121 * @param __result An output iterator.
5122 * @param __binary_pred A binary predicate.
5123 * @return An iterator designating the end of the resulting sequence.
5125 * Copies each element in the range @p [__first,__last) to the range
5126 * beginning at @p __result, except that only the first element is copied
5127 * from groups of consecutive elements for which @p __binary_pred returns
5129 * unique_copy() is stable, so the relative order of elements that are
5130 * copied is unchanged.
5132 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5133 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5135 template<typename _InputIterator
, typename _OutputIterator
,
5136 typename _BinaryPredicate
>
5137 inline _OutputIterator
5138 unique_copy(_InputIterator __first
, _InputIterator __last
,
5139 _OutputIterator __result
,
5140 _BinaryPredicate __binary_pred
)
5142 // concept requirements -- predicates checked later
5143 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5144 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5145 typename iterator_traits
<_InputIterator
>::value_type
>)
5146 __glibcxx_requires_valid_range(__first
, __last
);
5148 if (__first
== __last
)
5150 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
5151 std::__iterator_category(__first
),
5152 std::__iterator_category(__result
));
5157 * @brief Randomly shuffle the elements of a sequence.
5158 * @ingroup mutating_algorithms
5159 * @param __first A forward iterator.
5160 * @param __last A forward iterator.
5163 * Reorder the elements in the range @p [__first,__last) using a random
5164 * distribution, so that every possible ordering of the sequence is
5167 template<typename _RandomAccessIterator
>
5169 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5171 // concept requirements
5172 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5173 _RandomAccessIterator
>)
5174 __glibcxx_requires_valid_range(__first
, __last
);
5176 if (__first
!= __last
)
5177 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5178 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
5182 * @brief Shuffle the elements of a sequence using a random number
5184 * @ingroup mutating_algorithms
5185 * @param __first A forward iterator.
5186 * @param __last A forward iterator.
5187 * @param __rand The RNG functor or function.
5190 * Reorders the elements in the range @p [__first,__last) using @p __rand to
5191 * provide a random distribution. Calling @p __rand(N) for a positive
5192 * integer @p N should return a randomly chosen integer from the
5195 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
5197 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5198 #ifdef __GXX_EXPERIMENTAL_CXX0X__
5199 _RandomNumberGenerator
&& __rand
)
5201 _RandomNumberGenerator
& __rand
)
5204 // concept requirements
5205 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5206 _RandomAccessIterator
>)
5207 __glibcxx_requires_valid_range(__first
, __last
);
5209 if (__first
== __last
)
5211 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5212 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5217 * @brief Move elements for which a predicate is true to the beginning
5219 * @ingroup mutating_algorithms
5220 * @param __first A forward iterator.
5221 * @param __last A forward iterator.
5222 * @param __pred A predicate functor.
5223 * @return An iterator @p middle such that @p __pred(i) is true for each
5224 * iterator @p i in the range @p [__first,middle) and false for each @p i
5225 * in the range @p [middle,__last).
5227 * @p __pred must not modify its operand. @p partition() does not preserve
5228 * the relative ordering of elements in each group, use
5229 * @p stable_partition() if this is needed.
5231 template<typename _ForwardIterator
, typename _Predicate
>
5232 inline _ForwardIterator
5233 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5236 // concept requirements
5237 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5239 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5240 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5241 __glibcxx_requires_valid_range(__first
, __last
);
5243 return std::__partition(__first
, __last
, __pred
,
5244 std::__iterator_category(__first
));
5250 * @brief Sort the smallest elements of a sequence.
5251 * @ingroup sorting_algorithms
5252 * @param __first An iterator.
5253 * @param __middle Another iterator.
5254 * @param __last Another iterator.
5257 * Sorts the smallest @p (__middle-__first) elements in the range
5258 * @p [first,last) and moves them to the range @p [__first,__middle). The
5259 * order of the remaining elements in the range @p [__middle,__last) is
5261 * After the sort if @p i and @j are iterators in the range
5262 * @p [__first,__middle) such that @i precedes @j and @k is an iterator in
5263 * the range @p [__middle,__last) then @p *j<*i and @p *k<*i are both false.
5265 template<typename _RandomAccessIterator
>
5267 partial_sort(_RandomAccessIterator __first
,
5268 _RandomAccessIterator __middle
,
5269 _RandomAccessIterator __last
)
5271 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5274 // concept requirements
5275 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5276 _RandomAccessIterator
>)
5277 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5278 __glibcxx_requires_valid_range(__first
, __middle
);
5279 __glibcxx_requires_valid_range(__middle
, __last
);
5281 std::__heap_select(__first
, __middle
, __last
);
5282 std::sort_heap(__first
, __middle
);
5286 * @brief Sort the smallest elements of a sequence using a predicate
5288 * @ingroup sorting_algorithms
5289 * @param __first An iterator.
5290 * @param __middle Another iterator.
5291 * @param __last Another iterator.
5292 * @param __comp A comparison functor.
5295 * Sorts the smallest @p (__middle-__first) elements in the range
5296 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
5297 * order of the remaining elements in the range @p [__middle,__last) is
5299 * After the sort if @p i and @j are iterators in the range
5300 * @p [__first,__middle) such that @i precedes @j and @k is an iterator in
5301 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
5304 template<typename _RandomAccessIterator
, typename _Compare
>
5306 partial_sort(_RandomAccessIterator __first
,
5307 _RandomAccessIterator __middle
,
5308 _RandomAccessIterator __last
,
5311 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5314 // concept requirements
5315 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5316 _RandomAccessIterator
>)
5317 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5318 _ValueType
, _ValueType
>)
5319 __glibcxx_requires_valid_range(__first
, __middle
);
5320 __glibcxx_requires_valid_range(__middle
, __last
);
5322 std::__heap_select(__first
, __middle
, __last
, __comp
);
5323 std::sort_heap(__first
, __middle
, __comp
);
5327 * @brief Sort a sequence just enough to find a particular position.
5328 * @ingroup sorting_algorithms
5329 * @param __first An iterator.
5330 * @param __nth Another iterator.
5331 * @param __last Another iterator.
5334 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5335 * is the same element that would have been in that position had the
5336 * whole sequence been sorted.
5337 * whole sequence been sorted. The elements either side of @p *__nth are
5338 * not completely sorted, but for any iterator @i in the range
5339 * @p [__first,__nth) and any iterator @j in the range @p [__nth,__last) it
5340 * holds that @p *j<*i is false.
5342 template<typename _RandomAccessIterator
>
5344 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5345 _RandomAccessIterator __last
)
5347 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5350 // concept requirements
5351 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5352 _RandomAccessIterator
>)
5353 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5354 __glibcxx_requires_valid_range(__first
, __nth
);
5355 __glibcxx_requires_valid_range(__nth
, __last
);
5357 if (__first
== __last
|| __nth
== __last
)
5360 std::__introselect(__first
, __nth
, __last
,
5361 std::__lg(__last
- __first
) * 2);
5365 * @brief Sort a sequence just enough to find a particular position
5366 * using a predicate for comparison.
5367 * @ingroup sorting_algorithms
5368 * @param __first An iterator.
5369 * @param __nth Another iterator.
5370 * @param __last Another iterator.
5371 * @param __comp A comparison functor.
5374 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5375 * is the same element that would have been in that position had the
5376 * whole sequence been sorted. The elements either side of @p *__nth are
5377 * not completely sorted, but for any iterator @i in the range
5378 * @p [__first,__nth) and any iterator @j in the range @p [__nth,__last) it
5379 * holds that @p __comp(*j,*i) is false.
5381 template<typename _RandomAccessIterator
, typename _Compare
>
5383 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5384 _RandomAccessIterator __last
, _Compare __comp
)
5386 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5389 // concept requirements
5390 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5391 _RandomAccessIterator
>)
5392 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5393 _ValueType
, _ValueType
>)
5394 __glibcxx_requires_valid_range(__first
, __nth
);
5395 __glibcxx_requires_valid_range(__nth
, __last
);
5397 if (__first
== __last
|| __nth
== __last
)
5400 std::__introselect(__first
, __nth
, __last
,
5401 std::__lg(__last
- __first
) * 2, __comp
);
5406 * @brief Sort the elements of a sequence.
5407 * @ingroup sorting_algorithms
5408 * @param __first An iterator.
5409 * @param __last Another iterator.
5412 * Sorts the elements in the range @p [__first,__last) in ascending order,
5413 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5414 * @p [__first,__last-1).
5416 * The relative ordering of equivalent elements is not preserved, use
5417 * @p stable_sort() if this is needed.
5419 template<typename _RandomAccessIterator
>
5421 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5423 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5426 // concept requirements
5427 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5428 _RandomAccessIterator
>)
5429 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5430 __glibcxx_requires_valid_range(__first
, __last
);
5432 if (__first
!= __last
)
5434 std::__introsort_loop(__first
, __last
,
5435 std::__lg(__last
- __first
) * 2);
5436 std::__final_insertion_sort(__first
, __last
);
5441 * @brief Sort the elements of a sequence using a predicate for comparison.
5442 * @ingroup sorting_algorithms
5443 * @param __first An iterator.
5444 * @param __last Another iterator.
5445 * @param __comp A comparison functor.
5448 * Sorts the elements in the range @p [__first,__last) in ascending order,
5449 * such that @p __comp(*(i+1),*i) is false for every iterator @p i in the
5450 * range @p [__first,__last-1).
5452 * The relative ordering of equivalent elements is not preserved, use
5453 * @p stable_sort() if this is needed.
5455 template<typename _RandomAccessIterator
, typename _Compare
>
5457 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5460 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5463 // concept requirements
5464 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5465 _RandomAccessIterator
>)
5466 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5468 __glibcxx_requires_valid_range(__first
, __last
);
5470 if (__first
!= __last
)
5472 std::__introsort_loop(__first
, __last
,
5473 std::__lg(__last
- __first
) * 2, __comp
);
5474 std::__final_insertion_sort(__first
, __last
, __comp
);
5479 * @brief Merges two sorted ranges.
5480 * @ingroup sorting_algorithms
5481 * @param __first1 An iterator.
5482 * @param __first2 Another iterator.
5483 * @param __last1 Another iterator.
5484 * @param __last2 Another iterator.
5485 * @param __result An iterator pointing to the end of the merged range.
5486 * @return An iterator pointing to the first element <em>not less
5489 * Merges the ranges [__first1,__last1) and [__first2,__last2) into
5490 * the sorted range [__result, __result + (__last1-__first1) +
5491 * (__last2-__first2)). Both input ranges must be sorted, and the
5492 * output range must not overlap with either of the input ranges.
5493 * The sort is @e stable, that is, for equivalent elements in the
5494 * two ranges, elements from the first range will always come
5495 * before elements from the second.
5497 template<typename _InputIterator1
, typename _InputIterator2
,
5498 typename _OutputIterator
>
5500 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5501 _InputIterator2 __first2
, _InputIterator2 __last2
,
5502 _OutputIterator __result
)
5504 typedef typename iterator_traits
<_InputIterator1
>::value_type
5506 typedef typename iterator_traits
<_InputIterator2
>::value_type
5509 // concept requirements
5510 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5511 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5512 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5514 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5516 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5517 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5518 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5520 while (__first1
!= __last1
&& __first2
!= __last2
)
5522 if (*__first2
< *__first1
)
5524 *__result
= *__first2
;
5529 *__result
= *__first1
;
5534 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5539 * @brief Merges two sorted ranges.
5540 * @ingroup sorting_algorithms
5541 * @param __first1 An iterator.
5542 * @param __first2 Another iterator.
5543 * @param __last1 Another iterator.
5544 * @param __last2 Another iterator.
5545 * @param __result An iterator pointing to the end of the merged range.
5546 * @param __comp A functor to use for comparisons.
5547 * @return An iterator pointing to the first element "not less
5550 * Merges the ranges [__first1,__last1) and [__first2,__last2) into
5551 * the sorted range [__result, __result + (__last1-__first1) +
5552 * (__last2-__first2)). Both input ranges must be sorted, and the
5553 * output range must not overlap with either of the input ranges.
5554 * The sort is @e stable, that is, for equivalent elements in the
5555 * two ranges, elements from the first range will always come
5556 * before elements from the second.
5558 * The comparison function should have the same effects on ordering as
5559 * the function used for the initial sort.
5561 template<typename _InputIterator1
, typename _InputIterator2
,
5562 typename _OutputIterator
, typename _Compare
>
5564 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5565 _InputIterator2 __first2
, _InputIterator2 __last2
,
5566 _OutputIterator __result
, _Compare __comp
)
5568 typedef typename iterator_traits
<_InputIterator1
>::value_type
5570 typedef typename iterator_traits
<_InputIterator2
>::value_type
5573 // concept requirements
5574 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5575 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5576 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5578 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5580 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5581 _ValueType2
, _ValueType1
>)
5582 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5583 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5585 while (__first1
!= __last1
&& __first2
!= __last2
)
5587 if (__comp(*__first2
, *__first1
))
5589 *__result
= *__first2
;
5594 *__result
= *__first1
;
5599 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5605 * @brief Sort the elements of a sequence, preserving the relative order
5606 * of equivalent elements.
5607 * @ingroup sorting_algorithms
5608 * @param __first An iterator.
5609 * @param __last Another iterator.
5612 * Sorts the elements in the range @p [__first,__last) in ascending order,
5613 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5614 * @p [__first,__last-1).
5616 * The relative ordering of equivalent elements is preserved, so any two
5617 * elements @p x and @p y in the range @p [__first,__last) such that
5618 * @p x<y is false and @p y<x is false will have the same relative
5619 * ordering after calling @p stable_sort().
5621 template<typename _RandomAccessIterator
>
5623 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5625 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5627 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5630 // concept requirements
5631 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5632 _RandomAccessIterator
>)
5633 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5634 __glibcxx_requires_valid_range(__first
, __last
);
5636 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5638 if (__buf
.begin() == 0)
5639 std::__inplace_stable_sort(__first
, __last
);
5641 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5642 _DistanceType(__buf
.size()));
5646 * @brief Sort the elements of a sequence using a predicate for comparison,
5647 * preserving the relative order of equivalent elements.
5648 * @ingroup sorting_algorithms
5649 * @param __first An iterator.
5650 * @param __last Another iterator.
5651 * @param __comp A comparison functor.
5654 * Sorts the elements in the range @p [__first,__last) in ascending order,
5655 * such that @p __comp(*(i+1),*i) is false for each iterator @p i in the
5656 * range @p [__first,__last-1).
5658 * The relative ordering of equivalent elements is preserved, so any two
5659 * elements @p x and @p y in the range @p [__first,__last) such that
5660 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5661 * relative ordering after calling @p stable_sort().
5663 template<typename _RandomAccessIterator
, typename _Compare
>
5665 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5668 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5670 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5673 // concept requirements
5674 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5675 _RandomAccessIterator
>)
5676 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5679 __glibcxx_requires_valid_range(__first
, __last
);
5681 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5683 if (__buf
.begin() == 0)
5684 std::__inplace_stable_sort(__first
, __last
, __comp
);
5686 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5687 _DistanceType(__buf
.size()), __comp
);
5692 * @brief Return the union of two sorted ranges.
5693 * @ingroup set_algorithms
5694 * @param __first1 Start of first range.
5695 * @param __last1 End of first range.
5696 * @param __first2 Start of second range.
5697 * @param __last2 End of second range.
5698 * @return End of the output range.
5699 * @ingroup set_algorithms
5701 * This operation iterates over both ranges, copying elements present in
5702 * each range in order to the output range. Iterators increment for each
5703 * range. When the current element of one range is less than the other,
5704 * that element is copied and the iterator advanced. If an element is
5705 * contained in both ranges, the element from the first range is copied and
5706 * both ranges advance. The output range may not overlap either input
5709 template<typename _InputIterator1
, typename _InputIterator2
,
5710 typename _OutputIterator
>
5712 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5713 _InputIterator2 __first2
, _InputIterator2 __last2
,
5714 _OutputIterator __result
)
5716 typedef typename iterator_traits
<_InputIterator1
>::value_type
5718 typedef typename iterator_traits
<_InputIterator2
>::value_type
5721 // concept requirements
5722 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5723 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5724 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5726 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5728 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5729 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5730 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5731 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5733 while (__first1
!= __last1
&& __first2
!= __last2
)
5735 if (*__first1
< *__first2
)
5737 *__result
= *__first1
;
5740 else if (*__first2
< *__first1
)
5742 *__result
= *__first2
;
5747 *__result
= *__first1
;
5753 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5758 * @brief Return the union of two sorted ranges using a comparison functor.
5759 * @ingroup set_algorithms
5760 * @param __first1 Start of first range.
5761 * @param __last1 End of first range.
5762 * @param __first2 Start of second range.
5763 * @param __last2 End of second range.
5764 * @param __comp The comparison functor.
5765 * @return End of the output range.
5766 * @ingroup set_algorithms
5768 * This operation iterates over both ranges, copying elements present in
5769 * each range in order to the output range. Iterators increment for each
5770 * range. When the current element of one range is less than the other
5771 * according to @a __comp, that element is copied and the iterator advanced.
5772 * If an equivalent element according to @a __comp is contained in both
5773 * ranges, the element from the first range is copied and both ranges
5774 * advance. The output range may not overlap either input range.
5776 template<typename _InputIterator1
, typename _InputIterator2
,
5777 typename _OutputIterator
, typename _Compare
>
5779 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5780 _InputIterator2 __first2
, _InputIterator2 __last2
,
5781 _OutputIterator __result
, _Compare __comp
)
5783 typedef typename iterator_traits
<_InputIterator1
>::value_type
5785 typedef typename iterator_traits
<_InputIterator2
>::value_type
5788 // concept requirements
5789 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5790 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5791 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5793 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5795 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5796 _ValueType1
, _ValueType2
>)
5797 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5798 _ValueType2
, _ValueType1
>)
5799 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5800 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5802 while (__first1
!= __last1
&& __first2
!= __last2
)
5804 if (__comp(*__first1
, *__first2
))
5806 *__result
= *__first1
;
5809 else if (__comp(*__first2
, *__first1
))
5811 *__result
= *__first2
;
5816 *__result
= *__first1
;
5822 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5827 * @brief Return the intersection of two sorted ranges.
5828 * @ingroup set_algorithms
5829 * @param __first1 Start of first range.
5830 * @param __last1 End of first range.
5831 * @param __first2 Start of second range.
5832 * @param __last2 End of second range.
5833 * @return End of the output range.
5834 * @ingroup set_algorithms
5836 * This operation iterates over both ranges, copying elements present in
5837 * both ranges in order to the output range. Iterators increment for each
5838 * range. When the current element of one range is less than the other,
5839 * that iterator advances. If an element is contained in both ranges, the
5840 * element from the first range is copied and both ranges advance. The
5841 * output range may not overlap either input range.
5843 template<typename _InputIterator1
, typename _InputIterator2
,
5844 typename _OutputIterator
>
5846 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5847 _InputIterator2 __first2
, _InputIterator2 __last2
,
5848 _OutputIterator __result
)
5850 typedef typename iterator_traits
<_InputIterator1
>::value_type
5852 typedef typename iterator_traits
<_InputIterator2
>::value_type
5855 // concept requirements
5856 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5857 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5858 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5860 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5861 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5862 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5863 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5865 while (__first1
!= __last1
&& __first2
!= __last2
)
5866 if (*__first1
< *__first2
)
5868 else if (*__first2
< *__first1
)
5872 *__result
= *__first1
;
5881 * @brief Return the intersection of two sorted ranges using comparison
5883 * @ingroup set_algorithms
5884 * @param __first1 Start of first range.
5885 * @param __last1 End of first range.
5886 * @param __first2 Start of second range.
5887 * @param __last2 End of second range.
5888 * @param __comp The comparison functor.
5889 * @return End of the output range.
5890 * @ingroup set_algorithms
5892 * This operation iterates over both ranges, copying elements present in
5893 * both ranges in order to the output range. Iterators increment for each
5894 * range. When the current element of one range is less than the other
5895 * according to @a __comp, that iterator advances. If an element is
5896 * contained in both ranges according to @a __comp, the element from the
5897 * first range is copied and both ranges advance. The output range may not
5898 * overlap either input range.
5900 template<typename _InputIterator1
, typename _InputIterator2
,
5901 typename _OutputIterator
, typename _Compare
>
5903 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5904 _InputIterator2 __first2
, _InputIterator2 __last2
,
5905 _OutputIterator __result
, _Compare __comp
)
5907 typedef typename iterator_traits
<_InputIterator1
>::value_type
5909 typedef typename iterator_traits
<_InputIterator2
>::value_type
5912 // concept requirements
5913 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5914 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5915 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5917 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5918 _ValueType1
, _ValueType2
>)
5919 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5920 _ValueType2
, _ValueType1
>)
5921 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5922 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5924 while (__first1
!= __last1
&& __first2
!= __last2
)
5925 if (__comp(*__first1
, *__first2
))
5927 else if (__comp(*__first2
, *__first1
))
5931 *__result
= *__first1
;
5940 * @brief Return the difference of two sorted ranges.
5941 * @ingroup set_algorithms
5942 * @param __first1 Start of first range.
5943 * @param __last1 End of first range.
5944 * @param __first2 Start of second range.
5945 * @param __last2 End of second range.
5946 * @return End of the output range.
5947 * @ingroup set_algorithms
5949 * This operation iterates over both ranges, copying elements present in
5950 * the first range but not the second in order to the output range.
5951 * Iterators increment for each range. When the current element of the
5952 * first range is less than the second, that element is copied and the
5953 * iterator advances. If the current element of the second range is less,
5954 * the iterator advances, but no element is copied. If an element is
5955 * contained in both ranges, no elements are copied and both ranges
5956 * advance. The output range may not overlap either input range.
5958 template<typename _InputIterator1
, typename _InputIterator2
,
5959 typename _OutputIterator
>
5961 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5962 _InputIterator2 __first2
, _InputIterator2 __last2
,
5963 _OutputIterator __result
)
5965 typedef typename iterator_traits
<_InputIterator1
>::value_type
5967 typedef typename iterator_traits
<_InputIterator2
>::value_type
5970 // concept requirements
5971 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5972 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5973 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5975 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5976 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5977 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5978 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5980 while (__first1
!= __last1
&& __first2
!= __last2
)
5981 if (*__first1
< *__first2
)
5983 *__result
= *__first1
;
5987 else if (*__first2
< *__first1
)
5994 return std::copy(__first1
, __last1
, __result
);
5998 * @brief Return the difference of two sorted ranges using comparison
6000 * @ingroup set_algorithms
6001 * @param __first1 Start of first range.
6002 * @param __last1 End of first range.
6003 * @param __first2 Start of second range.
6004 * @param __last2 End of second range.
6005 * @param __comp The comparison functor.
6006 * @return End of the output range.
6007 * @ingroup set_algorithms
6009 * This operation iterates over both ranges, copying elements present in
6010 * the first range but not the second in order to the output range.
6011 * Iterators increment for each range. When the current element of the
6012 * first range is less than the second according to @a comp, that element
6013 * is copied and the iterator advances. If the current element of the
6014 * second range is less, no element is copied and the iterator advances.
6015 * If an element is contained in both ranges according to @a comp, no
6016 * elements are copied and both ranges advance. The output range may not
6017 * overlap either input range.
6019 template<typename _InputIterator1
, typename _InputIterator2
,
6020 typename _OutputIterator
, typename _Compare
>
6022 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6023 _InputIterator2 __first2
, _InputIterator2 __last2
,
6024 _OutputIterator __result
, _Compare __comp
)
6026 typedef typename iterator_traits
<_InputIterator1
>::value_type
6028 typedef typename iterator_traits
<_InputIterator2
>::value_type
6031 // concept requirements
6032 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6033 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6034 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6036 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6037 _ValueType1
, _ValueType2
>)
6038 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6039 _ValueType2
, _ValueType1
>)
6040 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6041 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6043 while (__first1
!= __last1
&& __first2
!= __last2
)
6044 if (__comp(*__first1
, *__first2
))
6046 *__result
= *__first1
;
6050 else if (__comp(*__first2
, *__first1
))
6057 return std::copy(__first1
, __last1
, __result
);
6061 * @brief Return the symmetric difference of two sorted ranges.
6062 * @ingroup set_algorithms
6063 * @param __first1 Start of first range.
6064 * @param __last1 End of first range.
6065 * @param __first2 Start of second range.
6066 * @param __last2 End of second range.
6067 * @return End of the output range.
6068 * @ingroup set_algorithms
6070 * This operation iterates over both ranges, copying elements present in
6071 * one range but not the other in order to the output range. Iterators
6072 * increment for each range. When the current element of one range is less
6073 * than the other, that element is copied and the iterator advances. If an
6074 * element is contained in both ranges, no elements are copied and both
6075 * ranges advance. The output range may not overlap either input range.
6077 template<typename _InputIterator1
, typename _InputIterator2
,
6078 typename _OutputIterator
>
6080 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6081 _InputIterator2 __first2
, _InputIterator2 __last2
,
6082 _OutputIterator __result
)
6084 typedef typename iterator_traits
<_InputIterator1
>::value_type
6086 typedef typename iterator_traits
<_InputIterator2
>::value_type
6089 // concept requirements
6090 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6091 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6092 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6094 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6096 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6097 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6098 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6099 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6101 while (__first1
!= __last1
&& __first2
!= __last2
)
6102 if (*__first1
< *__first2
)
6104 *__result
= *__first1
;
6108 else if (*__first2
< *__first1
)
6110 *__result
= *__first2
;
6119 return std::copy(__first2
, __last2
, std::copy(__first1
,
6120 __last1
, __result
));
6124 * @brief Return the symmetric difference of two sorted ranges using
6125 * comparison functor.
6126 * @ingroup set_algorithms
6127 * @param __first1 Start of first range.
6128 * @param __last1 End of first range.
6129 * @param __first2 Start of second range.
6130 * @param __last2 End of second range.
6131 * @param __comp The comparison functor.
6132 * @return End of the output range.
6133 * @ingroup set_algorithms
6135 * This operation iterates over both ranges, copying elements present in
6136 * one range but not the other in order to the output range. Iterators
6137 * increment for each range. When the current element of one range is less
6138 * than the other according to @a comp, that element is copied and the
6139 * iterator advances. If an element is contained in both ranges according
6140 * to @a comp, no elements are copied and both ranges advance. The output
6141 * range may not overlap either input range.
6143 template<typename _InputIterator1
, typename _InputIterator2
,
6144 typename _OutputIterator
, typename _Compare
>
6146 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6147 _InputIterator2 __first2
, _InputIterator2 __last2
,
6148 _OutputIterator __result
,
6151 typedef typename iterator_traits
<_InputIterator1
>::value_type
6153 typedef typename iterator_traits
<_InputIterator2
>::value_type
6156 // concept requirements
6157 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6158 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6159 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6161 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6163 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6164 _ValueType1
, _ValueType2
>)
6165 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6166 _ValueType2
, _ValueType1
>)
6167 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6168 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6170 while (__first1
!= __last1
&& __first2
!= __last2
)
6171 if (__comp(*__first1
, *__first2
))
6173 *__result
= *__first1
;
6177 else if (__comp(*__first2
, *__first1
))
6179 *__result
= *__first2
;
6188 return std::copy(__first2
, __last2
,
6189 std::copy(__first1
, __last1
, __result
));
6194 * @brief Return the minimum element in a range.
6195 * @ingroup sorting_algorithms
6196 * @param __first Start of range.
6197 * @param __last End of range.
6198 * @return Iterator referencing the first instance of the smallest value.
6200 template<typename _ForwardIterator
>
6202 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
6204 // concept requirements
6205 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6206 __glibcxx_function_requires(_LessThanComparableConcept
<
6207 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6208 __glibcxx_requires_valid_range(__first
, __last
);
6210 if (__first
== __last
)
6212 _ForwardIterator __result
= __first
;
6213 while (++__first
!= __last
)
6214 if (*__first
< *__result
)
6220 * @brief Return the minimum element in a range using comparison functor.
6221 * @ingroup sorting_algorithms
6222 * @param __first Start of range.
6223 * @param __last End of range.
6224 * @param __comp Comparison functor.
6225 * @return Iterator referencing the first instance of the smallest value
6226 * according to comp.
6228 template<typename _ForwardIterator
, typename _Compare
>
6230 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6233 // concept requirements
6234 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6235 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6236 typename iterator_traits
<_ForwardIterator
>::value_type
,
6237 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6238 __glibcxx_requires_valid_range(__first
, __last
);
6240 if (__first
== __last
)
6242 _ForwardIterator __result
= __first
;
6243 while (++__first
!= __last
)
6244 if (__comp(*__first
, *__result
))
6250 * @brief Return the maximum element in a range.
6251 * @ingroup sorting_algorithms
6252 * @param __first Start of range.
6253 * @param __last End of range.
6254 * @return Iterator referencing the first instance of the largest value.
6256 template<typename _ForwardIterator
>
6258 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6260 // concept requirements
6261 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6262 __glibcxx_function_requires(_LessThanComparableConcept
<
6263 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6264 __glibcxx_requires_valid_range(__first
, __last
);
6266 if (__first
== __last
)
6268 _ForwardIterator __result
= __first
;
6269 while (++__first
!= __last
)
6270 if (*__result
< *__first
)
6276 * @brief Return the maximum element in a range using comparison functor.
6277 * @ingroup sorting_algorithms
6278 * @param __first Start of range.
6279 * @param __last End of range.
6280 * @param __comp Comparison functor.
6281 * @return Iterator referencing the first instance of the largest value
6282 * according to __comp.
6284 template<typename _ForwardIterator
, typename _Compare
>
6286 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6289 // concept requirements
6290 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6291 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6292 typename iterator_traits
<_ForwardIterator
>::value_type
,
6293 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6294 __glibcxx_requires_valid_range(__first
, __last
);
6296 if (__first
== __last
) return __first
;
6297 _ForwardIterator __result
= __first
;
6298 while (++__first
!= __last
)
6299 if (__comp(*__result
, *__first
))
6304 _GLIBCXX_END_NAMESPACE_ALGO
6307 #endif /* _STL_ALGO_H */