1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <cstdlib> // for rand
66 #include <bits/algorithmfwd.h>
67 #include <bits/stl_heap.h>
68 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
69 #include <debug/debug.h>
71 // See concept_check.h for the __glibcxx_*_requires macros.
73 _GLIBCXX_BEGIN_NAMESPACE(std
)
76 * @brief Find the median of three values.
80 * @return One of @p a, @p b or @p c.
82 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
83 * then the value returned will be @c m.
84 * This is an SGI extension.
85 * @ingroup SGIextensions
87 template<typename _Tp
>
89 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
91 // concept requirements
92 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
109 * @brief Find the median of three values using a predicate for comparison.
113 * @param comp A binary predicate.
114 * @return One of @p a, @p b or @p c.
116 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
117 * and @p comp(m,n) are both true then the value returned will be @c m.
118 * This is an SGI extension.
119 * @ingroup SGIextensions
121 template<typename _Tp
, typename _Compare
>
123 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
125 // concept requirements
126 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
128 if (__comp(__a
, __b
))
129 if (__comp(__b
, __c
))
131 else if (__comp(__a
, __c
))
135 else if (__comp(__a
, __c
))
137 else if (__comp(__b
, __c
))
145 /// This is an overload used by find() for the Input Iterator case.
146 template<typename _InputIterator
, typename _Tp
>
147 inline _InputIterator
148 __find(_InputIterator __first
, _InputIterator __last
,
149 const _Tp
& __val
, input_iterator_tag
)
151 while (__first
!= __last
&& !(*__first
== __val
))
156 /// This is an overload used by find_if() for the Input Iterator case.
157 template<typename _InputIterator
, typename _Predicate
>
158 inline _InputIterator
159 __find_if(_InputIterator __first
, _InputIterator __last
,
160 _Predicate __pred
, input_iterator_tag
)
162 while (__first
!= __last
&& !bool(__pred(*__first
)))
167 /// This is an overload used by find() for the RAI case.
168 template<typename _RandomAccessIterator
, typename _Tp
>
169 _RandomAccessIterator
170 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
171 const _Tp
& __val
, random_access_iterator_tag
)
173 typename iterator_traits
<_RandomAccessIterator
>::difference_type
174 __trip_count
= (__last
- __first
) >> 2;
176 for (; __trip_count
> 0; --__trip_count
)
178 if (*__first
== __val
)
182 if (*__first
== __val
)
186 if (*__first
== __val
)
190 if (*__first
== __val
)
195 switch (__last
- __first
)
198 if (*__first
== __val
)
202 if (*__first
== __val
)
206 if (*__first
== __val
)
215 /// This is an overload used by find_if() for the RAI case.
216 template<typename _RandomAccessIterator
, typename _Predicate
>
217 _RandomAccessIterator
218 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
219 _Predicate __pred
, random_access_iterator_tag
)
221 typename iterator_traits
<_RandomAccessIterator
>::difference_type
222 __trip_count
= (__last
- __first
) >> 2;
224 for (; __trip_count
> 0; --__trip_count
)
226 if (__pred(*__first
))
230 if (__pred(*__first
))
234 if (__pred(*__first
))
238 if (__pred(*__first
))
243 switch (__last
- __first
)
246 if (__pred(*__first
))
250 if (__pred(*__first
))
254 if (__pred(*__first
))
263 #ifdef __GXX_EXPERIMENTAL_CXX0X__
264 /// This is an overload used by find_if_not() for the Input Iterator case.
265 template<typename _InputIterator
, typename _Predicate
>
266 inline _InputIterator
267 __find_if_not(_InputIterator __first
, _InputIterator __last
,
268 _Predicate __pred
, input_iterator_tag
)
270 while (__first
!= __last
&& bool(__pred(*__first
)))
275 /// This is an overload used by find_if_not() for the RAI case.
276 template<typename _RandomAccessIterator
, typename _Predicate
>
277 _RandomAccessIterator
278 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
279 _Predicate __pred
, random_access_iterator_tag
)
281 typename iterator_traits
<_RandomAccessIterator
>::difference_type
282 __trip_count
= (__last
- __first
) >> 2;
284 for (; __trip_count
> 0; --__trip_count
)
286 if (!bool(__pred(*__first
)))
290 if (!bool(__pred(*__first
)))
294 if (!bool(__pred(*__first
)))
298 if (!bool(__pred(*__first
)))
303 switch (__last
- __first
)
306 if (!bool(__pred(*__first
)))
310 if (!bool(__pred(*__first
)))
314 if (!bool(__pred(*__first
)))
326 // set_symmetric_difference
338 * This is an uglified
339 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
340 * overloaded for forward iterators.
342 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
344 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
345 _Integer __count
, const _Tp
& __val
,
346 std::forward_iterator_tag
)
348 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
349 while (__first
!= __last
)
351 typename iterator_traits
<_ForwardIterator
>::difference_type
353 _ForwardIterator __i
= __first
;
355 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
364 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
370 * This is an uglified
371 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
372 * overloaded for random access iterators.
374 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
376 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
377 _Integer __count
, const _Tp
& __val
,
378 std::random_access_iterator_tag
)
381 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
384 _DistanceType __tailSize
= __last
- __first
;
385 const _DistanceType __pattSize
= __count
;
387 if (__tailSize
< __pattSize
)
390 const _DistanceType __skipOffset
= __pattSize
- 1;
391 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
392 __tailSize
-= __pattSize
;
394 while (1) // the main loop...
396 // __lookAhead here is always pointing to the last element of next
398 while (!(*__lookAhead
== __val
)) // the skip loop...
400 if (__tailSize
< __pattSize
)
401 return __last
; // Failure
402 __lookAhead
+= __pattSize
;
403 __tailSize
-= __pattSize
;
405 _DistanceType __remainder
= __skipOffset
;
406 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
407 *__backTrack
== __val
; --__backTrack
)
409 if (--__remainder
== 0)
410 return (__lookAhead
- __skipOffset
); // Success
412 if (__remainder
> __tailSize
)
413 return __last
; // Failure
414 __lookAhead
+= __remainder
;
415 __tailSize
-= __remainder
;
422 * This is an uglified
423 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
425 * overloaded for forward iterators.
427 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
428 typename _BinaryPredicate
>
430 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
431 _Integer __count
, const _Tp
& __val
,
432 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
434 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
437 while (__first
!= __last
)
439 typename iterator_traits
<_ForwardIterator
>::difference_type
441 _ForwardIterator __i
= __first
;
443 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
453 while (__first
!= __last
454 && !bool(__binary_pred(*__first
, __val
)))
461 * This is an uglified
462 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
464 * overloaded for random access iterators.
466 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
467 typename _BinaryPredicate
>
469 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
470 _Integer __count
, const _Tp
& __val
,
471 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
474 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
477 _DistanceType __tailSize
= __last
- __first
;
478 const _DistanceType __pattSize
= __count
;
480 if (__tailSize
< __pattSize
)
483 const _DistanceType __skipOffset
= __pattSize
- 1;
484 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
485 __tailSize
-= __pattSize
;
487 while (1) // the main loop...
489 // __lookAhead here is always pointing to the last element of next
491 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
493 if (__tailSize
< __pattSize
)
494 return __last
; // Failure
495 __lookAhead
+= __pattSize
;
496 __tailSize
-= __pattSize
;
498 _DistanceType __remainder
= __skipOffset
;
499 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
500 __binary_pred(*__backTrack
, __val
); --__backTrack
)
502 if (--__remainder
== 0)
503 return (__lookAhead
- __skipOffset
); // Success
505 if (__remainder
> __tailSize
)
506 return __last
; // Failure
507 __lookAhead
+= __remainder
;
508 __tailSize
-= __remainder
;
512 // find_end for forward iterators.
513 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
515 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
516 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
517 forward_iterator_tag
, forward_iterator_tag
)
519 if (__first2
== __last2
)
523 _ForwardIterator1 __result
= __last1
;
526 _ForwardIterator1 __new_result
527 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
528 if (__new_result
== __last1
)
532 __result
= __new_result
;
533 __first1
= __new_result
;
540 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
541 typename _BinaryPredicate
>
543 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
544 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
545 forward_iterator_tag
, forward_iterator_tag
,
546 _BinaryPredicate __comp
)
548 if (__first2
== __last2
)
552 _ForwardIterator1 __result
= __last1
;
555 _ForwardIterator1 __new_result
556 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
558 if (__new_result
== __last1
)
562 __result
= __new_result
;
563 __first1
= __new_result
;
570 // find_end for bidirectional iterators (much faster).
571 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
572 _BidirectionalIterator1
573 __find_end(_BidirectionalIterator1 __first1
,
574 _BidirectionalIterator1 __last1
,
575 _BidirectionalIterator2 __first2
,
576 _BidirectionalIterator2 __last2
,
577 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
579 // concept requirements
580 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
581 _BidirectionalIterator1
>)
582 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
583 _BidirectionalIterator2
>)
585 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
586 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
588 _RevIterator1
__rlast1(__first1
);
589 _RevIterator2
__rlast2(__first2
);
590 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
592 _RevIterator2(__last2
),
595 if (__rresult
== __rlast1
)
599 _BidirectionalIterator1 __result
= __rresult
.base();
600 std::advance(__result
, -std::distance(__first2
, __last2
));
605 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
606 typename _BinaryPredicate
>
607 _BidirectionalIterator1
608 __find_end(_BidirectionalIterator1 __first1
,
609 _BidirectionalIterator1 __last1
,
610 _BidirectionalIterator2 __first2
,
611 _BidirectionalIterator2 __last2
,
612 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
613 _BinaryPredicate __comp
)
615 // concept requirements
616 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
617 _BidirectionalIterator1
>)
618 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
619 _BidirectionalIterator2
>)
621 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
622 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
624 _RevIterator1
__rlast1(__first1
);
625 _RevIterator2
__rlast2(__first2
);
626 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
627 _RevIterator2(__last2
), __rlast2
,
630 if (__rresult
== __rlast1
)
634 _BidirectionalIterator1 __result
= __rresult
.base();
635 std::advance(__result
, -std::distance(__first2
, __last2
));
641 * @brief Find last matching subsequence in a sequence.
642 * @param first1 Start of range to search.
643 * @param last1 End of range to search.
644 * @param first2 Start of sequence to match.
645 * @param last2 End of sequence to match.
646 * @return The last iterator @c i in the range
647 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
648 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
649 * such iterator exists.
651 * Searches the range @p [first1,last1) for a sub-sequence that compares
652 * equal value-by-value with the sequence given by @p [first2,last2) and
653 * returns an iterator to the first element of the sub-sequence, or
654 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
655 * last such subsequence contained in [first,last1).
657 * Because the sub-sequence must lie completely within the range
658 * @p [first1,last1) it must start at a position less than
659 * @p last1-(last2-first2) where @p last2-first2 is the length of the
661 * This means that the returned iterator @c i will be in the range
662 * @p [first1,last1-(last2-first2))
664 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
665 inline _ForwardIterator1
666 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
667 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
669 // concept requirements
670 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
671 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
672 __glibcxx_function_requires(_EqualOpConcept
<
673 typename iterator_traits
<_ForwardIterator1
>::value_type
,
674 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
675 __glibcxx_requires_valid_range(__first1
, __last1
);
676 __glibcxx_requires_valid_range(__first2
, __last2
);
678 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
679 std::__iterator_category(__first1
),
680 std::__iterator_category(__first2
));
684 * @brief Find last matching subsequence in a sequence using a predicate.
685 * @param first1 Start of range to search.
686 * @param last1 End of range to search.
687 * @param first2 Start of sequence to match.
688 * @param last2 End of sequence to match.
689 * @param comp The predicate to use.
690 * @return The last iterator @c i in the range
691 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
692 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
693 * @p last1 if no such iterator exists.
695 * Searches the range @p [first1,last1) for a sub-sequence that compares
696 * equal value-by-value with the sequence given by @p [first2,last2) using
697 * comp as a predicate and returns an iterator to the first element of the
698 * sub-sequence, or @p last1 if the sub-sequence is not found. The
699 * sub-sequence will be the last such subsequence contained in
702 * Because the sub-sequence must lie completely within the range
703 * @p [first1,last1) it must start at a position less than
704 * @p last1-(last2-first2) where @p last2-first2 is the length of the
706 * This means that the returned iterator @c i will be in the range
707 * @p [first1,last1-(last2-first2))
709 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
710 typename _BinaryPredicate
>
711 inline _ForwardIterator1
712 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
713 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
714 _BinaryPredicate __comp
)
716 // concept requirements
717 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
718 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
719 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
720 typename iterator_traits
<_ForwardIterator1
>::value_type
,
721 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
722 __glibcxx_requires_valid_range(__first1
, __last1
);
723 __glibcxx_requires_valid_range(__first2
, __last2
);
725 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
726 std::__iterator_category(__first1
),
727 std::__iterator_category(__first2
),
731 #ifdef __GXX_EXPERIMENTAL_CXX0X__
733 * @brief Checks that a predicate is true for all the elements
735 * @param first An input iterator.
736 * @param last An input iterator.
737 * @param pred A predicate.
738 * @return True if the check is true, false otherwise.
740 * Returns true if @p pred is true for each element in the range
741 * @p [first,last), and false otherwise.
743 template<typename _InputIterator
, typename _Predicate
>
745 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
746 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
749 * @brief Checks that a predicate is false for all the elements
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 * @param first An input iterator.
768 * @param last An input iterator.
769 * @param pred A predicate.
770 * @return True if the check is true, false otherwise.
772 * Returns true if an element exists in the range @p [first,last) such that
773 * @p pred is true, and false otherwise.
775 template<typename _InputIterator
, typename _Predicate
>
777 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
778 { return !std::none_of(__first
, __last
, __pred
); }
781 * @brief Find the first element in a sequence for which a
782 * predicate is false.
783 * @param first An input iterator.
784 * @param last An input iterator.
785 * @param pred A predicate.
786 * @return The first iterator @c i in the range @p [first,last)
787 * such that @p pred(*i) is false, or @p last if no such iterator exists.
789 template<typename _InputIterator
, typename _Predicate
>
790 inline _InputIterator
791 find_if_not(_InputIterator __first
, _InputIterator __last
,
794 // concept requirements
795 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
796 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
797 typename iterator_traits
<_InputIterator
>::value_type
>)
798 __glibcxx_requires_valid_range(__first
, __last
);
799 return std::__find_if_not(__first
, __last
, __pred
,
800 std::__iterator_category(__first
));
804 * @brief Checks whether the sequence is partitioned.
805 * @param first An input iterator.
806 * @param last An input iterator.
807 * @param pred A predicate.
808 * @return True if the range @p [first,last) is partioned by @p pred,
809 * i.e. if all elements that satisfy @p pred appear before those that
812 template<typename _InputIterator
, typename _Predicate
>
814 is_partitioned(_InputIterator __first
, _InputIterator __last
,
817 __first
= std::find_if_not(__first
, __last
, __pred
);
818 return std::none_of(__first
, __last
, __pred
);
822 * @brief Find the partition point of a partitioned range.
823 * @param first An iterator.
824 * @param last Another iterator.
825 * @param pred A predicate.
826 * @return An iterator @p mid such that @p all_of(first, mid, pred)
827 * and @p none_of(mid, last, pred) are both true.
829 template<typename _ForwardIterator
, typename _Predicate
>
831 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
834 // concept requirements
835 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
836 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
837 typename iterator_traits
<_ForwardIterator
>::value_type
>)
839 // A specific debug-mode test will be necessary...
840 __glibcxx_requires_valid_range(__first
, __last
);
842 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
845 _DistanceType __len
= std::distance(__first
, __last
);
846 _DistanceType __half
;
847 _ForwardIterator __middle
;
853 std::advance(__middle
, __half
);
854 if (__pred(*__middle
))
858 __len
= __len
- __half
- 1;
869 * @brief Copy a sequence, removing elements of a given value.
870 * @param first An input iterator.
871 * @param last An input iterator.
872 * @param result An output iterator.
873 * @param value The value to be removed.
874 * @return An iterator designating the end of the resulting sequence.
876 * Copies each element in the range @p [first,last) not equal to @p value
877 * to the range beginning at @p result.
878 * remove_copy() is stable, so the relative order of elements that are
879 * copied is unchanged.
881 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
883 remove_copy(_InputIterator __first
, _InputIterator __last
,
884 _OutputIterator __result
, const _Tp
& __value
)
886 // concept requirements
887 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
888 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
889 typename iterator_traits
<_InputIterator
>::value_type
>)
890 __glibcxx_function_requires(_EqualOpConcept
<
891 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
892 __glibcxx_requires_valid_range(__first
, __last
);
894 for (; __first
!= __last
; ++__first
)
895 if (!(*__first
== __value
))
897 *__result
= *__first
;
904 * @brief Copy a sequence, removing elements for which a predicate is true.
905 * @param first An input iterator.
906 * @param last An input iterator.
907 * @param result An output iterator.
908 * @param pred A predicate.
909 * @return An iterator designating the end of the resulting sequence.
911 * Copies each element in the range @p [first,last) for which
912 * @p pred returns false to the range beginning at @p result.
914 * remove_copy_if() is stable, so the relative order of elements that are
915 * copied is unchanged.
917 template<typename _InputIterator
, typename _OutputIterator
,
920 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
921 _OutputIterator __result
, _Predicate __pred
)
923 // concept requirements
924 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
925 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
926 typename iterator_traits
<_InputIterator
>::value_type
>)
927 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
928 typename iterator_traits
<_InputIterator
>::value_type
>)
929 __glibcxx_requires_valid_range(__first
, __last
);
931 for (; __first
!= __last
; ++__first
)
932 if (!bool(__pred(*__first
)))
934 *__result
= *__first
;
940 #ifdef __GXX_EXPERIMENTAL_CXX0X__
942 * @brief Copy the elements of a sequence for which a predicate is true.
943 * @param first An input iterator.
944 * @param last An input iterator.
945 * @param result An output iterator.
946 * @param pred A predicate.
947 * @return An iterator designating the end of the resulting sequence.
949 * Copies each element in the range @p [first,last) for which
950 * @p pred returns true to the range beginning at @p result.
952 * copy_if() is stable, so the relative order of elements that are
953 * copied is unchanged.
955 template<typename _InputIterator
, typename _OutputIterator
,
958 copy_if(_InputIterator __first
, _InputIterator __last
,
959 _OutputIterator __result
, _Predicate __pred
)
961 // concept requirements
962 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
963 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
964 typename iterator_traits
<_InputIterator
>::value_type
>)
965 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
966 typename iterator_traits
<_InputIterator
>::value_type
>)
967 __glibcxx_requires_valid_range(__first
, __last
);
969 for (; __first
!= __last
; ++__first
)
970 if (__pred(*__first
))
972 *__result
= *__first
;
979 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
981 __copy_n(_InputIterator __first
, _Size __n
,
982 _OutputIterator __result
, input_iterator_tag
)
984 for (; __n
> 0; --__n
)
986 *__result
= *__first
;
993 template<typename _RandomAccessIterator
, typename _Size
,
994 typename _OutputIterator
>
995 inline _OutputIterator
996 __copy_n(_RandomAccessIterator __first
, _Size __n
,
997 _OutputIterator __result
, random_access_iterator_tag
)
998 { return std::copy(__first
, __first
+ __n
, __result
); }
1001 * @brief Copies the range [first,first+n) into [result,result+n).
1002 * @param first An input iterator.
1003 * @param n The number of elements to copy.
1004 * @param result An output iterator.
1007 * This inline function will boil down to a call to @c memmove whenever
1008 * possible. Failing that, if random access iterators are passed, then the
1009 * loop count will be known (and therefore a candidate for compiler
1010 * optimizations such as unrolling).
1012 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1013 inline _OutputIterator
1014 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1016 // concept requirements
1017 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1018 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1019 typename iterator_traits
<_InputIterator
>::value_type
>)
1021 return std::__copy_n(__first
, __n
, __result
,
1022 std::__iterator_category(__first
));
1026 * @brief Copy the elements of a sequence to separate output sequences
1027 * depending on the truth value of a predicate.
1028 * @param first An input iterator.
1029 * @param last An input iterator.
1030 * @param out_true An output iterator.
1031 * @param out_false An output iterator.
1032 * @param pred A predicate.
1033 * @return A pair designating the ends of the resulting sequences.
1035 * Copies each element in the range @p [first,last) for which
1036 * @p pred returns true to the range beginning at @p out_true
1037 * and each element for which @p pred returns false to @p out_false.
1039 template<typename _InputIterator
, typename _OutputIterator1
,
1040 typename _OutputIterator2
, typename _Predicate
>
1041 pair
<_OutputIterator1
, _OutputIterator2
>
1042 partition_copy(_InputIterator __first
, _InputIterator __last
,
1043 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1046 // concept requirements
1047 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1048 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1049 typename iterator_traits
<_InputIterator
>::value_type
>)
1050 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1051 typename iterator_traits
<_InputIterator
>::value_type
>)
1052 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1053 typename iterator_traits
<_InputIterator
>::value_type
>)
1054 __glibcxx_requires_valid_range(__first
, __last
);
1056 for (; __first
!= __last
; ++__first
)
1057 if (__pred(*__first
))
1059 *__out_true
= *__first
;
1064 *__out_false
= *__first
;
1068 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1073 * @brief Remove elements from a sequence.
1074 * @param first An input iterator.
1075 * @param last An input iterator.
1076 * @param value The value to be removed.
1077 * @return An iterator designating the end of the resulting sequence.
1079 * All elements equal to @p value are removed from the range
1082 * remove() is stable, so the relative order of elements that are
1083 * not removed is unchanged.
1085 * Elements between the end of the resulting sequence and @p last
1086 * are still present, but their value is unspecified.
1088 template<typename _ForwardIterator
, typename _Tp
>
1090 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1093 // concept requirements
1094 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1096 __glibcxx_function_requires(_EqualOpConcept
<
1097 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1098 __glibcxx_requires_valid_range(__first
, __last
);
1100 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1101 if(__first
== __last
)
1103 _ForwardIterator __result
= __first
;
1105 for(; __first
!= __last
; ++__first
)
1106 if(!(*__first
== __value
))
1108 *__result
= _GLIBCXX_MOVE(*__first
);
1115 * @brief Remove elements from a sequence using a predicate.
1116 * @param first A forward iterator.
1117 * @param last A forward iterator.
1118 * @param pred A predicate.
1119 * @return An iterator designating the end of the resulting sequence.
1121 * All elements for which @p pred returns true are removed from the range
1124 * remove_if() is stable, so the relative order of elements that are
1125 * not removed is unchanged.
1127 * Elements between the end of the resulting sequence and @p last
1128 * are still present, but their value is unspecified.
1130 template<typename _ForwardIterator
, typename _Predicate
>
1132 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1135 // concept requirements
1136 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1138 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1139 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1140 __glibcxx_requires_valid_range(__first
, __last
);
1142 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1143 if(__first
== __last
)
1145 _ForwardIterator __result
= __first
;
1147 for(; __first
!= __last
; ++__first
)
1148 if(!bool(__pred(*__first
)))
1150 *__result
= _GLIBCXX_MOVE(*__first
);
1157 * @brief Remove consecutive duplicate values from a sequence.
1158 * @param first A forward iterator.
1159 * @param last A forward iterator.
1160 * @return An iterator designating the end of the resulting sequence.
1162 * Removes all but the first element from each group of consecutive
1163 * values that compare equal.
1164 * unique() is stable, so the relative order of elements that are
1165 * not removed is unchanged.
1166 * Elements between the end of the resulting sequence and @p last
1167 * are still present, but their value is unspecified.
1169 template<typename _ForwardIterator
>
1171 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1173 // concept requirements
1174 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1176 __glibcxx_function_requires(_EqualityComparableConcept
<
1177 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1178 __glibcxx_requires_valid_range(__first
, __last
);
1180 // Skip the beginning, if already unique.
1181 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1182 if (__first
== __last
)
1185 // Do the real copy work.
1186 _ForwardIterator __dest
= __first
;
1188 while (++__first
!= __last
)
1189 if (!(*__dest
== *__first
))
1190 *++__dest
= _GLIBCXX_MOVE(*__first
);
1195 * @brief Remove consecutive values from a sequence using a predicate.
1196 * @param first A forward iterator.
1197 * @param last A forward iterator.
1198 * @param binary_pred A binary predicate.
1199 * @return An iterator designating the end of the resulting sequence.
1201 * Removes all but the first element from each group of consecutive
1202 * values for which @p binary_pred returns true.
1203 * unique() is stable, so the relative order of elements that are
1204 * not removed is unchanged.
1205 * Elements between the end of the resulting sequence and @p last
1206 * are still present, but their value is unspecified.
1208 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1210 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1211 _BinaryPredicate __binary_pred
)
1213 // concept requirements
1214 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1216 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1217 typename iterator_traits
<_ForwardIterator
>::value_type
,
1218 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1219 __glibcxx_requires_valid_range(__first
, __last
);
1221 // Skip the beginning, if already unique.
1222 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1223 if (__first
== __last
)
1226 // Do the real copy work.
1227 _ForwardIterator __dest
= __first
;
1229 while (++__first
!= __last
)
1230 if (!bool(__binary_pred(*__dest
, *__first
)))
1231 *++__dest
= _GLIBCXX_MOVE(*__first
);
1236 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1238 * overloaded for forward iterators and output iterator as result.
1240 template<typename _ForwardIterator
, typename _OutputIterator
>
1242 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1243 _OutputIterator __result
,
1244 forward_iterator_tag
, output_iterator_tag
)
1246 // concept requirements -- taken care of in dispatching function
1247 _ForwardIterator __next
= __first
;
1248 *__result
= *__first
;
1249 while (++__next
!= __last
)
1250 if (!(*__first
== *__next
))
1253 *++__result
= *__first
;
1259 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1261 * overloaded for input iterators and output iterator as result.
1263 template<typename _InputIterator
, typename _OutputIterator
>
1265 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1266 _OutputIterator __result
,
1267 input_iterator_tag
, output_iterator_tag
)
1269 // concept requirements -- taken care of in dispatching function
1270 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1271 *__result
= __value
;
1272 while (++__first
!= __last
)
1273 if (!(__value
== *__first
))
1276 *++__result
= __value
;
1282 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1284 * overloaded for input iterators and forward iterator as result.
1286 template<typename _InputIterator
, typename _ForwardIterator
>
1288 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1289 _ForwardIterator __result
,
1290 input_iterator_tag
, forward_iterator_tag
)
1292 // concept requirements -- taken care of in dispatching function
1293 *__result
= *__first
;
1294 while (++__first
!= __last
)
1295 if (!(*__result
== *__first
))
1296 *++__result
= *__first
;
1301 * This is an uglified
1302 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1304 * overloaded for forward iterators and output iterator as result.
1306 template<typename _ForwardIterator
, typename _OutputIterator
,
1307 typename _BinaryPredicate
>
1309 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1310 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1311 forward_iterator_tag
, output_iterator_tag
)
1313 // concept requirements -- iterators already checked
1314 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1315 typename iterator_traits
<_ForwardIterator
>::value_type
,
1316 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1318 _ForwardIterator __next
= __first
;
1319 *__result
= *__first
;
1320 while (++__next
!= __last
)
1321 if (!bool(__binary_pred(*__first
, *__next
)))
1324 *++__result
= *__first
;
1330 * This is an uglified
1331 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1333 * overloaded for input iterators and output iterator as result.
1335 template<typename _InputIterator
, typename _OutputIterator
,
1336 typename _BinaryPredicate
>
1338 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1339 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1340 input_iterator_tag
, output_iterator_tag
)
1342 // concept requirements -- iterators already checked
1343 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1344 typename iterator_traits
<_InputIterator
>::value_type
,
1345 typename iterator_traits
<_InputIterator
>::value_type
>)
1347 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1348 *__result
= __value
;
1349 while (++__first
!= __last
)
1350 if (!bool(__binary_pred(__value
, *__first
)))
1353 *++__result
= __value
;
1359 * This is an uglified
1360 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1362 * overloaded for input iterators and forward iterator as result.
1364 template<typename _InputIterator
, typename _ForwardIterator
,
1365 typename _BinaryPredicate
>
1367 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1368 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1369 input_iterator_tag
, forward_iterator_tag
)
1371 // concept requirements -- iterators already checked
1372 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1373 typename iterator_traits
<_ForwardIterator
>::value_type
,
1374 typename iterator_traits
<_InputIterator
>::value_type
>)
1376 *__result
= *__first
;
1377 while (++__first
!= __last
)
1378 if (!bool(__binary_pred(*__result
, *__first
)))
1379 *++__result
= *__first
;
1384 * This is an uglified reverse(_BidirectionalIterator,
1385 * _BidirectionalIterator)
1386 * overloaded for bidirectional iterators.
1388 template<typename _BidirectionalIterator
>
1390 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1391 bidirectional_iterator_tag
)
1394 if (__first
== __last
|| __first
== --__last
)
1398 std::iter_swap(__first
, __last
);
1404 * This is an uglified reverse(_BidirectionalIterator,
1405 * _BidirectionalIterator)
1406 * overloaded for random access iterators.
1408 template<typename _RandomAccessIterator
>
1410 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1411 random_access_iterator_tag
)
1413 if (__first
== __last
)
1416 while (__first
< __last
)
1418 std::iter_swap(__first
, __last
);
1425 * @brief Reverse a sequence.
1426 * @param first A bidirectional iterator.
1427 * @param last A bidirectional iterator.
1428 * @return reverse() returns no value.
1430 * Reverses the order of the elements in the range @p [first,last),
1431 * so that the first element becomes the last etc.
1432 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1433 * swaps @p *(first+i) and @p *(last-(i+1))
1435 template<typename _BidirectionalIterator
>
1437 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1439 // concept requirements
1440 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1441 _BidirectionalIterator
>)
1442 __glibcxx_requires_valid_range(__first
, __last
);
1443 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1447 * @brief Copy a sequence, reversing its elements.
1448 * @param first A bidirectional iterator.
1449 * @param last A bidirectional iterator.
1450 * @param result An output iterator.
1451 * @return An iterator designating the end of the resulting sequence.
1453 * Copies the elements in the range @p [first,last) to the range
1454 * @p [result,result+(last-first)) such that the order of the
1455 * elements is reversed.
1456 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1457 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1458 * The ranges @p [first,last) and @p [result,result+(last-first))
1461 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1463 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1464 _OutputIterator __result
)
1466 // concept requirements
1467 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1468 _BidirectionalIterator
>)
1469 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1470 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1471 __glibcxx_requires_valid_range(__first
, __last
);
1473 while (__first
!= __last
)
1476 *__result
= *__last
;
1483 * This is a helper function for the rotate algorithm specialized on RAIs.
1484 * It returns the greatest common divisor of two integer values.
1486 template<typename _EuclideanRingElement
>
1487 _EuclideanRingElement
1488 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1492 _EuclideanRingElement __t
= __m
% __n
;
1499 /// This is a helper function for the rotate algorithm.
1500 template<typename _ForwardIterator
>
1502 __rotate(_ForwardIterator __first
,
1503 _ForwardIterator __middle
,
1504 _ForwardIterator __last
,
1505 forward_iterator_tag
)
1507 if (__first
== __middle
|| __last
== __middle
)
1510 _ForwardIterator __first2
= __middle
;
1513 std::iter_swap(__first
, __first2
);
1516 if (__first
== __middle
)
1517 __middle
= __first2
;
1519 while (__first2
!= __last
);
1521 __first2
= __middle
;
1523 while (__first2
!= __last
)
1525 std::iter_swap(__first
, __first2
);
1528 if (__first
== __middle
)
1529 __middle
= __first2
;
1530 else if (__first2
== __last
)
1531 __first2
= __middle
;
1535 /// This is a helper function for the rotate algorithm.
1536 template<typename _BidirectionalIterator
>
1538 __rotate(_BidirectionalIterator __first
,
1539 _BidirectionalIterator __middle
,
1540 _BidirectionalIterator __last
,
1541 bidirectional_iterator_tag
)
1543 // concept requirements
1544 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1545 _BidirectionalIterator
>)
1547 if (__first
== __middle
|| __last
== __middle
)
1550 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1551 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1553 while (__first
!= __middle
&& __middle
!= __last
)
1555 std::iter_swap(__first
, --__last
);
1559 if (__first
== __middle
)
1560 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1562 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1565 /// This is a helper function for the rotate algorithm.
1566 template<typename _RandomAccessIterator
>
1568 __rotate(_RandomAccessIterator __first
,
1569 _RandomAccessIterator __middle
,
1570 _RandomAccessIterator __last
,
1571 random_access_iterator_tag
)
1573 // concept requirements
1574 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1575 _RandomAccessIterator
>)
1577 if (__first
== __middle
|| __last
== __middle
)
1580 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1582 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1585 const _Distance __n
= __last
- __first
;
1586 const _Distance __k
= __middle
- __first
;
1587 const _Distance __l
= __n
- __k
;
1591 std::swap_ranges(__first
, __middle
, __middle
);
1595 const _Distance __d
= std::__gcd(__n
, __k
);
1597 for (_Distance __i
= 0; __i
< __d
; __i
++)
1599 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1600 _RandomAccessIterator __p
= __first
;
1604 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1606 if (__p
> __first
+ __l
)
1608 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1612 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1618 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1620 if (__p
< __last
- __k
)
1622 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1625 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1630 *__p
= _GLIBCXX_MOVE(__tmp
);
1636 * @brief Rotate the elements of a sequence.
1637 * @param first A forward iterator.
1638 * @param middle A forward iterator.
1639 * @param last A forward iterator.
1642 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1643 * positions so that the element at @p middle is moved to @p first, the
1644 * element at @p middle+1 is moved to @first+1 and so on for each element
1645 * in the range @p [first,last).
1647 * This effectively swaps the ranges @p [first,middle) and
1650 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1651 * each @p n in the range @p [0,last-first).
1653 template<typename _ForwardIterator
>
1655 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1656 _ForwardIterator __last
)
1658 // concept requirements
1659 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1661 __glibcxx_requires_valid_range(__first
, __middle
);
1662 __glibcxx_requires_valid_range(__middle
, __last
);
1664 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1666 std::__rotate(__first
, __middle
, __last
, _IterType());
1670 * @brief Copy a sequence, rotating its elements.
1671 * @param first A forward iterator.
1672 * @param middle A forward iterator.
1673 * @param last A forward iterator.
1674 * @param result An output iterator.
1675 * @return An iterator designating the end of the resulting sequence.
1677 * Copies the elements of the range @p [first,last) to the range
1678 * beginning at @result, rotating the copied elements by @p (middle-first)
1679 * positions so that the element at @p middle is moved to @p result, the
1680 * element at @p middle+1 is moved to @result+1 and so on for each element
1681 * in the range @p [first,last).
1683 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1684 * each @p n in the range @p [0,last-first).
1686 template<typename _ForwardIterator
, typename _OutputIterator
>
1688 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1689 _ForwardIterator __last
, _OutputIterator __result
)
1691 // concept requirements
1692 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1693 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1694 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1695 __glibcxx_requires_valid_range(__first
, __middle
);
1696 __glibcxx_requires_valid_range(__middle
, __last
);
1698 return std::copy(__first
, __middle
,
1699 std::copy(__middle
, __last
, __result
));
1702 /// This is a helper function...
1703 template<typename _ForwardIterator
, typename _Predicate
>
1705 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1706 _Predicate __pred
, forward_iterator_tag
)
1708 if (__first
== __last
)
1711 while (__pred(*__first
))
1712 if (++__first
== __last
)
1715 _ForwardIterator __next
= __first
;
1717 while (++__next
!= __last
)
1718 if (__pred(*__next
))
1720 std::iter_swap(__first
, __next
);
1727 /// This is a helper function...
1728 template<typename _BidirectionalIterator
, typename _Predicate
>
1729 _BidirectionalIterator
1730 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1731 _Predicate __pred
, bidirectional_iterator_tag
)
1736 if (__first
== __last
)
1738 else if (__pred(*__first
))
1744 if (__first
== __last
)
1746 else if (!bool(__pred(*__last
)))
1750 std::iter_swap(__first
, __last
);
1757 /// This is a helper function...
1758 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1760 __inplace_stable_partition(_ForwardIterator __first
,
1761 _ForwardIterator __last
,
1762 _Predicate __pred
, _Distance __len
)
1765 return __pred(*__first
) ? __last
: __first
;
1766 _ForwardIterator __middle
= __first
;
1767 std::advance(__middle
, __len
/ 2);
1768 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1772 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1776 std::rotate(__begin
, __middle
, __end
);
1777 std::advance(__begin
, std::distance(__middle
, __end
));
1781 /// This is a helper function...
1782 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1785 __stable_partition_adaptive(_ForwardIterator __first
,
1786 _ForwardIterator __last
,
1787 _Predicate __pred
, _Distance __len
,
1789 _Distance __buffer_size
)
1791 if (__len
<= __buffer_size
)
1793 _ForwardIterator __result1
= __first
;
1794 _Pointer __result2
= __buffer
;
1795 for (; __first
!= __last
; ++__first
)
1796 if (__pred(*__first
))
1798 *__result1
= *__first
;
1803 *__result2
= *__first
;
1806 std::copy(__buffer
, __result2
, __result1
);
1811 _ForwardIterator __middle
= __first
;
1812 std::advance(__middle
, __len
/ 2);
1813 _ForwardIterator __begin
=
1814 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1815 __len
/ 2, __buffer
,
1817 _ForwardIterator __end
=
1818 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1820 __buffer
, __buffer_size
);
1821 std::rotate(__begin
, __middle
, __end
);
1822 std::advance(__begin
, std::distance(__middle
, __end
));
1828 * @brief Move elements for which a predicate is true to the beginning
1829 * of a sequence, preserving relative ordering.
1830 * @param first A forward iterator.
1831 * @param last A forward iterator.
1832 * @param pred A predicate functor.
1833 * @return An iterator @p middle such that @p pred(i) is true for each
1834 * iterator @p i in the range @p [first,middle) and false for each @p i
1835 * in the range @p [middle,last).
1837 * Performs the same function as @p partition() with the additional
1838 * guarantee that the relative ordering of elements in each group is
1839 * preserved, so any two elements @p x and @p y in the range
1840 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1841 * relative ordering after calling @p stable_partition().
1843 template<typename _ForwardIterator
, typename _Predicate
>
1845 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1848 // concept requirements
1849 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1851 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1852 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1853 __glibcxx_requires_valid_range(__first
, __last
);
1855 if (__first
== __last
)
1859 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1861 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1864 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1866 if (__buf
.size() > 0)
1868 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1869 _DistanceType(__buf
.requested_size()),
1871 _DistanceType(__buf
.size()));
1874 std::__inplace_stable_partition(__first
, __last
, __pred
,
1875 _DistanceType(__buf
.requested_size()));
1879 /// This is a helper function for the sort routines.
1880 template<typename _RandomAccessIterator
>
1882 __heap_select(_RandomAccessIterator __first
,
1883 _RandomAccessIterator __middle
,
1884 _RandomAccessIterator __last
)
1886 std::make_heap(__first
, __middle
);
1887 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1888 if (*__i
< *__first
)
1889 std::__pop_heap(__first
, __middle
, __i
);
1892 /// This is a helper function for the sort routines.
1893 template<typename _RandomAccessIterator
, typename _Compare
>
1895 __heap_select(_RandomAccessIterator __first
,
1896 _RandomAccessIterator __middle
,
1897 _RandomAccessIterator __last
, _Compare __comp
)
1899 std::make_heap(__first
, __middle
, __comp
);
1900 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1901 if (__comp(*__i
, *__first
))
1902 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1908 * @brief Copy the smallest elements of a sequence.
1909 * @param first An iterator.
1910 * @param last Another iterator.
1911 * @param result_first A random-access iterator.
1912 * @param result_last Another random-access iterator.
1913 * @return An iterator indicating the end of the resulting sequence.
1915 * Copies and sorts the smallest N values from the range @p [first,last)
1916 * to the range beginning at @p result_first, where the number of
1917 * elements to be copied, @p N, is the smaller of @p (last-first) and
1918 * @p (result_last-result_first).
1919 * After the sort if @p i and @j are iterators in the range
1920 * @p [result_first,result_first+N) such that @i precedes @j then
1921 * @p *j<*i is false.
1922 * The value returned is @p result_first+N.
1924 template<typename _InputIterator
, typename _RandomAccessIterator
>
1925 _RandomAccessIterator
1926 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1927 _RandomAccessIterator __result_first
,
1928 _RandomAccessIterator __result_last
)
1930 typedef typename iterator_traits
<_InputIterator
>::value_type
1932 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1934 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1937 // concept requirements
1938 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1939 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1941 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1943 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1944 __glibcxx_requires_valid_range(__first
, __last
);
1945 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1947 if (__result_first
== __result_last
)
1948 return __result_last
;
1949 _RandomAccessIterator __result_real_last
= __result_first
;
1950 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1952 *__result_real_last
= *__first
;
1953 ++__result_real_last
;
1956 std::make_heap(__result_first
, __result_real_last
);
1957 while (__first
!= __last
)
1959 if (*__first
< *__result_first
)
1960 std::__adjust_heap(__result_first
, _DistanceType(0),
1961 _DistanceType(__result_real_last
1963 _InputValueType(*__first
));
1966 std::sort_heap(__result_first
, __result_real_last
);
1967 return __result_real_last
;
1971 * @brief Copy the smallest elements of a sequence using a predicate for
1973 * @param first An input iterator.
1974 * @param last Another input iterator.
1975 * @param result_first A random-access iterator.
1976 * @param result_last Another random-access iterator.
1977 * @param comp A comparison functor.
1978 * @return An iterator indicating the end of the resulting sequence.
1980 * Copies and sorts the smallest N values from the range @p [first,last)
1981 * to the range beginning at @p result_first, where the number of
1982 * elements to be copied, @p N, is the smaller of @p (last-first) and
1983 * @p (result_last-result_first).
1984 * After the sort if @p i and @j are iterators in the range
1985 * @p [result_first,result_first+N) such that @i precedes @j then
1986 * @p comp(*j,*i) is false.
1987 * The value returned is @p result_first+N.
1989 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1990 _RandomAccessIterator
1991 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1992 _RandomAccessIterator __result_first
,
1993 _RandomAccessIterator __result_last
,
1996 typedef typename iterator_traits
<_InputIterator
>::value_type
1998 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2000 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2003 // concept requirements
2004 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2005 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2006 _RandomAccessIterator
>)
2007 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2009 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2010 _InputValueType
, _OutputValueType
>)
2011 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2012 _OutputValueType
, _OutputValueType
>)
2013 __glibcxx_requires_valid_range(__first
, __last
);
2014 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2016 if (__result_first
== __result_last
)
2017 return __result_last
;
2018 _RandomAccessIterator __result_real_last
= __result_first
;
2019 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2021 *__result_real_last
= *__first
;
2022 ++__result_real_last
;
2025 std::make_heap(__result_first
, __result_real_last
, __comp
);
2026 while (__first
!= __last
)
2028 if (__comp(*__first
, *__result_first
))
2029 std::__adjust_heap(__result_first
, _DistanceType(0),
2030 _DistanceType(__result_real_last
2032 _InputValueType(*__first
),
2036 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2037 return __result_real_last
;
2040 /// This is a helper function for the sort routine.
2041 template<typename _RandomAccessIterator
, typename _Tp
>
2043 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2045 _RandomAccessIterator __next
= __last
;
2047 while (__val
< *__next
)
2056 /// This is a helper function for the sort routine.
2057 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2059 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2062 _RandomAccessIterator __next
= __last
;
2064 while (__comp(__val
, *__next
))
2073 /// This is a helper function for the sort routine.
2074 template<typename _RandomAccessIterator
>
2076 __insertion_sort(_RandomAccessIterator __first
,
2077 _RandomAccessIterator __last
)
2079 if (__first
== __last
)
2082 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2084 typename iterator_traits
<_RandomAccessIterator
>::value_type
2086 if (__val
< *__first
)
2088 std::copy_backward(__first
, __i
, __i
+ 1);
2092 std::__unguarded_linear_insert(__i
, __val
);
2096 /// This is a helper function for the sort routine.
2097 template<typename _RandomAccessIterator
, typename _Compare
>
2099 __insertion_sort(_RandomAccessIterator __first
,
2100 _RandomAccessIterator __last
, _Compare __comp
)
2102 if (__first
== __last
) return;
2104 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2106 typename iterator_traits
<_RandomAccessIterator
>::value_type
2108 if (__comp(__val
, *__first
))
2110 std::copy_backward(__first
, __i
, __i
+ 1);
2114 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2118 /// This is a helper function for the sort routine.
2119 template<typename _RandomAccessIterator
>
2121 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2122 _RandomAccessIterator __last
)
2124 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2127 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2128 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2131 /// This is a helper function for the sort routine.
2132 template<typename _RandomAccessIterator
, typename _Compare
>
2134 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2135 _RandomAccessIterator __last
, _Compare __comp
)
2137 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2140 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2141 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2146 * This controls some aspect of the sort routines.
2148 enum { _S_threshold
= 16 };
2150 /// This is a helper function for the sort routine.
2151 template<typename _RandomAccessIterator
>
2153 __final_insertion_sort(_RandomAccessIterator __first
,
2154 _RandomAccessIterator __last
)
2156 if (__last
- __first
> int(_S_threshold
))
2158 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2159 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2162 std::__insertion_sort(__first
, __last
);
2165 /// This is a helper function for the sort routine.
2166 template<typename _RandomAccessIterator
, typename _Compare
>
2168 __final_insertion_sort(_RandomAccessIterator __first
,
2169 _RandomAccessIterator __last
, _Compare __comp
)
2171 if (__last
- __first
> int(_S_threshold
))
2173 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2174 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2178 std::__insertion_sort(__first
, __last
, __comp
);
2181 /// This is a helper function...
2182 template<typename _RandomAccessIterator
, typename _Tp
>
2183 _RandomAccessIterator
2184 __unguarded_partition(_RandomAccessIterator __first
,
2185 _RandomAccessIterator __last
, _Tp __pivot
)
2189 while (*__first
< __pivot
)
2192 while (__pivot
< *__last
)
2194 if (!(__first
< __last
))
2196 std::iter_swap(__first
, __last
);
2201 /// This is a helper function...
2202 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2203 _RandomAccessIterator
2204 __unguarded_partition(_RandomAccessIterator __first
,
2205 _RandomAccessIterator __last
,
2206 _Tp __pivot
, _Compare __comp
)
2210 while (__comp(*__first
, __pivot
))
2213 while (__comp(__pivot
, *__last
))
2215 if (!(__first
< __last
))
2217 std::iter_swap(__first
, __last
);
2222 /// This is a helper function for the sort routine.
2223 template<typename _RandomAccessIterator
, typename _Size
>
2225 __introsort_loop(_RandomAccessIterator __first
,
2226 _RandomAccessIterator __last
,
2227 _Size __depth_limit
)
2229 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2232 while (__last
- __first
> int(_S_threshold
))
2234 if (__depth_limit
== 0)
2236 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2240 _RandomAccessIterator __cut
=
2241 std::__unguarded_partition(__first
, __last
,
2242 _ValueType(std::__median(*__first
,
2249 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2254 /// This is a helper function for the sort routine.
2255 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2257 __introsort_loop(_RandomAccessIterator __first
,
2258 _RandomAccessIterator __last
,
2259 _Size __depth_limit
, _Compare __comp
)
2261 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2264 while (__last
- __first
> int(_S_threshold
))
2266 if (__depth_limit
== 0)
2268 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2272 _RandomAccessIterator __cut
=
2273 std::__unguarded_partition(__first
, __last
,
2274 _ValueType(std::__median(*__first
,
2282 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2287 /// This is a helper function for the sort routines. Precondition: __n > 0.
2288 template<typename _Size
>
2293 for (__k
= 0; __n
!= 0; __n
>>= 1)
2300 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2304 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2308 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2312 template<typename _RandomAccessIterator
, typename _Size
>
2314 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2315 _RandomAccessIterator __last
, _Size __depth_limit
)
2317 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2320 while (__last
- __first
> 3)
2322 if (__depth_limit
== 0)
2324 std::__heap_select(__first
, __nth
+ 1, __last
);
2326 // Place the nth largest element in its final position.
2327 std::iter_swap(__first
, __nth
);
2331 _RandomAccessIterator __cut
=
2332 std::__unguarded_partition(__first
, __last
,
2333 _ValueType(std::__median(*__first
,
2345 std::__insertion_sort(__first
, __last
);
2348 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2350 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2351 _RandomAccessIterator __last
, _Size __depth_limit
,
2354 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2357 while (__last
- __first
> 3)
2359 if (__depth_limit
== 0)
2361 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2362 // Place the nth largest element in its final position.
2363 std::iter_swap(__first
, __nth
);
2367 _RandomAccessIterator __cut
=
2368 std::__unguarded_partition(__first
, __last
,
2369 _ValueType(std::__median(*__first
,
2382 std::__insertion_sort(__first
, __last
, __comp
);
2388 * @brief Finds the first position in which @a val could be inserted
2389 * without changing the ordering.
2390 * @param first An iterator.
2391 * @param last Another iterator.
2392 * @param val The search term.
2393 * @return An iterator pointing to the first element "not less
2394 * than" @a val, or end() if every element is less than
2396 * @ingroup binarysearch
2398 template<typename _ForwardIterator
, typename _Tp
>
2400 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2403 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2405 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2408 // concept requirements
2409 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2410 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2411 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2413 _DistanceType __len
= std::distance(__first
, __last
);
2414 _DistanceType __half
;
2415 _ForwardIterator __middle
;
2419 __half
= __len
>> 1;
2421 std::advance(__middle
, __half
);
2422 if (*__middle
< __val
)
2426 __len
= __len
- __half
- 1;
2435 * @brief Finds the first position in which @a val could be inserted
2436 * without changing the ordering.
2437 * @param first An iterator.
2438 * @param last Another iterator.
2439 * @param val The search term.
2440 * @param comp A functor to use for comparisons.
2441 * @return An iterator pointing to the first element "not less than" @a val,
2442 * or end() if every element is less than @a val.
2443 * @ingroup binarysearch
2445 * The comparison function should have the same effects on ordering as
2446 * the function used for the initial sort.
2448 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2450 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2451 const _Tp
& __val
, _Compare __comp
)
2453 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2455 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2458 // concept requirements
2459 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2460 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2462 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2465 _DistanceType __len
= std::distance(__first
, __last
);
2466 _DistanceType __half
;
2467 _ForwardIterator __middle
;
2471 __half
= __len
>> 1;
2473 std::advance(__middle
, __half
);
2474 if (__comp(*__middle
, __val
))
2478 __len
= __len
- __half
- 1;
2487 * @brief Finds the last position in which @a val could be inserted
2488 * without changing the ordering.
2489 * @param first An iterator.
2490 * @param last Another iterator.
2491 * @param val The search term.
2492 * @return An iterator pointing to the first element greater than @a val,
2493 * or end() if no elements are greater than @a val.
2494 * @ingroup binarysearch
2496 template<typename _ForwardIterator
, typename _Tp
>
2498 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2501 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2503 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2506 // concept requirements
2507 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2508 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2509 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2511 _DistanceType __len
= std::distance(__first
, __last
);
2512 _DistanceType __half
;
2513 _ForwardIterator __middle
;
2517 __half
= __len
>> 1;
2519 std::advance(__middle
, __half
);
2520 if (__val
< *__middle
)
2526 __len
= __len
- __half
- 1;
2533 * @brief Finds the last position in which @a val could be inserted
2534 * without changing the ordering.
2535 * @param first An iterator.
2536 * @param last Another iterator.
2537 * @param val The search term.
2538 * @param comp A functor to use for comparisons.
2539 * @return An iterator pointing to the first element greater than @a val,
2540 * or end() if no elements are greater than @a val.
2541 * @ingroup binarysearch
2543 * The comparison function should have the same effects on ordering as
2544 * the function used for the initial sort.
2546 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2548 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2549 const _Tp
& __val
, _Compare __comp
)
2551 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2553 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2556 // concept requirements
2557 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2558 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2560 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2563 _DistanceType __len
= std::distance(__first
, __last
);
2564 _DistanceType __half
;
2565 _ForwardIterator __middle
;
2569 __half
= __len
>> 1;
2571 std::advance(__middle
, __half
);
2572 if (__comp(__val
, *__middle
))
2578 __len
= __len
- __half
- 1;
2585 * @brief Finds the largest subrange in which @a val could be inserted
2586 * at any place in it without changing the ordering.
2587 * @param first An iterator.
2588 * @param last Another iterator.
2589 * @param val The search term.
2590 * @return An pair of iterators defining the subrange.
2591 * @ingroup binarysearch
2593 * This is equivalent to
2595 * std::make_pair(lower_bound(first, last, val),
2596 * upper_bound(first, last, val))
2598 * but does not actually call those functions.
2600 template<typename _ForwardIterator
, typename _Tp
>
2601 pair
<_ForwardIterator
, _ForwardIterator
>
2602 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2605 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2607 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2610 // concept requirements
2611 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2612 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2613 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2614 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2615 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2617 _DistanceType __len
= std::distance(__first
, __last
);
2618 _DistanceType __half
;
2619 _ForwardIterator __middle
, __left
, __right
;
2623 __half
= __len
>> 1;
2625 std::advance(__middle
, __half
);
2626 if (*__middle
< __val
)
2630 __len
= __len
- __half
- 1;
2632 else if (__val
< *__middle
)
2636 __left
= std::lower_bound(__first
, __middle
, __val
);
2637 std::advance(__first
, __len
);
2638 __right
= std::upper_bound(++__middle
, __first
, __val
);
2639 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2642 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2646 * @brief Finds the largest subrange in which @a val could be inserted
2647 * at any place in it without changing the ordering.
2648 * @param first An iterator.
2649 * @param last Another iterator.
2650 * @param val The search term.
2651 * @param comp A functor to use for comparisons.
2652 * @return An pair of iterators defining the subrange.
2653 * @ingroup binarysearch
2655 * This is equivalent to
2657 * std::make_pair(lower_bound(first, last, val, comp),
2658 * upper_bound(first, last, val, comp))
2660 * but does not actually call those functions.
2662 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2663 pair
<_ForwardIterator
, _ForwardIterator
>
2664 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2668 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2670 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2673 // concept requirements
2674 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2675 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2677 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2679 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2681 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2684 _DistanceType __len
= std::distance(__first
, __last
);
2685 _DistanceType __half
;
2686 _ForwardIterator __middle
, __left
, __right
;
2690 __half
= __len
>> 1;
2692 std::advance(__middle
, __half
);
2693 if (__comp(*__middle
, __val
))
2697 __len
= __len
- __half
- 1;
2699 else if (__comp(__val
, *__middle
))
2703 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2704 std::advance(__first
, __len
);
2705 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2706 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2709 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2713 * @brief Determines whether an element exists in a range.
2714 * @param first An iterator.
2715 * @param last Another iterator.
2716 * @param val The search term.
2717 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2718 * @ingroup binarysearch
2720 * Note that this does not actually return an iterator to @a val. For
2721 * that, use std::find or a container's specialized find member functions.
2723 template<typename _ForwardIterator
, typename _Tp
>
2725 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2728 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2731 // concept requirements
2732 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2733 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2734 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2735 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2737 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2738 return __i
!= __last
&& !(__val
< *__i
);
2742 * @brief Determines whether an element exists in a range.
2743 * @param first An iterator.
2744 * @param last Another iterator.
2745 * @param val The search term.
2746 * @param comp A functor to use for comparisons.
2747 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2748 * @ingroup binarysearch
2750 * Note that this does not actually return an iterator to @a val. For
2751 * that, use std::find or a container's specialized find member functions.
2753 * The comparison function should have the same effects on ordering as
2754 * the function used for the initial sort.
2756 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2758 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2759 const _Tp
& __val
, _Compare __comp
)
2761 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2764 // concept requirements
2765 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2766 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2768 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2770 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2773 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2774 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2779 /// This is a helper function for the merge routines.
2780 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2781 typename _BidirectionalIterator3
>
2782 _BidirectionalIterator3
2783 __merge_backward(_BidirectionalIterator1 __first1
,
2784 _BidirectionalIterator1 __last1
,
2785 _BidirectionalIterator2 __first2
,
2786 _BidirectionalIterator2 __last2
,
2787 _BidirectionalIterator3 __result
)
2789 if (__first1
== __last1
)
2790 return std::copy_backward(__first2
, __last2
, __result
);
2791 if (__first2
== __last2
)
2792 return std::copy_backward(__first1
, __last1
, __result
);
2797 if (*__last2
< *__last1
)
2799 *--__result
= *__last1
;
2800 if (__first1
== __last1
)
2801 return std::copy_backward(__first2
, ++__last2
, __result
);
2806 *--__result
= *__last2
;
2807 if (__first2
== __last2
)
2808 return std::copy_backward(__first1
, ++__last1
, __result
);
2814 /// This is a helper function for the merge routines.
2815 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2816 typename _BidirectionalIterator3
, typename _Compare
>
2817 _BidirectionalIterator3
2818 __merge_backward(_BidirectionalIterator1 __first1
,
2819 _BidirectionalIterator1 __last1
,
2820 _BidirectionalIterator2 __first2
,
2821 _BidirectionalIterator2 __last2
,
2822 _BidirectionalIterator3 __result
,
2825 if (__first1
== __last1
)
2826 return std::copy_backward(__first2
, __last2
, __result
);
2827 if (__first2
== __last2
)
2828 return std::copy_backward(__first1
, __last1
, __result
);
2833 if (__comp(*__last2
, *__last1
))
2835 *--__result
= *__last1
;
2836 if (__first1
== __last1
)
2837 return std::copy_backward(__first2
, ++__last2
, __result
);
2842 *--__result
= *__last2
;
2843 if (__first2
== __last2
)
2844 return std::copy_backward(__first1
, ++__last1
, __result
);
2850 /// This is a helper function for the merge routines.
2851 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2853 _BidirectionalIterator1
2854 __rotate_adaptive(_BidirectionalIterator1 __first
,
2855 _BidirectionalIterator1 __middle
,
2856 _BidirectionalIterator1 __last
,
2857 _Distance __len1
, _Distance __len2
,
2858 _BidirectionalIterator2 __buffer
,
2859 _Distance __buffer_size
)
2861 _BidirectionalIterator2 __buffer_end
;
2862 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2864 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2865 std::copy_backward(__first
, __middle
, __last
);
2866 return std::copy(__buffer
, __buffer_end
, __first
);
2868 else if (__len1
<= __buffer_size
)
2870 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2871 std::copy(__middle
, __last
, __first
);
2872 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2876 std::rotate(__first
, __middle
, __last
);
2877 std::advance(__first
, std::distance(__middle
, __last
));
2882 /// This is a helper function for the merge routines.
2883 template<typename _BidirectionalIterator
, typename _Distance
,
2886 __merge_adaptive(_BidirectionalIterator __first
,
2887 _BidirectionalIterator __middle
,
2888 _BidirectionalIterator __last
,
2889 _Distance __len1
, _Distance __len2
,
2890 _Pointer __buffer
, _Distance __buffer_size
)
2892 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2894 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2895 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2898 else if (__len2
<= __buffer_size
)
2900 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2901 std::__merge_backward(__first
, __middle
, __buffer
,
2902 __buffer_end
, __last
);
2906 _BidirectionalIterator __first_cut
= __first
;
2907 _BidirectionalIterator __second_cut
= __middle
;
2908 _Distance __len11
= 0;
2909 _Distance __len22
= 0;
2910 if (__len1
> __len2
)
2912 __len11
= __len1
/ 2;
2913 std::advance(__first_cut
, __len11
);
2914 __second_cut
= std::lower_bound(__middle
, __last
,
2916 __len22
= std::distance(__middle
, __second_cut
);
2920 __len22
= __len2
/ 2;
2921 std::advance(__second_cut
, __len22
);
2922 __first_cut
= std::upper_bound(__first
, __middle
,
2924 __len11
= std::distance(__first
, __first_cut
);
2926 _BidirectionalIterator __new_middle
=
2927 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2928 __len1
- __len11
, __len22
, __buffer
,
2930 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2931 __len22
, __buffer
, __buffer_size
);
2932 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2934 __len2
- __len22
, __buffer
, __buffer_size
);
2938 /// This is a helper function for the merge routines.
2939 template<typename _BidirectionalIterator
, typename _Distance
,
2940 typename _Pointer
, typename _Compare
>
2942 __merge_adaptive(_BidirectionalIterator __first
,
2943 _BidirectionalIterator __middle
,
2944 _BidirectionalIterator __last
,
2945 _Distance __len1
, _Distance __len2
,
2946 _Pointer __buffer
, _Distance __buffer_size
,
2949 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2951 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2952 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2955 else if (__len2
<= __buffer_size
)
2957 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2958 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2963 _BidirectionalIterator __first_cut
= __first
;
2964 _BidirectionalIterator __second_cut
= __middle
;
2965 _Distance __len11
= 0;
2966 _Distance __len22
= 0;
2967 if (__len1
> __len2
)
2969 __len11
= __len1
/ 2;
2970 std::advance(__first_cut
, __len11
);
2971 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2973 __len22
= std::distance(__middle
, __second_cut
);
2977 __len22
= __len2
/ 2;
2978 std::advance(__second_cut
, __len22
);
2979 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2981 __len11
= std::distance(__first
, __first_cut
);
2983 _BidirectionalIterator __new_middle
=
2984 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2985 __len1
- __len11
, __len22
, __buffer
,
2987 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2988 __len22
, __buffer
, __buffer_size
, __comp
);
2989 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2991 __len2
- __len22
, __buffer
,
2992 __buffer_size
, __comp
);
2996 /// This is a helper function for the merge routines.
2997 template<typename _BidirectionalIterator
, typename _Distance
>
2999 __merge_without_buffer(_BidirectionalIterator __first
,
3000 _BidirectionalIterator __middle
,
3001 _BidirectionalIterator __last
,
3002 _Distance __len1
, _Distance __len2
)
3004 if (__len1
== 0 || __len2
== 0)
3006 if (__len1
+ __len2
== 2)
3008 if (*__middle
< *__first
)
3009 std::iter_swap(__first
, __middle
);
3012 _BidirectionalIterator __first_cut
= __first
;
3013 _BidirectionalIterator __second_cut
= __middle
;
3014 _Distance __len11
= 0;
3015 _Distance __len22
= 0;
3016 if (__len1
> __len2
)
3018 __len11
= __len1
/ 2;
3019 std::advance(__first_cut
, __len11
);
3020 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3021 __len22
= std::distance(__middle
, __second_cut
);
3025 __len22
= __len2
/ 2;
3026 std::advance(__second_cut
, __len22
);
3027 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3028 __len11
= std::distance(__first
, __first_cut
);
3030 std::rotate(__first_cut
, __middle
, __second_cut
);
3031 _BidirectionalIterator __new_middle
= __first_cut
;
3032 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3033 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3035 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3036 __len1
- __len11
, __len2
- __len22
);
3039 /// This is a helper function for the merge routines.
3040 template<typename _BidirectionalIterator
, typename _Distance
,
3043 __merge_without_buffer(_BidirectionalIterator __first
,
3044 _BidirectionalIterator __middle
,
3045 _BidirectionalIterator __last
,
3046 _Distance __len1
, _Distance __len2
,
3049 if (__len1
== 0 || __len2
== 0)
3051 if (__len1
+ __len2
== 2)
3053 if (__comp(*__middle
, *__first
))
3054 std::iter_swap(__first
, __middle
);
3057 _BidirectionalIterator __first_cut
= __first
;
3058 _BidirectionalIterator __second_cut
= __middle
;
3059 _Distance __len11
= 0;
3060 _Distance __len22
= 0;
3061 if (__len1
> __len2
)
3063 __len11
= __len1
/ 2;
3064 std::advance(__first_cut
, __len11
);
3065 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3067 __len22
= std::distance(__middle
, __second_cut
);
3071 __len22
= __len2
/ 2;
3072 std::advance(__second_cut
, __len22
);
3073 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3075 __len11
= std::distance(__first
, __first_cut
);
3077 std::rotate(__first_cut
, __middle
, __second_cut
);
3078 _BidirectionalIterator __new_middle
= __first_cut
;
3079 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3080 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3081 __len11
, __len22
, __comp
);
3082 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3083 __len1
- __len11
, __len2
- __len22
, __comp
);
3087 * @brief Merges two sorted ranges in place.
3088 * @param first An iterator.
3089 * @param middle Another iterator.
3090 * @param last Another iterator.
3093 * Merges two sorted and consecutive ranges, [first,middle) and
3094 * [middle,last), and puts the result in [first,last). The output will
3095 * be sorted. The sort is @e stable, that is, for equivalent
3096 * elements in the two ranges, elements from the first range will always
3097 * come before elements from the second.
3099 * If enough additional memory is available, this takes (last-first)-1
3100 * comparisons. Otherwise an NlogN algorithm is used, where N is
3101 * distance(first,last).
3103 template<typename _BidirectionalIterator
>
3105 inplace_merge(_BidirectionalIterator __first
,
3106 _BidirectionalIterator __middle
,
3107 _BidirectionalIterator __last
)
3109 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3111 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3114 // concept requirements
3115 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3116 _BidirectionalIterator
>)
3117 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3118 __glibcxx_requires_sorted(__first
, __middle
);
3119 __glibcxx_requires_sorted(__middle
, __last
);
3121 if (__first
== __middle
|| __middle
== __last
)
3124 _DistanceType __len1
= std::distance(__first
, __middle
);
3125 _DistanceType __len2
= std::distance(__middle
, __last
);
3127 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3129 if (__buf
.begin() == 0)
3130 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3132 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3133 __buf
.begin(), _DistanceType(__buf
.size()));
3137 * @brief Merges two sorted ranges in place.
3138 * @param first An iterator.
3139 * @param middle Another iterator.
3140 * @param last Another iterator.
3141 * @param comp A functor to use for comparisons.
3144 * Merges two sorted and consecutive ranges, [first,middle) and
3145 * [middle,last), and puts the result in [first,last). The output will
3146 * be sorted. The sort is @e stable, that is, for equivalent
3147 * elements in the two ranges, elements from the first range will always
3148 * come before elements from the second.
3150 * If enough additional memory is available, this takes (last-first)-1
3151 * comparisons. Otherwise an NlogN algorithm is used, where N is
3152 * distance(first,last).
3154 * The comparison function should have the same effects on ordering as
3155 * the function used for the initial sort.
3157 template<typename _BidirectionalIterator
, typename _Compare
>
3159 inplace_merge(_BidirectionalIterator __first
,
3160 _BidirectionalIterator __middle
,
3161 _BidirectionalIterator __last
,
3164 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3166 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3169 // concept requirements
3170 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3171 _BidirectionalIterator
>)
3172 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3173 _ValueType
, _ValueType
>)
3174 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3175 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3177 if (__first
== __middle
|| __middle
== __last
)
3180 const _DistanceType __len1
= std::distance(__first
, __middle
);
3181 const _DistanceType __len2
= std::distance(__middle
, __last
);
3183 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3185 if (__buf
.begin() == 0)
3186 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3189 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3190 __buf
.begin(), _DistanceType(__buf
.size()),
3194 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3197 __merge_sort_loop(_RandomAccessIterator1 __first
,
3198 _RandomAccessIterator1 __last
,
3199 _RandomAccessIterator2 __result
,
3200 _Distance __step_size
)
3202 const _Distance __two_step
= 2 * __step_size
;
3204 while (__last
- __first
>= __two_step
)
3206 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3207 __first
+ __step_size
,
3208 __first
+ __two_step
,
3210 __first
+= __two_step
;
3213 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3214 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3215 __first
+ __step_size
, __last
,
3219 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3220 typename _Distance
, typename _Compare
>
3222 __merge_sort_loop(_RandomAccessIterator1 __first
,
3223 _RandomAccessIterator1 __last
,
3224 _RandomAccessIterator2 __result
, _Distance __step_size
,
3227 const _Distance __two_step
= 2 * __step_size
;
3229 while (__last
- __first
>= __two_step
)
3231 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3232 __first
+ __step_size
, __first
+ __two_step
,
3235 __first
+= __two_step
;
3237 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3239 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3240 __first
+ __step_size
, __last
, __result
, __comp
);
3243 template<typename _RandomAccessIterator
, typename _Distance
>
3245 __chunk_insertion_sort(_RandomAccessIterator __first
,
3246 _RandomAccessIterator __last
,
3247 _Distance __chunk_size
)
3249 while (__last
- __first
>= __chunk_size
)
3251 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3252 __first
+= __chunk_size
;
3254 std::__insertion_sort(__first
, __last
);
3257 template<typename _RandomAccessIterator
, typename _Distance
,
3260 __chunk_insertion_sort(_RandomAccessIterator __first
,
3261 _RandomAccessIterator __last
,
3262 _Distance __chunk_size
, _Compare __comp
)
3264 while (__last
- __first
>= __chunk_size
)
3266 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3267 __first
+= __chunk_size
;
3269 std::__insertion_sort(__first
, __last
, __comp
);
3272 enum { _S_chunk_size
= 7 };
3274 template<typename _RandomAccessIterator
, typename _Pointer
>
3276 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3277 _RandomAccessIterator __last
,
3280 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3283 const _Distance __len
= __last
- __first
;
3284 const _Pointer __buffer_last
= __buffer
+ __len
;
3286 _Distance __step_size
= _S_chunk_size
;
3287 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3289 while (__step_size
< __len
)
3291 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3293 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3298 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3300 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3301 _RandomAccessIterator __last
,
3302 _Pointer __buffer
, _Compare __comp
)
3304 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3307 const _Distance __len
= __last
- __first
;
3308 const _Pointer __buffer_last
= __buffer
+ __len
;
3310 _Distance __step_size
= _S_chunk_size
;
3311 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3313 while (__step_size
< __len
)
3315 std::__merge_sort_loop(__first
, __last
, __buffer
,
3316 __step_size
, __comp
);
3318 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3319 __step_size
, __comp
);
3324 template<typename _RandomAccessIterator
, typename _Pointer
,
3327 __stable_sort_adaptive(_RandomAccessIterator __first
,
3328 _RandomAccessIterator __last
,
3329 _Pointer __buffer
, _Distance __buffer_size
)
3331 const _Distance __len
= (__last
- __first
+ 1) / 2;
3332 const _RandomAccessIterator __middle
= __first
+ __len
;
3333 if (__len
> __buffer_size
)
3335 std::__stable_sort_adaptive(__first
, __middle
,
3336 __buffer
, __buffer_size
);
3337 std::__stable_sort_adaptive(__middle
, __last
,
3338 __buffer
, __buffer_size
);
3342 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3343 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3345 std::__merge_adaptive(__first
, __middle
, __last
,
3346 _Distance(__middle
- __first
),
3347 _Distance(__last
- __middle
),
3348 __buffer
, __buffer_size
);
3351 template<typename _RandomAccessIterator
, typename _Pointer
,
3352 typename _Distance
, typename _Compare
>
3354 __stable_sort_adaptive(_RandomAccessIterator __first
,
3355 _RandomAccessIterator __last
,
3356 _Pointer __buffer
, _Distance __buffer_size
,
3359 const _Distance __len
= (__last
- __first
+ 1) / 2;
3360 const _RandomAccessIterator __middle
= __first
+ __len
;
3361 if (__len
> __buffer_size
)
3363 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3364 __buffer_size
, __comp
);
3365 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3366 __buffer_size
, __comp
);
3370 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3371 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3373 std::__merge_adaptive(__first
, __middle
, __last
,
3374 _Distance(__middle
- __first
),
3375 _Distance(__last
- __middle
),
3376 __buffer
, __buffer_size
,
3380 /// This is a helper function for the stable sorting routines.
3381 template<typename _RandomAccessIterator
>
3383 __inplace_stable_sort(_RandomAccessIterator __first
,
3384 _RandomAccessIterator __last
)
3386 if (__last
- __first
< 15)
3388 std::__insertion_sort(__first
, __last
);
3391 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3392 std::__inplace_stable_sort(__first
, __middle
);
3393 std::__inplace_stable_sort(__middle
, __last
);
3394 std::__merge_without_buffer(__first
, __middle
, __last
,
3399 /// This is a helper function for the stable sorting routines.
3400 template<typename _RandomAccessIterator
, typename _Compare
>
3402 __inplace_stable_sort(_RandomAccessIterator __first
,
3403 _RandomAccessIterator __last
, _Compare __comp
)
3405 if (__last
- __first
< 15)
3407 std::__insertion_sort(__first
, __last
, __comp
);
3410 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3411 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3412 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3413 std::__merge_without_buffer(__first
, __middle
, __last
,
3421 // Set algorithms: includes, set_union, set_intersection, set_difference,
3422 // set_symmetric_difference. All of these algorithms have the precondition
3423 // that their input ranges are sorted and the postcondition that their output
3424 // ranges are sorted.
3427 * @brief Determines whether all elements of a sequence exists in a range.
3428 * @param first1 Start of search range.
3429 * @param last1 End of search range.
3430 * @param first2 Start of sequence
3431 * @param last2 End of sequence.
3432 * @return True if each element in [first2,last2) is contained in order
3433 * within [first1,last1). False otherwise.
3434 * @ingroup setoperations
3436 * This operation expects both [first1,last1) and [first2,last2) to be
3437 * sorted. Searches for the presence of each element in [first2,last2)
3438 * within [first1,last1). The iterators over each range only move forward,
3439 * so this is a linear algorithm. If an element in [first2,last2) is not
3440 * found before the search iterator reaches @a last2, false is returned.
3442 template<typename _InputIterator1
, typename _InputIterator2
>
3444 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3445 _InputIterator2 __first2
, _InputIterator2 __last2
)
3447 typedef typename iterator_traits
<_InputIterator1
>::value_type
3449 typedef typename iterator_traits
<_InputIterator2
>::value_type
3452 // concept requirements
3453 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3454 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3455 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3456 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3457 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3458 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3460 while (__first1
!= __last1
&& __first2
!= __last2
)
3461 if (*__first2
< *__first1
)
3463 else if(*__first1
< *__first2
)
3466 ++__first1
, ++__first2
;
3468 return __first2
== __last2
;
3472 * @brief Determines whether all elements of a sequence exists in a range
3474 * @param first1 Start of search range.
3475 * @param last1 End of search range.
3476 * @param first2 Start of sequence
3477 * @param last2 End of sequence.
3478 * @param comp Comparison function to use.
3479 * @return True if each element in [first2,last2) is contained in order
3480 * within [first1,last1) according to comp. False otherwise.
3481 * @ingroup setoperations
3483 * This operation expects both [first1,last1) and [first2,last2) to be
3484 * sorted. Searches for the presence of each element in [first2,last2)
3485 * within [first1,last1), using comp to decide. The iterators over each
3486 * range only move forward, so this is a linear algorithm. If an element
3487 * in [first2,last2) is not found before the search iterator reaches @a
3488 * last2, false is returned.
3490 template<typename _InputIterator1
, typename _InputIterator2
,
3493 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3494 _InputIterator2 __first2
, _InputIterator2 __last2
,
3497 typedef typename iterator_traits
<_InputIterator1
>::value_type
3499 typedef typename iterator_traits
<_InputIterator2
>::value_type
3502 // concept requirements
3503 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3504 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3505 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3506 _ValueType1
, _ValueType2
>)
3507 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3508 _ValueType2
, _ValueType1
>)
3509 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3510 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3512 while (__first1
!= __last1
&& __first2
!= __last2
)
3513 if (__comp(*__first2
, *__first1
))
3515 else if(__comp(*__first1
, *__first2
))
3518 ++__first1
, ++__first2
;
3520 return __first2
== __last2
;
3529 // set_symmetric_difference
3534 * @brief Permute range into the next "dictionary" ordering.
3535 * @param first Start of range.
3536 * @param last End of range.
3537 * @return False if wrapped to first permutation, true otherwise.
3539 * Treats all permutations of the range as a set of "dictionary" sorted
3540 * sequences. Permutes the current sequence into the next one of this set.
3541 * Returns true if there are more sequences to generate. If the sequence
3542 * is the largest of the set, the smallest is generated and false returned.
3544 template<typename _BidirectionalIterator
>
3546 next_permutation(_BidirectionalIterator __first
,
3547 _BidirectionalIterator __last
)
3549 // concept requirements
3550 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3551 _BidirectionalIterator
>)
3552 __glibcxx_function_requires(_LessThanComparableConcept
<
3553 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3554 __glibcxx_requires_valid_range(__first
, __last
);
3556 if (__first
== __last
)
3558 _BidirectionalIterator __i
= __first
;
3567 _BidirectionalIterator __ii
= __i
;
3571 _BidirectionalIterator __j
= __last
;
3572 while (!(*__i
< *--__j
))
3574 std::iter_swap(__i
, __j
);
3575 std::reverse(__ii
, __last
);
3580 std::reverse(__first
, __last
);
3587 * @brief Permute range into the next "dictionary" ordering using
3588 * comparison functor.
3589 * @param first Start of range.
3590 * @param last End of range.
3591 * @param comp A comparison functor.
3592 * @return False if wrapped to first permutation, true otherwise.
3594 * Treats all permutations of the range [first,last) as a set of
3595 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3596 * sequence into the next one of this set. Returns true if there are more
3597 * sequences to generate. If the sequence is the largest of the set, the
3598 * smallest is generated and false returned.
3600 template<typename _BidirectionalIterator
, typename _Compare
>
3602 next_permutation(_BidirectionalIterator __first
,
3603 _BidirectionalIterator __last
, _Compare __comp
)
3605 // concept requirements
3606 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3607 _BidirectionalIterator
>)
3608 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3609 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3610 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3611 __glibcxx_requires_valid_range(__first
, __last
);
3613 if (__first
== __last
)
3615 _BidirectionalIterator __i
= __first
;
3624 _BidirectionalIterator __ii
= __i
;
3626 if (__comp(*__i
, *__ii
))
3628 _BidirectionalIterator __j
= __last
;
3629 while (!bool(__comp(*__i
, *--__j
)))
3631 std::iter_swap(__i
, __j
);
3632 std::reverse(__ii
, __last
);
3637 std::reverse(__first
, __last
);
3644 * @brief Permute range into the previous "dictionary" ordering.
3645 * @param first Start of range.
3646 * @param last End of range.
3647 * @return False if wrapped to last permutation, true otherwise.
3649 * Treats all permutations of the range as a set of "dictionary" sorted
3650 * sequences. Permutes the current sequence into the previous one of this
3651 * set. Returns true if there are more sequences to generate. If the
3652 * sequence is the smallest of the set, the largest is generated and false
3655 template<typename _BidirectionalIterator
>
3657 prev_permutation(_BidirectionalIterator __first
,
3658 _BidirectionalIterator __last
)
3660 // concept requirements
3661 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3662 _BidirectionalIterator
>)
3663 __glibcxx_function_requires(_LessThanComparableConcept
<
3664 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3665 __glibcxx_requires_valid_range(__first
, __last
);
3667 if (__first
== __last
)
3669 _BidirectionalIterator __i
= __first
;
3678 _BidirectionalIterator __ii
= __i
;
3682 _BidirectionalIterator __j
= __last
;
3683 while (!(*--__j
< *__i
))
3685 std::iter_swap(__i
, __j
);
3686 std::reverse(__ii
, __last
);
3691 std::reverse(__first
, __last
);
3698 * @brief Permute range into the previous "dictionary" ordering using
3699 * comparison functor.
3700 * @param first Start of range.
3701 * @param last End of range.
3702 * @param comp A comparison functor.
3703 * @return False if wrapped to last permutation, true otherwise.
3705 * Treats all permutations of the range [first,last) as a set of
3706 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3707 * sequence into the previous one of this set. Returns true if there are
3708 * more sequences to generate. If the sequence is the smallest of the set,
3709 * the largest is generated and false returned.
3711 template<typename _BidirectionalIterator
, typename _Compare
>
3713 prev_permutation(_BidirectionalIterator __first
,
3714 _BidirectionalIterator __last
, _Compare __comp
)
3716 // concept requirements
3717 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3718 _BidirectionalIterator
>)
3719 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3720 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3721 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3722 __glibcxx_requires_valid_range(__first
, __last
);
3724 if (__first
== __last
)
3726 _BidirectionalIterator __i
= __first
;
3735 _BidirectionalIterator __ii
= __i
;
3737 if (__comp(*__ii
, *__i
))
3739 _BidirectionalIterator __j
= __last
;
3740 while (!bool(__comp(*--__j
, *__i
)))
3742 std::iter_swap(__i
, __j
);
3743 std::reverse(__ii
, __last
);
3748 std::reverse(__first
, __last
);
3758 * @brief Copy a sequence, replacing each element of one value with another
3760 * @param first An input iterator.
3761 * @param last An input iterator.
3762 * @param result An output iterator.
3763 * @param old_value The value to be replaced.
3764 * @param new_value The replacement value.
3765 * @return The end of the output sequence, @p result+(last-first).
3767 * Copies each element in the input range @p [first,last) to the
3768 * output range @p [result,result+(last-first)) replacing elements
3769 * equal to @p old_value with @p new_value.
3771 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3773 replace_copy(_InputIterator __first
, _InputIterator __last
,
3774 _OutputIterator __result
,
3775 const _Tp
& __old_value
, const _Tp
& __new_value
)
3777 // concept requirements
3778 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3779 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3780 typename iterator_traits
<_InputIterator
>::value_type
>)
3781 __glibcxx_function_requires(_EqualOpConcept
<
3782 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3783 __glibcxx_requires_valid_range(__first
, __last
);
3785 for (; __first
!= __last
; ++__first
, ++__result
)
3786 if (*__first
== __old_value
)
3787 *__result
= __new_value
;
3789 *__result
= *__first
;
3794 * @brief Copy a sequence, replacing each value for which a predicate
3795 * returns true with another value.
3796 * @param first An input iterator.
3797 * @param last An input iterator.
3798 * @param result An output iterator.
3799 * @param pred A predicate.
3800 * @param new_value The replacement value.
3801 * @return The end of the output sequence, @p result+(last-first).
3803 * Copies each element in the range @p [first,last) to the range
3804 * @p [result,result+(last-first)) replacing elements for which
3805 * @p pred returns true with @p new_value.
3807 template<typename _InputIterator
, typename _OutputIterator
,
3808 typename _Predicate
, typename _Tp
>
3810 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3811 _OutputIterator __result
,
3812 _Predicate __pred
, const _Tp
& __new_value
)
3814 // concept requirements
3815 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3816 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3817 typename iterator_traits
<_InputIterator
>::value_type
>)
3818 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3819 typename iterator_traits
<_InputIterator
>::value_type
>)
3820 __glibcxx_requires_valid_range(__first
, __last
);
3822 for (; __first
!= __last
; ++__first
, ++__result
)
3823 if (__pred(*__first
))
3824 *__result
= __new_value
;
3826 *__result
= *__first
;
3830 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3832 * @brief Determines whether the elements of a sequence are sorted.
3833 * @param first An iterator.
3834 * @param last Another iterator.
3835 * @return True if the elements are sorted, false otherwise.
3837 template<typename _ForwardIterator
>
3839 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3840 { return std::is_sorted_until(__first
, __last
) == __last
; }
3843 * @brief Determines whether the elements of a sequence are sorted
3844 * according to a comparison functor.
3845 * @param first An iterator.
3846 * @param last Another iterator.
3847 * @param comp A comparison functor.
3848 * @return True if the elements are sorted, false otherwise.
3850 template<typename _ForwardIterator
, typename _Compare
>
3852 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3854 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3857 * @brief Determines the end of a sorted sequence.
3858 * @param first An iterator.
3859 * @param last Another iterator.
3860 * @return An iterator pointing to the last iterator i in [first, last)
3861 * for which the range [first, i) is sorted.
3863 template<typename _ForwardIterator
>
3865 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3867 // concept requirements
3868 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3869 __glibcxx_function_requires(_LessThanComparableConcept
<
3870 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3871 __glibcxx_requires_valid_range(__first
, __last
);
3873 if (__first
== __last
)
3876 _ForwardIterator __next
= __first
;
3877 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3878 if (*__next
< *__first
)
3884 * @brief Determines the end of a sorted sequence using comparison functor.
3885 * @param first An iterator.
3886 * @param last Another iterator.
3887 * @param comp A comparison functor.
3888 * @return An iterator pointing to the last iterator i in [first, last)
3889 * for which the range [first, i) is sorted.
3891 template<typename _ForwardIterator
, typename _Compare
>
3893 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3896 // concept requirements
3897 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3898 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3899 typename iterator_traits
<_ForwardIterator
>::value_type
,
3900 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3901 __glibcxx_requires_valid_range(__first
, __last
);
3903 if (__first
== __last
)
3906 _ForwardIterator __next
= __first
;
3907 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3908 if (__comp(*__next
, *__first
))
3914 * @brief Determines min and max at once as an ordered pair.
3915 * @param a A thing of arbitrary type.
3916 * @param b Another thing of arbitrary type.
3917 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3919 template<typename _Tp
>
3920 inline pair
<const _Tp
&, const _Tp
&>
3921 minmax(const _Tp
& __a
, const _Tp
& __b
)
3923 // concept requirements
3924 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3926 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3927 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3931 * @brief Determines min and max at once as an ordered pair.
3932 * @param a A thing of arbitrary type.
3933 * @param b Another thing of arbitrary type.
3934 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3935 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3937 template<typename _Tp
, typename _Compare
>
3938 inline pair
<const _Tp
&, const _Tp
&>
3939 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3941 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3942 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3946 * @brief Return a pair of iterators pointing to the minimum and maximum
3947 * elements in a range.
3948 * @param first Start of range.
3949 * @param last End of range.
3950 * @return make_pair(m, M), where m is the first iterator i in
3951 * [first, last) such that no other element in the range is
3952 * smaller, and where M is the last iterator i in [first, last)
3953 * such that no other element in the range is larger.
3955 template<typename _ForwardIterator
>
3956 pair
<_ForwardIterator
, _ForwardIterator
>
3957 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3959 // concept requirements
3960 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3961 __glibcxx_function_requires(_LessThanComparableConcept
<
3962 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3963 __glibcxx_requires_valid_range(__first
, __last
);
3965 _ForwardIterator __next
= __first
;
3966 if (__first
== __last
3967 || ++__next
== __last
)
3968 return std::make_pair(__first
, __first
);
3970 _ForwardIterator __min
, __max
;
3971 if (*__next
< *__first
)
3985 while (__first
!= __last
)
3988 if (++__next
== __last
)
3990 if (*__first
< *__min
)
3992 else if (!(*__first
< *__max
))
3997 if (*__next
< *__first
)
3999 if (*__next
< *__min
)
4001 if (!(*__first
< *__max
))
4006 if (*__first
< *__min
)
4008 if (!(*__next
< *__max
))
4016 return std::make_pair(__min
, __max
);
4020 * @brief Return a pair of iterators pointing to the minimum and maximum
4021 * elements in a range.
4022 * @param first Start of range.
4023 * @param last End of range.
4024 * @param comp Comparison functor.
4025 * @return make_pair(m, M), where m is the first iterator i in
4026 * [first, last) such that no other element in the range is
4027 * smaller, and where M is the last iterator i in [first, last)
4028 * such that no other element in the range is larger.
4030 template<typename _ForwardIterator
, typename _Compare
>
4031 pair
<_ForwardIterator
, _ForwardIterator
>
4032 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4035 // concept requirements
4036 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4037 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4038 typename iterator_traits
<_ForwardIterator
>::value_type
,
4039 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4040 __glibcxx_requires_valid_range(__first
, __last
);
4042 _ForwardIterator __next
= __first
;
4043 if (__first
== __last
4044 || ++__next
== __last
)
4045 return std::make_pair(__first
, __first
);
4047 _ForwardIterator __min
, __max
;
4048 if (__comp(*__next
, *__first
))
4062 while (__first
!= __last
)
4065 if (++__next
== __last
)
4067 if (__comp(*__first
, *__min
))
4069 else if (!__comp(*__first
, *__max
))
4074 if (__comp(*__next
, *__first
))
4076 if (__comp(*__next
, *__min
))
4078 if (!__comp(*__first
, *__max
))
4083 if (__comp(*__first
, *__min
))
4085 if (!__comp(*__next
, *__max
))
4093 return std::make_pair(__min
, __max
);
4095 #endif // __GXX_EXPERIMENTAL_CXX0X__
4097 _GLIBCXX_END_NAMESPACE
4099 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4102 * @brief Apply a function to every element of a sequence.
4103 * @param first An input iterator.
4104 * @param last An input iterator.
4105 * @param f A unary function object.
4108 * Applies the function object @p f to each element in the range
4109 * @p [first,last). @p f must not modify the order of the sequence.
4110 * If @p f has a return value it is ignored.
4112 template<typename _InputIterator
, typename _Function
>
4114 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4116 // concept requirements
4117 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4118 __glibcxx_requires_valid_range(__first
, __last
);
4119 for (; __first
!= __last
; ++__first
)
4125 * @brief Find the first occurrence of a value in a sequence.
4126 * @param first An input iterator.
4127 * @param last An input iterator.
4128 * @param val The value to find.
4129 * @return The first iterator @c i in the range @p [first,last)
4130 * such that @c *i == @p val, or @p last if no such iterator exists.
4132 template<typename _InputIterator
, typename _Tp
>
4133 inline _InputIterator
4134 find(_InputIterator __first
, _InputIterator __last
,
4137 // concept requirements
4138 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4139 __glibcxx_function_requires(_EqualOpConcept
<
4140 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4141 __glibcxx_requires_valid_range(__first
, __last
);
4142 return std::__find(__first
, __last
, __val
,
4143 std::__iterator_category(__first
));
4147 * @brief Find the first element in a sequence for which a
4148 * predicate is true.
4149 * @param first An input iterator.
4150 * @param last An input iterator.
4151 * @param pred A predicate.
4152 * @return The first iterator @c i in the range @p [first,last)
4153 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4155 template<typename _InputIterator
, typename _Predicate
>
4156 inline _InputIterator
4157 find_if(_InputIterator __first
, _InputIterator __last
,
4160 // concept requirements
4161 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4162 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4163 typename iterator_traits
<_InputIterator
>::value_type
>)
4164 __glibcxx_requires_valid_range(__first
, __last
);
4165 return std::__find_if(__first
, __last
, __pred
,
4166 std::__iterator_category(__first
));
4170 * @brief Find element from a set in a sequence.
4171 * @param first1 Start of range to search.
4172 * @param last1 End of range to search.
4173 * @param first2 Start of match candidates.
4174 * @param last2 End of match candidates.
4175 * @return The first iterator @c i in the range
4176 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4177 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4179 * Searches the range @p [first1,last1) for an element that is equal to
4180 * some element in the range [first2,last2). If found, returns an iterator
4181 * in the range [first1,last1), otherwise returns @p last1.
4183 template<typename _InputIterator
, typename _ForwardIterator
>
4185 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4186 _ForwardIterator __first2
, _ForwardIterator __last2
)
4188 // concept requirements
4189 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4190 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4191 __glibcxx_function_requires(_EqualOpConcept
<
4192 typename iterator_traits
<_InputIterator
>::value_type
,
4193 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4194 __glibcxx_requires_valid_range(__first1
, __last1
);
4195 __glibcxx_requires_valid_range(__first2
, __last2
);
4197 for (; __first1
!= __last1
; ++__first1
)
4198 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4199 if (*__first1
== *__iter
)
4205 * @brief Find element from a set in a sequence using a predicate.
4206 * @param first1 Start of range to search.
4207 * @param last1 End of range to search.
4208 * @param first2 Start of match candidates.
4209 * @param last2 End of match candidates.
4210 * @param comp Predicate to use.
4211 * @return The first iterator @c i in the range
4212 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4213 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4216 * Searches the range @p [first1,last1) for an element that is
4217 * equal to some element in the range [first2,last2). If found,
4218 * returns an iterator in the range [first1,last1), otherwise
4221 template<typename _InputIterator
, typename _ForwardIterator
,
4222 typename _BinaryPredicate
>
4224 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4225 _ForwardIterator __first2
, _ForwardIterator __last2
,
4226 _BinaryPredicate __comp
)
4228 // concept requirements
4229 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4230 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4231 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4232 typename iterator_traits
<_InputIterator
>::value_type
,
4233 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4234 __glibcxx_requires_valid_range(__first1
, __last1
);
4235 __glibcxx_requires_valid_range(__first2
, __last2
);
4237 for (; __first1
!= __last1
; ++__first1
)
4238 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4239 if (__comp(*__first1
, *__iter
))
4245 * @brief Find two adjacent values in a sequence that are equal.
4246 * @param first A forward iterator.
4247 * @param last A forward iterator.
4248 * @return The first iterator @c i such that @c i and @c i+1 are both
4249 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4250 * or @p last if no such iterator exists.
4252 template<typename _ForwardIterator
>
4254 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4256 // concept requirements
4257 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4258 __glibcxx_function_requires(_EqualityComparableConcept
<
4259 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4260 __glibcxx_requires_valid_range(__first
, __last
);
4261 if (__first
== __last
)
4263 _ForwardIterator __next
= __first
;
4264 while(++__next
!= __last
)
4266 if (*__first
== *__next
)
4274 * @brief Find two adjacent values in a sequence using a predicate.
4275 * @param first A forward iterator.
4276 * @param last A forward iterator.
4277 * @param binary_pred A binary predicate.
4278 * @return The first iterator @c i such that @c i and @c i+1 are both
4279 * valid iterators in @p [first,last) and such that
4280 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4283 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4285 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4286 _BinaryPredicate __binary_pred
)
4288 // concept requirements
4289 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4290 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4291 typename iterator_traits
<_ForwardIterator
>::value_type
,
4292 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4293 __glibcxx_requires_valid_range(__first
, __last
);
4294 if (__first
== __last
)
4296 _ForwardIterator __next
= __first
;
4297 while(++__next
!= __last
)
4299 if (__binary_pred(*__first
, *__next
))
4307 * @brief Count the number of copies of a value in a sequence.
4308 * @param first An input iterator.
4309 * @param last An input iterator.
4310 * @param value The value to be counted.
4311 * @return The number of iterators @c i in the range @p [first,last)
4312 * for which @c *i == @p value
4314 template<typename _InputIterator
, typename _Tp
>
4315 typename iterator_traits
<_InputIterator
>::difference_type
4316 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4318 // concept requirements
4319 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4320 __glibcxx_function_requires(_EqualOpConcept
<
4321 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4322 __glibcxx_requires_valid_range(__first
, __last
);
4323 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4324 for (; __first
!= __last
; ++__first
)
4325 if (*__first
== __value
)
4331 * @brief Count the elements of a sequence for which a predicate is true.
4332 * @param first An input iterator.
4333 * @param last An input iterator.
4334 * @param pred A predicate.
4335 * @return The number of iterators @c i in the range @p [first,last)
4336 * for which @p pred(*i) is true.
4338 template<typename _InputIterator
, typename _Predicate
>
4339 typename iterator_traits
<_InputIterator
>::difference_type
4340 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4342 // concept requirements
4343 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4344 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4345 typename iterator_traits
<_InputIterator
>::value_type
>)
4346 __glibcxx_requires_valid_range(__first
, __last
);
4347 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4348 for (; __first
!= __last
; ++__first
)
4349 if (__pred(*__first
))
4355 * @brief Search a sequence for a matching sub-sequence.
4356 * @param first1 A forward iterator.
4357 * @param last1 A forward iterator.
4358 * @param first2 A forward iterator.
4359 * @param last2 A forward iterator.
4360 * @return The first iterator @c i in the range
4361 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4362 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4363 * such iterator exists.
4365 * Searches the range @p [first1,last1) for a sub-sequence that compares
4366 * equal value-by-value with the sequence given by @p [first2,last2) and
4367 * returns an iterator to the first element of the sub-sequence, or
4368 * @p last1 if the sub-sequence is not found.
4370 * Because the sub-sequence must lie completely within the range
4371 * @p [first1,last1) it must start at a position less than
4372 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4374 * This means that the returned iterator @c i will be in the range
4375 * @p [first1,last1-(last2-first2))
4377 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4379 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4380 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4382 // concept requirements
4383 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4384 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4385 __glibcxx_function_requires(_EqualOpConcept
<
4386 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4387 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4388 __glibcxx_requires_valid_range(__first1
, __last1
);
4389 __glibcxx_requires_valid_range(__first2
, __last2
);
4391 // Test for empty ranges
4392 if (__first1
== __last1
|| __first2
== __last2
)
4395 // Test for a pattern of length 1.
4396 _ForwardIterator2
__p1(__first2
);
4397 if (++__p1
== __last2
)
4398 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4401 _ForwardIterator2 __p
;
4402 _ForwardIterator1 __current
= __first1
;
4406 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4407 if (__first1
== __last1
)
4411 __current
= __first1
;
4412 if (++__current
== __last1
)
4415 while (*__current
== *__p
)
4417 if (++__p
== __last2
)
4419 if (++__current
== __last1
)
4428 * @brief Search a sequence for a matching sub-sequence using a predicate.
4429 * @param first1 A forward iterator.
4430 * @param last1 A forward iterator.
4431 * @param first2 A forward iterator.
4432 * @param last2 A forward iterator.
4433 * @param predicate A binary predicate.
4434 * @return The first iterator @c i in the range
4435 * @p [first1,last1-(last2-first2)) such that
4436 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4437 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4439 * Searches the range @p [first1,last1) for a sub-sequence that compares
4440 * equal value-by-value with the sequence given by @p [first2,last2),
4441 * using @p predicate to determine equality, and returns an iterator
4442 * to the first element of the sub-sequence, or @p last1 if no such
4445 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4447 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4448 typename _BinaryPredicate
>
4450 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4451 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4452 _BinaryPredicate __predicate
)
4454 // concept requirements
4455 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4456 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4457 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4458 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4459 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4460 __glibcxx_requires_valid_range(__first1
, __last1
);
4461 __glibcxx_requires_valid_range(__first2
, __last2
);
4463 // Test for empty ranges
4464 if (__first1
== __last1
|| __first2
== __last2
)
4467 // Test for a pattern of length 1.
4468 _ForwardIterator2
__p1(__first2
);
4469 if (++__p1
== __last2
)
4471 while (__first1
!= __last1
4472 && !bool(__predicate(*__first1
, *__first2
)))
4478 _ForwardIterator2 __p
;
4479 _ForwardIterator1 __current
= __first1
;
4483 while (__first1
!= __last1
4484 && !bool(__predicate(*__first1
, *__first2
)))
4486 if (__first1
== __last1
)
4490 __current
= __first1
;
4491 if (++__current
== __last1
)
4494 while (__predicate(*__current
, *__p
))
4496 if (++__p
== __last2
)
4498 if (++__current
== __last1
)
4508 * @brief Search a sequence for a number of consecutive values.
4509 * @param first A forward iterator.
4510 * @param last A forward iterator.
4511 * @param count The number of consecutive values.
4512 * @param val The value to find.
4513 * @return The first iterator @c i in the range @p [first,last-count)
4514 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4515 * or @p last if no such iterator exists.
4517 * Searches the range @p [first,last) for @p count consecutive elements
4520 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4522 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4523 _Integer __count
, const _Tp
& __val
)
4525 // concept requirements
4526 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4527 __glibcxx_function_requires(_EqualOpConcept
<
4528 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4529 __glibcxx_requires_valid_range(__first
, __last
);
4534 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4535 return std::__search_n(__first
, __last
, __count
, __val
,
4536 std::__iterator_category(__first
));
4541 * @brief Search a sequence for a number of consecutive values using a
4543 * @param first A forward iterator.
4544 * @param last A forward iterator.
4545 * @param count The number of consecutive values.
4546 * @param val The value to find.
4547 * @param binary_pred A binary predicate.
4548 * @return The first iterator @c i in the range @p [first,last-count)
4549 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4550 * range @p [0,count), or @p last if no such iterator exists.
4552 * Searches the range @p [first,last) for @p count consecutive elements
4553 * for which the predicate returns true.
4555 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4556 typename _BinaryPredicate
>
4558 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4559 _Integer __count
, const _Tp
& __val
,
4560 _BinaryPredicate __binary_pred
)
4562 // concept requirements
4563 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4564 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4565 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4566 __glibcxx_requires_valid_range(__first
, __last
);
4572 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4576 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4577 std::__iterator_category(__first
));
4582 * @brief Perform an operation on a sequence.
4583 * @param first An input iterator.
4584 * @param last An input iterator.
4585 * @param result An output iterator.
4586 * @param unary_op A unary operator.
4587 * @return An output iterator equal to @p result+(last-first).
4589 * Applies the operator to each element in the input range and assigns
4590 * the results to successive elements of the output sequence.
4591 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4592 * range @p [0,last-first).
4594 * @p unary_op must not alter its argument.
4596 template<typename _InputIterator
, typename _OutputIterator
,
4597 typename _UnaryOperation
>
4599 transform(_InputIterator __first
, _InputIterator __last
,
4600 _OutputIterator __result
, _UnaryOperation __unary_op
)
4602 // concept requirements
4603 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4604 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4605 // "the type returned by a _UnaryOperation"
4606 __typeof__(__unary_op(*__first
))>)
4607 __glibcxx_requires_valid_range(__first
, __last
);
4609 for (; __first
!= __last
; ++__first
, ++__result
)
4610 *__result
= __unary_op(*__first
);
4615 * @brief Perform an operation on corresponding elements of two sequences.
4616 * @param first1 An input iterator.
4617 * @param last1 An input iterator.
4618 * @param first2 An input iterator.
4619 * @param result An output iterator.
4620 * @param binary_op A binary operator.
4621 * @return An output iterator equal to @p result+(last-first).
4623 * Applies the operator to the corresponding elements in the two
4624 * input ranges and assigns the results to successive elements of the
4626 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4627 * @c N in the range @p [0,last1-first1).
4629 * @p binary_op must not alter either of its arguments.
4631 template<typename _InputIterator1
, typename _InputIterator2
,
4632 typename _OutputIterator
, typename _BinaryOperation
>
4634 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4635 _InputIterator2 __first2
, _OutputIterator __result
,
4636 _BinaryOperation __binary_op
)
4638 // concept requirements
4639 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4640 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4641 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4642 // "the type returned by a _BinaryOperation"
4643 __typeof__(__binary_op(*__first1
,*__first2
))>)
4644 __glibcxx_requires_valid_range(__first1
, __last1
);
4646 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4647 *__result
= __binary_op(*__first1
, *__first2
);
4652 * @brief Replace each occurrence of one value in a sequence with another
4654 * @param first A forward iterator.
4655 * @param last A forward iterator.
4656 * @param old_value The value to be replaced.
4657 * @param new_value The replacement value.
4658 * @return replace() returns no value.
4660 * For each iterator @c i in the range @p [first,last) if @c *i ==
4661 * @p old_value then the assignment @c *i = @p new_value is performed.
4663 template<typename _ForwardIterator
, typename _Tp
>
4665 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4666 const _Tp
& __old_value
, const _Tp
& __new_value
)
4668 // concept requirements
4669 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4671 __glibcxx_function_requires(_EqualOpConcept
<
4672 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4673 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4674 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4675 __glibcxx_requires_valid_range(__first
, __last
);
4677 for (; __first
!= __last
; ++__first
)
4678 if (*__first
== __old_value
)
4679 *__first
= __new_value
;
4683 * @brief Replace each value in a sequence for which a predicate returns
4684 * true with another value.
4685 * @param first A forward iterator.
4686 * @param last A forward iterator.
4687 * @param pred A predicate.
4688 * @param new_value The replacement value.
4689 * @return replace_if() returns no value.
4691 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4692 * is true then the assignment @c *i = @p new_value is performed.
4694 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4696 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4697 _Predicate __pred
, const _Tp
& __new_value
)
4699 // concept requirements
4700 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4702 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4703 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4704 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4705 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4706 __glibcxx_requires_valid_range(__first
, __last
);
4708 for (; __first
!= __last
; ++__first
)
4709 if (__pred(*__first
))
4710 *__first
= __new_value
;
4714 * @brief Assign the result of a function object to each value in a
4716 * @param first A forward iterator.
4717 * @param last A forward iterator.
4718 * @param gen A function object taking no arguments and returning
4719 * std::iterator_traits<_ForwardIterator>::value_type
4720 * @return generate() returns no value.
4722 * Performs the assignment @c *i = @p gen() for each @c i in the range
4725 template<typename _ForwardIterator
, typename _Generator
>
4727 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4730 // concept requirements
4731 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4732 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4733 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4734 __glibcxx_requires_valid_range(__first
, __last
);
4736 for (; __first
!= __last
; ++__first
)
4741 * @brief Assign the result of a function object to each value in a
4743 * @param first A forward iterator.
4744 * @param n The length of the sequence.
4745 * @param gen A function object taking no arguments and returning
4746 * std::iterator_traits<_ForwardIterator>::value_type
4747 * @return The end of the sequence, @p first+n
4749 * Performs the assignment @c *i = @p gen() for each @c i in the range
4750 * @p [first,first+n).
4752 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4754 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4756 // concept requirements
4757 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4758 // "the type returned by a _Generator"
4759 __typeof__(__gen())>)
4761 for (; __n
> 0; --__n
, ++__first
)
4768 * @brief Copy a sequence, removing consecutive duplicate values.
4769 * @param first An input iterator.
4770 * @param last An input iterator.
4771 * @param result An output iterator.
4772 * @return An iterator designating the end of the resulting sequence.
4774 * Copies each element in the range @p [first,last) to the range
4775 * beginning at @p result, except that only the first element is copied
4776 * from groups of consecutive elements that compare equal.
4777 * unique_copy() is stable, so the relative order of elements that are
4778 * copied is unchanged.
4780 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4781 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4783 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4784 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4787 template<typename _InputIterator
, typename _OutputIterator
>
4788 inline _OutputIterator
4789 unique_copy(_InputIterator __first
, _InputIterator __last
,
4790 _OutputIterator __result
)
4792 // concept requirements
4793 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4794 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4795 typename iterator_traits
<_InputIterator
>::value_type
>)
4796 __glibcxx_function_requires(_EqualityComparableConcept
<
4797 typename iterator_traits
<_InputIterator
>::value_type
>)
4798 __glibcxx_requires_valid_range(__first
, __last
);
4800 if (__first
== __last
)
4802 return std::__unique_copy(__first
, __last
, __result
,
4803 std::__iterator_category(__first
),
4804 std::__iterator_category(__result
));
4808 * @brief Copy a sequence, removing consecutive values using a predicate.
4809 * @param first An input iterator.
4810 * @param last An input iterator.
4811 * @param result An output iterator.
4812 * @param binary_pred A binary predicate.
4813 * @return An iterator designating the end of the resulting sequence.
4815 * Copies each element in the range @p [first,last) to the range
4816 * beginning at @p result, except that only the first element is copied
4817 * from groups of consecutive elements for which @p binary_pred returns
4819 * unique_copy() is stable, so the relative order of elements that are
4820 * copied is unchanged.
4822 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4823 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4825 template<typename _InputIterator
, typename _OutputIterator
,
4826 typename _BinaryPredicate
>
4827 inline _OutputIterator
4828 unique_copy(_InputIterator __first
, _InputIterator __last
,
4829 _OutputIterator __result
,
4830 _BinaryPredicate __binary_pred
)
4832 // concept requirements -- predicates checked later
4833 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4834 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4835 typename iterator_traits
<_InputIterator
>::value_type
>)
4836 __glibcxx_requires_valid_range(__first
, __last
);
4838 if (__first
== __last
)
4840 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4841 std::__iterator_category(__first
),
4842 std::__iterator_category(__result
));
4847 * @brief Randomly shuffle the elements of a sequence.
4848 * @param first A forward iterator.
4849 * @param last A forward iterator.
4852 * Reorder the elements in the range @p [first,last) using a random
4853 * distribution, so that every possible ordering of the sequence is
4856 template<typename _RandomAccessIterator
>
4858 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4860 // concept requirements
4861 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4862 _RandomAccessIterator
>)
4863 __glibcxx_requires_valid_range(__first
, __last
);
4865 if (__first
!= __last
)
4866 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4867 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4871 * @brief Shuffle the elements of a sequence using a random number
4873 * @param first A forward iterator.
4874 * @param last A forward iterator.
4875 * @param rand The RNG functor or function.
4878 * Reorders the elements in the range @p [first,last) using @p rand to
4879 * provide a random distribution. Calling @p rand(N) for a positive
4880 * integer @p N should return a randomly chosen integer from the
4883 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4885 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4886 _RandomNumberGenerator
& __rand
)
4888 // concept requirements
4889 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4890 _RandomAccessIterator
>)
4891 __glibcxx_requires_valid_range(__first
, __last
);
4893 if (__first
== __last
)
4895 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4896 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4901 * @brief Move elements for which a predicate is true to the beginning
4903 * @param first A forward iterator.
4904 * @param last A forward iterator.
4905 * @param pred A predicate functor.
4906 * @return An iterator @p middle such that @p pred(i) is true for each
4907 * iterator @p i in the range @p [first,middle) and false for each @p i
4908 * in the range @p [middle,last).
4910 * @p pred must not modify its operand. @p partition() does not preserve
4911 * the relative ordering of elements in each group, use
4912 * @p stable_partition() if this is needed.
4914 template<typename _ForwardIterator
, typename _Predicate
>
4915 inline _ForwardIterator
4916 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4919 // concept requirements
4920 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4922 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4923 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4924 __glibcxx_requires_valid_range(__first
, __last
);
4926 return std::__partition(__first
, __last
, __pred
,
4927 std::__iterator_category(__first
));
4933 * @brief Sort the smallest elements of a sequence.
4934 * @param first An iterator.
4935 * @param middle Another iterator.
4936 * @param last Another iterator.
4939 * Sorts the smallest @p (middle-first) elements in the range
4940 * @p [first,last) and moves them to the range @p [first,middle). The
4941 * order of the remaining elements in the range @p [middle,last) is
4943 * After the sort if @p i and @j are iterators in the range
4944 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4945 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4947 template<typename _RandomAccessIterator
>
4949 partial_sort(_RandomAccessIterator __first
,
4950 _RandomAccessIterator __middle
,
4951 _RandomAccessIterator __last
)
4953 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4956 // concept requirements
4957 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4958 _RandomAccessIterator
>)
4959 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4960 __glibcxx_requires_valid_range(__first
, __middle
);
4961 __glibcxx_requires_valid_range(__middle
, __last
);
4963 std::__heap_select(__first
, __middle
, __last
);
4964 std::sort_heap(__first
, __middle
);
4968 * @brief Sort the smallest elements of a sequence using a predicate
4970 * @param first An iterator.
4971 * @param middle Another iterator.
4972 * @param last Another iterator.
4973 * @param comp A comparison functor.
4976 * Sorts the smallest @p (middle-first) elements in the range
4977 * @p [first,last) and moves them to the range @p [first,middle). The
4978 * order of the remaining elements in the range @p [middle,last) is
4980 * After the sort if @p i and @j are iterators in the range
4981 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4982 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4985 template<typename _RandomAccessIterator
, typename _Compare
>
4987 partial_sort(_RandomAccessIterator __first
,
4988 _RandomAccessIterator __middle
,
4989 _RandomAccessIterator __last
,
4992 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4995 // concept requirements
4996 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4997 _RandomAccessIterator
>)
4998 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4999 _ValueType
, _ValueType
>)
5000 __glibcxx_requires_valid_range(__first
, __middle
);
5001 __glibcxx_requires_valid_range(__middle
, __last
);
5003 std::__heap_select(__first
, __middle
, __last
, __comp
);
5004 std::sort_heap(__first
, __middle
, __comp
);
5008 * @brief Sort a sequence just enough to find a particular position.
5009 * @param first An iterator.
5010 * @param nth Another iterator.
5011 * @param last Another iterator.
5014 * Rearranges the elements in the range @p [first,last) so that @p *nth
5015 * is the same element that would have been in that position had the
5016 * whole sequence been sorted.
5017 * whole sequence been sorted. The elements either side of @p *nth are
5018 * not completely sorted, but for any iterator @i in the range
5019 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5020 * holds that @p *j<*i is false.
5022 template<typename _RandomAccessIterator
>
5024 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5025 _RandomAccessIterator __last
)
5027 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5030 // concept requirements
5031 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5032 _RandomAccessIterator
>)
5033 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5034 __glibcxx_requires_valid_range(__first
, __nth
);
5035 __glibcxx_requires_valid_range(__nth
, __last
);
5037 if (__first
== __last
|| __nth
== __last
)
5040 std::__introselect(__first
, __nth
, __last
,
5041 std::__lg(__last
- __first
) * 2);
5045 * @brief Sort a sequence just enough to find a particular position
5046 * using a predicate for comparison.
5047 * @param first An iterator.
5048 * @param nth Another iterator.
5049 * @param last Another iterator.
5050 * @param comp A comparison functor.
5053 * Rearranges the elements in the range @p [first,last) so that @p *nth
5054 * is the same element that would have been in that position had the
5055 * whole sequence been sorted. The elements either side of @p *nth are
5056 * not completely sorted, but for any iterator @i in the range
5057 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5058 * holds that @p comp(*j,*i) is false.
5060 template<typename _RandomAccessIterator
, typename _Compare
>
5062 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5063 _RandomAccessIterator __last
, _Compare __comp
)
5065 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5068 // concept requirements
5069 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5070 _RandomAccessIterator
>)
5071 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5072 _ValueType
, _ValueType
>)
5073 __glibcxx_requires_valid_range(__first
, __nth
);
5074 __glibcxx_requires_valid_range(__nth
, __last
);
5076 if (__first
== __last
|| __nth
== __last
)
5079 std::__introselect(__first
, __nth
, __last
,
5080 std::__lg(__last
- __first
) * 2, __comp
);
5085 * @brief Sort the elements of a sequence.
5086 * @param first An iterator.
5087 * @param last Another iterator.
5090 * Sorts the elements in the range @p [first,last) in ascending order,
5091 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5092 * @p [first,last-1).
5094 * The relative ordering of equivalent elements is not preserved, use
5095 * @p stable_sort() if this is needed.
5097 template<typename _RandomAccessIterator
>
5099 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5101 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5104 // concept requirements
5105 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5106 _RandomAccessIterator
>)
5107 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5108 __glibcxx_requires_valid_range(__first
, __last
);
5110 if (__first
!= __last
)
5112 std::__introsort_loop(__first
, __last
,
5113 std::__lg(__last
- __first
) * 2);
5114 std::__final_insertion_sort(__first
, __last
);
5119 * @brief Sort the elements of a sequence using a predicate for comparison.
5120 * @param first An iterator.
5121 * @param last Another iterator.
5122 * @param comp A comparison functor.
5125 * Sorts the elements in the range @p [first,last) in ascending order,
5126 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5127 * range @p [first,last-1).
5129 * The relative ordering of equivalent elements is not preserved, use
5130 * @p stable_sort() if this is needed.
5132 template<typename _RandomAccessIterator
, typename _Compare
>
5134 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5137 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5140 // concept requirements
5141 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5142 _RandomAccessIterator
>)
5143 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5145 __glibcxx_requires_valid_range(__first
, __last
);
5147 if (__first
!= __last
)
5149 std::__introsort_loop(__first
, __last
,
5150 std::__lg(__last
- __first
) * 2, __comp
);
5151 std::__final_insertion_sort(__first
, __last
, __comp
);
5156 * @brief Merges two sorted ranges.
5157 * @param first1 An iterator.
5158 * @param first2 Another iterator.
5159 * @param last1 Another iterator.
5160 * @param last2 Another iterator.
5161 * @param result An iterator pointing to the end of the merged range.
5162 * @return An iterator pointing to the first element "not less
5165 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5166 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5167 * must be sorted, and the output range must not overlap with either of
5168 * the input ranges. The sort is @e stable, that is, for equivalent
5169 * elements in the two ranges, elements from the first range will always
5170 * come before elements from the second.
5172 template<typename _InputIterator1
, typename _InputIterator2
,
5173 typename _OutputIterator
>
5175 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5176 _InputIterator2 __first2
, _InputIterator2 __last2
,
5177 _OutputIterator __result
)
5179 typedef typename iterator_traits
<_InputIterator1
>::value_type
5181 typedef typename iterator_traits
<_InputIterator2
>::value_type
5184 // concept requirements
5185 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5186 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5187 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5189 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5191 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5192 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5193 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5195 while (__first1
!= __last1
&& __first2
!= __last2
)
5197 if (*__first2
< *__first1
)
5199 *__result
= *__first2
;
5204 *__result
= *__first1
;
5209 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5214 * @brief Merges two sorted ranges.
5215 * @param first1 An iterator.
5216 * @param first2 Another iterator.
5217 * @param last1 Another iterator.
5218 * @param last2 Another iterator.
5219 * @param result An iterator pointing to the end of the merged range.
5220 * @param comp A functor to use for comparisons.
5221 * @return An iterator pointing to the first element "not less
5224 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5225 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5226 * must be sorted, and the output range must not overlap with either of
5227 * the input ranges. The sort is @e stable, that is, for equivalent
5228 * elements in the two ranges, elements from the first range will always
5229 * come before elements from the second.
5231 * The comparison function should have the same effects on ordering as
5232 * the function used for the initial sort.
5234 template<typename _InputIterator1
, typename _InputIterator2
,
5235 typename _OutputIterator
, typename _Compare
>
5237 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5238 _InputIterator2 __first2
, _InputIterator2 __last2
,
5239 _OutputIterator __result
, _Compare __comp
)
5241 typedef typename iterator_traits
<_InputIterator1
>::value_type
5243 typedef typename iterator_traits
<_InputIterator2
>::value_type
5246 // concept requirements
5247 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5248 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5249 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5251 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5253 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5254 _ValueType2
, _ValueType1
>)
5255 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5256 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5258 while (__first1
!= __last1
&& __first2
!= __last2
)
5260 if (__comp(*__first2
, *__first1
))
5262 *__result
= *__first2
;
5267 *__result
= *__first1
;
5272 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5278 * @brief Sort the elements of a sequence, preserving the relative order
5279 * of equivalent elements.
5280 * @param first An iterator.
5281 * @param last Another iterator.
5284 * Sorts the elements in the range @p [first,last) in ascending order,
5285 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5286 * @p [first,last-1).
5288 * The relative ordering of equivalent elements is preserved, so any two
5289 * elements @p x and @p y in the range @p [first,last) such that
5290 * @p x<y is false and @p y<x is false will have the same relative
5291 * ordering after calling @p stable_sort().
5293 template<typename _RandomAccessIterator
>
5295 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5297 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5299 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5302 // concept requirements
5303 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5304 _RandomAccessIterator
>)
5305 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5306 __glibcxx_requires_valid_range(__first
, __last
);
5308 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5310 if (__buf
.begin() == 0)
5311 std::__inplace_stable_sort(__first
, __last
);
5313 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5314 _DistanceType(__buf
.size()));
5318 * @brief Sort the elements of a sequence using a predicate for comparison,
5319 * preserving the relative order of equivalent elements.
5320 * @param first An iterator.
5321 * @param last Another iterator.
5322 * @param comp A comparison functor.
5325 * Sorts the elements in the range @p [first,last) in ascending order,
5326 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5327 * range @p [first,last-1).
5329 * The relative ordering of equivalent elements is preserved, so any two
5330 * elements @p x and @p y in the range @p [first,last) such that
5331 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5332 * relative ordering after calling @p stable_sort().
5334 template<typename _RandomAccessIterator
, typename _Compare
>
5336 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5339 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5341 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5344 // concept requirements
5345 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5346 _RandomAccessIterator
>)
5347 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5350 __glibcxx_requires_valid_range(__first
, __last
);
5352 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5354 if (__buf
.begin() == 0)
5355 std::__inplace_stable_sort(__first
, __last
, __comp
);
5357 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5358 _DistanceType(__buf
.size()), __comp
);
5363 * @brief Return the union of two sorted ranges.
5364 * @param first1 Start of first range.
5365 * @param last1 End of first range.
5366 * @param first2 Start of second range.
5367 * @param last2 End of second range.
5368 * @return End of the output range.
5369 * @ingroup setoperations
5371 * This operation iterates over both ranges, copying elements present in
5372 * each range in order to the output range. Iterators increment for each
5373 * range. When the current element of one range is less than the other,
5374 * that element is copied and the iterator advanced. If an element is
5375 * contained in both ranges, the element from the first range is copied and
5376 * both ranges advance. The output range may not overlap either input
5379 template<typename _InputIterator1
, typename _InputIterator2
,
5380 typename _OutputIterator
>
5382 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5383 _InputIterator2 __first2
, _InputIterator2 __last2
,
5384 _OutputIterator __result
)
5386 typedef typename iterator_traits
<_InputIterator1
>::value_type
5388 typedef typename iterator_traits
<_InputIterator2
>::value_type
5391 // concept requirements
5392 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5393 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5394 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5396 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5398 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5399 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5400 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5401 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5403 while (__first1
!= __last1
&& __first2
!= __last2
)
5405 if (*__first1
< *__first2
)
5407 *__result
= *__first1
;
5410 else if (*__first2
< *__first1
)
5412 *__result
= *__first2
;
5417 *__result
= *__first1
;
5423 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5428 * @brief Return the union of two sorted ranges using a comparison functor.
5429 * @param first1 Start of first range.
5430 * @param last1 End of first range.
5431 * @param first2 Start of second range.
5432 * @param last2 End of second range.
5433 * @param comp The comparison functor.
5434 * @return End of the output range.
5435 * @ingroup setoperations
5437 * This operation iterates over both ranges, copying elements present in
5438 * each range in order to the output range. Iterators increment for each
5439 * range. When the current element of one range is less than the other
5440 * according to @a comp, that element is copied and the iterator advanced.
5441 * If an equivalent element according to @a comp is contained in both
5442 * ranges, the element from the first range is copied and both ranges
5443 * advance. The output range may not overlap either input range.
5445 template<typename _InputIterator1
, typename _InputIterator2
,
5446 typename _OutputIterator
, typename _Compare
>
5448 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5449 _InputIterator2 __first2
, _InputIterator2 __last2
,
5450 _OutputIterator __result
, _Compare __comp
)
5452 typedef typename iterator_traits
<_InputIterator1
>::value_type
5454 typedef typename iterator_traits
<_InputIterator2
>::value_type
5457 // concept requirements
5458 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5459 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5460 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5462 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5464 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5465 _ValueType1
, _ValueType2
>)
5466 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5467 _ValueType2
, _ValueType1
>)
5468 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5469 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5471 while (__first1
!= __last1
&& __first2
!= __last2
)
5473 if (__comp(*__first1
, *__first2
))
5475 *__result
= *__first1
;
5478 else if (__comp(*__first2
, *__first1
))
5480 *__result
= *__first2
;
5485 *__result
= *__first1
;
5491 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5496 * @brief Return the intersection of two sorted ranges.
5497 * @param first1 Start of first range.
5498 * @param last1 End of first range.
5499 * @param first2 Start of second range.
5500 * @param last2 End of second range.
5501 * @return End of the output range.
5502 * @ingroup setoperations
5504 * This operation iterates over both ranges, copying elements present in
5505 * both ranges in order to the output range. Iterators increment for each
5506 * range. When the current element of one range is less than the other,
5507 * that iterator advances. If an element is contained in both ranges, the
5508 * element from the first range is copied and both ranges advance. The
5509 * output range may not overlap either input range.
5511 template<typename _InputIterator1
, typename _InputIterator2
,
5512 typename _OutputIterator
>
5514 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5515 _InputIterator2 __first2
, _InputIterator2 __last2
,
5516 _OutputIterator __result
)
5518 typedef typename iterator_traits
<_InputIterator1
>::value_type
5520 typedef typename iterator_traits
<_InputIterator2
>::value_type
5523 // concept requirements
5524 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5525 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5526 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5528 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5529 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5530 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5531 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5533 while (__first1
!= __last1
&& __first2
!= __last2
)
5534 if (*__first1
< *__first2
)
5536 else if (*__first2
< *__first1
)
5540 *__result
= *__first1
;
5549 * @brief Return the intersection of two sorted ranges using comparison
5551 * @param first1 Start of first range.
5552 * @param last1 End of first range.
5553 * @param first2 Start of second range.
5554 * @param last2 End of second range.
5555 * @param comp The comparison functor.
5556 * @return End of the output range.
5557 * @ingroup setoperations
5559 * This operation iterates over both ranges, copying elements present in
5560 * both ranges in order to the output range. Iterators increment for each
5561 * range. When the current element of one range is less than the other
5562 * according to @a comp, that iterator advances. If an element is
5563 * contained in both ranges according to @a comp, the element from the
5564 * first range is copied and both ranges advance. The output range may not
5565 * overlap either input range.
5567 template<typename _InputIterator1
, typename _InputIterator2
,
5568 typename _OutputIterator
, typename _Compare
>
5570 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5571 _InputIterator2 __first2
, _InputIterator2 __last2
,
5572 _OutputIterator __result
, _Compare __comp
)
5574 typedef typename iterator_traits
<_InputIterator1
>::value_type
5576 typedef typename iterator_traits
<_InputIterator2
>::value_type
5579 // concept requirements
5580 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5581 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5582 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5584 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5585 _ValueType1
, _ValueType2
>)
5586 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5587 _ValueType2
, _ValueType1
>)
5588 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5589 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5591 while (__first1
!= __last1
&& __first2
!= __last2
)
5592 if (__comp(*__first1
, *__first2
))
5594 else if (__comp(*__first2
, *__first1
))
5598 *__result
= *__first1
;
5607 * @brief Return the difference of two sorted ranges.
5608 * @param first1 Start of first range.
5609 * @param last1 End of first range.
5610 * @param first2 Start of second range.
5611 * @param last2 End of second range.
5612 * @return End of the output range.
5613 * @ingroup setoperations
5615 * This operation iterates over both ranges, copying elements present in
5616 * the first range but not the second in order to the output range.
5617 * Iterators increment for each range. When the current element of the
5618 * first range is less than the second, that element is copied and the
5619 * iterator advances. If the current element of the second range is less,
5620 * the iterator advances, but no element is copied. If an element is
5621 * contained in both ranges, no elements are copied and both ranges
5622 * advance. The output range may not overlap either input range.
5624 template<typename _InputIterator1
, typename _InputIterator2
,
5625 typename _OutputIterator
>
5627 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5628 _InputIterator2 __first2
, _InputIterator2 __last2
,
5629 _OutputIterator __result
)
5631 typedef typename iterator_traits
<_InputIterator1
>::value_type
5633 typedef typename iterator_traits
<_InputIterator2
>::value_type
5636 // concept requirements
5637 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5638 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5639 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5641 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5642 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5643 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5644 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5646 while (__first1
!= __last1
&& __first2
!= __last2
)
5647 if (*__first1
< *__first2
)
5649 *__result
= *__first1
;
5653 else if (*__first2
< *__first1
)
5660 return std::copy(__first1
, __last1
, __result
);
5664 * @brief Return the difference of two sorted ranges using comparison
5666 * @param first1 Start of first range.
5667 * @param last1 End of first range.
5668 * @param first2 Start of second range.
5669 * @param last2 End of second range.
5670 * @param comp The comparison functor.
5671 * @return End of the output range.
5672 * @ingroup setoperations
5674 * This operation iterates over both ranges, copying elements present in
5675 * the first range but not the second in order to the output range.
5676 * Iterators increment for each range. When the current element of the
5677 * first range is less than the second according to @a comp, that element
5678 * is copied and the iterator advances. If the current element of the
5679 * second range is less, no element is copied and the iterator advances.
5680 * If an element is contained in both ranges according to @a comp, no
5681 * elements are 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_difference(_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 *__result
= *__first1
;
5715 else if (__comp(*__first2
, *__first1
))
5722 return std::copy(__first1
, __last1
, __result
);
5726 * @brief Return the symmetric difference of two sorted ranges.
5727 * @param first1 Start of first range.
5728 * @param last1 End of first range.
5729 * @param first2 Start of second range.
5730 * @param last2 End of second range.
5731 * @return End of the output range.
5732 * @ingroup setoperations
5734 * This operation iterates over both ranges, copying elements present in
5735 * one range but not the other in order to the output range. Iterators
5736 * increment for each range. When the current element of one range is less
5737 * than the other, that element is copied and the iterator advances. If an
5738 * element is contained in both ranges, no elements are copied and both
5739 * ranges advance. The output range may not overlap either input range.
5741 template<typename _InputIterator1
, typename _InputIterator2
,
5742 typename _OutputIterator
>
5744 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5745 _InputIterator2 __first2
, _InputIterator2 __last2
,
5746 _OutputIterator __result
)
5748 typedef typename iterator_traits
<_InputIterator1
>::value_type
5750 typedef typename iterator_traits
<_InputIterator2
>::value_type
5753 // concept requirements
5754 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5755 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5756 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5758 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5760 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5761 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5762 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5763 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5765 while (__first1
!= __last1
&& __first2
!= __last2
)
5766 if (*__first1
< *__first2
)
5768 *__result
= *__first1
;
5772 else if (*__first2
< *__first1
)
5774 *__result
= *__first2
;
5783 return std::copy(__first2
, __last2
, std::copy(__first1
,
5784 __last1
, __result
));
5788 * @brief Return the symmetric difference of two sorted ranges using
5789 * comparison functor.
5790 * @param first1 Start of first range.
5791 * @param last1 End of first range.
5792 * @param first2 Start of second range.
5793 * @param last2 End of second range.
5794 * @param comp The comparison functor.
5795 * @return End of the output range.
5796 * @ingroup setoperations
5798 * This operation iterates over both ranges, copying elements present in
5799 * one range but not the other in order to the output range. Iterators
5800 * increment for each range. When the current element of one range is less
5801 * than the other according to @a comp, that element is copied and the
5802 * iterator advances. If an element is contained in both ranges according
5803 * to @a comp, no elements are copied and both ranges advance. The output
5804 * range may not overlap either input range.
5806 template<typename _InputIterator1
, typename _InputIterator2
,
5807 typename _OutputIterator
, typename _Compare
>
5809 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5810 _InputIterator2 __first2
, _InputIterator2 __last2
,
5811 _OutputIterator __result
,
5814 typedef typename iterator_traits
<_InputIterator1
>::value_type
5816 typedef typename iterator_traits
<_InputIterator2
>::value_type
5819 // concept requirements
5820 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5821 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5822 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5824 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5826 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5827 _ValueType1
, _ValueType2
>)
5828 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5829 _ValueType2
, _ValueType1
>)
5830 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5831 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5833 while (__first1
!= __last1
&& __first2
!= __last2
)
5834 if (__comp(*__first1
, *__first2
))
5836 *__result
= *__first1
;
5840 else if (__comp(*__first2
, *__first1
))
5842 *__result
= *__first2
;
5851 return std::copy(__first2
, __last2
,
5852 std::copy(__first1
, __last1
, __result
));
5857 * @brief Return the minimum element in a range.
5858 * @param first Start of range.
5859 * @param last End of range.
5860 * @return Iterator referencing the first instance of the smallest value.
5862 template<typename _ForwardIterator
>
5864 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5866 // concept requirements
5867 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5868 __glibcxx_function_requires(_LessThanComparableConcept
<
5869 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5870 __glibcxx_requires_valid_range(__first
, __last
);
5872 if (__first
== __last
)
5874 _ForwardIterator __result
= __first
;
5875 while (++__first
!= __last
)
5876 if (*__first
< *__result
)
5882 * @brief Return the minimum element in a range using comparison functor.
5883 * @param first Start of range.
5884 * @param last End of range.
5885 * @param comp Comparison functor.
5886 * @return Iterator referencing the first instance of the smallest value
5887 * according to comp.
5889 template<typename _ForwardIterator
, typename _Compare
>
5891 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5894 // concept requirements
5895 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5896 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5897 typename iterator_traits
<_ForwardIterator
>::value_type
,
5898 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5899 __glibcxx_requires_valid_range(__first
, __last
);
5901 if (__first
== __last
)
5903 _ForwardIterator __result
= __first
;
5904 while (++__first
!= __last
)
5905 if (__comp(*__first
, *__result
))
5911 * @brief Return the maximum element in a range.
5912 * @param first Start of range.
5913 * @param last End of range.
5914 * @return Iterator referencing the first instance of the largest value.
5916 template<typename _ForwardIterator
>
5918 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5920 // concept requirements
5921 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5922 __glibcxx_function_requires(_LessThanComparableConcept
<
5923 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5924 __glibcxx_requires_valid_range(__first
, __last
);
5926 if (__first
== __last
)
5928 _ForwardIterator __result
= __first
;
5929 while (++__first
!= __last
)
5930 if (*__result
< *__first
)
5936 * @brief Return the maximum element in a range using comparison functor.
5937 * @param first Start of range.
5938 * @param last End of range.
5939 * @param comp Comparison functor.
5940 * @return Iterator referencing the first instance of the largest value
5941 * according to comp.
5943 template<typename _ForwardIterator
, typename _Compare
>
5945 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5948 // concept requirements
5949 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5950 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5951 typename iterator_traits
<_ForwardIterator
>::value_type
,
5952 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5953 __glibcxx_requires_valid_range(__first
, __last
);
5955 if (__first
== __last
) return __first
;
5956 _ForwardIterator __result
= __first
;
5957 while (++__first
!= __last
)
5958 if (__comp(*__result
, *__first
))
5963 _GLIBCXX_END_NESTED_NAMESPACE
5965 #endif /* _STL_ALGO_H */