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
))
141 /// This is an overload used by find() for the Input Iterator case.
142 template<typename _InputIterator
, typename _Tp
>
143 inline _InputIterator
144 __find(_InputIterator __first
, _InputIterator __last
,
145 const _Tp
& __val
, input_iterator_tag
)
147 while (__first
!= __last
&& !(*__first
== __val
))
152 /// This is an overload used by find_if() for the Input Iterator case.
153 template<typename _InputIterator
, typename _Predicate
>
154 inline _InputIterator
155 __find_if(_InputIterator __first
, _InputIterator __last
,
156 _Predicate __pred
, input_iterator_tag
)
158 while (__first
!= __last
&& !bool(__pred(*__first
)))
163 /// This is an overload used by find() for the RAI case.
164 template<typename _RandomAccessIterator
, typename _Tp
>
165 _RandomAccessIterator
166 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
167 const _Tp
& __val
, random_access_iterator_tag
)
169 typename iterator_traits
<_RandomAccessIterator
>::difference_type
170 __trip_count
= (__last
- __first
) >> 2;
172 for (; __trip_count
> 0; --__trip_count
)
174 if (*__first
== __val
)
178 if (*__first
== __val
)
182 if (*__first
== __val
)
186 if (*__first
== __val
)
191 switch (__last
- __first
)
194 if (*__first
== __val
)
198 if (*__first
== __val
)
202 if (*__first
== __val
)
211 /// This is an overload used by find_if() for the RAI case.
212 template<typename _RandomAccessIterator
, typename _Predicate
>
213 _RandomAccessIterator
214 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
215 _Predicate __pred
, random_access_iterator_tag
)
217 typename iterator_traits
<_RandomAccessIterator
>::difference_type
218 __trip_count
= (__last
- __first
) >> 2;
220 for (; __trip_count
> 0; --__trip_count
)
222 if (__pred(*__first
))
226 if (__pred(*__first
))
230 if (__pred(*__first
))
234 if (__pred(*__first
))
239 switch (__last
- __first
)
242 if (__pred(*__first
))
246 if (__pred(*__first
))
250 if (__pred(*__first
))
259 #ifdef __GXX_EXPERIMENTAL_CXX0X__
260 /// This is an overload used by find_if_not() for the Input Iterator case.
261 template<typename _InputIterator
, typename _Predicate
>
262 inline _InputIterator
263 __find_if_not(_InputIterator __first
, _InputIterator __last
,
264 _Predicate __pred
, input_iterator_tag
)
266 while (__first
!= __last
&& bool(__pred(*__first
)))
271 /// This is an overload used by find_if_not() for the RAI case.
272 template<typename _RandomAccessIterator
, typename _Predicate
>
273 _RandomAccessIterator
274 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
275 _Predicate __pred
, random_access_iterator_tag
)
277 typename iterator_traits
<_RandomAccessIterator
>::difference_type
278 __trip_count
= (__last
- __first
) >> 2;
280 for (; __trip_count
> 0; --__trip_count
)
282 if (!bool(__pred(*__first
)))
286 if (!bool(__pred(*__first
)))
290 if (!bool(__pred(*__first
)))
294 if (!bool(__pred(*__first
)))
299 switch (__last
- __first
)
302 if (!bool(__pred(*__first
)))
306 if (!bool(__pred(*__first
)))
310 if (!bool(__pred(*__first
)))
322 // set_symmetric_difference
334 * This is an uglified
335 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
336 * overloaded for forward iterators.
338 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
340 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
341 _Integer __count
, const _Tp
& __val
,
342 std::forward_iterator_tag
)
344 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
345 while (__first
!= __last
)
347 typename iterator_traits
<_ForwardIterator
>::difference_type
349 _ForwardIterator __i
= __first
;
351 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
360 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
366 * This is an uglified
367 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
368 * overloaded for random access iterators.
370 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
372 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
373 _Integer __count
, const _Tp
& __val
,
374 std::random_access_iterator_tag
)
377 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
380 _DistanceType __tailSize
= __last
- __first
;
381 const _DistanceType __pattSize
= __count
;
383 if (__tailSize
< __pattSize
)
386 const _DistanceType __skipOffset
= __pattSize
- 1;
387 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
388 __tailSize
-= __pattSize
;
390 while (1) // the main loop...
392 // __lookAhead here is always pointing to the last element of next
394 while (!(*__lookAhead
== __val
)) // the skip loop...
396 if (__tailSize
< __pattSize
)
397 return __last
; // Failure
398 __lookAhead
+= __pattSize
;
399 __tailSize
-= __pattSize
;
401 _DistanceType __remainder
= __skipOffset
;
402 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
403 *__backTrack
== __val
; --__backTrack
)
405 if (--__remainder
== 0)
406 return (__lookAhead
- __skipOffset
); // Success
408 if (__remainder
> __tailSize
)
409 return __last
; // Failure
410 __lookAhead
+= __remainder
;
411 __tailSize
-= __remainder
;
418 * This is an uglified
419 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
421 * overloaded for forward iterators.
423 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
424 typename _BinaryPredicate
>
426 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
427 _Integer __count
, const _Tp
& __val
,
428 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
430 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
433 while (__first
!= __last
)
435 typename iterator_traits
<_ForwardIterator
>::difference_type
437 _ForwardIterator __i
= __first
;
439 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
449 while (__first
!= __last
450 && !bool(__binary_pred(*__first
, __val
)))
457 * This is an uglified
458 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
460 * overloaded for random access iterators.
462 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
463 typename _BinaryPredicate
>
465 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
466 _Integer __count
, const _Tp
& __val
,
467 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
470 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
473 _DistanceType __tailSize
= __last
- __first
;
474 const _DistanceType __pattSize
= __count
;
476 if (__tailSize
< __pattSize
)
479 const _DistanceType __skipOffset
= __pattSize
- 1;
480 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
481 __tailSize
-= __pattSize
;
483 while (1) // the main loop...
485 // __lookAhead here is always pointing to the last element of next
487 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
489 if (__tailSize
< __pattSize
)
490 return __last
; // Failure
491 __lookAhead
+= __pattSize
;
492 __tailSize
-= __pattSize
;
494 _DistanceType __remainder
= __skipOffset
;
495 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
496 __binary_pred(*__backTrack
, __val
); --__backTrack
)
498 if (--__remainder
== 0)
499 return (__lookAhead
- __skipOffset
); // Success
501 if (__remainder
> __tailSize
)
502 return __last
; // Failure
503 __lookAhead
+= __remainder
;
504 __tailSize
-= __remainder
;
508 // find_end for forward iterators.
509 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
511 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
512 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
513 forward_iterator_tag
, forward_iterator_tag
)
515 if (__first2
== __last2
)
519 _ForwardIterator1 __result
= __last1
;
522 _ForwardIterator1 __new_result
523 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
524 if (__new_result
== __last1
)
528 __result
= __new_result
;
529 __first1
= __new_result
;
536 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
537 typename _BinaryPredicate
>
539 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
540 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
541 forward_iterator_tag
, forward_iterator_tag
,
542 _BinaryPredicate __comp
)
544 if (__first2
== __last2
)
548 _ForwardIterator1 __result
= __last1
;
551 _ForwardIterator1 __new_result
552 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
554 if (__new_result
== __last1
)
558 __result
= __new_result
;
559 __first1
= __new_result
;
566 // find_end for bidirectional iterators (much faster).
567 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
568 _BidirectionalIterator1
569 __find_end(_BidirectionalIterator1 __first1
,
570 _BidirectionalIterator1 __last1
,
571 _BidirectionalIterator2 __first2
,
572 _BidirectionalIterator2 __last2
,
573 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
575 // concept requirements
576 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
577 _BidirectionalIterator1
>)
578 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
579 _BidirectionalIterator2
>)
581 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
582 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
584 _RevIterator1
__rlast1(__first1
);
585 _RevIterator2
__rlast2(__first2
);
586 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
588 _RevIterator2(__last2
),
591 if (__rresult
== __rlast1
)
595 _BidirectionalIterator1 __result
= __rresult
.base();
596 std::advance(__result
, -std::distance(__first2
, __last2
));
601 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
602 typename _BinaryPredicate
>
603 _BidirectionalIterator1
604 __find_end(_BidirectionalIterator1 __first1
,
605 _BidirectionalIterator1 __last1
,
606 _BidirectionalIterator2 __first2
,
607 _BidirectionalIterator2 __last2
,
608 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
609 _BinaryPredicate __comp
)
611 // concept requirements
612 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
613 _BidirectionalIterator1
>)
614 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
615 _BidirectionalIterator2
>)
617 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
618 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
620 _RevIterator1
__rlast1(__first1
);
621 _RevIterator2
__rlast2(__first2
);
622 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
623 _RevIterator2(__last2
), __rlast2
,
626 if (__rresult
== __rlast1
)
630 _BidirectionalIterator1 __result
= __rresult
.base();
631 std::advance(__result
, -std::distance(__first2
, __last2
));
637 * @brief Find last matching subsequence in a sequence.
638 * @ingroup non_mutating_algorithms
639 * @param first1 Start of range to search.
640 * @param last1 End of range to search.
641 * @param first2 Start of sequence to match.
642 * @param last2 End of sequence to match.
643 * @return The last iterator @c i in the range
644 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
645 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
646 * such iterator exists.
648 * Searches the range @p [first1,last1) for a sub-sequence that compares
649 * equal value-by-value with the sequence given by @p [first2,last2) and
650 * returns an iterator to the first element of the sub-sequence, or
651 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
652 * last such subsequence contained in [first,last1).
654 * Because the sub-sequence must lie completely within the range
655 * @p [first1,last1) it must start at a position less than
656 * @p last1-(last2-first2) where @p last2-first2 is the length of the
658 * This means that the returned iterator @c i will be in the range
659 * @p [first1,last1-(last2-first2))
661 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
662 inline _ForwardIterator1
663 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
664 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
666 // concept requirements
667 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
668 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
669 __glibcxx_function_requires(_EqualOpConcept
<
670 typename iterator_traits
<_ForwardIterator1
>::value_type
,
671 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
672 __glibcxx_requires_valid_range(__first1
, __last1
);
673 __glibcxx_requires_valid_range(__first2
, __last2
);
675 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
676 std::__iterator_category(__first1
),
677 std::__iterator_category(__first2
));
681 * @brief Find last matching subsequence in a sequence using a predicate.
682 * @ingroup non_mutating_algorithms
683 * @param first1 Start of range to search.
684 * @param last1 End of range to search.
685 * @param first2 Start of sequence to match.
686 * @param last2 End of sequence to match.
687 * @param comp The predicate to use.
688 * @return The last iterator @c i in the range
689 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
690 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
691 * @p last1 if no such iterator exists.
693 * Searches the range @p [first1,last1) for a sub-sequence that compares
694 * equal value-by-value with the sequence given by @p [first2,last2) using
695 * comp as a predicate and returns an iterator to the first element of the
696 * sub-sequence, or @p last1 if the sub-sequence is not found. The
697 * sub-sequence will be the last such subsequence contained in
700 * Because the sub-sequence must lie completely within the range
701 * @p [first1,last1) it must start at a position less than
702 * @p last1-(last2-first2) where @p last2-first2 is the length of the
704 * This means that the returned iterator @c i will be in the range
705 * @p [first1,last1-(last2-first2))
707 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
708 typename _BinaryPredicate
>
709 inline _ForwardIterator1
710 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
711 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
712 _BinaryPredicate __comp
)
714 // concept requirements
715 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
716 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
717 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
718 typename iterator_traits
<_ForwardIterator1
>::value_type
,
719 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
720 __glibcxx_requires_valid_range(__first1
, __last1
);
721 __glibcxx_requires_valid_range(__first2
, __last2
);
723 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
724 std::__iterator_category(__first1
),
725 std::__iterator_category(__first2
),
729 #ifdef __GXX_EXPERIMENTAL_CXX0X__
731 * @brief Checks that a predicate is true for all the elements
733 * @ingroup non_mutating_algorithms
734 * @param first An input iterator.
735 * @param last An input iterator.
736 * @param pred A predicate.
737 * @return True if the check is true, false otherwise.
739 * Returns true if @p pred is true for each element in the range
740 * @p [first,last), and false otherwise.
742 template<typename _InputIterator
, typename _Predicate
>
744 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
745 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
748 * @brief Checks that a predicate is false for all the elements
750 * @ingroup non_mutating_algorithms
751 * @param first An input iterator.
752 * @param last An input iterator.
753 * @param pred A predicate.
754 * @return True if the check is true, false otherwise.
756 * Returns true if @p pred is false for each element in the range
757 * @p [first,last), and false otherwise.
759 template<typename _InputIterator
, typename _Predicate
>
761 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
762 { return __last
== _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
); }
765 * @brief Checks that a predicate is false for at least an element
767 * @ingroup non_mutating_algorithms
768 * @param first An input iterator.
769 * @param last An input iterator.
770 * @param pred A predicate.
771 * @return True if the check is true, false otherwise.
773 * Returns true if an element exists in the range @p [first,last) such that
774 * @p pred is true, and false otherwise.
776 template<typename _InputIterator
, typename _Predicate
>
778 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
779 { return !std::none_of(__first
, __last
, __pred
); }
782 * @brief Find the first element in a sequence for which a
783 * predicate is false.
784 * @ingroup non_mutating_algorithms
785 * @param first An input iterator.
786 * @param last An input iterator.
787 * @param pred A predicate.
788 * @return The first iterator @c i in the range @p [first,last)
789 * such that @p pred(*i) is false, or @p last if no such iterator exists.
791 template<typename _InputIterator
, typename _Predicate
>
792 inline _InputIterator
793 find_if_not(_InputIterator __first
, _InputIterator __last
,
796 // concept requirements
797 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
798 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
799 typename iterator_traits
<_InputIterator
>::value_type
>)
800 __glibcxx_requires_valid_range(__first
, __last
);
801 return std::__find_if_not(__first
, __last
, __pred
,
802 std::__iterator_category(__first
));
806 * @brief Checks whether the sequence is partitioned.
807 * @ingroup mutating_algorithms
808 * @param first An input iterator.
809 * @param last An input iterator.
810 * @param pred A predicate.
811 * @return True if the range @p [first,last) is partioned by @p pred,
812 * i.e. if all elements that satisfy @p pred appear before those that
815 template<typename _InputIterator
, typename _Predicate
>
817 is_partitioned(_InputIterator __first
, _InputIterator __last
,
820 __first
= std::find_if_not(__first
, __last
, __pred
);
821 return std::none_of(__first
, __last
, __pred
);
825 * @brief Find the partition point of a partitioned range.
826 * @ingroup mutating_algorithms
827 * @param first An iterator.
828 * @param last Another iterator.
829 * @param pred A predicate.
830 * @return An iterator @p mid such that @p all_of(first, mid, pred)
831 * and @p none_of(mid, last, pred) are both true.
833 template<typename _ForwardIterator
, typename _Predicate
>
835 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
838 // concept requirements
839 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
840 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
841 typename iterator_traits
<_ForwardIterator
>::value_type
>)
843 // A specific debug-mode test will be necessary...
844 __glibcxx_requires_valid_range(__first
, __last
);
846 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
849 _DistanceType __len
= std::distance(__first
, __last
);
850 _DistanceType __half
;
851 _ForwardIterator __middle
;
857 std::advance(__middle
, __half
);
858 if (__pred(*__middle
))
862 __len
= __len
- __half
- 1;
873 * @brief Copy a sequence, removing elements of a given value.
874 * @ingroup mutating_algorithms
875 * @param first An input iterator.
876 * @param last An input iterator.
877 * @param result An output iterator.
878 * @param value The value to be removed.
879 * @return An iterator designating the end of the resulting sequence.
881 * Copies each element in the range @p [first,last) not equal to @p value
882 * to the range beginning at @p result.
883 * remove_copy() is stable, so the relative order of elements that are
884 * copied is unchanged.
886 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
888 remove_copy(_InputIterator __first
, _InputIterator __last
,
889 _OutputIterator __result
, const _Tp
& __value
)
891 // concept requirements
892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
893 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
894 typename iterator_traits
<_InputIterator
>::value_type
>)
895 __glibcxx_function_requires(_EqualOpConcept
<
896 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
897 __glibcxx_requires_valid_range(__first
, __last
);
899 for (; __first
!= __last
; ++__first
)
900 if (!(*__first
== __value
))
902 *__result
= *__first
;
909 * @brief Copy a sequence, removing elements for which a predicate is true.
910 * @ingroup mutating_algorithms
911 * @param first An input iterator.
912 * @param last An input iterator.
913 * @param result An output iterator.
914 * @param pred A predicate.
915 * @return An iterator designating the end of the resulting sequence.
917 * Copies each element in the range @p [first,last) for which
918 * @p pred returns false to the range beginning at @p result.
920 * remove_copy_if() is stable, so the relative order of elements that are
921 * copied is unchanged.
923 template<typename _InputIterator
, typename _OutputIterator
,
926 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
927 _OutputIterator __result
, _Predicate __pred
)
929 // concept requirements
930 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
931 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
932 typename iterator_traits
<_InputIterator
>::value_type
>)
933 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
934 typename iterator_traits
<_InputIterator
>::value_type
>)
935 __glibcxx_requires_valid_range(__first
, __last
);
937 for (; __first
!= __last
; ++__first
)
938 if (!bool(__pred(*__first
)))
940 *__result
= *__first
;
946 #ifdef __GXX_EXPERIMENTAL_CXX0X__
948 * @brief Copy the elements of a sequence for which a predicate is true.
949 * @ingroup mutating_algorithms
950 * @param first An input iterator.
951 * @param last An input iterator.
952 * @param result An output iterator.
953 * @param pred A predicate.
954 * @return An iterator designating the end of the resulting sequence.
956 * Copies each element in the range @p [first,last) for which
957 * @p pred returns true to the range beginning at @p result.
959 * copy_if() is stable, so the relative order of elements that are
960 * copied is unchanged.
962 template<typename _InputIterator
, typename _OutputIterator
,
965 copy_if(_InputIterator __first
, _InputIterator __last
,
966 _OutputIterator __result
, _Predicate __pred
)
968 // concept requirements
969 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
970 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
971 typename iterator_traits
<_InputIterator
>::value_type
>)
972 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
973 typename iterator_traits
<_InputIterator
>::value_type
>)
974 __glibcxx_requires_valid_range(__first
, __last
);
976 for (; __first
!= __last
; ++__first
)
977 if (__pred(*__first
))
979 *__result
= *__first
;
986 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
988 __copy_n(_InputIterator __first
, _Size __n
,
989 _OutputIterator __result
, input_iterator_tag
)
991 for (; __n
> 0; --__n
)
993 *__result
= *__first
;
1000 template<typename _RandomAccessIterator
, typename _Size
,
1001 typename _OutputIterator
>
1002 inline _OutputIterator
1003 __copy_n(_RandomAccessIterator __first
, _Size __n
,
1004 _OutputIterator __result
, random_access_iterator_tag
)
1005 { return std::copy(__first
, __first
+ __n
, __result
); }
1008 * @brief Copies the range [first,first+n) into [result,result+n).
1009 * @ingroup mutating_algorithms
1010 * @param first An input iterator.
1011 * @param n The number of elements to copy.
1012 * @param result An output iterator.
1015 * This inline function will boil down to a call to @c memmove whenever
1016 * possible. Failing that, if random access iterators are passed, then the
1017 * loop count will be known (and therefore a candidate for compiler
1018 * optimizations such as unrolling).
1020 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1021 inline _OutputIterator
1022 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1024 // concept requirements
1025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1026 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1027 typename iterator_traits
<_InputIterator
>::value_type
>)
1029 return std::__copy_n(__first
, __n
, __result
,
1030 std::__iterator_category(__first
));
1034 * @brief Copy the elements of a sequence to separate output sequences
1035 * depending on the truth value of a predicate.
1036 * @ingroup mutating_algorithms
1037 * @param first An input iterator.
1038 * @param last An input iterator.
1039 * @param out_true An output iterator.
1040 * @param out_false An output iterator.
1041 * @param pred A predicate.
1042 * @return A pair designating the ends of the resulting sequences.
1044 * Copies each element in the range @p [first,last) for which
1045 * @p pred returns true to the range beginning at @p out_true
1046 * and each element for which @p pred returns false to @p out_false.
1048 template<typename _InputIterator
, typename _OutputIterator1
,
1049 typename _OutputIterator2
, typename _Predicate
>
1050 pair
<_OutputIterator1
, _OutputIterator2
>
1051 partition_copy(_InputIterator __first
, _InputIterator __last
,
1052 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1055 // concept requirements
1056 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1057 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1058 typename iterator_traits
<_InputIterator
>::value_type
>)
1059 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1060 typename iterator_traits
<_InputIterator
>::value_type
>)
1061 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1062 typename iterator_traits
<_InputIterator
>::value_type
>)
1063 __glibcxx_requires_valid_range(__first
, __last
);
1065 for (; __first
!= __last
; ++__first
)
1066 if (__pred(*__first
))
1068 *__out_true
= *__first
;
1073 *__out_false
= *__first
;
1077 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1082 * @brief Remove elements from a sequence.
1083 * @ingroup mutating_algorithms
1084 * @param first An input iterator.
1085 * @param last An input iterator.
1086 * @param value The value to be removed.
1087 * @return An iterator designating the end of the resulting sequence.
1089 * All elements equal to @p value are removed from the range
1092 * remove() is stable, so the relative order of elements that are
1093 * not removed is unchanged.
1095 * Elements between the end of the resulting sequence and @p last
1096 * are still present, but their value is unspecified.
1098 template<typename _ForwardIterator
, typename _Tp
>
1100 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1103 // concept requirements
1104 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1106 __glibcxx_function_requires(_EqualOpConcept
<
1107 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1108 __glibcxx_requires_valid_range(__first
, __last
);
1110 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1111 if(__first
== __last
)
1113 _ForwardIterator __result
= __first
;
1115 for(; __first
!= __last
; ++__first
)
1116 if(!(*__first
== __value
))
1118 *__result
= _GLIBCXX_MOVE(*__first
);
1125 * @brief Remove elements from a sequence using a predicate.
1126 * @ingroup mutating_algorithms
1127 * @param first A forward iterator.
1128 * @param last A forward iterator.
1129 * @param pred A predicate.
1130 * @return An iterator designating the end of the resulting sequence.
1132 * All elements for which @p pred returns true are removed from the range
1135 * remove_if() is stable, so the relative order of elements that are
1136 * not removed is unchanged.
1138 * Elements between the end of the resulting sequence and @p last
1139 * are still present, but their value is unspecified.
1141 template<typename _ForwardIterator
, typename _Predicate
>
1143 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1146 // concept requirements
1147 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1149 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1150 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1151 __glibcxx_requires_valid_range(__first
, __last
);
1153 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1154 if(__first
== __last
)
1156 _ForwardIterator __result
= __first
;
1158 for(; __first
!= __last
; ++__first
)
1159 if(!bool(__pred(*__first
)))
1161 *__result
= _GLIBCXX_MOVE(*__first
);
1168 * @brief Remove consecutive duplicate values from a sequence.
1169 * @ingroup mutating_algorithms
1170 * @param first A forward iterator.
1171 * @param last A forward iterator.
1172 * @return An iterator designating the end of the resulting sequence.
1174 * Removes all but the first element from each group of consecutive
1175 * values that compare equal.
1176 * unique() is stable, so the relative order of elements that are
1177 * not removed is unchanged.
1178 * Elements between the end of the resulting sequence and @p last
1179 * are still present, but their value is unspecified.
1181 template<typename _ForwardIterator
>
1183 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1185 // concept requirements
1186 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1188 __glibcxx_function_requires(_EqualityComparableConcept
<
1189 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1190 __glibcxx_requires_valid_range(__first
, __last
);
1192 // Skip the beginning, if already unique.
1193 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1194 if (__first
== __last
)
1197 // Do the real copy work.
1198 _ForwardIterator __dest
= __first
;
1200 while (++__first
!= __last
)
1201 if (!(*__dest
== *__first
))
1202 *++__dest
= _GLIBCXX_MOVE(*__first
);
1207 * @brief Remove consecutive values from a sequence using a predicate.
1208 * @ingroup mutating_algorithms
1209 * @param first A forward iterator.
1210 * @param last A forward iterator.
1211 * @param binary_pred A binary predicate.
1212 * @return An iterator designating the end of the resulting sequence.
1214 * Removes all but the first element from each group of consecutive
1215 * values for which @p binary_pred returns true.
1216 * unique() is stable, so the relative order of elements that are
1217 * not removed is unchanged.
1218 * Elements between the end of the resulting sequence and @p last
1219 * are still present, but their value is unspecified.
1221 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1223 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1224 _BinaryPredicate __binary_pred
)
1226 // concept requirements
1227 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1229 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1230 typename iterator_traits
<_ForwardIterator
>::value_type
,
1231 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1232 __glibcxx_requires_valid_range(__first
, __last
);
1234 // Skip the beginning, if already unique.
1235 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1236 if (__first
== __last
)
1239 // Do the real copy work.
1240 _ForwardIterator __dest
= __first
;
1242 while (++__first
!= __last
)
1243 if (!bool(__binary_pred(*__dest
, *__first
)))
1244 *++__dest
= _GLIBCXX_MOVE(*__first
);
1249 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1251 * overloaded for forward iterators and output iterator as result.
1253 template<typename _ForwardIterator
, typename _OutputIterator
>
1255 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1256 _OutputIterator __result
,
1257 forward_iterator_tag
, output_iterator_tag
)
1259 // concept requirements -- taken care of in dispatching function
1260 _ForwardIterator __next
= __first
;
1261 *__result
= *__first
;
1262 while (++__next
!= __last
)
1263 if (!(*__first
== *__next
))
1266 *++__result
= *__first
;
1272 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1274 * overloaded for input iterators and output iterator as result.
1276 template<typename _InputIterator
, typename _OutputIterator
>
1278 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1279 _OutputIterator __result
,
1280 input_iterator_tag
, output_iterator_tag
)
1282 // concept requirements -- taken care of in dispatching function
1283 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1284 *__result
= __value
;
1285 while (++__first
!= __last
)
1286 if (!(__value
== *__first
))
1289 *++__result
= __value
;
1295 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1297 * overloaded for input iterators and forward iterator as result.
1299 template<typename _InputIterator
, typename _ForwardIterator
>
1301 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1302 _ForwardIterator __result
,
1303 input_iterator_tag
, forward_iterator_tag
)
1305 // concept requirements -- taken care of in dispatching function
1306 *__result
= *__first
;
1307 while (++__first
!= __last
)
1308 if (!(*__result
== *__first
))
1309 *++__result
= *__first
;
1314 * This is an uglified
1315 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1317 * overloaded for forward iterators and output iterator as result.
1319 template<typename _ForwardIterator
, typename _OutputIterator
,
1320 typename _BinaryPredicate
>
1322 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1323 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1324 forward_iterator_tag
, output_iterator_tag
)
1326 // concept requirements -- iterators already checked
1327 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1328 typename iterator_traits
<_ForwardIterator
>::value_type
,
1329 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1331 _ForwardIterator __next
= __first
;
1332 *__result
= *__first
;
1333 while (++__next
!= __last
)
1334 if (!bool(__binary_pred(*__first
, *__next
)))
1337 *++__result
= *__first
;
1343 * This is an uglified
1344 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1346 * overloaded for input iterators and output iterator as result.
1348 template<typename _InputIterator
, typename _OutputIterator
,
1349 typename _BinaryPredicate
>
1351 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1352 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1353 input_iterator_tag
, output_iterator_tag
)
1355 // concept requirements -- iterators already checked
1356 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1357 typename iterator_traits
<_InputIterator
>::value_type
,
1358 typename iterator_traits
<_InputIterator
>::value_type
>)
1360 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1361 *__result
= __value
;
1362 while (++__first
!= __last
)
1363 if (!bool(__binary_pred(__value
, *__first
)))
1366 *++__result
= __value
;
1372 * This is an uglified
1373 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1375 * overloaded for input iterators and forward iterator as result.
1377 template<typename _InputIterator
, typename _ForwardIterator
,
1378 typename _BinaryPredicate
>
1380 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1381 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1382 input_iterator_tag
, forward_iterator_tag
)
1384 // concept requirements -- iterators already checked
1385 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1386 typename iterator_traits
<_ForwardIterator
>::value_type
,
1387 typename iterator_traits
<_InputIterator
>::value_type
>)
1389 *__result
= *__first
;
1390 while (++__first
!= __last
)
1391 if (!bool(__binary_pred(*__result
, *__first
)))
1392 *++__result
= *__first
;
1397 * This is an uglified reverse(_BidirectionalIterator,
1398 * _BidirectionalIterator)
1399 * overloaded for bidirectional iterators.
1401 template<typename _BidirectionalIterator
>
1403 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1404 bidirectional_iterator_tag
)
1407 if (__first
== __last
|| __first
== --__last
)
1411 std::iter_swap(__first
, __last
);
1417 * This is an uglified reverse(_BidirectionalIterator,
1418 * _BidirectionalIterator)
1419 * overloaded for random access iterators.
1421 template<typename _RandomAccessIterator
>
1423 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1424 random_access_iterator_tag
)
1426 if (__first
== __last
)
1429 while (__first
< __last
)
1431 std::iter_swap(__first
, __last
);
1438 * @brief Reverse a sequence.
1439 * @ingroup mutating_algorithms
1440 * @param first A bidirectional iterator.
1441 * @param last A bidirectional iterator.
1442 * @return reverse() returns no value.
1444 * Reverses the order of the elements in the range @p [first,last),
1445 * so that the first element becomes the last etc.
1446 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1447 * swaps @p *(first+i) and @p *(last-(i+1))
1449 template<typename _BidirectionalIterator
>
1451 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1453 // concept requirements
1454 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1455 _BidirectionalIterator
>)
1456 __glibcxx_requires_valid_range(__first
, __last
);
1457 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1461 * @brief Copy a sequence, reversing its elements.
1462 * @ingroup mutating_algorithms
1463 * @param first A bidirectional iterator.
1464 * @param last A bidirectional iterator.
1465 * @param result An output iterator.
1466 * @return An iterator designating the end of the resulting sequence.
1468 * Copies the elements in the range @p [first,last) to the range
1469 * @p [result,result+(last-first)) such that the order of the
1470 * elements is reversed.
1471 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1472 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1473 * The ranges @p [first,last) and @p [result,result+(last-first))
1476 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1478 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1479 _OutputIterator __result
)
1481 // concept requirements
1482 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1483 _BidirectionalIterator
>)
1484 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1485 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1486 __glibcxx_requires_valid_range(__first
, __last
);
1488 while (__first
!= __last
)
1491 *__result
= *__last
;
1498 * This is a helper function for the rotate algorithm specialized on RAIs.
1499 * It returns the greatest common divisor of two integer values.
1501 template<typename _EuclideanRingElement
>
1502 _EuclideanRingElement
1503 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1507 _EuclideanRingElement __t
= __m
% __n
;
1514 /// This is a helper function for the rotate algorithm.
1515 template<typename _ForwardIterator
>
1517 __rotate(_ForwardIterator __first
,
1518 _ForwardIterator __middle
,
1519 _ForwardIterator __last
,
1520 forward_iterator_tag
)
1522 if (__first
== __middle
|| __last
== __middle
)
1525 _ForwardIterator __first2
= __middle
;
1528 std::iter_swap(__first
, __first2
);
1531 if (__first
== __middle
)
1532 __middle
= __first2
;
1534 while (__first2
!= __last
);
1536 __first2
= __middle
;
1538 while (__first2
!= __last
)
1540 std::iter_swap(__first
, __first2
);
1543 if (__first
== __middle
)
1544 __middle
= __first2
;
1545 else if (__first2
== __last
)
1546 __first2
= __middle
;
1550 /// This is a helper function for the rotate algorithm.
1551 template<typename _BidirectionalIterator
>
1553 __rotate(_BidirectionalIterator __first
,
1554 _BidirectionalIterator __middle
,
1555 _BidirectionalIterator __last
,
1556 bidirectional_iterator_tag
)
1558 // concept requirements
1559 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1560 _BidirectionalIterator
>)
1562 if (__first
== __middle
|| __last
== __middle
)
1565 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1566 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1568 while (__first
!= __middle
&& __middle
!= __last
)
1570 std::iter_swap(__first
, --__last
);
1574 if (__first
== __middle
)
1575 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1577 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1580 /// This is a helper function for the rotate algorithm.
1581 template<typename _RandomAccessIterator
>
1583 __rotate(_RandomAccessIterator __first
,
1584 _RandomAccessIterator __middle
,
1585 _RandomAccessIterator __last
,
1586 random_access_iterator_tag
)
1588 // concept requirements
1589 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1590 _RandomAccessIterator
>)
1592 if (__first
== __middle
|| __last
== __middle
)
1595 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1597 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1600 const _Distance __n
= __last
- __first
;
1601 const _Distance __k
= __middle
- __first
;
1602 const _Distance __l
= __n
- __k
;
1606 std::swap_ranges(__first
, __middle
, __middle
);
1610 const _Distance __d
= std::__gcd(__n
, __k
);
1612 for (_Distance __i
= 0; __i
< __d
; __i
++)
1614 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1615 _RandomAccessIterator __p
= __first
;
1619 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1621 if (__p
> __first
+ __l
)
1623 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1627 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1633 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1635 if (__p
< __last
- __k
)
1637 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1640 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1645 *__p
= _GLIBCXX_MOVE(__tmp
);
1651 * @brief Rotate the elements of a sequence.
1652 * @ingroup mutating_algorithms
1653 * @param first A forward iterator.
1654 * @param middle A forward iterator.
1655 * @param last A forward iterator.
1658 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1659 * positions so that the element at @p middle is moved to @p first, the
1660 * element at @p middle+1 is moved to @first+1 and so on for each element
1661 * in the range @p [first,last).
1663 * This effectively swaps the ranges @p [first,middle) and
1666 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1667 * each @p n in the range @p [0,last-first).
1669 template<typename _ForwardIterator
>
1671 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1672 _ForwardIterator __last
)
1674 // concept requirements
1675 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1677 __glibcxx_requires_valid_range(__first
, __middle
);
1678 __glibcxx_requires_valid_range(__middle
, __last
);
1680 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1682 std::__rotate(__first
, __middle
, __last
, _IterType());
1686 * @brief Copy a sequence, rotating its elements.
1687 * @ingroup mutating_algorithms
1688 * @param first A forward iterator.
1689 * @param middle A forward iterator.
1690 * @param last A forward iterator.
1691 * @param result An output iterator.
1692 * @return An iterator designating the end of the resulting sequence.
1694 * Copies the elements of the range @p [first,last) to the range
1695 * beginning at @result, rotating the copied elements by @p (middle-first)
1696 * positions so that the element at @p middle is moved to @p result, the
1697 * element at @p middle+1 is moved to @result+1 and so on for each element
1698 * in the range @p [first,last).
1700 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1701 * each @p n in the range @p [0,last-first).
1703 template<typename _ForwardIterator
, typename _OutputIterator
>
1705 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1706 _ForwardIterator __last
, _OutputIterator __result
)
1708 // concept requirements
1709 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1710 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1711 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1712 __glibcxx_requires_valid_range(__first
, __middle
);
1713 __glibcxx_requires_valid_range(__middle
, __last
);
1715 return std::copy(__first
, __middle
,
1716 std::copy(__middle
, __last
, __result
));
1719 /// This is a helper function...
1720 template<typename _ForwardIterator
, typename _Predicate
>
1722 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1723 _Predicate __pred
, forward_iterator_tag
)
1725 if (__first
== __last
)
1728 while (__pred(*__first
))
1729 if (++__first
== __last
)
1732 _ForwardIterator __next
= __first
;
1734 while (++__next
!= __last
)
1735 if (__pred(*__next
))
1737 std::iter_swap(__first
, __next
);
1744 /// This is a helper function...
1745 template<typename _BidirectionalIterator
, typename _Predicate
>
1746 _BidirectionalIterator
1747 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1748 _Predicate __pred
, bidirectional_iterator_tag
)
1753 if (__first
== __last
)
1755 else if (__pred(*__first
))
1761 if (__first
== __last
)
1763 else if (!bool(__pred(*__last
)))
1767 std::iter_swap(__first
, __last
);
1774 /// This is a helper function...
1775 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1777 __inplace_stable_partition(_ForwardIterator __first
,
1778 _ForwardIterator __last
,
1779 _Predicate __pred
, _Distance __len
)
1782 return __pred(*__first
) ? __last
: __first
;
1783 _ForwardIterator __middle
= __first
;
1784 std::advance(__middle
, __len
/ 2);
1785 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1789 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1793 std::rotate(__begin
, __middle
, __end
);
1794 std::advance(__begin
, std::distance(__middle
, __end
));
1798 /// This is a helper function...
1799 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1802 __stable_partition_adaptive(_ForwardIterator __first
,
1803 _ForwardIterator __last
,
1804 _Predicate __pred
, _Distance __len
,
1806 _Distance __buffer_size
)
1808 if (__len
<= __buffer_size
)
1810 _ForwardIterator __result1
= __first
;
1811 _Pointer __result2
= __buffer
;
1812 for (; __first
!= __last
; ++__first
)
1813 if (__pred(*__first
))
1815 *__result1
= *__first
;
1820 *__result2
= *__first
;
1823 std::copy(__buffer
, __result2
, __result1
);
1828 _ForwardIterator __middle
= __first
;
1829 std::advance(__middle
, __len
/ 2);
1830 _ForwardIterator __begin
=
1831 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1832 __len
/ 2, __buffer
,
1834 _ForwardIterator __end
=
1835 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1837 __buffer
, __buffer_size
);
1838 std::rotate(__begin
, __middle
, __end
);
1839 std::advance(__begin
, std::distance(__middle
, __end
));
1845 * @brief Move elements for which a predicate is true to the beginning
1846 * of a sequence, preserving relative ordering.
1847 * @ingroup mutating_algorithms
1848 * @param first A forward iterator.
1849 * @param last A forward iterator.
1850 * @param pred A predicate functor.
1851 * @return An iterator @p middle such that @p pred(i) is true for each
1852 * iterator @p i in the range @p [first,middle) and false for each @p i
1853 * in the range @p [middle,last).
1855 * Performs the same function as @p partition() with the additional
1856 * guarantee that the relative ordering of elements in each group is
1857 * preserved, so any two elements @p x and @p y in the range
1858 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1859 * relative ordering after calling @p stable_partition().
1861 template<typename _ForwardIterator
, typename _Predicate
>
1863 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1866 // concept requirements
1867 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1869 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1870 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1871 __glibcxx_requires_valid_range(__first
, __last
);
1873 if (__first
== __last
)
1877 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1879 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1882 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1884 if (__buf
.size() > 0)
1886 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1887 _DistanceType(__buf
.requested_size()),
1889 _DistanceType(__buf
.size()));
1892 std::__inplace_stable_partition(__first
, __last
, __pred
,
1893 _DistanceType(__buf
.requested_size()));
1897 /// This is a helper function for the sort routines.
1898 template<typename _RandomAccessIterator
>
1900 __heap_select(_RandomAccessIterator __first
,
1901 _RandomAccessIterator __middle
,
1902 _RandomAccessIterator __last
)
1904 std::make_heap(__first
, __middle
);
1905 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1906 if (*__i
< *__first
)
1907 std::__pop_heap(__first
, __middle
, __i
);
1910 /// This is a helper function for the sort routines.
1911 template<typename _RandomAccessIterator
, typename _Compare
>
1913 __heap_select(_RandomAccessIterator __first
,
1914 _RandomAccessIterator __middle
,
1915 _RandomAccessIterator __last
, _Compare __comp
)
1917 std::make_heap(__first
, __middle
, __comp
);
1918 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1919 if (__comp(*__i
, *__first
))
1920 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1926 * @brief Copy the smallest elements of a sequence.
1927 * @ingroup sorting_algorithms
1928 * @param first An iterator.
1929 * @param last Another iterator.
1930 * @param result_first A random-access iterator.
1931 * @param result_last Another random-access iterator.
1932 * @return An iterator indicating the end of the resulting sequence.
1934 * Copies and sorts the smallest N values from the range @p [first,last)
1935 * to the range beginning at @p result_first, where the number of
1936 * elements to be copied, @p N, is the smaller of @p (last-first) and
1937 * @p (result_last-result_first).
1938 * After the sort if @p i and @j are iterators in the range
1939 * @p [result_first,result_first+N) such that @i precedes @j then
1940 * @p *j<*i is false.
1941 * The value returned is @p result_first+N.
1943 template<typename _InputIterator
, typename _RandomAccessIterator
>
1944 _RandomAccessIterator
1945 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1946 _RandomAccessIterator __result_first
,
1947 _RandomAccessIterator __result_last
)
1949 typedef typename iterator_traits
<_InputIterator
>::value_type
1951 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1953 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1956 // concept requirements
1957 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1958 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1960 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1962 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1963 __glibcxx_requires_valid_range(__first
, __last
);
1964 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1966 if (__result_first
== __result_last
)
1967 return __result_last
;
1968 _RandomAccessIterator __result_real_last
= __result_first
;
1969 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1971 *__result_real_last
= *__first
;
1972 ++__result_real_last
;
1975 std::make_heap(__result_first
, __result_real_last
);
1976 while (__first
!= __last
)
1978 if (*__first
< *__result_first
)
1979 std::__adjust_heap(__result_first
, _DistanceType(0),
1980 _DistanceType(__result_real_last
1982 _InputValueType(*__first
));
1985 std::sort_heap(__result_first
, __result_real_last
);
1986 return __result_real_last
;
1990 * @brief Copy the smallest elements of a sequence using a predicate for
1992 * @ingroup sorting_algorithms
1993 * @param first An input iterator.
1994 * @param last Another input iterator.
1995 * @param result_first A random-access iterator.
1996 * @param result_last Another random-access iterator.
1997 * @param comp A comparison functor.
1998 * @return An iterator indicating the end of the resulting sequence.
2000 * Copies and sorts the smallest N values from the range @p [first,last)
2001 * to the range beginning at @p result_first, where the number of
2002 * elements to be copied, @p N, is the smaller of @p (last-first) and
2003 * @p (result_last-result_first).
2004 * After the sort if @p i and @j are iterators in the range
2005 * @p [result_first,result_first+N) such that @i precedes @j then
2006 * @p comp(*j,*i) is false.
2007 * The value returned is @p result_first+N.
2009 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2010 _RandomAccessIterator
2011 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2012 _RandomAccessIterator __result_first
,
2013 _RandomAccessIterator __result_last
,
2016 typedef typename iterator_traits
<_InputIterator
>::value_type
2018 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2020 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2023 // concept requirements
2024 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2025 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2026 _RandomAccessIterator
>)
2027 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2029 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2030 _InputValueType
, _OutputValueType
>)
2031 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2032 _OutputValueType
, _OutputValueType
>)
2033 __glibcxx_requires_valid_range(__first
, __last
);
2034 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2036 if (__result_first
== __result_last
)
2037 return __result_last
;
2038 _RandomAccessIterator __result_real_last
= __result_first
;
2039 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2041 *__result_real_last
= *__first
;
2042 ++__result_real_last
;
2045 std::make_heap(__result_first
, __result_real_last
, __comp
);
2046 while (__first
!= __last
)
2048 if (__comp(*__first
, *__result_first
))
2049 std::__adjust_heap(__result_first
, _DistanceType(0),
2050 _DistanceType(__result_real_last
2052 _InputValueType(*__first
),
2056 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2057 return __result_real_last
;
2060 /// This is a helper function for the sort routine.
2061 template<typename _RandomAccessIterator
, typename _Tp
>
2063 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2065 _RandomAccessIterator __next
= __last
;
2067 while (__val
< *__next
)
2076 /// This is a helper function for the sort routine.
2077 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2079 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2082 _RandomAccessIterator __next
= __last
;
2084 while (__comp(__val
, *__next
))
2093 /// This is a helper function for the sort routine.
2094 template<typename _RandomAccessIterator
>
2096 __insertion_sort(_RandomAccessIterator __first
,
2097 _RandomAccessIterator __last
)
2099 if (__first
== __last
)
2102 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2104 typename iterator_traits
<_RandomAccessIterator
>::value_type
2106 if (__val
< *__first
)
2108 std::copy_backward(__first
, __i
, __i
+ 1);
2112 std::__unguarded_linear_insert(__i
, __val
);
2116 /// This is a helper function for the sort routine.
2117 template<typename _RandomAccessIterator
, typename _Compare
>
2119 __insertion_sort(_RandomAccessIterator __first
,
2120 _RandomAccessIterator __last
, _Compare __comp
)
2122 if (__first
== __last
) return;
2124 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2126 typename iterator_traits
<_RandomAccessIterator
>::value_type
2128 if (__comp(__val
, *__first
))
2130 std::copy_backward(__first
, __i
, __i
+ 1);
2134 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2138 /// This is a helper function for the sort routine.
2139 template<typename _RandomAccessIterator
>
2141 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2142 _RandomAccessIterator __last
)
2144 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2147 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2148 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2151 /// This is a helper function for the sort routine.
2152 template<typename _RandomAccessIterator
, typename _Compare
>
2154 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2155 _RandomAccessIterator __last
, _Compare __comp
)
2157 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2160 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2161 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2166 * This controls some aspect of the sort routines.
2168 enum { _S_threshold
= 16 };
2170 /// This is a helper function for the sort routine.
2171 template<typename _RandomAccessIterator
>
2173 __final_insertion_sort(_RandomAccessIterator __first
,
2174 _RandomAccessIterator __last
)
2176 if (__last
- __first
> int(_S_threshold
))
2178 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2179 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2182 std::__insertion_sort(__first
, __last
);
2185 /// This is a helper function for the sort routine.
2186 template<typename _RandomAccessIterator
, typename _Compare
>
2188 __final_insertion_sort(_RandomAccessIterator __first
,
2189 _RandomAccessIterator __last
, _Compare __comp
)
2191 if (__last
- __first
> int(_S_threshold
))
2193 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2194 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2198 std::__insertion_sort(__first
, __last
, __comp
);
2201 /// This is a helper function...
2202 template<typename _RandomAccessIterator
, typename _Tp
>
2203 _RandomAccessIterator
2204 __unguarded_partition(_RandomAccessIterator __first
,
2205 _RandomAccessIterator __last
, _Tp __pivot
)
2209 while (*__first
< __pivot
)
2212 while (__pivot
< *__last
)
2214 if (!(__first
< __last
))
2216 std::iter_swap(__first
, __last
);
2221 /// This is a helper function...
2222 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2223 _RandomAccessIterator
2224 __unguarded_partition(_RandomAccessIterator __first
,
2225 _RandomAccessIterator __last
,
2226 _Tp __pivot
, _Compare __comp
)
2230 while (__comp(*__first
, __pivot
))
2233 while (__comp(__pivot
, *__last
))
2235 if (!(__first
< __last
))
2237 std::iter_swap(__first
, __last
);
2242 /// This is a helper function for the sort routine.
2243 template<typename _RandomAccessIterator
, typename _Size
>
2245 __introsort_loop(_RandomAccessIterator __first
,
2246 _RandomAccessIterator __last
,
2247 _Size __depth_limit
)
2249 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2252 while (__last
- __first
> int(_S_threshold
))
2254 if (__depth_limit
== 0)
2256 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2260 _RandomAccessIterator __cut
=
2261 std::__unguarded_partition(__first
, __last
,
2262 _ValueType(std::__median(*__first
,
2269 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2274 /// This is a helper function for the sort routine.
2275 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2277 __introsort_loop(_RandomAccessIterator __first
,
2278 _RandomAccessIterator __last
,
2279 _Size __depth_limit
, _Compare __comp
)
2281 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2284 while (__last
- __first
> int(_S_threshold
))
2286 if (__depth_limit
== 0)
2288 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2292 _RandomAccessIterator __cut
=
2293 std::__unguarded_partition(__first
, __last
,
2294 _ValueType(std::__median(*__first
,
2302 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2307 /// This is a helper function for the sort routines. Precondition: __n > 0.
2308 template<typename _Size
>
2313 for (__k
= 0; __n
!= 0; __n
>>= 1)
2320 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2324 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2328 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2332 template<typename _RandomAccessIterator
, typename _Size
>
2334 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2335 _RandomAccessIterator __last
, _Size __depth_limit
)
2337 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2340 while (__last
- __first
> 3)
2342 if (__depth_limit
== 0)
2344 std::__heap_select(__first
, __nth
+ 1, __last
);
2346 // Place the nth largest element in its final position.
2347 std::iter_swap(__first
, __nth
);
2351 _RandomAccessIterator __cut
=
2352 std::__unguarded_partition(__first
, __last
,
2353 _ValueType(std::__median(*__first
,
2365 std::__insertion_sort(__first
, __last
);
2368 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2370 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2371 _RandomAccessIterator __last
, _Size __depth_limit
,
2374 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2377 while (__last
- __first
> 3)
2379 if (__depth_limit
== 0)
2381 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2382 // Place the nth largest element in its final position.
2383 std::iter_swap(__first
, __nth
);
2387 _RandomAccessIterator __cut
=
2388 std::__unguarded_partition(__first
, __last
,
2389 _ValueType(std::__median(*__first
,
2402 std::__insertion_sort(__first
, __last
, __comp
);
2408 * @brief Finds the first position in which @a val could be inserted
2409 * without changing the ordering.
2410 * @param first An iterator.
2411 * @param last Another iterator.
2412 * @param val The search term.
2413 * @return An iterator pointing to the first element "not less
2414 * than" @a val, or end() if every element is less than
2416 * @ingroup binary_search_algorithms
2418 template<typename _ForwardIterator
, typename _Tp
>
2420 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2423 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2425 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2428 // concept requirements
2429 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2430 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2431 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2433 _DistanceType __len
= std::distance(__first
, __last
);
2434 _DistanceType __half
;
2435 _ForwardIterator __middle
;
2439 __half
= __len
>> 1;
2441 std::advance(__middle
, __half
);
2442 if (*__middle
< __val
)
2446 __len
= __len
- __half
- 1;
2455 * @brief Finds the first position in which @a val could be inserted
2456 * without changing the ordering.
2457 * @ingroup binary_search_algorithms
2458 * @param first An iterator.
2459 * @param last Another iterator.
2460 * @param val The search term.
2461 * @param comp A functor to use for comparisons.
2462 * @return An iterator pointing to the first element "not less than" @a val,
2463 * or end() if every element is less than @a val.
2464 * @ingroup binary_search_algorithms
2466 * The comparison function should have the same effects on ordering as
2467 * the function used for the initial sort.
2469 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2471 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2472 const _Tp
& __val
, _Compare __comp
)
2474 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2476 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2479 // concept requirements
2480 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2481 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2483 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2486 _DistanceType __len
= std::distance(__first
, __last
);
2487 _DistanceType __half
;
2488 _ForwardIterator __middle
;
2492 __half
= __len
>> 1;
2494 std::advance(__middle
, __half
);
2495 if (__comp(*__middle
, __val
))
2499 __len
= __len
- __half
- 1;
2508 * @brief Finds the last position in which @a val could be inserted
2509 * without changing the ordering.
2510 * @ingroup binary_search_algorithms
2511 * @param first An iterator.
2512 * @param last Another iterator.
2513 * @param val The search term.
2514 * @return An iterator pointing to the first element greater than @a val,
2515 * or end() if no elements are greater than @a val.
2516 * @ingroup binary_search_algorithms
2518 template<typename _ForwardIterator
, typename _Tp
>
2520 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2523 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2525 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2528 // concept requirements
2529 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2530 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2531 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2533 _DistanceType __len
= std::distance(__first
, __last
);
2534 _DistanceType __half
;
2535 _ForwardIterator __middle
;
2539 __half
= __len
>> 1;
2541 std::advance(__middle
, __half
);
2542 if (__val
< *__middle
)
2548 __len
= __len
- __half
- 1;
2555 * @brief Finds the last position in which @a val could be inserted
2556 * without changing the ordering.
2557 * @ingroup binary_search_algorithms
2558 * @param first An iterator.
2559 * @param last Another iterator.
2560 * @param val The search term.
2561 * @param comp A functor to use for comparisons.
2562 * @return An iterator pointing to the first element greater than @a val,
2563 * or end() if no elements are greater than @a val.
2564 * @ingroup binary_search_algorithms
2566 * The comparison function should have the same effects on ordering as
2567 * the function used for the initial sort.
2569 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2571 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2572 const _Tp
& __val
, _Compare __comp
)
2574 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2576 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2579 // concept requirements
2580 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2581 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2583 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2586 _DistanceType __len
= std::distance(__first
, __last
);
2587 _DistanceType __half
;
2588 _ForwardIterator __middle
;
2592 __half
= __len
>> 1;
2594 std::advance(__middle
, __half
);
2595 if (__comp(__val
, *__middle
))
2601 __len
= __len
- __half
- 1;
2608 * @brief Finds the largest subrange in which @a val could be inserted
2609 * at any place in it without changing the ordering.
2610 * @ingroup binary_search_algorithms
2611 * @param first An iterator.
2612 * @param last Another iterator.
2613 * @param val The search term.
2614 * @return An pair of iterators defining the subrange.
2615 * @ingroup binary_search_algorithms
2617 * This is equivalent to
2619 * std::make_pair(lower_bound(first, last, val),
2620 * upper_bound(first, last, val))
2622 * but does not actually call those functions.
2624 template<typename _ForwardIterator
, typename _Tp
>
2625 pair
<_ForwardIterator
, _ForwardIterator
>
2626 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2629 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2631 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2634 // concept requirements
2635 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2636 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2637 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2638 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2639 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2641 _DistanceType __len
= std::distance(__first
, __last
);
2642 _DistanceType __half
;
2643 _ForwardIterator __middle
, __left
, __right
;
2647 __half
= __len
>> 1;
2649 std::advance(__middle
, __half
);
2650 if (*__middle
< __val
)
2654 __len
= __len
- __half
- 1;
2656 else if (__val
< *__middle
)
2660 __left
= std::lower_bound(__first
, __middle
, __val
);
2661 std::advance(__first
, __len
);
2662 __right
= std::upper_bound(++__middle
, __first
, __val
);
2663 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2666 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2670 * @brief Finds the largest subrange in which @a val could be inserted
2671 * at any place in it without changing the ordering.
2672 * @param first An iterator.
2673 * @param last Another iterator.
2674 * @param val The search term.
2675 * @param comp A functor to use for comparisons.
2676 * @return An pair of iterators defining the subrange.
2677 * @ingroup binary_search_algorithms
2679 * This is equivalent to
2681 * std::make_pair(lower_bound(first, last, val, comp),
2682 * upper_bound(first, last, val, comp))
2684 * but does not actually call those functions.
2686 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2687 pair
<_ForwardIterator
, _ForwardIterator
>
2688 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2692 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2694 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2697 // concept requirements
2698 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2699 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2701 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2703 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2705 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2708 _DistanceType __len
= std::distance(__first
, __last
);
2709 _DistanceType __half
;
2710 _ForwardIterator __middle
, __left
, __right
;
2714 __half
= __len
>> 1;
2716 std::advance(__middle
, __half
);
2717 if (__comp(*__middle
, __val
))
2721 __len
= __len
- __half
- 1;
2723 else if (__comp(__val
, *__middle
))
2727 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2728 std::advance(__first
, __len
);
2729 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2730 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2733 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2737 * @brief Determines whether an element exists in a range.
2738 * @ingroup binary_search_algorithms
2739 * @param first An iterator.
2740 * @param last Another iterator.
2741 * @param val The search term.
2742 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2744 * Note that this does not actually return an iterator to @a val. For
2745 * that, use std::find or a container's specialized find member functions.
2747 template<typename _ForwardIterator
, typename _Tp
>
2749 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2752 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2755 // concept requirements
2756 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2757 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2758 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2759 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2761 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2762 return __i
!= __last
&& !(__val
< *__i
);
2766 * @brief Determines whether an element exists in a range.
2767 * @ingroup binary_search_algorithms
2768 * @param first An iterator.
2769 * @param last Another iterator.
2770 * @param val The search term.
2771 * @param comp A functor to use for comparisons.
2772 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2774 * Note that this does not actually return an iterator to @a val. For
2775 * that, use std::find or a container's specialized find member functions.
2777 * The comparison function should have the same effects on ordering as
2778 * the function used for the initial sort.
2780 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2782 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2783 const _Tp
& __val
, _Compare __comp
)
2785 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2788 // concept requirements
2789 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2790 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2792 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2794 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2797 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2798 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2803 /// This is a helper function for the merge routines.
2804 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2805 typename _BidirectionalIterator3
>
2806 _BidirectionalIterator3
2807 __merge_backward(_BidirectionalIterator1 __first1
,
2808 _BidirectionalIterator1 __last1
,
2809 _BidirectionalIterator2 __first2
,
2810 _BidirectionalIterator2 __last2
,
2811 _BidirectionalIterator3 __result
)
2813 if (__first1
== __last1
)
2814 return std::copy_backward(__first2
, __last2
, __result
);
2815 if (__first2
== __last2
)
2816 return std::copy_backward(__first1
, __last1
, __result
);
2821 if (*__last2
< *__last1
)
2823 *--__result
= *__last1
;
2824 if (__first1
== __last1
)
2825 return std::copy_backward(__first2
, ++__last2
, __result
);
2830 *--__result
= *__last2
;
2831 if (__first2
== __last2
)
2832 return std::copy_backward(__first1
, ++__last1
, __result
);
2838 /// This is a helper function for the merge routines.
2839 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2840 typename _BidirectionalIterator3
, typename _Compare
>
2841 _BidirectionalIterator3
2842 __merge_backward(_BidirectionalIterator1 __first1
,
2843 _BidirectionalIterator1 __last1
,
2844 _BidirectionalIterator2 __first2
,
2845 _BidirectionalIterator2 __last2
,
2846 _BidirectionalIterator3 __result
,
2849 if (__first1
== __last1
)
2850 return std::copy_backward(__first2
, __last2
, __result
);
2851 if (__first2
== __last2
)
2852 return std::copy_backward(__first1
, __last1
, __result
);
2857 if (__comp(*__last2
, *__last1
))
2859 *--__result
= *__last1
;
2860 if (__first1
== __last1
)
2861 return std::copy_backward(__first2
, ++__last2
, __result
);
2866 *--__result
= *__last2
;
2867 if (__first2
== __last2
)
2868 return std::copy_backward(__first1
, ++__last1
, __result
);
2874 /// This is a helper function for the merge routines.
2875 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2877 _BidirectionalIterator1
2878 __rotate_adaptive(_BidirectionalIterator1 __first
,
2879 _BidirectionalIterator1 __middle
,
2880 _BidirectionalIterator1 __last
,
2881 _Distance __len1
, _Distance __len2
,
2882 _BidirectionalIterator2 __buffer
,
2883 _Distance __buffer_size
)
2885 _BidirectionalIterator2 __buffer_end
;
2886 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2888 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2889 std::copy_backward(__first
, __middle
, __last
);
2890 return std::copy(__buffer
, __buffer_end
, __first
);
2892 else if (__len1
<= __buffer_size
)
2894 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2895 std::copy(__middle
, __last
, __first
);
2896 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2900 std::rotate(__first
, __middle
, __last
);
2901 std::advance(__first
, std::distance(__middle
, __last
));
2906 /// This is a helper function for the merge routines.
2907 template<typename _BidirectionalIterator
, typename _Distance
,
2910 __merge_adaptive(_BidirectionalIterator __first
,
2911 _BidirectionalIterator __middle
,
2912 _BidirectionalIterator __last
,
2913 _Distance __len1
, _Distance __len2
,
2914 _Pointer __buffer
, _Distance __buffer_size
)
2916 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2918 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2919 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2922 else if (__len2
<= __buffer_size
)
2924 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2925 std::__merge_backward(__first
, __middle
, __buffer
,
2926 __buffer_end
, __last
);
2930 _BidirectionalIterator __first_cut
= __first
;
2931 _BidirectionalIterator __second_cut
= __middle
;
2932 _Distance __len11
= 0;
2933 _Distance __len22
= 0;
2934 if (__len1
> __len2
)
2936 __len11
= __len1
/ 2;
2937 std::advance(__first_cut
, __len11
);
2938 __second_cut
= std::lower_bound(__middle
, __last
,
2940 __len22
= std::distance(__middle
, __second_cut
);
2944 __len22
= __len2
/ 2;
2945 std::advance(__second_cut
, __len22
);
2946 __first_cut
= std::upper_bound(__first
, __middle
,
2948 __len11
= std::distance(__first
, __first_cut
);
2950 _BidirectionalIterator __new_middle
=
2951 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2952 __len1
- __len11
, __len22
, __buffer
,
2954 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2955 __len22
, __buffer
, __buffer_size
);
2956 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2958 __len2
- __len22
, __buffer
, __buffer_size
);
2962 /// This is a helper function for the merge routines.
2963 template<typename _BidirectionalIterator
, typename _Distance
,
2964 typename _Pointer
, typename _Compare
>
2966 __merge_adaptive(_BidirectionalIterator __first
,
2967 _BidirectionalIterator __middle
,
2968 _BidirectionalIterator __last
,
2969 _Distance __len1
, _Distance __len2
,
2970 _Pointer __buffer
, _Distance __buffer_size
,
2973 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2975 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2976 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2979 else if (__len2
<= __buffer_size
)
2981 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2982 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2987 _BidirectionalIterator __first_cut
= __first
;
2988 _BidirectionalIterator __second_cut
= __middle
;
2989 _Distance __len11
= 0;
2990 _Distance __len22
= 0;
2991 if (__len1
> __len2
)
2993 __len11
= __len1
/ 2;
2994 std::advance(__first_cut
, __len11
);
2995 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2997 __len22
= std::distance(__middle
, __second_cut
);
3001 __len22
= __len2
/ 2;
3002 std::advance(__second_cut
, __len22
);
3003 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3005 __len11
= std::distance(__first
, __first_cut
);
3007 _BidirectionalIterator __new_middle
=
3008 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3009 __len1
- __len11
, __len22
, __buffer
,
3011 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3012 __len22
, __buffer
, __buffer_size
, __comp
);
3013 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3015 __len2
- __len22
, __buffer
,
3016 __buffer_size
, __comp
);
3020 /// This is a helper function for the merge routines.
3021 template<typename _BidirectionalIterator
, typename _Distance
>
3023 __merge_without_buffer(_BidirectionalIterator __first
,
3024 _BidirectionalIterator __middle
,
3025 _BidirectionalIterator __last
,
3026 _Distance __len1
, _Distance __len2
)
3028 if (__len1
== 0 || __len2
== 0)
3030 if (__len1
+ __len2
== 2)
3032 if (*__middle
< *__first
)
3033 std::iter_swap(__first
, __middle
);
3036 _BidirectionalIterator __first_cut
= __first
;
3037 _BidirectionalIterator __second_cut
= __middle
;
3038 _Distance __len11
= 0;
3039 _Distance __len22
= 0;
3040 if (__len1
> __len2
)
3042 __len11
= __len1
/ 2;
3043 std::advance(__first_cut
, __len11
);
3044 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3045 __len22
= std::distance(__middle
, __second_cut
);
3049 __len22
= __len2
/ 2;
3050 std::advance(__second_cut
, __len22
);
3051 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3052 __len11
= std::distance(__first
, __first_cut
);
3054 std::rotate(__first_cut
, __middle
, __second_cut
);
3055 _BidirectionalIterator __new_middle
= __first_cut
;
3056 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3057 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3059 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3060 __len1
- __len11
, __len2
- __len22
);
3063 /// This is a helper function for the merge routines.
3064 template<typename _BidirectionalIterator
, typename _Distance
,
3067 __merge_without_buffer(_BidirectionalIterator __first
,
3068 _BidirectionalIterator __middle
,
3069 _BidirectionalIterator __last
,
3070 _Distance __len1
, _Distance __len2
,
3073 if (__len1
== 0 || __len2
== 0)
3075 if (__len1
+ __len2
== 2)
3077 if (__comp(*__middle
, *__first
))
3078 std::iter_swap(__first
, __middle
);
3081 _BidirectionalIterator __first_cut
= __first
;
3082 _BidirectionalIterator __second_cut
= __middle
;
3083 _Distance __len11
= 0;
3084 _Distance __len22
= 0;
3085 if (__len1
> __len2
)
3087 __len11
= __len1
/ 2;
3088 std::advance(__first_cut
, __len11
);
3089 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3091 __len22
= std::distance(__middle
, __second_cut
);
3095 __len22
= __len2
/ 2;
3096 std::advance(__second_cut
, __len22
);
3097 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3099 __len11
= std::distance(__first
, __first_cut
);
3101 std::rotate(__first_cut
, __middle
, __second_cut
);
3102 _BidirectionalIterator __new_middle
= __first_cut
;
3103 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3104 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3105 __len11
, __len22
, __comp
);
3106 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3107 __len1
- __len11
, __len2
- __len22
, __comp
);
3111 * @brief Merges two sorted ranges in place.
3112 * @ingroup sorting_algorithms
3113 * @param first An iterator.
3114 * @param middle Another iterator.
3115 * @param last Another iterator.
3118 * Merges two sorted and consecutive ranges, [first,middle) and
3119 * [middle,last), and puts the result in [first,last). The output will
3120 * be sorted. The sort is @e stable, that is, for equivalent
3121 * elements in the two ranges, elements from the first range will always
3122 * come before elements from the second.
3124 * If enough additional memory is available, this takes (last-first)-1
3125 * comparisons. Otherwise an NlogN algorithm is used, where N is
3126 * distance(first,last).
3128 template<typename _BidirectionalIterator
>
3130 inplace_merge(_BidirectionalIterator __first
,
3131 _BidirectionalIterator __middle
,
3132 _BidirectionalIterator __last
)
3134 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3136 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3139 // concept requirements
3140 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3141 _BidirectionalIterator
>)
3142 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3143 __glibcxx_requires_sorted(__first
, __middle
);
3144 __glibcxx_requires_sorted(__middle
, __last
);
3146 if (__first
== __middle
|| __middle
== __last
)
3149 _DistanceType __len1
= std::distance(__first
, __middle
);
3150 _DistanceType __len2
= std::distance(__middle
, __last
);
3152 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3154 if (__buf
.begin() == 0)
3155 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3157 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3158 __buf
.begin(), _DistanceType(__buf
.size()));
3162 * @brief Merges two sorted ranges in place.
3163 * @ingroup sorting_algorithms
3164 * @param first An iterator.
3165 * @param middle Another iterator.
3166 * @param last Another iterator.
3167 * @param comp A functor to use for comparisons.
3170 * Merges two sorted and consecutive ranges, [first,middle) and
3171 * [middle,last), and puts the result in [first,last). The output will
3172 * be sorted. The sort is @e stable, that is, for equivalent
3173 * elements in the two ranges, elements from the first range will always
3174 * come before elements from the second.
3176 * If enough additional memory is available, this takes (last-first)-1
3177 * comparisons. Otherwise an NlogN algorithm is used, where N is
3178 * distance(first,last).
3180 * The comparison function should have the same effects on ordering as
3181 * the function used for the initial sort.
3183 template<typename _BidirectionalIterator
, typename _Compare
>
3185 inplace_merge(_BidirectionalIterator __first
,
3186 _BidirectionalIterator __middle
,
3187 _BidirectionalIterator __last
,
3190 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3192 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3195 // concept requirements
3196 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3197 _BidirectionalIterator
>)
3198 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3199 _ValueType
, _ValueType
>)
3200 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3201 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3203 if (__first
== __middle
|| __middle
== __last
)
3206 const _DistanceType __len1
= std::distance(__first
, __middle
);
3207 const _DistanceType __len2
= std::distance(__middle
, __last
);
3209 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3211 if (__buf
.begin() == 0)
3212 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3215 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3216 __buf
.begin(), _DistanceType(__buf
.size()),
3220 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3223 __merge_sort_loop(_RandomAccessIterator1 __first
,
3224 _RandomAccessIterator1 __last
,
3225 _RandomAccessIterator2 __result
,
3226 _Distance __step_size
)
3228 const _Distance __two_step
= 2 * __step_size
;
3230 while (__last
- __first
>= __two_step
)
3232 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3233 __first
+ __step_size
,
3234 __first
+ __two_step
,
3236 __first
+= __two_step
;
3239 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3240 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3241 __first
+ __step_size
, __last
,
3245 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3246 typename _Distance
, typename _Compare
>
3248 __merge_sort_loop(_RandomAccessIterator1 __first
,
3249 _RandomAccessIterator1 __last
,
3250 _RandomAccessIterator2 __result
, _Distance __step_size
,
3253 const _Distance __two_step
= 2 * __step_size
;
3255 while (__last
- __first
>= __two_step
)
3257 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3258 __first
+ __step_size
, __first
+ __two_step
,
3261 __first
+= __two_step
;
3263 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3265 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3266 __first
+ __step_size
, __last
, __result
, __comp
);
3269 template<typename _RandomAccessIterator
, typename _Distance
>
3271 __chunk_insertion_sort(_RandomAccessIterator __first
,
3272 _RandomAccessIterator __last
,
3273 _Distance __chunk_size
)
3275 while (__last
- __first
>= __chunk_size
)
3277 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3278 __first
+= __chunk_size
;
3280 std::__insertion_sort(__first
, __last
);
3283 template<typename _RandomAccessIterator
, typename _Distance
,
3286 __chunk_insertion_sort(_RandomAccessIterator __first
,
3287 _RandomAccessIterator __last
,
3288 _Distance __chunk_size
, _Compare __comp
)
3290 while (__last
- __first
>= __chunk_size
)
3292 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3293 __first
+= __chunk_size
;
3295 std::__insertion_sort(__first
, __last
, __comp
);
3298 enum { _S_chunk_size
= 7 };
3300 template<typename _RandomAccessIterator
, typename _Pointer
>
3302 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3303 _RandomAccessIterator __last
,
3306 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3309 const _Distance __len
= __last
- __first
;
3310 const _Pointer __buffer_last
= __buffer
+ __len
;
3312 _Distance __step_size
= _S_chunk_size
;
3313 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3315 while (__step_size
< __len
)
3317 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3319 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3324 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3326 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3327 _RandomAccessIterator __last
,
3328 _Pointer __buffer
, _Compare __comp
)
3330 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3333 const _Distance __len
= __last
- __first
;
3334 const _Pointer __buffer_last
= __buffer
+ __len
;
3336 _Distance __step_size
= _S_chunk_size
;
3337 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3339 while (__step_size
< __len
)
3341 std::__merge_sort_loop(__first
, __last
, __buffer
,
3342 __step_size
, __comp
);
3344 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3345 __step_size
, __comp
);
3350 template<typename _RandomAccessIterator
, typename _Pointer
,
3353 __stable_sort_adaptive(_RandomAccessIterator __first
,
3354 _RandomAccessIterator __last
,
3355 _Pointer __buffer
, _Distance __buffer_size
)
3357 const _Distance __len
= (__last
- __first
+ 1) / 2;
3358 const _RandomAccessIterator __middle
= __first
+ __len
;
3359 if (__len
> __buffer_size
)
3361 std::__stable_sort_adaptive(__first
, __middle
,
3362 __buffer
, __buffer_size
);
3363 std::__stable_sort_adaptive(__middle
, __last
,
3364 __buffer
, __buffer_size
);
3368 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3369 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3371 std::__merge_adaptive(__first
, __middle
, __last
,
3372 _Distance(__middle
- __first
),
3373 _Distance(__last
- __middle
),
3374 __buffer
, __buffer_size
);
3377 template<typename _RandomAccessIterator
, typename _Pointer
,
3378 typename _Distance
, typename _Compare
>
3380 __stable_sort_adaptive(_RandomAccessIterator __first
,
3381 _RandomAccessIterator __last
,
3382 _Pointer __buffer
, _Distance __buffer_size
,
3385 const _Distance __len
= (__last
- __first
+ 1) / 2;
3386 const _RandomAccessIterator __middle
= __first
+ __len
;
3387 if (__len
> __buffer_size
)
3389 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3390 __buffer_size
, __comp
);
3391 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3392 __buffer_size
, __comp
);
3396 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3397 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3399 std::__merge_adaptive(__first
, __middle
, __last
,
3400 _Distance(__middle
- __first
),
3401 _Distance(__last
- __middle
),
3402 __buffer
, __buffer_size
,
3406 /// This is a helper function for the stable sorting routines.
3407 template<typename _RandomAccessIterator
>
3409 __inplace_stable_sort(_RandomAccessIterator __first
,
3410 _RandomAccessIterator __last
)
3412 if (__last
- __first
< 15)
3414 std::__insertion_sort(__first
, __last
);
3417 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3418 std::__inplace_stable_sort(__first
, __middle
);
3419 std::__inplace_stable_sort(__middle
, __last
);
3420 std::__merge_without_buffer(__first
, __middle
, __last
,
3425 /// This is a helper function for the stable sorting routines.
3426 template<typename _RandomAccessIterator
, typename _Compare
>
3428 __inplace_stable_sort(_RandomAccessIterator __first
,
3429 _RandomAccessIterator __last
, _Compare __comp
)
3431 if (__last
- __first
< 15)
3433 std::__insertion_sort(__first
, __last
, __comp
);
3436 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3437 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3438 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3439 std::__merge_without_buffer(__first
, __middle
, __last
,
3447 // Set algorithms: includes, set_union, set_intersection, set_difference,
3448 // set_symmetric_difference. All of these algorithms have the precondition
3449 // that their input ranges are sorted and the postcondition that their output
3450 // ranges are sorted.
3453 * @brief Determines whether all elements of a sequence exists in a range.
3454 * @param first1 Start of search range.
3455 * @param last1 End of search range.
3456 * @param first2 Start of sequence
3457 * @param last2 End of sequence.
3458 * @return True if each element in [first2,last2) is contained in order
3459 * within [first1,last1). False otherwise.
3460 * @ingroup set_algorithms
3462 * This operation expects both [first1,last1) and [first2,last2) to be
3463 * sorted. Searches for the presence of each element in [first2,last2)
3464 * within [first1,last1). The iterators over each range only move forward,
3465 * so this is a linear algorithm. If an element in [first2,last2) is not
3466 * found before the search iterator reaches @a last2, false is returned.
3468 template<typename _InputIterator1
, typename _InputIterator2
>
3470 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3471 _InputIterator2 __first2
, _InputIterator2 __last2
)
3473 typedef typename iterator_traits
<_InputIterator1
>::value_type
3475 typedef typename iterator_traits
<_InputIterator2
>::value_type
3478 // concept requirements
3479 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3480 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3481 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3482 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3483 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3484 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3486 while (__first1
!= __last1
&& __first2
!= __last2
)
3487 if (*__first2
< *__first1
)
3489 else if(*__first1
< *__first2
)
3492 ++__first1
, ++__first2
;
3494 return __first2
== __last2
;
3498 * @brief Determines whether all elements of a sequence exists in a range
3500 * @ingroup set_algorithms
3501 * @param first1 Start of search range.
3502 * @param last1 End of search range.
3503 * @param first2 Start of sequence
3504 * @param last2 End of sequence.
3505 * @param comp Comparison function to use.
3506 * @return True if each element in [first2,last2) is contained in order
3507 * within [first1,last1) according to comp. False otherwise.
3508 * @ingroup set_algorithms
3510 * This operation expects both [first1,last1) and [first2,last2) to be
3511 * sorted. Searches for the presence of each element in [first2,last2)
3512 * within [first1,last1), using comp to decide. The iterators over each
3513 * range only move forward, so this is a linear algorithm. If an element
3514 * in [first2,last2) is not found before the search iterator reaches @a
3515 * last2, false is returned.
3517 template<typename _InputIterator1
, typename _InputIterator2
,
3520 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3521 _InputIterator2 __first2
, _InputIterator2 __last2
,
3524 typedef typename iterator_traits
<_InputIterator1
>::value_type
3526 typedef typename iterator_traits
<_InputIterator2
>::value_type
3529 // concept requirements
3530 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3531 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3532 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3533 _ValueType1
, _ValueType2
>)
3534 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3535 _ValueType2
, _ValueType1
>)
3536 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3537 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3539 while (__first1
!= __last1
&& __first2
!= __last2
)
3540 if (__comp(*__first2
, *__first1
))
3542 else if(__comp(*__first1
, *__first2
))
3545 ++__first1
, ++__first2
;
3547 return __first2
== __last2
;
3556 // set_symmetric_difference
3561 * @brief Permute range into the next "dictionary" ordering.
3562 * @ingroup sorting_algorithms
3563 * @param first Start of range.
3564 * @param last End of range.
3565 * @return False if wrapped to first permutation, true otherwise.
3567 * Treats all permutations of the range as a set of "dictionary" sorted
3568 * sequences. Permutes the current sequence into the next one of this set.
3569 * Returns true if there are more sequences to generate. If the sequence
3570 * is the largest of the set, the smallest is generated and false returned.
3572 template<typename _BidirectionalIterator
>
3574 next_permutation(_BidirectionalIterator __first
,
3575 _BidirectionalIterator __last
)
3577 // concept requirements
3578 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3579 _BidirectionalIterator
>)
3580 __glibcxx_function_requires(_LessThanComparableConcept
<
3581 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3582 __glibcxx_requires_valid_range(__first
, __last
);
3584 if (__first
== __last
)
3586 _BidirectionalIterator __i
= __first
;
3595 _BidirectionalIterator __ii
= __i
;
3599 _BidirectionalIterator __j
= __last
;
3600 while (!(*__i
< *--__j
))
3602 std::iter_swap(__i
, __j
);
3603 std::reverse(__ii
, __last
);
3608 std::reverse(__first
, __last
);
3615 * @brief Permute range into the next "dictionary" ordering using
3616 * comparison functor.
3617 * @ingroup sorting_algorithms
3618 * @param first Start of range.
3619 * @param last End of range.
3620 * @param comp A comparison functor.
3621 * @return False if wrapped to first permutation, true otherwise.
3623 * Treats all permutations of the range [first,last) as a set of
3624 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3625 * sequence into the next one of this set. Returns true if there are more
3626 * sequences to generate. If the sequence is the largest of the set, the
3627 * smallest is generated and false returned.
3629 template<typename _BidirectionalIterator
, typename _Compare
>
3631 next_permutation(_BidirectionalIterator __first
,
3632 _BidirectionalIterator __last
, _Compare __comp
)
3634 // concept requirements
3635 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3636 _BidirectionalIterator
>)
3637 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3638 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3639 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3640 __glibcxx_requires_valid_range(__first
, __last
);
3642 if (__first
== __last
)
3644 _BidirectionalIterator __i
= __first
;
3653 _BidirectionalIterator __ii
= __i
;
3655 if (__comp(*__i
, *__ii
))
3657 _BidirectionalIterator __j
= __last
;
3658 while (!bool(__comp(*__i
, *--__j
)))
3660 std::iter_swap(__i
, __j
);
3661 std::reverse(__ii
, __last
);
3666 std::reverse(__first
, __last
);
3673 * @brief Permute range into the previous "dictionary" ordering.
3674 * @ingroup sorting_algorithms
3675 * @param first Start of range.
3676 * @param last End of range.
3677 * @return False if wrapped to last permutation, true otherwise.
3679 * Treats all permutations of the range as a set of "dictionary" sorted
3680 * sequences. Permutes the current sequence into the previous one of this
3681 * set. Returns true if there are more sequences to generate. If the
3682 * sequence is the smallest of the set, the largest is generated and false
3685 template<typename _BidirectionalIterator
>
3687 prev_permutation(_BidirectionalIterator __first
,
3688 _BidirectionalIterator __last
)
3690 // concept requirements
3691 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3692 _BidirectionalIterator
>)
3693 __glibcxx_function_requires(_LessThanComparableConcept
<
3694 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3695 __glibcxx_requires_valid_range(__first
, __last
);
3697 if (__first
== __last
)
3699 _BidirectionalIterator __i
= __first
;
3708 _BidirectionalIterator __ii
= __i
;
3712 _BidirectionalIterator __j
= __last
;
3713 while (!(*--__j
< *__i
))
3715 std::iter_swap(__i
, __j
);
3716 std::reverse(__ii
, __last
);
3721 std::reverse(__first
, __last
);
3728 * @brief Permute range into the previous "dictionary" ordering using
3729 * comparison functor.
3730 * @ingroup sorting_algorithms
3731 * @param first Start of range.
3732 * @param last End of range.
3733 * @param comp A comparison functor.
3734 * @return False if wrapped to last permutation, true otherwise.
3736 * Treats all permutations of the range [first,last) as a set of
3737 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3738 * sequence into the previous one of this set. Returns true if there are
3739 * more sequences to generate. If the sequence is the smallest of the set,
3740 * the largest is generated and false returned.
3742 template<typename _BidirectionalIterator
, typename _Compare
>
3744 prev_permutation(_BidirectionalIterator __first
,
3745 _BidirectionalIterator __last
, _Compare __comp
)
3747 // concept requirements
3748 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3749 _BidirectionalIterator
>)
3750 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3751 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3752 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3753 __glibcxx_requires_valid_range(__first
, __last
);
3755 if (__first
== __last
)
3757 _BidirectionalIterator __i
= __first
;
3766 _BidirectionalIterator __ii
= __i
;
3768 if (__comp(*__ii
, *__i
))
3770 _BidirectionalIterator __j
= __last
;
3771 while (!bool(__comp(*--__j
, *__i
)))
3773 std::iter_swap(__i
, __j
);
3774 std::reverse(__ii
, __last
);
3779 std::reverse(__first
, __last
);
3789 * @brief Copy a sequence, replacing each element of one value with another
3791 * @param first An input iterator.
3792 * @param last An input iterator.
3793 * @param result An output iterator.
3794 * @param old_value The value to be replaced.
3795 * @param new_value The replacement value.
3796 * @return The end of the output sequence, @p result+(last-first).
3798 * Copies each element in the input range @p [first,last) to the
3799 * output range @p [result,result+(last-first)) replacing elements
3800 * equal to @p old_value with @p new_value.
3802 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3804 replace_copy(_InputIterator __first
, _InputIterator __last
,
3805 _OutputIterator __result
,
3806 const _Tp
& __old_value
, const _Tp
& __new_value
)
3808 // concept requirements
3809 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3810 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3811 typename iterator_traits
<_InputIterator
>::value_type
>)
3812 __glibcxx_function_requires(_EqualOpConcept
<
3813 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3814 __glibcxx_requires_valid_range(__first
, __last
);
3816 for (; __first
!= __last
; ++__first
, ++__result
)
3817 if (*__first
== __old_value
)
3818 *__result
= __new_value
;
3820 *__result
= *__first
;
3825 * @brief Copy a sequence, replacing each value for which a predicate
3826 * returns true with another value.
3827 * @ingroup mutating_algorithms
3828 * @param first An input iterator.
3829 * @param last An input iterator.
3830 * @param result An output iterator.
3831 * @param pred A predicate.
3832 * @param new_value The replacement value.
3833 * @return The end of the output sequence, @p result+(last-first).
3835 * Copies each element in the range @p [first,last) to the range
3836 * @p [result,result+(last-first)) replacing elements for which
3837 * @p pred returns true with @p new_value.
3839 template<typename _InputIterator
, typename _OutputIterator
,
3840 typename _Predicate
, typename _Tp
>
3842 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3843 _OutputIterator __result
,
3844 _Predicate __pred
, const _Tp
& __new_value
)
3846 // concept requirements
3847 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3848 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3849 typename iterator_traits
<_InputIterator
>::value_type
>)
3850 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3851 typename iterator_traits
<_InputIterator
>::value_type
>)
3852 __glibcxx_requires_valid_range(__first
, __last
);
3854 for (; __first
!= __last
; ++__first
, ++__result
)
3855 if (__pred(*__first
))
3856 *__result
= __new_value
;
3858 *__result
= *__first
;
3862 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3864 * @brief Determines whether the elements of a sequence are sorted.
3865 * @ingroup sorting_algorithms
3866 * @param first An iterator.
3867 * @param last Another iterator.
3868 * @return True if the elements are sorted, false otherwise.
3870 template<typename _ForwardIterator
>
3872 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3873 { return std::is_sorted_until(__first
, __last
) == __last
; }
3876 * @brief Determines whether the elements of a sequence are sorted
3877 * according to a comparison functor.
3878 * @ingroup sorting_algorithms
3879 * @param first An iterator.
3880 * @param last Another iterator.
3881 * @param comp A comparison functor.
3882 * @return True if the elements are sorted, false otherwise.
3884 template<typename _ForwardIterator
, typename _Compare
>
3886 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3888 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3891 * @brief Determines the end of a sorted sequence.
3892 * @ingroup sorting_algorithms
3893 * @param first An iterator.
3894 * @param last Another iterator.
3895 * @return An iterator pointing to the last iterator i in [first, last)
3896 * for which the range [first, i) is sorted.
3898 template<typename _ForwardIterator
>
3900 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3902 // concept requirements
3903 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3904 __glibcxx_function_requires(_LessThanComparableConcept
<
3905 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3906 __glibcxx_requires_valid_range(__first
, __last
);
3908 if (__first
== __last
)
3911 _ForwardIterator __next
= __first
;
3912 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3913 if (*__next
< *__first
)
3919 * @brief Determines the end of a sorted sequence using comparison functor.
3920 * @ingroup sorting_algorithms
3921 * @param first An iterator.
3922 * @param last Another iterator.
3923 * @param comp A comparison functor.
3924 * @return An iterator pointing to the last iterator i in [first, last)
3925 * for which the range [first, i) is sorted.
3927 template<typename _ForwardIterator
, typename _Compare
>
3929 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3932 // concept requirements
3933 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3934 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3935 typename iterator_traits
<_ForwardIterator
>::value_type
,
3936 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3937 __glibcxx_requires_valid_range(__first
, __last
);
3939 if (__first
== __last
)
3942 _ForwardIterator __next
= __first
;
3943 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3944 if (__comp(*__next
, *__first
))
3950 * @brief Determines min and max at once as an ordered pair.
3951 * @ingroup sorting_algorithms
3952 * @param a A thing of arbitrary type.
3953 * @param b Another thing of arbitrary type.
3954 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3956 template<typename _Tp
>
3957 inline pair
<const _Tp
&, const _Tp
&>
3958 minmax(const _Tp
& __a
, const _Tp
& __b
)
3960 // concept requirements
3961 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3963 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3964 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3968 * @brief Determines min and max at once as an ordered pair.
3969 * @ingroup sorting_algorithms
3970 * @param a A thing of arbitrary type.
3971 * @param b Another thing of arbitrary type.
3972 * @param comp A @link comparison_functor comparison functor@endlink.
3973 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3975 template<typename _Tp
, typename _Compare
>
3976 inline pair
<const _Tp
&, const _Tp
&>
3977 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3979 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3980 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3984 * @brief Return a pair of iterators pointing to the minimum and maximum
3985 * elements in a range.
3986 * @ingroup sorting_algorithms
3987 * @param first Start of range.
3988 * @param last End of range.
3989 * @return make_pair(m, M), where m is the first iterator i in
3990 * [first, last) such that no other element in the range is
3991 * smaller, and where M is the last iterator i in [first, last)
3992 * such that no other element in the range is larger.
3994 template<typename _ForwardIterator
>
3995 pair
<_ForwardIterator
, _ForwardIterator
>
3996 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3998 // concept requirements
3999 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4000 __glibcxx_function_requires(_LessThanComparableConcept
<
4001 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4002 __glibcxx_requires_valid_range(__first
, __last
);
4004 _ForwardIterator __next
= __first
;
4005 if (__first
== __last
4006 || ++__next
== __last
)
4007 return std::make_pair(__first
, __first
);
4009 _ForwardIterator __min
, __max
;
4010 if (*__next
< *__first
)
4024 while (__first
!= __last
)
4027 if (++__next
== __last
)
4029 if (*__first
< *__min
)
4031 else if (!(*__first
< *__max
))
4036 if (*__next
< *__first
)
4038 if (*__next
< *__min
)
4040 if (!(*__first
< *__max
))
4045 if (*__first
< *__min
)
4047 if (!(*__next
< *__max
))
4055 return std::make_pair(__min
, __max
);
4059 * @brief Return a pair of iterators pointing to the minimum and maximum
4060 * elements in a range.
4061 * @ingroup sorting_algorithms
4062 * @param first Start of range.
4063 * @param last End of range.
4064 * @param comp Comparison functor.
4065 * @return make_pair(m, M), where m is the first iterator i in
4066 * [first, last) such that no other element in the range is
4067 * smaller, and where M is the last iterator i in [first, last)
4068 * such that no other element in the range is larger.
4070 template<typename _ForwardIterator
, typename _Compare
>
4071 pair
<_ForwardIterator
, _ForwardIterator
>
4072 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4075 // concept requirements
4076 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4077 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4078 typename iterator_traits
<_ForwardIterator
>::value_type
,
4079 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4080 __glibcxx_requires_valid_range(__first
, __last
);
4082 _ForwardIterator __next
= __first
;
4083 if (__first
== __last
4084 || ++__next
== __last
)
4085 return std::make_pair(__first
, __first
);
4087 _ForwardIterator __min
, __max
;
4088 if (__comp(*__next
, *__first
))
4102 while (__first
!= __last
)
4105 if (++__next
== __last
)
4107 if (__comp(*__first
, *__min
))
4109 else if (!__comp(*__first
, *__max
))
4114 if (__comp(*__next
, *__first
))
4116 if (__comp(*__next
, *__min
))
4118 if (!__comp(*__first
, *__max
))
4123 if (__comp(*__first
, *__min
))
4125 if (!__comp(*__next
, *__max
))
4133 return std::make_pair(__min
, __max
);
4137 template<typename _Tp
>
4139 min(initializer_list
<_Tp
> __l
)
4140 { return *std::min_element(__l
.begin(), __l
.end()); }
4142 template<typename _Tp
, typename _Compare
>
4144 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4145 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4147 template<typename _Tp
>
4149 max(initializer_list
<_Tp
> __l
)
4150 { return *std::max_element(__l
.begin(), __l
.end()); }
4152 template<typename _Tp
, typename _Compare
>
4154 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4155 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4157 template<typename _Tp
>
4158 inline pair
<_Tp
, _Tp
>
4159 minmax(initializer_list
<_Tp
> __l
)
4161 pair
<const _Tp
*, const _Tp
*> __p
=
4162 std::minmax_element(__l
.begin(), __l
.end());
4163 return std::make_pair(*__p
.first
, *__p
.second
);
4166 template<typename _Tp
, typename _Compare
>
4167 inline pair
<_Tp
, _Tp
>
4168 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4170 pair
<const _Tp
*, const _Tp
*> __p
=
4171 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4172 return std::make_pair(*__p
.first
, *__p
.second
);
4174 #endif // __GXX_EXPERIMENTAL_CXX0X__
4176 _GLIBCXX_END_NAMESPACE
4178 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4181 * @brief Apply a function to every element of a sequence.
4182 * @ingroup non_mutating_algorithms
4183 * @param first An input iterator.
4184 * @param last An input iterator.
4185 * @param f A unary function object.
4188 * Applies the function object @p f to each element in the range
4189 * @p [first,last). @p f must not modify the order of the sequence.
4190 * If @p f has a return value it is ignored.
4192 template<typename _InputIterator
, typename _Function
>
4194 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4196 // concept requirements
4197 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4198 __glibcxx_requires_valid_range(__first
, __last
);
4199 for (; __first
!= __last
; ++__first
)
4205 * @brief Find the first occurrence of a value in a sequence.
4206 * @ingroup non_mutating_algorithms
4207 * @param first An input iterator.
4208 * @param last An input iterator.
4209 * @param val The value to find.
4210 * @return The first iterator @c i in the range @p [first,last)
4211 * such that @c *i == @p val, or @p last if no such iterator exists.
4213 template<typename _InputIterator
, typename _Tp
>
4214 inline _InputIterator
4215 find(_InputIterator __first
, _InputIterator __last
,
4218 // concept requirements
4219 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4220 __glibcxx_function_requires(_EqualOpConcept
<
4221 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4222 __glibcxx_requires_valid_range(__first
, __last
);
4223 return std::__find(__first
, __last
, __val
,
4224 std::__iterator_category(__first
));
4228 * @brief Find the first element in a sequence for which a
4229 * predicate is true.
4230 * @ingroup non_mutating_algorithms
4231 * @param first An input iterator.
4232 * @param last An input iterator.
4233 * @param pred A predicate.
4234 * @return The first iterator @c i in the range @p [first,last)
4235 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4237 template<typename _InputIterator
, typename _Predicate
>
4238 inline _InputIterator
4239 find_if(_InputIterator __first
, _InputIterator __last
,
4242 // concept requirements
4243 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4244 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4245 typename iterator_traits
<_InputIterator
>::value_type
>)
4246 __glibcxx_requires_valid_range(__first
, __last
);
4247 return std::__find_if(__first
, __last
, __pred
,
4248 std::__iterator_category(__first
));
4252 * @brief Find element from a set in a sequence.
4253 * @ingroup non_mutating_algorithms
4254 * @param first1 Start of range to search.
4255 * @param last1 End of range to search.
4256 * @param first2 Start of match candidates.
4257 * @param last2 End of match candidates.
4258 * @return The first iterator @c i in the range
4259 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4260 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4262 * Searches the range @p [first1,last1) for an element that is equal to
4263 * some element in the range [first2,last2). If found, returns an iterator
4264 * in the range [first1,last1), otherwise returns @p last1.
4266 template<typename _InputIterator
, typename _ForwardIterator
>
4268 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4269 _ForwardIterator __first2
, _ForwardIterator __last2
)
4271 // concept requirements
4272 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4273 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4274 __glibcxx_function_requires(_EqualOpConcept
<
4275 typename iterator_traits
<_InputIterator
>::value_type
,
4276 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4277 __glibcxx_requires_valid_range(__first1
, __last1
);
4278 __glibcxx_requires_valid_range(__first2
, __last2
);
4280 for (; __first1
!= __last1
; ++__first1
)
4281 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4282 if (*__first1
== *__iter
)
4288 * @brief Find element from a set in a sequence using a predicate.
4289 * @ingroup non_mutating_algorithms
4290 * @param first1 Start of range to search.
4291 * @param last1 End of range to search.
4292 * @param first2 Start of match candidates.
4293 * @param last2 End of match candidates.
4294 * @param comp Predicate to use.
4295 * @return The first iterator @c i in the range
4296 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4297 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4300 * Searches the range @p [first1,last1) for an element that is
4301 * equal to some element in the range [first2,last2). If found,
4302 * returns an iterator in the range [first1,last1), otherwise
4305 template<typename _InputIterator
, typename _ForwardIterator
,
4306 typename _BinaryPredicate
>
4308 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4309 _ForwardIterator __first2
, _ForwardIterator __last2
,
4310 _BinaryPredicate __comp
)
4312 // concept requirements
4313 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4314 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4315 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4316 typename iterator_traits
<_InputIterator
>::value_type
,
4317 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4318 __glibcxx_requires_valid_range(__first1
, __last1
);
4319 __glibcxx_requires_valid_range(__first2
, __last2
);
4321 for (; __first1
!= __last1
; ++__first1
)
4322 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4323 if (__comp(*__first1
, *__iter
))
4329 * @brief Find two adjacent values in a sequence that are equal.
4330 * @ingroup non_mutating_algorithms
4331 * @param first A forward iterator.
4332 * @param last A forward iterator.
4333 * @return The first iterator @c i such that @c i and @c i+1 are both
4334 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4335 * or @p last if no such iterator exists.
4337 template<typename _ForwardIterator
>
4339 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4341 // concept requirements
4342 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4343 __glibcxx_function_requires(_EqualityComparableConcept
<
4344 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4345 __glibcxx_requires_valid_range(__first
, __last
);
4346 if (__first
== __last
)
4348 _ForwardIterator __next
= __first
;
4349 while(++__next
!= __last
)
4351 if (*__first
== *__next
)
4359 * @brief Find two adjacent values in a sequence using a predicate.
4360 * @ingroup non_mutating_algorithms
4361 * @param first A forward iterator.
4362 * @param last A forward iterator.
4363 * @param binary_pred A binary predicate.
4364 * @return The first iterator @c i such that @c i and @c i+1 are both
4365 * valid iterators in @p [first,last) and such that
4366 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4369 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4371 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4372 _BinaryPredicate __binary_pred
)
4374 // concept requirements
4375 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4376 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4377 typename iterator_traits
<_ForwardIterator
>::value_type
,
4378 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4379 __glibcxx_requires_valid_range(__first
, __last
);
4380 if (__first
== __last
)
4382 _ForwardIterator __next
= __first
;
4383 while(++__next
!= __last
)
4385 if (__binary_pred(*__first
, *__next
))
4393 * @brief Count the number of copies of a value in a sequence.
4394 * @ingroup non_mutating_algorithms
4395 * @param first An input iterator.
4396 * @param last An input iterator.
4397 * @param value The value to be counted.
4398 * @return The number of iterators @c i in the range @p [first,last)
4399 * for which @c *i == @p value
4401 template<typename _InputIterator
, typename _Tp
>
4402 typename iterator_traits
<_InputIterator
>::difference_type
4403 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4405 // concept requirements
4406 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4407 __glibcxx_function_requires(_EqualOpConcept
<
4408 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4409 __glibcxx_requires_valid_range(__first
, __last
);
4410 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4411 for (; __first
!= __last
; ++__first
)
4412 if (*__first
== __value
)
4418 * @brief Count the elements of a sequence for which a predicate is true.
4419 * @ingroup non_mutating_algorithms
4420 * @param first An input iterator.
4421 * @param last An input iterator.
4422 * @param pred A predicate.
4423 * @return The number of iterators @c i in the range @p [first,last)
4424 * for which @p pred(*i) is true.
4426 template<typename _InputIterator
, typename _Predicate
>
4427 typename iterator_traits
<_InputIterator
>::difference_type
4428 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4430 // concept requirements
4431 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4432 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4433 typename iterator_traits
<_InputIterator
>::value_type
>)
4434 __glibcxx_requires_valid_range(__first
, __last
);
4435 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4436 for (; __first
!= __last
; ++__first
)
4437 if (__pred(*__first
))
4443 * @brief Search a sequence for a matching sub-sequence.
4444 * @ingroup non_mutating_algorithms
4445 * @param first1 A forward iterator.
4446 * @param last1 A forward iterator.
4447 * @param first2 A forward iterator.
4448 * @param last2 A forward iterator.
4449 * @return The first iterator @c i in the range
4450 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4451 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4452 * such iterator exists.
4454 * Searches the range @p [first1,last1) for a sub-sequence that compares
4455 * equal value-by-value with the sequence given by @p [first2,last2) and
4456 * returns an iterator to the first element of the sub-sequence, or
4457 * @p last1 if the sub-sequence is not found.
4459 * Because the sub-sequence must lie completely within the range
4460 * @p [first1,last1) it must start at a position less than
4461 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4463 * This means that the returned iterator @c i will be in the range
4464 * @p [first1,last1-(last2-first2))
4466 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4468 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4469 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4471 // concept requirements
4472 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4473 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4474 __glibcxx_function_requires(_EqualOpConcept
<
4475 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4476 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4477 __glibcxx_requires_valid_range(__first1
, __last1
);
4478 __glibcxx_requires_valid_range(__first2
, __last2
);
4480 // Test for empty ranges
4481 if (__first1
== __last1
|| __first2
== __last2
)
4484 // Test for a pattern of length 1.
4485 _ForwardIterator2
__p1(__first2
);
4486 if (++__p1
== __last2
)
4487 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4490 _ForwardIterator2 __p
;
4491 _ForwardIterator1 __current
= __first1
;
4495 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4496 if (__first1
== __last1
)
4500 __current
= __first1
;
4501 if (++__current
== __last1
)
4504 while (*__current
== *__p
)
4506 if (++__p
== __last2
)
4508 if (++__current
== __last1
)
4517 * @brief Search a sequence for a matching sub-sequence using a predicate.
4518 * @ingroup non_mutating_algorithms
4519 * @param first1 A forward iterator.
4520 * @param last1 A forward iterator.
4521 * @param first2 A forward iterator.
4522 * @param last2 A forward iterator.
4523 * @param predicate A binary predicate.
4524 * @return The first iterator @c i in the range
4525 * @p [first1,last1-(last2-first2)) such that
4526 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4527 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4529 * Searches the range @p [first1,last1) for a sub-sequence that compares
4530 * equal value-by-value with the sequence given by @p [first2,last2),
4531 * using @p predicate to determine equality, and returns an iterator
4532 * to the first element of the sub-sequence, or @p last1 if no such
4535 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4537 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4538 typename _BinaryPredicate
>
4540 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4541 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4542 _BinaryPredicate __predicate
)
4544 // concept requirements
4545 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4546 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4547 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4548 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4549 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4550 __glibcxx_requires_valid_range(__first1
, __last1
);
4551 __glibcxx_requires_valid_range(__first2
, __last2
);
4553 // Test for empty ranges
4554 if (__first1
== __last1
|| __first2
== __last2
)
4557 // Test for a pattern of length 1.
4558 _ForwardIterator2
__p1(__first2
);
4559 if (++__p1
== __last2
)
4561 while (__first1
!= __last1
4562 && !bool(__predicate(*__first1
, *__first2
)))
4568 _ForwardIterator2 __p
;
4569 _ForwardIterator1 __current
= __first1
;
4573 while (__first1
!= __last1
4574 && !bool(__predicate(*__first1
, *__first2
)))
4576 if (__first1
== __last1
)
4580 __current
= __first1
;
4581 if (++__current
== __last1
)
4584 while (__predicate(*__current
, *__p
))
4586 if (++__p
== __last2
)
4588 if (++__current
== __last1
)
4598 * @brief Search a sequence for a number of consecutive values.
4599 * @ingroup non_mutating_algorithms
4600 * @param first A forward iterator.
4601 * @param last A forward iterator.
4602 * @param count The number of consecutive values.
4603 * @param val The value to find.
4604 * @return The first iterator @c i in the range @p [first,last-count)
4605 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4606 * or @p last if no such iterator exists.
4608 * Searches the range @p [first,last) for @p count consecutive elements
4611 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4613 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4614 _Integer __count
, const _Tp
& __val
)
4616 // concept requirements
4617 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4618 __glibcxx_function_requires(_EqualOpConcept
<
4619 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4620 __glibcxx_requires_valid_range(__first
, __last
);
4625 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4626 return std::__search_n(__first
, __last
, __count
, __val
,
4627 std::__iterator_category(__first
));
4632 * @brief Search a sequence for a number of consecutive values using a
4634 * @ingroup non_mutating_algorithms
4635 * @param first A forward iterator.
4636 * @param last A forward iterator.
4637 * @param count The number of consecutive values.
4638 * @param val The value to find.
4639 * @param binary_pred A binary predicate.
4640 * @return The first iterator @c i in the range @p [first,last-count)
4641 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4642 * range @p [0,count), or @p last if no such iterator exists.
4644 * Searches the range @p [first,last) for @p count consecutive elements
4645 * for which the predicate returns true.
4647 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4648 typename _BinaryPredicate
>
4650 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4651 _Integer __count
, const _Tp
& __val
,
4652 _BinaryPredicate __binary_pred
)
4654 // concept requirements
4655 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4656 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4657 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4658 __glibcxx_requires_valid_range(__first
, __last
);
4664 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4668 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4669 std::__iterator_category(__first
));
4674 * @brief Perform an operation on a sequence.
4675 * @ingroup mutating_algorithms
4676 * @param first An input iterator.
4677 * @param last An input iterator.
4678 * @param result An output iterator.
4679 * @param unary_op A unary operator.
4680 * @return An output iterator equal to @p result+(last-first).
4682 * Applies the operator to each element in the input range and assigns
4683 * the results to successive elements of the output sequence.
4684 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4685 * range @p [0,last-first).
4687 * @p unary_op must not alter its argument.
4689 template<typename _InputIterator
, typename _OutputIterator
,
4690 typename _UnaryOperation
>
4692 transform(_InputIterator __first
, _InputIterator __last
,
4693 _OutputIterator __result
, _UnaryOperation __unary_op
)
4695 // concept requirements
4696 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4697 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4698 // "the type returned by a _UnaryOperation"
4699 __typeof__(__unary_op(*__first
))>)
4700 __glibcxx_requires_valid_range(__first
, __last
);
4702 for (; __first
!= __last
; ++__first
, ++__result
)
4703 *__result
= __unary_op(*__first
);
4708 * @brief Perform an operation on corresponding elements of two sequences.
4709 * @ingroup mutating_algorithms
4710 * @param first1 An input iterator.
4711 * @param last1 An input iterator.
4712 * @param first2 An input iterator.
4713 * @param result An output iterator.
4714 * @param binary_op A binary operator.
4715 * @return An output iterator equal to @p result+(last-first).
4717 * Applies the operator to the corresponding elements in the two
4718 * input ranges and assigns the results to successive elements of the
4720 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4721 * @c N in the range @p [0,last1-first1).
4723 * @p binary_op must not alter either of its arguments.
4725 template<typename _InputIterator1
, typename _InputIterator2
,
4726 typename _OutputIterator
, typename _BinaryOperation
>
4728 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4729 _InputIterator2 __first2
, _OutputIterator __result
,
4730 _BinaryOperation __binary_op
)
4732 // concept requirements
4733 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4734 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4735 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4736 // "the type returned by a _BinaryOperation"
4737 __typeof__(__binary_op(*__first1
,*__first2
))>)
4738 __glibcxx_requires_valid_range(__first1
, __last1
);
4740 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4741 *__result
= __binary_op(*__first1
, *__first2
);
4746 * @brief Replace each occurrence of one value in a sequence with another
4748 * @ingroup mutating_algorithms
4749 * @param first A forward iterator.
4750 * @param last A forward iterator.
4751 * @param old_value The value to be replaced.
4752 * @param new_value The replacement value.
4753 * @return replace() returns no value.
4755 * For each iterator @c i in the range @p [first,last) if @c *i ==
4756 * @p old_value then the assignment @c *i = @p new_value is performed.
4758 template<typename _ForwardIterator
, typename _Tp
>
4760 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4761 const _Tp
& __old_value
, const _Tp
& __new_value
)
4763 // concept requirements
4764 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4766 __glibcxx_function_requires(_EqualOpConcept
<
4767 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4768 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4769 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4770 __glibcxx_requires_valid_range(__first
, __last
);
4772 for (; __first
!= __last
; ++__first
)
4773 if (*__first
== __old_value
)
4774 *__first
= __new_value
;
4778 * @brief Replace each value in a sequence for which a predicate returns
4779 * true with another value.
4780 * @ingroup mutating_algorithms
4781 * @param first A forward iterator.
4782 * @param last A forward iterator.
4783 * @param pred A predicate.
4784 * @param new_value The replacement value.
4785 * @return replace_if() returns no value.
4787 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4788 * is true then the assignment @c *i = @p new_value is performed.
4790 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4792 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4793 _Predicate __pred
, const _Tp
& __new_value
)
4795 // concept requirements
4796 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4798 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4799 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4800 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4801 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4802 __glibcxx_requires_valid_range(__first
, __last
);
4804 for (; __first
!= __last
; ++__first
)
4805 if (__pred(*__first
))
4806 *__first
= __new_value
;
4810 * @brief Assign the result of a function object to each value in a
4812 * @ingroup mutating_algorithms
4813 * @param first A forward iterator.
4814 * @param last A forward iterator.
4815 * @param gen A function object taking no arguments and returning
4816 * std::iterator_traits<_ForwardIterator>::value_type
4817 * @return generate() returns no value.
4819 * Performs the assignment @c *i = @p gen() for each @c i in the range
4822 template<typename _ForwardIterator
, typename _Generator
>
4824 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4827 // concept requirements
4828 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4829 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4830 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4831 __glibcxx_requires_valid_range(__first
, __last
);
4833 for (; __first
!= __last
; ++__first
)
4838 * @brief Assign the result of a function object to each value in a
4840 * @ingroup mutating_algorithms
4841 * @param first A forward iterator.
4842 * @param n The length of the sequence.
4843 * @param gen A function object taking no arguments and returning
4844 * std::iterator_traits<_ForwardIterator>::value_type
4845 * @return The end of the sequence, @p first+n
4847 * Performs the assignment @c *i = @p gen() for each @c i in the range
4848 * @p [first,first+n).
4850 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4852 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4854 // concept requirements
4855 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4856 // "the type returned by a _Generator"
4857 __typeof__(__gen())>)
4859 for (; __n
> 0; --__n
, ++__first
)
4866 * @brief Copy a sequence, removing consecutive duplicate values.
4867 * @ingroup mutating_algorithms
4868 * @param first An input iterator.
4869 * @param last An input iterator.
4870 * @param result An output iterator.
4871 * @return An iterator designating the end of the resulting sequence.
4873 * Copies each element in the range @p [first,last) to the range
4874 * beginning at @p result, except that only the first element is copied
4875 * from groups of consecutive elements that compare equal.
4876 * unique_copy() is stable, so the relative order of elements that are
4877 * copied is unchanged.
4879 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4880 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4882 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4883 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4886 template<typename _InputIterator
, typename _OutputIterator
>
4887 inline _OutputIterator
4888 unique_copy(_InputIterator __first
, _InputIterator __last
,
4889 _OutputIterator __result
)
4891 // concept requirements
4892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4893 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4894 typename iterator_traits
<_InputIterator
>::value_type
>)
4895 __glibcxx_function_requires(_EqualityComparableConcept
<
4896 typename iterator_traits
<_InputIterator
>::value_type
>)
4897 __glibcxx_requires_valid_range(__first
, __last
);
4899 if (__first
== __last
)
4901 return std::__unique_copy(__first
, __last
, __result
,
4902 std::__iterator_category(__first
),
4903 std::__iterator_category(__result
));
4907 * @brief Copy a sequence, removing consecutive values using a predicate.
4908 * @ingroup mutating_algorithms
4909 * @param first An input iterator.
4910 * @param last An input iterator.
4911 * @param result An output iterator.
4912 * @param binary_pred A binary predicate.
4913 * @return An iterator designating the end of the resulting sequence.
4915 * Copies each element in the range @p [first,last) to the range
4916 * beginning at @p result, except that only the first element is copied
4917 * from groups of consecutive elements for which @p binary_pred returns
4919 * unique_copy() is stable, so the relative order of elements that are
4920 * copied is unchanged.
4922 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4923 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4925 template<typename _InputIterator
, typename _OutputIterator
,
4926 typename _BinaryPredicate
>
4927 inline _OutputIterator
4928 unique_copy(_InputIterator __first
, _InputIterator __last
,
4929 _OutputIterator __result
,
4930 _BinaryPredicate __binary_pred
)
4932 // concept requirements -- predicates checked later
4933 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4934 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4935 typename iterator_traits
<_InputIterator
>::value_type
>)
4936 __glibcxx_requires_valid_range(__first
, __last
);
4938 if (__first
== __last
)
4940 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4941 std::__iterator_category(__first
),
4942 std::__iterator_category(__result
));
4947 * @brief Randomly shuffle the elements of a sequence.
4948 * @ingroup mutating_algorithms
4949 * @param first A forward iterator.
4950 * @param last A forward iterator.
4953 * Reorder the elements in the range @p [first,last) using a random
4954 * distribution, so that every possible ordering of the sequence is
4957 template<typename _RandomAccessIterator
>
4959 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4961 // concept requirements
4962 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4963 _RandomAccessIterator
>)
4964 __glibcxx_requires_valid_range(__first
, __last
);
4966 if (__first
!= __last
)
4967 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4968 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4972 * @brief Shuffle the elements of a sequence using a random number
4974 * @ingroup mutating_algorithms
4975 * @param first A forward iterator.
4976 * @param last A forward iterator.
4977 * @param rand The RNG functor or function.
4980 * Reorders the elements in the range @p [first,last) using @p rand to
4981 * provide a random distribution. Calling @p rand(N) for a positive
4982 * integer @p N should return a randomly chosen integer from the
4985 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4987 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4988 _RandomNumberGenerator
& __rand
)
4990 // concept requirements
4991 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4992 _RandomAccessIterator
>)
4993 __glibcxx_requires_valid_range(__first
, __last
);
4995 if (__first
== __last
)
4997 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4998 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5003 * @brief Move elements for which a predicate is true to the beginning
5005 * @ingroup mutating_algorithms
5006 * @param first A forward iterator.
5007 * @param last A forward iterator.
5008 * @param pred A predicate functor.
5009 * @return An iterator @p middle such that @p pred(i) is true for each
5010 * iterator @p i in the range @p [first,middle) and false for each @p i
5011 * in the range @p [middle,last).
5013 * @p pred must not modify its operand. @p partition() does not preserve
5014 * the relative ordering of elements in each group, use
5015 * @p stable_partition() if this is needed.
5017 template<typename _ForwardIterator
, typename _Predicate
>
5018 inline _ForwardIterator
5019 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5022 // concept requirements
5023 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5025 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5026 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5027 __glibcxx_requires_valid_range(__first
, __last
);
5029 return std::__partition(__first
, __last
, __pred
,
5030 std::__iterator_category(__first
));
5036 * @brief Sort the smallest elements of a sequence.
5037 * @ingroup sorting_algorithms
5038 * @param first An iterator.
5039 * @param middle Another iterator.
5040 * @param last Another iterator.
5043 * Sorts the smallest @p (middle-first) elements in the range
5044 * @p [first,last) and moves them to the range @p [first,middle). The
5045 * order of the remaining elements in the range @p [middle,last) is
5047 * After the sort if @p i and @j are iterators in the range
5048 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5049 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5051 template<typename _RandomAccessIterator
>
5053 partial_sort(_RandomAccessIterator __first
,
5054 _RandomAccessIterator __middle
,
5055 _RandomAccessIterator __last
)
5057 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5060 // concept requirements
5061 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5062 _RandomAccessIterator
>)
5063 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5064 __glibcxx_requires_valid_range(__first
, __middle
);
5065 __glibcxx_requires_valid_range(__middle
, __last
);
5067 std::__heap_select(__first
, __middle
, __last
);
5068 std::sort_heap(__first
, __middle
);
5072 * @brief Sort the smallest elements of a sequence using a predicate
5074 * @ingroup sorting_algorithms
5075 * @param first An iterator.
5076 * @param middle Another iterator.
5077 * @param last Another iterator.
5078 * @param comp A comparison functor.
5081 * Sorts the smallest @p (middle-first) elements in the range
5082 * @p [first,last) and moves them to the range @p [first,middle). The
5083 * order of the remaining elements in the range @p [middle,last) is
5085 * After the sort if @p i and @j are iterators in the range
5086 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5087 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5090 template<typename _RandomAccessIterator
, typename _Compare
>
5092 partial_sort(_RandomAccessIterator __first
,
5093 _RandomAccessIterator __middle
,
5094 _RandomAccessIterator __last
,
5097 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5100 // concept requirements
5101 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5102 _RandomAccessIterator
>)
5103 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5104 _ValueType
, _ValueType
>)
5105 __glibcxx_requires_valid_range(__first
, __middle
);
5106 __glibcxx_requires_valid_range(__middle
, __last
);
5108 std::__heap_select(__first
, __middle
, __last
, __comp
);
5109 std::sort_heap(__first
, __middle
, __comp
);
5113 * @brief Sort a sequence just enough to find a particular position.
5114 * @ingroup sorting_algorithms
5115 * @param first An iterator.
5116 * @param nth Another iterator.
5117 * @param last Another iterator.
5120 * Rearranges the elements in the range @p [first,last) so that @p *nth
5121 * is the same element that would have been in that position had the
5122 * whole sequence been sorted.
5123 * whole sequence been sorted. The elements either side of @p *nth are
5124 * not completely sorted, but for any iterator @i in the range
5125 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5126 * holds that @p *j<*i is false.
5128 template<typename _RandomAccessIterator
>
5130 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5131 _RandomAccessIterator __last
)
5133 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5136 // concept requirements
5137 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5138 _RandomAccessIterator
>)
5139 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5140 __glibcxx_requires_valid_range(__first
, __nth
);
5141 __glibcxx_requires_valid_range(__nth
, __last
);
5143 if (__first
== __last
|| __nth
== __last
)
5146 std::__introselect(__first
, __nth
, __last
,
5147 std::__lg(__last
- __first
) * 2);
5151 * @brief Sort a sequence just enough to find a particular position
5152 * using a predicate for comparison.
5153 * @ingroup sorting_algorithms
5154 * @param first An iterator.
5155 * @param nth Another iterator.
5156 * @param last Another iterator.
5157 * @param comp A comparison functor.
5160 * Rearranges the elements in the range @p [first,last) so that @p *nth
5161 * is the same element that would have been in that position had the
5162 * whole sequence been sorted. The elements either side of @p *nth are
5163 * not completely sorted, but for any iterator @i in the range
5164 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5165 * holds that @p comp(*j,*i) is false.
5167 template<typename _RandomAccessIterator
, typename _Compare
>
5169 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5170 _RandomAccessIterator __last
, _Compare __comp
)
5172 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5175 // concept requirements
5176 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5177 _RandomAccessIterator
>)
5178 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5179 _ValueType
, _ValueType
>)
5180 __glibcxx_requires_valid_range(__first
, __nth
);
5181 __glibcxx_requires_valid_range(__nth
, __last
);
5183 if (__first
== __last
|| __nth
== __last
)
5186 std::__introselect(__first
, __nth
, __last
,
5187 std::__lg(__last
- __first
) * 2, __comp
);
5192 * @brief Sort the elements of a sequence.
5193 * @ingroup sorting_algorithms
5194 * @param first An iterator.
5195 * @param last Another iterator.
5198 * Sorts the elements in the range @p [first,last) in ascending order,
5199 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5200 * @p [first,last-1).
5202 * The relative ordering of equivalent elements is not preserved, use
5203 * @p stable_sort() if this is needed.
5205 template<typename _RandomAccessIterator
>
5207 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5209 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5212 // concept requirements
5213 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5214 _RandomAccessIterator
>)
5215 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5216 __glibcxx_requires_valid_range(__first
, __last
);
5218 if (__first
!= __last
)
5220 std::__introsort_loop(__first
, __last
,
5221 std::__lg(__last
- __first
) * 2);
5222 std::__final_insertion_sort(__first
, __last
);
5227 * @brief Sort the elements of a sequence using a predicate for comparison.
5228 * @ingroup sorting_algorithms
5229 * @param first An iterator.
5230 * @param last Another iterator.
5231 * @param comp A comparison functor.
5234 * Sorts the elements in the range @p [first,last) in ascending order,
5235 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5236 * range @p [first,last-1).
5238 * The relative ordering of equivalent elements is not preserved, use
5239 * @p stable_sort() if this is needed.
5241 template<typename _RandomAccessIterator
, typename _Compare
>
5243 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5246 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5249 // concept requirements
5250 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5251 _RandomAccessIterator
>)
5252 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5254 __glibcxx_requires_valid_range(__first
, __last
);
5256 if (__first
!= __last
)
5258 std::__introsort_loop(__first
, __last
,
5259 std::__lg(__last
- __first
) * 2, __comp
);
5260 std::__final_insertion_sort(__first
, __last
, __comp
);
5265 * @brief Merges two sorted ranges.
5266 * @ingroup sorting_algorithms
5267 * @param first1 An iterator.
5268 * @param first2 Another iterator.
5269 * @param last1 Another iterator.
5270 * @param last2 Another iterator.
5271 * @param result An iterator pointing to the end of the merged range.
5272 * @return An iterator pointing to the first element "not less
5275 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5276 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5277 * must be sorted, and the output range must not overlap with either of
5278 * the input ranges. The sort is @e stable, that is, for equivalent
5279 * elements in the two ranges, elements from the first range will always
5280 * come before elements from the second.
5282 template<typename _InputIterator1
, typename _InputIterator2
,
5283 typename _OutputIterator
>
5285 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5286 _InputIterator2 __first2
, _InputIterator2 __last2
,
5287 _OutputIterator __result
)
5289 typedef typename iterator_traits
<_InputIterator1
>::value_type
5291 typedef typename iterator_traits
<_InputIterator2
>::value_type
5294 // concept requirements
5295 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5296 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5297 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5299 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5301 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5302 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5303 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5305 while (__first1
!= __last1
&& __first2
!= __last2
)
5307 if (*__first2
< *__first1
)
5309 *__result
= *__first2
;
5314 *__result
= *__first1
;
5319 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5324 * @brief Merges two sorted ranges.
5325 * @ingroup sorting_algorithms
5326 * @param first1 An iterator.
5327 * @param first2 Another iterator.
5328 * @param last1 Another iterator.
5329 * @param last2 Another iterator.
5330 * @param result An iterator pointing to the end of the merged range.
5331 * @param comp A functor to use for comparisons.
5332 * @return An iterator pointing to the first element "not less
5335 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5336 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5337 * must be sorted, and the output range must not overlap with either of
5338 * the input ranges. The sort is @e stable, that is, for equivalent
5339 * elements in the two ranges, elements from the first range will always
5340 * come before elements from the second.
5342 * The comparison function should have the same effects on ordering as
5343 * the function used for the initial sort.
5345 template<typename _InputIterator1
, typename _InputIterator2
,
5346 typename _OutputIterator
, typename _Compare
>
5348 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5349 _InputIterator2 __first2
, _InputIterator2 __last2
,
5350 _OutputIterator __result
, _Compare __comp
)
5352 typedef typename iterator_traits
<_InputIterator1
>::value_type
5354 typedef typename iterator_traits
<_InputIterator2
>::value_type
5357 // concept requirements
5358 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5359 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5360 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5362 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5364 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5365 _ValueType2
, _ValueType1
>)
5366 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5367 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5369 while (__first1
!= __last1
&& __first2
!= __last2
)
5371 if (__comp(*__first2
, *__first1
))
5373 *__result
= *__first2
;
5378 *__result
= *__first1
;
5383 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5389 * @brief Sort the elements of a sequence, preserving the relative order
5390 * of equivalent elements.
5391 * @ingroup sorting_algorithms
5392 * @param first An iterator.
5393 * @param last Another iterator.
5396 * Sorts the elements in the range @p [first,last) in ascending order,
5397 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5398 * @p [first,last-1).
5400 * The relative ordering of equivalent elements is preserved, so any two
5401 * elements @p x and @p y in the range @p [first,last) such that
5402 * @p x<y is false and @p y<x is false will have the same relative
5403 * ordering after calling @p stable_sort().
5405 template<typename _RandomAccessIterator
>
5407 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5409 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5411 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5414 // concept requirements
5415 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5416 _RandomAccessIterator
>)
5417 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5418 __glibcxx_requires_valid_range(__first
, __last
);
5420 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5422 if (__buf
.begin() == 0)
5423 std::__inplace_stable_sort(__first
, __last
);
5425 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5426 _DistanceType(__buf
.size()));
5430 * @brief Sort the elements of a sequence using a predicate for comparison,
5431 * preserving the relative order of equivalent elements.
5432 * @ingroup sorting_algorithms
5433 * @param first An iterator.
5434 * @param last Another iterator.
5435 * @param comp A comparison functor.
5438 * Sorts the elements in the range @p [first,last) in ascending order,
5439 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5440 * range @p [first,last-1).
5442 * The relative ordering of equivalent elements is preserved, so any two
5443 * elements @p x and @p y in the range @p [first,last) such that
5444 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5445 * relative ordering after calling @p stable_sort().
5447 template<typename _RandomAccessIterator
, typename _Compare
>
5449 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5452 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5454 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5457 // concept requirements
5458 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5459 _RandomAccessIterator
>)
5460 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5463 __glibcxx_requires_valid_range(__first
, __last
);
5465 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5467 if (__buf
.begin() == 0)
5468 std::__inplace_stable_sort(__first
, __last
, __comp
);
5470 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5471 _DistanceType(__buf
.size()), __comp
);
5476 * @brief Return the union of two sorted ranges.
5477 * @ingroup set_algorithms
5478 * @param first1 Start of first range.
5479 * @param last1 End of first range.
5480 * @param first2 Start of second range.
5481 * @param last2 End of second range.
5482 * @return End of the output range.
5483 * @ingroup set_algorithms
5485 * This operation iterates over both ranges, copying elements present in
5486 * each range in order to the output range. Iterators increment for each
5487 * range. When the current element of one range is less than the other,
5488 * that element is copied and the iterator advanced. If an element is
5489 * contained in both ranges, the element from the first range is copied and
5490 * both ranges advance. The output range may not overlap either input
5493 template<typename _InputIterator1
, typename _InputIterator2
,
5494 typename _OutputIterator
>
5496 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5497 _InputIterator2 __first2
, _InputIterator2 __last2
,
5498 _OutputIterator __result
)
5500 typedef typename iterator_traits
<_InputIterator1
>::value_type
5502 typedef typename iterator_traits
<_InputIterator2
>::value_type
5505 // concept requirements
5506 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5507 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5508 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5510 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5512 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5513 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5514 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5515 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5517 while (__first1
!= __last1
&& __first2
!= __last2
)
5519 if (*__first1
< *__first2
)
5521 *__result
= *__first1
;
5524 else if (*__first2
< *__first1
)
5526 *__result
= *__first2
;
5531 *__result
= *__first1
;
5537 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5542 * @brief Return the union of two sorted ranges using a comparison functor.
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 * @param comp The comparison functor.
5549 * @return End of the output range.
5550 * @ingroup set_algorithms
5552 * This operation iterates over both ranges, copying elements present in
5553 * each range in order to the output range. Iterators increment for each
5554 * range. When the current element of one range is less than the other
5555 * according to @a comp, that element is copied and the iterator advanced.
5556 * If an equivalent element according to @a comp is contained in both
5557 * ranges, the element from the first range is copied and both ranges
5558 * advance. The output range may not overlap either input range.
5560 template<typename _InputIterator1
, typename _InputIterator2
,
5561 typename _OutputIterator
, typename _Compare
>
5563 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5564 _InputIterator2 __first2
, _InputIterator2 __last2
,
5565 _OutputIterator __result
, _Compare __comp
)
5567 typedef typename iterator_traits
<_InputIterator1
>::value_type
5569 typedef typename iterator_traits
<_InputIterator2
>::value_type
5572 // concept requirements
5573 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5574 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5575 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5577 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5579 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5580 _ValueType1
, _ValueType2
>)
5581 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5582 _ValueType2
, _ValueType1
>)
5583 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5584 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5586 while (__first1
!= __last1
&& __first2
!= __last2
)
5588 if (__comp(*__first1
, *__first2
))
5590 *__result
= *__first1
;
5593 else if (__comp(*__first2
, *__first1
))
5595 *__result
= *__first2
;
5600 *__result
= *__first1
;
5606 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5611 * @brief Return the intersection of two sorted ranges.
5612 * @ingroup set_algorithms
5613 * @param first1 Start of first range.
5614 * @param last1 End of first range.
5615 * @param first2 Start of second range.
5616 * @param last2 End of second range.
5617 * @return End of the output range.
5618 * @ingroup set_algorithms
5620 * This operation iterates over both ranges, copying elements present in
5621 * both ranges in order to the output range. Iterators increment for each
5622 * range. When the current element of one range is less than the other,
5623 * that iterator advances. If an element is contained in both ranges, the
5624 * element from the first range is copied and both ranges advance. The
5625 * output range may not overlap either input range.
5627 template<typename _InputIterator1
, typename _InputIterator2
,
5628 typename _OutputIterator
>
5630 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5631 _InputIterator2 __first2
, _InputIterator2 __last2
,
5632 _OutputIterator __result
)
5634 typedef typename iterator_traits
<_InputIterator1
>::value_type
5636 typedef typename iterator_traits
<_InputIterator2
>::value_type
5639 // concept requirements
5640 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5641 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5642 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5644 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5645 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5646 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5647 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5649 while (__first1
!= __last1
&& __first2
!= __last2
)
5650 if (*__first1
< *__first2
)
5652 else if (*__first2
< *__first1
)
5656 *__result
= *__first1
;
5665 * @brief Return the intersection of two sorted ranges using comparison
5667 * @ingroup set_algorithms
5668 * @param first1 Start of first range.
5669 * @param last1 End of first range.
5670 * @param first2 Start of second range.
5671 * @param last2 End of second range.
5672 * @param comp The comparison functor.
5673 * @return End of the output range.
5674 * @ingroup set_algorithms
5676 * This operation iterates over both ranges, copying elements present in
5677 * both ranges in order to the output range. Iterators increment for each
5678 * range. When the current element of one range is less than the other
5679 * according to @a comp, that iterator advances. If an element is
5680 * contained in both ranges according to @a comp, the element from the
5681 * first range is copied and both ranges advance. The output range may not
5682 * overlap either input range.
5684 template<typename _InputIterator1
, typename _InputIterator2
,
5685 typename _OutputIterator
, typename _Compare
>
5687 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5688 _InputIterator2 __first2
, _InputIterator2 __last2
,
5689 _OutputIterator __result
, _Compare __comp
)
5691 typedef typename iterator_traits
<_InputIterator1
>::value_type
5693 typedef typename iterator_traits
<_InputIterator2
>::value_type
5696 // concept requirements
5697 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5698 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5699 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5701 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5702 _ValueType1
, _ValueType2
>)
5703 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5704 _ValueType2
, _ValueType1
>)
5705 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5706 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5708 while (__first1
!= __last1
&& __first2
!= __last2
)
5709 if (__comp(*__first1
, *__first2
))
5711 else if (__comp(*__first2
, *__first1
))
5715 *__result
= *__first1
;
5724 * @brief Return the difference of two sorted ranges.
5725 * @ingroup set_algorithms
5726 * @param first1 Start of first range.
5727 * @param last1 End of first range.
5728 * @param first2 Start of second range.
5729 * @param last2 End of second range.
5730 * @return End of the output range.
5731 * @ingroup set_algorithms
5733 * This operation iterates over both ranges, copying elements present in
5734 * the first range but not the second in order to the output range.
5735 * Iterators increment for each range. When the current element of the
5736 * first range is less than the second, that element is copied and the
5737 * iterator advances. If the current element of the second range is less,
5738 * the iterator advances, but no element is copied. If an element is
5739 * contained in both ranges, no elements are copied and both ranges
5740 * advance. The output range may not overlap either input range.
5742 template<typename _InputIterator1
, typename _InputIterator2
,
5743 typename _OutputIterator
>
5745 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5746 _InputIterator2 __first2
, _InputIterator2 __last2
,
5747 _OutputIterator __result
)
5749 typedef typename iterator_traits
<_InputIterator1
>::value_type
5751 typedef typename iterator_traits
<_InputIterator2
>::value_type
5754 // concept requirements
5755 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5756 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5757 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5759 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5760 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5761 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5762 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5764 while (__first1
!= __last1
&& __first2
!= __last2
)
5765 if (*__first1
< *__first2
)
5767 *__result
= *__first1
;
5771 else if (*__first2
< *__first1
)
5778 return std::copy(__first1
, __last1
, __result
);
5782 * @brief Return the difference of two sorted ranges using comparison
5784 * @ingroup set_algorithms
5785 * @param first1 Start of first range.
5786 * @param last1 End of first range.
5787 * @param first2 Start of second range.
5788 * @param last2 End of second range.
5789 * @param comp The comparison functor.
5790 * @return End of the output range.
5791 * @ingroup set_algorithms
5793 * This operation iterates over both ranges, copying elements present in
5794 * the first range but not the second in order to the output range.
5795 * Iterators increment for each range. When the current element of the
5796 * first range is less than the second according to @a comp, that element
5797 * is copied and the iterator advances. If the current element of the
5798 * second range is less, no element is copied and the iterator advances.
5799 * If an element is contained in both ranges according to @a comp, no
5800 * elements are copied and both ranges advance. The output range may not
5801 * overlap either input range.
5803 template<typename _InputIterator1
, typename _InputIterator2
,
5804 typename _OutputIterator
, typename _Compare
>
5806 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5807 _InputIterator2 __first2
, _InputIterator2 __last2
,
5808 _OutputIterator __result
, _Compare __comp
)
5810 typedef typename iterator_traits
<_InputIterator1
>::value_type
5812 typedef typename iterator_traits
<_InputIterator2
>::value_type
5815 // concept requirements
5816 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5817 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5818 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5820 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5821 _ValueType1
, _ValueType2
>)
5822 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5823 _ValueType2
, _ValueType1
>)
5824 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5825 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5827 while (__first1
!= __last1
&& __first2
!= __last2
)
5828 if (__comp(*__first1
, *__first2
))
5830 *__result
= *__first1
;
5834 else if (__comp(*__first2
, *__first1
))
5841 return std::copy(__first1
, __last1
, __result
);
5845 * @brief Return the symmetric difference of two sorted ranges.
5846 * @ingroup set_algorithms
5847 * @param first1 Start of first range.
5848 * @param last1 End of first range.
5849 * @param first2 Start of second range.
5850 * @param last2 End of second range.
5851 * @return End of the output range.
5852 * @ingroup set_algorithms
5854 * This operation iterates over both ranges, copying elements present in
5855 * one range but not the other in order to the output range. Iterators
5856 * increment for each range. When the current element of one range is less
5857 * than the other, that element is copied and the iterator advances. If an
5858 * element is contained in both ranges, no elements are copied and both
5859 * ranges advance. The output range may not overlap either input range.
5861 template<typename _InputIterator1
, typename _InputIterator2
,
5862 typename _OutputIterator
>
5864 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5865 _InputIterator2 __first2
, _InputIterator2 __last2
,
5866 _OutputIterator __result
)
5868 typedef typename iterator_traits
<_InputIterator1
>::value_type
5870 typedef typename iterator_traits
<_InputIterator2
>::value_type
5873 // concept requirements
5874 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5875 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5876 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5878 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5880 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5881 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5882 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5883 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5885 while (__first1
!= __last1
&& __first2
!= __last2
)
5886 if (*__first1
< *__first2
)
5888 *__result
= *__first1
;
5892 else if (*__first2
< *__first1
)
5894 *__result
= *__first2
;
5903 return std::copy(__first2
, __last2
, std::copy(__first1
,
5904 __last1
, __result
));
5908 * @brief Return the symmetric difference of two sorted ranges using
5909 * comparison functor.
5910 * @ingroup set_algorithms
5911 * @param first1 Start of first range.
5912 * @param last1 End of first range.
5913 * @param first2 Start of second range.
5914 * @param last2 End of second range.
5915 * @param comp The comparison functor.
5916 * @return End of the output range.
5917 * @ingroup set_algorithms
5919 * This operation iterates over both ranges, copying elements present in
5920 * one range but not the other in order to the output range. Iterators
5921 * increment for each range. When the current element of one range is less
5922 * than the other according to @a comp, that element is copied and the
5923 * iterator advances. If an element is contained in both ranges according
5924 * to @a comp, no elements are copied and both ranges advance. The output
5925 * range may not overlap either input range.
5927 template<typename _InputIterator1
, typename _InputIterator2
,
5928 typename _OutputIterator
, typename _Compare
>
5930 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5931 _InputIterator2 __first2
, _InputIterator2 __last2
,
5932 _OutputIterator __result
,
5935 typedef typename iterator_traits
<_InputIterator1
>::value_type
5937 typedef typename iterator_traits
<_InputIterator2
>::value_type
5940 // concept requirements
5941 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5942 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5943 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5945 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5947 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5948 _ValueType1
, _ValueType2
>)
5949 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5950 _ValueType2
, _ValueType1
>)
5951 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5952 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5954 while (__first1
!= __last1
&& __first2
!= __last2
)
5955 if (__comp(*__first1
, *__first2
))
5957 *__result
= *__first1
;
5961 else if (__comp(*__first2
, *__first1
))
5963 *__result
= *__first2
;
5972 return std::copy(__first2
, __last2
,
5973 std::copy(__first1
, __last1
, __result
));
5978 * @brief Return the minimum element in a range.
5979 * @ingroup sorting_algorithms
5980 * @param first Start of range.
5981 * @param last End of range.
5982 * @return Iterator referencing the first instance of the smallest value.
5984 template<typename _ForwardIterator
>
5986 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5988 // concept requirements
5989 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5990 __glibcxx_function_requires(_LessThanComparableConcept
<
5991 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5992 __glibcxx_requires_valid_range(__first
, __last
);
5994 if (__first
== __last
)
5996 _ForwardIterator __result
= __first
;
5997 while (++__first
!= __last
)
5998 if (*__first
< *__result
)
6004 * @brief Return the minimum element in a range using comparison functor.
6005 * @ingroup sorting_algorithms
6006 * @param first Start of range.
6007 * @param last End of range.
6008 * @param comp Comparison functor.
6009 * @return Iterator referencing the first instance of the smallest value
6010 * according to comp.
6012 template<typename _ForwardIterator
, typename _Compare
>
6014 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6017 // concept requirements
6018 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6019 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6020 typename iterator_traits
<_ForwardIterator
>::value_type
,
6021 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6022 __glibcxx_requires_valid_range(__first
, __last
);
6024 if (__first
== __last
)
6026 _ForwardIterator __result
= __first
;
6027 while (++__first
!= __last
)
6028 if (__comp(*__first
, *__result
))
6034 * @brief Return the maximum element in a range.
6035 * @ingroup sorting_algorithms
6036 * @param first Start of range.
6037 * @param last End of range.
6038 * @return Iterator referencing the first instance of the largest value.
6040 template<typename _ForwardIterator
>
6042 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6044 // concept requirements
6045 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6046 __glibcxx_function_requires(_LessThanComparableConcept
<
6047 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6048 __glibcxx_requires_valid_range(__first
, __last
);
6050 if (__first
== __last
)
6052 _ForwardIterator __result
= __first
;
6053 while (++__first
!= __last
)
6054 if (*__result
< *__first
)
6060 * @brief Return the maximum element in a range using comparison functor.
6061 * @ingroup sorting_algorithms
6062 * @param first Start of range.
6063 * @param last End of range.
6064 * @param comp Comparison functor.
6065 * @return Iterator referencing the first instance of the largest value
6066 * according to comp.
6068 template<typename _ForwardIterator
, typename _Compare
>
6070 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6073 // concept requirements
6074 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6075 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6076 typename iterator_traits
<_ForwardIterator
>::value_type
,
6077 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6078 __glibcxx_requires_valid_range(__first
, __last
);
6080 if (__first
== __last
) return __first
;
6081 _ForwardIterator __result
= __first
;
6082 while (++__first
!= __last
)
6083 if (__comp(*__result
, *__first
))
6088 _GLIBCXX_END_NESTED_NAMESPACE
6090 #endif /* _STL_ALGO_H */