1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
64 #include <debug/debug.h>
65 #include <initializer_list>
67 // See concept_check.h for the __glibcxx_*_requires macros.
69 _GLIBCXX_BEGIN_NAMESPACE(std
)
72 * @brief Find the median of three values.
76 * @return One of @p a, @p b or @p c.
78 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
79 * then the value returned will be @c m.
80 * This is an SGI extension.
81 * @ingroup SGIextensions
83 template<typename _Tp
>
85 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
87 // concept requirements
88 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
105 * @brief Find the median of three values using a predicate for comparison.
109 * @param comp A binary predicate.
110 * @return One of @p a, @p b or @p c.
112 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
113 * and @p comp(m,n) are both true then the value returned will be @c m.
114 * This is an SGI extension.
115 * @ingroup SGIextensions
117 template<typename _Tp
, typename _Compare
>
119 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
121 // concept requirements
122 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
124 if (__comp(__a
, __b
))
125 if (__comp(__b
, __c
))
127 else if (__comp(__a
, __c
))
131 else if (__comp(__a
, __c
))
133 else if (__comp(__b
, __c
))
139 /// Swaps the median value of *__a, *__b and *__c to *__a
140 template<typename _Iterator
>
142 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
)
144 // concept requirements
145 __glibcxx_function_requires(_LessThanComparableConcept
<
146 typename iterator_traits
<_Iterator
>::value_type
>)
151 std::iter_swap(__a
, __b
);
152 else if (*__a
< *__c
)
153 std::iter_swap(__a
, __c
);
155 else if (*__a
< *__c
)
157 else if (*__b
< *__c
)
158 std::iter_swap(__a
, __c
);
160 std::iter_swap(__a
, __b
);
163 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
164 template<typename _Iterator
, typename _Compare
>
166 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
,
169 // concept requirements
170 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
171 typename iterator_traits
<_Iterator
>::value_type
,
172 typename iterator_traits
<_Iterator
>::value_type
>)
174 if (__comp(*__a
, *__b
))
176 if (__comp(*__b
, *__c
))
177 std::iter_swap(__a
, __b
);
178 else if (__comp(*__a
, *__c
))
179 std::iter_swap(__a
, __c
);
181 else if (__comp(*__a
, *__c
))
183 else if (__comp(*__b
, *__c
))
184 std::iter_swap(__a
, __c
);
186 std::iter_swap(__a
, __b
);
191 /// This is an overload used by find() for the Input Iterator case.
192 template<typename _InputIterator
, typename _Tp
>
193 inline _InputIterator
194 __find(_InputIterator __first
, _InputIterator __last
,
195 const _Tp
& __val
, input_iterator_tag
)
197 while (__first
!= __last
&& !(*__first
== __val
))
202 /// This is an overload used by find_if() for the Input Iterator case.
203 template<typename _InputIterator
, typename _Predicate
>
204 inline _InputIterator
205 __find_if(_InputIterator __first
, _InputIterator __last
,
206 _Predicate __pred
, input_iterator_tag
)
208 while (__first
!= __last
&& !bool(__pred(*__first
)))
213 /// This is an overload used by find() for the RAI case.
214 template<typename _RandomAccessIterator
, typename _Tp
>
215 _RandomAccessIterator
216 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
217 const _Tp
& __val
, random_access_iterator_tag
)
219 typename iterator_traits
<_RandomAccessIterator
>::difference_type
220 __trip_count
= (__last
- __first
) >> 2;
222 for (; __trip_count
> 0; --__trip_count
)
224 if (*__first
== __val
)
228 if (*__first
== __val
)
232 if (*__first
== __val
)
236 if (*__first
== __val
)
241 switch (__last
- __first
)
244 if (*__first
== __val
)
248 if (*__first
== __val
)
252 if (*__first
== __val
)
261 /// This is an overload used by find_if() for the RAI case.
262 template<typename _RandomAccessIterator
, typename _Predicate
>
263 _RandomAccessIterator
264 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
265 _Predicate __pred
, random_access_iterator_tag
)
267 typename iterator_traits
<_RandomAccessIterator
>::difference_type
268 __trip_count
= (__last
- __first
) >> 2;
270 for (; __trip_count
> 0; --__trip_count
)
272 if (__pred(*__first
))
276 if (__pred(*__first
))
280 if (__pred(*__first
))
284 if (__pred(*__first
))
289 switch (__last
- __first
)
292 if (__pred(*__first
))
296 if (__pred(*__first
))
300 if (__pred(*__first
))
309 #ifdef __GXX_EXPERIMENTAL_CXX0X__
310 /// This is an overload used by find_if_not() for the Input Iterator case.
311 template<typename _InputIterator
, typename _Predicate
>
312 inline _InputIterator
313 __find_if_not(_InputIterator __first
, _InputIterator __last
,
314 _Predicate __pred
, input_iterator_tag
)
316 while (__first
!= __last
&& bool(__pred(*__first
)))
321 /// This is an overload used by find_if_not() for the RAI case.
322 template<typename _RandomAccessIterator
, typename _Predicate
>
323 _RandomAccessIterator
324 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
325 _Predicate __pred
, random_access_iterator_tag
)
327 typename iterator_traits
<_RandomAccessIterator
>::difference_type
328 __trip_count
= (__last
- __first
) >> 2;
330 for (; __trip_count
> 0; --__trip_count
)
332 if (!bool(__pred(*__first
)))
336 if (!bool(__pred(*__first
)))
340 if (!bool(__pred(*__first
)))
344 if (!bool(__pred(*__first
)))
349 switch (__last
- __first
)
352 if (!bool(__pred(*__first
)))
356 if (!bool(__pred(*__first
)))
360 if (!bool(__pred(*__first
)))
372 // set_symmetric_difference
384 * This is an uglified
385 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
386 * overloaded for forward iterators.
388 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
390 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
391 _Integer __count
, const _Tp
& __val
,
392 std::forward_iterator_tag
)
394 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
395 while (__first
!= __last
)
397 typename iterator_traits
<_ForwardIterator
>::difference_type
399 _ForwardIterator __i
= __first
;
401 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
410 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
416 * This is an uglified
417 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
418 * overloaded for random access iterators.
420 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
422 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
423 _Integer __count
, const _Tp
& __val
,
424 std::random_access_iterator_tag
)
427 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
430 _DistanceType __tailSize
= __last
- __first
;
431 const _DistanceType __pattSize
= __count
;
433 if (__tailSize
< __pattSize
)
436 const _DistanceType __skipOffset
= __pattSize
- 1;
437 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
438 __tailSize
-= __pattSize
;
440 while (1) // the main loop...
442 // __lookAhead here is always pointing to the last element of next
444 while (!(*__lookAhead
== __val
)) // the skip loop...
446 if (__tailSize
< __pattSize
)
447 return __last
; // Failure
448 __lookAhead
+= __pattSize
;
449 __tailSize
-= __pattSize
;
451 _DistanceType __remainder
= __skipOffset
;
452 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
453 *__backTrack
== __val
; --__backTrack
)
455 if (--__remainder
== 0)
456 return (__lookAhead
- __skipOffset
); // Success
458 if (__remainder
> __tailSize
)
459 return __last
; // Failure
460 __lookAhead
+= __remainder
;
461 __tailSize
-= __remainder
;
468 * This is an uglified
469 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
471 * overloaded for forward iterators.
473 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
474 typename _BinaryPredicate
>
476 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
477 _Integer __count
, const _Tp
& __val
,
478 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
480 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
483 while (__first
!= __last
)
485 typename iterator_traits
<_ForwardIterator
>::difference_type
487 _ForwardIterator __i
= __first
;
489 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
499 while (__first
!= __last
500 && !bool(__binary_pred(*__first
, __val
)))
507 * This is an uglified
508 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
510 * overloaded for random access iterators.
512 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
513 typename _BinaryPredicate
>
515 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
516 _Integer __count
, const _Tp
& __val
,
517 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
520 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
523 _DistanceType __tailSize
= __last
- __first
;
524 const _DistanceType __pattSize
= __count
;
526 if (__tailSize
< __pattSize
)
529 const _DistanceType __skipOffset
= __pattSize
- 1;
530 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
531 __tailSize
-= __pattSize
;
533 while (1) // the main loop...
535 // __lookAhead here is always pointing to the last element of next
537 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
539 if (__tailSize
< __pattSize
)
540 return __last
; // Failure
541 __lookAhead
+= __pattSize
;
542 __tailSize
-= __pattSize
;
544 _DistanceType __remainder
= __skipOffset
;
545 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
546 __binary_pred(*__backTrack
, __val
); --__backTrack
)
548 if (--__remainder
== 0)
549 return (__lookAhead
- __skipOffset
); // Success
551 if (__remainder
> __tailSize
)
552 return __last
; // Failure
553 __lookAhead
+= __remainder
;
554 __tailSize
-= __remainder
;
558 // find_end for forward iterators.
559 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
561 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
562 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
563 forward_iterator_tag
, forward_iterator_tag
)
565 if (__first2
== __last2
)
569 _ForwardIterator1 __result
= __last1
;
572 _ForwardIterator1 __new_result
573 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
574 if (__new_result
== __last1
)
578 __result
= __new_result
;
579 __first1
= __new_result
;
586 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
587 typename _BinaryPredicate
>
589 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
590 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
591 forward_iterator_tag
, forward_iterator_tag
,
592 _BinaryPredicate __comp
)
594 if (__first2
== __last2
)
598 _ForwardIterator1 __result
= __last1
;
601 _ForwardIterator1 __new_result
602 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
604 if (__new_result
== __last1
)
608 __result
= __new_result
;
609 __first1
= __new_result
;
616 // find_end for bidirectional iterators (much faster).
617 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
618 _BidirectionalIterator1
619 __find_end(_BidirectionalIterator1 __first1
,
620 _BidirectionalIterator1 __last1
,
621 _BidirectionalIterator2 __first2
,
622 _BidirectionalIterator2 __last2
,
623 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
625 // concept requirements
626 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
627 _BidirectionalIterator1
>)
628 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
629 _BidirectionalIterator2
>)
631 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
632 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
634 _RevIterator1
__rlast1(__first1
);
635 _RevIterator2
__rlast2(__first2
);
636 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
638 _RevIterator2(__last2
),
641 if (__rresult
== __rlast1
)
645 _BidirectionalIterator1 __result
= __rresult
.base();
646 std::advance(__result
, -std::distance(__first2
, __last2
));
651 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
652 typename _BinaryPredicate
>
653 _BidirectionalIterator1
654 __find_end(_BidirectionalIterator1 __first1
,
655 _BidirectionalIterator1 __last1
,
656 _BidirectionalIterator2 __first2
,
657 _BidirectionalIterator2 __last2
,
658 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
659 _BinaryPredicate __comp
)
661 // concept requirements
662 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
663 _BidirectionalIterator1
>)
664 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
665 _BidirectionalIterator2
>)
667 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
668 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
670 _RevIterator1
__rlast1(__first1
);
671 _RevIterator2
__rlast2(__first2
);
672 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
673 _RevIterator2(__last2
), __rlast2
,
676 if (__rresult
== __rlast1
)
680 _BidirectionalIterator1 __result
= __rresult
.base();
681 std::advance(__result
, -std::distance(__first2
, __last2
));
687 * @brief Find last matching subsequence in a sequence.
688 * @ingroup non_mutating_algorithms
689 * @param first1 Start of range to search.
690 * @param last1 End of range to search.
691 * @param first2 Start of sequence to match.
692 * @param last2 End of sequence to match.
693 * @return The last iterator @c i in the range
694 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
695 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
696 * such iterator exists.
698 * Searches the range @p [first1,last1) for a sub-sequence that compares
699 * equal value-by-value with the sequence given by @p [first2,last2) and
700 * returns an iterator to the first element of the sub-sequence, or
701 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
702 * last such subsequence contained in [first,last1).
704 * Because the sub-sequence must lie completely within the range
705 * @p [first1,last1) it must start at a position less than
706 * @p last1-(last2-first2) where @p last2-first2 is the length of the
708 * This means that the returned iterator @c i will be in the range
709 * @p [first1,last1-(last2-first2))
711 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
712 inline _ForwardIterator1
713 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
714 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
716 // concept requirements
717 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
718 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
719 __glibcxx_function_requires(_EqualOpConcept
<
720 typename iterator_traits
<_ForwardIterator1
>::value_type
,
721 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
722 __glibcxx_requires_valid_range(__first1
, __last1
);
723 __glibcxx_requires_valid_range(__first2
, __last2
);
725 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
726 std::__iterator_category(__first1
),
727 std::__iterator_category(__first2
));
731 * @brief Find last matching subsequence in a sequence using a predicate.
732 * @ingroup non_mutating_algorithms
733 * @param first1 Start of range to search.
734 * @param last1 End of range to search.
735 * @param first2 Start of sequence to match.
736 * @param last2 End of sequence to match.
737 * @param comp The predicate to use.
738 * @return The last iterator @c i in the range
739 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
740 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
741 * @p last1 if no such iterator exists.
743 * Searches the range @p [first1,last1) for a sub-sequence that compares
744 * equal value-by-value with the sequence given by @p [first2,last2) using
745 * comp as a predicate and returns an iterator to the first element of the
746 * sub-sequence, or @p last1 if the sub-sequence is not found. The
747 * sub-sequence will be the last such subsequence contained in
750 * Because the sub-sequence must lie completely within the range
751 * @p [first1,last1) it must start at a position less than
752 * @p last1-(last2-first2) where @p last2-first2 is the length of the
754 * This means that the returned iterator @c i will be in the range
755 * @p [first1,last1-(last2-first2))
757 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
758 typename _BinaryPredicate
>
759 inline _ForwardIterator1
760 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
761 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
762 _BinaryPredicate __comp
)
764 // concept requirements
765 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
766 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
767 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
768 typename iterator_traits
<_ForwardIterator1
>::value_type
,
769 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
770 __glibcxx_requires_valid_range(__first1
, __last1
);
771 __glibcxx_requires_valid_range(__first2
, __last2
);
773 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
774 std::__iterator_category(__first1
),
775 std::__iterator_category(__first2
),
779 #ifdef __GXX_EXPERIMENTAL_CXX0X__
781 * @brief Checks that a predicate is true for all the elements
783 * @ingroup non_mutating_algorithms
784 * @param first An input iterator.
785 * @param last An input iterator.
786 * @param pred A predicate.
787 * @return True if the check is true, false otherwise.
789 * Returns true if @p pred is true for each element in the range
790 * @p [first,last), and false otherwise.
792 template<typename _InputIterator
, typename _Predicate
>
794 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
795 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
798 * @brief Checks that a predicate is false for all the elements
800 * @ingroup non_mutating_algorithms
801 * @param first An input iterator.
802 * @param last An input iterator.
803 * @param pred A predicate.
804 * @return True if the check is true, false otherwise.
806 * Returns true if @p pred is false for each element in the range
807 * @p [first,last), and false otherwise.
809 template<typename _InputIterator
, typename _Predicate
>
811 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
812 { return __last
== _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
); }
815 * @brief Checks that a predicate is false for at least an element
817 * @ingroup non_mutating_algorithms
818 * @param first An input iterator.
819 * @param last An input iterator.
820 * @param pred A predicate.
821 * @return True if the check is true, false otherwise.
823 * Returns true if an element exists in the range @p [first,last) such that
824 * @p pred is true, and false otherwise.
826 template<typename _InputIterator
, typename _Predicate
>
828 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
829 { return !std::none_of(__first
, __last
, __pred
); }
832 * @brief Find the first element in a sequence for which a
833 * predicate is false.
834 * @ingroup non_mutating_algorithms
835 * @param first An input iterator.
836 * @param last An input iterator.
837 * @param pred A predicate.
838 * @return The first iterator @c i in the range @p [first,last)
839 * such that @p pred(*i) is false, or @p last if no such iterator exists.
841 template<typename _InputIterator
, typename _Predicate
>
842 inline _InputIterator
843 find_if_not(_InputIterator __first
, _InputIterator __last
,
846 // concept requirements
847 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
848 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
849 typename iterator_traits
<_InputIterator
>::value_type
>)
850 __glibcxx_requires_valid_range(__first
, __last
);
851 return std::__find_if_not(__first
, __last
, __pred
,
852 std::__iterator_category(__first
));
856 * @brief Checks whether the sequence is partitioned.
857 * @ingroup mutating_algorithms
858 * @param first An input iterator.
859 * @param last An input iterator.
860 * @param pred A predicate.
861 * @return True if the range @p [first,last) is partioned by @p pred,
862 * i.e. if all elements that satisfy @p pred appear before those that
865 template<typename _InputIterator
, typename _Predicate
>
867 is_partitioned(_InputIterator __first
, _InputIterator __last
,
870 __first
= std::find_if_not(__first
, __last
, __pred
);
871 return std::none_of(__first
, __last
, __pred
);
875 * @brief Find the partition point of a partitioned range.
876 * @ingroup mutating_algorithms
877 * @param first An iterator.
878 * @param last Another iterator.
879 * @param pred A predicate.
880 * @return An iterator @p mid such that @p all_of(first, mid, pred)
881 * and @p none_of(mid, last, pred) are both true.
883 template<typename _ForwardIterator
, typename _Predicate
>
885 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
888 // concept requirements
889 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
890 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
891 typename iterator_traits
<_ForwardIterator
>::value_type
>)
893 // A specific debug-mode test will be necessary...
894 __glibcxx_requires_valid_range(__first
, __last
);
896 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
899 _DistanceType __len
= std::distance(__first
, __last
);
900 _DistanceType __half
;
901 _ForwardIterator __middle
;
907 std::advance(__middle
, __half
);
908 if (__pred(*__middle
))
912 __len
= __len
- __half
- 1;
923 * @brief Copy a sequence, removing elements of a given value.
924 * @ingroup mutating_algorithms
925 * @param first An input iterator.
926 * @param last An input iterator.
927 * @param result An output iterator.
928 * @param value The value to be removed.
929 * @return An iterator designating the end of the resulting sequence.
931 * Copies each element in the range @p [first,last) not equal to @p value
932 * to the range beginning at @p result.
933 * remove_copy() is stable, so the relative order of elements that are
934 * copied is unchanged.
936 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
938 remove_copy(_InputIterator __first
, _InputIterator __last
,
939 _OutputIterator __result
, const _Tp
& __value
)
941 // concept requirements
942 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
943 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
944 typename iterator_traits
<_InputIterator
>::value_type
>)
945 __glibcxx_function_requires(_EqualOpConcept
<
946 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
947 __glibcxx_requires_valid_range(__first
, __last
);
949 for (; __first
!= __last
; ++__first
)
950 if (!(*__first
== __value
))
952 *__result
= *__first
;
959 * @brief Copy a sequence, removing elements for which a predicate is true.
960 * @ingroup mutating_algorithms
961 * @param first An input iterator.
962 * @param last An input iterator.
963 * @param result An output iterator.
964 * @param pred A predicate.
965 * @return An iterator designating the end of the resulting sequence.
967 * Copies each element in the range @p [first,last) for which
968 * @p pred returns false to the range beginning at @p result.
970 * remove_copy_if() is stable, so the relative order of elements that are
971 * copied is unchanged.
973 template<typename _InputIterator
, typename _OutputIterator
,
976 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
977 _OutputIterator __result
, _Predicate __pred
)
979 // concept requirements
980 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
981 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
982 typename iterator_traits
<_InputIterator
>::value_type
>)
983 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
984 typename iterator_traits
<_InputIterator
>::value_type
>)
985 __glibcxx_requires_valid_range(__first
, __last
);
987 for (; __first
!= __last
; ++__first
)
988 if (!bool(__pred(*__first
)))
990 *__result
= *__first
;
996 #ifdef __GXX_EXPERIMENTAL_CXX0X__
998 * @brief Copy the elements of a sequence for which a predicate is true.
999 * @ingroup mutating_algorithms
1000 * @param first An input iterator.
1001 * @param last An input iterator.
1002 * @param result An output iterator.
1003 * @param pred A predicate.
1004 * @return An iterator designating the end of the resulting sequence.
1006 * Copies each element in the range @p [first,last) for which
1007 * @p pred returns true to the range beginning at @p result.
1009 * copy_if() is stable, so the relative order of elements that are
1010 * copied is unchanged.
1012 template<typename _InputIterator
, typename _OutputIterator
,
1013 typename _Predicate
>
1015 copy_if(_InputIterator __first
, _InputIterator __last
,
1016 _OutputIterator __result
, _Predicate __pred
)
1018 // concept requirements
1019 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1020 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1021 typename iterator_traits
<_InputIterator
>::value_type
>)
1022 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1023 typename iterator_traits
<_InputIterator
>::value_type
>)
1024 __glibcxx_requires_valid_range(__first
, __last
);
1026 for (; __first
!= __last
; ++__first
)
1027 if (__pred(*__first
))
1029 *__result
= *__first
;
1036 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1038 __copy_n(_InputIterator __first
, _Size __n
,
1039 _OutputIterator __result
, input_iterator_tag
)
1041 for (; __n
> 0; --__n
)
1043 *__result
= *__first
;
1050 template<typename _RandomAccessIterator
, typename _Size
,
1051 typename _OutputIterator
>
1052 inline _OutputIterator
1053 __copy_n(_RandomAccessIterator __first
, _Size __n
,
1054 _OutputIterator __result
, random_access_iterator_tag
)
1055 { return std::copy(__first
, __first
+ __n
, __result
); }
1058 * @brief Copies the range [first,first+n) into [result,result+n).
1059 * @ingroup mutating_algorithms
1060 * @param first An input iterator.
1061 * @param n The number of elements to copy.
1062 * @param result An output iterator.
1065 * This inline function will boil down to a call to @c memmove whenever
1066 * possible. Failing that, if random access iterators are passed, then the
1067 * loop count will be known (and therefore a candidate for compiler
1068 * optimizations such as unrolling).
1070 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1071 inline _OutputIterator
1072 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1074 // concept requirements
1075 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1076 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1077 typename iterator_traits
<_InputIterator
>::value_type
>)
1079 return std::__copy_n(__first
, __n
, __result
,
1080 std::__iterator_category(__first
));
1084 * @brief Copy the elements of a sequence to separate output sequences
1085 * depending on the truth value of a predicate.
1086 * @ingroup mutating_algorithms
1087 * @param first An input iterator.
1088 * @param last An input iterator.
1089 * @param out_true An output iterator.
1090 * @param out_false An output iterator.
1091 * @param pred A predicate.
1092 * @return A pair designating the ends of the resulting sequences.
1094 * Copies each element in the range @p [first,last) for which
1095 * @p pred returns true to the range beginning at @p out_true
1096 * and each element for which @p pred returns false to @p out_false.
1098 template<typename _InputIterator
, typename _OutputIterator1
,
1099 typename _OutputIterator2
, typename _Predicate
>
1100 pair
<_OutputIterator1
, _OutputIterator2
>
1101 partition_copy(_InputIterator __first
, _InputIterator __last
,
1102 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1105 // concept requirements
1106 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1107 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1108 typename iterator_traits
<_InputIterator
>::value_type
>)
1109 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1110 typename iterator_traits
<_InputIterator
>::value_type
>)
1111 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1112 typename iterator_traits
<_InputIterator
>::value_type
>)
1113 __glibcxx_requires_valid_range(__first
, __last
);
1115 for (; __first
!= __last
; ++__first
)
1116 if (__pred(*__first
))
1118 *__out_true
= *__first
;
1123 *__out_false
= *__first
;
1127 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1132 * @brief Remove elements from a sequence.
1133 * @ingroup mutating_algorithms
1134 * @param first An input iterator.
1135 * @param last An input iterator.
1136 * @param value The value to be removed.
1137 * @return An iterator designating the end of the resulting sequence.
1139 * All elements equal to @p value are removed from the range
1142 * remove() is stable, so the relative order of elements that are
1143 * not removed is unchanged.
1145 * Elements between the end of the resulting sequence and @p last
1146 * are still present, but their value is unspecified.
1148 template<typename _ForwardIterator
, typename _Tp
>
1150 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1153 // concept requirements
1154 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1156 __glibcxx_function_requires(_EqualOpConcept
<
1157 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1158 __glibcxx_requires_valid_range(__first
, __last
);
1160 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1161 if(__first
== __last
)
1163 _ForwardIterator __result
= __first
;
1165 for(; __first
!= __last
; ++__first
)
1166 if(!(*__first
== __value
))
1168 *__result
= _GLIBCXX_MOVE(*__first
);
1175 * @brief Remove elements from a sequence using a predicate.
1176 * @ingroup mutating_algorithms
1177 * @param first A forward iterator.
1178 * @param last A forward iterator.
1179 * @param pred A predicate.
1180 * @return An iterator designating the end of the resulting sequence.
1182 * All elements for which @p pred returns true are removed from the range
1185 * remove_if() is stable, so the relative order of elements that are
1186 * not removed is unchanged.
1188 * Elements between the end of the resulting sequence and @p last
1189 * are still present, but their value is unspecified.
1191 template<typename _ForwardIterator
, typename _Predicate
>
1193 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1196 // concept requirements
1197 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1199 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1200 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1201 __glibcxx_requires_valid_range(__first
, __last
);
1203 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1204 if(__first
== __last
)
1206 _ForwardIterator __result
= __first
;
1208 for(; __first
!= __last
; ++__first
)
1209 if(!bool(__pred(*__first
)))
1211 *__result
= _GLIBCXX_MOVE(*__first
);
1218 * @brief Remove consecutive duplicate values from a sequence.
1219 * @ingroup mutating_algorithms
1220 * @param first A forward iterator.
1221 * @param last A forward iterator.
1222 * @return An iterator designating the end of the resulting sequence.
1224 * Removes all but the first element from each group of consecutive
1225 * values that compare equal.
1226 * unique() is stable, so the relative order of elements that are
1227 * not removed is unchanged.
1228 * Elements between the end of the resulting sequence and @p last
1229 * are still present, but their value is unspecified.
1231 template<typename _ForwardIterator
>
1233 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1235 // concept requirements
1236 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1238 __glibcxx_function_requires(_EqualityComparableConcept
<
1239 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1240 __glibcxx_requires_valid_range(__first
, __last
);
1242 // Skip the beginning, if already unique.
1243 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1244 if (__first
== __last
)
1247 // Do the real copy work.
1248 _ForwardIterator __dest
= __first
;
1250 while (++__first
!= __last
)
1251 if (!(*__dest
== *__first
))
1252 *++__dest
= _GLIBCXX_MOVE(*__first
);
1257 * @brief Remove consecutive values from a sequence using a predicate.
1258 * @ingroup mutating_algorithms
1259 * @param first A forward iterator.
1260 * @param last A forward iterator.
1261 * @param binary_pred A binary predicate.
1262 * @return An iterator designating the end of the resulting sequence.
1264 * Removes all but the first element from each group of consecutive
1265 * values for which @p binary_pred returns true.
1266 * unique() is stable, so the relative order of elements that are
1267 * not removed is unchanged.
1268 * Elements between the end of the resulting sequence and @p last
1269 * are still present, but their value is unspecified.
1271 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1273 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1274 _BinaryPredicate __binary_pred
)
1276 // concept requirements
1277 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1279 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1280 typename iterator_traits
<_ForwardIterator
>::value_type
,
1281 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1282 __glibcxx_requires_valid_range(__first
, __last
);
1284 // Skip the beginning, if already unique.
1285 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1286 if (__first
== __last
)
1289 // Do the real copy work.
1290 _ForwardIterator __dest
= __first
;
1292 while (++__first
!= __last
)
1293 if (!bool(__binary_pred(*__dest
, *__first
)))
1294 *++__dest
= _GLIBCXX_MOVE(*__first
);
1299 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1301 * overloaded for forward iterators and output iterator as result.
1303 template<typename _ForwardIterator
, typename _OutputIterator
>
1305 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1306 _OutputIterator __result
,
1307 forward_iterator_tag
, output_iterator_tag
)
1309 // concept requirements -- taken care of in dispatching function
1310 _ForwardIterator __next
= __first
;
1311 *__result
= *__first
;
1312 while (++__next
!= __last
)
1313 if (!(*__first
== *__next
))
1316 *++__result
= *__first
;
1322 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1324 * overloaded for input iterators and output iterator as result.
1326 template<typename _InputIterator
, typename _OutputIterator
>
1328 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1329 _OutputIterator __result
,
1330 input_iterator_tag
, output_iterator_tag
)
1332 // concept requirements -- taken care of in dispatching function
1333 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1334 *__result
= __value
;
1335 while (++__first
!= __last
)
1336 if (!(__value
== *__first
))
1339 *++__result
= __value
;
1345 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1347 * overloaded for input iterators and forward iterator as result.
1349 template<typename _InputIterator
, typename _ForwardIterator
>
1351 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1352 _ForwardIterator __result
,
1353 input_iterator_tag
, forward_iterator_tag
)
1355 // concept requirements -- taken care of in dispatching function
1356 *__result
= *__first
;
1357 while (++__first
!= __last
)
1358 if (!(*__result
== *__first
))
1359 *++__result
= *__first
;
1364 * This is an uglified
1365 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1367 * overloaded for forward iterators and output iterator as result.
1369 template<typename _ForwardIterator
, typename _OutputIterator
,
1370 typename _BinaryPredicate
>
1372 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1373 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1374 forward_iterator_tag
, output_iterator_tag
)
1376 // concept requirements -- iterators already checked
1377 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1378 typename iterator_traits
<_ForwardIterator
>::value_type
,
1379 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1381 _ForwardIterator __next
= __first
;
1382 *__result
= *__first
;
1383 while (++__next
!= __last
)
1384 if (!bool(__binary_pred(*__first
, *__next
)))
1387 *++__result
= *__first
;
1393 * This is an uglified
1394 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1396 * overloaded for input iterators and output iterator as result.
1398 template<typename _InputIterator
, typename _OutputIterator
,
1399 typename _BinaryPredicate
>
1401 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1402 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1403 input_iterator_tag
, output_iterator_tag
)
1405 // concept requirements -- iterators already checked
1406 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1407 typename iterator_traits
<_InputIterator
>::value_type
,
1408 typename iterator_traits
<_InputIterator
>::value_type
>)
1410 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1411 *__result
= __value
;
1412 while (++__first
!= __last
)
1413 if (!bool(__binary_pred(__value
, *__first
)))
1416 *++__result
= __value
;
1422 * This is an uglified
1423 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1425 * overloaded for input iterators and forward iterator as result.
1427 template<typename _InputIterator
, typename _ForwardIterator
,
1428 typename _BinaryPredicate
>
1430 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1431 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1432 input_iterator_tag
, forward_iterator_tag
)
1434 // concept requirements -- iterators already checked
1435 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1436 typename iterator_traits
<_ForwardIterator
>::value_type
,
1437 typename iterator_traits
<_InputIterator
>::value_type
>)
1439 *__result
= *__first
;
1440 while (++__first
!= __last
)
1441 if (!bool(__binary_pred(*__result
, *__first
)))
1442 *++__result
= *__first
;
1447 * This is an uglified reverse(_BidirectionalIterator,
1448 * _BidirectionalIterator)
1449 * overloaded for bidirectional iterators.
1451 template<typename _BidirectionalIterator
>
1453 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1454 bidirectional_iterator_tag
)
1457 if (__first
== __last
|| __first
== --__last
)
1461 std::iter_swap(__first
, __last
);
1467 * This is an uglified reverse(_BidirectionalIterator,
1468 * _BidirectionalIterator)
1469 * overloaded for random access iterators.
1471 template<typename _RandomAccessIterator
>
1473 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1474 random_access_iterator_tag
)
1476 if (__first
== __last
)
1479 while (__first
< __last
)
1481 std::iter_swap(__first
, __last
);
1488 * @brief Reverse a sequence.
1489 * @ingroup mutating_algorithms
1490 * @param first A bidirectional iterator.
1491 * @param last A bidirectional iterator.
1492 * @return reverse() returns no value.
1494 * Reverses the order of the elements in the range @p [first,last),
1495 * so that the first element becomes the last etc.
1496 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1497 * swaps @p *(first+i) and @p *(last-(i+1))
1499 template<typename _BidirectionalIterator
>
1501 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1503 // concept requirements
1504 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1505 _BidirectionalIterator
>)
1506 __glibcxx_requires_valid_range(__first
, __last
);
1507 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1511 * @brief Copy a sequence, reversing its elements.
1512 * @ingroup mutating_algorithms
1513 * @param first A bidirectional iterator.
1514 * @param last A bidirectional iterator.
1515 * @param result An output iterator.
1516 * @return An iterator designating the end of the resulting sequence.
1518 * Copies the elements in the range @p [first,last) to the range
1519 * @p [result,result+(last-first)) such that the order of the
1520 * elements is reversed.
1521 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1522 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1523 * The ranges @p [first,last) and @p [result,result+(last-first))
1526 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1528 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1529 _OutputIterator __result
)
1531 // concept requirements
1532 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1533 _BidirectionalIterator
>)
1534 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1535 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1536 __glibcxx_requires_valid_range(__first
, __last
);
1538 while (__first
!= __last
)
1541 *__result
= *__last
;
1548 * This is a helper function for the rotate algorithm specialized on RAIs.
1549 * It returns the greatest common divisor of two integer values.
1551 template<typename _EuclideanRingElement
>
1552 _EuclideanRingElement
1553 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1557 _EuclideanRingElement __t
= __m
% __n
;
1564 /// This is a helper function for the rotate algorithm.
1565 template<typename _ForwardIterator
>
1567 __rotate(_ForwardIterator __first
,
1568 _ForwardIterator __middle
,
1569 _ForwardIterator __last
,
1570 forward_iterator_tag
)
1572 if (__first
== __middle
|| __last
== __middle
)
1575 _ForwardIterator __first2
= __middle
;
1578 std::iter_swap(__first
, __first2
);
1581 if (__first
== __middle
)
1582 __middle
= __first2
;
1584 while (__first2
!= __last
);
1586 __first2
= __middle
;
1588 while (__first2
!= __last
)
1590 std::iter_swap(__first
, __first2
);
1593 if (__first
== __middle
)
1594 __middle
= __first2
;
1595 else if (__first2
== __last
)
1596 __first2
= __middle
;
1600 /// This is a helper function for the rotate algorithm.
1601 template<typename _BidirectionalIterator
>
1603 __rotate(_BidirectionalIterator __first
,
1604 _BidirectionalIterator __middle
,
1605 _BidirectionalIterator __last
,
1606 bidirectional_iterator_tag
)
1608 // concept requirements
1609 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1610 _BidirectionalIterator
>)
1612 if (__first
== __middle
|| __last
== __middle
)
1615 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1616 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1618 while (__first
!= __middle
&& __middle
!= __last
)
1620 std::iter_swap(__first
, --__last
);
1624 if (__first
== __middle
)
1625 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1627 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1630 /// This is a helper function for the rotate algorithm.
1631 template<typename _RandomAccessIterator
>
1633 __rotate(_RandomAccessIterator __first
,
1634 _RandomAccessIterator __middle
,
1635 _RandomAccessIterator __last
,
1636 random_access_iterator_tag
)
1638 // concept requirements
1639 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1640 _RandomAccessIterator
>)
1642 if (__first
== __middle
|| __last
== __middle
)
1645 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1647 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1650 const _Distance __n
= __last
- __first
;
1651 const _Distance __k
= __middle
- __first
;
1652 const _Distance __l
= __n
- __k
;
1656 std::swap_ranges(__first
, __middle
, __middle
);
1660 const _Distance __d
= std::__gcd(__n
, __k
);
1662 for (_Distance __i
= 0; __i
< __d
; __i
++)
1664 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1665 _RandomAccessIterator __p
= __first
;
1669 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1671 if (__p
> __first
+ __l
)
1673 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1677 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1683 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1685 if (__p
< __last
- __k
)
1687 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1690 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1695 *__p
= _GLIBCXX_MOVE(__tmp
);
1701 * @brief Rotate the elements of a sequence.
1702 * @ingroup mutating_algorithms
1703 * @param first A forward iterator.
1704 * @param middle A forward iterator.
1705 * @param last A forward iterator.
1708 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1709 * positions so that the element at @p middle is moved to @p first, the
1710 * element at @p middle+1 is moved to @first+1 and so on for each element
1711 * in the range @p [first,last).
1713 * This effectively swaps the ranges @p [first,middle) and
1716 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1717 * each @p n in the range @p [0,last-first).
1719 template<typename _ForwardIterator
>
1721 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1722 _ForwardIterator __last
)
1724 // concept requirements
1725 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1727 __glibcxx_requires_valid_range(__first
, __middle
);
1728 __glibcxx_requires_valid_range(__middle
, __last
);
1730 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1732 std::__rotate(__first
, __middle
, __last
, _IterType());
1736 * @brief Copy a sequence, rotating its elements.
1737 * @ingroup mutating_algorithms
1738 * @param first A forward iterator.
1739 * @param middle A forward iterator.
1740 * @param last A forward iterator.
1741 * @param result An output iterator.
1742 * @return An iterator designating the end of the resulting sequence.
1744 * Copies the elements of the range @p [first,last) to the range
1745 * beginning at @result, rotating the copied elements by @p (middle-first)
1746 * positions so that the element at @p middle is moved to @p result, the
1747 * element at @p middle+1 is moved to @result+1 and so on for each element
1748 * in the range @p [first,last).
1750 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1751 * each @p n in the range @p [0,last-first).
1753 template<typename _ForwardIterator
, typename _OutputIterator
>
1755 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1756 _ForwardIterator __last
, _OutputIterator __result
)
1758 // concept requirements
1759 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1760 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1761 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1762 __glibcxx_requires_valid_range(__first
, __middle
);
1763 __glibcxx_requires_valid_range(__middle
, __last
);
1765 return std::copy(__first
, __middle
,
1766 std::copy(__middle
, __last
, __result
));
1769 /// This is a helper function...
1770 template<typename _ForwardIterator
, typename _Predicate
>
1772 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1773 _Predicate __pred
, forward_iterator_tag
)
1775 if (__first
== __last
)
1778 while (__pred(*__first
))
1779 if (++__first
== __last
)
1782 _ForwardIterator __next
= __first
;
1784 while (++__next
!= __last
)
1785 if (__pred(*__next
))
1787 std::iter_swap(__first
, __next
);
1794 /// This is a helper function...
1795 template<typename _BidirectionalIterator
, typename _Predicate
>
1796 _BidirectionalIterator
1797 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1798 _Predicate __pred
, bidirectional_iterator_tag
)
1803 if (__first
== __last
)
1805 else if (__pred(*__first
))
1811 if (__first
== __last
)
1813 else if (!bool(__pred(*__last
)))
1817 std::iter_swap(__first
, __last
);
1824 /// This is a helper function...
1825 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1827 __inplace_stable_partition(_ForwardIterator __first
,
1828 _ForwardIterator __last
,
1829 _Predicate __pred
, _Distance __len
)
1832 return __pred(*__first
) ? __last
: __first
;
1833 _ForwardIterator __middle
= __first
;
1834 std::advance(__middle
, __len
/ 2);
1835 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1839 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1843 std::rotate(__begin
, __middle
, __end
);
1844 std::advance(__begin
, std::distance(__middle
, __end
));
1848 /// This is a helper function...
1849 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1852 __stable_partition_adaptive(_ForwardIterator __first
,
1853 _ForwardIterator __last
,
1854 _Predicate __pred
, _Distance __len
,
1856 _Distance __buffer_size
)
1858 if (__len
<= __buffer_size
)
1860 _ForwardIterator __result1
= __first
;
1861 _Pointer __result2
= __buffer
;
1862 for (; __first
!= __last
; ++__first
)
1863 if (__pred(*__first
))
1865 *__result1
= _GLIBCXX_MOVE(*__first
);
1870 *__result2
= _GLIBCXX_MOVE(*__first
);
1873 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1878 _ForwardIterator __middle
= __first
;
1879 std::advance(__middle
, __len
/ 2);
1880 _ForwardIterator __begin
=
1881 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1882 __len
/ 2, __buffer
,
1884 _ForwardIterator __end
=
1885 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1887 __buffer
, __buffer_size
);
1888 std::rotate(__begin
, __middle
, __end
);
1889 std::advance(__begin
, std::distance(__middle
, __end
));
1895 * @brief Move elements for which a predicate is true to the beginning
1896 * of a sequence, preserving relative ordering.
1897 * @ingroup mutating_algorithms
1898 * @param first A forward iterator.
1899 * @param last A forward iterator.
1900 * @param pred A predicate functor.
1901 * @return An iterator @p middle such that @p pred(i) is true for each
1902 * iterator @p i in the range @p [first,middle) and false for each @p i
1903 * in the range @p [middle,last).
1905 * Performs the same function as @p partition() with the additional
1906 * guarantee that the relative ordering of elements in each group is
1907 * preserved, so any two elements @p x and @p y in the range
1908 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1909 * relative ordering after calling @p stable_partition().
1911 template<typename _ForwardIterator
, typename _Predicate
>
1913 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1916 // concept requirements
1917 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1919 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1920 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1921 __glibcxx_requires_valid_range(__first
, __last
);
1923 if (__first
== __last
)
1927 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1929 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1932 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1934 if (__buf
.size() > 0)
1936 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1937 _DistanceType(__buf
.requested_size()),
1939 _DistanceType(__buf
.size()));
1942 std::__inplace_stable_partition(__first
, __last
, __pred
,
1943 _DistanceType(__buf
.requested_size()));
1947 /// This is a helper function for the sort routines.
1948 template<typename _RandomAccessIterator
>
1950 __heap_select(_RandomAccessIterator __first
,
1951 _RandomAccessIterator __middle
,
1952 _RandomAccessIterator __last
)
1954 std::make_heap(__first
, __middle
);
1955 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1956 if (*__i
< *__first
)
1957 std::__pop_heap(__first
, __middle
, __i
);
1960 /// This is a helper function for the sort routines.
1961 template<typename _RandomAccessIterator
, typename _Compare
>
1963 __heap_select(_RandomAccessIterator __first
,
1964 _RandomAccessIterator __middle
,
1965 _RandomAccessIterator __last
, _Compare __comp
)
1967 std::make_heap(__first
, __middle
, __comp
);
1968 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1969 if (__comp(*__i
, *__first
))
1970 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1976 * @brief Copy the smallest elements of a sequence.
1977 * @ingroup sorting_algorithms
1978 * @param first An iterator.
1979 * @param last Another iterator.
1980 * @param result_first A random-access iterator.
1981 * @param result_last Another random-access iterator.
1982 * @return An iterator indicating the end of the resulting sequence.
1984 * Copies and sorts the smallest N values from the range @p [first,last)
1985 * to the range beginning at @p result_first, where the number of
1986 * elements to be copied, @p N, is the smaller of @p (last-first) and
1987 * @p (result_last-result_first).
1988 * After the sort if @p i and @j are iterators in the range
1989 * @p [result_first,result_first+N) such that @i precedes @j then
1990 * @p *j<*i is false.
1991 * The value returned is @p result_first+N.
1993 template<typename _InputIterator
, typename _RandomAccessIterator
>
1994 _RandomAccessIterator
1995 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1996 _RandomAccessIterator __result_first
,
1997 _RandomAccessIterator __result_last
)
1999 typedef typename iterator_traits
<_InputIterator
>::value_type
2001 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2003 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2006 // concept requirements
2007 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2008 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2010 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
2012 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2013 __glibcxx_requires_valid_range(__first
, __last
);
2014 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2016 if (__result_first
== __result_last
)
2017 return __result_last
;
2018 _RandomAccessIterator __result_real_last
= __result_first
;
2019 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2021 *__result_real_last
= *__first
;
2022 ++__result_real_last
;
2025 std::make_heap(__result_first
, __result_real_last
);
2026 while (__first
!= __last
)
2028 if (*__first
< *__result_first
)
2029 std::__adjust_heap(__result_first
, _DistanceType(0),
2030 _DistanceType(__result_real_last
2032 _InputValueType(*__first
));
2035 std::sort_heap(__result_first
, __result_real_last
);
2036 return __result_real_last
;
2040 * @brief Copy the smallest elements of a sequence using a predicate for
2042 * @ingroup sorting_algorithms
2043 * @param first An input iterator.
2044 * @param last Another input iterator.
2045 * @param result_first A random-access iterator.
2046 * @param result_last Another random-access iterator.
2047 * @param comp A comparison functor.
2048 * @return An iterator indicating the end of the resulting sequence.
2050 * Copies and sorts the smallest N values from the range @p [first,last)
2051 * to the range beginning at @p result_first, where the number of
2052 * elements to be copied, @p N, is the smaller of @p (last-first) and
2053 * @p (result_last-result_first).
2054 * After the sort if @p i and @j are iterators in the range
2055 * @p [result_first,result_first+N) such that @i precedes @j then
2056 * @p comp(*j,*i) is false.
2057 * The value returned is @p result_first+N.
2059 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2060 _RandomAccessIterator
2061 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2062 _RandomAccessIterator __result_first
,
2063 _RandomAccessIterator __result_last
,
2066 typedef typename iterator_traits
<_InputIterator
>::value_type
2068 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2070 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2073 // concept requirements
2074 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2075 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2076 _RandomAccessIterator
>)
2077 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2079 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2080 _InputValueType
, _OutputValueType
>)
2081 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2082 _OutputValueType
, _OutputValueType
>)
2083 __glibcxx_requires_valid_range(__first
, __last
);
2084 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2086 if (__result_first
== __result_last
)
2087 return __result_last
;
2088 _RandomAccessIterator __result_real_last
= __result_first
;
2089 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2091 *__result_real_last
= *__first
;
2092 ++__result_real_last
;
2095 std::make_heap(__result_first
, __result_real_last
, __comp
);
2096 while (__first
!= __last
)
2098 if (__comp(*__first
, *__result_first
))
2099 std::__adjust_heap(__result_first
, _DistanceType(0),
2100 _DistanceType(__result_real_last
2102 _InputValueType(*__first
),
2106 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2107 return __result_real_last
;
2110 /// This is a helper function for the sort routine.
2111 template<typename _RandomAccessIterator
>
2113 __unguarded_linear_insert(_RandomAccessIterator __last
)
2115 typename iterator_traits
<_RandomAccessIterator
>::value_type
2116 __val
= _GLIBCXX_MOVE(*__last
);
2117 _RandomAccessIterator __next
= __last
;
2119 while (__val
< *__next
)
2121 *__last
= _GLIBCXX_MOVE(*__next
);
2125 *__last
= _GLIBCXX_MOVE(__val
);
2128 /// This is a helper function for the sort routine.
2129 template<typename _RandomAccessIterator
, typename _Compare
>
2131 __unguarded_linear_insert(_RandomAccessIterator __last
,
2134 typename iterator_traits
<_RandomAccessIterator
>::value_type
2135 __val
= _GLIBCXX_MOVE(*__last
);
2136 _RandomAccessIterator __next
= __last
;
2138 while (__comp(__val
, *__next
))
2140 *__last
= _GLIBCXX_MOVE(*__next
);
2144 *__last
= _GLIBCXX_MOVE(__val
);
2147 /// This is a helper function for the sort routine.
2148 template<typename _RandomAccessIterator
>
2150 __insertion_sort(_RandomAccessIterator __first
,
2151 _RandomAccessIterator __last
)
2153 if (__first
== __last
)
2156 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2158 if (*__i
< *__first
)
2160 typename iterator_traits
<_RandomAccessIterator
>::value_type
2161 __val
= _GLIBCXX_MOVE(*__i
);
2162 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2163 *__first
= _GLIBCXX_MOVE(__val
);
2166 std::__unguarded_linear_insert(__i
);
2170 /// This is a helper function for the sort routine.
2171 template<typename _RandomAccessIterator
, typename _Compare
>
2173 __insertion_sort(_RandomAccessIterator __first
,
2174 _RandomAccessIterator __last
, _Compare __comp
)
2176 if (__first
== __last
) return;
2178 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2180 if (__comp(*__i
, *__first
))
2182 typename iterator_traits
<_RandomAccessIterator
>::value_type
2183 __val
= _GLIBCXX_MOVE(*__i
);
2184 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2185 *__first
= _GLIBCXX_MOVE(__val
);
2188 std::__unguarded_linear_insert(__i
, __comp
);
2192 /// This is a helper function for the sort routine.
2193 template<typename _RandomAccessIterator
>
2195 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2196 _RandomAccessIterator __last
)
2198 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2201 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2202 std::__unguarded_linear_insert(__i
);
2205 /// This is a helper function for the sort routine.
2206 template<typename _RandomAccessIterator
, typename _Compare
>
2208 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2209 _RandomAccessIterator __last
, _Compare __comp
)
2211 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2214 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2215 std::__unguarded_linear_insert(__i
, __comp
);
2220 * This controls some aspect of the sort routines.
2222 enum { _S_threshold
= 16 };
2224 /// This is a helper function for the sort routine.
2225 template<typename _RandomAccessIterator
>
2227 __final_insertion_sort(_RandomAccessIterator __first
,
2228 _RandomAccessIterator __last
)
2230 if (__last
- __first
> int(_S_threshold
))
2232 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2233 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2236 std::__insertion_sort(__first
, __last
);
2239 /// This is a helper function for the sort routine.
2240 template<typename _RandomAccessIterator
, typename _Compare
>
2242 __final_insertion_sort(_RandomAccessIterator __first
,
2243 _RandomAccessIterator __last
, _Compare __comp
)
2245 if (__last
- __first
> int(_S_threshold
))
2247 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2248 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2252 std::__insertion_sort(__first
, __last
, __comp
);
2255 /// This is a helper function...
2256 template<typename _RandomAccessIterator
, typename _Tp
>
2257 _RandomAccessIterator
2258 __unguarded_partition(_RandomAccessIterator __first
,
2259 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2263 while (*__first
< __pivot
)
2266 while (__pivot
< *__last
)
2268 if (!(__first
< __last
))
2270 std::iter_swap(__first
, __last
);
2275 /// This is a helper function...
2276 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2277 _RandomAccessIterator
2278 __unguarded_partition(_RandomAccessIterator __first
,
2279 _RandomAccessIterator __last
,
2280 const _Tp
& __pivot
, _Compare __comp
)
2284 while (__comp(*__first
, __pivot
))
2287 while (__comp(__pivot
, *__last
))
2289 if (!(__first
< __last
))
2291 std::iter_swap(__first
, __last
);
2296 /// This is a helper function...
2297 template<typename _RandomAccessIterator
>
2298 inline _RandomAccessIterator
2299 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2300 _RandomAccessIterator __last
)
2302 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2303 std::__move_median_first(__first
, __mid
, (__last
- 1));
2304 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2308 /// This is a helper function...
2309 template<typename _RandomAccessIterator
, typename _Compare
>
2310 inline _RandomAccessIterator
2311 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2312 _RandomAccessIterator __last
, _Compare __comp
)
2314 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2315 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2316 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2319 /// This is a helper function for the sort routine.
2320 template<typename _RandomAccessIterator
, typename _Size
>
2322 __introsort_loop(_RandomAccessIterator __first
,
2323 _RandomAccessIterator __last
,
2324 _Size __depth_limit
)
2326 while (__last
- __first
> int(_S_threshold
))
2328 if (__depth_limit
== 0)
2330 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2334 _RandomAccessIterator __cut
=
2335 std::__unguarded_partition_pivot(__first
, __last
);
2336 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2341 /// This is a helper function for the sort routine.
2342 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2344 __introsort_loop(_RandomAccessIterator __first
,
2345 _RandomAccessIterator __last
,
2346 _Size __depth_limit
, _Compare __comp
)
2348 while (__last
- __first
> int(_S_threshold
))
2350 if (__depth_limit
== 0)
2352 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2356 _RandomAccessIterator __cut
=
2357 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2358 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2363 /// This is a helper function for the sort routines. Precondition: __n > 0.
2364 template<typename _Size
>
2369 for (__k
= 0; __n
!= 0; __n
>>= 1)
2376 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2380 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2384 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2388 template<typename _RandomAccessIterator
, typename _Size
>
2390 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2391 _RandomAccessIterator __last
, _Size __depth_limit
)
2393 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2396 while (__last
- __first
> 3)
2398 if (__depth_limit
== 0)
2400 std::__heap_select(__first
, __nth
+ 1, __last
);
2402 // Place the nth largest element in its final position.
2403 std::iter_swap(__first
, __nth
);
2407 _RandomAccessIterator __cut
=
2408 std::__unguarded_partition_pivot(__first
, __last
);
2414 std::__insertion_sort(__first
, __last
);
2417 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2419 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2420 _RandomAccessIterator __last
, _Size __depth_limit
,
2423 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2426 while (__last
- __first
> 3)
2428 if (__depth_limit
== 0)
2430 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2431 // Place the nth largest element in its final position.
2432 std::iter_swap(__first
, __nth
);
2436 _RandomAccessIterator __cut
=
2437 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2443 std::__insertion_sort(__first
, __last
, __comp
);
2449 * @brief Finds the first position in which @a val could be inserted
2450 * without changing the ordering.
2451 * @param first An iterator.
2452 * @param last Another iterator.
2453 * @param val The search term.
2454 * @return An iterator pointing to the first element "not less
2455 * than" @a val, or end() if every element is less than
2457 * @ingroup binary_search_algorithms
2459 template<typename _ForwardIterator
, typename _Tp
>
2461 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2464 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2466 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2469 // concept requirements
2470 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2471 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2472 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2474 _DistanceType __len
= std::distance(__first
, __last
);
2475 _DistanceType __half
;
2476 _ForwardIterator __middle
;
2480 __half
= __len
>> 1;
2482 std::advance(__middle
, __half
);
2483 if (*__middle
< __val
)
2487 __len
= __len
- __half
- 1;
2496 * @brief Finds the first position in which @a val could be inserted
2497 * without changing the ordering.
2498 * @ingroup binary_search_algorithms
2499 * @param first An iterator.
2500 * @param last Another iterator.
2501 * @param val The search term.
2502 * @param comp A functor to use for comparisons.
2503 * @return An iterator pointing to the first element "not less than" @a val,
2504 * or end() if every element is less than @a val.
2505 * @ingroup binary_search_algorithms
2507 * The comparison function should have the same effects on ordering as
2508 * the function used for the initial sort.
2510 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2512 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2513 const _Tp
& __val
, _Compare __comp
)
2515 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2517 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2520 // concept requirements
2521 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2522 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2524 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2527 _DistanceType __len
= std::distance(__first
, __last
);
2528 _DistanceType __half
;
2529 _ForwardIterator __middle
;
2533 __half
= __len
>> 1;
2535 std::advance(__middle
, __half
);
2536 if (__comp(*__middle
, __val
))
2540 __len
= __len
- __half
- 1;
2549 * @brief Finds the last position in which @a val could be inserted
2550 * without changing the ordering.
2551 * @ingroup binary_search_algorithms
2552 * @param first An iterator.
2553 * @param last Another iterator.
2554 * @param val The search term.
2555 * @return An iterator pointing to the first element greater than @a val,
2556 * or end() if no elements are greater than @a val.
2557 * @ingroup binary_search_algorithms
2559 template<typename _ForwardIterator
, typename _Tp
>
2561 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2564 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2566 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2569 // concept requirements
2570 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2571 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2572 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2574 _DistanceType __len
= std::distance(__first
, __last
);
2575 _DistanceType __half
;
2576 _ForwardIterator __middle
;
2580 __half
= __len
>> 1;
2582 std::advance(__middle
, __half
);
2583 if (__val
< *__middle
)
2589 __len
= __len
- __half
- 1;
2596 * @brief Finds the last position in which @a val could be inserted
2597 * without changing the ordering.
2598 * @ingroup binary_search_algorithms
2599 * @param first An iterator.
2600 * @param last Another iterator.
2601 * @param val The search term.
2602 * @param comp A functor to use for comparisons.
2603 * @return An iterator pointing to the first element greater than @a val,
2604 * or end() if no elements are greater than @a val.
2605 * @ingroup binary_search_algorithms
2607 * The comparison function should have the same effects on ordering as
2608 * the function used for the initial sort.
2610 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2612 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2613 const _Tp
& __val
, _Compare __comp
)
2615 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2617 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2620 // concept requirements
2621 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2622 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2624 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2627 _DistanceType __len
= std::distance(__first
, __last
);
2628 _DistanceType __half
;
2629 _ForwardIterator __middle
;
2633 __half
= __len
>> 1;
2635 std::advance(__middle
, __half
);
2636 if (__comp(__val
, *__middle
))
2642 __len
= __len
- __half
- 1;
2649 * @brief Finds the largest subrange in which @a val could be inserted
2650 * at any place in it without changing the ordering.
2651 * @ingroup binary_search_algorithms
2652 * @param first An iterator.
2653 * @param last Another iterator.
2654 * @param val The search term.
2655 * @return An pair of iterators defining the subrange.
2656 * @ingroup binary_search_algorithms
2658 * This is equivalent to
2660 * std::make_pair(lower_bound(first, last, val),
2661 * upper_bound(first, last, val))
2663 * but does not actually call those functions.
2665 template<typename _ForwardIterator
, typename _Tp
>
2666 pair
<_ForwardIterator
, _ForwardIterator
>
2667 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2670 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2672 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2675 // concept requirements
2676 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2677 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2678 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2679 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2680 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2682 _DistanceType __len
= std::distance(__first
, __last
);
2683 _DistanceType __half
;
2684 _ForwardIterator __middle
, __left
, __right
;
2688 __half
= __len
>> 1;
2690 std::advance(__middle
, __half
);
2691 if (*__middle
< __val
)
2695 __len
= __len
- __half
- 1;
2697 else if (__val
< *__middle
)
2701 __left
= std::lower_bound(__first
, __middle
, __val
);
2702 std::advance(__first
, __len
);
2703 __right
= std::upper_bound(++__middle
, __first
, __val
);
2704 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2707 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2711 * @brief Finds the largest subrange in which @a val could be inserted
2712 * at any place in it without changing the ordering.
2713 * @param first An iterator.
2714 * @param last Another iterator.
2715 * @param val The search term.
2716 * @param comp A functor to use for comparisons.
2717 * @return An pair of iterators defining the subrange.
2718 * @ingroup binary_search_algorithms
2720 * This is equivalent to
2722 * std::make_pair(lower_bound(first, last, val, comp),
2723 * upper_bound(first, last, val, comp))
2725 * but does not actually call those functions.
2727 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2728 pair
<_ForwardIterator
, _ForwardIterator
>
2729 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2733 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2735 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2738 // concept requirements
2739 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2740 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2742 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2744 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2746 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2749 _DistanceType __len
= std::distance(__first
, __last
);
2750 _DistanceType __half
;
2751 _ForwardIterator __middle
, __left
, __right
;
2755 __half
= __len
>> 1;
2757 std::advance(__middle
, __half
);
2758 if (__comp(*__middle
, __val
))
2762 __len
= __len
- __half
- 1;
2764 else if (__comp(__val
, *__middle
))
2768 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2769 std::advance(__first
, __len
);
2770 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2771 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2774 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2778 * @brief Determines whether an element exists in a range.
2779 * @ingroup binary_search_algorithms
2780 * @param first An iterator.
2781 * @param last Another iterator.
2782 * @param val The search term.
2783 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2785 * Note that this does not actually return an iterator to @a val. For
2786 * that, use std::find or a container's specialized find member functions.
2788 template<typename _ForwardIterator
, typename _Tp
>
2790 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2793 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2796 // concept requirements
2797 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2798 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2799 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2800 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2802 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2803 return __i
!= __last
&& !(__val
< *__i
);
2807 * @brief Determines whether an element exists in a range.
2808 * @ingroup binary_search_algorithms
2809 * @param first An iterator.
2810 * @param last Another iterator.
2811 * @param val The search term.
2812 * @param comp A functor to use for comparisons.
2813 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2815 * Note that this does not actually return an iterator to @a val. For
2816 * that, use std::find or a container's specialized find member functions.
2818 * The comparison function should have the same effects on ordering as
2819 * the function used for the initial sort.
2821 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2823 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2824 const _Tp
& __val
, _Compare __comp
)
2826 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2829 // concept requirements
2830 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2831 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2833 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2835 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2838 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2839 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2844 /// This is a helper function for the merge routines.
2845 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2846 typename _BidirectionalIterator3
>
2847 _BidirectionalIterator3
2848 __merge_backward(_BidirectionalIterator1 __first1
,
2849 _BidirectionalIterator1 __last1
,
2850 _BidirectionalIterator2 __first2
,
2851 _BidirectionalIterator2 __last2
,
2852 _BidirectionalIterator3 __result
)
2854 if (__first1
== __last1
)
2855 return std::copy_backward(__first2
, __last2
, __result
);
2856 if (__first2
== __last2
)
2857 return std::copy_backward(__first1
, __last1
, __result
);
2862 if (*__last2
< *__last1
)
2864 *--__result
= *__last1
;
2865 if (__first1
== __last1
)
2866 return std::copy_backward(__first2
, ++__last2
, __result
);
2871 *--__result
= *__last2
;
2872 if (__first2
== __last2
)
2873 return std::copy_backward(__first1
, ++__last1
, __result
);
2879 /// This is a helper function for the merge routines.
2880 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2881 typename _BidirectionalIterator3
, typename _Compare
>
2882 _BidirectionalIterator3
2883 __merge_backward(_BidirectionalIterator1 __first1
,
2884 _BidirectionalIterator1 __last1
,
2885 _BidirectionalIterator2 __first2
,
2886 _BidirectionalIterator2 __last2
,
2887 _BidirectionalIterator3 __result
,
2890 if (__first1
== __last1
)
2891 return std::copy_backward(__first2
, __last2
, __result
);
2892 if (__first2
== __last2
)
2893 return std::copy_backward(__first1
, __last1
, __result
);
2898 if (__comp(*__last2
, *__last1
))
2900 *--__result
= *__last1
;
2901 if (__first1
== __last1
)
2902 return std::copy_backward(__first2
, ++__last2
, __result
);
2907 *--__result
= *__last2
;
2908 if (__first2
== __last2
)
2909 return std::copy_backward(__first1
, ++__last1
, __result
);
2915 /// This is a helper function for the merge routines.
2916 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2918 _BidirectionalIterator1
2919 __rotate_adaptive(_BidirectionalIterator1 __first
,
2920 _BidirectionalIterator1 __middle
,
2921 _BidirectionalIterator1 __last
,
2922 _Distance __len1
, _Distance __len2
,
2923 _BidirectionalIterator2 __buffer
,
2924 _Distance __buffer_size
)
2926 _BidirectionalIterator2 __buffer_end
;
2927 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2929 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2930 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2931 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2933 else if (__len1
<= __buffer_size
)
2935 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2936 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2937 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2941 std::rotate(__first
, __middle
, __last
);
2942 std::advance(__first
, std::distance(__middle
, __last
));
2947 /// This is a helper function for the merge routines.
2948 template<typename _BidirectionalIterator
, typename _Distance
,
2951 __merge_adaptive(_BidirectionalIterator __first
,
2952 _BidirectionalIterator __middle
,
2953 _BidirectionalIterator __last
,
2954 _Distance __len1
, _Distance __len2
,
2955 _Pointer __buffer
, _Distance __buffer_size
)
2957 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2959 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2960 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
2961 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
2962 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
2963 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
2966 else if (__len2
<= __buffer_size
)
2968 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2969 std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
2970 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
2971 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
2972 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
2977 _BidirectionalIterator __first_cut
= __first
;
2978 _BidirectionalIterator __second_cut
= __middle
;
2979 _Distance __len11
= 0;
2980 _Distance __len22
= 0;
2981 if (__len1
> __len2
)
2983 __len11
= __len1
/ 2;
2984 std::advance(__first_cut
, __len11
);
2985 __second_cut
= std::lower_bound(__middle
, __last
,
2987 __len22
= std::distance(__middle
, __second_cut
);
2991 __len22
= __len2
/ 2;
2992 std::advance(__second_cut
, __len22
);
2993 __first_cut
= std::upper_bound(__first
, __middle
,
2995 __len11
= std::distance(__first
, __first_cut
);
2997 _BidirectionalIterator __new_middle
=
2998 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2999 __len1
- __len11
, __len22
, __buffer
,
3001 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3002 __len22
, __buffer
, __buffer_size
);
3003 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3005 __len2
- __len22
, __buffer
, __buffer_size
);
3009 /// This is a helper function for the merge routines.
3010 template<typename _BidirectionalIterator
, typename _Distance
,
3011 typename _Pointer
, typename _Compare
>
3013 __merge_adaptive(_BidirectionalIterator __first
,
3014 _BidirectionalIterator __middle
,
3015 _BidirectionalIterator __last
,
3016 _Distance __len1
, _Distance __len2
,
3017 _Pointer __buffer
, _Distance __buffer_size
,
3020 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3022 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
3023 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
3024 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
3025 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
3026 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
3029 else if (__len2
<= __buffer_size
)
3031 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
3032 std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3033 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle
),
3034 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer
),
3035 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end
),
3040 _BidirectionalIterator __first_cut
= __first
;
3041 _BidirectionalIterator __second_cut
= __middle
;
3042 _Distance __len11
= 0;
3043 _Distance __len22
= 0;
3044 if (__len1
> __len2
)
3046 __len11
= __len1
/ 2;
3047 std::advance(__first_cut
, __len11
);
3048 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3050 __len22
= std::distance(__middle
, __second_cut
);
3054 __len22
= __len2
/ 2;
3055 std::advance(__second_cut
, __len22
);
3056 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3058 __len11
= std::distance(__first
, __first_cut
);
3060 _BidirectionalIterator __new_middle
=
3061 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3062 __len1
- __len11
, __len22
, __buffer
,
3064 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3065 __len22
, __buffer
, __buffer_size
, __comp
);
3066 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3068 __len2
- __len22
, __buffer
,
3069 __buffer_size
, __comp
);
3073 /// This is a helper function for the merge routines.
3074 template<typename _BidirectionalIterator
, typename _Distance
>
3076 __merge_without_buffer(_BidirectionalIterator __first
,
3077 _BidirectionalIterator __middle
,
3078 _BidirectionalIterator __last
,
3079 _Distance __len1
, _Distance __len2
)
3081 if (__len1
== 0 || __len2
== 0)
3083 if (__len1
+ __len2
== 2)
3085 if (*__middle
< *__first
)
3086 std::iter_swap(__first
, __middle
);
3089 _BidirectionalIterator __first_cut
= __first
;
3090 _BidirectionalIterator __second_cut
= __middle
;
3091 _Distance __len11
= 0;
3092 _Distance __len22
= 0;
3093 if (__len1
> __len2
)
3095 __len11
= __len1
/ 2;
3096 std::advance(__first_cut
, __len11
);
3097 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3098 __len22
= std::distance(__middle
, __second_cut
);
3102 __len22
= __len2
/ 2;
3103 std::advance(__second_cut
, __len22
);
3104 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3105 __len11
= std::distance(__first
, __first_cut
);
3107 std::rotate(__first_cut
, __middle
, __second_cut
);
3108 _BidirectionalIterator __new_middle
= __first_cut
;
3109 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3110 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3112 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3113 __len1
- __len11
, __len2
- __len22
);
3116 /// This is a helper function for the merge routines.
3117 template<typename _BidirectionalIterator
, typename _Distance
,
3120 __merge_without_buffer(_BidirectionalIterator __first
,
3121 _BidirectionalIterator __middle
,
3122 _BidirectionalIterator __last
,
3123 _Distance __len1
, _Distance __len2
,
3126 if (__len1
== 0 || __len2
== 0)
3128 if (__len1
+ __len2
== 2)
3130 if (__comp(*__middle
, *__first
))
3131 std::iter_swap(__first
, __middle
);
3134 _BidirectionalIterator __first_cut
= __first
;
3135 _BidirectionalIterator __second_cut
= __middle
;
3136 _Distance __len11
= 0;
3137 _Distance __len22
= 0;
3138 if (__len1
> __len2
)
3140 __len11
= __len1
/ 2;
3141 std::advance(__first_cut
, __len11
);
3142 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3144 __len22
= std::distance(__middle
, __second_cut
);
3148 __len22
= __len2
/ 2;
3149 std::advance(__second_cut
, __len22
);
3150 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3152 __len11
= std::distance(__first
, __first_cut
);
3154 std::rotate(__first_cut
, __middle
, __second_cut
);
3155 _BidirectionalIterator __new_middle
= __first_cut
;
3156 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3157 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3158 __len11
, __len22
, __comp
);
3159 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3160 __len1
- __len11
, __len2
- __len22
, __comp
);
3164 * @brief Merges two sorted ranges in place.
3165 * @ingroup sorting_algorithms
3166 * @param first An iterator.
3167 * @param middle Another iterator.
3168 * @param last Another iterator.
3171 * Merges two sorted and consecutive ranges, [first,middle) and
3172 * [middle,last), and puts the result in [first,last). The output will
3173 * be sorted. The sort is @e stable, that is, for equivalent
3174 * elements in the two ranges, elements from the first range will always
3175 * come before elements from the second.
3177 * If enough additional memory is available, this takes (last-first)-1
3178 * comparisons. Otherwise an NlogN algorithm is used, where N is
3179 * distance(first,last).
3181 template<typename _BidirectionalIterator
>
3183 inplace_merge(_BidirectionalIterator __first
,
3184 _BidirectionalIterator __middle
,
3185 _BidirectionalIterator __last
)
3187 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3189 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3192 // concept requirements
3193 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3194 _BidirectionalIterator
>)
3195 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3196 __glibcxx_requires_sorted(__first
, __middle
);
3197 __glibcxx_requires_sorted(__middle
, __last
);
3199 if (__first
== __middle
|| __middle
== __last
)
3202 _DistanceType __len1
= std::distance(__first
, __middle
);
3203 _DistanceType __len2
= std::distance(__middle
, __last
);
3205 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3207 if (__buf
.begin() == 0)
3208 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3210 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3211 __buf
.begin(), _DistanceType(__buf
.size()));
3215 * @brief Merges two sorted ranges in place.
3216 * @ingroup sorting_algorithms
3217 * @param first An iterator.
3218 * @param middle Another iterator.
3219 * @param last Another iterator.
3220 * @param comp A functor to use for comparisons.
3223 * Merges two sorted and consecutive ranges, [first,middle) and
3224 * [middle,last), and puts the result in [first,last). The output will
3225 * be sorted. The sort is @e stable, that is, for equivalent
3226 * elements in the two ranges, elements from the first range will always
3227 * come before elements from the second.
3229 * If enough additional memory is available, this takes (last-first)-1
3230 * comparisons. Otherwise an NlogN algorithm is used, where N is
3231 * distance(first,last).
3233 * The comparison function should have the same effects on ordering as
3234 * the function used for the initial sort.
3236 template<typename _BidirectionalIterator
, typename _Compare
>
3238 inplace_merge(_BidirectionalIterator __first
,
3239 _BidirectionalIterator __middle
,
3240 _BidirectionalIterator __last
,
3243 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3245 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3248 // concept requirements
3249 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3250 _BidirectionalIterator
>)
3251 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3252 _ValueType
, _ValueType
>)
3253 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3254 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3256 if (__first
== __middle
|| __middle
== __last
)
3259 const _DistanceType __len1
= std::distance(__first
, __middle
);
3260 const _DistanceType __len2
= std::distance(__middle
, __last
);
3262 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3264 if (__buf
.begin() == 0)
3265 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3268 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3269 __buf
.begin(), _DistanceType(__buf
.size()),
3273 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3276 __merge_sort_loop(_RandomAccessIterator1 __first
,
3277 _RandomAccessIterator1 __last
,
3278 _RandomAccessIterator2 __result
,
3279 _Distance __step_size
)
3281 const _Distance __two_step
= 2 * __step_size
;
3283 while (__last
- __first
>= __two_step
)
3285 __result
= _GLIBCXX_STD_P::merge(
3286 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3287 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3288 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3289 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __two_step
),
3291 __first
+= __two_step
;
3294 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3295 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3296 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3298 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3300 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
3304 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3305 typename _Distance
, typename _Compare
>
3307 __merge_sort_loop(_RandomAccessIterator1 __first
,
3308 _RandomAccessIterator1 __last
,
3309 _RandomAccessIterator2 __result
, _Distance __step_size
,
3312 const _Distance __two_step
= 2 * __step_size
;
3314 while (__last
- __first
>= __two_step
)
3316 __result
= _GLIBCXX_STD_P::merge(
3317 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3318 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3319 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __step_size
),
3320 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+ __two_step
),
3322 __first
+= __two_step
;
3324 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3326 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
3327 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3329 _GLIBCXX_MAKE_MOVE_ITERATOR(__first
+
3331 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
3335 template<typename _RandomAccessIterator
, typename _Distance
>
3337 __chunk_insertion_sort(_RandomAccessIterator __first
,
3338 _RandomAccessIterator __last
,
3339 _Distance __chunk_size
)
3341 while (__last
- __first
>= __chunk_size
)
3343 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3344 __first
+= __chunk_size
;
3346 std::__insertion_sort(__first
, __last
);
3349 template<typename _RandomAccessIterator
, typename _Distance
,
3352 __chunk_insertion_sort(_RandomAccessIterator __first
,
3353 _RandomAccessIterator __last
,
3354 _Distance __chunk_size
, _Compare __comp
)
3356 while (__last
- __first
>= __chunk_size
)
3358 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3359 __first
+= __chunk_size
;
3361 std::__insertion_sort(__first
, __last
, __comp
);
3364 enum { _S_chunk_size
= 7 };
3366 template<typename _RandomAccessIterator
, typename _Pointer
>
3368 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3369 _RandomAccessIterator __last
,
3372 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3375 const _Distance __len
= __last
- __first
;
3376 const _Pointer __buffer_last
= __buffer
+ __len
;
3378 _Distance __step_size
= _S_chunk_size
;
3379 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3381 while (__step_size
< __len
)
3383 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3385 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3390 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3392 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3393 _RandomAccessIterator __last
,
3394 _Pointer __buffer
, _Compare __comp
)
3396 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3399 const _Distance __len
= __last
- __first
;
3400 const _Pointer __buffer_last
= __buffer
+ __len
;
3402 _Distance __step_size
= _S_chunk_size
;
3403 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3405 while (__step_size
< __len
)
3407 std::__merge_sort_loop(__first
, __last
, __buffer
,
3408 __step_size
, __comp
);
3410 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3411 __step_size
, __comp
);
3416 template<typename _RandomAccessIterator
, typename _Pointer
,
3419 __stable_sort_adaptive(_RandomAccessIterator __first
,
3420 _RandomAccessIterator __last
,
3421 _Pointer __buffer
, _Distance __buffer_size
)
3423 const _Distance __len
= (__last
- __first
+ 1) / 2;
3424 const _RandomAccessIterator __middle
= __first
+ __len
;
3425 if (__len
> __buffer_size
)
3427 std::__stable_sort_adaptive(__first
, __middle
,
3428 __buffer
, __buffer_size
);
3429 std::__stable_sort_adaptive(__middle
, __last
,
3430 __buffer
, __buffer_size
);
3434 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3435 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3437 std::__merge_adaptive(__first
, __middle
, __last
,
3438 _Distance(__middle
- __first
),
3439 _Distance(__last
- __middle
),
3440 __buffer
, __buffer_size
);
3443 template<typename _RandomAccessIterator
, typename _Pointer
,
3444 typename _Distance
, typename _Compare
>
3446 __stable_sort_adaptive(_RandomAccessIterator __first
,
3447 _RandomAccessIterator __last
,
3448 _Pointer __buffer
, _Distance __buffer_size
,
3451 const _Distance __len
= (__last
- __first
+ 1) / 2;
3452 const _RandomAccessIterator __middle
= __first
+ __len
;
3453 if (__len
> __buffer_size
)
3455 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3456 __buffer_size
, __comp
);
3457 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3458 __buffer_size
, __comp
);
3462 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3463 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3465 std::__merge_adaptive(__first
, __middle
, __last
,
3466 _Distance(__middle
- __first
),
3467 _Distance(__last
- __middle
),
3468 __buffer
, __buffer_size
,
3472 /// This is a helper function for the stable sorting routines.
3473 template<typename _RandomAccessIterator
>
3475 __inplace_stable_sort(_RandomAccessIterator __first
,
3476 _RandomAccessIterator __last
)
3478 if (__last
- __first
< 15)
3480 std::__insertion_sort(__first
, __last
);
3483 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3484 std::__inplace_stable_sort(__first
, __middle
);
3485 std::__inplace_stable_sort(__middle
, __last
);
3486 std::__merge_without_buffer(__first
, __middle
, __last
,
3491 /// This is a helper function for the stable sorting routines.
3492 template<typename _RandomAccessIterator
, typename _Compare
>
3494 __inplace_stable_sort(_RandomAccessIterator __first
,
3495 _RandomAccessIterator __last
, _Compare __comp
)
3497 if (__last
- __first
< 15)
3499 std::__insertion_sort(__first
, __last
, __comp
);
3502 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3503 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3504 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3505 std::__merge_without_buffer(__first
, __middle
, __last
,
3513 // Set algorithms: includes, set_union, set_intersection, set_difference,
3514 // set_symmetric_difference. All of these algorithms have the precondition
3515 // that their input ranges are sorted and the postcondition that their output
3516 // ranges are sorted.
3519 * @brief Determines whether all elements of a sequence exists in a range.
3520 * @param first1 Start of search range.
3521 * @param last1 End of search range.
3522 * @param first2 Start of sequence
3523 * @param last2 End of sequence.
3524 * @return True if each element in [first2,last2) is contained in order
3525 * within [first1,last1). False otherwise.
3526 * @ingroup set_algorithms
3528 * This operation expects both [first1,last1) and [first2,last2) to be
3529 * sorted. Searches for the presence of each element in [first2,last2)
3530 * within [first1,last1). The iterators over each range only move forward,
3531 * so this is a linear algorithm. If an element in [first2,last2) is not
3532 * found before the search iterator reaches @a last2, false is returned.
3534 template<typename _InputIterator1
, typename _InputIterator2
>
3536 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3537 _InputIterator2 __first2
, _InputIterator2 __last2
)
3539 typedef typename iterator_traits
<_InputIterator1
>::value_type
3541 typedef typename iterator_traits
<_InputIterator2
>::value_type
3544 // concept requirements
3545 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3546 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3547 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3548 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3549 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3550 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3552 while (__first1
!= __last1
&& __first2
!= __last2
)
3553 if (*__first2
< *__first1
)
3555 else if(*__first1
< *__first2
)
3558 ++__first1
, ++__first2
;
3560 return __first2
== __last2
;
3564 * @brief Determines whether all elements of a sequence exists in a range
3566 * @ingroup set_algorithms
3567 * @param first1 Start of search range.
3568 * @param last1 End of search range.
3569 * @param first2 Start of sequence
3570 * @param last2 End of sequence.
3571 * @param comp Comparison function to use.
3572 * @return True if each element in [first2,last2) is contained in order
3573 * within [first1,last1) according to comp. False otherwise.
3574 * @ingroup set_algorithms
3576 * This operation expects both [first1,last1) and [first2,last2) to be
3577 * sorted. Searches for the presence of each element in [first2,last2)
3578 * within [first1,last1), using comp to decide. The iterators over each
3579 * range only move forward, so this is a linear algorithm. If an element
3580 * in [first2,last2) is not found before the search iterator reaches @a
3581 * last2, false is returned.
3583 template<typename _InputIterator1
, typename _InputIterator2
,
3586 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3587 _InputIterator2 __first2
, _InputIterator2 __last2
,
3590 typedef typename iterator_traits
<_InputIterator1
>::value_type
3592 typedef typename iterator_traits
<_InputIterator2
>::value_type
3595 // concept requirements
3596 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3597 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3598 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3599 _ValueType1
, _ValueType2
>)
3600 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3601 _ValueType2
, _ValueType1
>)
3602 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3603 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3605 while (__first1
!= __last1
&& __first2
!= __last2
)
3606 if (__comp(*__first2
, *__first1
))
3608 else if(__comp(*__first1
, *__first2
))
3611 ++__first1
, ++__first2
;
3613 return __first2
== __last2
;
3622 // set_symmetric_difference
3627 * @brief Permute range into the next "dictionary" ordering.
3628 * @ingroup sorting_algorithms
3629 * @param first Start of range.
3630 * @param last End of range.
3631 * @return False if wrapped to first permutation, true otherwise.
3633 * Treats all permutations of the range as a set of "dictionary" sorted
3634 * sequences. Permutes the current sequence into the next one of this set.
3635 * Returns true if there are more sequences to generate. If the sequence
3636 * is the largest of the set, the smallest is generated and false returned.
3638 template<typename _BidirectionalIterator
>
3640 next_permutation(_BidirectionalIterator __first
,
3641 _BidirectionalIterator __last
)
3643 // concept requirements
3644 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3645 _BidirectionalIterator
>)
3646 __glibcxx_function_requires(_LessThanComparableConcept
<
3647 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3648 __glibcxx_requires_valid_range(__first
, __last
);
3650 if (__first
== __last
)
3652 _BidirectionalIterator __i
= __first
;
3661 _BidirectionalIterator __ii
= __i
;
3665 _BidirectionalIterator __j
= __last
;
3666 while (!(*__i
< *--__j
))
3668 std::iter_swap(__i
, __j
);
3669 std::reverse(__ii
, __last
);
3674 std::reverse(__first
, __last
);
3681 * @brief Permute range into the next "dictionary" ordering using
3682 * comparison functor.
3683 * @ingroup sorting_algorithms
3684 * @param first Start of range.
3685 * @param last End of range.
3686 * @param comp A comparison functor.
3687 * @return False if wrapped to first permutation, true otherwise.
3689 * Treats all permutations of the range [first,last) as a set of
3690 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3691 * sequence into the next one of this set. Returns true if there are more
3692 * sequences to generate. If the sequence is the largest of the set, the
3693 * smallest is generated and false returned.
3695 template<typename _BidirectionalIterator
, typename _Compare
>
3697 next_permutation(_BidirectionalIterator __first
,
3698 _BidirectionalIterator __last
, _Compare __comp
)
3700 // concept requirements
3701 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3702 _BidirectionalIterator
>)
3703 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3704 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3705 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3706 __glibcxx_requires_valid_range(__first
, __last
);
3708 if (__first
== __last
)
3710 _BidirectionalIterator __i
= __first
;
3719 _BidirectionalIterator __ii
= __i
;
3721 if (__comp(*__i
, *__ii
))
3723 _BidirectionalIterator __j
= __last
;
3724 while (!bool(__comp(*__i
, *--__j
)))
3726 std::iter_swap(__i
, __j
);
3727 std::reverse(__ii
, __last
);
3732 std::reverse(__first
, __last
);
3739 * @brief Permute range into the previous "dictionary" ordering.
3740 * @ingroup sorting_algorithms
3741 * @param first Start of range.
3742 * @param last End of range.
3743 * @return False if wrapped to last permutation, true otherwise.
3745 * Treats all permutations of the range as a set of "dictionary" sorted
3746 * sequences. Permutes the current sequence into the previous one of this
3747 * set. Returns true if there are more sequences to generate. If the
3748 * sequence is the smallest of the set, the largest is generated and false
3751 template<typename _BidirectionalIterator
>
3753 prev_permutation(_BidirectionalIterator __first
,
3754 _BidirectionalIterator __last
)
3756 // concept requirements
3757 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3758 _BidirectionalIterator
>)
3759 __glibcxx_function_requires(_LessThanComparableConcept
<
3760 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3761 __glibcxx_requires_valid_range(__first
, __last
);
3763 if (__first
== __last
)
3765 _BidirectionalIterator __i
= __first
;
3774 _BidirectionalIterator __ii
= __i
;
3778 _BidirectionalIterator __j
= __last
;
3779 while (!(*--__j
< *__i
))
3781 std::iter_swap(__i
, __j
);
3782 std::reverse(__ii
, __last
);
3787 std::reverse(__first
, __last
);
3794 * @brief Permute range into the previous "dictionary" ordering using
3795 * comparison functor.
3796 * @ingroup sorting_algorithms
3797 * @param first Start of range.
3798 * @param last End of range.
3799 * @param comp A comparison functor.
3800 * @return False if wrapped to last permutation, true otherwise.
3802 * Treats all permutations of the range [first,last) as a set of
3803 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3804 * sequence into the previous one of this set. Returns true if there are
3805 * more sequences to generate. If the sequence is the smallest of the set,
3806 * the largest is generated and false returned.
3808 template<typename _BidirectionalIterator
, typename _Compare
>
3810 prev_permutation(_BidirectionalIterator __first
,
3811 _BidirectionalIterator __last
, _Compare __comp
)
3813 // concept requirements
3814 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3815 _BidirectionalIterator
>)
3816 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3817 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3818 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3819 __glibcxx_requires_valid_range(__first
, __last
);
3821 if (__first
== __last
)
3823 _BidirectionalIterator __i
= __first
;
3832 _BidirectionalIterator __ii
= __i
;
3834 if (__comp(*__ii
, *__i
))
3836 _BidirectionalIterator __j
= __last
;
3837 while (!bool(__comp(*--__j
, *__i
)))
3839 std::iter_swap(__i
, __j
);
3840 std::reverse(__ii
, __last
);
3845 std::reverse(__first
, __last
);
3855 * @brief Copy a sequence, replacing each element of one value with another
3857 * @param first An input iterator.
3858 * @param last An input iterator.
3859 * @param result An output iterator.
3860 * @param old_value The value to be replaced.
3861 * @param new_value The replacement value.
3862 * @return The end of the output sequence, @p result+(last-first).
3864 * Copies each element in the input range @p [first,last) to the
3865 * output range @p [result,result+(last-first)) replacing elements
3866 * equal to @p old_value with @p new_value.
3868 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3870 replace_copy(_InputIterator __first
, _InputIterator __last
,
3871 _OutputIterator __result
,
3872 const _Tp
& __old_value
, const _Tp
& __new_value
)
3874 // concept requirements
3875 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3876 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3877 typename iterator_traits
<_InputIterator
>::value_type
>)
3878 __glibcxx_function_requires(_EqualOpConcept
<
3879 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3880 __glibcxx_requires_valid_range(__first
, __last
);
3882 for (; __first
!= __last
; ++__first
, ++__result
)
3883 if (*__first
== __old_value
)
3884 *__result
= __new_value
;
3886 *__result
= *__first
;
3891 * @brief Copy a sequence, replacing each value for which a predicate
3892 * returns true with another value.
3893 * @ingroup mutating_algorithms
3894 * @param first An input iterator.
3895 * @param last An input iterator.
3896 * @param result An output iterator.
3897 * @param pred A predicate.
3898 * @param new_value The replacement value.
3899 * @return The end of the output sequence, @p result+(last-first).
3901 * Copies each element in the range @p [first,last) to the range
3902 * @p [result,result+(last-first)) replacing elements for which
3903 * @p pred returns true with @p new_value.
3905 template<typename _InputIterator
, typename _OutputIterator
,
3906 typename _Predicate
, typename _Tp
>
3908 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3909 _OutputIterator __result
,
3910 _Predicate __pred
, const _Tp
& __new_value
)
3912 // concept requirements
3913 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3914 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3915 typename iterator_traits
<_InputIterator
>::value_type
>)
3916 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3917 typename iterator_traits
<_InputIterator
>::value_type
>)
3918 __glibcxx_requires_valid_range(__first
, __last
);
3920 for (; __first
!= __last
; ++__first
, ++__result
)
3921 if (__pred(*__first
))
3922 *__result
= __new_value
;
3924 *__result
= *__first
;
3928 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3930 * @brief Determines whether the elements of a sequence are sorted.
3931 * @ingroup sorting_algorithms
3932 * @param first An iterator.
3933 * @param last Another iterator.
3934 * @return True if the elements are sorted, false otherwise.
3936 template<typename _ForwardIterator
>
3938 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3939 { return std::is_sorted_until(__first
, __last
) == __last
; }
3942 * @brief Determines whether the elements of a sequence are sorted
3943 * according to a comparison functor.
3944 * @ingroup sorting_algorithms
3945 * @param first An iterator.
3946 * @param last Another iterator.
3947 * @param comp A comparison functor.
3948 * @return True if the elements are sorted, false otherwise.
3950 template<typename _ForwardIterator
, typename _Compare
>
3952 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3954 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3957 * @brief Determines the end of a sorted sequence.
3958 * @ingroup sorting_algorithms
3959 * @param first An iterator.
3960 * @param last Another iterator.
3961 * @return An iterator pointing to the last iterator i in [first, last)
3962 * for which the range [first, i) is sorted.
3964 template<typename _ForwardIterator
>
3966 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3968 // concept requirements
3969 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3970 __glibcxx_function_requires(_LessThanComparableConcept
<
3971 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3972 __glibcxx_requires_valid_range(__first
, __last
);
3974 if (__first
== __last
)
3977 _ForwardIterator __next
= __first
;
3978 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3979 if (*__next
< *__first
)
3985 * @brief Determines the end of a sorted sequence using comparison functor.
3986 * @ingroup sorting_algorithms
3987 * @param first An iterator.
3988 * @param last Another iterator.
3989 * @param comp A comparison functor.
3990 * @return An iterator pointing to the last iterator i in [first, last)
3991 * for which the range [first, i) is sorted.
3993 template<typename _ForwardIterator
, typename _Compare
>
3995 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3998 // concept requirements
3999 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4000 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4001 typename iterator_traits
<_ForwardIterator
>::value_type
,
4002 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4003 __glibcxx_requires_valid_range(__first
, __last
);
4005 if (__first
== __last
)
4008 _ForwardIterator __next
= __first
;
4009 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
4010 if (__comp(*__next
, *__first
))
4016 * @brief Determines min and max at once as an ordered pair.
4017 * @ingroup sorting_algorithms
4018 * @param a A thing of arbitrary type.
4019 * @param b Another thing of arbitrary type.
4020 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
4022 template<typename _Tp
>
4023 inline pair
<const _Tp
&, const _Tp
&>
4024 minmax(const _Tp
& __a
, const _Tp
& __b
)
4026 // concept requirements
4027 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
4029 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4030 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4034 * @brief Determines min and max at once as an ordered pair.
4035 * @ingroup sorting_algorithms
4036 * @param a A thing of arbitrary type.
4037 * @param b Another thing of arbitrary type.
4038 * @param comp A @link comparison_functor comparison functor@endlink.
4039 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
4041 template<typename _Tp
, typename _Compare
>
4042 inline pair
<const _Tp
&, const _Tp
&>
4043 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
4045 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4046 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4050 * @brief Return a pair of iterators pointing to the minimum and maximum
4051 * elements in a range.
4052 * @ingroup sorting_algorithms
4053 * @param first Start of range.
4054 * @param last End of range.
4055 * @return make_pair(m, M), where m is the first iterator i in
4056 * [first, last) such that no other element in the range is
4057 * smaller, and where M is the last iterator i in [first, last)
4058 * such that no other element in the range is larger.
4060 template<typename _ForwardIterator
>
4061 pair
<_ForwardIterator
, _ForwardIterator
>
4062 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4064 // concept requirements
4065 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4066 __glibcxx_function_requires(_LessThanComparableConcept
<
4067 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4068 __glibcxx_requires_valid_range(__first
, __last
);
4070 _ForwardIterator __next
= __first
;
4071 if (__first
== __last
4072 || ++__next
== __last
)
4073 return std::make_pair(__first
, __first
);
4075 _ForwardIterator __min
, __max
;
4076 if (*__next
< *__first
)
4090 while (__first
!= __last
)
4093 if (++__next
== __last
)
4095 if (*__first
< *__min
)
4097 else if (!(*__first
< *__max
))
4102 if (*__next
< *__first
)
4104 if (*__next
< *__min
)
4106 if (!(*__first
< *__max
))
4111 if (*__first
< *__min
)
4113 if (!(*__next
< *__max
))
4121 return std::make_pair(__min
, __max
);
4125 * @brief Return a pair of iterators pointing to the minimum and maximum
4126 * elements in a range.
4127 * @ingroup sorting_algorithms
4128 * @param first Start of range.
4129 * @param last End of range.
4130 * @param comp Comparison functor.
4131 * @return make_pair(m, M), where m is the first iterator i in
4132 * [first, last) such that no other element in the range is
4133 * smaller, and where M is the last iterator i in [first, last)
4134 * such that no other element in the range is larger.
4136 template<typename _ForwardIterator
, typename _Compare
>
4137 pair
<_ForwardIterator
, _ForwardIterator
>
4138 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4141 // concept requirements
4142 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4143 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4144 typename iterator_traits
<_ForwardIterator
>::value_type
,
4145 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4146 __glibcxx_requires_valid_range(__first
, __last
);
4148 _ForwardIterator __next
= __first
;
4149 if (__first
== __last
4150 || ++__next
== __last
)
4151 return std::make_pair(__first
, __first
);
4153 _ForwardIterator __min
, __max
;
4154 if (__comp(*__next
, *__first
))
4168 while (__first
!= __last
)
4171 if (++__next
== __last
)
4173 if (__comp(*__first
, *__min
))
4175 else if (!__comp(*__first
, *__max
))
4180 if (__comp(*__next
, *__first
))
4182 if (__comp(*__next
, *__min
))
4184 if (!__comp(*__first
, *__max
))
4189 if (__comp(*__first
, *__min
))
4191 if (!__comp(*__next
, *__max
))
4199 return std::make_pair(__min
, __max
);
4203 template<typename _Tp
>
4205 min(initializer_list
<_Tp
> __l
)
4206 { return *std::min_element(__l
.begin(), __l
.end()); }
4208 template<typename _Tp
, typename _Compare
>
4210 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4211 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4213 template<typename _Tp
>
4215 max(initializer_list
<_Tp
> __l
)
4216 { return *std::max_element(__l
.begin(), __l
.end()); }
4218 template<typename _Tp
, typename _Compare
>
4220 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4221 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4223 template<typename _Tp
>
4224 inline pair
<_Tp
, _Tp
>
4225 minmax(initializer_list
<_Tp
> __l
)
4227 pair
<const _Tp
*, const _Tp
*> __p
=
4228 std::minmax_element(__l
.begin(), __l
.end());
4229 return std::make_pair(*__p
.first
, *__p
.second
);
4232 template<typename _Tp
, typename _Compare
>
4233 inline pair
<_Tp
, _Tp
>
4234 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4236 pair
<const _Tp
*, const _Tp
*> __p
=
4237 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4238 return std::make_pair(*__p
.first
, *__p
.second
);
4240 #endif // __GXX_EXPERIMENTAL_CXX0X__
4242 _GLIBCXX_END_NAMESPACE
4244 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4247 * @brief Apply a function to every element of a sequence.
4248 * @ingroup non_mutating_algorithms
4249 * @param first An input iterator.
4250 * @param last An input iterator.
4251 * @param f A unary function object.
4254 * Applies the function object @p f to each element in the range
4255 * @p [first,last). @p f must not modify the order of the sequence.
4256 * If @p f has a return value it is ignored.
4258 template<typename _InputIterator
, typename _Function
>
4260 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4262 // concept requirements
4263 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4264 __glibcxx_requires_valid_range(__first
, __last
);
4265 for (; __first
!= __last
; ++__first
)
4271 * @brief Find the first occurrence of a value in a sequence.
4272 * @ingroup non_mutating_algorithms
4273 * @param first An input iterator.
4274 * @param last An input iterator.
4275 * @param val The value to find.
4276 * @return The first iterator @c i in the range @p [first,last)
4277 * such that @c *i == @p val, or @p last if no such iterator exists.
4279 template<typename _InputIterator
, typename _Tp
>
4280 inline _InputIterator
4281 find(_InputIterator __first
, _InputIterator __last
,
4284 // concept requirements
4285 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4286 __glibcxx_function_requires(_EqualOpConcept
<
4287 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4288 __glibcxx_requires_valid_range(__first
, __last
);
4289 return std::__find(__first
, __last
, __val
,
4290 std::__iterator_category(__first
));
4294 * @brief Find the first element in a sequence for which a
4295 * predicate is true.
4296 * @ingroup non_mutating_algorithms
4297 * @param first An input iterator.
4298 * @param last An input iterator.
4299 * @param pred A predicate.
4300 * @return The first iterator @c i in the range @p [first,last)
4301 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4303 template<typename _InputIterator
, typename _Predicate
>
4304 inline _InputIterator
4305 find_if(_InputIterator __first
, _InputIterator __last
,
4308 // concept requirements
4309 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4310 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4311 typename iterator_traits
<_InputIterator
>::value_type
>)
4312 __glibcxx_requires_valid_range(__first
, __last
);
4313 return std::__find_if(__first
, __last
, __pred
,
4314 std::__iterator_category(__first
));
4318 * @brief Find element from a set in a sequence.
4319 * @ingroup non_mutating_algorithms
4320 * @param first1 Start of range to search.
4321 * @param last1 End of range to search.
4322 * @param first2 Start of match candidates.
4323 * @param last2 End of match candidates.
4324 * @return The first iterator @c i in the range
4325 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4326 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4328 * Searches the range @p [first1,last1) for an element that is equal to
4329 * some element in the range [first2,last2). If found, returns an iterator
4330 * in the range [first1,last1), otherwise returns @p last1.
4332 template<typename _InputIterator
, typename _ForwardIterator
>
4334 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4335 _ForwardIterator __first2
, _ForwardIterator __last2
)
4337 // concept requirements
4338 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4339 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4340 __glibcxx_function_requires(_EqualOpConcept
<
4341 typename iterator_traits
<_InputIterator
>::value_type
,
4342 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4343 __glibcxx_requires_valid_range(__first1
, __last1
);
4344 __glibcxx_requires_valid_range(__first2
, __last2
);
4346 for (; __first1
!= __last1
; ++__first1
)
4347 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4348 if (*__first1
== *__iter
)
4354 * @brief Find element from a set in a sequence using a predicate.
4355 * @ingroup non_mutating_algorithms
4356 * @param first1 Start of range to search.
4357 * @param last1 End of range to search.
4358 * @param first2 Start of match candidates.
4359 * @param last2 End of match candidates.
4360 * @param comp Predicate to use.
4361 * @return The first iterator @c i in the range
4362 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4363 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4366 * Searches the range @p [first1,last1) for an element that is
4367 * equal to some element in the range [first2,last2). If found,
4368 * returns an iterator in the range [first1,last1), otherwise
4371 template<typename _InputIterator
, typename _ForwardIterator
,
4372 typename _BinaryPredicate
>
4374 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4375 _ForwardIterator __first2
, _ForwardIterator __last2
,
4376 _BinaryPredicate __comp
)
4378 // concept requirements
4379 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4380 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4381 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4382 typename iterator_traits
<_InputIterator
>::value_type
,
4383 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4384 __glibcxx_requires_valid_range(__first1
, __last1
);
4385 __glibcxx_requires_valid_range(__first2
, __last2
);
4387 for (; __first1
!= __last1
; ++__first1
)
4388 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4389 if (__comp(*__first1
, *__iter
))
4395 * @brief Find two adjacent values in a sequence that are equal.
4396 * @ingroup non_mutating_algorithms
4397 * @param first A forward iterator.
4398 * @param last A forward iterator.
4399 * @return The first iterator @c i such that @c i and @c i+1 are both
4400 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4401 * or @p last if no such iterator exists.
4403 template<typename _ForwardIterator
>
4405 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4407 // concept requirements
4408 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4409 __glibcxx_function_requires(_EqualityComparableConcept
<
4410 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4411 __glibcxx_requires_valid_range(__first
, __last
);
4412 if (__first
== __last
)
4414 _ForwardIterator __next
= __first
;
4415 while(++__next
!= __last
)
4417 if (*__first
== *__next
)
4425 * @brief Find two adjacent values in a sequence using a predicate.
4426 * @ingroup non_mutating_algorithms
4427 * @param first A forward iterator.
4428 * @param last A forward iterator.
4429 * @param binary_pred A binary predicate.
4430 * @return The first iterator @c i such that @c i and @c i+1 are both
4431 * valid iterators in @p [first,last) and such that
4432 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4435 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4437 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4438 _BinaryPredicate __binary_pred
)
4440 // concept requirements
4441 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4442 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4443 typename iterator_traits
<_ForwardIterator
>::value_type
,
4444 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4445 __glibcxx_requires_valid_range(__first
, __last
);
4446 if (__first
== __last
)
4448 _ForwardIterator __next
= __first
;
4449 while(++__next
!= __last
)
4451 if (__binary_pred(*__first
, *__next
))
4459 * @brief Count the number of copies of a value in a sequence.
4460 * @ingroup non_mutating_algorithms
4461 * @param first An input iterator.
4462 * @param last An input iterator.
4463 * @param value The value to be counted.
4464 * @return The number of iterators @c i in the range @p [first,last)
4465 * for which @c *i == @p value
4467 template<typename _InputIterator
, typename _Tp
>
4468 typename iterator_traits
<_InputIterator
>::difference_type
4469 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4471 // concept requirements
4472 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4473 __glibcxx_function_requires(_EqualOpConcept
<
4474 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4475 __glibcxx_requires_valid_range(__first
, __last
);
4476 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4477 for (; __first
!= __last
; ++__first
)
4478 if (*__first
== __value
)
4484 * @brief Count the elements of a sequence for which a predicate is true.
4485 * @ingroup non_mutating_algorithms
4486 * @param first An input iterator.
4487 * @param last An input iterator.
4488 * @param pred A predicate.
4489 * @return The number of iterators @c i in the range @p [first,last)
4490 * for which @p pred(*i) is true.
4492 template<typename _InputIterator
, typename _Predicate
>
4493 typename iterator_traits
<_InputIterator
>::difference_type
4494 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4496 // concept requirements
4497 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4498 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4499 typename iterator_traits
<_InputIterator
>::value_type
>)
4500 __glibcxx_requires_valid_range(__first
, __last
);
4501 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4502 for (; __first
!= __last
; ++__first
)
4503 if (__pred(*__first
))
4509 * @brief Search a sequence for a matching sub-sequence.
4510 * @ingroup non_mutating_algorithms
4511 * @param first1 A forward iterator.
4512 * @param last1 A forward iterator.
4513 * @param first2 A forward iterator.
4514 * @param last2 A forward iterator.
4515 * @return The first iterator @c i in the range
4516 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4517 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4518 * such iterator exists.
4520 * Searches the range @p [first1,last1) for a sub-sequence that compares
4521 * equal value-by-value with the sequence given by @p [first2,last2) and
4522 * returns an iterator to the first element of the sub-sequence, or
4523 * @p last1 if the sub-sequence is not found.
4525 * Because the sub-sequence must lie completely within the range
4526 * @p [first1,last1) it must start at a position less than
4527 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4529 * This means that the returned iterator @c i will be in the range
4530 * @p [first1,last1-(last2-first2))
4532 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4534 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4535 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4537 // concept requirements
4538 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4539 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4540 __glibcxx_function_requires(_EqualOpConcept
<
4541 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4542 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4543 __glibcxx_requires_valid_range(__first1
, __last1
);
4544 __glibcxx_requires_valid_range(__first2
, __last2
);
4546 // Test for empty ranges
4547 if (__first1
== __last1
|| __first2
== __last2
)
4550 // Test for a pattern of length 1.
4551 _ForwardIterator2
__p1(__first2
);
4552 if (++__p1
== __last2
)
4553 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4556 _ForwardIterator2 __p
;
4557 _ForwardIterator1 __current
= __first1
;
4561 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4562 if (__first1
== __last1
)
4566 __current
= __first1
;
4567 if (++__current
== __last1
)
4570 while (*__current
== *__p
)
4572 if (++__p
== __last2
)
4574 if (++__current
== __last1
)
4583 * @brief Search a sequence for a matching sub-sequence using a predicate.
4584 * @ingroup non_mutating_algorithms
4585 * @param first1 A forward iterator.
4586 * @param last1 A forward iterator.
4587 * @param first2 A forward iterator.
4588 * @param last2 A forward iterator.
4589 * @param predicate A binary predicate.
4590 * @return The first iterator @c i in the range
4591 * @p [first1,last1-(last2-first2)) such that
4592 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4593 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4595 * Searches the range @p [first1,last1) for a sub-sequence that compares
4596 * equal value-by-value with the sequence given by @p [first2,last2),
4597 * using @p predicate to determine equality, and returns an iterator
4598 * to the first element of the sub-sequence, or @p last1 if no such
4601 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4603 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4604 typename _BinaryPredicate
>
4606 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4607 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4608 _BinaryPredicate __predicate
)
4610 // concept requirements
4611 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4612 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4613 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4614 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4615 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4616 __glibcxx_requires_valid_range(__first1
, __last1
);
4617 __glibcxx_requires_valid_range(__first2
, __last2
);
4619 // Test for empty ranges
4620 if (__first1
== __last1
|| __first2
== __last2
)
4623 // Test for a pattern of length 1.
4624 _ForwardIterator2
__p1(__first2
);
4625 if (++__p1
== __last2
)
4627 while (__first1
!= __last1
4628 && !bool(__predicate(*__first1
, *__first2
)))
4634 _ForwardIterator2 __p
;
4635 _ForwardIterator1 __current
= __first1
;
4639 while (__first1
!= __last1
4640 && !bool(__predicate(*__first1
, *__first2
)))
4642 if (__first1
== __last1
)
4646 __current
= __first1
;
4647 if (++__current
== __last1
)
4650 while (__predicate(*__current
, *__p
))
4652 if (++__p
== __last2
)
4654 if (++__current
== __last1
)
4664 * @brief Search a sequence for a number of consecutive values.
4665 * @ingroup non_mutating_algorithms
4666 * @param first A forward iterator.
4667 * @param last A forward iterator.
4668 * @param count The number of consecutive values.
4669 * @param val The value to find.
4670 * @return The first iterator @c i in the range @p [first,last-count)
4671 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4672 * or @p last if no such iterator exists.
4674 * Searches the range @p [first,last) for @p count consecutive elements
4677 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4679 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4680 _Integer __count
, const _Tp
& __val
)
4682 // concept requirements
4683 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4684 __glibcxx_function_requires(_EqualOpConcept
<
4685 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4686 __glibcxx_requires_valid_range(__first
, __last
);
4691 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4692 return std::__search_n(__first
, __last
, __count
, __val
,
4693 std::__iterator_category(__first
));
4698 * @brief Search a sequence for a number of consecutive values using a
4700 * @ingroup non_mutating_algorithms
4701 * @param first A forward iterator.
4702 * @param last A forward iterator.
4703 * @param count The number of consecutive values.
4704 * @param val The value to find.
4705 * @param binary_pred A binary predicate.
4706 * @return The first iterator @c i in the range @p [first,last-count)
4707 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4708 * range @p [0,count), or @p last if no such iterator exists.
4710 * Searches the range @p [first,last) for @p count consecutive elements
4711 * for which the predicate returns true.
4713 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4714 typename _BinaryPredicate
>
4716 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4717 _Integer __count
, const _Tp
& __val
,
4718 _BinaryPredicate __binary_pred
)
4720 // concept requirements
4721 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4722 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4723 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4724 __glibcxx_requires_valid_range(__first
, __last
);
4730 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4734 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4735 std::__iterator_category(__first
));
4740 * @brief Perform an operation on a sequence.
4741 * @ingroup mutating_algorithms
4742 * @param first An input iterator.
4743 * @param last An input iterator.
4744 * @param result An output iterator.
4745 * @param unary_op A unary operator.
4746 * @return An output iterator equal to @p result+(last-first).
4748 * Applies the operator to each element in the input range and assigns
4749 * the results to successive elements of the output sequence.
4750 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4751 * range @p [0,last-first).
4753 * @p unary_op must not alter its argument.
4755 template<typename _InputIterator
, typename _OutputIterator
,
4756 typename _UnaryOperation
>
4758 transform(_InputIterator __first
, _InputIterator __last
,
4759 _OutputIterator __result
, _UnaryOperation __unary_op
)
4761 // concept requirements
4762 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4763 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4764 // "the type returned by a _UnaryOperation"
4765 __typeof__(__unary_op(*__first
))>)
4766 __glibcxx_requires_valid_range(__first
, __last
);
4768 for (; __first
!= __last
; ++__first
, ++__result
)
4769 *__result
= __unary_op(*__first
);
4774 * @brief Perform an operation on corresponding elements of two sequences.
4775 * @ingroup mutating_algorithms
4776 * @param first1 An input iterator.
4777 * @param last1 An input iterator.
4778 * @param first2 An input iterator.
4779 * @param result An output iterator.
4780 * @param binary_op A binary operator.
4781 * @return An output iterator equal to @p result+(last-first).
4783 * Applies the operator to the corresponding elements in the two
4784 * input ranges and assigns the results to successive elements of the
4786 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4787 * @c N in the range @p [0,last1-first1).
4789 * @p binary_op must not alter either of its arguments.
4791 template<typename _InputIterator1
, typename _InputIterator2
,
4792 typename _OutputIterator
, typename _BinaryOperation
>
4794 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4795 _InputIterator2 __first2
, _OutputIterator __result
,
4796 _BinaryOperation __binary_op
)
4798 // concept requirements
4799 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4800 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4801 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4802 // "the type returned by a _BinaryOperation"
4803 __typeof__(__binary_op(*__first1
,*__first2
))>)
4804 __glibcxx_requires_valid_range(__first1
, __last1
);
4806 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4807 *__result
= __binary_op(*__first1
, *__first2
);
4812 * @brief Replace each occurrence of one value in a sequence with another
4814 * @ingroup mutating_algorithms
4815 * @param first A forward iterator.
4816 * @param last A forward iterator.
4817 * @param old_value The value to be replaced.
4818 * @param new_value The replacement value.
4819 * @return replace() returns no value.
4821 * For each iterator @c i in the range @p [first,last) if @c *i ==
4822 * @p old_value then the assignment @c *i = @p new_value is performed.
4824 template<typename _ForwardIterator
, typename _Tp
>
4826 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4827 const _Tp
& __old_value
, const _Tp
& __new_value
)
4829 // concept requirements
4830 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4832 __glibcxx_function_requires(_EqualOpConcept
<
4833 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4834 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4835 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4836 __glibcxx_requires_valid_range(__first
, __last
);
4838 for (; __first
!= __last
; ++__first
)
4839 if (*__first
== __old_value
)
4840 *__first
= __new_value
;
4844 * @brief Replace each value in a sequence for which a predicate returns
4845 * true with another value.
4846 * @ingroup mutating_algorithms
4847 * @param first A forward iterator.
4848 * @param last A forward iterator.
4849 * @param pred A predicate.
4850 * @param new_value The replacement value.
4851 * @return replace_if() returns no value.
4853 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4854 * is true then the assignment @c *i = @p new_value is performed.
4856 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4858 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4859 _Predicate __pred
, const _Tp
& __new_value
)
4861 // concept requirements
4862 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4864 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4865 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4866 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4867 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4868 __glibcxx_requires_valid_range(__first
, __last
);
4870 for (; __first
!= __last
; ++__first
)
4871 if (__pred(*__first
))
4872 *__first
= __new_value
;
4876 * @brief Assign the result of a function object to each value in a
4878 * @ingroup mutating_algorithms
4879 * @param first A forward iterator.
4880 * @param last A forward iterator.
4881 * @param gen A function object taking no arguments and returning
4882 * std::iterator_traits<_ForwardIterator>::value_type
4883 * @return generate() returns no value.
4885 * Performs the assignment @c *i = @p gen() for each @c i in the range
4888 template<typename _ForwardIterator
, typename _Generator
>
4890 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4893 // concept requirements
4894 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4895 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4896 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4897 __glibcxx_requires_valid_range(__first
, __last
);
4899 for (; __first
!= __last
; ++__first
)
4904 * @brief Assign the result of a function object to each value in a
4906 * @ingroup mutating_algorithms
4907 * @param first A forward iterator.
4908 * @param n The length of the sequence.
4909 * @param gen A function object taking no arguments and returning
4910 * std::iterator_traits<_ForwardIterator>::value_type
4911 * @return The end of the sequence, @p first+n
4913 * Performs the assignment @c *i = @p gen() for each @c i in the range
4914 * @p [first,first+n).
4916 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4918 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4920 // concept requirements
4921 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4922 // "the type returned by a _Generator"
4923 __typeof__(__gen())>)
4925 for (; __n
> 0; --__n
, ++__first
)
4932 * @brief Copy a sequence, removing consecutive duplicate values.
4933 * @ingroup mutating_algorithms
4934 * @param first An input iterator.
4935 * @param last An input iterator.
4936 * @param result An output iterator.
4937 * @return An iterator designating the end of the resulting sequence.
4939 * Copies each element in the range @p [first,last) to the range
4940 * beginning at @p result, except that only the first element is copied
4941 * from groups of consecutive elements that compare equal.
4942 * unique_copy() is stable, so the relative order of elements that are
4943 * copied is unchanged.
4945 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4946 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4948 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4949 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4952 template<typename _InputIterator
, typename _OutputIterator
>
4953 inline _OutputIterator
4954 unique_copy(_InputIterator __first
, _InputIterator __last
,
4955 _OutputIterator __result
)
4957 // concept requirements
4958 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4959 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4960 typename iterator_traits
<_InputIterator
>::value_type
>)
4961 __glibcxx_function_requires(_EqualityComparableConcept
<
4962 typename iterator_traits
<_InputIterator
>::value_type
>)
4963 __glibcxx_requires_valid_range(__first
, __last
);
4965 if (__first
== __last
)
4967 return std::__unique_copy(__first
, __last
, __result
,
4968 std::__iterator_category(__first
),
4969 std::__iterator_category(__result
));
4973 * @brief Copy a sequence, removing consecutive values using a predicate.
4974 * @ingroup mutating_algorithms
4975 * @param first An input iterator.
4976 * @param last An input iterator.
4977 * @param result An output iterator.
4978 * @param binary_pred A binary predicate.
4979 * @return An iterator designating the end of the resulting sequence.
4981 * Copies each element in the range @p [first,last) to the range
4982 * beginning at @p result, except that only the first element is copied
4983 * from groups of consecutive elements for which @p binary_pred returns
4985 * unique_copy() is stable, so the relative order of elements that are
4986 * copied is unchanged.
4988 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4989 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4991 template<typename _InputIterator
, typename _OutputIterator
,
4992 typename _BinaryPredicate
>
4993 inline _OutputIterator
4994 unique_copy(_InputIterator __first
, _InputIterator __last
,
4995 _OutputIterator __result
,
4996 _BinaryPredicate __binary_pred
)
4998 // concept requirements -- predicates checked later
4999 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5000 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5001 typename iterator_traits
<_InputIterator
>::value_type
>)
5002 __glibcxx_requires_valid_range(__first
, __last
);
5004 if (__first
== __last
)
5006 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
5007 std::__iterator_category(__first
),
5008 std::__iterator_category(__result
));
5013 * @brief Randomly shuffle the elements of a sequence.
5014 * @ingroup mutating_algorithms
5015 * @param first A forward iterator.
5016 * @param last A forward iterator.
5019 * Reorder the elements in the range @p [first,last) using a random
5020 * distribution, so that every possible ordering of the sequence is
5023 template<typename _RandomAccessIterator
>
5025 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5027 // concept requirements
5028 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5029 _RandomAccessIterator
>)
5030 __glibcxx_requires_valid_range(__first
, __last
);
5032 if (__first
!= __last
)
5033 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5034 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
5038 * @brief Shuffle the elements of a sequence using a random number
5040 * @ingroup mutating_algorithms
5041 * @param first A forward iterator.
5042 * @param last A forward iterator.
5043 * @param rand The RNG functor or function.
5046 * Reorders the elements in the range @p [first,last) using @p rand to
5047 * provide a random distribution. Calling @p rand(N) for a positive
5048 * integer @p N should return a randomly chosen integer from the
5051 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
5053 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5054 _RandomNumberGenerator
& __rand
)
5056 // concept requirements
5057 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5058 _RandomAccessIterator
>)
5059 __glibcxx_requires_valid_range(__first
, __last
);
5061 if (__first
== __last
)
5063 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5064 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5069 * @brief Move elements for which a predicate is true to the beginning
5071 * @ingroup mutating_algorithms
5072 * @param first A forward iterator.
5073 * @param last A forward iterator.
5074 * @param pred A predicate functor.
5075 * @return An iterator @p middle such that @p pred(i) is true for each
5076 * iterator @p i in the range @p [first,middle) and false for each @p i
5077 * in the range @p [middle,last).
5079 * @p pred must not modify its operand. @p partition() does not preserve
5080 * the relative ordering of elements in each group, use
5081 * @p stable_partition() if this is needed.
5083 template<typename _ForwardIterator
, typename _Predicate
>
5084 inline _ForwardIterator
5085 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5088 // concept requirements
5089 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5091 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5092 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5093 __glibcxx_requires_valid_range(__first
, __last
);
5095 return std::__partition(__first
, __last
, __pred
,
5096 std::__iterator_category(__first
));
5102 * @brief Sort the smallest elements of a sequence.
5103 * @ingroup sorting_algorithms
5104 * @param first An iterator.
5105 * @param middle Another iterator.
5106 * @param last Another iterator.
5109 * Sorts the smallest @p (middle-first) elements in the range
5110 * @p [first,last) and moves them to the range @p [first,middle). The
5111 * order of the remaining elements in the range @p [middle,last) is
5113 * After the sort if @p i and @j are iterators in the range
5114 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5115 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5117 template<typename _RandomAccessIterator
>
5119 partial_sort(_RandomAccessIterator __first
,
5120 _RandomAccessIterator __middle
,
5121 _RandomAccessIterator __last
)
5123 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5126 // concept requirements
5127 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5128 _RandomAccessIterator
>)
5129 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5130 __glibcxx_requires_valid_range(__first
, __middle
);
5131 __glibcxx_requires_valid_range(__middle
, __last
);
5133 std::__heap_select(__first
, __middle
, __last
);
5134 std::sort_heap(__first
, __middle
);
5138 * @brief Sort the smallest elements of a sequence using a predicate
5140 * @ingroup sorting_algorithms
5141 * @param first An iterator.
5142 * @param middle Another iterator.
5143 * @param last Another iterator.
5144 * @param comp A comparison functor.
5147 * Sorts the smallest @p (middle-first) elements in the range
5148 * @p [first,last) and moves them to the range @p [first,middle). The
5149 * order of the remaining elements in the range @p [middle,last) is
5151 * After the sort if @p i and @j are iterators in the range
5152 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5153 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5156 template<typename _RandomAccessIterator
, typename _Compare
>
5158 partial_sort(_RandomAccessIterator __first
,
5159 _RandomAccessIterator __middle
,
5160 _RandomAccessIterator __last
,
5163 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5166 // concept requirements
5167 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5168 _RandomAccessIterator
>)
5169 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5170 _ValueType
, _ValueType
>)
5171 __glibcxx_requires_valid_range(__first
, __middle
);
5172 __glibcxx_requires_valid_range(__middle
, __last
);
5174 std::__heap_select(__first
, __middle
, __last
, __comp
);
5175 std::sort_heap(__first
, __middle
, __comp
);
5179 * @brief Sort a sequence just enough to find a particular position.
5180 * @ingroup sorting_algorithms
5181 * @param first An iterator.
5182 * @param nth Another iterator.
5183 * @param last Another iterator.
5186 * Rearranges the elements in the range @p [first,last) so that @p *nth
5187 * is the same element that would have been in that position had the
5188 * whole sequence been sorted.
5189 * whole sequence been sorted. The elements either side of @p *nth are
5190 * not completely sorted, but for any iterator @i in the range
5191 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5192 * holds that @p *j<*i is false.
5194 template<typename _RandomAccessIterator
>
5196 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5197 _RandomAccessIterator __last
)
5199 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5202 // concept requirements
5203 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5204 _RandomAccessIterator
>)
5205 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5206 __glibcxx_requires_valid_range(__first
, __nth
);
5207 __glibcxx_requires_valid_range(__nth
, __last
);
5209 if (__first
== __last
|| __nth
== __last
)
5212 std::__introselect(__first
, __nth
, __last
,
5213 std::__lg(__last
- __first
) * 2);
5217 * @brief Sort a sequence just enough to find a particular position
5218 * using a predicate for comparison.
5219 * @ingroup sorting_algorithms
5220 * @param first An iterator.
5221 * @param nth Another iterator.
5222 * @param last Another iterator.
5223 * @param comp A comparison functor.
5226 * Rearranges the elements in the range @p [first,last) so that @p *nth
5227 * is the same element that would have been in that position had the
5228 * whole sequence been sorted. The elements either side of @p *nth are
5229 * not completely sorted, but for any iterator @i in the range
5230 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5231 * holds that @p comp(*j,*i) is false.
5233 template<typename _RandomAccessIterator
, typename _Compare
>
5235 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5236 _RandomAccessIterator __last
, _Compare __comp
)
5238 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5241 // concept requirements
5242 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5243 _RandomAccessIterator
>)
5244 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5245 _ValueType
, _ValueType
>)
5246 __glibcxx_requires_valid_range(__first
, __nth
);
5247 __glibcxx_requires_valid_range(__nth
, __last
);
5249 if (__first
== __last
|| __nth
== __last
)
5252 std::__introselect(__first
, __nth
, __last
,
5253 std::__lg(__last
- __first
) * 2, __comp
);
5258 * @brief Sort the elements of a sequence.
5259 * @ingroup sorting_algorithms
5260 * @param first An iterator.
5261 * @param last Another iterator.
5264 * Sorts the elements in the range @p [first,last) in ascending order,
5265 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5266 * @p [first,last-1).
5268 * The relative ordering of equivalent elements is not preserved, use
5269 * @p stable_sort() if this is needed.
5271 template<typename _RandomAccessIterator
>
5273 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5275 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5278 // concept requirements
5279 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5280 _RandomAccessIterator
>)
5281 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5282 __glibcxx_requires_valid_range(__first
, __last
);
5284 if (__first
!= __last
)
5286 std::__introsort_loop(__first
, __last
,
5287 std::__lg(__last
- __first
) * 2);
5288 std::__final_insertion_sort(__first
, __last
);
5293 * @brief Sort the elements of a sequence using a predicate for comparison.
5294 * @ingroup sorting_algorithms
5295 * @param first An iterator.
5296 * @param last Another iterator.
5297 * @param comp A comparison functor.
5300 * Sorts the elements in the range @p [first,last) in ascending order,
5301 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5302 * range @p [first,last-1).
5304 * The relative ordering of equivalent elements is not preserved, use
5305 * @p stable_sort() if this is needed.
5307 template<typename _RandomAccessIterator
, typename _Compare
>
5309 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5312 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5315 // concept requirements
5316 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5317 _RandomAccessIterator
>)
5318 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5320 __glibcxx_requires_valid_range(__first
, __last
);
5322 if (__first
!= __last
)
5324 std::__introsort_loop(__first
, __last
,
5325 std::__lg(__last
- __first
) * 2, __comp
);
5326 std::__final_insertion_sort(__first
, __last
, __comp
);
5331 * @brief Merges two sorted ranges.
5332 * @ingroup sorting_algorithms
5333 * @param first1 An iterator.
5334 * @param first2 Another iterator.
5335 * @param last1 Another iterator.
5336 * @param last2 Another iterator.
5337 * @param result An iterator pointing to the end of the merged range.
5338 * @return An iterator pointing to the first element "not less
5341 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5342 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5343 * must be sorted, and the output range must not overlap with either of
5344 * the input ranges. The sort is @e stable, that is, for equivalent
5345 * elements in the two ranges, elements from the first range will always
5346 * come before elements from the second.
5348 template<typename _InputIterator1
, typename _InputIterator2
,
5349 typename _OutputIterator
>
5351 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5352 _InputIterator2 __first2
, _InputIterator2 __last2
,
5353 _OutputIterator __result
)
5355 typedef typename iterator_traits
<_InputIterator1
>::value_type
5357 typedef typename iterator_traits
<_InputIterator2
>::value_type
5360 // concept requirements
5361 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5362 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5363 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5365 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5367 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5368 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5369 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5371 while (__first1
!= __last1
&& __first2
!= __last2
)
5373 if (*__first2
< *__first1
)
5375 *__result
= *__first2
;
5380 *__result
= *__first1
;
5385 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5390 * @brief Merges two sorted ranges.
5391 * @ingroup sorting_algorithms
5392 * @param first1 An iterator.
5393 * @param first2 Another iterator.
5394 * @param last1 Another iterator.
5395 * @param last2 Another iterator.
5396 * @param result An iterator pointing to the end of the merged range.
5397 * @param comp A functor to use for comparisons.
5398 * @return An iterator pointing to the first element "not less
5401 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5402 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5403 * must be sorted, and the output range must not overlap with either of
5404 * the input ranges. The sort is @e stable, that is, for equivalent
5405 * elements in the two ranges, elements from the first range will always
5406 * come before elements from the second.
5408 * The comparison function should have the same effects on ordering as
5409 * the function used for the initial sort.
5411 template<typename _InputIterator1
, typename _InputIterator2
,
5412 typename _OutputIterator
, typename _Compare
>
5414 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5415 _InputIterator2 __first2
, _InputIterator2 __last2
,
5416 _OutputIterator __result
, _Compare __comp
)
5418 typedef typename iterator_traits
<_InputIterator1
>::value_type
5420 typedef typename iterator_traits
<_InputIterator2
>::value_type
5423 // concept requirements
5424 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5425 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5426 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5428 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5430 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5431 _ValueType2
, _ValueType1
>)
5432 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5433 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5435 while (__first1
!= __last1
&& __first2
!= __last2
)
5437 if (__comp(*__first2
, *__first1
))
5439 *__result
= *__first2
;
5444 *__result
= *__first1
;
5449 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5455 * @brief Sort the elements of a sequence, preserving the relative order
5456 * of equivalent elements.
5457 * @ingroup sorting_algorithms
5458 * @param first An iterator.
5459 * @param last Another iterator.
5462 * Sorts the elements in the range @p [first,last) in ascending order,
5463 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5464 * @p [first,last-1).
5466 * The relative ordering of equivalent elements is preserved, so any two
5467 * elements @p x and @p y in the range @p [first,last) such that
5468 * @p x<y is false and @p y<x is false will have the same relative
5469 * ordering after calling @p stable_sort().
5471 template<typename _RandomAccessIterator
>
5473 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5475 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5477 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5480 // concept requirements
5481 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5482 _RandomAccessIterator
>)
5483 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5484 __glibcxx_requires_valid_range(__first
, __last
);
5486 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5488 if (__buf
.begin() == 0)
5489 std::__inplace_stable_sort(__first
, __last
);
5491 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5492 _DistanceType(__buf
.size()));
5496 * @brief Sort the elements of a sequence using a predicate for comparison,
5497 * preserving the relative order of equivalent elements.
5498 * @ingroup sorting_algorithms
5499 * @param first An iterator.
5500 * @param last Another iterator.
5501 * @param comp A comparison functor.
5504 * Sorts the elements in the range @p [first,last) in ascending order,
5505 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5506 * range @p [first,last-1).
5508 * The relative ordering of equivalent elements is preserved, so any two
5509 * elements @p x and @p y in the range @p [first,last) such that
5510 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5511 * relative ordering after calling @p stable_sort().
5513 template<typename _RandomAccessIterator
, typename _Compare
>
5515 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5518 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5520 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5523 // concept requirements
5524 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5525 _RandomAccessIterator
>)
5526 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5529 __glibcxx_requires_valid_range(__first
, __last
);
5531 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5533 if (__buf
.begin() == 0)
5534 std::__inplace_stable_sort(__first
, __last
, __comp
);
5536 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5537 _DistanceType(__buf
.size()), __comp
);
5542 * @brief Return the union of two sorted ranges.
5543 * @ingroup set_algorithms
5544 * @param first1 Start of first range.
5545 * @param last1 End of first range.
5546 * @param first2 Start of second range.
5547 * @param last2 End of second range.
5548 * @return End of the output range.
5549 * @ingroup set_algorithms
5551 * This operation iterates over both ranges, copying elements present in
5552 * each range in order to the output range. Iterators increment for each
5553 * range. When the current element of one range is less than the other,
5554 * that element is copied and the iterator advanced. If an element is
5555 * contained in both ranges, the element from the first range is copied and
5556 * both ranges advance. The output range may not overlap either input
5559 template<typename _InputIterator1
, typename _InputIterator2
,
5560 typename _OutputIterator
>
5562 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5563 _InputIterator2 __first2
, _InputIterator2 __last2
,
5564 _OutputIterator __result
)
5566 typedef typename iterator_traits
<_InputIterator1
>::value_type
5568 typedef typename iterator_traits
<_InputIterator2
>::value_type
5571 // concept requirements
5572 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5573 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5574 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5576 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5578 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5579 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5580 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5581 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5583 while (__first1
!= __last1
&& __first2
!= __last2
)
5585 if (*__first1
< *__first2
)
5587 *__result
= *__first1
;
5590 else if (*__first2
< *__first1
)
5592 *__result
= *__first2
;
5597 *__result
= *__first1
;
5603 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5608 * @brief Return the union of two sorted ranges using a comparison functor.
5609 * @ingroup set_algorithms
5610 * @param first1 Start of first range.
5611 * @param last1 End of first range.
5612 * @param first2 Start of second range.
5613 * @param last2 End of second range.
5614 * @param comp The comparison functor.
5615 * @return End of the output range.
5616 * @ingroup set_algorithms
5618 * This operation iterates over both ranges, copying elements present in
5619 * each range in order to the output range. Iterators increment for each
5620 * range. When the current element of one range is less than the other
5621 * according to @a comp, that element is copied and the iterator advanced.
5622 * If an equivalent element according to @a comp is contained in both
5623 * ranges, the element from the first range is copied and both ranges
5624 * advance. The output range may not overlap either input range.
5626 template<typename _InputIterator1
, typename _InputIterator2
,
5627 typename _OutputIterator
, typename _Compare
>
5629 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5630 _InputIterator2 __first2
, _InputIterator2 __last2
,
5631 _OutputIterator __result
, _Compare __comp
)
5633 typedef typename iterator_traits
<_InputIterator1
>::value_type
5635 typedef typename iterator_traits
<_InputIterator2
>::value_type
5638 // concept requirements
5639 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5640 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5641 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5643 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5645 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5646 _ValueType1
, _ValueType2
>)
5647 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5648 _ValueType2
, _ValueType1
>)
5649 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5650 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5652 while (__first1
!= __last1
&& __first2
!= __last2
)
5654 if (__comp(*__first1
, *__first2
))
5656 *__result
= *__first1
;
5659 else if (__comp(*__first2
, *__first1
))
5661 *__result
= *__first2
;
5666 *__result
= *__first1
;
5672 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5677 * @brief Return the intersection of two sorted ranges.
5678 * @ingroup set_algorithms
5679 * @param first1 Start of first range.
5680 * @param last1 End of first range.
5681 * @param first2 Start of second range.
5682 * @param last2 End of second range.
5683 * @return End of the output range.
5684 * @ingroup set_algorithms
5686 * This operation iterates over both ranges, copying elements present in
5687 * both ranges in order to the output range. Iterators increment for each
5688 * range. When the current element of one range is less than the other,
5689 * that iterator advances. If an element is contained in both ranges, the
5690 * element from the first range is copied and both ranges advance. The
5691 * output range may not overlap either input range.
5693 template<typename _InputIterator1
, typename _InputIterator2
,
5694 typename _OutputIterator
>
5696 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5697 _InputIterator2 __first2
, _InputIterator2 __last2
,
5698 _OutputIterator __result
)
5700 typedef typename iterator_traits
<_InputIterator1
>::value_type
5702 typedef typename iterator_traits
<_InputIterator2
>::value_type
5705 // concept requirements
5706 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5707 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5708 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5710 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5711 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5712 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5713 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5715 while (__first1
!= __last1
&& __first2
!= __last2
)
5716 if (*__first1
< *__first2
)
5718 else if (*__first2
< *__first1
)
5722 *__result
= *__first1
;
5731 * @brief Return the intersection of two sorted ranges using comparison
5733 * @ingroup set_algorithms
5734 * @param first1 Start of first range.
5735 * @param last1 End of first range.
5736 * @param first2 Start of second range.
5737 * @param last2 End of second range.
5738 * @param comp The comparison functor.
5739 * @return End of the output range.
5740 * @ingroup set_algorithms
5742 * This operation iterates over both ranges, copying elements present in
5743 * both ranges in order to the output range. Iterators increment for each
5744 * range. When the current element of one range is less than the other
5745 * according to @a comp, that iterator advances. If an element is
5746 * contained in both ranges according to @a comp, the element from the
5747 * first range is copied and both ranges advance. The output range may not
5748 * overlap either input range.
5750 template<typename _InputIterator1
, typename _InputIterator2
,
5751 typename _OutputIterator
, typename _Compare
>
5753 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5754 _InputIterator2 __first2
, _InputIterator2 __last2
,
5755 _OutputIterator __result
, _Compare __comp
)
5757 typedef typename iterator_traits
<_InputIterator1
>::value_type
5759 typedef typename iterator_traits
<_InputIterator2
>::value_type
5762 // concept requirements
5763 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5764 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5765 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5767 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5768 _ValueType1
, _ValueType2
>)
5769 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5770 _ValueType2
, _ValueType1
>)
5771 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5772 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5774 while (__first1
!= __last1
&& __first2
!= __last2
)
5775 if (__comp(*__first1
, *__first2
))
5777 else if (__comp(*__first2
, *__first1
))
5781 *__result
= *__first1
;
5790 * @brief Return the difference of two sorted ranges.
5791 * @ingroup set_algorithms
5792 * @param first1 Start of first range.
5793 * @param last1 End of first range.
5794 * @param first2 Start of second range.
5795 * @param last2 End of second range.
5796 * @return End of the output range.
5797 * @ingroup set_algorithms
5799 * This operation iterates over both ranges, copying elements present in
5800 * the first range but not the second in order to the output range.
5801 * Iterators increment for each range. When the current element of the
5802 * first range is less than the second, that element is copied and the
5803 * iterator advances. If the current element of the second range is less,
5804 * the iterator advances, but no element is copied. If an element is
5805 * contained in both ranges, no elements are copied and both ranges
5806 * advance. The output range may not overlap either input range.
5808 template<typename _InputIterator1
, typename _InputIterator2
,
5809 typename _OutputIterator
>
5811 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5812 _InputIterator2 __first2
, _InputIterator2 __last2
,
5813 _OutputIterator __result
)
5815 typedef typename iterator_traits
<_InputIterator1
>::value_type
5817 typedef typename iterator_traits
<_InputIterator2
>::value_type
5820 // concept requirements
5821 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5822 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5823 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5825 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5826 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5827 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5828 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5830 while (__first1
!= __last1
&& __first2
!= __last2
)
5831 if (*__first1
< *__first2
)
5833 *__result
= *__first1
;
5837 else if (*__first2
< *__first1
)
5844 return std::copy(__first1
, __last1
, __result
);
5848 * @brief Return the difference of two sorted ranges using comparison
5850 * @ingroup set_algorithms
5851 * @param first1 Start of first range.
5852 * @param last1 End of first range.
5853 * @param first2 Start of second range.
5854 * @param last2 End of second range.
5855 * @param comp The comparison functor.
5856 * @return End of the output range.
5857 * @ingroup set_algorithms
5859 * This operation iterates over both ranges, copying elements present in
5860 * the first range but not the second in order to the output range.
5861 * Iterators increment for each range. When the current element of the
5862 * first range is less than the second according to @a comp, that element
5863 * is copied and the iterator advances. If the current element of the
5864 * second range is less, no element is copied and the iterator advances.
5865 * If an element is contained in both ranges according to @a comp, no
5866 * elements are copied and both ranges advance. The output range may not
5867 * overlap either input range.
5869 template<typename _InputIterator1
, typename _InputIterator2
,
5870 typename _OutputIterator
, typename _Compare
>
5872 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5873 _InputIterator2 __first2
, _InputIterator2 __last2
,
5874 _OutputIterator __result
, _Compare __comp
)
5876 typedef typename iterator_traits
<_InputIterator1
>::value_type
5878 typedef typename iterator_traits
<_InputIterator2
>::value_type
5881 // concept requirements
5882 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5883 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5884 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5886 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5887 _ValueType1
, _ValueType2
>)
5888 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5889 _ValueType2
, _ValueType1
>)
5890 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5891 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5893 while (__first1
!= __last1
&& __first2
!= __last2
)
5894 if (__comp(*__first1
, *__first2
))
5896 *__result
= *__first1
;
5900 else if (__comp(*__first2
, *__first1
))
5907 return std::copy(__first1
, __last1
, __result
);
5911 * @brief Return the symmetric difference of two sorted ranges.
5912 * @ingroup set_algorithms
5913 * @param first1 Start of first range.
5914 * @param last1 End of first range.
5915 * @param first2 Start of second range.
5916 * @param last2 End of second range.
5917 * @return End of the output range.
5918 * @ingroup set_algorithms
5920 * This operation iterates over both ranges, copying elements present in
5921 * one range but not the other in order to the output range. Iterators
5922 * increment for each range. When the current element of one range is less
5923 * than the other, that element is copied and the iterator advances. If an
5924 * element is contained in both ranges, no elements are copied and both
5925 * ranges advance. The output range may not overlap either input range.
5927 template<typename _InputIterator1
, typename _InputIterator2
,
5928 typename _OutputIterator
>
5930 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5931 _InputIterator2 __first2
, _InputIterator2 __last2
,
5932 _OutputIterator __result
)
5934 typedef typename iterator_traits
<_InputIterator1
>::value_type
5936 typedef typename iterator_traits
<_InputIterator2
>::value_type
5939 // concept requirements
5940 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5941 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5942 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5944 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5946 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5947 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5948 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5949 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5951 while (__first1
!= __last1
&& __first2
!= __last2
)
5952 if (*__first1
< *__first2
)
5954 *__result
= *__first1
;
5958 else if (*__first2
< *__first1
)
5960 *__result
= *__first2
;
5969 return std::copy(__first2
, __last2
, std::copy(__first1
,
5970 __last1
, __result
));
5974 * @brief Return the symmetric difference of two sorted ranges using
5975 * comparison functor.
5976 * @ingroup set_algorithms
5977 * @param first1 Start of first range.
5978 * @param last1 End of first range.
5979 * @param first2 Start of second range.
5980 * @param last2 End of second range.
5981 * @param comp The comparison functor.
5982 * @return End of the output range.
5983 * @ingroup set_algorithms
5985 * This operation iterates over both ranges, copying elements present in
5986 * one range but not the other in order to the output range. Iterators
5987 * increment for each range. When the current element of one range is less
5988 * than the other according to @a comp, that element is copied and the
5989 * iterator advances. If an element is contained in both ranges according
5990 * to @a comp, no elements are copied and both ranges advance. The output
5991 * range may not overlap either input range.
5993 template<typename _InputIterator1
, typename _InputIterator2
,
5994 typename _OutputIterator
, typename _Compare
>
5996 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5997 _InputIterator2 __first2
, _InputIterator2 __last2
,
5998 _OutputIterator __result
,
6001 typedef typename iterator_traits
<_InputIterator1
>::value_type
6003 typedef typename iterator_traits
<_InputIterator2
>::value_type
6006 // concept requirements
6007 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6008 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6009 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6011 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6013 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6014 _ValueType1
, _ValueType2
>)
6015 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6016 _ValueType2
, _ValueType1
>)
6017 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6018 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6020 while (__first1
!= __last1
&& __first2
!= __last2
)
6021 if (__comp(*__first1
, *__first2
))
6023 *__result
= *__first1
;
6027 else if (__comp(*__first2
, *__first1
))
6029 *__result
= *__first2
;
6038 return std::copy(__first2
, __last2
,
6039 std::copy(__first1
, __last1
, __result
));
6044 * @brief Return the minimum element in a range.
6045 * @ingroup sorting_algorithms
6046 * @param first Start of range.
6047 * @param last End of range.
6048 * @return Iterator referencing the first instance of the smallest value.
6050 template<typename _ForwardIterator
>
6052 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
6054 // concept requirements
6055 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6056 __glibcxx_function_requires(_LessThanComparableConcept
<
6057 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6058 __glibcxx_requires_valid_range(__first
, __last
);
6060 if (__first
== __last
)
6062 _ForwardIterator __result
= __first
;
6063 while (++__first
!= __last
)
6064 if (*__first
< *__result
)
6070 * @brief Return the minimum element in a range using comparison functor.
6071 * @ingroup sorting_algorithms
6072 * @param first Start of range.
6073 * @param last End of range.
6074 * @param comp Comparison functor.
6075 * @return Iterator referencing the first instance of the smallest value
6076 * according to comp.
6078 template<typename _ForwardIterator
, typename _Compare
>
6080 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6083 // concept requirements
6084 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6085 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6086 typename iterator_traits
<_ForwardIterator
>::value_type
,
6087 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6088 __glibcxx_requires_valid_range(__first
, __last
);
6090 if (__first
== __last
)
6092 _ForwardIterator __result
= __first
;
6093 while (++__first
!= __last
)
6094 if (__comp(*__first
, *__result
))
6100 * @brief Return the maximum element in a range.
6101 * @ingroup sorting_algorithms
6102 * @param first Start of range.
6103 * @param last End of range.
6104 * @return Iterator referencing the first instance of the largest value.
6106 template<typename _ForwardIterator
>
6108 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6110 // concept requirements
6111 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6112 __glibcxx_function_requires(_LessThanComparableConcept
<
6113 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6114 __glibcxx_requires_valid_range(__first
, __last
);
6116 if (__first
== __last
)
6118 _ForwardIterator __result
= __first
;
6119 while (++__first
!= __last
)
6120 if (*__result
< *__first
)
6126 * @brief Return the maximum element in a range using comparison functor.
6127 * @ingroup sorting_algorithms
6128 * @param first Start of range.
6129 * @param last End of range.
6130 * @param comp Comparison functor.
6131 * @return Iterator referencing the first instance of the largest value
6132 * according to comp.
6134 template<typename _ForwardIterator
, typename _Compare
>
6136 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6139 // concept requirements
6140 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6141 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6142 typename iterator_traits
<_ForwardIterator
>::value_type
,
6143 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6144 __glibcxx_requires_valid_range(__first
, __last
);
6146 if (__first
== __last
) return __first
;
6147 _ForwardIterator __result
= __first
;
6148 while (++__first
!= __last
)
6149 if (__comp(*__result
, *__first
))
6154 _GLIBCXX_END_NESTED_NAMESPACE
6156 #endif /* _STL_ALGO_H */