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) == @p *(first2+N)
633 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
634 * such iterator exists.
636 * Searches the range @p [first1,last1) for a sub-sequence that compares
637 * equal value-by-value with the sequence given by @p [first2,last2) and
638 * returns an iterator to the first element of the sub-sequence, or
639 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
640 * last such subsequence contained in [first,last1).
642 * Because the sub-sequence must lie completely within the range
643 * @p [first1,last1) it must start at a position less than
644 * @p last1-(last2-first2) where @p last2-first2 is the length of the
646 * This means that the returned iterator @c i will be in the range
647 * @p [first1,last1-(last2-first2))
649 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
650 inline _ForwardIterator1
651 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
652 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
654 // concept requirements
655 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
656 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
657 __glibcxx_function_requires(_EqualOpConcept
<
658 typename iterator_traits
<_ForwardIterator1
>::value_type
,
659 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
660 __glibcxx_requires_valid_range(__first1
, __last1
);
661 __glibcxx_requires_valid_range(__first2
, __last2
);
663 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
664 std::__iterator_category(__first1
),
665 std::__iterator_category(__first2
));
669 * @brief Find last matching subsequence in a sequence using a predicate.
670 * @ingroup non_mutating_algorithms
671 * @param first1 Start of range to search.
672 * @param last1 End of range to search.
673 * @param first2 Start of sequence to match.
674 * @param last2 End of sequence to match.
675 * @param comp The predicate to use.
676 * @return The last iterator @c i in the range
677 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
678 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
679 * @p last1 if no such iterator exists.
681 * Searches the range @p [first1,last1) for a sub-sequence that compares
682 * equal value-by-value with the sequence given by @p [first2,last2) using
683 * comp as a predicate and returns an iterator to the first element of the
684 * sub-sequence, or @p last1 if the sub-sequence is not found. The
685 * sub-sequence will be the last such subsequence contained in
688 * Because the sub-sequence must lie completely within the range
689 * @p [first1,last1) it must start at a position less than
690 * @p last1-(last2-first2) where @p last2-first2 is the length of the
692 * This means that the returned iterator @c i will be in the range
693 * @p [first1,last1-(last2-first2))
695 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
696 typename _BinaryPredicate
>
697 inline _ForwardIterator1
698 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
699 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
700 _BinaryPredicate __comp
)
702 // concept requirements
703 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
704 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
705 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
706 typename iterator_traits
<_ForwardIterator1
>::value_type
,
707 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
708 __glibcxx_requires_valid_range(__first1
, __last1
);
709 __glibcxx_requires_valid_range(__first2
, __last2
);
711 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
712 std::__iterator_category(__first1
),
713 std::__iterator_category(__first2
),
717 #ifdef __GXX_EXPERIMENTAL_CXX0X__
719 * @brief Checks that a predicate is true for all the elements
721 * @ingroup non_mutating_algorithms
722 * @param first An input iterator.
723 * @param last An input iterator.
724 * @param pred A predicate.
725 * @return True if the check is true, false otherwise.
727 * Returns true if @p pred is true for each element in the range
728 * @p [first,last), and false otherwise.
730 template<typename _InputIterator
, typename _Predicate
>
732 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
733 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
736 * @brief Checks that a predicate is false for all the elements
738 * @ingroup non_mutating_algorithms
739 * @param first An input iterator.
740 * @param last An input iterator.
741 * @param pred A predicate.
742 * @return True if the check is true, false otherwise.
744 * Returns true if @p pred is false for each element in the range
745 * @p [first,last), and false otherwise.
747 template<typename _InputIterator
, typename _Predicate
>
749 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
750 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
753 * @brief Checks that a predicate is false for at least an element
755 * @ingroup non_mutating_algorithms
756 * @param first An input iterator.
757 * @param last An input iterator.
758 * @param pred A predicate.
759 * @return True if the check is true, false otherwise.
761 * Returns true if an element exists in the range @p [first,last) such that
762 * @p pred is true, and false otherwise.
764 template<typename _InputIterator
, typename _Predicate
>
766 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
767 { return !std::none_of(__first
, __last
, __pred
); }
770 * @brief Find the first element in a sequence for which a
771 * predicate is false.
772 * @ingroup non_mutating_algorithms
773 * @param first An input iterator.
774 * @param last An input iterator.
775 * @param pred A predicate.
776 * @return The first iterator @c i in the range @p [first,last)
777 * such that @p pred(*i) is false, or @p last if no such iterator exists.
779 template<typename _InputIterator
, typename _Predicate
>
780 inline _InputIterator
781 find_if_not(_InputIterator __first
, _InputIterator __last
,
784 // concept requirements
785 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
786 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
787 typename iterator_traits
<_InputIterator
>::value_type
>)
788 __glibcxx_requires_valid_range(__first
, __last
);
789 return std::__find_if_not(__first
, __last
, __pred
,
790 std::__iterator_category(__first
));
794 * @brief Checks whether the sequence is partitioned.
795 * @ingroup mutating_algorithms
796 * @param first An input iterator.
797 * @param last An input iterator.
798 * @param pred A predicate.
799 * @return True if the range @p [first,last) is partioned by @p pred,
800 * i.e. if all elements that satisfy @p pred appear before those that
803 template<typename _InputIterator
, typename _Predicate
>
805 is_partitioned(_InputIterator __first
, _InputIterator __last
,
808 __first
= std::find_if_not(__first
, __last
, __pred
);
809 return std::none_of(__first
, __last
, __pred
);
813 * @brief Find the partition point of a partitioned range.
814 * @ingroup mutating_algorithms
815 * @param first An iterator.
816 * @param last Another iterator.
817 * @param pred A predicate.
818 * @return An iterator @p mid such that @p all_of(first, mid, pred)
819 * and @p none_of(mid, last, pred) are both true.
821 template<typename _ForwardIterator
, typename _Predicate
>
823 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
826 // concept requirements
827 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
828 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
829 typename iterator_traits
<_ForwardIterator
>::value_type
>)
831 // A specific debug-mode test will be necessary...
832 __glibcxx_requires_valid_range(__first
, __last
);
834 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
837 _DistanceType __len
= std::distance(__first
, __last
);
838 _DistanceType __half
;
839 _ForwardIterator __middle
;
845 std::advance(__middle
, __half
);
846 if (__pred(*__middle
))
850 __len
= __len
- __half
- 1;
861 * @brief Copy a sequence, removing elements of a given value.
862 * @ingroup mutating_algorithms
863 * @param first An input iterator.
864 * @param last An input iterator.
865 * @param result An output iterator.
866 * @param value The value to be removed.
867 * @return An iterator designating the end of the resulting sequence.
869 * Copies each element in the range @p [first,last) not equal to @p value
870 * to the range beginning at @p result.
871 * remove_copy() is stable, so the relative order of elements that are
872 * copied is unchanged.
874 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
876 remove_copy(_InputIterator __first
, _InputIterator __last
,
877 _OutputIterator __result
, const _Tp
& __value
)
879 // concept requirements
880 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
881 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
882 typename iterator_traits
<_InputIterator
>::value_type
>)
883 __glibcxx_function_requires(_EqualOpConcept
<
884 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
885 __glibcxx_requires_valid_range(__first
, __last
);
887 for (; __first
!= __last
; ++__first
)
888 if (!(*__first
== __value
))
890 *__result
= *__first
;
897 * @brief Copy a sequence, removing elements for which a predicate is true.
898 * @ingroup mutating_algorithms
899 * @param first An input iterator.
900 * @param last An input iterator.
901 * @param result An output iterator.
902 * @param pred A predicate.
903 * @return An iterator designating the end of the resulting sequence.
905 * Copies each element in the range @p [first,last) for which
906 * @p pred returns false to the range beginning at @p result.
908 * remove_copy_if() is stable, so the relative order of elements that are
909 * copied is unchanged.
911 template<typename _InputIterator
, typename _OutputIterator
,
914 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
915 _OutputIterator __result
, _Predicate __pred
)
917 // concept requirements
918 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
919 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
920 typename iterator_traits
<_InputIterator
>::value_type
>)
921 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
922 typename iterator_traits
<_InputIterator
>::value_type
>)
923 __glibcxx_requires_valid_range(__first
, __last
);
925 for (; __first
!= __last
; ++__first
)
926 if (!bool(__pred(*__first
)))
928 *__result
= *__first
;
934 #ifdef __GXX_EXPERIMENTAL_CXX0X__
936 * @brief Copy the elements of a sequence for which a predicate is true.
937 * @ingroup mutating_algorithms
938 * @param first An input iterator.
939 * @param last An input iterator.
940 * @param result An output iterator.
941 * @param pred A predicate.
942 * @return An iterator designating the end of the resulting sequence.
944 * Copies each element in the range @p [first,last) for which
945 * @p pred returns true to the range beginning at @p result.
947 * copy_if() is stable, so the relative order of elements that are
948 * copied is unchanged.
950 template<typename _InputIterator
, typename _OutputIterator
,
953 copy_if(_InputIterator __first
, _InputIterator __last
,
954 _OutputIterator __result
, _Predicate __pred
)
956 // concept requirements
957 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
958 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
959 typename iterator_traits
<_InputIterator
>::value_type
>)
960 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
961 typename iterator_traits
<_InputIterator
>::value_type
>)
962 __glibcxx_requires_valid_range(__first
, __last
);
964 for (; __first
!= __last
; ++__first
)
965 if (__pred(*__first
))
967 *__result
= *__first
;
974 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
976 __copy_n(_InputIterator __first
, _Size __n
,
977 _OutputIterator __result
, input_iterator_tag
)
979 for (; __n
> 0; --__n
)
981 *__result
= *__first
;
988 template<typename _RandomAccessIterator
, typename _Size
,
989 typename _OutputIterator
>
990 inline _OutputIterator
991 __copy_n(_RandomAccessIterator __first
, _Size __n
,
992 _OutputIterator __result
, random_access_iterator_tag
)
993 { return std::copy(__first
, __first
+ __n
, __result
); }
996 * @brief Copies the range [first,first+n) into [result,result+n).
997 * @ingroup mutating_algorithms
998 * @param first An input iterator.
999 * @param n The number of elements to copy.
1000 * @param result An output iterator.
1003 * This inline function will boil down to a call to @c memmove whenever
1004 * possible. Failing that, if random access iterators are passed, then the
1005 * loop count will be known (and therefore a candidate for compiler
1006 * optimizations such as unrolling).
1008 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1009 inline _OutputIterator
1010 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1012 // concept requirements
1013 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1014 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1015 typename iterator_traits
<_InputIterator
>::value_type
>)
1017 return std::__copy_n(__first
, __n
, __result
,
1018 std::__iterator_category(__first
));
1022 * @brief Copy the elements of a sequence to separate output sequences
1023 * depending on the truth value of a predicate.
1024 * @ingroup mutating_algorithms
1025 * @param first An input iterator.
1026 * @param last An input iterator.
1027 * @param out_true An output iterator.
1028 * @param out_false An output iterator.
1029 * @param pred A predicate.
1030 * @return A pair designating the ends of the resulting sequences.
1032 * Copies each element in the range @p [first,last) for which
1033 * @p pred returns true to the range beginning at @p out_true
1034 * and each element for which @p pred returns false to @p out_false.
1036 template<typename _InputIterator
, typename _OutputIterator1
,
1037 typename _OutputIterator2
, typename _Predicate
>
1038 pair
<_OutputIterator1
, _OutputIterator2
>
1039 partition_copy(_InputIterator __first
, _InputIterator __last
,
1040 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1043 // concept requirements
1044 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1045 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1046 typename iterator_traits
<_InputIterator
>::value_type
>)
1047 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1048 typename iterator_traits
<_InputIterator
>::value_type
>)
1049 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1050 typename iterator_traits
<_InputIterator
>::value_type
>)
1051 __glibcxx_requires_valid_range(__first
, __last
);
1053 for (; __first
!= __last
; ++__first
)
1054 if (__pred(*__first
))
1056 *__out_true
= *__first
;
1061 *__out_false
= *__first
;
1065 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1070 * @brief Remove elements from a sequence.
1071 * @ingroup mutating_algorithms
1072 * @param first An input iterator.
1073 * @param last An input iterator.
1074 * @param value The value to be removed.
1075 * @return An iterator designating the end of the resulting sequence.
1077 * All elements equal to @p value are removed from the range
1080 * remove() is stable, so the relative order of elements that are
1081 * not removed is unchanged.
1083 * Elements between the end of the resulting sequence and @p last
1084 * are still present, but their value is unspecified.
1086 template<typename _ForwardIterator
, typename _Tp
>
1088 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1091 // concept requirements
1092 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1094 __glibcxx_function_requires(_EqualOpConcept
<
1095 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1096 __glibcxx_requires_valid_range(__first
, __last
);
1098 __first
= _GLIBCXX_STD_A::find(__first
, __last
, __value
);
1099 if(__first
== __last
)
1101 _ForwardIterator __result
= __first
;
1103 for(; __first
!= __last
; ++__first
)
1104 if(!(*__first
== __value
))
1106 *__result
= _GLIBCXX_MOVE(*__first
);
1113 * @brief Remove elements from a sequence using a predicate.
1114 * @ingroup mutating_algorithms
1115 * @param first A forward iterator.
1116 * @param last A forward iterator.
1117 * @param pred A predicate.
1118 * @return An iterator designating the end of the resulting sequence.
1120 * All elements for which @p pred returns true are removed from the range
1123 * remove_if() is stable, so the relative order of elements that are
1124 * not removed is unchanged.
1126 * Elements between the end of the resulting sequence and @p last
1127 * are still present, but their value is unspecified.
1129 template<typename _ForwardIterator
, typename _Predicate
>
1131 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1134 // concept requirements
1135 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1137 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1138 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1139 __glibcxx_requires_valid_range(__first
, __last
);
1141 __first
= _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
);
1142 if(__first
== __last
)
1144 _ForwardIterator __result
= __first
;
1146 for(; __first
!= __last
; ++__first
)
1147 if(!bool(__pred(*__first
)))
1149 *__result
= _GLIBCXX_MOVE(*__first
);
1156 * @brief Remove consecutive duplicate values from a sequence.
1157 * @ingroup mutating_algorithms
1158 * @param first A forward iterator.
1159 * @param last A forward iterator.
1160 * @return An iterator designating the end of the resulting sequence.
1162 * Removes all but the first element from each group of consecutive
1163 * values that compare equal.
1164 * unique() is stable, so the relative order of elements that are
1165 * not removed is unchanged.
1166 * Elements between the end of the resulting sequence and @p last
1167 * are still present, but their value is unspecified.
1169 template<typename _ForwardIterator
>
1171 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1173 // concept requirements
1174 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1176 __glibcxx_function_requires(_EqualityComparableConcept
<
1177 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1178 __glibcxx_requires_valid_range(__first
, __last
);
1180 // Skip the beginning, if already unique.
1181 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
);
1182 if (__first
== __last
)
1185 // Do the real copy work.
1186 _ForwardIterator __dest
= __first
;
1188 while (++__first
!= __last
)
1189 if (!(*__dest
== *__first
))
1190 *++__dest
= _GLIBCXX_MOVE(*__first
);
1195 * @brief Remove consecutive values from a sequence using a predicate.
1196 * @ingroup mutating_algorithms
1197 * @param first A forward iterator.
1198 * @param last A forward iterator.
1199 * @param binary_pred A binary predicate.
1200 * @return An iterator designating the end of the resulting sequence.
1202 * Removes all but the first element from each group of consecutive
1203 * values for which @p binary_pred returns true.
1204 * unique() is stable, so the relative order of elements that are
1205 * not removed is unchanged.
1206 * Elements between the end of the resulting sequence and @p last
1207 * are still present, but their value is unspecified.
1209 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1211 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1212 _BinaryPredicate __binary_pred
)
1214 // concept requirements
1215 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1217 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1218 typename iterator_traits
<_ForwardIterator
>::value_type
,
1219 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1220 __glibcxx_requires_valid_range(__first
, __last
);
1222 // Skip the beginning, if already unique.
1223 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
, __binary_pred
);
1224 if (__first
== __last
)
1227 // Do the real copy work.
1228 _ForwardIterator __dest
= __first
;
1230 while (++__first
!= __last
)
1231 if (!bool(__binary_pred(*__dest
, *__first
)))
1232 *++__dest
= _GLIBCXX_MOVE(*__first
);
1237 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1239 * overloaded for forward iterators and output iterator as result.
1241 template<typename _ForwardIterator
, typename _OutputIterator
>
1243 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1244 _OutputIterator __result
,
1245 forward_iterator_tag
, output_iterator_tag
)
1247 // concept requirements -- taken care of in dispatching function
1248 _ForwardIterator __next
= __first
;
1249 *__result
= *__first
;
1250 while (++__next
!= __last
)
1251 if (!(*__first
== *__next
))
1254 *++__result
= *__first
;
1260 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1262 * overloaded for input iterators and output iterator as result.
1264 template<typename _InputIterator
, typename _OutputIterator
>
1266 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1267 _OutputIterator __result
,
1268 input_iterator_tag
, output_iterator_tag
)
1270 // concept requirements -- taken care of in dispatching function
1271 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1272 *__result
= __value
;
1273 while (++__first
!= __last
)
1274 if (!(__value
== *__first
))
1277 *++__result
= __value
;
1283 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1285 * overloaded for input iterators and forward iterator as result.
1287 template<typename _InputIterator
, typename _ForwardIterator
>
1289 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1290 _ForwardIterator __result
,
1291 input_iterator_tag
, forward_iterator_tag
)
1293 // concept requirements -- taken care of in dispatching function
1294 *__result
= *__first
;
1295 while (++__first
!= __last
)
1296 if (!(*__result
== *__first
))
1297 *++__result
= *__first
;
1302 * This is an uglified
1303 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1305 * overloaded for forward iterators and output iterator as result.
1307 template<typename _ForwardIterator
, typename _OutputIterator
,
1308 typename _BinaryPredicate
>
1310 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1311 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1312 forward_iterator_tag
, output_iterator_tag
)
1314 // concept requirements -- iterators already checked
1315 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1316 typename iterator_traits
<_ForwardIterator
>::value_type
,
1317 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1319 _ForwardIterator __next
= __first
;
1320 *__result
= *__first
;
1321 while (++__next
!= __last
)
1322 if (!bool(__binary_pred(*__first
, *__next
)))
1325 *++__result
= *__first
;
1331 * This is an uglified
1332 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1334 * overloaded for input iterators and output iterator as result.
1336 template<typename _InputIterator
, typename _OutputIterator
,
1337 typename _BinaryPredicate
>
1339 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1340 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1341 input_iterator_tag
, output_iterator_tag
)
1343 // concept requirements -- iterators already checked
1344 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1345 typename iterator_traits
<_InputIterator
>::value_type
,
1346 typename iterator_traits
<_InputIterator
>::value_type
>)
1348 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1349 *__result
= __value
;
1350 while (++__first
!= __last
)
1351 if (!bool(__binary_pred(__value
, *__first
)))
1354 *++__result
= __value
;
1360 * This is an uglified
1361 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1363 * overloaded for input iterators and forward iterator as result.
1365 template<typename _InputIterator
, typename _ForwardIterator
,
1366 typename _BinaryPredicate
>
1368 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1369 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1370 input_iterator_tag
, forward_iterator_tag
)
1372 // concept requirements -- iterators already checked
1373 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1374 typename iterator_traits
<_ForwardIterator
>::value_type
,
1375 typename iterator_traits
<_InputIterator
>::value_type
>)
1377 *__result
= *__first
;
1378 while (++__first
!= __last
)
1379 if (!bool(__binary_pred(*__result
, *__first
)))
1380 *++__result
= *__first
;
1385 * This is an uglified reverse(_BidirectionalIterator,
1386 * _BidirectionalIterator)
1387 * overloaded for bidirectional iterators.
1389 template<typename _BidirectionalIterator
>
1391 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1392 bidirectional_iterator_tag
)
1395 if (__first
== __last
|| __first
== --__last
)
1399 std::iter_swap(__first
, __last
);
1405 * This is an uglified reverse(_BidirectionalIterator,
1406 * _BidirectionalIterator)
1407 * overloaded for random access iterators.
1409 template<typename _RandomAccessIterator
>
1411 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1412 random_access_iterator_tag
)
1414 if (__first
== __last
)
1417 while (__first
< __last
)
1419 std::iter_swap(__first
, __last
);
1426 * @brief Reverse a sequence.
1427 * @ingroup mutating_algorithms
1428 * @param first A bidirectional iterator.
1429 * @param last A bidirectional iterator.
1430 * @return reverse() returns no value.
1432 * Reverses the order of the elements in the range @p [first,last),
1433 * so that the first element becomes the last etc.
1434 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1435 * swaps @p *(first+i) and @p *(last-(i+1))
1437 template<typename _BidirectionalIterator
>
1439 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1441 // concept requirements
1442 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1443 _BidirectionalIterator
>)
1444 __glibcxx_requires_valid_range(__first
, __last
);
1445 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1449 * @brief Copy a sequence, reversing its elements.
1450 * @ingroup mutating_algorithms
1451 * @param first A bidirectional iterator.
1452 * @param last A bidirectional iterator.
1453 * @param result An output iterator.
1454 * @return An iterator designating the end of the resulting sequence.
1456 * Copies the elements in the range @p [first,last) to the range
1457 * @p [result,result+(last-first)) such that the order of the
1458 * elements is reversed.
1459 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1460 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1461 * The ranges @p [first,last) and @p [result,result+(last-first))
1464 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1466 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1467 _OutputIterator __result
)
1469 // concept requirements
1470 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1471 _BidirectionalIterator
>)
1472 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1473 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1474 __glibcxx_requires_valid_range(__first
, __last
);
1476 while (__first
!= __last
)
1479 *__result
= *__last
;
1486 * This is a helper function for the rotate algorithm specialized on RAIs.
1487 * It returns the greatest common divisor of two integer values.
1489 template<typename _EuclideanRingElement
>
1490 _EuclideanRingElement
1491 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1495 _EuclideanRingElement __t
= __m
% __n
;
1502 /// This is a helper function for the rotate algorithm.
1503 template<typename _ForwardIterator
>
1505 __rotate(_ForwardIterator __first
,
1506 _ForwardIterator __middle
,
1507 _ForwardIterator __last
,
1508 forward_iterator_tag
)
1510 if (__first
== __middle
|| __last
== __middle
)
1513 _ForwardIterator __first2
= __middle
;
1516 std::iter_swap(__first
, __first2
);
1519 if (__first
== __middle
)
1520 __middle
= __first2
;
1522 while (__first2
!= __last
);
1524 __first2
= __middle
;
1526 while (__first2
!= __last
)
1528 std::iter_swap(__first
, __first2
);
1531 if (__first
== __middle
)
1532 __middle
= __first2
;
1533 else if (__first2
== __last
)
1534 __first2
= __middle
;
1538 /// This is a helper function for the rotate algorithm.
1539 template<typename _BidirectionalIterator
>
1541 __rotate(_BidirectionalIterator __first
,
1542 _BidirectionalIterator __middle
,
1543 _BidirectionalIterator __last
,
1544 bidirectional_iterator_tag
)
1546 // concept requirements
1547 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1548 _BidirectionalIterator
>)
1550 if (__first
== __middle
|| __last
== __middle
)
1553 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1554 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1556 while (__first
!= __middle
&& __middle
!= __last
)
1558 std::iter_swap(__first
, --__last
);
1562 if (__first
== __middle
)
1563 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1565 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1568 /// This is a helper function for the rotate algorithm.
1569 template<typename _RandomAccessIterator
>
1571 __rotate(_RandomAccessIterator __first
,
1572 _RandomAccessIterator __middle
,
1573 _RandomAccessIterator __last
,
1574 random_access_iterator_tag
)
1576 // concept requirements
1577 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1578 _RandomAccessIterator
>)
1580 if (__first
== __middle
|| __last
== __middle
)
1583 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1585 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1588 _Distance __n
= __last
- __first
;
1589 _Distance __k
= __middle
- __first
;
1591 if (__k
== __n
- __k
)
1593 std::swap_ranges(__first
, __middle
, __middle
);
1597 _RandomAccessIterator __p
= __first
;
1601 if (__k
< __n
- __k
)
1603 if (__is_pod(_ValueType
) && __k
== 1)
1605 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1606 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1607 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1610 _RandomAccessIterator __q
= __p
+ __k
;
1611 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1613 std::iter_swap(__p
, __q
);
1620 std::swap(__n
, __k
);
1626 if (__is_pod(_ValueType
) && __k
== 1)
1628 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1629 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1630 *__p
= _GLIBCXX_MOVE(__t
);
1633 _RandomAccessIterator __q
= __p
+ __n
;
1635 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1639 std::iter_swap(__p
, __q
);
1644 std::swap(__n
, __k
);
1650 * @brief Rotate the elements of a sequence.
1651 * @ingroup mutating_algorithms
1652 * @param first A forward iterator.
1653 * @param middle A forward iterator.
1654 * @param last A forward iterator.
1657 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1658 * positions so that the element at @p middle is moved to @p first, the
1659 * element at @p middle+1 is moved to @first+1 and so on for each element
1660 * in the range @p [first,last).
1662 * This effectively swaps the ranges @p [first,middle) and
1665 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1666 * each @p n in the range @p [0,last-first).
1668 template<typename _ForwardIterator
>
1670 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1671 _ForwardIterator __last
)
1673 // concept requirements
1674 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1676 __glibcxx_requires_valid_range(__first
, __middle
);
1677 __glibcxx_requires_valid_range(__middle
, __last
);
1679 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1681 std::__rotate(__first
, __middle
, __last
, _IterType());
1685 * @brief Copy a sequence, rotating its elements.
1686 * @ingroup mutating_algorithms
1687 * @param first A forward iterator.
1688 * @param middle A forward iterator.
1689 * @param last A forward iterator.
1690 * @param result An output iterator.
1691 * @return An iterator designating the end of the resulting sequence.
1693 * Copies the elements of the range @p [first,last) to the range
1694 * beginning at @result, rotating the copied elements by @p (middle-first)
1695 * positions so that the element at @p middle is moved to @p result, the
1696 * element at @p middle+1 is moved to @result+1 and so on for each element
1697 * in the range @p [first,last).
1699 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1700 * each @p n in the range @p [0,last-first).
1702 template<typename _ForwardIterator
, typename _OutputIterator
>
1704 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1705 _ForwardIterator __last
, _OutputIterator __result
)
1707 // concept requirements
1708 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1709 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1710 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1711 __glibcxx_requires_valid_range(__first
, __middle
);
1712 __glibcxx_requires_valid_range(__middle
, __last
);
1714 return std::copy(__first
, __middle
,
1715 std::copy(__middle
, __last
, __result
));
1718 /// This is a helper function...
1719 template<typename _ForwardIterator
, typename _Predicate
>
1721 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1722 _Predicate __pred
, forward_iterator_tag
)
1724 if (__first
== __last
)
1727 while (__pred(*__first
))
1728 if (++__first
== __last
)
1731 _ForwardIterator __next
= __first
;
1733 while (++__next
!= __last
)
1734 if (__pred(*__next
))
1736 std::iter_swap(__first
, __next
);
1743 /// This is a helper function...
1744 template<typename _BidirectionalIterator
, typename _Predicate
>
1745 _BidirectionalIterator
1746 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1747 _Predicate __pred
, bidirectional_iterator_tag
)
1752 if (__first
== __last
)
1754 else if (__pred(*__first
))
1760 if (__first
== __last
)
1762 else if (!bool(__pred(*__last
)))
1766 std::iter_swap(__first
, __last
);
1773 /// This is a helper function...
1774 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1776 __inplace_stable_partition(_ForwardIterator __first
,
1777 _ForwardIterator __last
,
1778 _Predicate __pred
, _Distance __len
)
1781 return __pred(*__first
) ? __last
: __first
;
1782 _ForwardIterator __middle
= __first
;
1783 std::advance(__middle
, __len
/ 2);
1784 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1788 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1792 std::rotate(__begin
, __middle
, __end
);
1793 std::advance(__begin
, std::distance(__middle
, __end
));
1797 /// This is a helper function...
1798 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1801 __stable_partition_adaptive(_ForwardIterator __first
,
1802 _ForwardIterator __last
,
1803 _Predicate __pred
, _Distance __len
,
1805 _Distance __buffer_size
)
1807 if (__len
<= __buffer_size
)
1809 _ForwardIterator __result1
= __first
;
1810 _Pointer __result2
= __buffer
;
1811 for (; __first
!= __last
; ++__first
)
1812 if (__pred(*__first
))
1814 *__result1
= _GLIBCXX_MOVE(*__first
);
1819 *__result2
= _GLIBCXX_MOVE(*__first
);
1822 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1827 _ForwardIterator __middle
= __first
;
1828 std::advance(__middle
, __len
/ 2);
1829 _ForwardIterator __begin
=
1830 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1831 __len
/ 2, __buffer
,
1833 _ForwardIterator __end
=
1834 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1836 __buffer
, __buffer_size
);
1837 std::rotate(__begin
, __middle
, __end
);
1838 std::advance(__begin
, std::distance(__middle
, __end
));
1844 * @brief Move elements for which a predicate is true to the beginning
1845 * of a sequence, preserving relative ordering.
1846 * @ingroup mutating_algorithms
1847 * @param first A forward iterator.
1848 * @param last A forward iterator.
1849 * @param pred A predicate functor.
1850 * @return An iterator @p middle such that @p pred(i) is true for each
1851 * iterator @p i in the range @p [first,middle) and false for each @p i
1852 * in the range @p [middle,last).
1854 * Performs the same function as @p partition() with the additional
1855 * guarantee that the relative ordering of elements in each group is
1856 * preserved, so any two elements @p x and @p y in the range
1857 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1858 * relative ordering after calling @p stable_partition().
1860 template<typename _ForwardIterator
, typename _Predicate
>
1862 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1865 // concept requirements
1866 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1868 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1869 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1870 __glibcxx_requires_valid_range(__first
, __last
);
1872 if (__first
== __last
)
1876 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1878 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1881 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1883 if (__buf
.size() > 0)
1885 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1886 _DistanceType(__buf
.requested_size()),
1888 _DistanceType(__buf
.size()));
1891 std::__inplace_stable_partition(__first
, __last
, __pred
,
1892 _DistanceType(__buf
.requested_size()));
1896 /// This is a helper function for the sort routines.
1897 template<typename _RandomAccessIterator
>
1899 __heap_select(_RandomAccessIterator __first
,
1900 _RandomAccessIterator __middle
,
1901 _RandomAccessIterator __last
)
1903 std::make_heap(__first
, __middle
);
1904 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1905 if (*__i
< *__first
)
1906 std::__pop_heap(__first
, __middle
, __i
);
1909 /// This is a helper function for the sort routines.
1910 template<typename _RandomAccessIterator
, typename _Compare
>
1912 __heap_select(_RandomAccessIterator __first
,
1913 _RandomAccessIterator __middle
,
1914 _RandomAccessIterator __last
, _Compare __comp
)
1916 std::make_heap(__first
, __middle
, __comp
);
1917 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1918 if (__comp(*__i
, *__first
))
1919 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1925 * @brief Copy the smallest elements of a sequence.
1926 * @ingroup sorting_algorithms
1927 * @param first An iterator.
1928 * @param last Another iterator.
1929 * @param result_first A random-access iterator.
1930 * @param result_last Another random-access iterator.
1931 * @return An iterator indicating the end of the resulting sequence.
1933 * Copies and sorts the smallest N values from the range @p [first,last)
1934 * to the range beginning at @p result_first, where the number of
1935 * elements to be copied, @p N, is the smaller of @p (last-first) and
1936 * @p (result_last-result_first).
1937 * After the sort if @p i and @j are iterators in the range
1938 * @p [result_first,result_first+N) such that @i precedes @j then
1939 * @p *j<*i is false.
1940 * The value returned is @p result_first+N.
1942 template<typename _InputIterator
, typename _RandomAccessIterator
>
1943 _RandomAccessIterator
1944 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1945 _RandomAccessIterator __result_first
,
1946 _RandomAccessIterator __result_last
)
1948 typedef typename iterator_traits
<_InputIterator
>::value_type
1950 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1952 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1955 // concept requirements
1956 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1957 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1959 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1961 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1962 __glibcxx_requires_valid_range(__first
, __last
);
1963 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1965 if (__result_first
== __result_last
)
1966 return __result_last
;
1967 _RandomAccessIterator __result_real_last
= __result_first
;
1968 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1970 *__result_real_last
= *__first
;
1971 ++__result_real_last
;
1974 std::make_heap(__result_first
, __result_real_last
);
1975 while (__first
!= __last
)
1977 if (*__first
< *__result_first
)
1978 std::__adjust_heap(__result_first
, _DistanceType(0),
1979 _DistanceType(__result_real_last
1981 _InputValueType(*__first
));
1984 std::sort_heap(__result_first
, __result_real_last
);
1985 return __result_real_last
;
1989 * @brief Copy the smallest elements of a sequence using a predicate for
1991 * @ingroup sorting_algorithms
1992 * @param first An input iterator.
1993 * @param last Another input iterator.
1994 * @param result_first A random-access iterator.
1995 * @param result_last Another random-access iterator.
1996 * @param comp A comparison functor.
1997 * @return An iterator indicating the end of the resulting sequence.
1999 * Copies and sorts the smallest N values from the range @p [first,last)
2000 * to the range beginning at @p result_first, where the number of
2001 * elements to be copied, @p N, is the smaller of @p (last-first) and
2002 * @p (result_last-result_first).
2003 * After the sort if @p i and @j are iterators in the range
2004 * @p [result_first,result_first+N) such that @i precedes @j then
2005 * @p comp(*j,*i) is false.
2006 * The value returned is @p result_first+N.
2008 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2009 _RandomAccessIterator
2010 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2011 _RandomAccessIterator __result_first
,
2012 _RandomAccessIterator __result_last
,
2015 typedef typename iterator_traits
<_InputIterator
>::value_type
2017 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2019 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2022 // concept requirements
2023 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2024 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2025 _RandomAccessIterator
>)
2026 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2028 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2029 _InputValueType
, _OutputValueType
>)
2030 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2031 _OutputValueType
, _OutputValueType
>)
2032 __glibcxx_requires_valid_range(__first
, __last
);
2033 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2035 if (__result_first
== __result_last
)
2036 return __result_last
;
2037 _RandomAccessIterator __result_real_last
= __result_first
;
2038 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2040 *__result_real_last
= *__first
;
2041 ++__result_real_last
;
2044 std::make_heap(__result_first
, __result_real_last
, __comp
);
2045 while (__first
!= __last
)
2047 if (__comp(*__first
, *__result_first
))
2048 std::__adjust_heap(__result_first
, _DistanceType(0),
2049 _DistanceType(__result_real_last
2051 _InputValueType(*__first
),
2055 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2056 return __result_real_last
;
2059 /// This is a helper function for the sort routine.
2060 template<typename _RandomAccessIterator
>
2062 __unguarded_linear_insert(_RandomAccessIterator __last
)
2064 typename iterator_traits
<_RandomAccessIterator
>::value_type
2065 __val
= _GLIBCXX_MOVE(*__last
);
2066 _RandomAccessIterator __next
= __last
;
2068 while (__val
< *__next
)
2070 *__last
= _GLIBCXX_MOVE(*__next
);
2074 *__last
= _GLIBCXX_MOVE(__val
);
2077 /// This is a helper function for the sort routine.
2078 template<typename _RandomAccessIterator
, typename _Compare
>
2080 __unguarded_linear_insert(_RandomAccessIterator __last
,
2083 typename iterator_traits
<_RandomAccessIterator
>::value_type
2084 __val
= _GLIBCXX_MOVE(*__last
);
2085 _RandomAccessIterator __next
= __last
;
2087 while (__comp(__val
, *__next
))
2089 *__last
= _GLIBCXX_MOVE(*__next
);
2093 *__last
= _GLIBCXX_MOVE(__val
);
2096 /// This is a helper function for the sort routine.
2097 template<typename _RandomAccessIterator
>
2099 __insertion_sort(_RandomAccessIterator __first
,
2100 _RandomAccessIterator __last
)
2102 if (__first
== __last
)
2105 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2107 if (*__i
< *__first
)
2109 typename iterator_traits
<_RandomAccessIterator
>::value_type
2110 __val
= _GLIBCXX_MOVE(*__i
);
2111 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2112 *__first
= _GLIBCXX_MOVE(__val
);
2115 std::__unguarded_linear_insert(__i
);
2119 /// This is a helper function for the sort routine.
2120 template<typename _RandomAccessIterator
, typename _Compare
>
2122 __insertion_sort(_RandomAccessIterator __first
,
2123 _RandomAccessIterator __last
, _Compare __comp
)
2125 if (__first
== __last
) return;
2127 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2129 if (__comp(*__i
, *__first
))
2131 typename iterator_traits
<_RandomAccessIterator
>::value_type
2132 __val
= _GLIBCXX_MOVE(*__i
);
2133 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2134 *__first
= _GLIBCXX_MOVE(__val
);
2137 std::__unguarded_linear_insert(__i
, __comp
);
2141 /// This is a helper function for the sort routine.
2142 template<typename _RandomAccessIterator
>
2144 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2145 _RandomAccessIterator __last
)
2147 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2150 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2151 std::__unguarded_linear_insert(__i
);
2154 /// This is a helper function for the sort routine.
2155 template<typename _RandomAccessIterator
, typename _Compare
>
2157 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2158 _RandomAccessIterator __last
, _Compare __comp
)
2160 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2163 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2164 std::__unguarded_linear_insert(__i
, __comp
);
2169 * This controls some aspect of the sort routines.
2171 enum { _S_threshold
= 16 };
2173 /// This is a helper function for the sort routine.
2174 template<typename _RandomAccessIterator
>
2176 __final_insertion_sort(_RandomAccessIterator __first
,
2177 _RandomAccessIterator __last
)
2179 if (__last
- __first
> int(_S_threshold
))
2181 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2182 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2185 std::__insertion_sort(__first
, __last
);
2188 /// This is a helper function for the sort routine.
2189 template<typename _RandomAccessIterator
, typename _Compare
>
2191 __final_insertion_sort(_RandomAccessIterator __first
,
2192 _RandomAccessIterator __last
, _Compare __comp
)
2194 if (__last
- __first
> int(_S_threshold
))
2196 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2197 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2201 std::__insertion_sort(__first
, __last
, __comp
);
2204 /// This is a helper function...
2205 template<typename _RandomAccessIterator
, typename _Tp
>
2206 _RandomAccessIterator
2207 __unguarded_partition(_RandomAccessIterator __first
,
2208 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2212 while (*__first
< __pivot
)
2215 while (__pivot
< *__last
)
2217 if (!(__first
< __last
))
2219 std::iter_swap(__first
, __last
);
2224 /// This is a helper function...
2225 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2226 _RandomAccessIterator
2227 __unguarded_partition(_RandomAccessIterator __first
,
2228 _RandomAccessIterator __last
,
2229 const _Tp
& __pivot
, _Compare __comp
)
2233 while (__comp(*__first
, __pivot
))
2236 while (__comp(__pivot
, *__last
))
2238 if (!(__first
< __last
))
2240 std::iter_swap(__first
, __last
);
2245 /// This is a helper function...
2246 template<typename _RandomAccessIterator
>
2247 inline _RandomAccessIterator
2248 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2249 _RandomAccessIterator __last
)
2251 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2252 std::__move_median_first(__first
, __mid
, (__last
- 1));
2253 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2257 /// This is a helper function...
2258 template<typename _RandomAccessIterator
, typename _Compare
>
2259 inline _RandomAccessIterator
2260 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2261 _RandomAccessIterator __last
, _Compare __comp
)
2263 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2264 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2265 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2268 /// This is a helper function for the sort routine.
2269 template<typename _RandomAccessIterator
, typename _Size
>
2271 __introsort_loop(_RandomAccessIterator __first
,
2272 _RandomAccessIterator __last
,
2273 _Size __depth_limit
)
2275 while (__last
- __first
> int(_S_threshold
))
2277 if (__depth_limit
== 0)
2279 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
);
2283 _RandomAccessIterator __cut
=
2284 std::__unguarded_partition_pivot(__first
, __last
);
2285 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2290 /// This is a helper function for the sort routine.
2291 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2293 __introsort_loop(_RandomAccessIterator __first
,
2294 _RandomAccessIterator __last
,
2295 _Size __depth_limit
, _Compare __comp
)
2297 while (__last
- __first
> int(_S_threshold
))
2299 if (__depth_limit
== 0)
2301 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
, __comp
);
2305 _RandomAccessIterator __cut
=
2306 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2307 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2314 template<typename _RandomAccessIterator
, typename _Size
>
2316 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2317 _RandomAccessIterator __last
, _Size __depth_limit
)
2319 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2322 while (__last
- __first
> 3)
2324 if (__depth_limit
== 0)
2326 std::__heap_select(__first
, __nth
+ 1, __last
);
2328 // Place the nth largest element in its final position.
2329 std::iter_swap(__first
, __nth
);
2333 _RandomAccessIterator __cut
=
2334 std::__unguarded_partition_pivot(__first
, __last
);
2340 std::__insertion_sort(__first
, __last
);
2343 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2345 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2346 _RandomAccessIterator __last
, _Size __depth_limit
,
2349 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2352 while (__last
- __first
> 3)
2354 if (__depth_limit
== 0)
2356 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2357 // Place the nth largest element in its final position.
2358 std::iter_swap(__first
, __nth
);
2362 _RandomAccessIterator __cut
=
2363 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2369 std::__insertion_sort(__first
, __last
, __comp
);
2374 // lower_bound moved to stl_algobase.h
2377 * @brief Finds the first position in which @a val could be inserted
2378 * without changing the ordering.
2379 * @ingroup binary_search_algorithms
2380 * @param first An iterator.
2381 * @param last Another iterator.
2382 * @param val The search term.
2383 * @param comp A functor to use for comparisons.
2384 * @return An iterator pointing to the first element <em>not less
2385 * than</em> @a val, or end() if every element is less
2387 * @ingroup binary_search_algorithms
2389 * The comparison function should have the same effects on ordering as
2390 * the function used for the initial sort.
2392 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2394 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2395 const _Tp
& __val
, _Compare __comp
)
2397 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2399 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2402 // concept requirements
2403 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2404 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2406 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2409 _DistanceType __len
= std::distance(__first
, __last
);
2413 _DistanceType __half
= __len
>> 1;
2414 _ForwardIterator __middle
= __first
;
2415 std::advance(__middle
, __half
);
2416 if (__comp(*__middle
, __val
))
2420 __len
= __len
- __half
- 1;
2429 * @brief Finds the last position in which @a val could be inserted
2430 * without changing the ordering.
2431 * @ingroup binary_search_algorithms
2432 * @param first An iterator.
2433 * @param last Another iterator.
2434 * @param val The search term.
2435 * @return An iterator pointing to the first element greater than @a val,
2436 * or end() if no elements are greater than @a val.
2437 * @ingroup binary_search_algorithms
2439 template<typename _ForwardIterator
, typename _Tp
>
2441 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2444 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2446 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2449 // concept requirements
2450 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2451 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2452 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2454 _DistanceType __len
= std::distance(__first
, __last
);
2458 _DistanceType __half
= __len
>> 1;
2459 _ForwardIterator __middle
= __first
;
2460 std::advance(__middle
, __half
);
2461 if (__val
< *__middle
)
2467 __len
= __len
- __half
- 1;
2474 * @brief Finds the last position in which @a val could be inserted
2475 * without changing the ordering.
2476 * @ingroup binary_search_algorithms
2477 * @param first An iterator.
2478 * @param last Another iterator.
2479 * @param val The search term.
2480 * @param comp A functor to use for comparisons.
2481 * @return An iterator pointing to the first element greater than @a val,
2482 * or end() if no elements are greater than @a val.
2483 * @ingroup binary_search_algorithms
2485 * The comparison function should have the same effects on ordering as
2486 * the function used for the initial sort.
2488 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2490 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2491 const _Tp
& __val
, _Compare __comp
)
2493 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2495 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2498 // concept requirements
2499 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2500 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2502 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2505 _DistanceType __len
= std::distance(__first
, __last
);
2509 _DistanceType __half
= __len
>> 1;
2510 _ForwardIterator __middle
= __first
;
2511 std::advance(__middle
, __half
);
2512 if (__comp(__val
, *__middle
))
2518 __len
= __len
- __half
- 1;
2525 * @brief Finds the largest subrange in which @a val could be inserted
2526 * at any place in it without changing the ordering.
2527 * @ingroup binary_search_algorithms
2528 * @param first An iterator.
2529 * @param last Another iterator.
2530 * @param val The search term.
2531 * @return An pair of iterators defining the subrange.
2532 * @ingroup binary_search_algorithms
2534 * This is equivalent to
2536 * std::make_pair(lower_bound(first, last, val),
2537 * upper_bound(first, last, val))
2539 * but does not actually call those functions.
2541 template<typename _ForwardIterator
, typename _Tp
>
2542 pair
<_ForwardIterator
, _ForwardIterator
>
2543 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2546 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2548 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2551 // concept requirements
2552 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2553 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2554 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2555 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2556 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2558 _DistanceType __len
= std::distance(__first
, __last
);
2562 _DistanceType __half
= __len
>> 1;
2563 _ForwardIterator __middle
= __first
;
2564 std::advance(__middle
, __half
);
2565 if (*__middle
< __val
)
2569 __len
= __len
- __half
- 1;
2571 else if (__val
< *__middle
)
2575 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2577 std::advance(__first
, __len
);
2578 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2580 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2583 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2587 * @brief Finds the largest subrange in which @a val could be inserted
2588 * at any place in it without changing the ordering.
2589 * @param first An iterator.
2590 * @param last Another iterator.
2591 * @param val The search term.
2592 * @param comp A functor to use for comparisons.
2593 * @return An pair of iterators defining the subrange.
2594 * @ingroup binary_search_algorithms
2596 * This is equivalent to
2598 * std::make_pair(lower_bound(first, last, val, comp),
2599 * upper_bound(first, last, val, comp))
2601 * but does not actually call those functions.
2603 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2604 pair
<_ForwardIterator
, _ForwardIterator
>
2605 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2606 const _Tp
& __val
, _Compare __comp
)
2608 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2610 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2613 // concept requirements
2614 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2615 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2617 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2619 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2621 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2624 _DistanceType __len
= std::distance(__first
, __last
);
2628 _DistanceType __half
= __len
>> 1;
2629 _ForwardIterator __middle
= __first
;
2630 std::advance(__middle
, __half
);
2631 if (__comp(*__middle
, __val
))
2635 __len
= __len
- __half
- 1;
2637 else if (__comp(__val
, *__middle
))
2641 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2643 std::advance(__first
, __len
);
2644 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2646 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2649 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2653 * @brief Determines whether an element exists in a range.
2654 * @ingroup binary_search_algorithms
2655 * @param first An iterator.
2656 * @param last Another iterator.
2657 * @param val The search term.
2658 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2660 * Note that this does not actually return an iterator to @a val. For
2661 * that, use std::find or a container's specialized find member functions.
2663 template<typename _ForwardIterator
, typename _Tp
>
2665 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2668 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2671 // concept requirements
2672 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2673 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2674 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2675 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2677 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2678 return __i
!= __last
&& !(__val
< *__i
);
2682 * @brief Determines whether an element exists in a range.
2683 * @ingroup binary_search_algorithms
2684 * @param first An iterator.
2685 * @param last Another iterator.
2686 * @param val The search term.
2687 * @param comp A functor to use for comparisons.
2688 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2690 * Note that this does not actually return an iterator to @a val. For
2691 * that, use std::find or a container's specialized find member functions.
2693 * The comparison function should have the same effects on ordering as
2694 * the function used for the initial sort.
2696 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2698 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2699 const _Tp
& __val
, _Compare __comp
)
2701 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2704 // concept requirements
2705 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2706 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2708 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2710 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2713 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2714 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2719 /// This is a helper function for the __merge_adaptive routines.
2720 template<typename _InputIterator1
, typename _InputIterator2
,
2721 typename _OutputIterator
>
2723 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2724 _InputIterator2 __first2
, _InputIterator2 __last2
,
2725 _OutputIterator __result
)
2727 while (__first1
!= __last1
&& __first2
!= __last2
)
2729 if (*__first2
< *__first1
)
2731 *__result
= _GLIBCXX_MOVE(*__first2
);
2736 *__result
= _GLIBCXX_MOVE(*__first1
);
2741 if (__first1
!= __last1
)
2742 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2745 /// This is a helper function for the __merge_adaptive routines.
2746 template<typename _InputIterator1
, typename _InputIterator2
,
2747 typename _OutputIterator
, typename _Compare
>
2749 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2750 _InputIterator2 __first2
, _InputIterator2 __last2
,
2751 _OutputIterator __result
, _Compare __comp
)
2753 while (__first1
!= __last1
&& __first2
!= __last2
)
2755 if (__comp(*__first2
, *__first1
))
2757 *__result
= _GLIBCXX_MOVE(*__first2
);
2762 *__result
= _GLIBCXX_MOVE(*__first1
);
2767 if (__first1
!= __last1
)
2768 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2771 /// This is a helper function for the __merge_adaptive routines.
2772 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2773 typename _BidirectionalIterator3
>
2775 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2776 _BidirectionalIterator1 __last1
,
2777 _BidirectionalIterator2 __first2
,
2778 _BidirectionalIterator2 __last2
,
2779 _BidirectionalIterator3 __result
)
2781 if (__first1
== __last1
)
2783 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2786 else if (__first2
== __last2
)
2793 if (*__last2
< *__last1
)
2795 *--__result
= _GLIBCXX_MOVE(*__last1
);
2796 if (__first1
== __last1
)
2798 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2805 *--__result
= _GLIBCXX_MOVE(*__last2
);
2806 if (__first2
== __last2
)
2813 /// This is a helper function for the __merge_adaptive routines.
2814 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2815 typename _BidirectionalIterator3
, typename _Compare
>
2817 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2818 _BidirectionalIterator1 __last1
,
2819 _BidirectionalIterator2 __first2
,
2820 _BidirectionalIterator2 __last2
,
2821 _BidirectionalIterator3 __result
,
2824 if (__first1
== __last1
)
2826 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2829 else if (__first2
== __last2
)
2836 if (__comp(*__last2
, *__last1
))
2838 *--__result
= _GLIBCXX_MOVE(*__last1
);
2839 if (__first1
== __last1
)
2841 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2848 *--__result
= _GLIBCXX_MOVE(*__last2
);
2849 if (__first2
== __last2
)
2856 /// This is a helper function for the merge routines.
2857 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2859 _BidirectionalIterator1
2860 __rotate_adaptive(_BidirectionalIterator1 __first
,
2861 _BidirectionalIterator1 __middle
,
2862 _BidirectionalIterator1 __last
,
2863 _Distance __len1
, _Distance __len2
,
2864 _BidirectionalIterator2 __buffer
,
2865 _Distance __buffer_size
)
2867 _BidirectionalIterator2 __buffer_end
;
2868 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2872 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2873 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2874 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2879 else if (__len1
<= __buffer_size
)
2883 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2884 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2885 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2892 std::rotate(__first
, __middle
, __last
);
2893 std::advance(__first
, std::distance(__middle
, __last
));
2898 /// This is a helper function for the merge routines.
2899 template<typename _BidirectionalIterator
, typename _Distance
,
2902 __merge_adaptive(_BidirectionalIterator __first
,
2903 _BidirectionalIterator __middle
,
2904 _BidirectionalIterator __last
,
2905 _Distance __len1
, _Distance __len2
,
2906 _Pointer __buffer
, _Distance __buffer_size
)
2908 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2910 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2911 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2914 else if (__len2
<= __buffer_size
)
2916 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2917 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2918 __buffer_end
, __last
);
2922 _BidirectionalIterator __first_cut
= __first
;
2923 _BidirectionalIterator __second_cut
= __middle
;
2924 _Distance __len11
= 0;
2925 _Distance __len22
= 0;
2926 if (__len1
> __len2
)
2928 __len11
= __len1
/ 2;
2929 std::advance(__first_cut
, __len11
);
2930 __second_cut
= std::lower_bound(__middle
, __last
,
2932 __len22
= std::distance(__middle
, __second_cut
);
2936 __len22
= __len2
/ 2;
2937 std::advance(__second_cut
, __len22
);
2938 __first_cut
= std::upper_bound(__first
, __middle
,
2940 __len11
= std::distance(__first
, __first_cut
);
2942 _BidirectionalIterator __new_middle
=
2943 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2944 __len1
- __len11
, __len22
, __buffer
,
2946 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2947 __len22
, __buffer
, __buffer_size
);
2948 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2950 __len2
- __len22
, __buffer
, __buffer_size
);
2954 /// This is a helper function for the merge routines.
2955 template<typename _BidirectionalIterator
, typename _Distance
,
2956 typename _Pointer
, typename _Compare
>
2958 __merge_adaptive(_BidirectionalIterator __first
,
2959 _BidirectionalIterator __middle
,
2960 _BidirectionalIterator __last
,
2961 _Distance __len1
, _Distance __len2
,
2962 _Pointer __buffer
, _Distance __buffer_size
,
2965 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2967 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2968 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2971 else if (__len2
<= __buffer_size
)
2973 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2974 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2975 __buffer_end
, __last
, __comp
);
2979 _BidirectionalIterator __first_cut
= __first
;
2980 _BidirectionalIterator __second_cut
= __middle
;
2981 _Distance __len11
= 0;
2982 _Distance __len22
= 0;
2983 if (__len1
> __len2
)
2985 __len11
= __len1
/ 2;
2986 std::advance(__first_cut
, __len11
);
2987 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2989 __len22
= std::distance(__middle
, __second_cut
);
2993 __len22
= __len2
/ 2;
2994 std::advance(__second_cut
, __len22
);
2995 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2997 __len11
= std::distance(__first
, __first_cut
);
2999 _BidirectionalIterator __new_middle
=
3000 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3001 __len1
- __len11
, __len22
, __buffer
,
3003 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3004 __len22
, __buffer
, __buffer_size
, __comp
);
3005 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3007 __len2
- __len22
, __buffer
,
3008 __buffer_size
, __comp
);
3012 /// This is a helper function for the merge routines.
3013 template<typename _BidirectionalIterator
, typename _Distance
>
3015 __merge_without_buffer(_BidirectionalIterator __first
,
3016 _BidirectionalIterator __middle
,
3017 _BidirectionalIterator __last
,
3018 _Distance __len1
, _Distance __len2
)
3020 if (__len1
== 0 || __len2
== 0)
3022 if (__len1
+ __len2
== 2)
3024 if (*__middle
< *__first
)
3025 std::iter_swap(__first
, __middle
);
3028 _BidirectionalIterator __first_cut
= __first
;
3029 _BidirectionalIterator __second_cut
= __middle
;
3030 _Distance __len11
= 0;
3031 _Distance __len22
= 0;
3032 if (__len1
> __len2
)
3034 __len11
= __len1
/ 2;
3035 std::advance(__first_cut
, __len11
);
3036 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3037 __len22
= std::distance(__middle
, __second_cut
);
3041 __len22
= __len2
/ 2;
3042 std::advance(__second_cut
, __len22
);
3043 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3044 __len11
= std::distance(__first
, __first_cut
);
3046 std::rotate(__first_cut
, __middle
, __second_cut
);
3047 _BidirectionalIterator __new_middle
= __first_cut
;
3048 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3049 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3051 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3052 __len1
- __len11
, __len2
- __len22
);
3055 /// This is a helper function for the merge routines.
3056 template<typename _BidirectionalIterator
, typename _Distance
,
3059 __merge_without_buffer(_BidirectionalIterator __first
,
3060 _BidirectionalIterator __middle
,
3061 _BidirectionalIterator __last
,
3062 _Distance __len1
, _Distance __len2
,
3065 if (__len1
== 0 || __len2
== 0)
3067 if (__len1
+ __len2
== 2)
3069 if (__comp(*__middle
, *__first
))
3070 std::iter_swap(__first
, __middle
);
3073 _BidirectionalIterator __first_cut
= __first
;
3074 _BidirectionalIterator __second_cut
= __middle
;
3075 _Distance __len11
= 0;
3076 _Distance __len22
= 0;
3077 if (__len1
> __len2
)
3079 __len11
= __len1
/ 2;
3080 std::advance(__first_cut
, __len11
);
3081 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3083 __len22
= std::distance(__middle
, __second_cut
);
3087 __len22
= __len2
/ 2;
3088 std::advance(__second_cut
, __len22
);
3089 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3091 __len11
= std::distance(__first
, __first_cut
);
3093 std::rotate(__first_cut
, __middle
, __second_cut
);
3094 _BidirectionalIterator __new_middle
= __first_cut
;
3095 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3096 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3097 __len11
, __len22
, __comp
);
3098 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3099 __len1
- __len11
, __len2
- __len22
, __comp
);
3103 * @brief Merges two sorted ranges in place.
3104 * @ingroup sorting_algorithms
3105 * @param first An iterator.
3106 * @param middle Another iterator.
3107 * @param last Another iterator.
3110 * Merges two sorted and consecutive ranges, [first,middle) and
3111 * [middle,last), and puts the result in [first,last). The output will
3112 * be sorted. The sort is @e stable, that is, for equivalent
3113 * elements in the two ranges, elements from the first range will always
3114 * come before elements from the second.
3116 * If enough additional memory is available, this takes (last-first)-1
3117 * comparisons. Otherwise an NlogN algorithm is used, where N is
3118 * distance(first,last).
3120 template<typename _BidirectionalIterator
>
3122 inplace_merge(_BidirectionalIterator __first
,
3123 _BidirectionalIterator __middle
,
3124 _BidirectionalIterator __last
)
3126 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3128 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3131 // concept requirements
3132 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3133 _BidirectionalIterator
>)
3134 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3135 __glibcxx_requires_sorted(__first
, __middle
);
3136 __glibcxx_requires_sorted(__middle
, __last
);
3138 if (__first
== __middle
|| __middle
== __last
)
3141 _DistanceType __len1
= std::distance(__first
, __middle
);
3142 _DistanceType __len2
= std::distance(__middle
, __last
);
3144 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3146 if (__buf
.begin() == 0)
3147 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3149 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3150 __buf
.begin(), _DistanceType(__buf
.size()));
3154 * @brief Merges two sorted ranges in place.
3155 * @ingroup sorting_algorithms
3156 * @param first An iterator.
3157 * @param middle Another iterator.
3158 * @param last Another iterator.
3159 * @param comp A functor to use for comparisons.
3162 * Merges two sorted and consecutive ranges, [first,middle) and
3163 * [middle,last), and puts the result in [first,last). The output will
3164 * be sorted. The sort is @e stable, that is, for equivalent
3165 * elements in the two ranges, elements from the first range will always
3166 * come before elements from the second.
3168 * If enough additional memory is available, this takes (last-first)-1
3169 * comparisons. Otherwise an NlogN algorithm is used, where N is
3170 * distance(first,last).
3172 * The comparison function should have the same effects on ordering as
3173 * the function used for the initial sort.
3175 template<typename _BidirectionalIterator
, typename _Compare
>
3177 inplace_merge(_BidirectionalIterator __first
,
3178 _BidirectionalIterator __middle
,
3179 _BidirectionalIterator __last
,
3182 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3184 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3187 // concept requirements
3188 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3189 _BidirectionalIterator
>)
3190 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3191 _ValueType
, _ValueType
>)
3192 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3193 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3195 if (__first
== __middle
|| __middle
== __last
)
3198 const _DistanceType __len1
= std::distance(__first
, __middle
);
3199 const _DistanceType __len2
= std::distance(__middle
, __last
);
3201 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3203 if (__buf
.begin() == 0)
3204 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3207 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3208 __buf
.begin(), _DistanceType(__buf
.size()),
3213 /// This is a helper function for the __merge_sort_loop routines.
3214 template<typename _InputIterator1
, typename _InputIterator2
,
3215 typename _OutputIterator
>
3217 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3218 _InputIterator2 __first2
, _InputIterator2 __last2
,
3219 _OutputIterator __result
)
3221 while (__first1
!= __last1
&& __first2
!= __last2
)
3223 if (*__first2
< *__first1
)
3225 *__result
= _GLIBCXX_MOVE(*__first2
);
3230 *__result
= _GLIBCXX_MOVE(*__first1
);
3235 return _GLIBCXX_MOVE3(__first2
, __last2
,
3236 _GLIBCXX_MOVE3(__first1
, __last1
,
3240 /// This is a helper function for the __merge_sort_loop routines.
3241 template<typename _InputIterator1
, typename _InputIterator2
,
3242 typename _OutputIterator
, typename _Compare
>
3244 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3245 _InputIterator2 __first2
, _InputIterator2 __last2
,
3246 _OutputIterator __result
, _Compare __comp
)
3248 while (__first1
!= __last1
&& __first2
!= __last2
)
3250 if (__comp(*__first2
, *__first1
))
3252 *__result
= _GLIBCXX_MOVE(*__first2
);
3257 *__result
= _GLIBCXX_MOVE(*__first1
);
3262 return _GLIBCXX_MOVE3(__first2
, __last2
,
3263 _GLIBCXX_MOVE3(__first1
, __last1
,
3267 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3270 __merge_sort_loop(_RandomAccessIterator1 __first
,
3271 _RandomAccessIterator1 __last
,
3272 _RandomAccessIterator2 __result
,
3273 _Distance __step_size
)
3275 const _Distance __two_step
= 2 * __step_size
;
3277 while (__last
- __first
>= __two_step
)
3279 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3280 __first
+ __step_size
,
3281 __first
+ __two_step
, __result
);
3282 __first
+= __two_step
;
3285 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3286 std::__move_merge(__first
, __first
+ __step_size
,
3287 __first
+ __step_size
, __last
, __result
);
3290 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3291 typename _Distance
, typename _Compare
>
3293 __merge_sort_loop(_RandomAccessIterator1 __first
,
3294 _RandomAccessIterator1 __last
,
3295 _RandomAccessIterator2 __result
, _Distance __step_size
,
3298 const _Distance __two_step
= 2 * __step_size
;
3300 while (__last
- __first
>= __two_step
)
3302 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3303 __first
+ __step_size
,
3304 __first
+ __two_step
,
3306 __first
+= __two_step
;
3308 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3310 std::__move_merge(__first
,__first
+ __step_size
,
3311 __first
+ __step_size
, __last
, __result
, __comp
);
3314 template<typename _RandomAccessIterator
, typename _Distance
>
3316 __chunk_insertion_sort(_RandomAccessIterator __first
,
3317 _RandomAccessIterator __last
,
3318 _Distance __chunk_size
)
3320 while (__last
- __first
>= __chunk_size
)
3322 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3323 __first
+= __chunk_size
;
3325 std::__insertion_sort(__first
, __last
);
3328 template<typename _RandomAccessIterator
, typename _Distance
,
3331 __chunk_insertion_sort(_RandomAccessIterator __first
,
3332 _RandomAccessIterator __last
,
3333 _Distance __chunk_size
, _Compare __comp
)
3335 while (__last
- __first
>= __chunk_size
)
3337 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3338 __first
+= __chunk_size
;
3340 std::__insertion_sort(__first
, __last
, __comp
);
3343 enum { _S_chunk_size
= 7 };
3345 template<typename _RandomAccessIterator
, typename _Pointer
>
3347 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3348 _RandomAccessIterator __last
,
3351 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3354 const _Distance __len
= __last
- __first
;
3355 const _Pointer __buffer_last
= __buffer
+ __len
;
3357 _Distance __step_size
= _S_chunk_size
;
3358 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3360 while (__step_size
< __len
)
3362 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3364 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3369 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3371 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3372 _RandomAccessIterator __last
,
3373 _Pointer __buffer
, _Compare __comp
)
3375 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3378 const _Distance __len
= __last
- __first
;
3379 const _Pointer __buffer_last
= __buffer
+ __len
;
3381 _Distance __step_size
= _S_chunk_size
;
3382 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3384 while (__step_size
< __len
)
3386 std::__merge_sort_loop(__first
, __last
, __buffer
,
3387 __step_size
, __comp
);
3389 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3390 __step_size
, __comp
);
3395 template<typename _RandomAccessIterator
, typename _Pointer
,
3398 __stable_sort_adaptive(_RandomAccessIterator __first
,
3399 _RandomAccessIterator __last
,
3400 _Pointer __buffer
, _Distance __buffer_size
)
3402 const _Distance __len
= (__last
- __first
+ 1) / 2;
3403 const _RandomAccessIterator __middle
= __first
+ __len
;
3404 if (__len
> __buffer_size
)
3406 std::__stable_sort_adaptive(__first
, __middle
,
3407 __buffer
, __buffer_size
);
3408 std::__stable_sort_adaptive(__middle
, __last
,
3409 __buffer
, __buffer_size
);
3413 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3414 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3416 std::__merge_adaptive(__first
, __middle
, __last
,
3417 _Distance(__middle
- __first
),
3418 _Distance(__last
- __middle
),
3419 __buffer
, __buffer_size
);
3422 template<typename _RandomAccessIterator
, typename _Pointer
,
3423 typename _Distance
, typename _Compare
>
3425 __stable_sort_adaptive(_RandomAccessIterator __first
,
3426 _RandomAccessIterator __last
,
3427 _Pointer __buffer
, _Distance __buffer_size
,
3430 const _Distance __len
= (__last
- __first
+ 1) / 2;
3431 const _RandomAccessIterator __middle
= __first
+ __len
;
3432 if (__len
> __buffer_size
)
3434 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3435 __buffer_size
, __comp
);
3436 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3437 __buffer_size
, __comp
);
3441 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3442 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3444 std::__merge_adaptive(__first
, __middle
, __last
,
3445 _Distance(__middle
- __first
),
3446 _Distance(__last
- __middle
),
3447 __buffer
, __buffer_size
,
3451 /// This is a helper function for the stable sorting routines.
3452 template<typename _RandomAccessIterator
>
3454 __inplace_stable_sort(_RandomAccessIterator __first
,
3455 _RandomAccessIterator __last
)
3457 if (__last
- __first
< 15)
3459 std::__insertion_sort(__first
, __last
);
3462 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3463 std::__inplace_stable_sort(__first
, __middle
);
3464 std::__inplace_stable_sort(__middle
, __last
);
3465 std::__merge_without_buffer(__first
, __middle
, __last
,
3470 /// This is a helper function for the stable sorting routines.
3471 template<typename _RandomAccessIterator
, typename _Compare
>
3473 __inplace_stable_sort(_RandomAccessIterator __first
,
3474 _RandomAccessIterator __last
, _Compare __comp
)
3476 if (__last
- __first
< 15)
3478 std::__insertion_sort(__first
, __last
, __comp
);
3481 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3482 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3483 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3484 std::__merge_without_buffer(__first
, __middle
, __last
,
3492 // Set algorithms: includes, set_union, set_intersection, set_difference,
3493 // set_symmetric_difference. All of these algorithms have the precondition
3494 // that their input ranges are sorted and the postcondition that their output
3495 // ranges are sorted.
3498 * @brief Determines whether all elements of a sequence exists in a range.
3499 * @param first1 Start of search range.
3500 * @param last1 End of search range.
3501 * @param first2 Start of sequence
3502 * @param last2 End of sequence.
3503 * @return True if each element in [first2,last2) is contained in order
3504 * within [first1,last1). False otherwise.
3505 * @ingroup set_algorithms
3507 * This operation expects both [first1,last1) and [first2,last2) to be
3508 * sorted. Searches for the presence of each element in [first2,last2)
3509 * within [first1,last1). The iterators over each range only move forward,
3510 * so this is a linear algorithm. If an element in [first2,last2) is not
3511 * found before the search iterator reaches @a last2, false is returned.
3513 template<typename _InputIterator1
, typename _InputIterator2
>
3515 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3516 _InputIterator2 __first2
, _InputIterator2 __last2
)
3518 typedef typename iterator_traits
<_InputIterator1
>::value_type
3520 typedef typename iterator_traits
<_InputIterator2
>::value_type
3523 // concept requirements
3524 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3525 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3526 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3527 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3528 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3529 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3531 while (__first1
!= __last1
&& __first2
!= __last2
)
3532 if (*__first2
< *__first1
)
3534 else if(*__first1
< *__first2
)
3537 ++__first1
, ++__first2
;
3539 return __first2
== __last2
;
3543 * @brief Determines whether all elements of a sequence exists in a range
3545 * @ingroup set_algorithms
3546 * @param first1 Start of search range.
3547 * @param last1 End of search range.
3548 * @param first2 Start of sequence
3549 * @param last2 End of sequence.
3550 * @param comp Comparison function to use.
3551 * @return True if each element in [first2,last2) is contained in order
3552 * within [first1,last1) according to comp. False otherwise.
3553 * @ingroup set_algorithms
3555 * This operation expects both [first1,last1) and [first2,last2) to be
3556 * sorted. Searches for the presence of each element in [first2,last2)
3557 * within [first1,last1), using comp to decide. The iterators over each
3558 * range only move forward, so this is a linear algorithm. If an element
3559 * in [first2,last2) is not found before the search iterator reaches @a
3560 * last2, false is returned.
3562 template<typename _InputIterator1
, typename _InputIterator2
,
3565 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3566 _InputIterator2 __first2
, _InputIterator2 __last2
,
3569 typedef typename iterator_traits
<_InputIterator1
>::value_type
3571 typedef typename iterator_traits
<_InputIterator2
>::value_type
3574 // concept requirements
3575 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3576 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3577 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3578 _ValueType1
, _ValueType2
>)
3579 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3580 _ValueType2
, _ValueType1
>)
3581 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3582 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3584 while (__first1
!= __last1
&& __first2
!= __last2
)
3585 if (__comp(*__first2
, *__first1
))
3587 else if(__comp(*__first1
, *__first2
))
3590 ++__first1
, ++__first2
;
3592 return __first2
== __last2
;
3601 // set_symmetric_difference
3606 * @brief Permute range into the next @a dictionary ordering.
3607 * @ingroup sorting_algorithms
3608 * @param first Start of range.
3609 * @param last End of range.
3610 * @return False if wrapped to first permutation, true otherwise.
3612 * Treats all permutations of the range as a set of @a dictionary sorted
3613 * sequences. Permutes the current sequence into the next one of this set.
3614 * Returns true if there are more sequences to generate. If the sequence
3615 * is the largest of the set, the smallest is generated and false returned.
3617 template<typename _BidirectionalIterator
>
3619 next_permutation(_BidirectionalIterator __first
,
3620 _BidirectionalIterator __last
)
3622 // concept requirements
3623 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3624 _BidirectionalIterator
>)
3625 __glibcxx_function_requires(_LessThanComparableConcept
<
3626 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3627 __glibcxx_requires_valid_range(__first
, __last
);
3629 if (__first
== __last
)
3631 _BidirectionalIterator __i
= __first
;
3640 _BidirectionalIterator __ii
= __i
;
3644 _BidirectionalIterator __j
= __last
;
3645 while (!(*__i
< *--__j
))
3647 std::iter_swap(__i
, __j
);
3648 std::reverse(__ii
, __last
);
3653 std::reverse(__first
, __last
);
3660 * @brief Permute range into the next @a dictionary ordering using
3661 * comparison functor.
3662 * @ingroup sorting_algorithms
3663 * @param first Start of range.
3664 * @param last End of range.
3665 * @param comp A comparison functor.
3666 * @return False if wrapped to first permutation, true otherwise.
3668 * Treats all permutations of the range [first,last) as a set of
3669 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3670 * sequence into the next one of this set. Returns true if there are more
3671 * sequences to generate. If the sequence is the largest of the set, the
3672 * smallest is generated and false returned.
3674 template<typename _BidirectionalIterator
, typename _Compare
>
3676 next_permutation(_BidirectionalIterator __first
,
3677 _BidirectionalIterator __last
, _Compare __comp
)
3679 // concept requirements
3680 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3681 _BidirectionalIterator
>)
3682 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3683 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3684 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3685 __glibcxx_requires_valid_range(__first
, __last
);
3687 if (__first
== __last
)
3689 _BidirectionalIterator __i
= __first
;
3698 _BidirectionalIterator __ii
= __i
;
3700 if (__comp(*__i
, *__ii
))
3702 _BidirectionalIterator __j
= __last
;
3703 while (!bool(__comp(*__i
, *--__j
)))
3705 std::iter_swap(__i
, __j
);
3706 std::reverse(__ii
, __last
);
3711 std::reverse(__first
, __last
);
3718 * @brief Permute range into the previous @a dictionary ordering.
3719 * @ingroup sorting_algorithms
3720 * @param first Start of range.
3721 * @param last End of range.
3722 * @return False if wrapped to last permutation, true otherwise.
3724 * Treats all permutations of the range as a set of @a dictionary sorted
3725 * sequences. Permutes the current sequence into the previous one of this
3726 * set. Returns true if there are more sequences to generate. If the
3727 * sequence is the smallest of the set, the largest is generated and false
3730 template<typename _BidirectionalIterator
>
3732 prev_permutation(_BidirectionalIterator __first
,
3733 _BidirectionalIterator __last
)
3735 // concept requirements
3736 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3737 _BidirectionalIterator
>)
3738 __glibcxx_function_requires(_LessThanComparableConcept
<
3739 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3740 __glibcxx_requires_valid_range(__first
, __last
);
3742 if (__first
== __last
)
3744 _BidirectionalIterator __i
= __first
;
3753 _BidirectionalIterator __ii
= __i
;
3757 _BidirectionalIterator __j
= __last
;
3758 while (!(*--__j
< *__i
))
3760 std::iter_swap(__i
, __j
);
3761 std::reverse(__ii
, __last
);
3766 std::reverse(__first
, __last
);
3773 * @brief Permute range into the previous @a dictionary ordering using
3774 * comparison functor.
3775 * @ingroup sorting_algorithms
3776 * @param first Start of range.
3777 * @param last End of range.
3778 * @param comp A comparison functor.
3779 * @return False if wrapped to last permutation, true otherwise.
3781 * Treats all permutations of the range [first,last) as a set of
3782 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3783 * sequence into the previous one of this set. Returns true if there are
3784 * more sequences to generate. If the sequence is the smallest of the set,
3785 * the largest is generated and false returned.
3787 template<typename _BidirectionalIterator
, typename _Compare
>
3789 prev_permutation(_BidirectionalIterator __first
,
3790 _BidirectionalIterator __last
, _Compare __comp
)
3792 // concept requirements
3793 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3794 _BidirectionalIterator
>)
3795 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3796 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3797 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3798 __glibcxx_requires_valid_range(__first
, __last
);
3800 if (__first
== __last
)
3802 _BidirectionalIterator __i
= __first
;
3811 _BidirectionalIterator __ii
= __i
;
3813 if (__comp(*__ii
, *__i
))
3815 _BidirectionalIterator __j
= __last
;
3816 while (!bool(__comp(*--__j
, *__i
)))
3818 std::iter_swap(__i
, __j
);
3819 std::reverse(__ii
, __last
);
3824 std::reverse(__first
, __last
);
3834 * @brief Copy a sequence, replacing each element of one value with another
3836 * @param first An input iterator.
3837 * @param last An input iterator.
3838 * @param result An output iterator.
3839 * @param old_value The value to be replaced.
3840 * @param new_value The replacement value.
3841 * @return The end of the output sequence, @p result+(last-first).
3843 * Copies each element in the input range @p [first,last) to the
3844 * output range @p [result,result+(last-first)) replacing elements
3845 * equal to @p old_value with @p new_value.
3847 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3849 replace_copy(_InputIterator __first
, _InputIterator __last
,
3850 _OutputIterator __result
,
3851 const _Tp
& __old_value
, const _Tp
& __new_value
)
3853 // concept requirements
3854 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3855 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3856 typename iterator_traits
<_InputIterator
>::value_type
>)
3857 __glibcxx_function_requires(_EqualOpConcept
<
3858 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3859 __glibcxx_requires_valid_range(__first
, __last
);
3861 for (; __first
!= __last
; ++__first
, ++__result
)
3862 if (*__first
== __old_value
)
3863 *__result
= __new_value
;
3865 *__result
= *__first
;
3870 * @brief Copy a sequence, replacing each value for which a predicate
3871 * returns true with another value.
3872 * @ingroup mutating_algorithms
3873 * @param first An input iterator.
3874 * @param last An input iterator.
3875 * @param result An output iterator.
3876 * @param pred A predicate.
3877 * @param new_value The replacement value.
3878 * @return The end of the output sequence, @p result+(last-first).
3880 * Copies each element in the range @p [first,last) to the range
3881 * @p [result,result+(last-first)) replacing elements for which
3882 * @p pred returns true with @p new_value.
3884 template<typename _InputIterator
, typename _OutputIterator
,
3885 typename _Predicate
, typename _Tp
>
3887 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3888 _OutputIterator __result
,
3889 _Predicate __pred
, const _Tp
& __new_value
)
3891 // concept requirements
3892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3893 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3894 typename iterator_traits
<_InputIterator
>::value_type
>)
3895 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3896 typename iterator_traits
<_InputIterator
>::value_type
>)
3897 __glibcxx_requires_valid_range(__first
, __last
);
3899 for (; __first
!= __last
; ++__first
, ++__result
)
3900 if (__pred(*__first
))
3901 *__result
= __new_value
;
3903 *__result
= *__first
;
3907 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3909 * @brief Determines whether the elements of a sequence are sorted.
3910 * @ingroup sorting_algorithms
3911 * @param first An iterator.
3912 * @param last Another iterator.
3913 * @return True if the elements are sorted, false otherwise.
3915 template<typename _ForwardIterator
>
3917 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3918 { return std::is_sorted_until(__first
, __last
) == __last
; }
3921 * @brief Determines whether the elements of a sequence are sorted
3922 * according to a comparison functor.
3923 * @ingroup sorting_algorithms
3924 * @param first An iterator.
3925 * @param last Another iterator.
3926 * @param comp A comparison functor.
3927 * @return True if the elements are sorted, false otherwise.
3929 template<typename _ForwardIterator
, typename _Compare
>
3931 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3933 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3936 * @brief Determines the end of a sorted sequence.
3937 * @ingroup sorting_algorithms
3938 * @param first An iterator.
3939 * @param last Another iterator.
3940 * @return An iterator pointing to the last iterator i in [first, last)
3941 * for which the range [first, i) is sorted.
3943 template<typename _ForwardIterator
>
3945 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3947 // concept requirements
3948 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3949 __glibcxx_function_requires(_LessThanComparableConcept
<
3950 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3951 __glibcxx_requires_valid_range(__first
, __last
);
3953 if (__first
== __last
)
3956 _ForwardIterator __next
= __first
;
3957 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3958 if (*__next
< *__first
)
3964 * @brief Determines the end of a sorted sequence using comparison functor.
3965 * @ingroup sorting_algorithms
3966 * @param first An iterator.
3967 * @param last Another iterator.
3968 * @param comp A comparison functor.
3969 * @return An iterator pointing to the last iterator i in [first, last)
3970 * for which the range [first, i) is sorted.
3972 template<typename _ForwardIterator
, typename _Compare
>
3974 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3977 // concept requirements
3978 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3979 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3980 typename iterator_traits
<_ForwardIterator
>::value_type
,
3981 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3982 __glibcxx_requires_valid_range(__first
, __last
);
3984 if (__first
== __last
)
3987 _ForwardIterator __next
= __first
;
3988 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3989 if (__comp(*__next
, *__first
))
3995 * @brief Determines min and max at once as an ordered pair.
3996 * @ingroup sorting_algorithms
3997 * @param a A thing of arbitrary type.
3998 * @param b Another thing of arbitrary type.
3999 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
4001 template<typename _Tp
>
4002 inline pair
<const _Tp
&, const _Tp
&>
4003 minmax(const _Tp
& __a
, const _Tp
& __b
)
4005 // concept requirements
4006 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
4008 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4009 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4013 * @brief Determines min and max at once as an ordered pair.
4014 * @ingroup sorting_algorithms
4015 * @param a A thing of arbitrary type.
4016 * @param b Another thing of arbitrary type.
4017 * @param comp A @link comparison_functor comparison functor@endlink.
4018 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
4020 template<typename _Tp
, typename _Compare
>
4021 inline pair
<const _Tp
&, const _Tp
&>
4022 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
4024 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4025 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4029 * @brief Return a pair of iterators pointing to the minimum and maximum
4030 * elements in a range.
4031 * @ingroup sorting_algorithms
4032 * @param first Start of range.
4033 * @param last End of range.
4034 * @return make_pair(m, M), where m is the first iterator i in
4035 * [first, last) such that no other element in the range is
4036 * smaller, and where M is the last iterator i in [first, last)
4037 * such that no other element in the range is larger.
4039 template<typename _ForwardIterator
>
4040 pair
<_ForwardIterator
, _ForwardIterator
>
4041 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4043 // concept requirements
4044 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4045 __glibcxx_function_requires(_LessThanComparableConcept
<
4046 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4047 __glibcxx_requires_valid_range(__first
, __last
);
4049 _ForwardIterator __next
= __first
;
4050 if (__first
== __last
4051 || ++__next
== __last
)
4052 return std::make_pair(__first
, __first
);
4054 _ForwardIterator __min
, __max
;
4055 if (*__next
< *__first
)
4069 while (__first
!= __last
)
4072 if (++__next
== __last
)
4074 if (*__first
< *__min
)
4076 else if (!(*__first
< *__max
))
4081 if (*__next
< *__first
)
4083 if (*__next
< *__min
)
4085 if (!(*__first
< *__max
))
4090 if (*__first
< *__min
)
4092 if (!(*__next
< *__max
))
4100 return std::make_pair(__min
, __max
);
4104 * @brief Return a pair of iterators pointing to the minimum and maximum
4105 * elements in a range.
4106 * @ingroup sorting_algorithms
4107 * @param first Start of range.
4108 * @param last End of range.
4109 * @param comp Comparison functor.
4110 * @return make_pair(m, M), where m is the first iterator i in
4111 * [first, last) such that no other element in the range is
4112 * smaller, and where M is the last iterator i in [first, last)
4113 * such that no other element in the range is larger.
4115 template<typename _ForwardIterator
, typename _Compare
>
4116 pair
<_ForwardIterator
, _ForwardIterator
>
4117 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4120 // concept requirements
4121 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4122 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4123 typename iterator_traits
<_ForwardIterator
>::value_type
,
4124 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4125 __glibcxx_requires_valid_range(__first
, __last
);
4127 _ForwardIterator __next
= __first
;
4128 if (__first
== __last
4129 || ++__next
== __last
)
4130 return std::make_pair(__first
, __first
);
4132 _ForwardIterator __min
, __max
;
4133 if (__comp(*__next
, *__first
))
4147 while (__first
!= __last
)
4150 if (++__next
== __last
)
4152 if (__comp(*__first
, *__min
))
4154 else if (!__comp(*__first
, *__max
))
4159 if (__comp(*__next
, *__first
))
4161 if (__comp(*__next
, *__min
))
4163 if (!__comp(*__first
, *__max
))
4168 if (__comp(*__first
, *__min
))
4170 if (!__comp(*__next
, *__max
))
4178 return std::make_pair(__min
, __max
);
4182 template<typename _Tp
>
4184 min(initializer_list
<_Tp
> __l
)
4185 { return *std::min_element(__l
.begin(), __l
.end()); }
4187 template<typename _Tp
, typename _Compare
>
4189 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4190 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4192 template<typename _Tp
>
4194 max(initializer_list
<_Tp
> __l
)
4195 { return *std::max_element(__l
.begin(), __l
.end()); }
4197 template<typename _Tp
, typename _Compare
>
4199 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4200 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4202 template<typename _Tp
>
4203 inline pair
<_Tp
, _Tp
>
4204 minmax(initializer_list
<_Tp
> __l
)
4206 pair
<const _Tp
*, const _Tp
*> __p
=
4207 std::minmax_element(__l
.begin(), __l
.end());
4208 return std::make_pair(*__p
.first
, *__p
.second
);
4211 template<typename _Tp
, typename _Compare
>
4212 inline pair
<_Tp
, _Tp
>
4213 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4215 pair
<const _Tp
*, const _Tp
*> __p
=
4216 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4217 return std::make_pair(*__p
.first
, *__p
.second
);
4221 * @brief Checks whether a permutaion of the second sequence is equal
4222 * to the first sequence.
4223 * @ingroup non_mutating_algorithms
4224 * @param first1 Start of first range.
4225 * @param last1 End of first range.
4226 * @param first2 Start of second range.
4227 * @return true if there exists a permutation of the elements in the range
4228 * [first2, first2 + (last1 - first1)), beginning with
4229 * ForwardIterator2 begin, such that equal(first1, last1, begin)
4230 * returns true; otherwise, returns false.
4232 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4234 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4235 _ForwardIterator2 __first2
)
4237 // Efficiently compare identical prefixes: O(N) if sequences
4238 // have the same elements in the same order.
4239 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4240 if (!(*__first1
== *__first2
))
4243 if (__first1
== __last1
)
4246 // Establish __last2 assuming equal ranges by iterating over the
4247 // rest of the list.
4248 _ForwardIterator2 __last2
= __first2
;
4249 std::advance(__last2
, std::distance(__first1
, __last1
));
4250 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4252 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4253 continue; // We've seen this one before.
4255 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4257 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4264 * @brief Checks whether a permutation of the second sequence is equal
4265 * to the first sequence.
4266 * @ingroup non_mutating_algorithms
4267 * @param first1 Start of first range.
4268 * @param last1 End of first range.
4269 * @param first2 Start of second range.
4270 * @param pred A binary predicate.
4271 * @return true if there exists a permutation of the elements in the range
4272 * [first2, first2 + (last1 - first1)), beginning with
4273 * ForwardIterator2 begin, such that equal(first1, last1, begin,
4274 * pred) returns true; otherwise, returns false.
4276 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4277 typename _BinaryPredicate
>
4279 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4280 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
4282 // Efficiently compare identical prefixes: O(N) if sequences
4283 // have the same elements in the same order.
4284 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4285 if (!bool(__pred(*__first1
, *__first2
)))
4288 if (__first1
== __last1
)
4291 // Establish __last2 assuming equal ranges by iterating over the
4292 // rest of the list.
4293 _ForwardIterator2 __last2
= __first2
;
4294 std::advance(__last2
, std::distance(__first1
, __last1
));
4295 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4297 using std::placeholders::_1
;
4299 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4300 std::bind(__pred
, _1
, *__scan
)))
4301 continue; // We've seen this one before.
4303 auto __matches
= std::count_if(__first2
, __last2
,
4304 std::bind(__pred
, _1
, *__scan
));
4306 || std::count_if(__scan
, __last1
,
4307 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4313 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4315 * @brief Shuffle the elements of a sequence using a uniform random
4317 * @ingroup mutating_algorithms
4318 * @param first A forward iterator.
4319 * @param last A forward iterator.
4320 * @param g A UniformRandomNumberGenerator (26.5.1.3).
4323 * Reorders the elements in the range @p [first,last) using @p g to
4324 * provide random numbers.
4326 template<typename _RandomAccessIterator
,
4327 typename _UniformRandomNumberGenerator
>
4329 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4330 _UniformRandomNumberGenerator
&& __g
)
4332 // concept requirements
4333 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4334 _RandomAccessIterator
>)
4335 __glibcxx_requires_valid_range(__first
, __last
);
4337 if (__first
== __last
)
4340 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4343 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
4344 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
4345 typedef typename
__distr_type::param_type __p_type
;
4348 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4349 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
4353 #endif // __GXX_EXPERIMENTAL_CXX0X__
4355 _GLIBCXX_END_NAMESPACE_VERSION
4357 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4360 * @brief Apply a function to every element of a sequence.
4361 * @ingroup non_mutating_algorithms
4362 * @param first An input iterator.
4363 * @param last An input iterator.
4364 * @param f A unary function object.
4365 * @return @p f (std::move(@p f) in C++0x).
4367 * Applies the function object @p f to each element in the range
4368 * @p [first,last). @p f must not modify the order of the sequence.
4369 * If @p f has a return value it is ignored.
4371 template<typename _InputIterator
, typename _Function
>
4373 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4375 // concept requirements
4376 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4377 __glibcxx_requires_valid_range(__first
, __last
);
4378 for (; __first
!= __last
; ++__first
)
4380 return _GLIBCXX_MOVE(__f
);
4384 * @brief Find the first occurrence of a value in a sequence.
4385 * @ingroup non_mutating_algorithms
4386 * @param first An input iterator.
4387 * @param last An input iterator.
4388 * @param val The value to find.
4389 * @return The first iterator @c i in the range @p [first,last)
4390 * such that @c *i == @p val, or @p last if no such iterator exists.
4392 template<typename _InputIterator
, typename _Tp
>
4393 inline _InputIterator
4394 find(_InputIterator __first
, _InputIterator __last
,
4397 // concept requirements
4398 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4399 __glibcxx_function_requires(_EqualOpConcept
<
4400 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4401 __glibcxx_requires_valid_range(__first
, __last
);
4402 return std::__find(__first
, __last
, __val
,
4403 std::__iterator_category(__first
));
4407 * @brief Find the first element in a sequence for which a
4408 * predicate is true.
4409 * @ingroup non_mutating_algorithms
4410 * @param first An input iterator.
4411 * @param last An input iterator.
4412 * @param pred A predicate.
4413 * @return The first iterator @c i in the range @p [first,last)
4414 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4416 template<typename _InputIterator
, typename _Predicate
>
4417 inline _InputIterator
4418 find_if(_InputIterator __first
, _InputIterator __last
,
4421 // concept requirements
4422 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4423 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4424 typename iterator_traits
<_InputIterator
>::value_type
>)
4425 __glibcxx_requires_valid_range(__first
, __last
);
4426 return std::__find_if(__first
, __last
, __pred
,
4427 std::__iterator_category(__first
));
4431 * @brief Find element from a set in a sequence.
4432 * @ingroup non_mutating_algorithms
4433 * @param first1 Start of range to search.
4434 * @param last1 End of range to search.
4435 * @param first2 Start of match candidates.
4436 * @param last2 End of match candidates.
4437 * @return The first iterator @c i in the range
4438 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4439 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4441 * Searches the range @p [first1,last1) for an element that is equal to
4442 * some element in the range [first2,last2). If found, returns an iterator
4443 * in the range [first1,last1), otherwise returns @p last1.
4445 template<typename _InputIterator
, typename _ForwardIterator
>
4447 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4448 _ForwardIterator __first2
, _ForwardIterator __last2
)
4450 // concept requirements
4451 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4452 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4453 __glibcxx_function_requires(_EqualOpConcept
<
4454 typename iterator_traits
<_InputIterator
>::value_type
,
4455 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4456 __glibcxx_requires_valid_range(__first1
, __last1
);
4457 __glibcxx_requires_valid_range(__first2
, __last2
);
4459 for (; __first1
!= __last1
; ++__first1
)
4460 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4461 if (*__first1
== *__iter
)
4467 * @brief Find element from a set in a sequence using a predicate.
4468 * @ingroup non_mutating_algorithms
4469 * @param first1 Start of range to search.
4470 * @param last1 End of range to search.
4471 * @param first2 Start of match candidates.
4472 * @param last2 End of match candidates.
4473 * @param comp Predicate to use.
4474 * @return The first iterator @c i in the range
4475 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4476 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4479 * Searches the range @p [first1,last1) for an element that is
4480 * equal to some element in the range [first2,last2). If found,
4481 * returns an iterator in the range [first1,last1), otherwise
4484 template<typename _InputIterator
, typename _ForwardIterator
,
4485 typename _BinaryPredicate
>
4487 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4488 _ForwardIterator __first2
, _ForwardIterator __last2
,
4489 _BinaryPredicate __comp
)
4491 // concept requirements
4492 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4493 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4494 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4495 typename iterator_traits
<_InputIterator
>::value_type
,
4496 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4497 __glibcxx_requires_valid_range(__first1
, __last1
);
4498 __glibcxx_requires_valid_range(__first2
, __last2
);
4500 for (; __first1
!= __last1
; ++__first1
)
4501 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4502 if (__comp(*__first1
, *__iter
))
4508 * @brief Find two adjacent values in a sequence that are equal.
4509 * @ingroup non_mutating_algorithms
4510 * @param first A forward iterator.
4511 * @param last A forward iterator.
4512 * @return The first iterator @c i such that @c i and @c i+1 are both
4513 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4514 * or @p last if no such iterator exists.
4516 template<typename _ForwardIterator
>
4518 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4520 // concept requirements
4521 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4522 __glibcxx_function_requires(_EqualityComparableConcept
<
4523 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4524 __glibcxx_requires_valid_range(__first
, __last
);
4525 if (__first
== __last
)
4527 _ForwardIterator __next
= __first
;
4528 while(++__next
!= __last
)
4530 if (*__first
== *__next
)
4538 * @brief Find two adjacent values in a sequence using a predicate.
4539 * @ingroup non_mutating_algorithms
4540 * @param first A forward iterator.
4541 * @param last A forward iterator.
4542 * @param binary_pred A binary predicate.
4543 * @return The first iterator @c i such that @c i and @c i+1 are both
4544 * valid iterators in @p [first,last) and such that
4545 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4548 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4550 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4551 _BinaryPredicate __binary_pred
)
4553 // concept requirements
4554 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4555 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4556 typename iterator_traits
<_ForwardIterator
>::value_type
,
4557 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4558 __glibcxx_requires_valid_range(__first
, __last
);
4559 if (__first
== __last
)
4561 _ForwardIterator __next
= __first
;
4562 while(++__next
!= __last
)
4564 if (__binary_pred(*__first
, *__next
))
4572 * @brief Count the number of copies of a value in a sequence.
4573 * @ingroup non_mutating_algorithms
4574 * @param first An input iterator.
4575 * @param last An input iterator.
4576 * @param value The value to be counted.
4577 * @return The number of iterators @c i in the range @p [first,last)
4578 * for which @c *i == @p value
4580 template<typename _InputIterator
, typename _Tp
>
4581 typename iterator_traits
<_InputIterator
>::difference_type
4582 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4584 // concept requirements
4585 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4586 __glibcxx_function_requires(_EqualOpConcept
<
4587 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4588 __glibcxx_requires_valid_range(__first
, __last
);
4589 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4590 for (; __first
!= __last
; ++__first
)
4591 if (*__first
== __value
)
4597 * @brief Count the elements of a sequence for which a predicate is true.
4598 * @ingroup non_mutating_algorithms
4599 * @param first An input iterator.
4600 * @param last An input iterator.
4601 * @param pred A predicate.
4602 * @return The number of iterators @c i in the range @p [first,last)
4603 * for which @p pred(*i) is true.
4605 template<typename _InputIterator
, typename _Predicate
>
4606 typename iterator_traits
<_InputIterator
>::difference_type
4607 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4609 // concept requirements
4610 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4611 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4612 typename iterator_traits
<_InputIterator
>::value_type
>)
4613 __glibcxx_requires_valid_range(__first
, __last
);
4614 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4615 for (; __first
!= __last
; ++__first
)
4616 if (__pred(*__first
))
4622 * @brief Search a sequence for a matching sub-sequence.
4623 * @ingroup non_mutating_algorithms
4624 * @param first1 A forward iterator.
4625 * @param last1 A forward iterator.
4626 * @param first2 A forward iterator.
4627 * @param last2 A forward iterator.
4628 * @return The first iterator @c i in the range
4629 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4630 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4631 * such iterator exists.
4633 * Searches the range @p [first1,last1) for a sub-sequence that compares
4634 * equal value-by-value with the sequence given by @p [first2,last2) and
4635 * returns an iterator to the first element of the sub-sequence, or
4636 * @p last1 if the sub-sequence is not found.
4638 * Because the sub-sequence must lie completely within the range
4639 * @p [first1,last1) it must start at a position less than
4640 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4642 * This means that the returned iterator @c i will be in the range
4643 * @p [first1,last1-(last2-first2))
4645 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4647 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4648 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4650 // concept requirements
4651 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4652 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4653 __glibcxx_function_requires(_EqualOpConcept
<
4654 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4655 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4656 __glibcxx_requires_valid_range(__first1
, __last1
);
4657 __glibcxx_requires_valid_range(__first2
, __last2
);
4659 // Test for empty ranges
4660 if (__first1
== __last1
|| __first2
== __last2
)
4663 // Test for a pattern of length 1.
4664 _ForwardIterator2
__p1(__first2
);
4665 if (++__p1
== __last2
)
4666 return _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4669 _ForwardIterator2 __p
;
4670 _ForwardIterator1 __current
= __first1
;
4674 __first1
= _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4675 if (__first1
== __last1
)
4679 __current
= __first1
;
4680 if (++__current
== __last1
)
4683 while (*__current
== *__p
)
4685 if (++__p
== __last2
)
4687 if (++__current
== __last1
)
4696 * @brief Search a sequence for a matching sub-sequence using a predicate.
4697 * @ingroup non_mutating_algorithms
4698 * @param first1 A forward iterator.
4699 * @param last1 A forward iterator.
4700 * @param first2 A forward iterator.
4701 * @param last2 A forward iterator.
4702 * @param predicate A binary predicate.
4703 * @return The first iterator @c i in the range
4704 * @p [first1,last1-(last2-first2)) such that
4705 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4706 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4708 * Searches the range @p [first1,last1) for a sub-sequence that compares
4709 * equal value-by-value with the sequence given by @p [first2,last2),
4710 * using @p predicate to determine equality, and returns an iterator
4711 * to the first element of the sub-sequence, or @p last1 if no such
4714 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4716 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4717 typename _BinaryPredicate
>
4719 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4720 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4721 _BinaryPredicate __predicate
)
4723 // concept requirements
4724 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4725 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4726 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4727 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4728 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4729 __glibcxx_requires_valid_range(__first1
, __last1
);
4730 __glibcxx_requires_valid_range(__first2
, __last2
);
4732 // Test for empty ranges
4733 if (__first1
== __last1
|| __first2
== __last2
)
4736 // Test for a pattern of length 1.
4737 _ForwardIterator2
__p1(__first2
);
4738 if (++__p1
== __last2
)
4740 while (__first1
!= __last1
4741 && !bool(__predicate(*__first1
, *__first2
)))
4747 _ForwardIterator2 __p
;
4748 _ForwardIterator1 __current
= __first1
;
4752 while (__first1
!= __last1
4753 && !bool(__predicate(*__first1
, *__first2
)))
4755 if (__first1
== __last1
)
4759 __current
= __first1
;
4760 if (++__current
== __last1
)
4763 while (__predicate(*__current
, *__p
))
4765 if (++__p
== __last2
)
4767 if (++__current
== __last1
)
4777 * @brief Search a sequence for a number of consecutive values.
4778 * @ingroup non_mutating_algorithms
4779 * @param first A forward iterator.
4780 * @param last A forward iterator.
4781 * @param count The number of consecutive values.
4782 * @param val The value to find.
4783 * @return The first iterator @c i in the range @p [first,last-count)
4784 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4785 * or @p last if no such iterator exists.
4787 * Searches the range @p [first,last) for @p count consecutive elements
4790 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4792 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4793 _Integer __count
, const _Tp
& __val
)
4795 // concept requirements
4796 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4797 __glibcxx_function_requires(_EqualOpConcept
<
4798 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4799 __glibcxx_requires_valid_range(__first
, __last
);
4804 return _GLIBCXX_STD_A::find(__first
, __last
, __val
);
4805 return std::__search_n(__first
, __last
, __count
, __val
,
4806 std::__iterator_category(__first
));
4811 * @brief Search a sequence for a number of consecutive values using a
4813 * @ingroup non_mutating_algorithms
4814 * @param first A forward iterator.
4815 * @param last A forward iterator.
4816 * @param count The number of consecutive values.
4817 * @param val The value to find.
4818 * @param binary_pred A binary predicate.
4819 * @return The first iterator @c i in the range @p [first,last-count)
4820 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4821 * range @p [0,count), or @p last if no such iterator exists.
4823 * Searches the range @p [first,last) for @p count consecutive elements
4824 * for which the predicate returns true.
4826 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4827 typename _BinaryPredicate
>
4829 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4830 _Integer __count
, const _Tp
& __val
,
4831 _BinaryPredicate __binary_pred
)
4833 // concept requirements
4834 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4835 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4836 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4837 __glibcxx_requires_valid_range(__first
, __last
);
4843 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4847 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4848 std::__iterator_category(__first
));
4853 * @brief Perform an operation on a sequence.
4854 * @ingroup mutating_algorithms
4855 * @param first An input iterator.
4856 * @param last An input iterator.
4857 * @param result An output iterator.
4858 * @param unary_op A unary operator.
4859 * @return An output iterator equal to @p result+(last-first).
4861 * Applies the operator to each element in the input range and assigns
4862 * the results to successive elements of the output sequence.
4863 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4864 * range @p [0,last-first).
4866 * @p unary_op must not alter its argument.
4868 template<typename _InputIterator
, typename _OutputIterator
,
4869 typename _UnaryOperation
>
4871 transform(_InputIterator __first
, _InputIterator __last
,
4872 _OutputIterator __result
, _UnaryOperation __unary_op
)
4874 // concept requirements
4875 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4876 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4877 // "the type returned by a _UnaryOperation"
4878 __typeof__(__unary_op(*__first
))>)
4879 __glibcxx_requires_valid_range(__first
, __last
);
4881 for (; __first
!= __last
; ++__first
, ++__result
)
4882 *__result
= __unary_op(*__first
);
4887 * @brief Perform an operation on corresponding elements of two sequences.
4888 * @ingroup mutating_algorithms
4889 * @param first1 An input iterator.
4890 * @param last1 An input iterator.
4891 * @param first2 An input iterator.
4892 * @param result An output iterator.
4893 * @param binary_op A binary operator.
4894 * @return An output iterator equal to @p result+(last-first).
4896 * Applies the operator to the corresponding elements in the two
4897 * input ranges and assigns the results to successive elements of the
4899 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4900 * @c N in the range @p [0,last1-first1).
4902 * @p binary_op must not alter either of its arguments.
4904 template<typename _InputIterator1
, typename _InputIterator2
,
4905 typename _OutputIterator
, typename _BinaryOperation
>
4907 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4908 _InputIterator2 __first2
, _OutputIterator __result
,
4909 _BinaryOperation __binary_op
)
4911 // concept requirements
4912 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4913 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4914 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4915 // "the type returned by a _BinaryOperation"
4916 __typeof__(__binary_op(*__first1
,*__first2
))>)
4917 __glibcxx_requires_valid_range(__first1
, __last1
);
4919 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4920 *__result
= __binary_op(*__first1
, *__first2
);
4925 * @brief Replace each occurrence of one value in a sequence with another
4927 * @ingroup mutating_algorithms
4928 * @param first A forward iterator.
4929 * @param last A forward iterator.
4930 * @param old_value The value to be replaced.
4931 * @param new_value The replacement value.
4932 * @return replace() returns no value.
4934 * For each iterator @c i in the range @p [first,last) if @c *i ==
4935 * @p old_value then the assignment @c *i = @p new_value is performed.
4937 template<typename _ForwardIterator
, typename _Tp
>
4939 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4940 const _Tp
& __old_value
, const _Tp
& __new_value
)
4942 // concept requirements
4943 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4945 __glibcxx_function_requires(_EqualOpConcept
<
4946 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4947 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4948 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4949 __glibcxx_requires_valid_range(__first
, __last
);
4951 for (; __first
!= __last
; ++__first
)
4952 if (*__first
== __old_value
)
4953 *__first
= __new_value
;
4957 * @brief Replace each value in a sequence for which a predicate returns
4958 * true with another value.
4959 * @ingroup mutating_algorithms
4960 * @param first A forward iterator.
4961 * @param last A forward iterator.
4962 * @param pred A predicate.
4963 * @param new_value The replacement value.
4964 * @return replace_if() returns no value.
4966 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4967 * is true then the assignment @c *i = @p new_value is performed.
4969 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4971 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4972 _Predicate __pred
, const _Tp
& __new_value
)
4974 // concept requirements
4975 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4977 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4979 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4980 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4981 __glibcxx_requires_valid_range(__first
, __last
);
4983 for (; __first
!= __last
; ++__first
)
4984 if (__pred(*__first
))
4985 *__first
= __new_value
;
4989 * @brief Assign the result of a function object to each value in a
4991 * @ingroup mutating_algorithms
4992 * @param first A forward iterator.
4993 * @param last A forward iterator.
4994 * @param gen A function object taking no arguments and returning
4995 * std::iterator_traits<_ForwardIterator>::value_type
4996 * @return generate() returns no value.
4998 * Performs the assignment @c *i = @p gen() for each @c i in the range
5001 template<typename _ForwardIterator
, typename _Generator
>
5003 generate(_ForwardIterator __first
, _ForwardIterator __last
,
5006 // concept requirements
5007 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5008 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
5009 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5010 __glibcxx_requires_valid_range(__first
, __last
);
5012 for (; __first
!= __last
; ++__first
)
5017 * @brief Assign the result of a function object to each value in a
5019 * @ingroup mutating_algorithms
5020 * @param first A forward iterator.
5021 * @param n The length of the sequence.
5022 * @param gen A function object taking no arguments and returning
5023 * std::iterator_traits<_ForwardIterator>::value_type
5024 * @return The end of the sequence, @p first+n
5026 * Performs the assignment @c *i = @p gen() for each @c i in the range
5027 * @p [first,first+n).
5029 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5030 * DR 865. More algorithms that throw away information
5032 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
5034 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
5036 // concept requirements
5037 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5038 // "the type returned by a _Generator"
5039 __typeof__(__gen())>)
5041 for (__decltype(__n
+ 0) __niter
= __n
;
5042 __niter
> 0; --__niter
, ++__first
)
5049 * @brief Copy a sequence, removing consecutive duplicate values.
5050 * @ingroup mutating_algorithms
5051 * @param first An input iterator.
5052 * @param last An input iterator.
5053 * @param result An output iterator.
5054 * @return An iterator designating the end of the resulting sequence.
5056 * Copies each element in the range @p [first,last) to the range
5057 * beginning at @p result, except that only the first element is copied
5058 * from groups of consecutive elements that compare equal.
5059 * unique_copy() is stable, so the relative order of elements that are
5060 * copied is unchanged.
5062 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5063 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5065 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5066 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
5069 template<typename _InputIterator
, typename _OutputIterator
>
5070 inline _OutputIterator
5071 unique_copy(_InputIterator __first
, _InputIterator __last
,
5072 _OutputIterator __result
)
5074 // concept requirements
5075 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5076 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5077 typename iterator_traits
<_InputIterator
>::value_type
>)
5078 __glibcxx_function_requires(_EqualityComparableConcept
<
5079 typename iterator_traits
<_InputIterator
>::value_type
>)
5080 __glibcxx_requires_valid_range(__first
, __last
);
5082 if (__first
== __last
)
5084 return std::__unique_copy(__first
, __last
, __result
,
5085 std::__iterator_category(__first
),
5086 std::__iterator_category(__result
));
5090 * @brief Copy a sequence, removing consecutive values using a predicate.
5091 * @ingroup mutating_algorithms
5092 * @param first An input iterator.
5093 * @param last An input iterator.
5094 * @param result An output iterator.
5095 * @param binary_pred A binary predicate.
5096 * @return An iterator designating the end of the resulting sequence.
5098 * Copies each element in the range @p [first,last) to the range
5099 * beginning at @p result, except that only the first element is copied
5100 * from groups of consecutive elements for which @p binary_pred returns
5102 * unique_copy() is stable, so the relative order of elements that are
5103 * copied is unchanged.
5105 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5106 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5108 template<typename _InputIterator
, typename _OutputIterator
,
5109 typename _BinaryPredicate
>
5110 inline _OutputIterator
5111 unique_copy(_InputIterator __first
, _InputIterator __last
,
5112 _OutputIterator __result
,
5113 _BinaryPredicate __binary_pred
)
5115 // concept requirements -- predicates checked later
5116 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5117 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5118 typename iterator_traits
<_InputIterator
>::value_type
>)
5119 __glibcxx_requires_valid_range(__first
, __last
);
5121 if (__first
== __last
)
5123 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
5124 std::__iterator_category(__first
),
5125 std::__iterator_category(__result
));
5130 * @brief Randomly shuffle the elements of a sequence.
5131 * @ingroup mutating_algorithms
5132 * @param first A forward iterator.
5133 * @param last A forward iterator.
5136 * Reorder the elements in the range @p [first,last) using a random
5137 * distribution, so that every possible ordering of the sequence is
5140 template<typename _RandomAccessIterator
>
5142 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5144 // concept requirements
5145 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5146 _RandomAccessIterator
>)
5147 __glibcxx_requires_valid_range(__first
, __last
);
5149 if (__first
!= __last
)
5150 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5151 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
5155 * @brief Shuffle the elements of a sequence using a random number
5157 * @ingroup mutating_algorithms
5158 * @param first A forward iterator.
5159 * @param last A forward iterator.
5160 * @param rand The RNG functor or function.
5163 * Reorders the elements in the range @p [first,last) using @p rand to
5164 * provide a random distribution. Calling @p rand(N) for a positive
5165 * integer @p N should return a randomly chosen integer from the
5168 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
5170 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5171 #ifdef __GXX_EXPERIMENTAL_CXX0X__
5172 _RandomNumberGenerator
&& __rand
)
5174 _RandomNumberGenerator
& __rand
)
5177 // concept requirements
5178 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5179 _RandomAccessIterator
>)
5180 __glibcxx_requires_valid_range(__first
, __last
);
5182 if (__first
== __last
)
5184 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5185 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5190 * @brief Move elements for which a predicate is true to the beginning
5192 * @ingroup mutating_algorithms
5193 * @param first A forward iterator.
5194 * @param last A forward iterator.
5195 * @param pred A predicate functor.
5196 * @return An iterator @p middle such that @p pred(i) is true for each
5197 * iterator @p i in the range @p [first,middle) and false for each @p i
5198 * in the range @p [middle,last).
5200 * @p pred must not modify its operand. @p partition() does not preserve
5201 * the relative ordering of elements in each group, use
5202 * @p stable_partition() if this is needed.
5204 template<typename _ForwardIterator
, typename _Predicate
>
5205 inline _ForwardIterator
5206 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5209 // concept requirements
5210 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5212 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5213 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5214 __glibcxx_requires_valid_range(__first
, __last
);
5216 return std::__partition(__first
, __last
, __pred
,
5217 std::__iterator_category(__first
));
5223 * @brief Sort the smallest elements of a sequence.
5224 * @ingroup sorting_algorithms
5225 * @param first An iterator.
5226 * @param middle Another iterator.
5227 * @param last Another iterator.
5230 * Sorts the smallest @p (middle-first) elements in the range
5231 * @p [first,last) and moves them to the range @p [first,middle). The
5232 * order of the remaining elements in the range @p [middle,last) is
5234 * After the sort if @p i and @j are iterators in the range
5235 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5236 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5238 template<typename _RandomAccessIterator
>
5240 partial_sort(_RandomAccessIterator __first
,
5241 _RandomAccessIterator __middle
,
5242 _RandomAccessIterator __last
)
5244 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5247 // concept requirements
5248 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5249 _RandomAccessIterator
>)
5250 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5251 __glibcxx_requires_valid_range(__first
, __middle
);
5252 __glibcxx_requires_valid_range(__middle
, __last
);
5254 std::__heap_select(__first
, __middle
, __last
);
5255 std::sort_heap(__first
, __middle
);
5259 * @brief Sort the smallest elements of a sequence using a predicate
5261 * @ingroup sorting_algorithms
5262 * @param first An iterator.
5263 * @param middle Another iterator.
5264 * @param last Another iterator.
5265 * @param comp A comparison functor.
5268 * Sorts the smallest @p (middle-first) elements in the range
5269 * @p [first,last) and moves them to the range @p [first,middle). The
5270 * order of the remaining elements in the range @p [middle,last) is
5272 * After the sort if @p i and @j are iterators in the range
5273 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5274 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5277 template<typename _RandomAccessIterator
, typename _Compare
>
5279 partial_sort(_RandomAccessIterator __first
,
5280 _RandomAccessIterator __middle
,
5281 _RandomAccessIterator __last
,
5284 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5287 // concept requirements
5288 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5289 _RandomAccessIterator
>)
5290 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5291 _ValueType
, _ValueType
>)
5292 __glibcxx_requires_valid_range(__first
, __middle
);
5293 __glibcxx_requires_valid_range(__middle
, __last
);
5295 std::__heap_select(__first
, __middle
, __last
, __comp
);
5296 std::sort_heap(__first
, __middle
, __comp
);
5300 * @brief Sort a sequence just enough to find a particular position.
5301 * @ingroup sorting_algorithms
5302 * @param first An iterator.
5303 * @param nth Another iterator.
5304 * @param last Another iterator.
5307 * Rearranges the elements in the range @p [first,last) so that @p *nth
5308 * is the same element that would have been in that position had the
5309 * whole sequence been sorted.
5310 * whole sequence been sorted. The elements either side of @p *nth are
5311 * not completely sorted, but for any iterator @i in the range
5312 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5313 * holds that @p *j<*i is false.
5315 template<typename _RandomAccessIterator
>
5317 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5318 _RandomAccessIterator __last
)
5320 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5323 // concept requirements
5324 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5325 _RandomAccessIterator
>)
5326 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5327 __glibcxx_requires_valid_range(__first
, __nth
);
5328 __glibcxx_requires_valid_range(__nth
, __last
);
5330 if (__first
== __last
|| __nth
== __last
)
5333 std::__introselect(__first
, __nth
, __last
,
5334 std::__lg(__last
- __first
) * 2);
5338 * @brief Sort a sequence just enough to find a particular position
5339 * using a predicate for comparison.
5340 * @ingroup sorting_algorithms
5341 * @param first An iterator.
5342 * @param nth Another iterator.
5343 * @param last Another iterator.
5344 * @param comp A comparison functor.
5347 * Rearranges the elements in the range @p [first,last) so that @p *nth
5348 * is the same element that would have been in that position had the
5349 * whole sequence been sorted. The elements either side of @p *nth are
5350 * not completely sorted, but for any iterator @i in the range
5351 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5352 * holds that @p comp(*j,*i) is false.
5354 template<typename _RandomAccessIterator
, typename _Compare
>
5356 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5357 _RandomAccessIterator __last
, _Compare __comp
)
5359 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5362 // concept requirements
5363 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5364 _RandomAccessIterator
>)
5365 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5366 _ValueType
, _ValueType
>)
5367 __glibcxx_requires_valid_range(__first
, __nth
);
5368 __glibcxx_requires_valid_range(__nth
, __last
);
5370 if (__first
== __last
|| __nth
== __last
)
5373 std::__introselect(__first
, __nth
, __last
,
5374 std::__lg(__last
- __first
) * 2, __comp
);
5379 * @brief Sort the elements of a sequence.
5380 * @ingroup sorting_algorithms
5381 * @param first An iterator.
5382 * @param last Another iterator.
5385 * Sorts the elements in the range @p [first,last) in ascending order,
5386 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5387 * @p [first,last-1).
5389 * The relative ordering of equivalent elements is not preserved, use
5390 * @p stable_sort() if this is needed.
5392 template<typename _RandomAccessIterator
>
5394 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5396 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5399 // concept requirements
5400 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5401 _RandomAccessIterator
>)
5402 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5403 __glibcxx_requires_valid_range(__first
, __last
);
5405 if (__first
!= __last
)
5407 std::__introsort_loop(__first
, __last
,
5408 std::__lg(__last
- __first
) * 2);
5409 std::__final_insertion_sort(__first
, __last
);
5414 * @brief Sort the elements of a sequence using a predicate for comparison.
5415 * @ingroup sorting_algorithms
5416 * @param first An iterator.
5417 * @param last Another iterator.
5418 * @param comp A comparison functor.
5421 * Sorts the elements in the range @p [first,last) in ascending order,
5422 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5423 * range @p [first,last-1).
5425 * The relative ordering of equivalent elements is not preserved, use
5426 * @p stable_sort() if this is needed.
5428 template<typename _RandomAccessIterator
, typename _Compare
>
5430 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5433 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5436 // concept requirements
5437 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5438 _RandomAccessIterator
>)
5439 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5441 __glibcxx_requires_valid_range(__first
, __last
);
5443 if (__first
!= __last
)
5445 std::__introsort_loop(__first
, __last
,
5446 std::__lg(__last
- __first
) * 2, __comp
);
5447 std::__final_insertion_sort(__first
, __last
, __comp
);
5452 * @brief Merges two sorted ranges.
5453 * @ingroup sorting_algorithms
5454 * @param first1 An iterator.
5455 * @param first2 Another iterator.
5456 * @param last1 Another iterator.
5457 * @param last2 Another iterator.
5458 * @param result An iterator pointing to the end of the merged range.
5459 * @return An iterator pointing to the first element <em>not less
5462 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5463 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5464 * must be sorted, and the output range must not overlap with either of
5465 * the input ranges. The sort is @e stable, that is, for equivalent
5466 * elements in the two ranges, elements from the first range will always
5467 * come before elements from the second.
5469 template<typename _InputIterator1
, typename _InputIterator2
,
5470 typename _OutputIterator
>
5472 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5473 _InputIterator2 __first2
, _InputIterator2 __last2
,
5474 _OutputIterator __result
)
5476 typedef typename iterator_traits
<_InputIterator1
>::value_type
5478 typedef typename iterator_traits
<_InputIterator2
>::value_type
5481 // concept requirements
5482 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5483 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5484 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5486 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5488 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5489 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5490 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5492 while (__first1
!= __last1
&& __first2
!= __last2
)
5494 if (*__first2
< *__first1
)
5496 *__result
= *__first2
;
5501 *__result
= *__first1
;
5506 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5511 * @brief Merges two sorted ranges.
5512 * @ingroup sorting_algorithms
5513 * @param first1 An iterator.
5514 * @param first2 Another iterator.
5515 * @param last1 Another iterator.
5516 * @param last2 Another iterator.
5517 * @param result An iterator pointing to the end of the merged range.
5518 * @param comp A functor to use for comparisons.
5519 * @return An iterator pointing to the first element "not less
5522 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5523 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5524 * must be sorted, and the output range must not overlap with either of
5525 * the input ranges. The sort is @e stable, that is, for equivalent
5526 * elements in the two ranges, elements from the first range will always
5527 * come before elements from the second.
5529 * The comparison function should have the same effects on ordering as
5530 * the function used for the initial sort.
5532 template<typename _InputIterator1
, typename _InputIterator2
,
5533 typename _OutputIterator
, typename _Compare
>
5535 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5536 _InputIterator2 __first2
, _InputIterator2 __last2
,
5537 _OutputIterator __result
, _Compare __comp
)
5539 typedef typename iterator_traits
<_InputIterator1
>::value_type
5541 typedef typename iterator_traits
<_InputIterator2
>::value_type
5544 // concept requirements
5545 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5546 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5547 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5549 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5551 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5552 _ValueType2
, _ValueType1
>)
5553 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5554 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5556 while (__first1
!= __last1
&& __first2
!= __last2
)
5558 if (__comp(*__first2
, *__first1
))
5560 *__result
= *__first2
;
5565 *__result
= *__first1
;
5570 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5576 * @brief Sort the elements of a sequence, preserving the relative order
5577 * of equivalent elements.
5578 * @ingroup sorting_algorithms
5579 * @param first An iterator.
5580 * @param last Another iterator.
5583 * Sorts the elements in the range @p [first,last) in ascending order,
5584 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5585 * @p [first,last-1).
5587 * The relative ordering of equivalent elements is preserved, so any two
5588 * elements @p x and @p y in the range @p [first,last) such that
5589 * @p x<y is false and @p y<x is false will have the same relative
5590 * ordering after calling @p stable_sort().
5592 template<typename _RandomAccessIterator
>
5594 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5596 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5598 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5601 // concept requirements
5602 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5603 _RandomAccessIterator
>)
5604 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5605 __glibcxx_requires_valid_range(__first
, __last
);
5607 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5609 if (__buf
.begin() == 0)
5610 std::__inplace_stable_sort(__first
, __last
);
5612 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5613 _DistanceType(__buf
.size()));
5617 * @brief Sort the elements of a sequence using a predicate for comparison,
5618 * preserving the relative order of equivalent elements.
5619 * @ingroup sorting_algorithms
5620 * @param first An iterator.
5621 * @param last Another iterator.
5622 * @param comp A comparison functor.
5625 * Sorts the elements in the range @p [first,last) in ascending order,
5626 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5627 * range @p [first,last-1).
5629 * The relative ordering of equivalent elements is preserved, so any two
5630 * elements @p x and @p y in the range @p [first,last) such that
5631 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5632 * relative ordering after calling @p stable_sort().
5634 template<typename _RandomAccessIterator
, typename _Compare
>
5636 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5639 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5641 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5644 // concept requirements
5645 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5646 _RandomAccessIterator
>)
5647 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5650 __glibcxx_requires_valid_range(__first
, __last
);
5652 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5654 if (__buf
.begin() == 0)
5655 std::__inplace_stable_sort(__first
, __last
, __comp
);
5657 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5658 _DistanceType(__buf
.size()), __comp
);
5663 * @brief Return the union of two sorted ranges.
5664 * @ingroup set_algorithms
5665 * @param first1 Start of first range.
5666 * @param last1 End of first range.
5667 * @param first2 Start of second range.
5668 * @param last2 End of second range.
5669 * @return End of the output range.
5670 * @ingroup set_algorithms
5672 * This operation iterates over both ranges, copying elements present in
5673 * each range in order to the output range. Iterators increment for each
5674 * range. When the current element of one range is less than the other,
5675 * that element is copied and the iterator advanced. If an element is
5676 * contained in both ranges, the element from the first range is copied and
5677 * both ranges advance. The output range may not overlap either input
5680 template<typename _InputIterator1
, typename _InputIterator2
,
5681 typename _OutputIterator
>
5683 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5684 _InputIterator2 __first2
, _InputIterator2 __last2
,
5685 _OutputIterator __result
)
5687 typedef typename iterator_traits
<_InputIterator1
>::value_type
5689 typedef typename iterator_traits
<_InputIterator2
>::value_type
5692 // concept requirements
5693 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5694 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5695 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5697 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5699 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5700 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5701 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5702 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5704 while (__first1
!= __last1
&& __first2
!= __last2
)
5706 if (*__first1
< *__first2
)
5708 *__result
= *__first1
;
5711 else if (*__first2
< *__first1
)
5713 *__result
= *__first2
;
5718 *__result
= *__first1
;
5724 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5729 * @brief Return the union of two sorted ranges using a comparison functor.
5730 * @ingroup set_algorithms
5731 * @param first1 Start of first range.
5732 * @param last1 End of first range.
5733 * @param first2 Start of second range.
5734 * @param last2 End of second range.
5735 * @param comp The comparison functor.
5736 * @return End of the output range.
5737 * @ingroup set_algorithms
5739 * This operation iterates over both ranges, copying elements present in
5740 * each range in order to the output range. Iterators increment for each
5741 * range. When the current element of one range is less than the other
5742 * according to @a comp, that element is copied and the iterator advanced.
5743 * If an equivalent element according to @a comp is contained in both
5744 * ranges, the element from the first range is copied and both ranges
5745 * advance. The output range may not overlap either input range.
5747 template<typename _InputIterator1
, typename _InputIterator2
,
5748 typename _OutputIterator
, typename _Compare
>
5750 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5751 _InputIterator2 __first2
, _InputIterator2 __last2
,
5752 _OutputIterator __result
, _Compare __comp
)
5754 typedef typename iterator_traits
<_InputIterator1
>::value_type
5756 typedef typename iterator_traits
<_InputIterator2
>::value_type
5759 // concept requirements
5760 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5761 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5762 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5764 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5766 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5767 _ValueType1
, _ValueType2
>)
5768 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5769 _ValueType2
, _ValueType1
>)
5770 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5771 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5773 while (__first1
!= __last1
&& __first2
!= __last2
)
5775 if (__comp(*__first1
, *__first2
))
5777 *__result
= *__first1
;
5780 else if (__comp(*__first2
, *__first1
))
5782 *__result
= *__first2
;
5787 *__result
= *__first1
;
5793 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5798 * @brief Return the intersection of two sorted ranges.
5799 * @ingroup set_algorithms
5800 * @param first1 Start of first range.
5801 * @param last1 End of first range.
5802 * @param first2 Start of second range.
5803 * @param last2 End of second range.
5804 * @return End of the output range.
5805 * @ingroup set_algorithms
5807 * This operation iterates over both ranges, copying elements present in
5808 * both ranges in order to the output range. Iterators increment for each
5809 * range. When the current element of one range is less than the other,
5810 * that iterator advances. If an element is contained in both ranges, the
5811 * element from the first range is copied and both ranges advance. The
5812 * output range may not overlap either input range.
5814 template<typename _InputIterator1
, typename _InputIterator2
,
5815 typename _OutputIterator
>
5817 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5818 _InputIterator2 __first2
, _InputIterator2 __last2
,
5819 _OutputIterator __result
)
5821 typedef typename iterator_traits
<_InputIterator1
>::value_type
5823 typedef typename iterator_traits
<_InputIterator2
>::value_type
5826 // concept requirements
5827 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5828 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5829 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5831 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5832 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5833 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5834 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5836 while (__first1
!= __last1
&& __first2
!= __last2
)
5837 if (*__first1
< *__first2
)
5839 else if (*__first2
< *__first1
)
5843 *__result
= *__first1
;
5852 * @brief Return the intersection of two sorted ranges using comparison
5854 * @ingroup set_algorithms
5855 * @param first1 Start of first range.
5856 * @param last1 End of first range.
5857 * @param first2 Start of second range.
5858 * @param last2 End of second range.
5859 * @param comp The comparison functor.
5860 * @return End of the output range.
5861 * @ingroup set_algorithms
5863 * This operation iterates over both ranges, copying elements present in
5864 * both ranges in order to the output range. Iterators increment for each
5865 * range. When the current element of one range is less than the other
5866 * according to @a comp, that iterator advances. If an element is
5867 * contained in both ranges according to @a comp, the element from the
5868 * first range is copied and both ranges advance. The output range may not
5869 * overlap either input range.
5871 template<typename _InputIterator1
, typename _InputIterator2
,
5872 typename _OutputIterator
, typename _Compare
>
5874 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5875 _InputIterator2 __first2
, _InputIterator2 __last2
,
5876 _OutputIterator __result
, _Compare __comp
)
5878 typedef typename iterator_traits
<_InputIterator1
>::value_type
5880 typedef typename iterator_traits
<_InputIterator2
>::value_type
5883 // concept requirements
5884 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5885 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5886 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5888 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5889 _ValueType1
, _ValueType2
>)
5890 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5891 _ValueType2
, _ValueType1
>)
5892 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5893 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5895 while (__first1
!= __last1
&& __first2
!= __last2
)
5896 if (__comp(*__first1
, *__first2
))
5898 else if (__comp(*__first2
, *__first1
))
5902 *__result
= *__first1
;
5911 * @brief Return the difference of two sorted ranges.
5912 * @ingroup set_algorithms
5913 * @param first1 Start of first range.
5914 * @param last1 End of first range.
5915 * @param first2 Start of second range.
5916 * @param last2 End of second range.
5917 * @return End of the output range.
5918 * @ingroup set_algorithms
5920 * This operation iterates over both ranges, copying elements present in
5921 * the first range but not the second in order to the output range.
5922 * Iterators increment for each range. When the current element of the
5923 * first range is less than the second, that element is copied and the
5924 * iterator advances. If the current element of the second range is less,
5925 * the iterator advances, but no element is copied. If an element is
5926 * contained in both ranges, no elements are copied and both ranges
5927 * advance. The output range may not overlap either input range.
5929 template<typename _InputIterator1
, typename _InputIterator2
,
5930 typename _OutputIterator
>
5932 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5933 _InputIterator2 __first2
, _InputIterator2 __last2
,
5934 _OutputIterator __result
)
5936 typedef typename iterator_traits
<_InputIterator1
>::value_type
5938 typedef typename iterator_traits
<_InputIterator2
>::value_type
5941 // concept requirements
5942 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5943 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5944 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5946 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5947 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5948 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5949 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5951 while (__first1
!= __last1
&& __first2
!= __last2
)
5952 if (*__first1
< *__first2
)
5954 *__result
= *__first1
;
5958 else if (*__first2
< *__first1
)
5965 return std::copy(__first1
, __last1
, __result
);
5969 * @brief Return the difference of two sorted ranges using comparison
5971 * @ingroup set_algorithms
5972 * @param first1 Start of first range.
5973 * @param last1 End of first range.
5974 * @param first2 Start of second range.
5975 * @param last2 End of second range.
5976 * @param comp The comparison functor.
5977 * @return End of the output range.
5978 * @ingroup set_algorithms
5980 * This operation iterates over both ranges, copying elements present in
5981 * the first range but not the second in order to the output range.
5982 * Iterators increment for each range. When the current element of the
5983 * first range is less than the second according to @a comp, that element
5984 * is copied and the iterator advances. If the current element of the
5985 * second range is less, no element is copied and the iterator advances.
5986 * If an element is contained in both ranges according to @a comp, no
5987 * elements are copied and both ranges advance. The output range may not
5988 * overlap either input range.
5990 template<typename _InputIterator1
, typename _InputIterator2
,
5991 typename _OutputIterator
, typename _Compare
>
5993 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5994 _InputIterator2 __first2
, _InputIterator2 __last2
,
5995 _OutputIterator __result
, _Compare __comp
)
5997 typedef typename iterator_traits
<_InputIterator1
>::value_type
5999 typedef typename iterator_traits
<_InputIterator2
>::value_type
6002 // concept requirements
6003 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6004 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6005 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6007 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6008 _ValueType1
, _ValueType2
>)
6009 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6010 _ValueType2
, _ValueType1
>)
6011 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6012 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6014 while (__first1
!= __last1
&& __first2
!= __last2
)
6015 if (__comp(*__first1
, *__first2
))
6017 *__result
= *__first1
;
6021 else if (__comp(*__first2
, *__first1
))
6028 return std::copy(__first1
, __last1
, __result
);
6032 * @brief Return the symmetric difference of two sorted ranges.
6033 * @ingroup set_algorithms
6034 * @param first1 Start of first range.
6035 * @param last1 End of first range.
6036 * @param first2 Start of second range.
6037 * @param last2 End of second range.
6038 * @return End of the output range.
6039 * @ingroup set_algorithms
6041 * This operation iterates over both ranges, copying elements present in
6042 * one range but not the other in order to the output range. Iterators
6043 * increment for each range. When the current element of one range is less
6044 * than the other, that element is copied and the iterator advances. If an
6045 * element is contained in both ranges, no elements are copied and both
6046 * ranges advance. The output range may not overlap either input range.
6048 template<typename _InputIterator1
, typename _InputIterator2
,
6049 typename _OutputIterator
>
6051 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6052 _InputIterator2 __first2
, _InputIterator2 __last2
,
6053 _OutputIterator __result
)
6055 typedef typename iterator_traits
<_InputIterator1
>::value_type
6057 typedef typename iterator_traits
<_InputIterator2
>::value_type
6060 // concept requirements
6061 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6062 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6063 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6065 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6067 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6068 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6069 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6070 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6072 while (__first1
!= __last1
&& __first2
!= __last2
)
6073 if (*__first1
< *__first2
)
6075 *__result
= *__first1
;
6079 else if (*__first2
< *__first1
)
6081 *__result
= *__first2
;
6090 return std::copy(__first2
, __last2
, std::copy(__first1
,
6091 __last1
, __result
));
6095 * @brief Return the symmetric difference of two sorted ranges using
6096 * comparison functor.
6097 * @ingroup set_algorithms
6098 * @param first1 Start of first range.
6099 * @param last1 End of first range.
6100 * @param first2 Start of second range.
6101 * @param last2 End of second range.
6102 * @param comp The comparison functor.
6103 * @return End of the output range.
6104 * @ingroup set_algorithms
6106 * This operation iterates over both ranges, copying elements present in
6107 * one range but not the other in order to the output range. Iterators
6108 * increment for each range. When the current element of one range is less
6109 * than the other according to @a comp, that element is copied and the
6110 * iterator advances. If an element is contained in both ranges according
6111 * to @a comp, no elements are copied and both ranges advance. The output
6112 * range may not overlap either input range.
6114 template<typename _InputIterator1
, typename _InputIterator2
,
6115 typename _OutputIterator
, typename _Compare
>
6117 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6118 _InputIterator2 __first2
, _InputIterator2 __last2
,
6119 _OutputIterator __result
,
6122 typedef typename iterator_traits
<_InputIterator1
>::value_type
6124 typedef typename iterator_traits
<_InputIterator2
>::value_type
6127 // concept requirements
6128 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6129 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6130 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6132 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6134 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6135 _ValueType1
, _ValueType2
>)
6136 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6137 _ValueType2
, _ValueType1
>)
6138 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6139 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6141 while (__first1
!= __last1
&& __first2
!= __last2
)
6142 if (__comp(*__first1
, *__first2
))
6144 *__result
= *__first1
;
6148 else if (__comp(*__first2
, *__first1
))
6150 *__result
= *__first2
;
6159 return std::copy(__first2
, __last2
,
6160 std::copy(__first1
, __last1
, __result
));
6165 * @brief Return the minimum element in a range.
6166 * @ingroup sorting_algorithms
6167 * @param first Start of range.
6168 * @param last End of range.
6169 * @return Iterator referencing the first instance of the smallest value.
6171 template<typename _ForwardIterator
>
6173 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
6175 // concept requirements
6176 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6177 __glibcxx_function_requires(_LessThanComparableConcept
<
6178 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6179 __glibcxx_requires_valid_range(__first
, __last
);
6181 if (__first
== __last
)
6183 _ForwardIterator __result
= __first
;
6184 while (++__first
!= __last
)
6185 if (*__first
< *__result
)
6191 * @brief Return the minimum element in a range using comparison functor.
6192 * @ingroup sorting_algorithms
6193 * @param first Start of range.
6194 * @param last End of range.
6195 * @param comp Comparison functor.
6196 * @return Iterator referencing the first instance of the smallest value
6197 * according to comp.
6199 template<typename _ForwardIterator
, typename _Compare
>
6201 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6204 // concept requirements
6205 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6206 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6207 typename iterator_traits
<_ForwardIterator
>::value_type
,
6208 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6209 __glibcxx_requires_valid_range(__first
, __last
);
6211 if (__first
== __last
)
6213 _ForwardIterator __result
= __first
;
6214 while (++__first
!= __last
)
6215 if (__comp(*__first
, *__result
))
6221 * @brief Return the maximum element in a range.
6222 * @ingroup sorting_algorithms
6223 * @param first Start of range.
6224 * @param last End of range.
6225 * @return Iterator referencing the first instance of the largest value.
6227 template<typename _ForwardIterator
>
6229 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6231 // concept requirements
6232 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6233 __glibcxx_function_requires(_LessThanComparableConcept
<
6234 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6235 __glibcxx_requires_valid_range(__first
, __last
);
6237 if (__first
== __last
)
6239 _ForwardIterator __result
= __first
;
6240 while (++__first
!= __last
)
6241 if (*__result
< *__first
)
6247 * @brief Return the maximum element in a range using comparison functor.
6248 * @ingroup sorting_algorithms
6249 * @param first Start of range.
6250 * @param last End of range.
6251 * @param comp Comparison functor.
6252 * @return Iterator referencing the first instance of the largest value
6253 * according to comp.
6255 template<typename _ForwardIterator
, typename _Compare
>
6257 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6260 // concept requirements
6261 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6262 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6263 typename iterator_traits
<_ForwardIterator
>::value_type
,
6264 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6265 __glibcxx_requires_valid_range(__first
, __last
);
6267 if (__first
== __last
) return __first
;
6268 _ForwardIterator __result
= __first
;
6269 while (++__first
!= __last
)
6270 if (__comp(*__result
, *__first
))
6275 _GLIBCXX_END_NAMESPACE_ALGO
6278 #endif /* _STL_ALGO_H */