1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
60 #include <cstdlib> // for rand
61 #include <bits/algorithmfwd.h>
62 #include <bits/stl_heap.h>
63 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
65 #ifdef __GXX_EXPERIMENTAL_CXX0X__
66 #include <random> // for std::uniform_int_distribution
69 // See concept_check.h for the __glibcxx_*_requires macros.
71 _GLIBCXX_BEGIN_NAMESPACE(std
)
73 /// Swaps the median value of *__a, *__b and *__c to *__a
74 template<typename _Iterator
>
76 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
)
78 // concept requirements
79 __glibcxx_function_requires(_LessThanComparableConcept
<
80 typename iterator_traits
<_Iterator
>::value_type
>)
85 std::iter_swap(__a
, __b
);
87 std::iter_swap(__a
, __c
);
92 std::iter_swap(__a
, __c
);
94 std::iter_swap(__a
, __b
);
97 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
98 template<typename _Iterator
, typename _Compare
>
100 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
,
103 // concept requirements
104 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
105 typename iterator_traits
<_Iterator
>::value_type
,
106 typename iterator_traits
<_Iterator
>::value_type
>)
108 if (__comp(*__a
, *__b
))
110 if (__comp(*__b
, *__c
))
111 std::iter_swap(__a
, __b
);
112 else if (__comp(*__a
, *__c
))
113 std::iter_swap(__a
, __c
);
115 else if (__comp(*__a
, *__c
))
117 else if (__comp(*__b
, *__c
))
118 std::iter_swap(__a
, __c
);
120 std::iter_swap(__a
, __b
);
125 /// This is an overload used by find() for the Input Iterator case.
126 template<typename _InputIterator
, typename _Tp
>
127 inline _InputIterator
128 __find(_InputIterator __first
, _InputIterator __last
,
129 const _Tp
& __val
, input_iterator_tag
)
131 while (__first
!= __last
&& !(*__first
== __val
))
136 /// This is an overload used by find_if() for the Input Iterator case.
137 template<typename _InputIterator
, typename _Predicate
>
138 inline _InputIterator
139 __find_if(_InputIterator __first
, _InputIterator __last
,
140 _Predicate __pred
, input_iterator_tag
)
142 while (__first
!= __last
&& !bool(__pred(*__first
)))
147 /// This is an overload used by find() for the RAI case.
148 template<typename _RandomAccessIterator
, typename _Tp
>
149 _RandomAccessIterator
150 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
151 const _Tp
& __val
, random_access_iterator_tag
)
153 typename iterator_traits
<_RandomAccessIterator
>::difference_type
154 __trip_count
= (__last
- __first
) >> 2;
156 for (; __trip_count
> 0; --__trip_count
)
158 if (*__first
== __val
)
162 if (*__first
== __val
)
166 if (*__first
== __val
)
170 if (*__first
== __val
)
175 switch (__last
- __first
)
178 if (*__first
== __val
)
182 if (*__first
== __val
)
186 if (*__first
== __val
)
195 /// This is an overload used by find_if() for the RAI case.
196 template<typename _RandomAccessIterator
, typename _Predicate
>
197 _RandomAccessIterator
198 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
199 _Predicate __pred
, random_access_iterator_tag
)
201 typename iterator_traits
<_RandomAccessIterator
>::difference_type
202 __trip_count
= (__last
- __first
) >> 2;
204 for (; __trip_count
> 0; --__trip_count
)
206 if (__pred(*__first
))
210 if (__pred(*__first
))
214 if (__pred(*__first
))
218 if (__pred(*__first
))
223 switch (__last
- __first
)
226 if (__pred(*__first
))
230 if (__pred(*__first
))
234 if (__pred(*__first
))
243 #ifdef __GXX_EXPERIMENTAL_CXX0X__
244 /// This is an overload used by find_if_not() for the Input Iterator case.
245 template<typename _InputIterator
, typename _Predicate
>
246 inline _InputIterator
247 __find_if_not(_InputIterator __first
, _InputIterator __last
,
248 _Predicate __pred
, input_iterator_tag
)
250 while (__first
!= __last
&& bool(__pred(*__first
)))
255 /// This is an overload used by find_if_not() for the RAI case.
256 template<typename _RandomAccessIterator
, typename _Predicate
>
257 _RandomAccessIterator
258 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
259 _Predicate __pred
, random_access_iterator_tag
)
261 typename iterator_traits
<_RandomAccessIterator
>::difference_type
262 __trip_count
= (__last
- __first
) >> 2;
264 for (; __trip_count
> 0; --__trip_count
)
266 if (!bool(__pred(*__first
)))
270 if (!bool(__pred(*__first
)))
274 if (!bool(__pred(*__first
)))
278 if (!bool(__pred(*__first
)))
283 switch (__last
- __first
)
286 if (!bool(__pred(*__first
)))
290 if (!bool(__pred(*__first
)))
294 if (!bool(__pred(*__first
)))
306 // set_symmetric_difference
318 * This is an uglified
319 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
320 * overloaded for forward iterators.
322 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
324 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
325 _Integer __count
, const _Tp
& __val
,
326 std::forward_iterator_tag
)
328 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
329 while (__first
!= __last
)
331 typename iterator_traits
<_ForwardIterator
>::difference_type
333 _ForwardIterator __i
= __first
;
335 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
344 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
350 * This is an uglified
351 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
352 * overloaded for random access iterators.
354 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
356 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
357 _Integer __count
, const _Tp
& __val
,
358 std::random_access_iterator_tag
)
361 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
364 _DistanceType __tailSize
= __last
- __first
;
365 const _DistanceType __pattSize
= __count
;
367 if (__tailSize
< __pattSize
)
370 const _DistanceType __skipOffset
= __pattSize
- 1;
371 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
372 __tailSize
-= __pattSize
;
374 while (1) // the main loop...
376 // __lookAhead here is always pointing to the last element of next
378 while (!(*__lookAhead
== __val
)) // the skip loop...
380 if (__tailSize
< __pattSize
)
381 return __last
; // Failure
382 __lookAhead
+= __pattSize
;
383 __tailSize
-= __pattSize
;
385 _DistanceType __remainder
= __skipOffset
;
386 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
387 *__backTrack
== __val
; --__backTrack
)
389 if (--__remainder
== 0)
390 return (__lookAhead
- __skipOffset
); // Success
392 if (__remainder
> __tailSize
)
393 return __last
; // Failure
394 __lookAhead
+= __remainder
;
395 __tailSize
-= __remainder
;
402 * This is an uglified
403 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
405 * overloaded for forward iterators.
407 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
408 typename _BinaryPredicate
>
410 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
411 _Integer __count
, const _Tp
& __val
,
412 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
414 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
417 while (__first
!= __last
)
419 typename iterator_traits
<_ForwardIterator
>::difference_type
421 _ForwardIterator __i
= __first
;
423 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
433 while (__first
!= __last
434 && !bool(__binary_pred(*__first
, __val
)))
441 * This is an uglified
442 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
444 * overloaded for random access iterators.
446 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
447 typename _BinaryPredicate
>
449 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
450 _Integer __count
, const _Tp
& __val
,
451 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
454 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
457 _DistanceType __tailSize
= __last
- __first
;
458 const _DistanceType __pattSize
= __count
;
460 if (__tailSize
< __pattSize
)
463 const _DistanceType __skipOffset
= __pattSize
- 1;
464 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
465 __tailSize
-= __pattSize
;
467 while (1) // the main loop...
469 // __lookAhead here is always pointing to the last element of next
471 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
473 if (__tailSize
< __pattSize
)
474 return __last
; // Failure
475 __lookAhead
+= __pattSize
;
476 __tailSize
-= __pattSize
;
478 _DistanceType __remainder
= __skipOffset
;
479 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
480 __binary_pred(*__backTrack
, __val
); --__backTrack
)
482 if (--__remainder
== 0)
483 return (__lookAhead
- __skipOffset
); // Success
485 if (__remainder
> __tailSize
)
486 return __last
; // Failure
487 __lookAhead
+= __remainder
;
488 __tailSize
-= __remainder
;
492 // find_end for forward iterators.
493 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
495 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
496 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
497 forward_iterator_tag
, forward_iterator_tag
)
499 if (__first2
== __last2
)
503 _ForwardIterator1 __result
= __last1
;
506 _ForwardIterator1 __new_result
507 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
508 if (__new_result
== __last1
)
512 __result
= __new_result
;
513 __first1
= __new_result
;
520 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
521 typename _BinaryPredicate
>
523 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
524 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
525 forward_iterator_tag
, forward_iterator_tag
,
526 _BinaryPredicate __comp
)
528 if (__first2
== __last2
)
532 _ForwardIterator1 __result
= __last1
;
535 _ForwardIterator1 __new_result
536 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
538 if (__new_result
== __last1
)
542 __result
= __new_result
;
543 __first1
= __new_result
;
550 // find_end for bidirectional iterators (much faster).
551 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
552 _BidirectionalIterator1
553 __find_end(_BidirectionalIterator1 __first1
,
554 _BidirectionalIterator1 __last1
,
555 _BidirectionalIterator2 __first2
,
556 _BidirectionalIterator2 __last2
,
557 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
559 // concept requirements
560 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
561 _BidirectionalIterator1
>)
562 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
563 _BidirectionalIterator2
>)
565 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
566 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
568 _RevIterator1
__rlast1(__first1
);
569 _RevIterator2
__rlast2(__first2
);
570 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
572 _RevIterator2(__last2
),
575 if (__rresult
== __rlast1
)
579 _BidirectionalIterator1 __result
= __rresult
.base();
580 std::advance(__result
, -std::distance(__first2
, __last2
));
585 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
586 typename _BinaryPredicate
>
587 _BidirectionalIterator1
588 __find_end(_BidirectionalIterator1 __first1
,
589 _BidirectionalIterator1 __last1
,
590 _BidirectionalIterator2 __first2
,
591 _BidirectionalIterator2 __last2
,
592 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
593 _BinaryPredicate __comp
)
595 // concept requirements
596 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
597 _BidirectionalIterator1
>)
598 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
599 _BidirectionalIterator2
>)
601 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
602 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
604 _RevIterator1
__rlast1(__first1
);
605 _RevIterator2
__rlast2(__first2
);
606 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
607 _RevIterator2(__last2
), __rlast2
,
610 if (__rresult
== __rlast1
)
614 _BidirectionalIterator1 __result
= __rresult
.base();
615 std::advance(__result
, -std::distance(__first2
, __last2
));
621 * @brief Find last matching subsequence in a sequence.
622 * @ingroup non_mutating_algorithms
623 * @param first1 Start of range to search.
624 * @param last1 End of range to search.
625 * @param first2 Start of sequence to match.
626 * @param last2 End of sequence to match.
627 * @return The last iterator @c i in the range
628 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
629 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
630 * such iterator exists.
632 * Searches the range @p [first1,last1) for a sub-sequence that compares
633 * equal value-by-value with the sequence given by @p [first2,last2) and
634 * returns an iterator to the first element of the sub-sequence, or
635 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
636 * last such subsequence contained in [first,last1).
638 * Because the sub-sequence must lie completely within the range
639 * @p [first1,last1) it must start at a position less than
640 * @p last1-(last2-first2) where @p last2-first2 is the length of the
642 * This means that the returned iterator @c i will be in the range
643 * @p [first1,last1-(last2-first2))
645 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
646 inline _ForwardIterator1
647 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
648 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
650 // concept requirements
651 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
652 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
653 __glibcxx_function_requires(_EqualOpConcept
<
654 typename iterator_traits
<_ForwardIterator1
>::value_type
,
655 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
656 __glibcxx_requires_valid_range(__first1
, __last1
);
657 __glibcxx_requires_valid_range(__first2
, __last2
);
659 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
660 std::__iterator_category(__first1
),
661 std::__iterator_category(__first2
));
665 * @brief Find last matching subsequence in a sequence using a predicate.
666 * @ingroup non_mutating_algorithms
667 * @param first1 Start of range to search.
668 * @param last1 End of range to search.
669 * @param first2 Start of sequence to match.
670 * @param last2 End of sequence to match.
671 * @param comp The predicate to use.
672 * @return The last iterator @c i in the range
673 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
674 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
675 * @p last1 if no such iterator exists.
677 * Searches the range @p [first1,last1) for a sub-sequence that compares
678 * equal value-by-value with the sequence given by @p [first2,last2) using
679 * comp as a predicate and returns an iterator to the first element of the
680 * sub-sequence, or @p last1 if the sub-sequence is not found. The
681 * sub-sequence will be the last such subsequence contained in
684 * Because the sub-sequence must lie completely within the range
685 * @p [first1,last1) it must start at a position less than
686 * @p last1-(last2-first2) where @p last2-first2 is the length of the
688 * This means that the returned iterator @c i will be in the range
689 * @p [first1,last1-(last2-first2))
691 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
692 typename _BinaryPredicate
>
693 inline _ForwardIterator1
694 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
695 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
696 _BinaryPredicate __comp
)
698 // concept requirements
699 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
700 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
701 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
702 typename iterator_traits
<_ForwardIterator1
>::value_type
,
703 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
704 __glibcxx_requires_valid_range(__first1
, __last1
);
705 __glibcxx_requires_valid_range(__first2
, __last2
);
707 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
708 std::__iterator_category(__first1
),
709 std::__iterator_category(__first2
),
713 #ifdef __GXX_EXPERIMENTAL_CXX0X__
715 * @brief Checks that a predicate is true for all the elements
717 * @ingroup non_mutating_algorithms
718 * @param first An input iterator.
719 * @param last An input iterator.
720 * @param pred A predicate.
721 * @return True if the check is true, false otherwise.
723 * Returns true if @p pred is true for each element in the range
724 * @p [first,last), and false otherwise.
726 template<typename _InputIterator
, typename _Predicate
>
728 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
729 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
732 * @brief Checks that a predicate is false for all the elements
734 * @ingroup non_mutating_algorithms
735 * @param first An input iterator.
736 * @param last An input iterator.
737 * @param pred A predicate.
738 * @return True if the check is true, false otherwise.
740 * Returns true if @p pred is false for each element in the range
741 * @p [first,last), and false otherwise.
743 template<typename _InputIterator
, typename _Predicate
>
745 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
746 { return __last
== _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
); }
749 * @brief Checks that a predicate is false for at least an element
751 * @ingroup non_mutating_algorithms
752 * @param first An input iterator.
753 * @param last An input iterator.
754 * @param pred A predicate.
755 * @return True if the check is true, false otherwise.
757 * Returns true if an element exists in the range @p [first,last) such that
758 * @p pred is true, and false otherwise.
760 template<typename _InputIterator
, typename _Predicate
>
762 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
763 { return !std::none_of(__first
, __last
, __pred
); }
766 * @brief Find the first element in a sequence for which a
767 * predicate is false.
768 * @ingroup non_mutating_algorithms
769 * @param first An input iterator.
770 * @param last An input iterator.
771 * @param pred A predicate.
772 * @return The first iterator @c i in the range @p [first,last)
773 * such that @p pred(*i) is false, or @p last if no such iterator exists.
775 template<typename _InputIterator
, typename _Predicate
>
776 inline _InputIterator
777 find_if_not(_InputIterator __first
, _InputIterator __last
,
780 // concept requirements
781 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
782 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
783 typename iterator_traits
<_InputIterator
>::value_type
>)
784 __glibcxx_requires_valid_range(__first
, __last
);
785 return std::__find_if_not(__first
, __last
, __pred
,
786 std::__iterator_category(__first
));
790 * @brief Checks whether the sequence is partitioned.
791 * @ingroup mutating_algorithms
792 * @param first An input iterator.
793 * @param last An input iterator.
794 * @param pred A predicate.
795 * @return True if the range @p [first,last) is partioned by @p pred,
796 * i.e. if all elements that satisfy @p pred appear before those that
799 template<typename _InputIterator
, typename _Predicate
>
801 is_partitioned(_InputIterator __first
, _InputIterator __last
,
804 __first
= std::find_if_not(__first
, __last
, __pred
);
805 return std::none_of(__first
, __last
, __pred
);
809 * @brief Find the partition point of a partitioned range.
810 * @ingroup mutating_algorithms
811 * @param first An iterator.
812 * @param last Another iterator.
813 * @param pred A predicate.
814 * @return An iterator @p mid such that @p all_of(first, mid, pred)
815 * and @p none_of(mid, last, pred) are both true.
817 template<typename _ForwardIterator
, typename _Predicate
>
819 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
822 // concept requirements
823 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
824 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
825 typename iterator_traits
<_ForwardIterator
>::value_type
>)
827 // A specific debug-mode test will be necessary...
828 __glibcxx_requires_valid_range(__first
, __last
);
830 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
833 _DistanceType __len
= std::distance(__first
, __last
);
834 _DistanceType __half
;
835 _ForwardIterator __middle
;
841 std::advance(__middle
, __half
);
842 if (__pred(*__middle
))
846 __len
= __len
- __half
- 1;
857 * @brief Copy a sequence, removing elements of a given value.
858 * @ingroup mutating_algorithms
859 * @param first An input iterator.
860 * @param last An input iterator.
861 * @param result An output iterator.
862 * @param value The value to be removed.
863 * @return An iterator designating the end of the resulting sequence.
865 * Copies each element in the range @p [first,last) not equal to @p value
866 * to the range beginning at @p result.
867 * remove_copy() is stable, so the relative order of elements that are
868 * copied is unchanged.
870 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
872 remove_copy(_InputIterator __first
, _InputIterator __last
,
873 _OutputIterator __result
, const _Tp
& __value
)
875 // concept requirements
876 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
877 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
878 typename iterator_traits
<_InputIterator
>::value_type
>)
879 __glibcxx_function_requires(_EqualOpConcept
<
880 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
881 __glibcxx_requires_valid_range(__first
, __last
);
883 for (; __first
!= __last
; ++__first
)
884 if (!(*__first
== __value
))
886 *__result
= *__first
;
893 * @brief Copy a sequence, removing elements for which a predicate is true.
894 * @ingroup mutating_algorithms
895 * @param first An input iterator.
896 * @param last An input iterator.
897 * @param result An output iterator.
898 * @param pred A predicate.
899 * @return An iterator designating the end of the resulting sequence.
901 * Copies each element in the range @p [first,last) for which
902 * @p pred returns false to the range beginning at @p result.
904 * remove_copy_if() is stable, so the relative order of elements that are
905 * copied is unchanged.
907 template<typename _InputIterator
, typename _OutputIterator
,
910 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
911 _OutputIterator __result
, _Predicate __pred
)
913 // concept requirements
914 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
915 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
916 typename iterator_traits
<_InputIterator
>::value_type
>)
917 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
918 typename iterator_traits
<_InputIterator
>::value_type
>)
919 __glibcxx_requires_valid_range(__first
, __last
);
921 for (; __first
!= __last
; ++__first
)
922 if (!bool(__pred(*__first
)))
924 *__result
= *__first
;
930 #ifdef __GXX_EXPERIMENTAL_CXX0X__
932 * @brief Copy the elements of a sequence for which a predicate is true.
933 * @ingroup mutating_algorithms
934 * @param first An input iterator.
935 * @param last An input iterator.
936 * @param result An output iterator.
937 * @param pred A predicate.
938 * @return An iterator designating the end of the resulting sequence.
940 * Copies each element in the range @p [first,last) for which
941 * @p pred returns true to the range beginning at @p result.
943 * copy_if() is stable, so the relative order of elements that are
944 * copied is unchanged.
946 template<typename _InputIterator
, typename _OutputIterator
,
949 copy_if(_InputIterator __first
, _InputIterator __last
,
950 _OutputIterator __result
, _Predicate __pred
)
952 // concept requirements
953 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
954 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
955 typename iterator_traits
<_InputIterator
>::value_type
>)
956 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
957 typename iterator_traits
<_InputIterator
>::value_type
>)
958 __glibcxx_requires_valid_range(__first
, __last
);
960 for (; __first
!= __last
; ++__first
)
961 if (__pred(*__first
))
963 *__result
= *__first
;
970 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
972 __copy_n(_InputIterator __first
, _Size __n
,
973 _OutputIterator __result
, input_iterator_tag
)
975 for (; __n
> 0; --__n
)
977 *__result
= *__first
;
984 template<typename _RandomAccessIterator
, typename _Size
,
985 typename _OutputIterator
>
986 inline _OutputIterator
987 __copy_n(_RandomAccessIterator __first
, _Size __n
,
988 _OutputIterator __result
, random_access_iterator_tag
)
989 { return std::copy(__first
, __first
+ __n
, __result
); }
992 * @brief Copies the range [first,first+n) into [result,result+n).
993 * @ingroup mutating_algorithms
994 * @param first An input iterator.
995 * @param n The number of elements to copy.
996 * @param result An output iterator.
999 * This inline function will boil down to a call to @c memmove whenever
1000 * possible. Failing that, if random access iterators are passed, then the
1001 * loop count will be known (and therefore a candidate for compiler
1002 * optimizations such as unrolling).
1004 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1005 inline _OutputIterator
1006 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1008 // concept requirements
1009 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1010 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1011 typename iterator_traits
<_InputIterator
>::value_type
>)
1013 return std::__copy_n(__first
, __n
, __result
,
1014 std::__iterator_category(__first
));
1018 * @brief Copy the elements of a sequence to separate output sequences
1019 * depending on the truth value of a predicate.
1020 * @ingroup mutating_algorithms
1021 * @param first An input iterator.
1022 * @param last An input iterator.
1023 * @param out_true An output iterator.
1024 * @param out_false An output iterator.
1025 * @param pred A predicate.
1026 * @return A pair designating the ends of the resulting sequences.
1028 * Copies each element in the range @p [first,last) for which
1029 * @p pred returns true to the range beginning at @p out_true
1030 * and each element for which @p pred returns false to @p out_false.
1032 template<typename _InputIterator
, typename _OutputIterator1
,
1033 typename _OutputIterator2
, typename _Predicate
>
1034 pair
<_OutputIterator1
, _OutputIterator2
>
1035 partition_copy(_InputIterator __first
, _InputIterator __last
,
1036 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1039 // concept requirements
1040 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1041 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1042 typename iterator_traits
<_InputIterator
>::value_type
>)
1043 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1044 typename iterator_traits
<_InputIterator
>::value_type
>)
1045 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1046 typename iterator_traits
<_InputIterator
>::value_type
>)
1047 __glibcxx_requires_valid_range(__first
, __last
);
1049 for (; __first
!= __last
; ++__first
)
1050 if (__pred(*__first
))
1052 *__out_true
= *__first
;
1057 *__out_false
= *__first
;
1061 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1066 * @brief Remove elements from a sequence.
1067 * @ingroup mutating_algorithms
1068 * @param first An input iterator.
1069 * @param last An input iterator.
1070 * @param value The value to be removed.
1071 * @return An iterator designating the end of the resulting sequence.
1073 * All elements equal to @p value are removed from the range
1076 * remove() is stable, so the relative order of elements that are
1077 * not removed is unchanged.
1079 * Elements between the end of the resulting sequence and @p last
1080 * are still present, but their value is unspecified.
1082 template<typename _ForwardIterator
, typename _Tp
>
1084 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1087 // concept requirements
1088 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1090 __glibcxx_function_requires(_EqualOpConcept
<
1091 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1092 __glibcxx_requires_valid_range(__first
, __last
);
1094 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1095 if(__first
== __last
)
1097 _ForwardIterator __result
= __first
;
1099 for(; __first
!= __last
; ++__first
)
1100 if(!(*__first
== __value
))
1102 *__result
= _GLIBCXX_MOVE(*__first
);
1109 * @brief Remove elements from a sequence using a predicate.
1110 * @ingroup mutating_algorithms
1111 * @param first A forward iterator.
1112 * @param last A forward iterator.
1113 * @param pred A predicate.
1114 * @return An iterator designating the end of the resulting sequence.
1116 * All elements for which @p pred returns true are removed from the range
1119 * remove_if() is stable, so the relative order of elements that are
1120 * not removed is unchanged.
1122 * Elements between the end of the resulting sequence and @p last
1123 * are still present, but their value is unspecified.
1125 template<typename _ForwardIterator
, typename _Predicate
>
1127 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1130 // concept requirements
1131 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1133 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1134 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1135 __glibcxx_requires_valid_range(__first
, __last
);
1137 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1138 if(__first
== __last
)
1140 _ForwardIterator __result
= __first
;
1142 for(; __first
!= __last
; ++__first
)
1143 if(!bool(__pred(*__first
)))
1145 *__result
= _GLIBCXX_MOVE(*__first
);
1152 * @brief Remove consecutive duplicate values from a sequence.
1153 * @ingroup mutating_algorithms
1154 * @param first A forward iterator.
1155 * @param last A forward iterator.
1156 * @return An iterator designating the end of the resulting sequence.
1158 * Removes all but the first element from each group of consecutive
1159 * values that compare equal.
1160 * unique() is stable, so the relative order of elements that are
1161 * not removed is unchanged.
1162 * Elements between the end of the resulting sequence and @p last
1163 * are still present, but their value is unspecified.
1165 template<typename _ForwardIterator
>
1167 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1169 // concept requirements
1170 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1172 __glibcxx_function_requires(_EqualityComparableConcept
<
1173 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1174 __glibcxx_requires_valid_range(__first
, __last
);
1176 // Skip the beginning, if already unique.
1177 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1178 if (__first
== __last
)
1181 // Do the real copy work.
1182 _ForwardIterator __dest
= __first
;
1184 while (++__first
!= __last
)
1185 if (!(*__dest
== *__first
))
1186 *++__dest
= _GLIBCXX_MOVE(*__first
);
1191 * @brief Remove consecutive values from a sequence using a predicate.
1192 * @ingroup mutating_algorithms
1193 * @param first A forward iterator.
1194 * @param last A forward iterator.
1195 * @param binary_pred A binary predicate.
1196 * @return An iterator designating the end of the resulting sequence.
1198 * Removes all but the first element from each group of consecutive
1199 * values for which @p binary_pred returns true.
1200 * unique() is stable, so the relative order of elements that are
1201 * not removed is unchanged.
1202 * Elements between the end of the resulting sequence and @p last
1203 * are still present, but their value is unspecified.
1205 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1207 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1208 _BinaryPredicate __binary_pred
)
1210 // concept requirements
1211 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1213 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1214 typename iterator_traits
<_ForwardIterator
>::value_type
,
1215 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1216 __glibcxx_requires_valid_range(__first
, __last
);
1218 // Skip the beginning, if already unique.
1219 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1220 if (__first
== __last
)
1223 // Do the real copy work.
1224 _ForwardIterator __dest
= __first
;
1226 while (++__first
!= __last
)
1227 if (!bool(__binary_pred(*__dest
, *__first
)))
1228 *++__dest
= _GLIBCXX_MOVE(*__first
);
1233 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1235 * overloaded for forward iterators and output iterator as result.
1237 template<typename _ForwardIterator
, typename _OutputIterator
>
1239 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1240 _OutputIterator __result
,
1241 forward_iterator_tag
, output_iterator_tag
)
1243 // concept requirements -- taken care of in dispatching function
1244 _ForwardIterator __next
= __first
;
1245 *__result
= *__first
;
1246 while (++__next
!= __last
)
1247 if (!(*__first
== *__next
))
1250 *++__result
= *__first
;
1256 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1258 * overloaded for input iterators and output iterator as result.
1260 template<typename _InputIterator
, typename _OutputIterator
>
1262 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1263 _OutputIterator __result
,
1264 input_iterator_tag
, output_iterator_tag
)
1266 // concept requirements -- taken care of in dispatching function
1267 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1268 *__result
= __value
;
1269 while (++__first
!= __last
)
1270 if (!(__value
== *__first
))
1273 *++__result
= __value
;
1279 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1281 * overloaded for input iterators and forward iterator as result.
1283 template<typename _InputIterator
, typename _ForwardIterator
>
1285 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1286 _ForwardIterator __result
,
1287 input_iterator_tag
, forward_iterator_tag
)
1289 // concept requirements -- taken care of in dispatching function
1290 *__result
= *__first
;
1291 while (++__first
!= __last
)
1292 if (!(*__result
== *__first
))
1293 *++__result
= *__first
;
1298 * This is an uglified
1299 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1301 * overloaded for forward iterators and output iterator as result.
1303 template<typename _ForwardIterator
, typename _OutputIterator
,
1304 typename _BinaryPredicate
>
1306 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1307 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1308 forward_iterator_tag
, output_iterator_tag
)
1310 // concept requirements -- iterators already checked
1311 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1312 typename iterator_traits
<_ForwardIterator
>::value_type
,
1313 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1315 _ForwardIterator __next
= __first
;
1316 *__result
= *__first
;
1317 while (++__next
!= __last
)
1318 if (!bool(__binary_pred(*__first
, *__next
)))
1321 *++__result
= *__first
;
1327 * This is an uglified
1328 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1330 * overloaded for input iterators and output iterator as result.
1332 template<typename _InputIterator
, typename _OutputIterator
,
1333 typename _BinaryPredicate
>
1335 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1336 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1337 input_iterator_tag
, output_iterator_tag
)
1339 // concept requirements -- iterators already checked
1340 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1341 typename iterator_traits
<_InputIterator
>::value_type
,
1342 typename iterator_traits
<_InputIterator
>::value_type
>)
1344 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1345 *__result
= __value
;
1346 while (++__first
!= __last
)
1347 if (!bool(__binary_pred(__value
, *__first
)))
1350 *++__result
= __value
;
1356 * This is an uglified
1357 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1359 * overloaded for input iterators and forward iterator as result.
1361 template<typename _InputIterator
, typename _ForwardIterator
,
1362 typename _BinaryPredicate
>
1364 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1365 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1366 input_iterator_tag
, forward_iterator_tag
)
1368 // concept requirements -- iterators already checked
1369 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1370 typename iterator_traits
<_ForwardIterator
>::value_type
,
1371 typename iterator_traits
<_InputIterator
>::value_type
>)
1373 *__result
= *__first
;
1374 while (++__first
!= __last
)
1375 if (!bool(__binary_pred(*__result
, *__first
)))
1376 *++__result
= *__first
;
1381 * This is an uglified reverse(_BidirectionalIterator,
1382 * _BidirectionalIterator)
1383 * overloaded for bidirectional iterators.
1385 template<typename _BidirectionalIterator
>
1387 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1388 bidirectional_iterator_tag
)
1391 if (__first
== __last
|| __first
== --__last
)
1395 std::iter_swap(__first
, __last
);
1401 * This is an uglified reverse(_BidirectionalIterator,
1402 * _BidirectionalIterator)
1403 * overloaded for random access iterators.
1405 template<typename _RandomAccessIterator
>
1407 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1408 random_access_iterator_tag
)
1410 if (__first
== __last
)
1413 while (__first
< __last
)
1415 std::iter_swap(__first
, __last
);
1422 * @brief Reverse a sequence.
1423 * @ingroup mutating_algorithms
1424 * @param first A bidirectional iterator.
1425 * @param last A bidirectional iterator.
1426 * @return reverse() returns no value.
1428 * Reverses the order of the elements in the range @p [first,last),
1429 * so that the first element becomes the last etc.
1430 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1431 * swaps @p *(first+i) and @p *(last-(i+1))
1433 template<typename _BidirectionalIterator
>
1435 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1437 // concept requirements
1438 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1439 _BidirectionalIterator
>)
1440 __glibcxx_requires_valid_range(__first
, __last
);
1441 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1445 * @brief Copy a sequence, reversing its elements.
1446 * @ingroup mutating_algorithms
1447 * @param first A bidirectional iterator.
1448 * @param last A bidirectional iterator.
1449 * @param result An output iterator.
1450 * @return An iterator designating the end of the resulting sequence.
1452 * Copies the elements in the range @p [first,last) to the range
1453 * @p [result,result+(last-first)) such that the order of the
1454 * elements is reversed.
1455 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1456 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1457 * The ranges @p [first,last) and @p [result,result+(last-first))
1460 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1462 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1463 _OutputIterator __result
)
1465 // concept requirements
1466 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1467 _BidirectionalIterator
>)
1468 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1469 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1470 __glibcxx_requires_valid_range(__first
, __last
);
1472 while (__first
!= __last
)
1475 *__result
= *__last
;
1482 * This is a helper function for the rotate algorithm specialized on RAIs.
1483 * It returns the greatest common divisor of two integer values.
1485 template<typename _EuclideanRingElement
>
1486 _EuclideanRingElement
1487 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1491 _EuclideanRingElement __t
= __m
% __n
;
1498 /// This is a helper function for the rotate algorithm.
1499 template<typename _ForwardIterator
>
1501 __rotate(_ForwardIterator __first
,
1502 _ForwardIterator __middle
,
1503 _ForwardIterator __last
,
1504 forward_iterator_tag
)
1506 if (__first
== __middle
|| __last
== __middle
)
1509 _ForwardIterator __first2
= __middle
;
1512 std::iter_swap(__first
, __first2
);
1515 if (__first
== __middle
)
1516 __middle
= __first2
;
1518 while (__first2
!= __last
);
1520 __first2
= __middle
;
1522 while (__first2
!= __last
)
1524 std::iter_swap(__first
, __first2
);
1527 if (__first
== __middle
)
1528 __middle
= __first2
;
1529 else if (__first2
== __last
)
1530 __first2
= __middle
;
1534 /// This is a helper function for the rotate algorithm.
1535 template<typename _BidirectionalIterator
>
1537 __rotate(_BidirectionalIterator __first
,
1538 _BidirectionalIterator __middle
,
1539 _BidirectionalIterator __last
,
1540 bidirectional_iterator_tag
)
1542 // concept requirements
1543 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1544 _BidirectionalIterator
>)
1546 if (__first
== __middle
|| __last
== __middle
)
1549 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1550 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1552 while (__first
!= __middle
&& __middle
!= __last
)
1554 std::iter_swap(__first
, --__last
);
1558 if (__first
== __middle
)
1559 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1561 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1564 /// This is a helper function for the rotate algorithm.
1565 template<typename _RandomAccessIterator
>
1567 __rotate(_RandomAccessIterator __first
,
1568 _RandomAccessIterator __middle
,
1569 _RandomAccessIterator __last
,
1570 random_access_iterator_tag
)
1572 // concept requirements
1573 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1574 _RandomAccessIterator
>)
1576 if (__first
== __middle
|| __last
== __middle
)
1579 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1581 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1584 _Distance __n
= __last
- __first
;
1585 _Distance __k
= __middle
- __first
;
1587 if (__k
== __n
- __k
)
1589 std::swap_ranges(__first
, __middle
, __middle
);
1593 _RandomAccessIterator __p
= __first
;
1597 if (__k
< __n
- __k
)
1599 if (__is_pod(_ValueType
) && __k
== 1)
1601 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1602 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1603 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1606 _RandomAccessIterator __q
= __p
+ __k
;
1607 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1609 std::iter_swap(__p
, __q
);
1616 std::swap(__n
, __k
);
1622 if (__is_pod(_ValueType
) && __k
== 1)
1624 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1625 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1626 *__p
= _GLIBCXX_MOVE(__t
);
1629 _RandomAccessIterator __q
= __p
+ __n
;
1631 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1635 std::iter_swap(__p
, __q
);
1640 std::swap(__n
, __k
);
1646 * @brief Rotate the elements of a sequence.
1647 * @ingroup mutating_algorithms
1648 * @param first A forward iterator.
1649 * @param middle A forward iterator.
1650 * @param last A forward iterator.
1653 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1654 * positions so that the element at @p middle is moved to @p first, the
1655 * element at @p middle+1 is moved to @first+1 and so on for each element
1656 * in the range @p [first,last).
1658 * This effectively swaps the ranges @p [first,middle) and
1661 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1662 * each @p n in the range @p [0,last-first).
1664 template<typename _ForwardIterator
>
1666 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1667 _ForwardIterator __last
)
1669 // concept requirements
1670 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1672 __glibcxx_requires_valid_range(__first
, __middle
);
1673 __glibcxx_requires_valid_range(__middle
, __last
);
1675 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1677 std::__rotate(__first
, __middle
, __last
, _IterType());
1681 * @brief Copy a sequence, rotating its elements.
1682 * @ingroup mutating_algorithms
1683 * @param first A forward iterator.
1684 * @param middle A forward iterator.
1685 * @param last A forward iterator.
1686 * @param result An output iterator.
1687 * @return An iterator designating the end of the resulting sequence.
1689 * Copies the elements of the range @p [first,last) to the range
1690 * beginning at @result, rotating the copied elements by @p (middle-first)
1691 * positions so that the element at @p middle is moved to @p result, the
1692 * element at @p middle+1 is moved to @result+1 and so on for each element
1693 * in the range @p [first,last).
1695 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1696 * each @p n in the range @p [0,last-first).
1698 template<typename _ForwardIterator
, typename _OutputIterator
>
1700 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1701 _ForwardIterator __last
, _OutputIterator __result
)
1703 // concept requirements
1704 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1705 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1706 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1707 __glibcxx_requires_valid_range(__first
, __middle
);
1708 __glibcxx_requires_valid_range(__middle
, __last
);
1710 return std::copy(__first
, __middle
,
1711 std::copy(__middle
, __last
, __result
));
1714 /// This is a helper function...
1715 template<typename _ForwardIterator
, typename _Predicate
>
1717 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1718 _Predicate __pred
, forward_iterator_tag
)
1720 if (__first
== __last
)
1723 while (__pred(*__first
))
1724 if (++__first
== __last
)
1727 _ForwardIterator __next
= __first
;
1729 while (++__next
!= __last
)
1730 if (__pred(*__next
))
1732 std::iter_swap(__first
, __next
);
1739 /// This is a helper function...
1740 template<typename _BidirectionalIterator
, typename _Predicate
>
1741 _BidirectionalIterator
1742 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1743 _Predicate __pred
, bidirectional_iterator_tag
)
1748 if (__first
== __last
)
1750 else if (__pred(*__first
))
1756 if (__first
== __last
)
1758 else if (!bool(__pred(*__last
)))
1762 std::iter_swap(__first
, __last
);
1769 /// This is a helper function...
1770 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1772 __inplace_stable_partition(_ForwardIterator __first
,
1773 _ForwardIterator __last
,
1774 _Predicate __pred
, _Distance __len
)
1777 return __pred(*__first
) ? __last
: __first
;
1778 _ForwardIterator __middle
= __first
;
1779 std::advance(__middle
, __len
/ 2);
1780 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1784 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1788 std::rotate(__begin
, __middle
, __end
);
1789 std::advance(__begin
, std::distance(__middle
, __end
));
1793 /// This is a helper function...
1794 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1797 __stable_partition_adaptive(_ForwardIterator __first
,
1798 _ForwardIterator __last
,
1799 _Predicate __pred
, _Distance __len
,
1801 _Distance __buffer_size
)
1803 if (__len
<= __buffer_size
)
1805 _ForwardIterator __result1
= __first
;
1806 _Pointer __result2
= __buffer
;
1807 for (; __first
!= __last
; ++__first
)
1808 if (__pred(*__first
))
1810 *__result1
= _GLIBCXX_MOVE(*__first
);
1815 *__result2
= _GLIBCXX_MOVE(*__first
);
1818 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1823 _ForwardIterator __middle
= __first
;
1824 std::advance(__middle
, __len
/ 2);
1825 _ForwardIterator __begin
=
1826 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1827 __len
/ 2, __buffer
,
1829 _ForwardIterator __end
=
1830 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1832 __buffer
, __buffer_size
);
1833 std::rotate(__begin
, __middle
, __end
);
1834 std::advance(__begin
, std::distance(__middle
, __end
));
1840 * @brief Move elements for which a predicate is true to the beginning
1841 * of a sequence, preserving relative ordering.
1842 * @ingroup mutating_algorithms
1843 * @param first A forward iterator.
1844 * @param last A forward iterator.
1845 * @param pred A predicate functor.
1846 * @return An iterator @p middle such that @p pred(i) is true for each
1847 * iterator @p i in the range @p [first,middle) and false for each @p i
1848 * in the range @p [middle,last).
1850 * Performs the same function as @p partition() with the additional
1851 * guarantee that the relative ordering of elements in each group is
1852 * preserved, so any two elements @p x and @p y in the range
1853 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1854 * relative ordering after calling @p stable_partition().
1856 template<typename _ForwardIterator
, typename _Predicate
>
1858 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1861 // concept requirements
1862 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1864 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1865 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1866 __glibcxx_requires_valid_range(__first
, __last
);
1868 if (__first
== __last
)
1872 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1874 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1877 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1879 if (__buf
.size() > 0)
1881 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1882 _DistanceType(__buf
.requested_size()),
1884 _DistanceType(__buf
.size()));
1887 std::__inplace_stable_partition(__first
, __last
, __pred
,
1888 _DistanceType(__buf
.requested_size()));
1892 /// This is a helper function for the sort routines.
1893 template<typename _RandomAccessIterator
>
1895 __heap_select(_RandomAccessIterator __first
,
1896 _RandomAccessIterator __middle
,
1897 _RandomAccessIterator __last
)
1899 std::make_heap(__first
, __middle
);
1900 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1901 if (*__i
< *__first
)
1902 std::__pop_heap(__first
, __middle
, __i
);
1905 /// This is a helper function for the sort routines.
1906 template<typename _RandomAccessIterator
, typename _Compare
>
1908 __heap_select(_RandomAccessIterator __first
,
1909 _RandomAccessIterator __middle
,
1910 _RandomAccessIterator __last
, _Compare __comp
)
1912 std::make_heap(__first
, __middle
, __comp
);
1913 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1914 if (__comp(*__i
, *__first
))
1915 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1921 * @brief Copy the smallest elements of a sequence.
1922 * @ingroup sorting_algorithms
1923 * @param first An iterator.
1924 * @param last Another iterator.
1925 * @param result_first A random-access iterator.
1926 * @param result_last Another random-access iterator.
1927 * @return An iterator indicating the end of the resulting sequence.
1929 * Copies and sorts the smallest N values from the range @p [first,last)
1930 * to the range beginning at @p result_first, where the number of
1931 * elements to be copied, @p N, is the smaller of @p (last-first) and
1932 * @p (result_last-result_first).
1933 * After the sort if @p i and @j are iterators in the range
1934 * @p [result_first,result_first+N) such that @i precedes @j then
1935 * @p *j<*i is false.
1936 * The value returned is @p result_first+N.
1938 template<typename _InputIterator
, typename _RandomAccessIterator
>
1939 _RandomAccessIterator
1940 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1941 _RandomAccessIterator __result_first
,
1942 _RandomAccessIterator __result_last
)
1944 typedef typename iterator_traits
<_InputIterator
>::value_type
1946 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1948 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1951 // concept requirements
1952 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1953 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1955 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1957 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1958 __glibcxx_requires_valid_range(__first
, __last
);
1959 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1961 if (__result_first
== __result_last
)
1962 return __result_last
;
1963 _RandomAccessIterator __result_real_last
= __result_first
;
1964 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1966 *__result_real_last
= *__first
;
1967 ++__result_real_last
;
1970 std::make_heap(__result_first
, __result_real_last
);
1971 while (__first
!= __last
)
1973 if (*__first
< *__result_first
)
1974 std::__adjust_heap(__result_first
, _DistanceType(0),
1975 _DistanceType(__result_real_last
1977 _InputValueType(*__first
));
1980 std::sort_heap(__result_first
, __result_real_last
);
1981 return __result_real_last
;
1985 * @brief Copy the smallest elements of a sequence using a predicate for
1987 * @ingroup sorting_algorithms
1988 * @param first An input iterator.
1989 * @param last Another input iterator.
1990 * @param result_first A random-access iterator.
1991 * @param result_last Another random-access iterator.
1992 * @param comp A comparison functor.
1993 * @return An iterator indicating the end of the resulting sequence.
1995 * Copies and sorts the smallest N values from the range @p [first,last)
1996 * to the range beginning at @p result_first, where the number of
1997 * elements to be copied, @p N, is the smaller of @p (last-first) and
1998 * @p (result_last-result_first).
1999 * After the sort if @p i and @j are iterators in the range
2000 * @p [result_first,result_first+N) such that @i precedes @j then
2001 * @p comp(*j,*i) is false.
2002 * The value returned is @p result_first+N.
2004 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2005 _RandomAccessIterator
2006 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2007 _RandomAccessIterator __result_first
,
2008 _RandomAccessIterator __result_last
,
2011 typedef typename iterator_traits
<_InputIterator
>::value_type
2013 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2015 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2018 // concept requirements
2019 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2020 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2021 _RandomAccessIterator
>)
2022 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2024 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2025 _InputValueType
, _OutputValueType
>)
2026 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2027 _OutputValueType
, _OutputValueType
>)
2028 __glibcxx_requires_valid_range(__first
, __last
);
2029 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2031 if (__result_first
== __result_last
)
2032 return __result_last
;
2033 _RandomAccessIterator __result_real_last
= __result_first
;
2034 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2036 *__result_real_last
= *__first
;
2037 ++__result_real_last
;
2040 std::make_heap(__result_first
, __result_real_last
, __comp
);
2041 while (__first
!= __last
)
2043 if (__comp(*__first
, *__result_first
))
2044 std::__adjust_heap(__result_first
, _DistanceType(0),
2045 _DistanceType(__result_real_last
2047 _InputValueType(*__first
),
2051 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2052 return __result_real_last
;
2055 /// This is a helper function for the sort routine.
2056 template<typename _RandomAccessIterator
>
2058 __unguarded_linear_insert(_RandomAccessIterator __last
)
2060 typename iterator_traits
<_RandomAccessIterator
>::value_type
2061 __val
= _GLIBCXX_MOVE(*__last
);
2062 _RandomAccessIterator __next
= __last
;
2064 while (__val
< *__next
)
2066 *__last
= _GLIBCXX_MOVE(*__next
);
2070 *__last
= _GLIBCXX_MOVE(__val
);
2073 /// This is a helper function for the sort routine.
2074 template<typename _RandomAccessIterator
, typename _Compare
>
2076 __unguarded_linear_insert(_RandomAccessIterator __last
,
2079 typename iterator_traits
<_RandomAccessIterator
>::value_type
2080 __val
= _GLIBCXX_MOVE(*__last
);
2081 _RandomAccessIterator __next
= __last
;
2083 while (__comp(__val
, *__next
))
2085 *__last
= _GLIBCXX_MOVE(*__next
);
2089 *__last
= _GLIBCXX_MOVE(__val
);
2092 /// This is a helper function for the sort routine.
2093 template<typename _RandomAccessIterator
>
2095 __insertion_sort(_RandomAccessIterator __first
,
2096 _RandomAccessIterator __last
)
2098 if (__first
== __last
)
2101 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2103 if (*__i
< *__first
)
2105 typename iterator_traits
<_RandomAccessIterator
>::value_type
2106 __val
= _GLIBCXX_MOVE(*__i
);
2107 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2108 *__first
= _GLIBCXX_MOVE(__val
);
2111 std::__unguarded_linear_insert(__i
);
2115 /// This is a helper function for the sort routine.
2116 template<typename _RandomAccessIterator
, typename _Compare
>
2118 __insertion_sort(_RandomAccessIterator __first
,
2119 _RandomAccessIterator __last
, _Compare __comp
)
2121 if (__first
== __last
) return;
2123 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2125 if (__comp(*__i
, *__first
))
2127 typename iterator_traits
<_RandomAccessIterator
>::value_type
2128 __val
= _GLIBCXX_MOVE(*__i
);
2129 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2130 *__first
= _GLIBCXX_MOVE(__val
);
2133 std::__unguarded_linear_insert(__i
, __comp
);
2137 /// This is a helper function for the sort routine.
2138 template<typename _RandomAccessIterator
>
2140 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2141 _RandomAccessIterator __last
)
2143 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2146 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2147 std::__unguarded_linear_insert(__i
);
2150 /// This is a helper function for the sort routine.
2151 template<typename _RandomAccessIterator
, typename _Compare
>
2153 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2154 _RandomAccessIterator __last
, _Compare __comp
)
2156 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2159 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2160 std::__unguarded_linear_insert(__i
, __comp
);
2165 * This controls some aspect of the sort routines.
2167 enum { _S_threshold
= 16 };
2169 /// This is a helper function for the sort routine.
2170 template<typename _RandomAccessIterator
>
2172 __final_insertion_sort(_RandomAccessIterator __first
,
2173 _RandomAccessIterator __last
)
2175 if (__last
- __first
> int(_S_threshold
))
2177 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2178 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2181 std::__insertion_sort(__first
, __last
);
2184 /// This is a helper function for the sort routine.
2185 template<typename _RandomAccessIterator
, typename _Compare
>
2187 __final_insertion_sort(_RandomAccessIterator __first
,
2188 _RandomAccessIterator __last
, _Compare __comp
)
2190 if (__last
- __first
> int(_S_threshold
))
2192 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2193 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2197 std::__insertion_sort(__first
, __last
, __comp
);
2200 /// This is a helper function...
2201 template<typename _RandomAccessIterator
, typename _Tp
>
2202 _RandomAccessIterator
2203 __unguarded_partition(_RandomAccessIterator __first
,
2204 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2208 while (*__first
< __pivot
)
2211 while (__pivot
< *__last
)
2213 if (!(__first
< __last
))
2215 std::iter_swap(__first
, __last
);
2220 /// This is a helper function...
2221 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2222 _RandomAccessIterator
2223 __unguarded_partition(_RandomAccessIterator __first
,
2224 _RandomAccessIterator __last
,
2225 const _Tp
& __pivot
, _Compare __comp
)
2229 while (__comp(*__first
, __pivot
))
2232 while (__comp(__pivot
, *__last
))
2234 if (!(__first
< __last
))
2236 std::iter_swap(__first
, __last
);
2241 /// This is a helper function...
2242 template<typename _RandomAccessIterator
>
2243 inline _RandomAccessIterator
2244 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2245 _RandomAccessIterator __last
)
2247 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2248 std::__move_median_first(__first
, __mid
, (__last
- 1));
2249 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2253 /// This is a helper function...
2254 template<typename _RandomAccessIterator
, typename _Compare
>
2255 inline _RandomAccessIterator
2256 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2257 _RandomAccessIterator __last
, _Compare __comp
)
2259 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2260 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2261 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2264 /// This is a helper function for the sort routine.
2265 template<typename _RandomAccessIterator
, typename _Size
>
2267 __introsort_loop(_RandomAccessIterator __first
,
2268 _RandomAccessIterator __last
,
2269 _Size __depth_limit
)
2271 while (__last
- __first
> int(_S_threshold
))
2273 if (__depth_limit
== 0)
2275 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2279 _RandomAccessIterator __cut
=
2280 std::__unguarded_partition_pivot(__first
, __last
);
2281 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2286 /// This is a helper function for the sort routine.
2287 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2289 __introsort_loop(_RandomAccessIterator __first
,
2290 _RandomAccessIterator __last
,
2291 _Size __depth_limit
, _Compare __comp
)
2293 while (__last
- __first
> int(_S_threshold
))
2295 if (__depth_limit
== 0)
2297 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2301 _RandomAccessIterator __cut
=
2302 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2303 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2310 template<typename _RandomAccessIterator
, typename _Size
>
2312 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2313 _RandomAccessIterator __last
, _Size __depth_limit
)
2315 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2318 while (__last
- __first
> 3)
2320 if (__depth_limit
== 0)
2322 std::__heap_select(__first
, __nth
+ 1, __last
);
2324 // Place the nth largest element in its final position.
2325 std::iter_swap(__first
, __nth
);
2329 _RandomAccessIterator __cut
=
2330 std::__unguarded_partition_pivot(__first
, __last
);
2336 std::__insertion_sort(__first
, __last
);
2339 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2341 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2342 _RandomAccessIterator __last
, _Size __depth_limit
,
2345 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2348 while (__last
- __first
> 3)
2350 if (__depth_limit
== 0)
2352 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2353 // Place the nth largest element in its final position.
2354 std::iter_swap(__first
, __nth
);
2358 _RandomAccessIterator __cut
=
2359 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2365 std::__insertion_sort(__first
, __last
, __comp
);
2370 // lower_bound moved to stl_algobase.h
2373 * @brief Finds the first position in which @a val could be inserted
2374 * without changing the ordering.
2375 * @ingroup binary_search_algorithms
2376 * @param first An iterator.
2377 * @param last Another iterator.
2378 * @param val The search term.
2379 * @param comp A functor to use for comparisons.
2380 * @return An iterator pointing to the first element <em>not less
2381 * than</em> @a val, or end() if every element is less
2383 * @ingroup binary_search_algorithms
2385 * The comparison function should have the same effects on ordering as
2386 * the function used for the initial sort.
2388 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2390 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2391 const _Tp
& __val
, _Compare __comp
)
2393 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2395 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2398 // concept requirements
2399 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2400 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2402 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2405 _DistanceType __len
= std::distance(__first
, __last
);
2406 _DistanceType __half
;
2407 _ForwardIterator __middle
;
2411 __half
= __len
>> 1;
2413 std::advance(__middle
, __half
);
2414 if (__comp(*__middle
, __val
))
2418 __len
= __len
- __half
- 1;
2427 * @brief Finds the last position in which @a val could be inserted
2428 * without changing the ordering.
2429 * @ingroup binary_search_algorithms
2430 * @param first An iterator.
2431 * @param last Another iterator.
2432 * @param val The search term.
2433 * @return An iterator pointing to the first element greater than @a val,
2434 * or end() if no elements are greater than @a val.
2435 * @ingroup binary_search_algorithms
2437 template<typename _ForwardIterator
, typename _Tp
>
2439 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2442 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2444 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2447 // concept requirements
2448 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2449 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2450 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2452 _DistanceType __len
= std::distance(__first
, __last
);
2453 _DistanceType __half
;
2454 _ForwardIterator __middle
;
2458 __half
= __len
>> 1;
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
);
2506 _DistanceType __half
;
2507 _ForwardIterator __middle
;
2511 __half
= __len
>> 1;
2513 std::advance(__middle
, __half
);
2514 if (__comp(__val
, *__middle
))
2520 __len
= __len
- __half
- 1;
2527 * @brief Finds the largest subrange in which @a val could be inserted
2528 * at any place in it without changing the ordering.
2529 * @ingroup binary_search_algorithms
2530 * @param first An iterator.
2531 * @param last Another iterator.
2532 * @param val The search term.
2533 * @return An pair of iterators defining the subrange.
2534 * @ingroup binary_search_algorithms
2536 * This is equivalent to
2538 * std::make_pair(lower_bound(first, last, val),
2539 * upper_bound(first, last, val))
2541 * but does not actually call those functions.
2543 template<typename _ForwardIterator
, typename _Tp
>
2544 pair
<_ForwardIterator
, _ForwardIterator
>
2545 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2548 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2550 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2553 // concept requirements
2554 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2555 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2556 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2557 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2558 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2560 _DistanceType __len
= std::distance(__first
, __last
);
2561 _DistanceType __half
;
2562 _ForwardIterator __middle
, __left
, __right
;
2566 __half
= __len
>> 1;
2568 std::advance(__middle
, __half
);
2569 if (*__middle
< __val
)
2573 __len
= __len
- __half
- 1;
2575 else if (__val
< *__middle
)
2579 __left
= std::lower_bound(__first
, __middle
, __val
);
2580 std::advance(__first
, __len
);
2581 __right
= std::upper_bound(++__middle
, __first
, __val
);
2582 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2585 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2589 * @brief Finds the largest subrange in which @a val could be inserted
2590 * at any place in it without changing the ordering.
2591 * @param first An iterator.
2592 * @param last Another iterator.
2593 * @param val The search term.
2594 * @param comp A functor to use for comparisons.
2595 * @return An pair of iterators defining the subrange.
2596 * @ingroup binary_search_algorithms
2598 * This is equivalent to
2600 * std::make_pair(lower_bound(first, last, val, comp),
2601 * upper_bound(first, last, val, comp))
2603 * but does not actually call those functions.
2605 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2606 pair
<_ForwardIterator
, _ForwardIterator
>
2607 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2611 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2613 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2616 // concept requirements
2617 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2618 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2620 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2622 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2624 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2627 _DistanceType __len
= std::distance(__first
, __last
);
2628 _DistanceType __half
;
2629 _ForwardIterator __middle
, __left
, __right
;
2633 __half
= __len
>> 1;
2635 std::advance(__middle
, __half
);
2636 if (__comp(*__middle
, __val
))
2640 __len
= __len
- __half
- 1;
2642 else if (__comp(__val
, *__middle
))
2646 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2647 std::advance(__first
, __len
);
2648 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2649 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2652 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2656 * @brief Determines whether an element exists in a range.
2657 * @ingroup binary_search_algorithms
2658 * @param first An iterator.
2659 * @param last Another iterator.
2660 * @param val The search term.
2661 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2663 * Note that this does not actually return an iterator to @a val. For
2664 * that, use std::find or a container's specialized find member functions.
2666 template<typename _ForwardIterator
, typename _Tp
>
2668 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2671 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2674 // concept requirements
2675 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2676 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2677 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2678 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2680 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2681 return __i
!= __last
&& !(__val
< *__i
);
2685 * @brief Determines whether an element exists in a range.
2686 * @ingroup binary_search_algorithms
2687 * @param first An iterator.
2688 * @param last Another iterator.
2689 * @param val The search term.
2690 * @param comp A functor to use for comparisons.
2691 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2693 * Note that this does not actually return an iterator to @a val. For
2694 * that, use std::find or a container's specialized find member functions.
2696 * The comparison function should have the same effects on ordering as
2697 * the function used for the initial sort.
2699 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2701 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2702 const _Tp
& __val
, _Compare __comp
)
2704 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2707 // concept requirements
2708 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2709 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2711 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2713 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2716 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2717 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2722 /// This is a helper function for the merge routines.
2723 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2724 typename _BidirectionalIterator3
>
2725 _BidirectionalIterator3
2726 __merge_backward(_BidirectionalIterator1 __first1
,
2727 _BidirectionalIterator1 __last1
,
2728 _BidirectionalIterator2 __first2
,
2729 _BidirectionalIterator2 __last2
,
2730 _BidirectionalIterator3 __result
)
2732 if (__first1
== __last1
)
2733 return std::copy_backward(__first2
, __last2
, __result
);
2734 if (__first2
== __last2
)
2735 return std::copy_backward(__first1
, __last1
, __result
);
2740 if (*__last2
< *__last1
)
2742 *--__result
= *__last1
;
2743 if (__first1
== __last1
)
2744 return std::copy_backward(__first2
, ++__last2
, __result
);
2749 *--__result
= *__last2
;
2750 if (__first2
== __last2
)
2751 return std::copy_backward(__first1
, ++__last1
, __result
);
2757 /// This is a helper function for the merge routines.
2758 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2759 typename _BidirectionalIterator3
, typename _Compare
>
2760 _BidirectionalIterator3
2761 __merge_backward(_BidirectionalIterator1 __first1
,
2762 _BidirectionalIterator1 __last1
,
2763 _BidirectionalIterator2 __first2
,
2764 _BidirectionalIterator2 __last2
,
2765 _BidirectionalIterator3 __result
,
2768 if (__first1
== __last1
)
2769 return std::copy_backward(__first2
, __last2
, __result
);
2770 if (__first2
== __last2
)
2771 return std::copy_backward(__first1
, __last1
, __result
);
2776 if (__comp(*__last2
, *__last1
))
2778 *--__result
= *__last1
;
2779 if (__first1
== __last1
)
2780 return std::copy_backward(__first2
, ++__last2
, __result
);
2785 *--__result
= *__last2
;
2786 if (__first2
== __last2
)
2787 return std::copy_backward(__first1
, ++__last1
, __result
);
2793 /// This is a helper function for the merge routines.
2794 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2796 _BidirectionalIterator1
2797 __rotate_adaptive(_BidirectionalIterator1 __first
,
2798 _BidirectionalIterator1 __middle
,
2799 _BidirectionalIterator1 __last
,
2800 _Distance __len1
, _Distance __len2
,
2801 _BidirectionalIterator2 __buffer
,
2802 _Distance __buffer_size
)
2804 _BidirectionalIterator2 __buffer_end
;
2805 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2807 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2808 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2809 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2811 else if (__len1
<= __buffer_size
)
2813 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2814 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2815 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2819 std::rotate(__first
, __middle
, __last
);
2820 std::advance(__first
, std::distance(__middle
, __last
));
2825 /// This is a helper function for the merge routines.
2826 template<typename _BidirectionalIterator
, typename _Distance
,
2829 __merge_adaptive(_BidirectionalIterator __first
,
2830 _BidirectionalIterator __middle
,
2831 _BidirectionalIterator __last
,
2832 _Distance __len1
, _Distance __len2
,
2833 _Pointer __buffer
, _Distance __buffer_size
)
2835 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2837 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2838 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
2839 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
2840 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
2841 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
2844 else if (__len2
<= __buffer_size
)
2846 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2847 std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
2848 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
2849 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
2850 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
2855 _BidirectionalIterator __first_cut
= __first
;
2856 _BidirectionalIterator __second_cut
= __middle
;
2857 _Distance __len11
= 0;
2858 _Distance __len22
= 0;
2859 if (__len1
> __len2
)
2861 __len11
= __len1
/ 2;
2862 std::advance(__first_cut
, __len11
);
2863 __second_cut
= std::lower_bound(__middle
, __last
,
2865 __len22
= std::distance(__middle
, __second_cut
);
2869 __len22
= __len2
/ 2;
2870 std::advance(__second_cut
, __len22
);
2871 __first_cut
= std::upper_bound(__first
, __middle
,
2873 __len11
= std::distance(__first
, __first_cut
);
2875 _BidirectionalIterator __new_middle
=
2876 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2877 __len1
- __len11
, __len22
, __buffer
,
2879 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2880 __len22
, __buffer
, __buffer_size
);
2881 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2883 __len2
- __len22
, __buffer
, __buffer_size
);
2887 /// This is a helper function for the merge routines.
2888 template<typename _BidirectionalIterator
, typename _Distance
,
2889 typename _Pointer
, typename _Compare
>
2891 __merge_adaptive(_BidirectionalIterator __first
,
2892 _BidirectionalIterator __middle
,
2893 _BidirectionalIterator __last
,
2894 _Distance __len1
, _Distance __len2
,
2895 _Pointer __buffer
, _Distance __buffer_size
,
2898 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2900 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2901 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
2902 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
2903 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
2904 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
2907 else if (__len2
<= __buffer_size
)
2909 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2910 std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
2911 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
2912 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
2913 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
2918 _BidirectionalIterator __first_cut
= __first
;
2919 _BidirectionalIterator __second_cut
= __middle
;
2920 _Distance __len11
= 0;
2921 _Distance __len22
= 0;
2922 if (__len1
> __len2
)
2924 __len11
= __len1
/ 2;
2925 std::advance(__first_cut
, __len11
);
2926 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2928 __len22
= std::distance(__middle
, __second_cut
);
2932 __len22
= __len2
/ 2;
2933 std::advance(__second_cut
, __len22
);
2934 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2936 __len11
= std::distance(__first
, __first_cut
);
2938 _BidirectionalIterator __new_middle
=
2939 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2940 __len1
- __len11
, __len22
, __buffer
,
2942 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2943 __len22
, __buffer
, __buffer_size
, __comp
);
2944 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2946 __len2
- __len22
, __buffer
,
2947 __buffer_size
, __comp
);
2951 /// This is a helper function for the merge routines.
2952 template<typename _BidirectionalIterator
, typename _Distance
>
2954 __merge_without_buffer(_BidirectionalIterator __first
,
2955 _BidirectionalIterator __middle
,
2956 _BidirectionalIterator __last
,
2957 _Distance __len1
, _Distance __len2
)
2959 if (__len1
== 0 || __len2
== 0)
2961 if (__len1
+ __len2
== 2)
2963 if (*__middle
< *__first
)
2964 std::iter_swap(__first
, __middle
);
2967 _BidirectionalIterator __first_cut
= __first
;
2968 _BidirectionalIterator __second_cut
= __middle
;
2969 _Distance __len11
= 0;
2970 _Distance __len22
= 0;
2971 if (__len1
> __len2
)
2973 __len11
= __len1
/ 2;
2974 std::advance(__first_cut
, __len11
);
2975 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2976 __len22
= std::distance(__middle
, __second_cut
);
2980 __len22
= __len2
/ 2;
2981 std::advance(__second_cut
, __len22
);
2982 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2983 __len11
= std::distance(__first
, __first_cut
);
2985 std::rotate(__first_cut
, __middle
, __second_cut
);
2986 _BidirectionalIterator __new_middle
= __first_cut
;
2987 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2988 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2990 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2991 __len1
- __len11
, __len2
- __len22
);
2994 /// This is a helper function for the merge routines.
2995 template<typename _BidirectionalIterator
, typename _Distance
,
2998 __merge_without_buffer(_BidirectionalIterator __first
,
2999 _BidirectionalIterator __middle
,
3000 _BidirectionalIterator __last
,
3001 _Distance __len1
, _Distance __len2
,
3004 if (__len1
== 0 || __len2
== 0)
3006 if (__len1
+ __len2
== 2)
3008 if (__comp(*__middle
, *__first
))
3009 std::iter_swap(__first
, __middle
);
3012 _BidirectionalIterator __first_cut
= __first
;
3013 _BidirectionalIterator __second_cut
= __middle
;
3014 _Distance __len11
= 0;
3015 _Distance __len22
= 0;
3016 if (__len1
> __len2
)
3018 __len11
= __len1
/ 2;
3019 std::advance(__first_cut
, __len11
);
3020 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3022 __len22
= std::distance(__middle
, __second_cut
);
3026 __len22
= __len2
/ 2;
3027 std::advance(__second_cut
, __len22
);
3028 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3030 __len11
= std::distance(__first
, __first_cut
);
3032 std::rotate(__first_cut
, __middle
, __second_cut
);
3033 _BidirectionalIterator __new_middle
= __first_cut
;
3034 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3035 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3036 __len11
, __len22
, __comp
);
3037 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3038 __len1
- __len11
, __len2
- __len22
, __comp
);
3042 * @brief Merges two sorted ranges in place.
3043 * @ingroup sorting_algorithms
3044 * @param first An iterator.
3045 * @param middle Another iterator.
3046 * @param last Another iterator.
3049 * Merges two sorted and consecutive ranges, [first,middle) and
3050 * [middle,last), and puts the result in [first,last). The output will
3051 * be sorted. The sort is @e stable, that is, for equivalent
3052 * elements in the two ranges, elements from the first range will always
3053 * come before elements from the second.
3055 * If enough additional memory is available, this takes (last-first)-1
3056 * comparisons. Otherwise an NlogN algorithm is used, where N is
3057 * distance(first,last).
3059 template<typename _BidirectionalIterator
>
3061 inplace_merge(_BidirectionalIterator __first
,
3062 _BidirectionalIterator __middle
,
3063 _BidirectionalIterator __last
)
3065 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3067 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3070 // concept requirements
3071 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3072 _BidirectionalIterator
>)
3073 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3074 __glibcxx_requires_sorted(__first
, __middle
);
3075 __glibcxx_requires_sorted(__middle
, __last
);
3077 if (__first
== __middle
|| __middle
== __last
)
3080 _DistanceType __len1
= std::distance(__first
, __middle
);
3081 _DistanceType __len2
= std::distance(__middle
, __last
);
3083 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3085 if (__buf
.begin() == 0)
3086 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3088 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3089 __buf
.begin(), _DistanceType(__buf
.size()));
3093 * @brief Merges two sorted ranges in place.
3094 * @ingroup sorting_algorithms
3095 * @param first An iterator.
3096 * @param middle Another iterator.
3097 * @param last Another iterator.
3098 * @param comp A functor to use for comparisons.
3101 * Merges two sorted and consecutive ranges, [first,middle) and
3102 * [middle,last), and puts the result in [first,last). The output will
3103 * be sorted. The sort is @e stable, that is, for equivalent
3104 * elements in the two ranges, elements from the first range will always
3105 * come before elements from the second.
3107 * If enough additional memory is available, this takes (last-first)-1
3108 * comparisons. Otherwise an NlogN algorithm is used, where N is
3109 * distance(first,last).
3111 * The comparison function should have the same effects on ordering as
3112 * the function used for the initial sort.
3114 template<typename _BidirectionalIterator
, typename _Compare
>
3116 inplace_merge(_BidirectionalIterator __first
,
3117 _BidirectionalIterator __middle
,
3118 _BidirectionalIterator __last
,
3121 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3123 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3126 // concept requirements
3127 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3128 _BidirectionalIterator
>)
3129 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3130 _ValueType
, _ValueType
>)
3131 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3132 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3134 if (__first
== __middle
|| __middle
== __last
)
3137 const _DistanceType __len1
= std::distance(__first
, __middle
);
3138 const _DistanceType __len2
= std::distance(__middle
, __last
);
3140 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3142 if (__buf
.begin() == 0)
3143 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3146 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3147 __buf
.begin(), _DistanceType(__buf
.size()),
3151 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3154 __merge_sort_loop(_RandomAccessIterator1 __first
,
3155 _RandomAccessIterator1 __last
,
3156 _RandomAccessIterator2 __result
,
3157 _Distance __step_size
)
3159 const _Distance __two_step
= 2 * __step_size
;
3161 while (__last
- __first
>= __two_step
)
3163 __result
= _GLIBCXX_STD_P::merge(
3164 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3165 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3166 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3167 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __two_step
),
3169 __first
+= __two_step
;
3172 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3173 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3174 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3176 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3178 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
3182 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3183 typename _Distance
, typename _Compare
>
3185 __merge_sort_loop(_RandomAccessIterator1 __first
,
3186 _RandomAccessIterator1 __last
,
3187 _RandomAccessIterator2 __result
, _Distance __step_size
,
3190 const _Distance __two_step
= 2 * __step_size
;
3192 while (__last
- __first
>= __two_step
)
3194 __result
= _GLIBCXX_STD_P::merge(
3195 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3196 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3197 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3198 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __two_step
),
3200 __first
+= __two_step
;
3202 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3204 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3205 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3207 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3209 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
3213 template<typename _RandomAccessIterator
, typename _Distance
>
3215 __chunk_insertion_sort(_RandomAccessIterator __first
,
3216 _RandomAccessIterator __last
,
3217 _Distance __chunk_size
)
3219 while (__last
- __first
>= __chunk_size
)
3221 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3222 __first
+= __chunk_size
;
3224 std::__insertion_sort(__first
, __last
);
3227 template<typename _RandomAccessIterator
, typename _Distance
,
3230 __chunk_insertion_sort(_RandomAccessIterator __first
,
3231 _RandomAccessIterator __last
,
3232 _Distance __chunk_size
, _Compare __comp
)
3234 while (__last
- __first
>= __chunk_size
)
3236 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3237 __first
+= __chunk_size
;
3239 std::__insertion_sort(__first
, __last
, __comp
);
3242 enum { _S_chunk_size
= 7 };
3244 template<typename _RandomAccessIterator
, typename _Pointer
>
3246 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3247 _RandomAccessIterator __last
,
3250 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3253 const _Distance __len
= __last
- __first
;
3254 const _Pointer __buffer_last
= __buffer
+ __len
;
3256 _Distance __step_size
= _S_chunk_size
;
3257 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3259 while (__step_size
< __len
)
3261 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3263 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3268 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3270 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3271 _RandomAccessIterator __last
,
3272 _Pointer __buffer
, _Compare __comp
)
3274 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3277 const _Distance __len
= __last
- __first
;
3278 const _Pointer __buffer_last
= __buffer
+ __len
;
3280 _Distance __step_size
= _S_chunk_size
;
3281 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3283 while (__step_size
< __len
)
3285 std::__merge_sort_loop(__first
, __last
, __buffer
,
3286 __step_size
, __comp
);
3288 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3289 __step_size
, __comp
);
3294 template<typename _RandomAccessIterator
, typename _Pointer
,
3297 __stable_sort_adaptive(_RandomAccessIterator __first
,
3298 _RandomAccessIterator __last
,
3299 _Pointer __buffer
, _Distance __buffer_size
)
3301 const _Distance __len
= (__last
- __first
+ 1) / 2;
3302 const _RandomAccessIterator __middle
= __first
+ __len
;
3303 if (__len
> __buffer_size
)
3305 std::__stable_sort_adaptive(__first
, __middle
,
3306 __buffer
, __buffer_size
);
3307 std::__stable_sort_adaptive(__middle
, __last
,
3308 __buffer
, __buffer_size
);
3312 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3313 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3315 std::__merge_adaptive(__first
, __middle
, __last
,
3316 _Distance(__middle
- __first
),
3317 _Distance(__last
- __middle
),
3318 __buffer
, __buffer_size
);
3321 template<typename _RandomAccessIterator
, typename _Pointer
,
3322 typename _Distance
, typename _Compare
>
3324 __stable_sort_adaptive(_RandomAccessIterator __first
,
3325 _RandomAccessIterator __last
,
3326 _Pointer __buffer
, _Distance __buffer_size
,
3329 const _Distance __len
= (__last
- __first
+ 1) / 2;
3330 const _RandomAccessIterator __middle
= __first
+ __len
;
3331 if (__len
> __buffer_size
)
3333 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3334 __buffer_size
, __comp
);
3335 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3336 __buffer_size
, __comp
);
3340 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3341 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3343 std::__merge_adaptive(__first
, __middle
, __last
,
3344 _Distance(__middle
- __first
),
3345 _Distance(__last
- __middle
),
3346 __buffer
, __buffer_size
,
3350 /// This is a helper function for the stable sorting routines.
3351 template<typename _RandomAccessIterator
>
3353 __inplace_stable_sort(_RandomAccessIterator __first
,
3354 _RandomAccessIterator __last
)
3356 if (__last
- __first
< 15)
3358 std::__insertion_sort(__first
, __last
);
3361 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3362 std::__inplace_stable_sort(__first
, __middle
);
3363 std::__inplace_stable_sort(__middle
, __last
);
3364 std::__merge_without_buffer(__first
, __middle
, __last
,
3369 /// This is a helper function for the stable sorting routines.
3370 template<typename _RandomAccessIterator
, typename _Compare
>
3372 __inplace_stable_sort(_RandomAccessIterator __first
,
3373 _RandomAccessIterator __last
, _Compare __comp
)
3375 if (__last
- __first
< 15)
3377 std::__insertion_sort(__first
, __last
, __comp
);
3380 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3381 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3382 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3383 std::__merge_without_buffer(__first
, __middle
, __last
,
3391 // Set algorithms: includes, set_union, set_intersection, set_difference,
3392 // set_symmetric_difference. All of these algorithms have the precondition
3393 // that their input ranges are sorted and the postcondition that their output
3394 // ranges are sorted.
3397 * @brief Determines whether all elements of a sequence exists in a range.
3398 * @param first1 Start of search range.
3399 * @param last1 End of search range.
3400 * @param first2 Start of sequence
3401 * @param last2 End of sequence.
3402 * @return True if each element in [first2,last2) is contained in order
3403 * within [first1,last1). False otherwise.
3404 * @ingroup set_algorithms
3406 * This operation expects both [first1,last1) and [first2,last2) to be
3407 * sorted. Searches for the presence of each element in [first2,last2)
3408 * within [first1,last1). The iterators over each range only move forward,
3409 * so this is a linear algorithm. If an element in [first2,last2) is not
3410 * found before the search iterator reaches @a last2, false is returned.
3412 template<typename _InputIterator1
, typename _InputIterator2
>
3414 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3415 _InputIterator2 __first2
, _InputIterator2 __last2
)
3417 typedef typename iterator_traits
<_InputIterator1
>::value_type
3419 typedef typename iterator_traits
<_InputIterator2
>::value_type
3422 // concept requirements
3423 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3424 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3425 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3426 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3427 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3428 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3430 while (__first1
!= __last1
&& __first2
!= __last2
)
3431 if (*__first2
< *__first1
)
3433 else if(*__first1
< *__first2
)
3436 ++__first1
, ++__first2
;
3438 return __first2
== __last2
;
3442 * @brief Determines whether all elements of a sequence exists in a range
3444 * @ingroup set_algorithms
3445 * @param first1 Start of search range.
3446 * @param last1 End of search range.
3447 * @param first2 Start of sequence
3448 * @param last2 End of sequence.
3449 * @param comp Comparison function to use.
3450 * @return True if each element in [first2,last2) is contained in order
3451 * within [first1,last1) according to comp. False otherwise.
3452 * @ingroup set_algorithms
3454 * This operation expects both [first1,last1) and [first2,last2) to be
3455 * sorted. Searches for the presence of each element in [first2,last2)
3456 * within [first1,last1), using comp to decide. The iterators over each
3457 * range only move forward, so this is a linear algorithm. If an element
3458 * in [first2,last2) is not found before the search iterator reaches @a
3459 * last2, false is returned.
3461 template<typename _InputIterator1
, typename _InputIterator2
,
3464 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3465 _InputIterator2 __first2
, _InputIterator2 __last2
,
3468 typedef typename iterator_traits
<_InputIterator1
>::value_type
3470 typedef typename iterator_traits
<_InputIterator2
>::value_type
3473 // concept requirements
3474 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3475 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3476 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3477 _ValueType1
, _ValueType2
>)
3478 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3479 _ValueType2
, _ValueType1
>)
3480 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3481 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3483 while (__first1
!= __last1
&& __first2
!= __last2
)
3484 if (__comp(*__first2
, *__first1
))
3486 else if(__comp(*__first1
, *__first2
))
3489 ++__first1
, ++__first2
;
3491 return __first2
== __last2
;
3500 // set_symmetric_difference
3505 * @brief Permute range into the next @a dictionary ordering.
3506 * @ingroup sorting_algorithms
3507 * @param first Start of range.
3508 * @param last End of range.
3509 * @return False if wrapped to first permutation, true otherwise.
3511 * Treats all permutations of the range as a set of @a dictionary sorted
3512 * sequences. Permutes the current sequence into the next one of this set.
3513 * Returns true if there are more sequences to generate. If the sequence
3514 * is the largest of the set, the smallest is generated and false returned.
3516 template<typename _BidirectionalIterator
>
3518 next_permutation(_BidirectionalIterator __first
,
3519 _BidirectionalIterator __last
)
3521 // concept requirements
3522 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3523 _BidirectionalIterator
>)
3524 __glibcxx_function_requires(_LessThanComparableConcept
<
3525 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3526 __glibcxx_requires_valid_range(__first
, __last
);
3528 if (__first
== __last
)
3530 _BidirectionalIterator __i
= __first
;
3539 _BidirectionalIterator __ii
= __i
;
3543 _BidirectionalIterator __j
= __last
;
3544 while (!(*__i
< *--__j
))
3546 std::iter_swap(__i
, __j
);
3547 std::reverse(__ii
, __last
);
3552 std::reverse(__first
, __last
);
3559 * @brief Permute range into the next @a dictionary ordering using
3560 * comparison functor.
3561 * @ingroup sorting_algorithms
3562 * @param first Start of range.
3563 * @param last End of range.
3564 * @param comp A comparison functor.
3565 * @return False if wrapped to first permutation, true otherwise.
3567 * Treats all permutations of the range [first,last) as a set of
3568 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3569 * sequence into the next one of this set. Returns true if there are more
3570 * sequences to generate. If the sequence is the largest of the set, the
3571 * smallest is generated and false returned.
3573 template<typename _BidirectionalIterator
, typename _Compare
>
3575 next_permutation(_BidirectionalIterator __first
,
3576 _BidirectionalIterator __last
, _Compare __comp
)
3578 // concept requirements
3579 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3580 _BidirectionalIterator
>)
3581 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3582 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3583 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3584 __glibcxx_requires_valid_range(__first
, __last
);
3586 if (__first
== __last
)
3588 _BidirectionalIterator __i
= __first
;
3597 _BidirectionalIterator __ii
= __i
;
3599 if (__comp(*__i
, *__ii
))
3601 _BidirectionalIterator __j
= __last
;
3602 while (!bool(__comp(*__i
, *--__j
)))
3604 std::iter_swap(__i
, __j
);
3605 std::reverse(__ii
, __last
);
3610 std::reverse(__first
, __last
);
3617 * @brief Permute range into the previous @a dictionary ordering.
3618 * @ingroup sorting_algorithms
3619 * @param first Start of range.
3620 * @param last End of range.
3621 * @return False if wrapped to last permutation, true otherwise.
3623 * Treats all permutations of the range as a set of @a dictionary sorted
3624 * sequences. Permutes the current sequence into the previous one of this
3625 * set. Returns true if there are more sequences to generate. If the
3626 * sequence is the smallest of the set, the largest is generated and false
3629 template<typename _BidirectionalIterator
>
3631 prev_permutation(_BidirectionalIterator __first
,
3632 _BidirectionalIterator __last
)
3634 // concept requirements
3635 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3636 _BidirectionalIterator
>)
3637 __glibcxx_function_requires(_LessThanComparableConcept
<
3638 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3639 __glibcxx_requires_valid_range(__first
, __last
);
3641 if (__first
== __last
)
3643 _BidirectionalIterator __i
= __first
;
3652 _BidirectionalIterator __ii
= __i
;
3656 _BidirectionalIterator __j
= __last
;
3657 while (!(*--__j
< *__i
))
3659 std::iter_swap(__i
, __j
);
3660 std::reverse(__ii
, __last
);
3665 std::reverse(__first
, __last
);
3672 * @brief Permute range into the previous @a dictionary ordering using
3673 * comparison functor.
3674 * @ingroup sorting_algorithms
3675 * @param first Start of range.
3676 * @param last End of range.
3677 * @param comp A comparison functor.
3678 * @return False if wrapped to last permutation, true otherwise.
3680 * Treats all permutations of the range [first,last) as a set of
3681 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3682 * sequence into the previous one of this set. Returns true if there are
3683 * more sequences to generate. If the sequence is the smallest of the set,
3684 * the largest is generated and false returned.
3686 template<typename _BidirectionalIterator
, typename _Compare
>
3688 prev_permutation(_BidirectionalIterator __first
,
3689 _BidirectionalIterator __last
, _Compare __comp
)
3691 // concept requirements
3692 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3693 _BidirectionalIterator
>)
3694 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3695 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3696 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3697 __glibcxx_requires_valid_range(__first
, __last
);
3699 if (__first
== __last
)
3701 _BidirectionalIterator __i
= __first
;
3710 _BidirectionalIterator __ii
= __i
;
3712 if (__comp(*__ii
, *__i
))
3714 _BidirectionalIterator __j
= __last
;
3715 while (!bool(__comp(*--__j
, *__i
)))
3717 std::iter_swap(__i
, __j
);
3718 std::reverse(__ii
, __last
);
3723 std::reverse(__first
, __last
);
3733 * @brief Copy a sequence, replacing each element of one value with another
3735 * @param first An input iterator.
3736 * @param last An input iterator.
3737 * @param result An output iterator.
3738 * @param old_value The value to be replaced.
3739 * @param new_value The replacement value.
3740 * @return The end of the output sequence, @p result+(last-first).
3742 * Copies each element in the input range @p [first,last) to the
3743 * output range @p [result,result+(last-first)) replacing elements
3744 * equal to @p old_value with @p new_value.
3746 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3748 replace_copy(_InputIterator __first
, _InputIterator __last
,
3749 _OutputIterator __result
,
3750 const _Tp
& __old_value
, const _Tp
& __new_value
)
3752 // concept requirements
3753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3754 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3755 typename iterator_traits
<_InputIterator
>::value_type
>)
3756 __glibcxx_function_requires(_EqualOpConcept
<
3757 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3758 __glibcxx_requires_valid_range(__first
, __last
);
3760 for (; __first
!= __last
; ++__first
, ++__result
)
3761 if (*__first
== __old_value
)
3762 *__result
= __new_value
;
3764 *__result
= *__first
;
3769 * @brief Copy a sequence, replacing each value for which a predicate
3770 * returns true with another value.
3771 * @ingroup mutating_algorithms
3772 * @param first An input iterator.
3773 * @param last An input iterator.
3774 * @param result An output iterator.
3775 * @param pred A predicate.
3776 * @param new_value The replacement value.
3777 * @return The end of the output sequence, @p result+(last-first).
3779 * Copies each element in the range @p [first,last) to the range
3780 * @p [result,result+(last-first)) replacing elements for which
3781 * @p pred returns true with @p new_value.
3783 template<typename _InputIterator
, typename _OutputIterator
,
3784 typename _Predicate
, typename _Tp
>
3786 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3787 _OutputIterator __result
,
3788 _Predicate __pred
, const _Tp
& __new_value
)
3790 // concept requirements
3791 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3792 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3793 typename iterator_traits
<_InputIterator
>::value_type
>)
3794 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3795 typename iterator_traits
<_InputIterator
>::value_type
>)
3796 __glibcxx_requires_valid_range(__first
, __last
);
3798 for (; __first
!= __last
; ++__first
, ++__result
)
3799 if (__pred(*__first
))
3800 *__result
= __new_value
;
3802 *__result
= *__first
;
3806 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3808 * @brief Determines whether the elements of a sequence are sorted.
3809 * @ingroup sorting_algorithms
3810 * @param first An iterator.
3811 * @param last Another iterator.
3812 * @return True if the elements are sorted, false otherwise.
3814 template<typename _ForwardIterator
>
3816 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3817 { return std::is_sorted_until(__first
, __last
) == __last
; }
3820 * @brief Determines whether the elements of a sequence are sorted
3821 * according to a comparison functor.
3822 * @ingroup sorting_algorithms
3823 * @param first An iterator.
3824 * @param last Another iterator.
3825 * @param comp A comparison functor.
3826 * @return True if the elements are sorted, false otherwise.
3828 template<typename _ForwardIterator
, typename _Compare
>
3830 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3832 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3835 * @brief Determines the end of a sorted sequence.
3836 * @ingroup sorting_algorithms
3837 * @param first An iterator.
3838 * @param last Another iterator.
3839 * @return An iterator pointing to the last iterator i in [first, last)
3840 * for which the range [first, i) is sorted.
3842 template<typename _ForwardIterator
>
3844 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3846 // concept requirements
3847 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3848 __glibcxx_function_requires(_LessThanComparableConcept
<
3849 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3850 __glibcxx_requires_valid_range(__first
, __last
);
3852 if (__first
== __last
)
3855 _ForwardIterator __next
= __first
;
3856 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3857 if (*__next
< *__first
)
3863 * @brief Determines the end of a sorted sequence using comparison functor.
3864 * @ingroup sorting_algorithms
3865 * @param first An iterator.
3866 * @param last Another iterator.
3867 * @param comp A comparison functor.
3868 * @return An iterator pointing to the last iterator i in [first, last)
3869 * for which the range [first, i) is sorted.
3871 template<typename _ForwardIterator
, typename _Compare
>
3873 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3876 // concept requirements
3877 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3878 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3879 typename iterator_traits
<_ForwardIterator
>::value_type
,
3880 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3881 __glibcxx_requires_valid_range(__first
, __last
);
3883 if (__first
== __last
)
3886 _ForwardIterator __next
= __first
;
3887 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3888 if (__comp(*__next
, *__first
))
3894 * @brief Determines min and max at once as an ordered pair.
3895 * @ingroup sorting_algorithms
3896 * @param a A thing of arbitrary type.
3897 * @param b Another thing of arbitrary type.
3898 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3900 template<typename _Tp
>
3901 inline pair
<const _Tp
&, const _Tp
&>
3902 minmax(const _Tp
& __a
, const _Tp
& __b
)
3904 // concept requirements
3905 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3907 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3908 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3912 * @brief Determines min and max at once as an ordered pair.
3913 * @ingroup sorting_algorithms
3914 * @param a A thing of arbitrary type.
3915 * @param b Another thing of arbitrary type.
3916 * @param comp A @link comparison_functor comparison functor@endlink.
3917 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3919 template<typename _Tp
, typename _Compare
>
3920 inline pair
<const _Tp
&, const _Tp
&>
3921 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3923 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3924 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3928 * @brief Return a pair of iterators pointing to the minimum and maximum
3929 * elements in a range.
3930 * @ingroup sorting_algorithms
3931 * @param first Start of range.
3932 * @param last End of range.
3933 * @return make_pair(m, M), where m is the first iterator i in
3934 * [first, last) such that no other element in the range is
3935 * smaller, and where M is the last iterator i in [first, last)
3936 * such that no other element in the range is larger.
3938 template<typename _ForwardIterator
>
3939 pair
<_ForwardIterator
, _ForwardIterator
>
3940 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3942 // concept requirements
3943 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3944 __glibcxx_function_requires(_LessThanComparableConcept
<
3945 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3946 __glibcxx_requires_valid_range(__first
, __last
);
3948 _ForwardIterator __next
= __first
;
3949 if (__first
== __last
3950 || ++__next
== __last
)
3951 return std::make_pair(__first
, __first
);
3953 _ForwardIterator __min
, __max
;
3954 if (*__next
< *__first
)
3968 while (__first
!= __last
)
3971 if (++__next
== __last
)
3973 if (*__first
< *__min
)
3975 else if (!(*__first
< *__max
))
3980 if (*__next
< *__first
)
3982 if (*__next
< *__min
)
3984 if (!(*__first
< *__max
))
3989 if (*__first
< *__min
)
3991 if (!(*__next
< *__max
))
3999 return std::make_pair(__min
, __max
);
4003 * @brief Return a pair of iterators pointing to the minimum and maximum
4004 * elements in a range.
4005 * @ingroup sorting_algorithms
4006 * @param first Start of range.
4007 * @param last End of range.
4008 * @param comp Comparison functor.
4009 * @return make_pair(m, M), where m is the first iterator i in
4010 * [first, last) such that no other element in the range is
4011 * smaller, and where M is the last iterator i in [first, last)
4012 * such that no other element in the range is larger.
4014 template<typename _ForwardIterator
, typename _Compare
>
4015 pair
<_ForwardIterator
, _ForwardIterator
>
4016 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4019 // concept requirements
4020 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4021 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4022 typename iterator_traits
<_ForwardIterator
>::value_type
,
4023 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4024 __glibcxx_requires_valid_range(__first
, __last
);
4026 _ForwardIterator __next
= __first
;
4027 if (__first
== __last
4028 || ++__next
== __last
)
4029 return std::make_pair(__first
, __first
);
4031 _ForwardIterator __min
, __max
;
4032 if (__comp(*__next
, *__first
))
4046 while (__first
!= __last
)
4049 if (++__next
== __last
)
4051 if (__comp(*__first
, *__min
))
4053 else if (!__comp(*__first
, *__max
))
4058 if (__comp(*__next
, *__first
))
4060 if (__comp(*__next
, *__min
))
4062 if (!__comp(*__first
, *__max
))
4067 if (__comp(*__first
, *__min
))
4069 if (!__comp(*__next
, *__max
))
4077 return std::make_pair(__min
, __max
);
4081 template<typename _Tp
>
4083 min(initializer_list
<_Tp
> __l
)
4084 { return *std::min_element(__l
.begin(), __l
.end()); }
4086 template<typename _Tp
, typename _Compare
>
4088 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4089 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4091 template<typename _Tp
>
4093 max(initializer_list
<_Tp
> __l
)
4094 { return *std::max_element(__l
.begin(), __l
.end()); }
4096 template<typename _Tp
, typename _Compare
>
4098 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4099 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4101 template<typename _Tp
>
4102 inline pair
<_Tp
, _Tp
>
4103 minmax(initializer_list
<_Tp
> __l
)
4105 pair
<const _Tp
*, const _Tp
*> __p
=
4106 std::minmax_element(__l
.begin(), __l
.end());
4107 return std::make_pair(*__p
.first
, *__p
.second
);
4110 template<typename _Tp
, typename _Compare
>
4111 inline pair
<_Tp
, _Tp
>
4112 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4114 pair
<const _Tp
*, const _Tp
*> __p
=
4115 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4116 return std::make_pair(*__p
.first
, *__p
.second
);
4119 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4121 * @brief Shuffle the elements of a sequence using a uniform random
4123 * @ingroup mutating_algorithms
4124 * @param first A forward iterator.
4125 * @param last A forward iterator.
4126 * @param g A UniformRandomNumberGenerator (26.5.1.3).
4129 * Reorders the elements in the range @p [first,last) using @p g to
4130 * provide random numbers.
4132 template<typename _RandomAccessIterator
,
4133 typename _UniformRandomNumberGenerator
>
4135 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4136 _UniformRandomNumberGenerator
&& __g
)
4138 // concept requirements
4139 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4140 _RandomAccessIterator
>)
4141 __glibcxx_requires_valid_range(__first
, __last
);
4143 if (__first
== __last
)
4146 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4149 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
4150 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
4151 typedef typename
__distr_type::param_type __p_type
;
4154 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4155 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
4159 #endif // __GXX_EXPERIMENTAL_CXX0X__
4161 _GLIBCXX_END_NAMESPACE
4163 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4166 * @brief Apply a function to every element of a sequence.
4167 * @ingroup non_mutating_algorithms
4168 * @param first An input iterator.
4169 * @param last An input iterator.
4170 * @param f A unary function object.
4171 * @return @p f (std::move(@p f) in C++0x).
4173 * Applies the function object @p f to each element in the range
4174 * @p [first,last). @p f must not modify the order of the sequence.
4175 * If @p f has a return value it is ignored.
4177 template<typename _InputIterator
, typename _Function
>
4179 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4181 // concept requirements
4182 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4183 __glibcxx_requires_valid_range(__first
, __last
);
4184 for (; __first
!= __last
; ++__first
)
4186 return _GLIBCXX_MOVE(__f
);
4190 * @brief Find the first occurrence of a value in a sequence.
4191 * @ingroup non_mutating_algorithms
4192 * @param first An input iterator.
4193 * @param last An input iterator.
4194 * @param val The value to find.
4195 * @return The first iterator @c i in the range @p [first,last)
4196 * such that @c *i == @p val, or @p last if no such iterator exists.
4198 template<typename _InputIterator
, typename _Tp
>
4199 inline _InputIterator
4200 find(_InputIterator __first
, _InputIterator __last
,
4203 // concept requirements
4204 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4205 __glibcxx_function_requires(_EqualOpConcept
<
4206 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4207 __glibcxx_requires_valid_range(__first
, __last
);
4208 return std::__find(__first
, __last
, __val
,
4209 std::__iterator_category(__first
));
4213 * @brief Find the first element in a sequence for which a
4214 * predicate is true.
4215 * @ingroup non_mutating_algorithms
4216 * @param first An input iterator.
4217 * @param last An input iterator.
4218 * @param pred A predicate.
4219 * @return The first iterator @c i in the range @p [first,last)
4220 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4222 template<typename _InputIterator
, typename _Predicate
>
4223 inline _InputIterator
4224 find_if(_InputIterator __first
, _InputIterator __last
,
4227 // concept requirements
4228 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4229 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4230 typename iterator_traits
<_InputIterator
>::value_type
>)
4231 __glibcxx_requires_valid_range(__first
, __last
);
4232 return std::__find_if(__first
, __last
, __pred
,
4233 std::__iterator_category(__first
));
4237 * @brief Find element from a set in a sequence.
4238 * @ingroup non_mutating_algorithms
4239 * @param first1 Start of range to search.
4240 * @param last1 End of range to search.
4241 * @param first2 Start of match candidates.
4242 * @param last2 End of match candidates.
4243 * @return The first iterator @c i in the range
4244 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4245 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4247 * Searches the range @p [first1,last1) for an element that is equal to
4248 * some element in the range [first2,last2). If found, returns an iterator
4249 * in the range [first1,last1), otherwise returns @p last1.
4251 template<typename _InputIterator
, typename _ForwardIterator
>
4253 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4254 _ForwardIterator __first2
, _ForwardIterator __last2
)
4256 // concept requirements
4257 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4258 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4259 __glibcxx_function_requires(_EqualOpConcept
<
4260 typename iterator_traits
<_InputIterator
>::value_type
,
4261 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4262 __glibcxx_requires_valid_range(__first1
, __last1
);
4263 __glibcxx_requires_valid_range(__first2
, __last2
);
4265 for (; __first1
!= __last1
; ++__first1
)
4266 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4267 if (*__first1
== *__iter
)
4273 * @brief Find element from a set in a sequence using a predicate.
4274 * @ingroup non_mutating_algorithms
4275 * @param first1 Start of range to search.
4276 * @param last1 End of range to search.
4277 * @param first2 Start of match candidates.
4278 * @param last2 End of match candidates.
4279 * @param comp Predicate to use.
4280 * @return The first iterator @c i in the range
4281 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4282 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4285 * Searches the range @p [first1,last1) for an element that is
4286 * equal to some element in the range [first2,last2). If found,
4287 * returns an iterator in the range [first1,last1), otherwise
4290 template<typename _InputIterator
, typename _ForwardIterator
,
4291 typename _BinaryPredicate
>
4293 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4294 _ForwardIterator __first2
, _ForwardIterator __last2
,
4295 _BinaryPredicate __comp
)
4297 // concept requirements
4298 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4299 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4300 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4301 typename iterator_traits
<_InputIterator
>::value_type
,
4302 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4303 __glibcxx_requires_valid_range(__first1
, __last1
);
4304 __glibcxx_requires_valid_range(__first2
, __last2
);
4306 for (; __first1
!= __last1
; ++__first1
)
4307 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4308 if (__comp(*__first1
, *__iter
))
4314 * @brief Find two adjacent values in a sequence that are equal.
4315 * @ingroup non_mutating_algorithms
4316 * @param first A forward iterator.
4317 * @param last A forward iterator.
4318 * @return The first iterator @c i such that @c i and @c i+1 are both
4319 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4320 * or @p last if no such iterator exists.
4322 template<typename _ForwardIterator
>
4324 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4326 // concept requirements
4327 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4328 __glibcxx_function_requires(_EqualityComparableConcept
<
4329 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4330 __glibcxx_requires_valid_range(__first
, __last
);
4331 if (__first
== __last
)
4333 _ForwardIterator __next
= __first
;
4334 while(++__next
!= __last
)
4336 if (*__first
== *__next
)
4344 * @brief Find two adjacent values in a sequence using a predicate.
4345 * @ingroup non_mutating_algorithms
4346 * @param first A forward iterator.
4347 * @param last A forward iterator.
4348 * @param binary_pred A binary predicate.
4349 * @return The first iterator @c i such that @c i and @c i+1 are both
4350 * valid iterators in @p [first,last) and such that
4351 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4354 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4356 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4357 _BinaryPredicate __binary_pred
)
4359 // concept requirements
4360 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4361 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4362 typename iterator_traits
<_ForwardIterator
>::value_type
,
4363 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4364 __glibcxx_requires_valid_range(__first
, __last
);
4365 if (__first
== __last
)
4367 _ForwardIterator __next
= __first
;
4368 while(++__next
!= __last
)
4370 if (__binary_pred(*__first
, *__next
))
4378 * @brief Count the number of copies of a value in a sequence.
4379 * @ingroup non_mutating_algorithms
4380 * @param first An input iterator.
4381 * @param last An input iterator.
4382 * @param value The value to be counted.
4383 * @return The number of iterators @c i in the range @p [first,last)
4384 * for which @c *i == @p value
4386 template<typename _InputIterator
, typename _Tp
>
4387 typename iterator_traits
<_InputIterator
>::difference_type
4388 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4390 // concept requirements
4391 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4392 __glibcxx_function_requires(_EqualOpConcept
<
4393 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4394 __glibcxx_requires_valid_range(__first
, __last
);
4395 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4396 for (; __first
!= __last
; ++__first
)
4397 if (*__first
== __value
)
4403 * @brief Count the elements of a sequence for which a predicate is true.
4404 * @ingroup non_mutating_algorithms
4405 * @param first An input iterator.
4406 * @param last An input iterator.
4407 * @param pred A predicate.
4408 * @return The number of iterators @c i in the range @p [first,last)
4409 * for which @p pred(*i) is true.
4411 template<typename _InputIterator
, typename _Predicate
>
4412 typename iterator_traits
<_InputIterator
>::difference_type
4413 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4415 // concept requirements
4416 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4417 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4418 typename iterator_traits
<_InputIterator
>::value_type
>)
4419 __glibcxx_requires_valid_range(__first
, __last
);
4420 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4421 for (; __first
!= __last
; ++__first
)
4422 if (__pred(*__first
))
4428 * @brief Search a sequence for a matching sub-sequence.
4429 * @ingroup non_mutating_algorithms
4430 * @param first1 A forward iterator.
4431 * @param last1 A forward iterator.
4432 * @param first2 A forward iterator.
4433 * @param last2 A forward iterator.
4434 * @return The first iterator @c i in the range
4435 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4436 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4437 * such iterator exists.
4439 * Searches the range @p [first1,last1) for a sub-sequence that compares
4440 * equal value-by-value with the sequence given by @p [first2,last2) and
4441 * returns an iterator to the first element of the sub-sequence, or
4442 * @p last1 if the sub-sequence is not found.
4444 * Because the sub-sequence must lie completely within the range
4445 * @p [first1,last1) it must start at a position less than
4446 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4448 * This means that the returned iterator @c i will be in the range
4449 * @p [first1,last1-(last2-first2))
4451 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4453 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4454 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4456 // concept requirements
4457 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4458 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4459 __glibcxx_function_requires(_EqualOpConcept
<
4460 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4461 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4462 __glibcxx_requires_valid_range(__first1
, __last1
);
4463 __glibcxx_requires_valid_range(__first2
, __last2
);
4465 // Test for empty ranges
4466 if (__first1
== __last1
|| __first2
== __last2
)
4469 // Test for a pattern of length 1.
4470 _ForwardIterator2
__p1(__first2
);
4471 if (++__p1
== __last2
)
4472 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4475 _ForwardIterator2 __p
;
4476 _ForwardIterator1 __current
= __first1
;
4480 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4481 if (__first1
== __last1
)
4485 __current
= __first1
;
4486 if (++__current
== __last1
)
4489 while (*__current
== *__p
)
4491 if (++__p
== __last2
)
4493 if (++__current
== __last1
)
4502 * @brief Search a sequence for a matching sub-sequence using a predicate.
4503 * @ingroup non_mutating_algorithms
4504 * @param first1 A forward iterator.
4505 * @param last1 A forward iterator.
4506 * @param first2 A forward iterator.
4507 * @param last2 A forward iterator.
4508 * @param predicate A binary predicate.
4509 * @return The first iterator @c i in the range
4510 * @p [first1,last1-(last2-first2)) such that
4511 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4512 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4514 * Searches the range @p [first1,last1) for a sub-sequence that compares
4515 * equal value-by-value with the sequence given by @p [first2,last2),
4516 * using @p predicate to determine equality, and returns an iterator
4517 * to the first element of the sub-sequence, or @p last1 if no such
4520 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4522 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4523 typename _BinaryPredicate
>
4525 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4526 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4527 _BinaryPredicate __predicate
)
4529 // concept requirements
4530 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4531 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4532 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4533 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4534 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4535 __glibcxx_requires_valid_range(__first1
, __last1
);
4536 __glibcxx_requires_valid_range(__first2
, __last2
);
4538 // Test for empty ranges
4539 if (__first1
== __last1
|| __first2
== __last2
)
4542 // Test for a pattern of length 1.
4543 _ForwardIterator2
__p1(__first2
);
4544 if (++__p1
== __last2
)
4546 while (__first1
!= __last1
4547 && !bool(__predicate(*__first1
, *__first2
)))
4553 _ForwardIterator2 __p
;
4554 _ForwardIterator1 __current
= __first1
;
4558 while (__first1
!= __last1
4559 && !bool(__predicate(*__first1
, *__first2
)))
4561 if (__first1
== __last1
)
4565 __current
= __first1
;
4566 if (++__current
== __last1
)
4569 while (__predicate(*__current
, *__p
))
4571 if (++__p
== __last2
)
4573 if (++__current
== __last1
)
4583 * @brief Search a sequence for a number of consecutive values.
4584 * @ingroup non_mutating_algorithms
4585 * @param first A forward iterator.
4586 * @param last A forward iterator.
4587 * @param count The number of consecutive values.
4588 * @param val The value to find.
4589 * @return The first iterator @c i in the range @p [first,last-count)
4590 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4591 * or @p last if no such iterator exists.
4593 * Searches the range @p [first,last) for @p count consecutive elements
4596 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4598 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4599 _Integer __count
, const _Tp
& __val
)
4601 // concept requirements
4602 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4603 __glibcxx_function_requires(_EqualOpConcept
<
4604 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4605 __glibcxx_requires_valid_range(__first
, __last
);
4610 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4611 return std::__search_n(__first
, __last
, __count
, __val
,
4612 std::__iterator_category(__first
));
4617 * @brief Search a sequence for a number of consecutive values using a
4619 * @ingroup non_mutating_algorithms
4620 * @param first A forward iterator.
4621 * @param last A forward iterator.
4622 * @param count The number of consecutive values.
4623 * @param val The value to find.
4624 * @param binary_pred A binary predicate.
4625 * @return The first iterator @c i in the range @p [first,last-count)
4626 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4627 * range @p [0,count), or @p last if no such iterator exists.
4629 * Searches the range @p [first,last) for @p count consecutive elements
4630 * for which the predicate returns true.
4632 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4633 typename _BinaryPredicate
>
4635 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4636 _Integer __count
, const _Tp
& __val
,
4637 _BinaryPredicate __binary_pred
)
4639 // concept requirements
4640 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4641 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4642 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4643 __glibcxx_requires_valid_range(__first
, __last
);
4649 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4653 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4654 std::__iterator_category(__first
));
4659 * @brief Perform an operation on a sequence.
4660 * @ingroup mutating_algorithms
4661 * @param first An input iterator.
4662 * @param last An input iterator.
4663 * @param result An output iterator.
4664 * @param unary_op A unary operator.
4665 * @return An output iterator equal to @p result+(last-first).
4667 * Applies the operator to each element in the input range and assigns
4668 * the results to successive elements of the output sequence.
4669 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4670 * range @p [0,last-first).
4672 * @p unary_op must not alter its argument.
4674 template<typename _InputIterator
, typename _OutputIterator
,
4675 typename _UnaryOperation
>
4677 transform(_InputIterator __first
, _InputIterator __last
,
4678 _OutputIterator __result
, _UnaryOperation __unary_op
)
4680 // concept requirements
4681 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4682 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4683 // "the type returned by a _UnaryOperation"
4684 __typeof__(__unary_op(*__first
))>)
4685 __glibcxx_requires_valid_range(__first
, __last
);
4687 for (; __first
!= __last
; ++__first
, ++__result
)
4688 *__result
= __unary_op(*__first
);
4693 * @brief Perform an operation on corresponding elements of two sequences.
4694 * @ingroup mutating_algorithms
4695 * @param first1 An input iterator.
4696 * @param last1 An input iterator.
4697 * @param first2 An input iterator.
4698 * @param result An output iterator.
4699 * @param binary_op A binary operator.
4700 * @return An output iterator equal to @p result+(last-first).
4702 * Applies the operator to the corresponding elements in the two
4703 * input ranges and assigns the results to successive elements of the
4705 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4706 * @c N in the range @p [0,last1-first1).
4708 * @p binary_op must not alter either of its arguments.
4710 template<typename _InputIterator1
, typename _InputIterator2
,
4711 typename _OutputIterator
, typename _BinaryOperation
>
4713 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4714 _InputIterator2 __first2
, _OutputIterator __result
,
4715 _BinaryOperation __binary_op
)
4717 // concept requirements
4718 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4719 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4720 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4721 // "the type returned by a _BinaryOperation"
4722 __typeof__(__binary_op(*__first1
,*__first2
))>)
4723 __glibcxx_requires_valid_range(__first1
, __last1
);
4725 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4726 *__result
= __binary_op(*__first1
, *__first2
);
4731 * @brief Replace each occurrence of one value in a sequence with another
4733 * @ingroup mutating_algorithms
4734 * @param first A forward iterator.
4735 * @param last A forward iterator.
4736 * @param old_value The value to be replaced.
4737 * @param new_value The replacement value.
4738 * @return replace() returns no value.
4740 * For each iterator @c i in the range @p [first,last) if @c *i ==
4741 * @p old_value then the assignment @c *i = @p new_value is performed.
4743 template<typename _ForwardIterator
, typename _Tp
>
4745 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4746 const _Tp
& __old_value
, const _Tp
& __new_value
)
4748 // concept requirements
4749 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4751 __glibcxx_function_requires(_EqualOpConcept
<
4752 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4753 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4754 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4755 __glibcxx_requires_valid_range(__first
, __last
);
4757 for (; __first
!= __last
; ++__first
)
4758 if (*__first
== __old_value
)
4759 *__first
= __new_value
;
4763 * @brief Replace each value in a sequence for which a predicate returns
4764 * true with another value.
4765 * @ingroup mutating_algorithms
4766 * @param first A forward iterator.
4767 * @param last A forward iterator.
4768 * @param pred A predicate.
4769 * @param new_value The replacement value.
4770 * @return replace_if() returns no value.
4772 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4773 * is true then the assignment @c *i = @p new_value is performed.
4775 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4777 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4778 _Predicate __pred
, const _Tp
& __new_value
)
4780 // concept requirements
4781 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4783 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4784 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4785 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4786 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4787 __glibcxx_requires_valid_range(__first
, __last
);
4789 for (; __first
!= __last
; ++__first
)
4790 if (__pred(*__first
))
4791 *__first
= __new_value
;
4795 * @brief Assign the result of a function object to each value in a
4797 * @ingroup mutating_algorithms
4798 * @param first A forward iterator.
4799 * @param last A forward iterator.
4800 * @param gen A function object taking no arguments and returning
4801 * std::iterator_traits<_ForwardIterator>::value_type
4802 * @return generate() returns no value.
4804 * Performs the assignment @c *i = @p gen() for each @c i in the range
4807 template<typename _ForwardIterator
, typename _Generator
>
4809 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4812 // concept requirements
4813 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4814 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4815 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4816 __glibcxx_requires_valid_range(__first
, __last
);
4818 for (; __first
!= __last
; ++__first
)
4823 * @brief Assign the result of a function object to each value in a
4825 * @ingroup mutating_algorithms
4826 * @param first A forward iterator.
4827 * @param n The length of the sequence.
4828 * @param gen A function object taking no arguments and returning
4829 * std::iterator_traits<_ForwardIterator>::value_type
4830 * @return The end of the sequence, @p first+n
4832 * Performs the assignment @c *i = @p gen() for each @c i in the range
4833 * @p [first,first+n).
4835 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4836 * DR 865. More algorithms that throw away information
4838 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4840 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4842 // concept requirements
4843 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4844 // "the type returned by a _Generator"
4845 __typeof__(__gen())>)
4847 for (__decltype(__n
+ 0) __niter
= __n
;
4848 __niter
> 0; --__niter
, ++__first
)
4855 * @brief Copy a sequence, removing consecutive duplicate values.
4856 * @ingroup mutating_algorithms
4857 * @param first An input iterator.
4858 * @param last An input iterator.
4859 * @param result An output iterator.
4860 * @return An iterator designating the end of the resulting sequence.
4862 * Copies each element in the range @p [first,last) to the range
4863 * beginning at @p result, except that only the first element is copied
4864 * from groups of consecutive elements that compare equal.
4865 * unique_copy() is stable, so the relative order of elements that are
4866 * copied is unchanged.
4868 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4869 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4871 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4872 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4875 template<typename _InputIterator
, typename _OutputIterator
>
4876 inline _OutputIterator
4877 unique_copy(_InputIterator __first
, _InputIterator __last
,
4878 _OutputIterator __result
)
4880 // concept requirements
4881 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4882 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4883 typename iterator_traits
<_InputIterator
>::value_type
>)
4884 __glibcxx_function_requires(_EqualityComparableConcept
<
4885 typename iterator_traits
<_InputIterator
>::value_type
>)
4886 __glibcxx_requires_valid_range(__first
, __last
);
4888 if (__first
== __last
)
4890 return std::__unique_copy(__first
, __last
, __result
,
4891 std::__iterator_category(__first
),
4892 std::__iterator_category(__result
));
4896 * @brief Copy a sequence, removing consecutive values using a predicate.
4897 * @ingroup mutating_algorithms
4898 * @param first An input iterator.
4899 * @param last An input iterator.
4900 * @param result An output iterator.
4901 * @param binary_pred A binary predicate.
4902 * @return An iterator designating the end of the resulting sequence.
4904 * Copies each element in the range @p [first,last) to the range
4905 * beginning at @p result, except that only the first element is copied
4906 * from groups of consecutive elements for which @p binary_pred returns
4908 * unique_copy() is stable, so the relative order of elements that are
4909 * copied is unchanged.
4911 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4912 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4914 template<typename _InputIterator
, typename _OutputIterator
,
4915 typename _BinaryPredicate
>
4916 inline _OutputIterator
4917 unique_copy(_InputIterator __first
, _InputIterator __last
,
4918 _OutputIterator __result
,
4919 _BinaryPredicate __binary_pred
)
4921 // concept requirements -- predicates checked later
4922 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4923 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4924 typename iterator_traits
<_InputIterator
>::value_type
>)
4925 __glibcxx_requires_valid_range(__first
, __last
);
4927 if (__first
== __last
)
4929 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4930 std::__iterator_category(__first
),
4931 std::__iterator_category(__result
));
4936 * @brief Randomly shuffle the elements of a sequence.
4937 * @ingroup mutating_algorithms
4938 * @param first A forward iterator.
4939 * @param last A forward iterator.
4942 * Reorder the elements in the range @p [first,last) using a random
4943 * distribution, so that every possible ordering of the sequence is
4946 template<typename _RandomAccessIterator
>
4948 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4950 // concept requirements
4951 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4952 _RandomAccessIterator
>)
4953 __glibcxx_requires_valid_range(__first
, __last
);
4955 if (__first
!= __last
)
4956 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4957 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4961 * @brief Shuffle the elements of a sequence using a random number
4963 * @ingroup mutating_algorithms
4964 * @param first A forward iterator.
4965 * @param last A forward iterator.
4966 * @param rand The RNG functor or function.
4969 * Reorders the elements in the range @p [first,last) using @p rand to
4970 * provide a random distribution. Calling @p rand(N) for a positive
4971 * integer @p N should return a randomly chosen integer from the
4974 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4976 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4977 #ifdef __GXX_EXPERIMENTAL_CXX0X__
4978 _RandomNumberGenerator
&& __rand
)
4980 _RandomNumberGenerator
& __rand
)
4983 // concept requirements
4984 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4985 _RandomAccessIterator
>)
4986 __glibcxx_requires_valid_range(__first
, __last
);
4988 if (__first
== __last
)
4990 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4991 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4996 * @brief Move elements for which a predicate is true to the beginning
4998 * @ingroup mutating_algorithms
4999 * @param first A forward iterator.
5000 * @param last A forward iterator.
5001 * @param pred A predicate functor.
5002 * @return An iterator @p middle such that @p pred(i) is true for each
5003 * iterator @p i in the range @p [first,middle) and false for each @p i
5004 * in the range @p [middle,last).
5006 * @p pred must not modify its operand. @p partition() does not preserve
5007 * the relative ordering of elements in each group, use
5008 * @p stable_partition() if this is needed.
5010 template<typename _ForwardIterator
, typename _Predicate
>
5011 inline _ForwardIterator
5012 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5015 // concept requirements
5016 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5018 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5019 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5020 __glibcxx_requires_valid_range(__first
, __last
);
5022 return std::__partition(__first
, __last
, __pred
,
5023 std::__iterator_category(__first
));
5029 * @brief Sort the smallest elements of a sequence.
5030 * @ingroup sorting_algorithms
5031 * @param first An iterator.
5032 * @param middle Another iterator.
5033 * @param last Another iterator.
5036 * Sorts the smallest @p (middle-first) elements in the range
5037 * @p [first,last) and moves them to the range @p [first,middle). The
5038 * order of the remaining elements in the range @p [middle,last) is
5040 * After the sort if @p i and @j are iterators in the range
5041 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5042 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5044 template<typename _RandomAccessIterator
>
5046 partial_sort(_RandomAccessIterator __first
,
5047 _RandomAccessIterator __middle
,
5048 _RandomAccessIterator __last
)
5050 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5053 // concept requirements
5054 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5055 _RandomAccessIterator
>)
5056 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5057 __glibcxx_requires_valid_range(__first
, __middle
);
5058 __glibcxx_requires_valid_range(__middle
, __last
);
5060 std::__heap_select(__first
, __middle
, __last
);
5061 std::sort_heap(__first
, __middle
);
5065 * @brief Sort the smallest elements of a sequence using a predicate
5067 * @ingroup sorting_algorithms
5068 * @param first An iterator.
5069 * @param middle Another iterator.
5070 * @param last Another iterator.
5071 * @param comp A comparison functor.
5074 * Sorts the smallest @p (middle-first) elements in the range
5075 * @p [first,last) and moves them to the range @p [first,middle). The
5076 * order of the remaining elements in the range @p [middle,last) is
5078 * After the sort if @p i and @j are iterators in the range
5079 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5080 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5083 template<typename _RandomAccessIterator
, typename _Compare
>
5085 partial_sort(_RandomAccessIterator __first
,
5086 _RandomAccessIterator __middle
,
5087 _RandomAccessIterator __last
,
5090 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5093 // concept requirements
5094 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5095 _RandomAccessIterator
>)
5096 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5097 _ValueType
, _ValueType
>)
5098 __glibcxx_requires_valid_range(__first
, __middle
);
5099 __glibcxx_requires_valid_range(__middle
, __last
);
5101 std::__heap_select(__first
, __middle
, __last
, __comp
);
5102 std::sort_heap(__first
, __middle
, __comp
);
5106 * @brief Sort a sequence just enough to find a particular position.
5107 * @ingroup sorting_algorithms
5108 * @param first An iterator.
5109 * @param nth Another iterator.
5110 * @param last Another iterator.
5113 * Rearranges the elements in the range @p [first,last) so that @p *nth
5114 * is the same element that would have been in that position had the
5115 * whole sequence been sorted.
5116 * whole sequence been sorted. The elements either side of @p *nth are
5117 * not completely sorted, but for any iterator @i in the range
5118 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5119 * holds that @p *j<*i is false.
5121 template<typename _RandomAccessIterator
>
5123 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5124 _RandomAccessIterator __last
)
5126 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5129 // concept requirements
5130 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5131 _RandomAccessIterator
>)
5132 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5133 __glibcxx_requires_valid_range(__first
, __nth
);
5134 __glibcxx_requires_valid_range(__nth
, __last
);
5136 if (__first
== __last
|| __nth
== __last
)
5139 std::__introselect(__first
, __nth
, __last
,
5140 std::__lg(__last
- __first
) * 2);
5144 * @brief Sort a sequence just enough to find a particular position
5145 * using a predicate for comparison.
5146 * @ingroup sorting_algorithms
5147 * @param first An iterator.
5148 * @param nth Another iterator.
5149 * @param last Another iterator.
5150 * @param comp A comparison functor.
5153 * Rearranges the elements in the range @p [first,last) so that @p *nth
5154 * is the same element that would have been in that position had the
5155 * whole sequence been sorted. The elements either side of @p *nth are
5156 * not completely sorted, but for any iterator @i in the range
5157 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5158 * holds that @p comp(*j,*i) is false.
5160 template<typename _RandomAccessIterator
, typename _Compare
>
5162 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5163 _RandomAccessIterator __last
, _Compare __comp
)
5165 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5168 // concept requirements
5169 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5170 _RandomAccessIterator
>)
5171 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5172 _ValueType
, _ValueType
>)
5173 __glibcxx_requires_valid_range(__first
, __nth
);
5174 __glibcxx_requires_valid_range(__nth
, __last
);
5176 if (__first
== __last
|| __nth
== __last
)
5179 std::__introselect(__first
, __nth
, __last
,
5180 std::__lg(__last
- __first
) * 2, __comp
);
5185 * @brief Sort the elements of a sequence.
5186 * @ingroup sorting_algorithms
5187 * @param first An iterator.
5188 * @param last Another iterator.
5191 * Sorts the elements in the range @p [first,last) in ascending order,
5192 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5193 * @p [first,last-1).
5195 * The relative ordering of equivalent elements is not preserved, use
5196 * @p stable_sort() if this is needed.
5198 template<typename _RandomAccessIterator
>
5200 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5202 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5205 // concept requirements
5206 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5207 _RandomAccessIterator
>)
5208 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5209 __glibcxx_requires_valid_range(__first
, __last
);
5211 if (__first
!= __last
)
5213 std::__introsort_loop(__first
, __last
,
5214 std::__lg(__last
- __first
) * 2);
5215 std::__final_insertion_sort(__first
, __last
);
5220 * @brief Sort the elements of a sequence using a predicate for comparison.
5221 * @ingroup sorting_algorithms
5222 * @param first An iterator.
5223 * @param last Another iterator.
5224 * @param comp A comparison functor.
5227 * Sorts the elements in the range @p [first,last) in ascending order,
5228 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5229 * range @p [first,last-1).
5231 * The relative ordering of equivalent elements is not preserved, use
5232 * @p stable_sort() if this is needed.
5234 template<typename _RandomAccessIterator
, typename _Compare
>
5236 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5239 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5242 // concept requirements
5243 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5244 _RandomAccessIterator
>)
5245 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5247 __glibcxx_requires_valid_range(__first
, __last
);
5249 if (__first
!= __last
)
5251 std::__introsort_loop(__first
, __last
,
5252 std::__lg(__last
- __first
) * 2, __comp
);
5253 std::__final_insertion_sort(__first
, __last
, __comp
);
5258 * @brief Merges two sorted ranges.
5259 * @ingroup sorting_algorithms
5260 * @param first1 An iterator.
5261 * @param first2 Another iterator.
5262 * @param last1 Another iterator.
5263 * @param last2 Another iterator.
5264 * @param result An iterator pointing to the end of the merged range.
5265 * @return An iterator pointing to the first element <em>not less
5268 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5269 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5270 * must be sorted, and the output range must not overlap with either of
5271 * the input ranges. The sort is @e stable, that is, for equivalent
5272 * elements in the two ranges, elements from the first range will always
5273 * come before elements from the second.
5275 template<typename _InputIterator1
, typename _InputIterator2
,
5276 typename _OutputIterator
>
5278 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5279 _InputIterator2 __first2
, _InputIterator2 __last2
,
5280 _OutputIterator __result
)
5282 typedef typename iterator_traits
<_InputIterator1
>::value_type
5284 typedef typename iterator_traits
<_InputIterator2
>::value_type
5287 // concept requirements
5288 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5289 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5290 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5292 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5294 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5295 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5296 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5298 while (__first1
!= __last1
&& __first2
!= __last2
)
5300 if (*__first2
< *__first1
)
5302 *__result
= *__first2
;
5307 *__result
= *__first1
;
5312 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5317 * @brief Merges two sorted ranges.
5318 * @ingroup sorting_algorithms
5319 * @param first1 An iterator.
5320 * @param first2 Another iterator.
5321 * @param last1 Another iterator.
5322 * @param last2 Another iterator.
5323 * @param result An iterator pointing to the end of the merged range.
5324 * @param comp A functor to use for comparisons.
5325 * @return An iterator pointing to the first element "not less
5328 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5329 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5330 * must be sorted, and the output range must not overlap with either of
5331 * the input ranges. The sort is @e stable, that is, for equivalent
5332 * elements in the two ranges, elements from the first range will always
5333 * come before elements from the second.
5335 * The comparison function should have the same effects on ordering as
5336 * the function used for the initial sort.
5338 template<typename _InputIterator1
, typename _InputIterator2
,
5339 typename _OutputIterator
, typename _Compare
>
5341 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5342 _InputIterator2 __first2
, _InputIterator2 __last2
,
5343 _OutputIterator __result
, _Compare __comp
)
5345 typedef typename iterator_traits
<_InputIterator1
>::value_type
5347 typedef typename iterator_traits
<_InputIterator2
>::value_type
5350 // concept requirements
5351 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5352 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5353 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5355 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5357 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5358 _ValueType2
, _ValueType1
>)
5359 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5360 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5362 while (__first1
!= __last1
&& __first2
!= __last2
)
5364 if (__comp(*__first2
, *__first1
))
5366 *__result
= *__first2
;
5371 *__result
= *__first1
;
5376 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5382 * @brief Sort the elements of a sequence, preserving the relative order
5383 * of equivalent elements.
5384 * @ingroup sorting_algorithms
5385 * @param first An iterator.
5386 * @param last Another iterator.
5389 * Sorts the elements in the range @p [first,last) in ascending order,
5390 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5391 * @p [first,last-1).
5393 * The relative ordering of equivalent elements is preserved, so any two
5394 * elements @p x and @p y in the range @p [first,last) such that
5395 * @p x<y is false and @p y<x is false will have the same relative
5396 * ordering after calling @p stable_sort().
5398 template<typename _RandomAccessIterator
>
5400 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5402 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5404 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5407 // concept requirements
5408 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5409 _RandomAccessIterator
>)
5410 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5411 __glibcxx_requires_valid_range(__first
, __last
);
5413 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5415 if (__buf
.begin() == 0)
5416 std::__inplace_stable_sort(__first
, __last
);
5418 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5419 _DistanceType(__buf
.size()));
5423 * @brief Sort the elements of a sequence using a predicate for comparison,
5424 * preserving the relative order of equivalent elements.
5425 * @ingroup sorting_algorithms
5426 * @param first An iterator.
5427 * @param last Another iterator.
5428 * @param comp A comparison functor.
5431 * Sorts the elements in the range @p [first,last) in ascending order,
5432 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5433 * range @p [first,last-1).
5435 * The relative ordering of equivalent elements is preserved, so any two
5436 * elements @p x and @p y in the range @p [first,last) such that
5437 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5438 * relative ordering after calling @p stable_sort().
5440 template<typename _RandomAccessIterator
, typename _Compare
>
5442 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5445 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5447 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5450 // concept requirements
5451 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5452 _RandomAccessIterator
>)
5453 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5456 __glibcxx_requires_valid_range(__first
, __last
);
5458 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5460 if (__buf
.begin() == 0)
5461 std::__inplace_stable_sort(__first
, __last
, __comp
);
5463 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5464 _DistanceType(__buf
.size()), __comp
);
5469 * @brief Return the union of two sorted ranges.
5470 * @ingroup set_algorithms
5471 * @param first1 Start of first range.
5472 * @param last1 End of first range.
5473 * @param first2 Start of second range.
5474 * @param last2 End of second range.
5475 * @return End of the output range.
5476 * @ingroup set_algorithms
5478 * This operation iterates over both ranges, copying elements present in
5479 * each range in order to the output range. Iterators increment for each
5480 * range. When the current element of one range is less than the other,
5481 * that element is copied and the iterator advanced. If an element is
5482 * contained in both ranges, the element from the first range is copied and
5483 * both ranges advance. The output range may not overlap either input
5486 template<typename _InputIterator1
, typename _InputIterator2
,
5487 typename _OutputIterator
>
5489 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5490 _InputIterator2 __first2
, _InputIterator2 __last2
,
5491 _OutputIterator __result
)
5493 typedef typename iterator_traits
<_InputIterator1
>::value_type
5495 typedef typename iterator_traits
<_InputIterator2
>::value_type
5498 // concept requirements
5499 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5500 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5501 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5503 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5505 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5506 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5507 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5508 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5510 while (__first1
!= __last1
&& __first2
!= __last2
)
5512 if (*__first1
< *__first2
)
5514 *__result
= *__first1
;
5517 else if (*__first2
< *__first1
)
5519 *__result
= *__first2
;
5524 *__result
= *__first1
;
5530 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5535 * @brief Return the union of two sorted ranges using a comparison functor.
5536 * @ingroup set_algorithms
5537 * @param first1 Start of first range.
5538 * @param last1 End of first range.
5539 * @param first2 Start of second range.
5540 * @param last2 End of second range.
5541 * @param comp The comparison functor.
5542 * @return End of the output range.
5543 * @ingroup set_algorithms
5545 * This operation iterates over both ranges, copying elements present in
5546 * each range in order to the output range. Iterators increment for each
5547 * range. When the current element of one range is less than the other
5548 * according to @a comp, that element is copied and the iterator advanced.
5549 * If an equivalent element according to @a comp is contained in both
5550 * ranges, the element from the first range is copied and both ranges
5551 * advance. The output range may not overlap either input range.
5553 template<typename _InputIterator1
, typename _InputIterator2
,
5554 typename _OutputIterator
, typename _Compare
>
5556 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5557 _InputIterator2 __first2
, _InputIterator2 __last2
,
5558 _OutputIterator __result
, _Compare __comp
)
5560 typedef typename iterator_traits
<_InputIterator1
>::value_type
5562 typedef typename iterator_traits
<_InputIterator2
>::value_type
5565 // concept requirements
5566 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5567 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5568 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5570 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5572 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5573 _ValueType1
, _ValueType2
>)
5574 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5575 _ValueType2
, _ValueType1
>)
5576 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5577 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5579 while (__first1
!= __last1
&& __first2
!= __last2
)
5581 if (__comp(*__first1
, *__first2
))
5583 *__result
= *__first1
;
5586 else if (__comp(*__first2
, *__first1
))
5588 *__result
= *__first2
;
5593 *__result
= *__first1
;
5599 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5604 * @brief Return the intersection of two sorted ranges.
5605 * @ingroup set_algorithms
5606 * @param first1 Start of first range.
5607 * @param last1 End of first range.
5608 * @param first2 Start of second range.
5609 * @param last2 End of second range.
5610 * @return End of the output range.
5611 * @ingroup set_algorithms
5613 * This operation iterates over both ranges, copying elements present in
5614 * both ranges in order to the output range. Iterators increment for each
5615 * range. When the current element of one range is less than the other,
5616 * that iterator advances. If an element is contained in both ranges, the
5617 * element from the first range is copied and both ranges advance. The
5618 * output range may not overlap either input range.
5620 template<typename _InputIterator1
, typename _InputIterator2
,
5621 typename _OutputIterator
>
5623 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5624 _InputIterator2 __first2
, _InputIterator2 __last2
,
5625 _OutputIterator __result
)
5627 typedef typename iterator_traits
<_InputIterator1
>::value_type
5629 typedef typename iterator_traits
<_InputIterator2
>::value_type
5632 // concept requirements
5633 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5634 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5635 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5637 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5638 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5639 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5640 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5642 while (__first1
!= __last1
&& __first2
!= __last2
)
5643 if (*__first1
< *__first2
)
5645 else if (*__first2
< *__first1
)
5649 *__result
= *__first1
;
5658 * @brief Return the intersection of two sorted ranges using comparison
5660 * @ingroup set_algorithms
5661 * @param first1 Start of first range.
5662 * @param last1 End of first range.
5663 * @param first2 Start of second range.
5664 * @param last2 End of second range.
5665 * @param comp The comparison functor.
5666 * @return End of the output range.
5667 * @ingroup set_algorithms
5669 * This operation iterates over both ranges, copying elements present in
5670 * both ranges in order to the output range. Iterators increment for each
5671 * range. When the current element of one range is less than the other
5672 * according to @a comp, that iterator advances. If an element is
5673 * contained in both ranges according to @a comp, the element from the
5674 * first range is copied and both ranges advance. The output range may not
5675 * overlap either input range.
5677 template<typename _InputIterator1
, typename _InputIterator2
,
5678 typename _OutputIterator
, typename _Compare
>
5680 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5681 _InputIterator2 __first2
, _InputIterator2 __last2
,
5682 _OutputIterator __result
, _Compare __comp
)
5684 typedef typename iterator_traits
<_InputIterator1
>::value_type
5686 typedef typename iterator_traits
<_InputIterator2
>::value_type
5689 // concept requirements
5690 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5691 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5692 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5694 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5695 _ValueType1
, _ValueType2
>)
5696 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5697 _ValueType2
, _ValueType1
>)
5698 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5699 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5701 while (__first1
!= __last1
&& __first2
!= __last2
)
5702 if (__comp(*__first1
, *__first2
))
5704 else if (__comp(*__first2
, *__first1
))
5708 *__result
= *__first1
;
5717 * @brief Return the difference of two sorted ranges.
5718 * @ingroup set_algorithms
5719 * @param first1 Start of first range.
5720 * @param last1 End of first range.
5721 * @param first2 Start of second range.
5722 * @param last2 End of second range.
5723 * @return End of the output range.
5724 * @ingroup set_algorithms
5726 * This operation iterates over both ranges, copying elements present in
5727 * the first range but not the second in order to the output range.
5728 * Iterators increment for each range. When the current element of the
5729 * first range is less than the second, that element is copied and the
5730 * iterator advances. If the current element of the second range is less,
5731 * the iterator advances, but no element is copied. If an element is
5732 * contained in both ranges, no elements are copied and both ranges
5733 * advance. The output range may not overlap either input range.
5735 template<typename _InputIterator1
, typename _InputIterator2
,
5736 typename _OutputIterator
>
5738 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5739 _InputIterator2 __first2
, _InputIterator2 __last2
,
5740 _OutputIterator __result
)
5742 typedef typename iterator_traits
<_InputIterator1
>::value_type
5744 typedef typename iterator_traits
<_InputIterator2
>::value_type
5747 // concept requirements
5748 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5749 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5750 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5752 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5753 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5754 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5755 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5757 while (__first1
!= __last1
&& __first2
!= __last2
)
5758 if (*__first1
< *__first2
)
5760 *__result
= *__first1
;
5764 else if (*__first2
< *__first1
)
5771 return std::copy(__first1
, __last1
, __result
);
5775 * @brief Return the difference of two sorted ranges using comparison
5777 * @ingroup set_algorithms
5778 * @param first1 Start of first range.
5779 * @param last1 End of first range.
5780 * @param first2 Start of second range.
5781 * @param last2 End of second range.
5782 * @param comp The comparison functor.
5783 * @return End of the output range.
5784 * @ingroup set_algorithms
5786 * This operation iterates over both ranges, copying elements present in
5787 * the first range but not the second in order to the output range.
5788 * Iterators increment for each range. When the current element of the
5789 * first range is less than the second according to @a comp, that element
5790 * is copied and the iterator advances. If the current element of the
5791 * second range is less, no element is copied and the iterator advances.
5792 * If an element is contained in both ranges according to @a comp, no
5793 * elements are copied and both ranges advance. The output range may not
5794 * overlap either input range.
5796 template<typename _InputIterator1
, typename _InputIterator2
,
5797 typename _OutputIterator
, typename _Compare
>
5799 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5800 _InputIterator2 __first2
, _InputIterator2 __last2
,
5801 _OutputIterator __result
, _Compare __comp
)
5803 typedef typename iterator_traits
<_InputIterator1
>::value_type
5805 typedef typename iterator_traits
<_InputIterator2
>::value_type
5808 // concept requirements
5809 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5810 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5811 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5813 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5814 _ValueType1
, _ValueType2
>)
5815 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5816 _ValueType2
, _ValueType1
>)
5817 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5818 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5820 while (__first1
!= __last1
&& __first2
!= __last2
)
5821 if (__comp(*__first1
, *__first2
))
5823 *__result
= *__first1
;
5827 else if (__comp(*__first2
, *__first1
))
5834 return std::copy(__first1
, __last1
, __result
);
5838 * @brief Return the symmetric difference of two sorted ranges.
5839 * @ingroup set_algorithms
5840 * @param first1 Start of first range.
5841 * @param last1 End of first range.
5842 * @param first2 Start of second range.
5843 * @param last2 End of second range.
5844 * @return End of the output range.
5845 * @ingroup set_algorithms
5847 * This operation iterates over both ranges, copying elements present in
5848 * one range but not the other in order to the output range. Iterators
5849 * increment for each range. When the current element of one range is less
5850 * than the other, that element is copied and the iterator advances. If an
5851 * element is contained in both ranges, no elements are copied and both
5852 * ranges advance. The output range may not overlap either input range.
5854 template<typename _InputIterator1
, typename _InputIterator2
,
5855 typename _OutputIterator
>
5857 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5858 _InputIterator2 __first2
, _InputIterator2 __last2
,
5859 _OutputIterator __result
)
5861 typedef typename iterator_traits
<_InputIterator1
>::value_type
5863 typedef typename iterator_traits
<_InputIterator2
>::value_type
5866 // concept requirements
5867 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5868 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5869 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5871 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5873 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5874 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5875 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5876 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5878 while (__first1
!= __last1
&& __first2
!= __last2
)
5879 if (*__first1
< *__first2
)
5881 *__result
= *__first1
;
5885 else if (*__first2
< *__first1
)
5887 *__result
= *__first2
;
5896 return std::copy(__first2
, __last2
, std::copy(__first1
,
5897 __last1
, __result
));
5901 * @brief Return the symmetric difference of two sorted ranges using
5902 * comparison functor.
5903 * @ingroup set_algorithms
5904 * @param first1 Start of first range.
5905 * @param last1 End of first range.
5906 * @param first2 Start of second range.
5907 * @param last2 End of second range.
5908 * @param comp The comparison functor.
5909 * @return End of the output range.
5910 * @ingroup set_algorithms
5912 * This operation iterates over both ranges, copying elements present in
5913 * one range but not the other in order to the output range. Iterators
5914 * increment for each range. When the current element of one range is less
5915 * than the other according to @a comp, that element is copied and the
5916 * iterator advances. If an element is contained in both ranges according
5917 * to @a comp, no elements are copied and both ranges advance. The output
5918 * range may not overlap either input range.
5920 template<typename _InputIterator1
, typename _InputIterator2
,
5921 typename _OutputIterator
, typename _Compare
>
5923 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5924 _InputIterator2 __first2
, _InputIterator2 __last2
,
5925 _OutputIterator __result
,
5928 typedef typename iterator_traits
<_InputIterator1
>::value_type
5930 typedef typename iterator_traits
<_InputIterator2
>::value_type
5933 // concept requirements
5934 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5935 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5936 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5938 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5940 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5941 _ValueType1
, _ValueType2
>)
5942 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5943 _ValueType2
, _ValueType1
>)
5944 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5945 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5947 while (__first1
!= __last1
&& __first2
!= __last2
)
5948 if (__comp(*__first1
, *__first2
))
5950 *__result
= *__first1
;
5954 else if (__comp(*__first2
, *__first1
))
5956 *__result
= *__first2
;
5965 return std::copy(__first2
, __last2
,
5966 std::copy(__first1
, __last1
, __result
));
5971 * @brief Return the minimum element in a range.
5972 * @ingroup sorting_algorithms
5973 * @param first Start of range.
5974 * @param last End of range.
5975 * @return Iterator referencing the first instance of the smallest value.
5977 template<typename _ForwardIterator
>
5979 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5981 // concept requirements
5982 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5983 __glibcxx_function_requires(_LessThanComparableConcept
<
5984 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5985 __glibcxx_requires_valid_range(__first
, __last
);
5987 if (__first
== __last
)
5989 _ForwardIterator __result
= __first
;
5990 while (++__first
!= __last
)
5991 if (*__first
< *__result
)
5997 * @brief Return the minimum element in a range using comparison functor.
5998 * @ingroup sorting_algorithms
5999 * @param first Start of range.
6000 * @param last End of range.
6001 * @param comp Comparison functor.
6002 * @return Iterator referencing the first instance of the smallest value
6003 * according to comp.
6005 template<typename _ForwardIterator
, typename _Compare
>
6007 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6010 // concept requirements
6011 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6012 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6013 typename iterator_traits
<_ForwardIterator
>::value_type
,
6014 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6015 __glibcxx_requires_valid_range(__first
, __last
);
6017 if (__first
== __last
)
6019 _ForwardIterator __result
= __first
;
6020 while (++__first
!= __last
)
6021 if (__comp(*__first
, *__result
))
6027 * @brief Return the maximum element in a range.
6028 * @ingroup sorting_algorithms
6029 * @param first Start of range.
6030 * @param last End of range.
6031 * @return Iterator referencing the first instance of the largest value.
6033 template<typename _ForwardIterator
>
6035 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6037 // concept requirements
6038 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6039 __glibcxx_function_requires(_LessThanComparableConcept
<
6040 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6041 __glibcxx_requires_valid_range(__first
, __last
);
6043 if (__first
== __last
)
6045 _ForwardIterator __result
= __first
;
6046 while (++__first
!= __last
)
6047 if (*__result
< *__first
)
6053 * @brief Return the maximum element in a range using comparison functor.
6054 * @ingroup sorting_algorithms
6055 * @param first Start of range.
6056 * @param last End of range.
6057 * @param comp Comparison functor.
6058 * @return Iterator referencing the first instance of the largest value
6059 * according to comp.
6061 template<typename _ForwardIterator
, typename _Compare
>
6063 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6066 // concept requirements
6067 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6068 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6069 typename iterator_traits
<_ForwardIterator
>::value_type
,
6070 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6071 __glibcxx_requires_valid_range(__first
, __last
);
6073 if (__first
== __last
) return __first
;
6074 _ForwardIterator __result
= __first
;
6075 while (++__first
!= __last
)
6076 if (__comp(*__result
, *__first
))
6081 _GLIBCXX_END_NESTED_NAMESPACE
6083 #endif /* _STL_ALGO_H */