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
);
824 * @brief Copy a sequence, removing elements of a given value.
825 * @param first An input iterator.
826 * @param last An input iterator.
827 * @param result An output iterator.
828 * @param value The value to be removed.
829 * @return An iterator designating the end of the resulting sequence.
831 * Copies each element in the range @p [first,last) not equal to @p value
832 * to the range beginning at @p result.
833 * remove_copy() is stable, so the relative order of elements that are
834 * copied is unchanged.
836 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
838 remove_copy(_InputIterator __first
, _InputIterator __last
,
839 _OutputIterator __result
, const _Tp
& __value
)
841 // concept requirements
842 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
843 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
844 typename iterator_traits
<_InputIterator
>::value_type
>)
845 __glibcxx_function_requires(_EqualOpConcept
<
846 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
847 __glibcxx_requires_valid_range(__first
, __last
);
849 for (; __first
!= __last
; ++__first
)
850 if (!(*__first
== __value
))
852 *__result
= *__first
;
859 * @brief Copy a sequence, removing elements for which a predicate is true.
860 * @param first An input iterator.
861 * @param last An input iterator.
862 * @param result An output iterator.
863 * @param pred A predicate.
864 * @return An iterator designating the end of the resulting sequence.
866 * Copies each element in the range @p [first,last) for which
867 * @p pred returns false to the range beginning at @p result.
869 * remove_copy_if() is stable, so the relative order of elements that are
870 * copied is unchanged.
872 template<typename _InputIterator
, typename _OutputIterator
,
875 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
876 _OutputIterator __result
, _Predicate __pred
)
878 // concept requirements
879 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
880 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
881 typename iterator_traits
<_InputIterator
>::value_type
>)
882 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
883 typename iterator_traits
<_InputIterator
>::value_type
>)
884 __glibcxx_requires_valid_range(__first
, __last
);
886 for (; __first
!= __last
; ++__first
)
887 if (!bool(__pred(*__first
)))
889 *__result
= *__first
;
895 #ifdef __GXX_EXPERIMENTAL_CXX0X__
897 * @brief Copy the elements of a sequence for which a predicate is true.
898 * @param first An input iterator.
899 * @param last An input iterator.
900 * @param result An output iterator.
901 * @param pred A predicate.
902 * @return An iterator designating the end of the resulting sequence.
904 * Copies each element in the range @p [first,last) for which
905 * @p pred returns true to the range beginning at @p result.
907 * copy_if() is stable, so the relative order of elements that are
908 * copied is unchanged.
910 template<typename _InputIterator
, typename _OutputIterator
,
913 copy_if(_InputIterator __first
, _InputIterator __last
,
914 _OutputIterator __result
, _Predicate __pred
)
916 // concept requirements
917 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
918 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
919 typename iterator_traits
<_InputIterator
>::value_type
>)
920 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
921 typename iterator_traits
<_InputIterator
>::value_type
>)
922 __glibcxx_requires_valid_range(__first
, __last
);
924 for (; __first
!= __last
; ++__first
)
925 if (__pred(*__first
))
927 *__result
= *__first
;
934 * @brief Copy the elements of a sequence to separate output sequences
935 * depending on the truth value of a predicate.
936 * @param first An input iterator.
937 * @param last An input iterator.
938 * @param out_true An output iterator.
939 * @param out_false An output iterator.
940 * @param pred A predicate.
941 * @return A pair designating the ends of the resulting sequences.
943 * Copies each element in the range @p [first,last) for which
944 * @p pred returns true to the range beginning at @p out_true
945 * and each element for which @p pred returns false to @p out_false.
947 template<typename _InputIterator
, typename _OutputIterator1
,
948 typename _OutputIterator2
, typename _Predicate
>
949 pair
<_OutputIterator1
, _OutputIterator2
>
950 partition_copy(_InputIterator __first
, _InputIterator __last
,
951 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
954 // concept requirements
955 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
956 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
957 typename iterator_traits
<_InputIterator
>::value_type
>)
958 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
959 typename iterator_traits
<_InputIterator
>::value_type
>)
960 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
961 typename iterator_traits
<_InputIterator
>::value_type
>)
962 __glibcxx_requires_valid_range(__first
, __last
);
964 for (; __first
!= __last
; ++__first
)
965 if (__pred(*__first
))
967 *__out_true
= *__first
;
972 *__out_false
= *__first
;
976 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
981 * @brief Remove elements from a sequence.
982 * @param first An input iterator.
983 * @param last An input iterator.
984 * @param value The value to be removed.
985 * @return An iterator designating the end of the resulting sequence.
987 * All elements equal to @p value are removed from the range
990 * remove() is stable, so the relative order of elements that are
991 * not removed is unchanged.
993 * Elements between the end of the resulting sequence and @p last
994 * are still present, but their value is unspecified.
996 template<typename _ForwardIterator
, typename _Tp
>
998 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1001 // concept requirements
1002 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1004 __glibcxx_function_requires(_EqualOpConcept
<
1005 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1006 __glibcxx_requires_valid_range(__first
, __last
);
1008 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
1009 if(__first
== __last
)
1011 _ForwardIterator __result
= __first
;
1013 for(; __first
!= __last
; ++__first
)
1014 if(!(*__first
== __value
))
1016 *__result
= _GLIBCXX_MOVE(*__first
);
1023 * @brief Remove elements from a sequence using a predicate.
1024 * @param first A forward iterator.
1025 * @param last A forward iterator.
1026 * @param pred A predicate.
1027 * @return An iterator designating the end of the resulting sequence.
1029 * All elements for which @p pred returns true are removed from the range
1032 * remove_if() is stable, so the relative order of elements that are
1033 * not removed is unchanged.
1035 * Elements between the end of the resulting sequence and @p last
1036 * are still present, but their value is unspecified.
1038 template<typename _ForwardIterator
, typename _Predicate
>
1040 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1043 // concept requirements
1044 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1046 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1047 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1048 __glibcxx_requires_valid_range(__first
, __last
);
1050 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1051 if(__first
== __last
)
1053 _ForwardIterator __result
= __first
;
1055 for(; __first
!= __last
; ++__first
)
1056 if(!bool(__pred(*__first
)))
1058 *__result
= _GLIBCXX_MOVE(*__first
);
1065 * @brief Remove consecutive duplicate values from a sequence.
1066 * @param first A forward iterator.
1067 * @param last A forward iterator.
1068 * @return An iterator designating the end of the resulting sequence.
1070 * Removes all but the first element from each group of consecutive
1071 * values that compare equal.
1072 * unique() is stable, so the relative order of elements that are
1073 * not removed is unchanged.
1074 * Elements between the end of the resulting sequence and @p last
1075 * are still present, but their value is unspecified.
1077 template<typename _ForwardIterator
>
1079 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1081 // concept requirements
1082 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1084 __glibcxx_function_requires(_EqualityComparableConcept
<
1085 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1086 __glibcxx_requires_valid_range(__first
, __last
);
1088 // Skip the beginning, if already unique.
1089 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1090 if (__first
== __last
)
1093 // Do the real copy work.
1094 _ForwardIterator __dest
= __first
;
1096 while (++__first
!= __last
)
1097 if (!(*__dest
== *__first
))
1098 *++__dest
= _GLIBCXX_MOVE(*__first
);
1103 * @brief Remove consecutive values from a sequence using a predicate.
1104 * @param first A forward iterator.
1105 * @param last A forward iterator.
1106 * @param binary_pred A binary predicate.
1107 * @return An iterator designating the end of the resulting sequence.
1109 * Removes all but the first element from each group of consecutive
1110 * values for which @p binary_pred returns true.
1111 * unique() is stable, so the relative order of elements that are
1112 * not removed is unchanged.
1113 * Elements between the end of the resulting sequence and @p last
1114 * are still present, but their value is unspecified.
1116 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1118 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1119 _BinaryPredicate __binary_pred
)
1121 // concept requirements
1122 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1124 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1125 typename iterator_traits
<_ForwardIterator
>::value_type
,
1126 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1127 __glibcxx_requires_valid_range(__first
, __last
);
1129 // Skip the beginning, if already unique.
1130 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1131 if (__first
== __last
)
1134 // Do the real copy work.
1135 _ForwardIterator __dest
= __first
;
1137 while (++__first
!= __last
)
1138 if (!bool(__binary_pred(*__dest
, *__first
)))
1139 *++__dest
= _GLIBCXX_MOVE(*__first
);
1144 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1146 * overloaded for forward iterators and output iterator as result.
1148 template<typename _ForwardIterator
, typename _OutputIterator
>
1150 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1151 _OutputIterator __result
,
1152 forward_iterator_tag
, output_iterator_tag
)
1154 // concept requirements -- taken care of in dispatching function
1155 _ForwardIterator __next
= __first
;
1156 *__result
= *__first
;
1157 while (++__next
!= __last
)
1158 if (!(*__first
== *__next
))
1161 *++__result
= *__first
;
1167 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1169 * overloaded for input iterators and output iterator as result.
1171 template<typename _InputIterator
, typename _OutputIterator
>
1173 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1174 _OutputIterator __result
,
1175 input_iterator_tag
, output_iterator_tag
)
1177 // concept requirements -- taken care of in dispatching function
1178 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1179 *__result
= __value
;
1180 while (++__first
!= __last
)
1181 if (!(__value
== *__first
))
1184 *++__result
= __value
;
1190 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1192 * overloaded for input iterators and forward iterator as result.
1194 template<typename _InputIterator
, typename _ForwardIterator
>
1196 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1197 _ForwardIterator __result
,
1198 input_iterator_tag
, forward_iterator_tag
)
1200 // concept requirements -- taken care of in dispatching function
1201 *__result
= *__first
;
1202 while (++__first
!= __last
)
1203 if (!(*__result
== *__first
))
1204 *++__result
= *__first
;
1209 * This is an uglified
1210 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1212 * overloaded for forward iterators and output iterator as result.
1214 template<typename _ForwardIterator
, typename _OutputIterator
,
1215 typename _BinaryPredicate
>
1217 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1218 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1219 forward_iterator_tag
, output_iterator_tag
)
1221 // concept requirements -- iterators already checked
1222 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1223 typename iterator_traits
<_ForwardIterator
>::value_type
,
1224 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1226 _ForwardIterator __next
= __first
;
1227 *__result
= *__first
;
1228 while (++__next
!= __last
)
1229 if (!bool(__binary_pred(*__first
, *__next
)))
1232 *++__result
= *__first
;
1238 * This is an uglified
1239 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1241 * overloaded for input iterators and output iterator as result.
1243 template<typename _InputIterator
, typename _OutputIterator
,
1244 typename _BinaryPredicate
>
1246 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1247 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1248 input_iterator_tag
, output_iterator_tag
)
1250 // concept requirements -- iterators already checked
1251 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1252 typename iterator_traits
<_InputIterator
>::value_type
,
1253 typename iterator_traits
<_InputIterator
>::value_type
>)
1255 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1256 *__result
= __value
;
1257 while (++__first
!= __last
)
1258 if (!bool(__binary_pred(__value
, *__first
)))
1261 *++__result
= __value
;
1267 * This is an uglified
1268 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1270 * overloaded for input iterators and forward iterator as result.
1272 template<typename _InputIterator
, typename _ForwardIterator
,
1273 typename _BinaryPredicate
>
1275 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1276 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1277 input_iterator_tag
, forward_iterator_tag
)
1279 // concept requirements -- iterators already checked
1280 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1281 typename iterator_traits
<_ForwardIterator
>::value_type
,
1282 typename iterator_traits
<_InputIterator
>::value_type
>)
1284 *__result
= *__first
;
1285 while (++__first
!= __last
)
1286 if (!bool(__binary_pred(*__result
, *__first
)))
1287 *++__result
= *__first
;
1292 * This is an uglified reverse(_BidirectionalIterator,
1293 * _BidirectionalIterator)
1294 * overloaded for bidirectional iterators.
1296 template<typename _BidirectionalIterator
>
1298 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1299 bidirectional_iterator_tag
)
1302 if (__first
== __last
|| __first
== --__last
)
1306 std::iter_swap(__first
, __last
);
1312 * This is an uglified reverse(_BidirectionalIterator,
1313 * _BidirectionalIterator)
1314 * overloaded for random access iterators.
1316 template<typename _RandomAccessIterator
>
1318 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1319 random_access_iterator_tag
)
1321 if (__first
== __last
)
1324 while (__first
< __last
)
1326 std::iter_swap(__first
, __last
);
1333 * @brief Reverse a sequence.
1334 * @param first A bidirectional iterator.
1335 * @param last A bidirectional iterator.
1336 * @return reverse() returns no value.
1338 * Reverses the order of the elements in the range @p [first,last),
1339 * so that the first element becomes the last etc.
1340 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1341 * swaps @p *(first+i) and @p *(last-(i+1))
1343 template<typename _BidirectionalIterator
>
1345 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1347 // concept requirements
1348 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1349 _BidirectionalIterator
>)
1350 __glibcxx_requires_valid_range(__first
, __last
);
1351 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1355 * @brief Copy a sequence, reversing its elements.
1356 * @param first A bidirectional iterator.
1357 * @param last A bidirectional iterator.
1358 * @param result An output iterator.
1359 * @return An iterator designating the end of the resulting sequence.
1361 * Copies the elements in the range @p [first,last) to the range
1362 * @p [result,result+(last-first)) such that the order of the
1363 * elements is reversed.
1364 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1365 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1366 * The ranges @p [first,last) and @p [result,result+(last-first))
1369 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1371 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1372 _OutputIterator __result
)
1374 // concept requirements
1375 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1376 _BidirectionalIterator
>)
1377 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1378 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1379 __glibcxx_requires_valid_range(__first
, __last
);
1381 while (__first
!= __last
)
1384 *__result
= *__last
;
1391 * This is a helper function for the rotate algorithm specialized on RAIs.
1392 * It returns the greatest common divisor of two integer values.
1394 template<typename _EuclideanRingElement
>
1395 _EuclideanRingElement
1396 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1400 _EuclideanRingElement __t
= __m
% __n
;
1407 /// This is a helper function for the rotate algorithm.
1408 template<typename _ForwardIterator
>
1410 __rotate(_ForwardIterator __first
,
1411 _ForwardIterator __middle
,
1412 _ForwardIterator __last
,
1413 forward_iterator_tag
)
1415 if (__first
== __middle
|| __last
== __middle
)
1418 _ForwardIterator __first2
= __middle
;
1421 std::iter_swap(__first
, __first2
);
1424 if (__first
== __middle
)
1425 __middle
= __first2
;
1427 while (__first2
!= __last
);
1429 __first2
= __middle
;
1431 while (__first2
!= __last
)
1433 std::iter_swap(__first
, __first2
);
1436 if (__first
== __middle
)
1437 __middle
= __first2
;
1438 else if (__first2
== __last
)
1439 __first2
= __middle
;
1443 /// This is a helper function for the rotate algorithm.
1444 template<typename _BidirectionalIterator
>
1446 __rotate(_BidirectionalIterator __first
,
1447 _BidirectionalIterator __middle
,
1448 _BidirectionalIterator __last
,
1449 bidirectional_iterator_tag
)
1451 // concept requirements
1452 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1453 _BidirectionalIterator
>)
1455 if (__first
== __middle
|| __last
== __middle
)
1458 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1459 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1461 while (__first
!= __middle
&& __middle
!= __last
)
1463 std::iter_swap(__first
, --__last
);
1467 if (__first
== __middle
)
1468 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1470 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1473 /// This is a helper function for the rotate algorithm.
1474 template<typename _RandomAccessIterator
>
1476 __rotate(_RandomAccessIterator __first
,
1477 _RandomAccessIterator __middle
,
1478 _RandomAccessIterator __last
,
1479 random_access_iterator_tag
)
1481 // concept requirements
1482 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1483 _RandomAccessIterator
>)
1485 if (__first
== __middle
|| __last
== __middle
)
1488 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1490 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1493 const _Distance __n
= __last
- __first
;
1494 const _Distance __k
= __middle
- __first
;
1495 const _Distance __l
= __n
- __k
;
1499 std::swap_ranges(__first
, __middle
, __middle
);
1503 const _Distance __d
= std::__gcd(__n
, __k
);
1505 for (_Distance __i
= 0; __i
< __d
; __i
++)
1507 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1508 _RandomAccessIterator __p
= __first
;
1512 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1514 if (__p
> __first
+ __l
)
1516 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1520 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1526 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1528 if (__p
< __last
- __k
)
1530 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1533 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1538 *__p
= _GLIBCXX_MOVE(__tmp
);
1544 * @brief Rotate the elements of a sequence.
1545 * @param first A forward iterator.
1546 * @param middle A forward iterator.
1547 * @param last A forward iterator.
1550 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1551 * positions so that the element at @p middle is moved to @p first, the
1552 * element at @p middle+1 is moved to @first+1 and so on for each element
1553 * in the range @p [first,last).
1555 * This effectively swaps the ranges @p [first,middle) and
1558 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1559 * each @p n in the range @p [0,last-first).
1561 template<typename _ForwardIterator
>
1563 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1564 _ForwardIterator __last
)
1566 // concept requirements
1567 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1569 __glibcxx_requires_valid_range(__first
, __middle
);
1570 __glibcxx_requires_valid_range(__middle
, __last
);
1572 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1574 std::__rotate(__first
, __middle
, __last
, _IterType());
1578 * @brief Copy a sequence, rotating its elements.
1579 * @param first A forward iterator.
1580 * @param middle A forward iterator.
1581 * @param last A forward iterator.
1582 * @param result An output iterator.
1583 * @return An iterator designating the end of the resulting sequence.
1585 * Copies the elements of the range @p [first,last) to the range
1586 * beginning at @result, rotating the copied elements by @p (middle-first)
1587 * positions so that the element at @p middle is moved to @p result, the
1588 * element at @p middle+1 is moved to @result+1 and so on for each element
1589 * in the range @p [first,last).
1591 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1592 * each @p n in the range @p [0,last-first).
1594 template<typename _ForwardIterator
, typename _OutputIterator
>
1596 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1597 _ForwardIterator __last
, _OutputIterator __result
)
1599 // concept requirements
1600 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1601 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1602 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1603 __glibcxx_requires_valid_range(__first
, __middle
);
1604 __glibcxx_requires_valid_range(__middle
, __last
);
1606 return std::copy(__first
, __middle
,
1607 std::copy(__middle
, __last
, __result
));
1610 /// This is a helper function...
1611 template<typename _ForwardIterator
, typename _Predicate
>
1613 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1614 _Predicate __pred
, forward_iterator_tag
)
1616 if (__first
== __last
)
1619 while (__pred(*__first
))
1620 if (++__first
== __last
)
1623 _ForwardIterator __next
= __first
;
1625 while (++__next
!= __last
)
1626 if (__pred(*__next
))
1628 std::iter_swap(__first
, __next
);
1635 /// This is a helper function...
1636 template<typename _BidirectionalIterator
, typename _Predicate
>
1637 _BidirectionalIterator
1638 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1639 _Predicate __pred
, bidirectional_iterator_tag
)
1644 if (__first
== __last
)
1646 else if (__pred(*__first
))
1652 if (__first
== __last
)
1654 else if (!bool(__pred(*__last
)))
1658 std::iter_swap(__first
, __last
);
1665 /// This is a helper function...
1666 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1668 __inplace_stable_partition(_ForwardIterator __first
,
1669 _ForwardIterator __last
,
1670 _Predicate __pred
, _Distance __len
)
1673 return __pred(*__first
) ? __last
: __first
;
1674 _ForwardIterator __middle
= __first
;
1675 std::advance(__middle
, __len
/ 2);
1676 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1680 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1684 std::rotate(__begin
, __middle
, __end
);
1685 std::advance(__begin
, std::distance(__middle
, __end
));
1689 /// This is a helper function...
1690 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1693 __stable_partition_adaptive(_ForwardIterator __first
,
1694 _ForwardIterator __last
,
1695 _Predicate __pred
, _Distance __len
,
1697 _Distance __buffer_size
)
1699 if (__len
<= __buffer_size
)
1701 _ForwardIterator __result1
= __first
;
1702 _Pointer __result2
= __buffer
;
1703 for (; __first
!= __last
; ++__first
)
1704 if (__pred(*__first
))
1706 *__result1
= *__first
;
1711 *__result2
= *__first
;
1714 std::copy(__buffer
, __result2
, __result1
);
1719 _ForwardIterator __middle
= __first
;
1720 std::advance(__middle
, __len
/ 2);
1721 _ForwardIterator __begin
=
1722 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1723 __len
/ 2, __buffer
,
1725 _ForwardIterator __end
=
1726 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1728 __buffer
, __buffer_size
);
1729 std::rotate(__begin
, __middle
, __end
);
1730 std::advance(__begin
, std::distance(__middle
, __end
));
1736 * @brief Move elements for which a predicate is true to the beginning
1737 * of a sequence, preserving relative ordering.
1738 * @param first A forward iterator.
1739 * @param last A forward iterator.
1740 * @param pred A predicate functor.
1741 * @return An iterator @p middle such that @p pred(i) is true for each
1742 * iterator @p i in the range @p [first,middle) and false for each @p i
1743 * in the range @p [middle,last).
1745 * Performs the same function as @p partition() with the additional
1746 * guarantee that the relative ordering of elements in each group is
1747 * preserved, so any two elements @p x and @p y in the range
1748 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1749 * relative ordering after calling @p stable_partition().
1751 template<typename _ForwardIterator
, typename _Predicate
>
1753 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1756 // concept requirements
1757 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1759 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1760 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1761 __glibcxx_requires_valid_range(__first
, __last
);
1763 if (__first
== __last
)
1767 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1769 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1772 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1774 if (__buf
.size() > 0)
1776 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1777 _DistanceType(__buf
.requested_size()),
1779 _DistanceType(__buf
.size()));
1782 std::__inplace_stable_partition(__first
, __last
, __pred
,
1783 _DistanceType(__buf
.requested_size()));
1787 /// This is a helper function for the sort routines.
1788 template<typename _RandomAccessIterator
>
1790 __heap_select(_RandomAccessIterator __first
,
1791 _RandomAccessIterator __middle
,
1792 _RandomAccessIterator __last
)
1794 std::make_heap(__first
, __middle
);
1795 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1796 if (*__i
< *__first
)
1797 std::__pop_heap(__first
, __middle
, __i
);
1800 /// This is a helper function for the sort routines.
1801 template<typename _RandomAccessIterator
, typename _Compare
>
1803 __heap_select(_RandomAccessIterator __first
,
1804 _RandomAccessIterator __middle
,
1805 _RandomAccessIterator __last
, _Compare __comp
)
1807 std::make_heap(__first
, __middle
, __comp
);
1808 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1809 if (__comp(*__i
, *__first
))
1810 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1816 * @brief Copy the smallest elements of a sequence.
1817 * @param first An iterator.
1818 * @param last Another iterator.
1819 * @param result_first A random-access iterator.
1820 * @param result_last Another random-access iterator.
1821 * @return An iterator indicating the end of the resulting sequence.
1823 * Copies and sorts the smallest N values from the range @p [first,last)
1824 * to the range beginning at @p result_first, where the number of
1825 * elements to be copied, @p N, is the smaller of @p (last-first) and
1826 * @p (result_last-result_first).
1827 * After the sort if @p i and @j are iterators in the range
1828 * @p [result_first,result_first+N) such that @i precedes @j then
1829 * @p *j<*i is false.
1830 * The value returned is @p result_first+N.
1832 template<typename _InputIterator
, typename _RandomAccessIterator
>
1833 _RandomAccessIterator
1834 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1835 _RandomAccessIterator __result_first
,
1836 _RandomAccessIterator __result_last
)
1838 typedef typename iterator_traits
<_InputIterator
>::value_type
1840 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1842 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1845 // concept requirements
1846 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1847 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1849 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1851 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1852 __glibcxx_requires_valid_range(__first
, __last
);
1853 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1855 if (__result_first
== __result_last
)
1856 return __result_last
;
1857 _RandomAccessIterator __result_real_last
= __result_first
;
1858 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1860 *__result_real_last
= *__first
;
1861 ++__result_real_last
;
1864 std::make_heap(__result_first
, __result_real_last
);
1865 while (__first
!= __last
)
1867 if (*__first
< *__result_first
)
1868 std::__adjust_heap(__result_first
, _DistanceType(0),
1869 _DistanceType(__result_real_last
1871 _InputValueType(*__first
));
1874 std::sort_heap(__result_first
, __result_real_last
);
1875 return __result_real_last
;
1879 * @brief Copy the smallest elements of a sequence using a predicate for
1881 * @param first An input iterator.
1882 * @param last Another input iterator.
1883 * @param result_first A random-access iterator.
1884 * @param result_last Another random-access iterator.
1885 * @param comp A comparison functor.
1886 * @return An iterator indicating the end of the resulting sequence.
1888 * Copies and sorts the smallest N values from the range @p [first,last)
1889 * to the range beginning at @p result_first, where the number of
1890 * elements to be copied, @p N, is the smaller of @p (last-first) and
1891 * @p (result_last-result_first).
1892 * After the sort if @p i and @j are iterators in the range
1893 * @p [result_first,result_first+N) such that @i precedes @j then
1894 * @p comp(*j,*i) is false.
1895 * The value returned is @p result_first+N.
1897 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1898 _RandomAccessIterator
1899 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1900 _RandomAccessIterator __result_first
,
1901 _RandomAccessIterator __result_last
,
1904 typedef typename iterator_traits
<_InputIterator
>::value_type
1906 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1908 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1911 // concept requirements
1912 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1913 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1914 _RandomAccessIterator
>)
1915 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1917 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1918 _InputValueType
, _OutputValueType
>)
1919 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1920 _OutputValueType
, _OutputValueType
>)
1921 __glibcxx_requires_valid_range(__first
, __last
);
1922 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1924 if (__result_first
== __result_last
)
1925 return __result_last
;
1926 _RandomAccessIterator __result_real_last
= __result_first
;
1927 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1929 *__result_real_last
= *__first
;
1930 ++__result_real_last
;
1933 std::make_heap(__result_first
, __result_real_last
, __comp
);
1934 while (__first
!= __last
)
1936 if (__comp(*__first
, *__result_first
))
1937 std::__adjust_heap(__result_first
, _DistanceType(0),
1938 _DistanceType(__result_real_last
1940 _InputValueType(*__first
),
1944 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1945 return __result_real_last
;
1948 /// This is a helper function for the sort routine.
1949 template<typename _RandomAccessIterator
, typename _Tp
>
1951 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1953 _RandomAccessIterator __next
= __last
;
1955 while (__val
< *__next
)
1964 /// This is a helper function for the sort routine.
1965 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1967 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1970 _RandomAccessIterator __next
= __last
;
1972 while (__comp(__val
, *__next
))
1981 /// This is a helper function for the sort routine.
1982 template<typename _RandomAccessIterator
>
1984 __insertion_sort(_RandomAccessIterator __first
,
1985 _RandomAccessIterator __last
)
1987 if (__first
== __last
)
1990 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1992 typename iterator_traits
<_RandomAccessIterator
>::value_type
1994 if (__val
< *__first
)
1996 std::copy_backward(__first
, __i
, __i
+ 1);
2000 std::__unguarded_linear_insert(__i
, __val
);
2004 /// This is a helper function for the sort routine.
2005 template<typename _RandomAccessIterator
, typename _Compare
>
2007 __insertion_sort(_RandomAccessIterator __first
,
2008 _RandomAccessIterator __last
, _Compare __comp
)
2010 if (__first
== __last
) return;
2012 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2014 typename iterator_traits
<_RandomAccessIterator
>::value_type
2016 if (__comp(__val
, *__first
))
2018 std::copy_backward(__first
, __i
, __i
+ 1);
2022 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2026 /// This is a helper function for the sort routine.
2027 template<typename _RandomAccessIterator
>
2029 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2030 _RandomAccessIterator __last
)
2032 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2035 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2036 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2039 /// This is a helper function for the sort routine.
2040 template<typename _RandomAccessIterator
, typename _Compare
>
2042 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2043 _RandomAccessIterator __last
, _Compare __comp
)
2045 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2048 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2049 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2054 * This controls some aspect of the sort routines.
2056 enum { _S_threshold
= 16 };
2058 /// This is a helper function for the sort routine.
2059 template<typename _RandomAccessIterator
>
2061 __final_insertion_sort(_RandomAccessIterator __first
,
2062 _RandomAccessIterator __last
)
2064 if (__last
- __first
> int(_S_threshold
))
2066 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2067 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2070 std::__insertion_sort(__first
, __last
);
2073 /// This is a helper function for the sort routine.
2074 template<typename _RandomAccessIterator
, typename _Compare
>
2076 __final_insertion_sort(_RandomAccessIterator __first
,
2077 _RandomAccessIterator __last
, _Compare __comp
)
2079 if (__last
- __first
> int(_S_threshold
))
2081 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2082 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2086 std::__insertion_sort(__first
, __last
, __comp
);
2089 /// This is a helper function...
2090 template<typename _RandomAccessIterator
, typename _Tp
>
2091 _RandomAccessIterator
2092 __unguarded_partition(_RandomAccessIterator __first
,
2093 _RandomAccessIterator __last
, _Tp __pivot
)
2097 while (*__first
< __pivot
)
2100 while (__pivot
< *__last
)
2102 if (!(__first
< __last
))
2104 std::iter_swap(__first
, __last
);
2109 /// This is a helper function...
2110 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2111 _RandomAccessIterator
2112 __unguarded_partition(_RandomAccessIterator __first
,
2113 _RandomAccessIterator __last
,
2114 _Tp __pivot
, _Compare __comp
)
2118 while (__comp(*__first
, __pivot
))
2121 while (__comp(__pivot
, *__last
))
2123 if (!(__first
< __last
))
2125 std::iter_swap(__first
, __last
);
2130 /// This is a helper function for the sort routine.
2131 template<typename _RandomAccessIterator
, typename _Size
>
2133 __introsort_loop(_RandomAccessIterator __first
,
2134 _RandomAccessIterator __last
,
2135 _Size __depth_limit
)
2137 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2140 while (__last
- __first
> int(_S_threshold
))
2142 if (__depth_limit
== 0)
2144 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2148 _RandomAccessIterator __cut
=
2149 std::__unguarded_partition(__first
, __last
,
2150 _ValueType(std::__median(*__first
,
2157 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2162 /// This is a helper function for the sort routine.
2163 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2165 __introsort_loop(_RandomAccessIterator __first
,
2166 _RandomAccessIterator __last
,
2167 _Size __depth_limit
, _Compare __comp
)
2169 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2172 while (__last
- __first
> int(_S_threshold
))
2174 if (__depth_limit
== 0)
2176 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2180 _RandomAccessIterator __cut
=
2181 std::__unguarded_partition(__first
, __last
,
2182 _ValueType(std::__median(*__first
,
2190 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2195 /// This is a helper function for the sort routines. Precondition: __n > 0.
2196 template<typename _Size
>
2201 for (__k
= 0; __n
!= 0; __n
>>= 1)
2208 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2212 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2216 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2220 template<typename _RandomAccessIterator
, typename _Size
>
2222 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2223 _RandomAccessIterator __last
, _Size __depth_limit
)
2225 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2228 while (__last
- __first
> 3)
2230 if (__depth_limit
== 0)
2232 std::__heap_select(__first
, __nth
+ 1, __last
);
2234 // Place the nth largest element in its final position.
2235 std::iter_swap(__first
, __nth
);
2239 _RandomAccessIterator __cut
=
2240 std::__unguarded_partition(__first
, __last
,
2241 _ValueType(std::__median(*__first
,
2253 std::__insertion_sort(__first
, __last
);
2256 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2258 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2259 _RandomAccessIterator __last
, _Size __depth_limit
,
2262 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2265 while (__last
- __first
> 3)
2267 if (__depth_limit
== 0)
2269 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2270 // Place the nth largest element in its final position.
2271 std::iter_swap(__first
, __nth
);
2275 _RandomAccessIterator __cut
=
2276 std::__unguarded_partition(__first
, __last
,
2277 _ValueType(std::__median(*__first
,
2290 std::__insertion_sort(__first
, __last
, __comp
);
2296 * @brief Finds the first position in which @a val could be inserted
2297 * without changing the ordering.
2298 * @param first An iterator.
2299 * @param last Another iterator.
2300 * @param val The search term.
2301 * @return An iterator pointing to the first element "not less
2302 * than" @a val, or end() if every element is less than
2304 * @ingroup binarysearch
2306 template<typename _ForwardIterator
, typename _Tp
>
2308 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2311 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2313 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2316 // concept requirements
2317 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2318 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2319 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2321 _DistanceType __len
= std::distance(__first
, __last
);
2322 _DistanceType __half
;
2323 _ForwardIterator __middle
;
2327 __half
= __len
>> 1;
2329 std::advance(__middle
, __half
);
2330 if (*__middle
< __val
)
2334 __len
= __len
- __half
- 1;
2343 * @brief Finds the first position in which @a val could be inserted
2344 * without changing the ordering.
2345 * @param first An iterator.
2346 * @param last Another iterator.
2347 * @param val The search term.
2348 * @param comp A functor to use for comparisons.
2349 * @return An iterator pointing to the first element "not less than" @a val,
2350 * or end() if every element is less than @a val.
2351 * @ingroup binarysearch
2353 * The comparison function should have the same effects on ordering as
2354 * the function used for the initial sort.
2356 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2358 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2359 const _Tp
& __val
, _Compare __comp
)
2361 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2363 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2366 // concept requirements
2367 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2368 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2370 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2373 _DistanceType __len
= std::distance(__first
, __last
);
2374 _DistanceType __half
;
2375 _ForwardIterator __middle
;
2379 __half
= __len
>> 1;
2381 std::advance(__middle
, __half
);
2382 if (__comp(*__middle
, __val
))
2386 __len
= __len
- __half
- 1;
2395 * @brief Finds the last position in which @a val could be inserted
2396 * without changing the ordering.
2397 * @param first An iterator.
2398 * @param last Another iterator.
2399 * @param val The search term.
2400 * @return An iterator pointing to the first element greater than @a val,
2401 * or end() if no elements are greater than @a val.
2402 * @ingroup binarysearch
2404 template<typename _ForwardIterator
, typename _Tp
>
2406 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2409 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2411 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2414 // concept requirements
2415 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2416 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2417 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2419 _DistanceType __len
= std::distance(__first
, __last
);
2420 _DistanceType __half
;
2421 _ForwardIterator __middle
;
2425 __half
= __len
>> 1;
2427 std::advance(__middle
, __half
);
2428 if (__val
< *__middle
)
2434 __len
= __len
- __half
- 1;
2441 * @brief Finds the last position in which @a val could be inserted
2442 * without changing the ordering.
2443 * @param first An iterator.
2444 * @param last Another iterator.
2445 * @param val The search term.
2446 * @param comp A functor to use for comparisons.
2447 * @return An iterator pointing to the first element greater than @a val,
2448 * or end() if no elements are greater than @a val.
2449 * @ingroup binarysearch
2451 * The comparison function should have the same effects on ordering as
2452 * the function used for the initial sort.
2454 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2456 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2457 const _Tp
& __val
, _Compare __comp
)
2459 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2461 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2464 // concept requirements
2465 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2466 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2468 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2471 _DistanceType __len
= std::distance(__first
, __last
);
2472 _DistanceType __half
;
2473 _ForwardIterator __middle
;
2477 __half
= __len
>> 1;
2479 std::advance(__middle
, __half
);
2480 if (__comp(__val
, *__middle
))
2486 __len
= __len
- __half
- 1;
2493 * @brief Finds the largest subrange in which @a val could be inserted
2494 * at any place in it without changing the ordering.
2495 * @param first An iterator.
2496 * @param last Another iterator.
2497 * @param val The search term.
2498 * @return An pair of iterators defining the subrange.
2499 * @ingroup binarysearch
2501 * This is equivalent to
2503 * std::make_pair(lower_bound(first, last, val),
2504 * upper_bound(first, last, val))
2506 * but does not actually call those functions.
2508 template<typename _ForwardIterator
, typename _Tp
>
2509 pair
<_ForwardIterator
, _ForwardIterator
>
2510 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2513 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2515 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2518 // concept requirements
2519 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2520 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2521 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2522 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2523 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2525 _DistanceType __len
= std::distance(__first
, __last
);
2526 _DistanceType __half
;
2527 _ForwardIterator __middle
, __left
, __right
;
2531 __half
= __len
>> 1;
2533 std::advance(__middle
, __half
);
2534 if (*__middle
< __val
)
2538 __len
= __len
- __half
- 1;
2540 else if (__val
< *__middle
)
2544 __left
= std::lower_bound(__first
, __middle
, __val
);
2545 std::advance(__first
, __len
);
2546 __right
= std::upper_bound(++__middle
, __first
, __val
);
2547 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2550 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2554 * @brief Finds the largest subrange in which @a val could be inserted
2555 * at any place in it without changing the ordering.
2556 * @param first An iterator.
2557 * @param last Another iterator.
2558 * @param val The search term.
2559 * @param comp A functor to use for comparisons.
2560 * @return An pair of iterators defining the subrange.
2561 * @ingroup binarysearch
2563 * This is equivalent to
2565 * std::make_pair(lower_bound(first, last, val, comp),
2566 * upper_bound(first, last, val, comp))
2568 * but does not actually call those functions.
2570 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2571 pair
<_ForwardIterator
, _ForwardIterator
>
2572 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2576 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2578 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2581 // concept requirements
2582 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2583 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2585 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2587 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2589 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2592 _DistanceType __len
= std::distance(__first
, __last
);
2593 _DistanceType __half
;
2594 _ForwardIterator __middle
, __left
, __right
;
2598 __half
= __len
>> 1;
2600 std::advance(__middle
, __half
);
2601 if (__comp(*__middle
, __val
))
2605 __len
= __len
- __half
- 1;
2607 else if (__comp(__val
, *__middle
))
2611 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2612 std::advance(__first
, __len
);
2613 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2614 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2617 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2621 * @brief Determines whether an element exists in a range.
2622 * @param first An iterator.
2623 * @param last Another iterator.
2624 * @param val The search term.
2625 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2626 * @ingroup binarysearch
2628 * Note that this does not actually return an iterator to @a val. For
2629 * that, use std::find or a container's specialized find member functions.
2631 template<typename _ForwardIterator
, typename _Tp
>
2633 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2636 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2639 // concept requirements
2640 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2641 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2642 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2643 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2645 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2646 return __i
!= __last
&& !(__val
< *__i
);
2650 * @brief Determines whether an element exists in a range.
2651 * @param first An iterator.
2652 * @param last Another iterator.
2653 * @param val The search term.
2654 * @param comp A functor to use for comparisons.
2655 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2656 * @ingroup binarysearch
2658 * Note that this does not actually return an iterator to @a val. For
2659 * that, use std::find or a container's specialized find member functions.
2661 * The comparison function should have the same effects on ordering as
2662 * the function used for the initial sort.
2664 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2666 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2667 const _Tp
& __val
, _Compare __comp
)
2669 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2672 // concept requirements
2673 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2674 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2676 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2678 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2681 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2682 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2687 /// This is a helper function for the merge routines.
2688 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2689 typename _BidirectionalIterator3
>
2690 _BidirectionalIterator3
2691 __merge_backward(_BidirectionalIterator1 __first1
,
2692 _BidirectionalIterator1 __last1
,
2693 _BidirectionalIterator2 __first2
,
2694 _BidirectionalIterator2 __last2
,
2695 _BidirectionalIterator3 __result
)
2697 if (__first1
== __last1
)
2698 return std::copy_backward(__first2
, __last2
, __result
);
2699 if (__first2
== __last2
)
2700 return std::copy_backward(__first1
, __last1
, __result
);
2705 if (*__last2
< *__last1
)
2707 *--__result
= *__last1
;
2708 if (__first1
== __last1
)
2709 return std::copy_backward(__first2
, ++__last2
, __result
);
2714 *--__result
= *__last2
;
2715 if (__first2
== __last2
)
2716 return std::copy_backward(__first1
, ++__last1
, __result
);
2722 /// This is a helper function for the merge routines.
2723 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2724 typename _BidirectionalIterator3
, typename _Compare
>
2725 _BidirectionalIterator3
2726 __merge_backward(_BidirectionalIterator1 __first1
,
2727 _BidirectionalIterator1 __last1
,
2728 _BidirectionalIterator2 __first2
,
2729 _BidirectionalIterator2 __last2
,
2730 _BidirectionalIterator3 __result
,
2733 if (__first1
== __last1
)
2734 return std::copy_backward(__first2
, __last2
, __result
);
2735 if (__first2
== __last2
)
2736 return std::copy_backward(__first1
, __last1
, __result
);
2741 if (__comp(*__last2
, *__last1
))
2743 *--__result
= *__last1
;
2744 if (__first1
== __last1
)
2745 return std::copy_backward(__first2
, ++__last2
, __result
);
2750 *--__result
= *__last2
;
2751 if (__first2
== __last2
)
2752 return std::copy_backward(__first1
, ++__last1
, __result
);
2758 /// This is a helper function for the merge routines.
2759 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2761 _BidirectionalIterator1
2762 __rotate_adaptive(_BidirectionalIterator1 __first
,
2763 _BidirectionalIterator1 __middle
,
2764 _BidirectionalIterator1 __last
,
2765 _Distance __len1
, _Distance __len2
,
2766 _BidirectionalIterator2 __buffer
,
2767 _Distance __buffer_size
)
2769 _BidirectionalIterator2 __buffer_end
;
2770 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2772 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2773 std::copy_backward(__first
, __middle
, __last
);
2774 return std::copy(__buffer
, __buffer_end
, __first
);
2776 else if (__len1
<= __buffer_size
)
2778 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2779 std::copy(__middle
, __last
, __first
);
2780 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2784 std::rotate(__first
, __middle
, __last
);
2785 std::advance(__first
, std::distance(__middle
, __last
));
2790 /// This is a helper function for the merge routines.
2791 template<typename _BidirectionalIterator
, typename _Distance
,
2794 __merge_adaptive(_BidirectionalIterator __first
,
2795 _BidirectionalIterator __middle
,
2796 _BidirectionalIterator __last
,
2797 _Distance __len1
, _Distance __len2
,
2798 _Pointer __buffer
, _Distance __buffer_size
)
2800 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2802 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2803 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2806 else if (__len2
<= __buffer_size
)
2808 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2809 std::__merge_backward(__first
, __middle
, __buffer
,
2810 __buffer_end
, __last
);
2814 _BidirectionalIterator __first_cut
= __first
;
2815 _BidirectionalIterator __second_cut
= __middle
;
2816 _Distance __len11
= 0;
2817 _Distance __len22
= 0;
2818 if (__len1
> __len2
)
2820 __len11
= __len1
/ 2;
2821 std::advance(__first_cut
, __len11
);
2822 __second_cut
= std::lower_bound(__middle
, __last
,
2824 __len22
= std::distance(__middle
, __second_cut
);
2828 __len22
= __len2
/ 2;
2829 std::advance(__second_cut
, __len22
);
2830 __first_cut
= std::upper_bound(__first
, __middle
,
2832 __len11
= std::distance(__first
, __first_cut
);
2834 _BidirectionalIterator __new_middle
=
2835 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2836 __len1
- __len11
, __len22
, __buffer
,
2838 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2839 __len22
, __buffer
, __buffer_size
);
2840 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2842 __len2
- __len22
, __buffer
, __buffer_size
);
2846 /// This is a helper function for the merge routines.
2847 template<typename _BidirectionalIterator
, typename _Distance
,
2848 typename _Pointer
, typename _Compare
>
2850 __merge_adaptive(_BidirectionalIterator __first
,
2851 _BidirectionalIterator __middle
,
2852 _BidirectionalIterator __last
,
2853 _Distance __len1
, _Distance __len2
,
2854 _Pointer __buffer
, _Distance __buffer_size
,
2857 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2859 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2860 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2863 else if (__len2
<= __buffer_size
)
2865 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2866 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2871 _BidirectionalIterator __first_cut
= __first
;
2872 _BidirectionalIterator __second_cut
= __middle
;
2873 _Distance __len11
= 0;
2874 _Distance __len22
= 0;
2875 if (__len1
> __len2
)
2877 __len11
= __len1
/ 2;
2878 std::advance(__first_cut
, __len11
);
2879 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2881 __len22
= std::distance(__middle
, __second_cut
);
2885 __len22
= __len2
/ 2;
2886 std::advance(__second_cut
, __len22
);
2887 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2889 __len11
= std::distance(__first
, __first_cut
);
2891 _BidirectionalIterator __new_middle
=
2892 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2893 __len1
- __len11
, __len22
, __buffer
,
2895 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2896 __len22
, __buffer
, __buffer_size
, __comp
);
2897 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2899 __len2
- __len22
, __buffer
,
2900 __buffer_size
, __comp
);
2904 /// This is a helper function for the merge routines.
2905 template<typename _BidirectionalIterator
, typename _Distance
>
2907 __merge_without_buffer(_BidirectionalIterator __first
,
2908 _BidirectionalIterator __middle
,
2909 _BidirectionalIterator __last
,
2910 _Distance __len1
, _Distance __len2
)
2912 if (__len1
== 0 || __len2
== 0)
2914 if (__len1
+ __len2
== 2)
2916 if (*__middle
< *__first
)
2917 std::iter_swap(__first
, __middle
);
2920 _BidirectionalIterator __first_cut
= __first
;
2921 _BidirectionalIterator __second_cut
= __middle
;
2922 _Distance __len11
= 0;
2923 _Distance __len22
= 0;
2924 if (__len1
> __len2
)
2926 __len11
= __len1
/ 2;
2927 std::advance(__first_cut
, __len11
);
2928 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2929 __len22
= std::distance(__middle
, __second_cut
);
2933 __len22
= __len2
/ 2;
2934 std::advance(__second_cut
, __len22
);
2935 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2936 __len11
= std::distance(__first
, __first_cut
);
2938 std::rotate(__first_cut
, __middle
, __second_cut
);
2939 _BidirectionalIterator __new_middle
= __first_cut
;
2940 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2941 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2943 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2944 __len1
- __len11
, __len2
- __len22
);
2947 /// This is a helper function for the merge routines.
2948 template<typename _BidirectionalIterator
, typename _Distance
,
2951 __merge_without_buffer(_BidirectionalIterator __first
,
2952 _BidirectionalIterator __middle
,
2953 _BidirectionalIterator __last
,
2954 _Distance __len1
, _Distance __len2
,
2957 if (__len1
== 0 || __len2
== 0)
2959 if (__len1
+ __len2
== 2)
2961 if (__comp(*__middle
, *__first
))
2962 std::iter_swap(__first
, __middle
);
2965 _BidirectionalIterator __first_cut
= __first
;
2966 _BidirectionalIterator __second_cut
= __middle
;
2967 _Distance __len11
= 0;
2968 _Distance __len22
= 0;
2969 if (__len1
> __len2
)
2971 __len11
= __len1
/ 2;
2972 std::advance(__first_cut
, __len11
);
2973 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2975 __len22
= std::distance(__middle
, __second_cut
);
2979 __len22
= __len2
/ 2;
2980 std::advance(__second_cut
, __len22
);
2981 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2983 __len11
= std::distance(__first
, __first_cut
);
2985 std::rotate(__first_cut
, __middle
, __second_cut
);
2986 _BidirectionalIterator __new_middle
= __first_cut
;
2987 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2988 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2989 __len11
, __len22
, __comp
);
2990 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2991 __len1
- __len11
, __len2
- __len22
, __comp
);
2995 * @brief Merges two sorted ranges in place.
2996 * @param first An iterator.
2997 * @param middle Another iterator.
2998 * @param last Another iterator.
3001 * Merges two sorted and consecutive ranges, [first,middle) and
3002 * [middle,last), and puts the result in [first,last). The output will
3003 * be sorted. The sort is @e stable, that is, for equivalent
3004 * elements in the two ranges, elements from the first range will always
3005 * come before elements from the second.
3007 * If enough additional memory is available, this takes (last-first)-1
3008 * comparisons. Otherwise an NlogN algorithm is used, where N is
3009 * distance(first,last).
3011 template<typename _BidirectionalIterator
>
3013 inplace_merge(_BidirectionalIterator __first
,
3014 _BidirectionalIterator __middle
,
3015 _BidirectionalIterator __last
)
3017 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3019 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3022 // concept requirements
3023 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3024 _BidirectionalIterator
>)
3025 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3026 __glibcxx_requires_sorted(__first
, __middle
);
3027 __glibcxx_requires_sorted(__middle
, __last
);
3029 if (__first
== __middle
|| __middle
== __last
)
3032 _DistanceType __len1
= std::distance(__first
, __middle
);
3033 _DistanceType __len2
= std::distance(__middle
, __last
);
3035 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3037 if (__buf
.begin() == 0)
3038 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3040 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3041 __buf
.begin(), _DistanceType(__buf
.size()));
3045 * @brief Merges two sorted ranges in place.
3046 * @param first An iterator.
3047 * @param middle Another iterator.
3048 * @param last Another iterator.
3049 * @param comp A functor to use for comparisons.
3052 * Merges two sorted and consecutive ranges, [first,middle) and
3053 * [middle,last), and puts the result in [first,last). The output will
3054 * be sorted. The sort is @e stable, that is, for equivalent
3055 * elements in the two ranges, elements from the first range will always
3056 * come before elements from the second.
3058 * If enough additional memory is available, this takes (last-first)-1
3059 * comparisons. Otherwise an NlogN algorithm is used, where N is
3060 * distance(first,last).
3062 * The comparison function should have the same effects on ordering as
3063 * the function used for the initial sort.
3065 template<typename _BidirectionalIterator
, typename _Compare
>
3067 inplace_merge(_BidirectionalIterator __first
,
3068 _BidirectionalIterator __middle
,
3069 _BidirectionalIterator __last
,
3072 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3074 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3077 // concept requirements
3078 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3079 _BidirectionalIterator
>)
3080 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3081 _ValueType
, _ValueType
>)
3082 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3083 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3085 if (__first
== __middle
|| __middle
== __last
)
3088 const _DistanceType __len1
= std::distance(__first
, __middle
);
3089 const _DistanceType __len2
= std::distance(__middle
, __last
);
3091 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3093 if (__buf
.begin() == 0)
3094 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3097 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3098 __buf
.begin(), _DistanceType(__buf
.size()),
3102 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3105 __merge_sort_loop(_RandomAccessIterator1 __first
,
3106 _RandomAccessIterator1 __last
,
3107 _RandomAccessIterator2 __result
,
3108 _Distance __step_size
)
3110 const _Distance __two_step
= 2 * __step_size
;
3112 while (__last
- __first
>= __two_step
)
3114 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3115 __first
+ __step_size
,
3116 __first
+ __two_step
,
3118 __first
+= __two_step
;
3121 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3122 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3123 __first
+ __step_size
, __last
,
3127 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3128 typename _Distance
, typename _Compare
>
3130 __merge_sort_loop(_RandomAccessIterator1 __first
,
3131 _RandomAccessIterator1 __last
,
3132 _RandomAccessIterator2 __result
, _Distance __step_size
,
3135 const _Distance __two_step
= 2 * __step_size
;
3137 while (__last
- __first
>= __two_step
)
3139 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3140 __first
+ __step_size
, __first
+ __two_step
,
3143 __first
+= __two_step
;
3145 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3147 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3148 __first
+ __step_size
, __last
, __result
, __comp
);
3151 template<typename _RandomAccessIterator
, typename _Distance
>
3153 __chunk_insertion_sort(_RandomAccessIterator __first
,
3154 _RandomAccessIterator __last
,
3155 _Distance __chunk_size
)
3157 while (__last
- __first
>= __chunk_size
)
3159 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3160 __first
+= __chunk_size
;
3162 std::__insertion_sort(__first
, __last
);
3165 template<typename _RandomAccessIterator
, typename _Distance
,
3168 __chunk_insertion_sort(_RandomAccessIterator __first
,
3169 _RandomAccessIterator __last
,
3170 _Distance __chunk_size
, _Compare __comp
)
3172 while (__last
- __first
>= __chunk_size
)
3174 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3175 __first
+= __chunk_size
;
3177 std::__insertion_sort(__first
, __last
, __comp
);
3180 enum { _S_chunk_size
= 7 };
3182 template<typename _RandomAccessIterator
, typename _Pointer
>
3184 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3185 _RandomAccessIterator __last
,
3188 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3191 const _Distance __len
= __last
- __first
;
3192 const _Pointer __buffer_last
= __buffer
+ __len
;
3194 _Distance __step_size
= _S_chunk_size
;
3195 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3197 while (__step_size
< __len
)
3199 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3201 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3206 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3208 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3209 _RandomAccessIterator __last
,
3210 _Pointer __buffer
, _Compare __comp
)
3212 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3215 const _Distance __len
= __last
- __first
;
3216 const _Pointer __buffer_last
= __buffer
+ __len
;
3218 _Distance __step_size
= _S_chunk_size
;
3219 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3221 while (__step_size
< __len
)
3223 std::__merge_sort_loop(__first
, __last
, __buffer
,
3224 __step_size
, __comp
);
3226 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3227 __step_size
, __comp
);
3232 template<typename _RandomAccessIterator
, typename _Pointer
,
3235 __stable_sort_adaptive(_RandomAccessIterator __first
,
3236 _RandomAccessIterator __last
,
3237 _Pointer __buffer
, _Distance __buffer_size
)
3239 const _Distance __len
= (__last
- __first
+ 1) / 2;
3240 const _RandomAccessIterator __middle
= __first
+ __len
;
3241 if (__len
> __buffer_size
)
3243 std::__stable_sort_adaptive(__first
, __middle
,
3244 __buffer
, __buffer_size
);
3245 std::__stable_sort_adaptive(__middle
, __last
,
3246 __buffer
, __buffer_size
);
3250 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3251 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3253 std::__merge_adaptive(__first
, __middle
, __last
,
3254 _Distance(__middle
- __first
),
3255 _Distance(__last
- __middle
),
3256 __buffer
, __buffer_size
);
3259 template<typename _RandomAccessIterator
, typename _Pointer
,
3260 typename _Distance
, typename _Compare
>
3262 __stable_sort_adaptive(_RandomAccessIterator __first
,
3263 _RandomAccessIterator __last
,
3264 _Pointer __buffer
, _Distance __buffer_size
,
3267 const _Distance __len
= (__last
- __first
+ 1) / 2;
3268 const _RandomAccessIterator __middle
= __first
+ __len
;
3269 if (__len
> __buffer_size
)
3271 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3272 __buffer_size
, __comp
);
3273 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3274 __buffer_size
, __comp
);
3278 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3279 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3281 std::__merge_adaptive(__first
, __middle
, __last
,
3282 _Distance(__middle
- __first
),
3283 _Distance(__last
- __middle
),
3284 __buffer
, __buffer_size
,
3288 /// This is a helper function for the stable sorting routines.
3289 template<typename _RandomAccessIterator
>
3291 __inplace_stable_sort(_RandomAccessIterator __first
,
3292 _RandomAccessIterator __last
)
3294 if (__last
- __first
< 15)
3296 std::__insertion_sort(__first
, __last
);
3299 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3300 std::__inplace_stable_sort(__first
, __middle
);
3301 std::__inplace_stable_sort(__middle
, __last
);
3302 std::__merge_without_buffer(__first
, __middle
, __last
,
3307 /// This is a helper function for the stable sorting routines.
3308 template<typename _RandomAccessIterator
, typename _Compare
>
3310 __inplace_stable_sort(_RandomAccessIterator __first
,
3311 _RandomAccessIterator __last
, _Compare __comp
)
3313 if (__last
- __first
< 15)
3315 std::__insertion_sort(__first
, __last
, __comp
);
3318 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3319 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3320 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3321 std::__merge_without_buffer(__first
, __middle
, __last
,
3329 // Set algorithms: includes, set_union, set_intersection, set_difference,
3330 // set_symmetric_difference. All of these algorithms have the precondition
3331 // that their input ranges are sorted and the postcondition that their output
3332 // ranges are sorted.
3335 * @brief Determines whether all elements of a sequence exists in a range.
3336 * @param first1 Start of search range.
3337 * @param last1 End of search range.
3338 * @param first2 Start of sequence
3339 * @param last2 End of sequence.
3340 * @return True if each element in [first2,last2) is contained in order
3341 * within [first1,last1). False otherwise.
3342 * @ingroup setoperations
3344 * This operation expects both [first1,last1) and [first2,last2) to be
3345 * sorted. Searches for the presence of each element in [first2,last2)
3346 * within [first1,last1). The iterators over each range only move forward,
3347 * so this is a linear algorithm. If an element in [first2,last2) is not
3348 * found before the search iterator reaches @a last2, false is returned.
3350 template<typename _InputIterator1
, typename _InputIterator2
>
3352 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3353 _InputIterator2 __first2
, _InputIterator2 __last2
)
3355 typedef typename iterator_traits
<_InputIterator1
>::value_type
3357 typedef typename iterator_traits
<_InputIterator2
>::value_type
3360 // concept requirements
3361 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3362 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3363 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3364 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3365 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3366 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3368 while (__first1
!= __last1
&& __first2
!= __last2
)
3369 if (*__first2
< *__first1
)
3371 else if(*__first1
< *__first2
)
3374 ++__first1
, ++__first2
;
3376 return __first2
== __last2
;
3380 * @brief Determines whether all elements of a sequence exists in a range
3382 * @param first1 Start of search range.
3383 * @param last1 End of search range.
3384 * @param first2 Start of sequence
3385 * @param last2 End of sequence.
3386 * @param comp Comparison function to use.
3387 * @return True if each element in [first2,last2) is contained in order
3388 * within [first1,last1) according to comp. False otherwise.
3389 * @ingroup setoperations
3391 * This operation expects both [first1,last1) and [first2,last2) to be
3392 * sorted. Searches for the presence of each element in [first2,last2)
3393 * within [first1,last1), using comp to decide. The iterators over each
3394 * range only move forward, so this is a linear algorithm. If an element
3395 * in [first2,last2) is not found before the search iterator reaches @a
3396 * last2, false is returned.
3398 template<typename _InputIterator1
, typename _InputIterator2
,
3401 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3402 _InputIterator2 __first2
, _InputIterator2 __last2
,
3405 typedef typename iterator_traits
<_InputIterator1
>::value_type
3407 typedef typename iterator_traits
<_InputIterator2
>::value_type
3410 // concept requirements
3411 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3412 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3413 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3414 _ValueType1
, _ValueType2
>)
3415 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3416 _ValueType2
, _ValueType1
>)
3417 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3418 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3420 while (__first1
!= __last1
&& __first2
!= __last2
)
3421 if (__comp(*__first2
, *__first1
))
3423 else if(__comp(*__first1
, *__first2
))
3426 ++__first1
, ++__first2
;
3428 return __first2
== __last2
;
3437 // set_symmetric_difference
3442 * @brief Permute range into the next "dictionary" ordering.
3443 * @param first Start of range.
3444 * @param last End of range.
3445 * @return False if wrapped to first permutation, true otherwise.
3447 * Treats all permutations of the range as a set of "dictionary" sorted
3448 * sequences. Permutes the current sequence into the next one of this set.
3449 * Returns true if there are more sequences to generate. If the sequence
3450 * is the largest of the set, the smallest is generated and false returned.
3452 template<typename _BidirectionalIterator
>
3454 next_permutation(_BidirectionalIterator __first
,
3455 _BidirectionalIterator __last
)
3457 // concept requirements
3458 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3459 _BidirectionalIterator
>)
3460 __glibcxx_function_requires(_LessThanComparableConcept
<
3461 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3462 __glibcxx_requires_valid_range(__first
, __last
);
3464 if (__first
== __last
)
3466 _BidirectionalIterator __i
= __first
;
3475 _BidirectionalIterator __ii
= __i
;
3479 _BidirectionalIterator __j
= __last
;
3480 while (!(*__i
< *--__j
))
3482 std::iter_swap(__i
, __j
);
3483 std::reverse(__ii
, __last
);
3488 std::reverse(__first
, __last
);
3495 * @brief Permute range into the next "dictionary" ordering using
3496 * comparison functor.
3497 * @param first Start of range.
3498 * @param last End of range.
3499 * @param comp A comparison functor.
3500 * @return False if wrapped to first permutation, true otherwise.
3502 * Treats all permutations of the range [first,last) as a set of
3503 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3504 * sequence into the next one of this set. Returns true if there are more
3505 * sequences to generate. If the sequence is the largest of the set, the
3506 * smallest is generated and false returned.
3508 template<typename _BidirectionalIterator
, typename _Compare
>
3510 next_permutation(_BidirectionalIterator __first
,
3511 _BidirectionalIterator __last
, _Compare __comp
)
3513 // concept requirements
3514 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3515 _BidirectionalIterator
>)
3516 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3517 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3518 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3519 __glibcxx_requires_valid_range(__first
, __last
);
3521 if (__first
== __last
)
3523 _BidirectionalIterator __i
= __first
;
3532 _BidirectionalIterator __ii
= __i
;
3534 if (__comp(*__i
, *__ii
))
3536 _BidirectionalIterator __j
= __last
;
3537 while (!bool(__comp(*__i
, *--__j
)))
3539 std::iter_swap(__i
, __j
);
3540 std::reverse(__ii
, __last
);
3545 std::reverse(__first
, __last
);
3552 * @brief Permute range into the previous "dictionary" ordering.
3553 * @param first Start of range.
3554 * @param last End of range.
3555 * @return False if wrapped to last permutation, true otherwise.
3557 * Treats all permutations of the range as a set of "dictionary" sorted
3558 * sequences. Permutes the current sequence into the previous one of this
3559 * set. Returns true if there are more sequences to generate. If the
3560 * sequence is the smallest of the set, the largest is generated and false
3563 template<typename _BidirectionalIterator
>
3565 prev_permutation(_BidirectionalIterator __first
,
3566 _BidirectionalIterator __last
)
3568 // concept requirements
3569 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3570 _BidirectionalIterator
>)
3571 __glibcxx_function_requires(_LessThanComparableConcept
<
3572 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3573 __glibcxx_requires_valid_range(__first
, __last
);
3575 if (__first
== __last
)
3577 _BidirectionalIterator __i
= __first
;
3586 _BidirectionalIterator __ii
= __i
;
3590 _BidirectionalIterator __j
= __last
;
3591 while (!(*--__j
< *__i
))
3593 std::iter_swap(__i
, __j
);
3594 std::reverse(__ii
, __last
);
3599 std::reverse(__first
, __last
);
3606 * @brief Permute range into the previous "dictionary" ordering using
3607 * comparison functor.
3608 * @param first Start of range.
3609 * @param last End of range.
3610 * @param comp A comparison functor.
3611 * @return False if wrapped to last permutation, true otherwise.
3613 * Treats all permutations of the range [first,last) as a set of
3614 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3615 * sequence into the previous one of this set. Returns true if there are
3616 * more sequences to generate. If the sequence is the smallest of the set,
3617 * the largest is generated and false returned.
3619 template<typename _BidirectionalIterator
, typename _Compare
>
3621 prev_permutation(_BidirectionalIterator __first
,
3622 _BidirectionalIterator __last
, _Compare __comp
)
3624 // concept requirements
3625 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3626 _BidirectionalIterator
>)
3627 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3628 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3629 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3630 __glibcxx_requires_valid_range(__first
, __last
);
3632 if (__first
== __last
)
3634 _BidirectionalIterator __i
= __first
;
3643 _BidirectionalIterator __ii
= __i
;
3645 if (__comp(*__ii
, *__i
))
3647 _BidirectionalIterator __j
= __last
;
3648 while (!bool(__comp(*--__j
, *__i
)))
3650 std::iter_swap(__i
, __j
);
3651 std::reverse(__ii
, __last
);
3656 std::reverse(__first
, __last
);
3666 * @brief Copy a sequence, replacing each element of one value with another
3668 * @param first An input iterator.
3669 * @param last An input iterator.
3670 * @param result An output iterator.
3671 * @param old_value The value to be replaced.
3672 * @param new_value The replacement value.
3673 * @return The end of the output sequence, @p result+(last-first).
3675 * Copies each element in the input range @p [first,last) to the
3676 * output range @p [result,result+(last-first)) replacing elements
3677 * equal to @p old_value with @p new_value.
3679 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3681 replace_copy(_InputIterator __first
, _InputIterator __last
,
3682 _OutputIterator __result
,
3683 const _Tp
& __old_value
, const _Tp
& __new_value
)
3685 // concept requirements
3686 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3687 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3688 typename iterator_traits
<_InputIterator
>::value_type
>)
3689 __glibcxx_function_requires(_EqualOpConcept
<
3690 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3691 __glibcxx_requires_valid_range(__first
, __last
);
3693 for (; __first
!= __last
; ++__first
, ++__result
)
3694 if (*__first
== __old_value
)
3695 *__result
= __new_value
;
3697 *__result
= *__first
;
3702 * @brief Copy a sequence, replacing each value for which a predicate
3703 * returns true with another value.
3704 * @param first An input iterator.
3705 * @param last An input iterator.
3706 * @param result An output iterator.
3707 * @param pred A predicate.
3708 * @param new_value The replacement value.
3709 * @return The end of the output sequence, @p result+(last-first).
3711 * Copies each element in the range @p [first,last) to the range
3712 * @p [result,result+(last-first)) replacing elements for which
3713 * @p pred returns true with @p new_value.
3715 template<typename _InputIterator
, typename _OutputIterator
,
3716 typename _Predicate
, typename _Tp
>
3718 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3719 _OutputIterator __result
,
3720 _Predicate __pred
, const _Tp
& __new_value
)
3722 // concept requirements
3723 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3724 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3725 typename iterator_traits
<_InputIterator
>::value_type
>)
3726 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3727 typename iterator_traits
<_InputIterator
>::value_type
>)
3728 __glibcxx_requires_valid_range(__first
, __last
);
3730 for (; __first
!= __last
; ++__first
, ++__result
)
3731 if (__pred(*__first
))
3732 *__result
= __new_value
;
3734 *__result
= *__first
;
3738 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3740 * @brief Determines whether the elements of a sequence are sorted.
3741 * @param first An iterator.
3742 * @param last Another iterator.
3743 * @return True if the elements are sorted, false otherwise.
3745 template<typename _ForwardIterator
>
3747 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3748 { return std::is_sorted_until(__first
, __last
) == __last
; }
3751 * @brief Determines whether the elements of a sequence are sorted
3752 * according to a comparison functor.
3753 * @param first An iterator.
3754 * @param last Another iterator.
3755 * @param comp A comparison functor.
3756 * @return True if the elements are sorted, false otherwise.
3758 template<typename _ForwardIterator
, typename _Compare
>
3760 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3762 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3765 * @brief Determines the end of a sorted sequence.
3766 * @param first An iterator.
3767 * @param last Another iterator.
3768 * @return An iterator pointing to the last iterator i in [first, last)
3769 * for which the range [first, i) is sorted.
3771 template<typename _ForwardIterator
>
3773 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3775 // concept requirements
3776 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3777 __glibcxx_function_requires(_LessThanComparableConcept
<
3778 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3779 __glibcxx_requires_valid_range(__first
, __last
);
3781 if (__first
== __last
)
3784 _ForwardIterator __next
= __first
;
3785 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3786 if (*__next
< *__first
)
3792 * @brief Determines the end of a sorted sequence using comparison functor.
3793 * @param first An iterator.
3794 * @param last Another iterator.
3795 * @param comp A comparison functor.
3796 * @return An iterator pointing to the last iterator i in [first, last)
3797 * for which the range [first, i) is sorted.
3799 template<typename _ForwardIterator
, typename _Compare
>
3801 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3804 // concept requirements
3805 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3806 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3807 typename iterator_traits
<_ForwardIterator
>::value_type
,
3808 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3809 __glibcxx_requires_valid_range(__first
, __last
);
3811 if (__first
== __last
)
3814 _ForwardIterator __next
= __first
;
3815 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3816 if (__comp(*__next
, *__first
))
3822 * @brief Determines min and max at once as an ordered pair.
3823 * @param a A thing of arbitrary type.
3824 * @param b Another thing of arbitrary type.
3825 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3827 template<typename _Tp
>
3828 inline pair
<const _Tp
&, const _Tp
&>
3829 minmax(const _Tp
& __a
, const _Tp
& __b
)
3831 // concept requirements
3832 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3834 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3835 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3839 * @brief Determines min and max at once as an ordered pair.
3840 * @param a A thing of arbitrary type.
3841 * @param b Another thing of arbitrary type.
3842 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3843 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3845 template<typename _Tp
, typename _Compare
>
3846 inline pair
<const _Tp
&, const _Tp
&>
3847 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3849 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3850 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3854 * @brief Return a pair of iterators pointing to the minimum and maximum
3855 * elements in a range.
3856 * @param first Start of range.
3857 * @param last End of range.
3858 * @return make_pair(m, M), where m is the first iterator i in
3859 * [first, last) such that no other element in the range is
3860 * smaller, and where M is the last iterator i in [first, last)
3861 * such that no other element in the range is larger.
3863 template<typename _ForwardIterator
>
3864 pair
<_ForwardIterator
, _ForwardIterator
>
3865 minmax_element(_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 _ForwardIterator __next
= __first
;
3874 if (__first
== __last
3875 || ++__next
== __last
)
3876 return std::make_pair(__first
, __first
);
3878 _ForwardIterator __min
, __max
;
3879 if (*__next
< *__first
)
3893 while (__first
!= __last
)
3896 if (++__next
== __last
)
3898 if (*__first
< *__min
)
3900 else if (!(*__first
< *__max
))
3905 if (*__next
< *__first
)
3907 if (*__next
< *__min
)
3909 if (!(*__first
< *__max
))
3914 if (*__first
< *__min
)
3916 if (!(*__next
< *__max
))
3924 return std::make_pair(__min
, __max
);
3928 * @brief Return a pair of iterators pointing to the minimum and maximum
3929 * elements in a range.
3930 * @param first Start of range.
3931 * @param last End of range.
3932 * @param comp Comparison functor.
3933 * @return make_pair(m, M), where m is the first iterator i in
3934 * [first, last) such that no other element in the range is
3935 * smaller, and where M is the last iterator i in [first, last)
3936 * such that no other element in the range is larger.
3938 template<typename _ForwardIterator
, typename _Compare
>
3939 pair
<_ForwardIterator
, _ForwardIterator
>
3940 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3943 // concept requirements
3944 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3945 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3946 typename iterator_traits
<_ForwardIterator
>::value_type
,
3947 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3948 __glibcxx_requires_valid_range(__first
, __last
);
3950 _ForwardIterator __next
= __first
;
3951 if (__first
== __last
3952 || ++__next
== __last
)
3953 return std::make_pair(__first
, __first
);
3955 _ForwardIterator __min
, __max
;
3956 if (__comp(*__next
, *__first
))
3970 while (__first
!= __last
)
3973 if (++__next
== __last
)
3975 if (__comp(*__first
, *__min
))
3977 else if (!__comp(*__first
, *__max
))
3982 if (__comp(*__next
, *__first
))
3984 if (__comp(*__next
, *__min
))
3986 if (!__comp(*__first
, *__max
))
3991 if (__comp(*__first
, *__min
))
3993 if (!__comp(*__next
, *__max
))
4001 return std::make_pair(__min
, __max
);
4003 #endif // __GXX_EXPERIMENTAL_CXX0X__
4005 _GLIBCXX_END_NAMESPACE
4007 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
4010 * @brief Apply a function to every element of a sequence.
4011 * @param first An input iterator.
4012 * @param last An input iterator.
4013 * @param f A unary function object.
4016 * Applies the function object @p f to each element in the range
4017 * @p [first,last). @p f must not modify the order of the sequence.
4018 * If @p f has a return value it is ignored.
4020 template<typename _InputIterator
, typename _Function
>
4022 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4024 // concept requirements
4025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4026 __glibcxx_requires_valid_range(__first
, __last
);
4027 for (; __first
!= __last
; ++__first
)
4033 * @brief Find the first occurrence of a value in a sequence.
4034 * @param first An input iterator.
4035 * @param last An input iterator.
4036 * @param val The value to find.
4037 * @return The first iterator @c i in the range @p [first,last)
4038 * such that @c *i == @p val, or @p last if no such iterator exists.
4040 template<typename _InputIterator
, typename _Tp
>
4041 inline _InputIterator
4042 find(_InputIterator __first
, _InputIterator __last
,
4045 // concept requirements
4046 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4047 __glibcxx_function_requires(_EqualOpConcept
<
4048 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4049 __glibcxx_requires_valid_range(__first
, __last
);
4050 return std::__find(__first
, __last
, __val
,
4051 std::__iterator_category(__first
));
4055 * @brief Find the first element in a sequence for which a
4056 * predicate is true.
4057 * @param first An input iterator.
4058 * @param last An input iterator.
4059 * @param pred A predicate.
4060 * @return The first iterator @c i in the range @p [first,last)
4061 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4063 template<typename _InputIterator
, typename _Predicate
>
4064 inline _InputIterator
4065 find_if(_InputIterator __first
, _InputIterator __last
,
4068 // concept requirements
4069 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4070 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4071 typename iterator_traits
<_InputIterator
>::value_type
>)
4072 __glibcxx_requires_valid_range(__first
, __last
);
4073 return std::__find_if(__first
, __last
, __pred
,
4074 std::__iterator_category(__first
));
4078 * @brief Find element from a set in a sequence.
4079 * @param first1 Start of range to search.
4080 * @param last1 End of range to search.
4081 * @param first2 Start of match candidates.
4082 * @param last2 End of match candidates.
4083 * @return The first iterator @c i in the range
4084 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4085 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4087 * Searches the range @p [first1,last1) for an element that is equal to
4088 * some element in the range [first2,last2). If found, returns an iterator
4089 * in the range [first1,last1), otherwise returns @p last1.
4091 template<typename _InputIterator
, typename _ForwardIterator
>
4093 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4094 _ForwardIterator __first2
, _ForwardIterator __last2
)
4096 // concept requirements
4097 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4098 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4099 __glibcxx_function_requires(_EqualOpConcept
<
4100 typename iterator_traits
<_InputIterator
>::value_type
,
4101 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4102 __glibcxx_requires_valid_range(__first1
, __last1
);
4103 __glibcxx_requires_valid_range(__first2
, __last2
);
4105 for (; __first1
!= __last1
; ++__first1
)
4106 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4107 if (*__first1
== *__iter
)
4113 * @brief Find element from a set in a sequence using a predicate.
4114 * @param first1 Start of range to search.
4115 * @param last1 End of range to search.
4116 * @param first2 Start of match candidates.
4117 * @param last2 End of match candidates.
4118 * @param comp Predicate to use.
4119 * @return The first iterator @c i in the range
4120 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4121 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4124 * Searches the range @p [first1,last1) for an element that is
4125 * equal to some element in the range [first2,last2). If found,
4126 * returns an iterator in the range [first1,last1), otherwise
4129 template<typename _InputIterator
, typename _ForwardIterator
,
4130 typename _BinaryPredicate
>
4132 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4133 _ForwardIterator __first2
, _ForwardIterator __last2
,
4134 _BinaryPredicate __comp
)
4136 // concept requirements
4137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4138 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4139 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4140 typename iterator_traits
<_InputIterator
>::value_type
,
4141 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4142 __glibcxx_requires_valid_range(__first1
, __last1
);
4143 __glibcxx_requires_valid_range(__first2
, __last2
);
4145 for (; __first1
!= __last1
; ++__first1
)
4146 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4147 if (__comp(*__first1
, *__iter
))
4153 * @brief Find two adjacent values in a sequence that are equal.
4154 * @param first A forward iterator.
4155 * @param last A forward iterator.
4156 * @return The first iterator @c i such that @c i and @c i+1 are both
4157 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4158 * or @p last if no such iterator exists.
4160 template<typename _ForwardIterator
>
4162 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4164 // concept requirements
4165 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4166 __glibcxx_function_requires(_EqualityComparableConcept
<
4167 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4168 __glibcxx_requires_valid_range(__first
, __last
);
4169 if (__first
== __last
)
4171 _ForwardIterator __next
= __first
;
4172 while(++__next
!= __last
)
4174 if (*__first
== *__next
)
4182 * @brief Find two adjacent values in a sequence using a predicate.
4183 * @param first A forward iterator.
4184 * @param last A forward iterator.
4185 * @param binary_pred A binary predicate.
4186 * @return The first iterator @c i such that @c i and @c i+1 are both
4187 * valid iterators in @p [first,last) and such that
4188 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4191 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4193 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4194 _BinaryPredicate __binary_pred
)
4196 // concept requirements
4197 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4198 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4199 typename iterator_traits
<_ForwardIterator
>::value_type
,
4200 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4201 __glibcxx_requires_valid_range(__first
, __last
);
4202 if (__first
== __last
)
4204 _ForwardIterator __next
= __first
;
4205 while(++__next
!= __last
)
4207 if (__binary_pred(*__first
, *__next
))
4215 * @brief Count the number of copies of a value in a sequence.
4216 * @param first An input iterator.
4217 * @param last An input iterator.
4218 * @param value The value to be counted.
4219 * @return The number of iterators @c i in the range @p [first,last)
4220 * for which @c *i == @p value
4222 template<typename _InputIterator
, typename _Tp
>
4223 typename iterator_traits
<_InputIterator
>::difference_type
4224 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4226 // concept requirements
4227 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4228 __glibcxx_function_requires(_EqualOpConcept
<
4229 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4230 __glibcxx_requires_valid_range(__first
, __last
);
4231 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4232 for (; __first
!= __last
; ++__first
)
4233 if (*__first
== __value
)
4239 * @brief Count the elements of a sequence for which a predicate is true.
4240 * @param first An input iterator.
4241 * @param last An input iterator.
4242 * @param pred A predicate.
4243 * @return The number of iterators @c i in the range @p [first,last)
4244 * for which @p pred(*i) is true.
4246 template<typename _InputIterator
, typename _Predicate
>
4247 typename iterator_traits
<_InputIterator
>::difference_type
4248 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4250 // concept requirements
4251 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4252 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4253 typename iterator_traits
<_InputIterator
>::value_type
>)
4254 __glibcxx_requires_valid_range(__first
, __last
);
4255 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4256 for (; __first
!= __last
; ++__first
)
4257 if (__pred(*__first
))
4263 * @brief Search a sequence for a matching sub-sequence.
4264 * @param first1 A forward iterator.
4265 * @param last1 A forward iterator.
4266 * @param first2 A forward iterator.
4267 * @param last2 A forward iterator.
4268 * @return The first iterator @c i in the range
4269 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4270 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4271 * such iterator exists.
4273 * Searches the range @p [first1,last1) for a sub-sequence that compares
4274 * equal value-by-value with the sequence given by @p [first2,last2) and
4275 * returns an iterator to the first element of the sub-sequence, or
4276 * @p last1 if the sub-sequence is not found.
4278 * Because the sub-sequence must lie completely within the range
4279 * @p [first1,last1) it must start at a position less than
4280 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4282 * This means that the returned iterator @c i will be in the range
4283 * @p [first1,last1-(last2-first2))
4285 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4287 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4288 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4290 // concept requirements
4291 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4292 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4293 __glibcxx_function_requires(_EqualOpConcept
<
4294 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4295 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4296 __glibcxx_requires_valid_range(__first1
, __last1
);
4297 __glibcxx_requires_valid_range(__first2
, __last2
);
4299 // Test for empty ranges
4300 if (__first1
== __last1
|| __first2
== __last2
)
4303 // Test for a pattern of length 1.
4304 _ForwardIterator2
__p1(__first2
);
4305 if (++__p1
== __last2
)
4306 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4309 _ForwardIterator2 __p
;
4310 _ForwardIterator1 __current
= __first1
;
4314 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4315 if (__first1
== __last1
)
4319 __current
= __first1
;
4320 if (++__current
== __last1
)
4323 while (*__current
== *__p
)
4325 if (++__p
== __last2
)
4327 if (++__current
== __last1
)
4336 * @brief Search a sequence for a matching sub-sequence using a predicate.
4337 * @param first1 A forward iterator.
4338 * @param last1 A forward iterator.
4339 * @param first2 A forward iterator.
4340 * @param last2 A forward iterator.
4341 * @param predicate A binary predicate.
4342 * @return The first iterator @c i in the range
4343 * @p [first1,last1-(last2-first2)) such that
4344 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4345 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4347 * Searches the range @p [first1,last1) for a sub-sequence that compares
4348 * equal value-by-value with the sequence given by @p [first2,last2),
4349 * using @p predicate to determine equality, and returns an iterator
4350 * to the first element of the sub-sequence, or @p last1 if no such
4353 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4355 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4356 typename _BinaryPredicate
>
4358 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4359 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4360 _BinaryPredicate __predicate
)
4362 // concept requirements
4363 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4364 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4365 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4366 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4367 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4368 __glibcxx_requires_valid_range(__first1
, __last1
);
4369 __glibcxx_requires_valid_range(__first2
, __last2
);
4371 // Test for empty ranges
4372 if (__first1
== __last1
|| __first2
== __last2
)
4375 // Test for a pattern of length 1.
4376 _ForwardIterator2
__p1(__first2
);
4377 if (++__p1
== __last2
)
4379 while (__first1
!= __last1
4380 && !bool(__predicate(*__first1
, *__first2
)))
4386 _ForwardIterator2 __p
;
4387 _ForwardIterator1 __current
= __first1
;
4391 while (__first1
!= __last1
4392 && !bool(__predicate(*__first1
, *__first2
)))
4394 if (__first1
== __last1
)
4398 __current
= __first1
;
4399 if (++__current
== __last1
)
4402 while (__predicate(*__current
, *__p
))
4404 if (++__p
== __last2
)
4406 if (++__current
== __last1
)
4416 * @brief Search a sequence for a number of consecutive values.
4417 * @param first A forward iterator.
4418 * @param last A forward iterator.
4419 * @param count The number of consecutive values.
4420 * @param val The value to find.
4421 * @return The first iterator @c i in the range @p [first,last-count)
4422 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4423 * or @p last if no such iterator exists.
4425 * Searches the range @p [first,last) for @p count consecutive elements
4428 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4430 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4431 _Integer __count
, const _Tp
& __val
)
4433 // concept requirements
4434 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4435 __glibcxx_function_requires(_EqualOpConcept
<
4436 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4437 __glibcxx_requires_valid_range(__first
, __last
);
4442 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4443 return std::__search_n(__first
, __last
, __count
, __val
,
4444 std::__iterator_category(__first
));
4449 * @brief Search a sequence for a number of consecutive values using a
4451 * @param first A forward iterator.
4452 * @param last A forward iterator.
4453 * @param count The number of consecutive values.
4454 * @param val The value to find.
4455 * @param binary_pred A binary predicate.
4456 * @return The first iterator @c i in the range @p [first,last-count)
4457 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4458 * range @p [0,count), or @p last if no such iterator exists.
4460 * Searches the range @p [first,last) for @p count consecutive elements
4461 * for which the predicate returns true.
4463 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4464 typename _BinaryPredicate
>
4466 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4467 _Integer __count
, const _Tp
& __val
,
4468 _BinaryPredicate __binary_pred
)
4470 // concept requirements
4471 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4472 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4473 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4474 __glibcxx_requires_valid_range(__first
, __last
);
4480 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4484 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4485 std::__iterator_category(__first
));
4490 * @brief Perform an operation on a sequence.
4491 * @param first An input iterator.
4492 * @param last An input iterator.
4493 * @param result An output iterator.
4494 * @param unary_op A unary operator.
4495 * @return An output iterator equal to @p result+(last-first).
4497 * Applies the operator to each element in the input range and assigns
4498 * the results to successive elements of the output sequence.
4499 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4500 * range @p [0,last-first).
4502 * @p unary_op must not alter its argument.
4504 template<typename _InputIterator
, typename _OutputIterator
,
4505 typename _UnaryOperation
>
4507 transform(_InputIterator __first
, _InputIterator __last
,
4508 _OutputIterator __result
, _UnaryOperation __unary_op
)
4510 // concept requirements
4511 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4512 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4513 // "the type returned by a _UnaryOperation"
4514 __typeof__(__unary_op(*__first
))>)
4515 __glibcxx_requires_valid_range(__first
, __last
);
4517 for (; __first
!= __last
; ++__first
, ++__result
)
4518 *__result
= __unary_op(*__first
);
4523 * @brief Perform an operation on corresponding elements of two sequences.
4524 * @param first1 An input iterator.
4525 * @param last1 An input iterator.
4526 * @param first2 An input iterator.
4527 * @param result An output iterator.
4528 * @param binary_op A binary operator.
4529 * @return An output iterator equal to @p result+(last-first).
4531 * Applies the operator to the corresponding elements in the two
4532 * input ranges and assigns the results to successive elements of the
4534 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4535 * @c N in the range @p [0,last1-first1).
4537 * @p binary_op must not alter either of its arguments.
4539 template<typename _InputIterator1
, typename _InputIterator2
,
4540 typename _OutputIterator
, typename _BinaryOperation
>
4542 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4543 _InputIterator2 __first2
, _OutputIterator __result
,
4544 _BinaryOperation __binary_op
)
4546 // concept requirements
4547 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4548 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4549 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4550 // "the type returned by a _BinaryOperation"
4551 __typeof__(__binary_op(*__first1
,*__first2
))>)
4552 __glibcxx_requires_valid_range(__first1
, __last1
);
4554 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4555 *__result
= __binary_op(*__first1
, *__first2
);
4560 * @brief Replace each occurrence of one value in a sequence with another
4562 * @param first A forward iterator.
4563 * @param last A forward iterator.
4564 * @param old_value The value to be replaced.
4565 * @param new_value The replacement value.
4566 * @return replace() returns no value.
4568 * For each iterator @c i in the range @p [first,last) if @c *i ==
4569 * @p old_value then the assignment @c *i = @p new_value is performed.
4571 template<typename _ForwardIterator
, typename _Tp
>
4573 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4574 const _Tp
& __old_value
, const _Tp
& __new_value
)
4576 // concept requirements
4577 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4579 __glibcxx_function_requires(_EqualOpConcept
<
4580 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4581 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4582 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4583 __glibcxx_requires_valid_range(__first
, __last
);
4585 for (; __first
!= __last
; ++__first
)
4586 if (*__first
== __old_value
)
4587 *__first
= __new_value
;
4591 * @brief Replace each value in a sequence for which a predicate returns
4592 * true with another value.
4593 * @param first A forward iterator.
4594 * @param last A forward iterator.
4595 * @param pred A predicate.
4596 * @param new_value The replacement value.
4597 * @return replace_if() returns no value.
4599 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4600 * is true then the assignment @c *i = @p new_value is performed.
4602 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4604 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4605 _Predicate __pred
, const _Tp
& __new_value
)
4607 // concept requirements
4608 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4610 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4611 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4612 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4613 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4614 __glibcxx_requires_valid_range(__first
, __last
);
4616 for (; __first
!= __last
; ++__first
)
4617 if (__pred(*__first
))
4618 *__first
= __new_value
;
4622 * @brief Assign the result of a function object to each value in a
4624 * @param first A forward iterator.
4625 * @param last A forward iterator.
4626 * @param gen A function object taking no arguments and returning
4627 * std::iterator_traits<_ForwardIterator>::value_type
4628 * @return generate() returns no value.
4630 * Performs the assignment @c *i = @p gen() for each @c i in the range
4633 template<typename _ForwardIterator
, typename _Generator
>
4635 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4638 // concept requirements
4639 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4640 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4641 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4642 __glibcxx_requires_valid_range(__first
, __last
);
4644 for (; __first
!= __last
; ++__first
)
4649 * @brief Assign the result of a function object to each value in a
4651 * @param first A forward iterator.
4652 * @param n The length of the sequence.
4653 * @param gen A function object taking no arguments and returning
4654 * std::iterator_traits<_ForwardIterator>::value_type
4655 * @return The end of the sequence, @p first+n
4657 * Performs the assignment @c *i = @p gen() for each @c i in the range
4658 * @p [first,first+n).
4660 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4662 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4664 // concept requirements
4665 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4666 // "the type returned by a _Generator"
4667 __typeof__(__gen())>)
4669 for (; __n
> 0; --__n
, ++__first
)
4676 * @brief Copy a sequence, removing consecutive duplicate values.
4677 * @param first An input iterator.
4678 * @param last An input iterator.
4679 * @param result An output iterator.
4680 * @return An iterator designating the end of the resulting sequence.
4682 * Copies each element in the range @p [first,last) to the range
4683 * beginning at @p result, except that only the first element is copied
4684 * from groups of consecutive elements that compare equal.
4685 * unique_copy() is stable, so the relative order of elements that are
4686 * copied is unchanged.
4688 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4689 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4691 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4692 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4695 template<typename _InputIterator
, typename _OutputIterator
>
4696 inline _OutputIterator
4697 unique_copy(_InputIterator __first
, _InputIterator __last
,
4698 _OutputIterator __result
)
4700 // concept requirements
4701 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4702 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4703 typename iterator_traits
<_InputIterator
>::value_type
>)
4704 __glibcxx_function_requires(_EqualityComparableConcept
<
4705 typename iterator_traits
<_InputIterator
>::value_type
>)
4706 __glibcxx_requires_valid_range(__first
, __last
);
4708 if (__first
== __last
)
4710 return std::__unique_copy(__first
, __last
, __result
,
4711 std::__iterator_category(__first
),
4712 std::__iterator_category(__result
));
4716 * @brief Copy a sequence, removing consecutive values using a predicate.
4717 * @param first An input iterator.
4718 * @param last An input iterator.
4719 * @param result An output iterator.
4720 * @param binary_pred A binary predicate.
4721 * @return An iterator designating the end of the resulting sequence.
4723 * Copies each element in the range @p [first,last) to the range
4724 * beginning at @p result, except that only the first element is copied
4725 * from groups of consecutive elements for which @p binary_pred returns
4727 * unique_copy() is stable, so the relative order of elements that are
4728 * copied is unchanged.
4730 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4731 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4733 template<typename _InputIterator
, typename _OutputIterator
,
4734 typename _BinaryPredicate
>
4735 inline _OutputIterator
4736 unique_copy(_InputIterator __first
, _InputIterator __last
,
4737 _OutputIterator __result
,
4738 _BinaryPredicate __binary_pred
)
4740 // concept requirements -- predicates checked later
4741 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4742 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4743 typename iterator_traits
<_InputIterator
>::value_type
>)
4744 __glibcxx_requires_valid_range(__first
, __last
);
4746 if (__first
== __last
)
4748 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4749 std::__iterator_category(__first
),
4750 std::__iterator_category(__result
));
4755 * @brief Randomly shuffle the elements of a sequence.
4756 * @param first A forward iterator.
4757 * @param last A forward iterator.
4760 * Reorder the elements in the range @p [first,last) using a random
4761 * distribution, so that every possible ordering of the sequence is
4764 template<typename _RandomAccessIterator
>
4766 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4768 // concept requirements
4769 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4770 _RandomAccessIterator
>)
4771 __glibcxx_requires_valid_range(__first
, __last
);
4773 if (__first
!= __last
)
4774 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4775 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4779 * @brief Shuffle the elements of a sequence using a random number
4781 * @param first A forward iterator.
4782 * @param last A forward iterator.
4783 * @param rand The RNG functor or function.
4786 * Reorders the elements in the range @p [first,last) using @p rand to
4787 * provide a random distribution. Calling @p rand(N) for a positive
4788 * integer @p N should return a randomly chosen integer from the
4791 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4793 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4794 _RandomNumberGenerator
& __rand
)
4796 // concept requirements
4797 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4798 _RandomAccessIterator
>)
4799 __glibcxx_requires_valid_range(__first
, __last
);
4801 if (__first
== __last
)
4803 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4804 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4809 * @brief Move elements for which a predicate is true to the beginning
4811 * @param first A forward iterator.
4812 * @param last A forward iterator.
4813 * @param pred A predicate functor.
4814 * @return An iterator @p middle such that @p pred(i) is true for each
4815 * iterator @p i in the range @p [first,middle) and false for each @p i
4816 * in the range @p [middle,last).
4818 * @p pred must not modify its operand. @p partition() does not preserve
4819 * the relative ordering of elements in each group, use
4820 * @p stable_partition() if this is needed.
4822 template<typename _ForwardIterator
, typename _Predicate
>
4823 inline _ForwardIterator
4824 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4827 // concept requirements
4828 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4830 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4831 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4832 __glibcxx_requires_valid_range(__first
, __last
);
4834 return std::__partition(__first
, __last
, __pred
,
4835 std::__iterator_category(__first
));
4841 * @brief Sort the smallest elements of a sequence.
4842 * @param first An iterator.
4843 * @param middle Another iterator.
4844 * @param last Another iterator.
4847 * Sorts the smallest @p (middle-first) elements in the range
4848 * @p [first,last) and moves them to the range @p [first,middle). The
4849 * order of the remaining elements in the range @p [middle,last) is
4851 * After the sort if @p i and @j are iterators in the range
4852 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4853 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4855 template<typename _RandomAccessIterator
>
4857 partial_sort(_RandomAccessIterator __first
,
4858 _RandomAccessIterator __middle
,
4859 _RandomAccessIterator __last
)
4861 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4864 // concept requirements
4865 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4866 _RandomAccessIterator
>)
4867 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4868 __glibcxx_requires_valid_range(__first
, __middle
);
4869 __glibcxx_requires_valid_range(__middle
, __last
);
4871 std::__heap_select(__first
, __middle
, __last
);
4872 std::sort_heap(__first
, __middle
);
4876 * @brief Sort the smallest elements of a sequence using a predicate
4878 * @param first An iterator.
4879 * @param middle Another iterator.
4880 * @param last Another iterator.
4881 * @param comp A comparison functor.
4884 * Sorts the smallest @p (middle-first) elements in the range
4885 * @p [first,last) and moves them to the range @p [first,middle). The
4886 * order of the remaining elements in the range @p [middle,last) is
4888 * After the sort if @p i and @j are iterators in the range
4889 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4890 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4893 template<typename _RandomAccessIterator
, typename _Compare
>
4895 partial_sort(_RandomAccessIterator __first
,
4896 _RandomAccessIterator __middle
,
4897 _RandomAccessIterator __last
,
4900 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4903 // concept requirements
4904 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4905 _RandomAccessIterator
>)
4906 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4907 _ValueType
, _ValueType
>)
4908 __glibcxx_requires_valid_range(__first
, __middle
);
4909 __glibcxx_requires_valid_range(__middle
, __last
);
4911 std::__heap_select(__first
, __middle
, __last
, __comp
);
4912 std::sort_heap(__first
, __middle
, __comp
);
4916 * @brief Sort a sequence just enough to find a particular position.
4917 * @param first An iterator.
4918 * @param nth Another iterator.
4919 * @param last Another iterator.
4922 * Rearranges the elements in the range @p [first,last) so that @p *nth
4923 * is the same element that would have been in that position had the
4924 * whole sequence been sorted.
4925 * whole sequence been sorted. The elements either side of @p *nth are
4926 * not completely sorted, but for any iterator @i in the range
4927 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4928 * holds that @p *j<*i is false.
4930 template<typename _RandomAccessIterator
>
4932 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4933 _RandomAccessIterator __last
)
4935 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4938 // concept requirements
4939 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4940 _RandomAccessIterator
>)
4941 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4942 __glibcxx_requires_valid_range(__first
, __nth
);
4943 __glibcxx_requires_valid_range(__nth
, __last
);
4945 if (__first
== __last
|| __nth
== __last
)
4948 std::__introselect(__first
, __nth
, __last
,
4949 std::__lg(__last
- __first
) * 2);
4953 * @brief Sort a sequence just enough to find a particular position
4954 * using a predicate for comparison.
4955 * @param first An iterator.
4956 * @param nth Another iterator.
4957 * @param last Another iterator.
4958 * @param comp A comparison functor.
4961 * Rearranges the elements in the range @p [first,last) so that @p *nth
4962 * is the same element that would have been in that position had the
4963 * whole sequence been sorted. The elements either side of @p *nth are
4964 * not completely sorted, but for any iterator @i in the range
4965 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4966 * holds that @p comp(*j,*i) is false.
4968 template<typename _RandomAccessIterator
, typename _Compare
>
4970 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4971 _RandomAccessIterator __last
, _Compare __comp
)
4973 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4976 // concept requirements
4977 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4978 _RandomAccessIterator
>)
4979 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4980 _ValueType
, _ValueType
>)
4981 __glibcxx_requires_valid_range(__first
, __nth
);
4982 __glibcxx_requires_valid_range(__nth
, __last
);
4984 if (__first
== __last
|| __nth
== __last
)
4987 std::__introselect(__first
, __nth
, __last
,
4988 std::__lg(__last
- __first
) * 2, __comp
);
4993 * @brief Sort the elements of a sequence.
4994 * @param first An iterator.
4995 * @param last Another iterator.
4998 * Sorts the elements in the range @p [first,last) in ascending order,
4999 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5000 * @p [first,last-1).
5002 * The relative ordering of equivalent elements is not preserved, use
5003 * @p stable_sort() if this is needed.
5005 template<typename _RandomAccessIterator
>
5007 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5009 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5012 // concept requirements
5013 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5014 _RandomAccessIterator
>)
5015 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5016 __glibcxx_requires_valid_range(__first
, __last
);
5018 if (__first
!= __last
)
5020 std::__introsort_loop(__first
, __last
,
5021 std::__lg(__last
- __first
) * 2);
5022 std::__final_insertion_sort(__first
, __last
);
5027 * @brief Sort the elements of a sequence using a predicate for comparison.
5028 * @param first An iterator.
5029 * @param last Another iterator.
5030 * @param comp A comparison functor.
5033 * Sorts the elements in the range @p [first,last) in ascending order,
5034 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5035 * range @p [first,last-1).
5037 * The relative ordering of equivalent elements is not preserved, use
5038 * @p stable_sort() if this is needed.
5040 template<typename _RandomAccessIterator
, typename _Compare
>
5042 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5045 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5048 // concept requirements
5049 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5050 _RandomAccessIterator
>)
5051 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5053 __glibcxx_requires_valid_range(__first
, __last
);
5055 if (__first
!= __last
)
5057 std::__introsort_loop(__first
, __last
,
5058 std::__lg(__last
- __first
) * 2, __comp
);
5059 std::__final_insertion_sort(__first
, __last
, __comp
);
5064 * @brief Merges two sorted ranges.
5065 * @param first1 An iterator.
5066 * @param first2 Another iterator.
5067 * @param last1 Another iterator.
5068 * @param last2 Another iterator.
5069 * @param result An iterator pointing to the end of the merged range.
5070 * @return An iterator pointing to the first element "not less
5073 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5074 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5075 * must be sorted, and the output range must not overlap with either of
5076 * the input ranges. The sort is @e stable, that is, for equivalent
5077 * elements in the two ranges, elements from the first range will always
5078 * come before elements from the second.
5080 template<typename _InputIterator1
, typename _InputIterator2
,
5081 typename _OutputIterator
>
5083 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5084 _InputIterator2 __first2
, _InputIterator2 __last2
,
5085 _OutputIterator __result
)
5087 typedef typename iterator_traits
<_InputIterator1
>::value_type
5089 typedef typename iterator_traits
<_InputIterator2
>::value_type
5092 // concept requirements
5093 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5094 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5095 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5097 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5099 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5100 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5101 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5103 while (__first1
!= __last1
&& __first2
!= __last2
)
5105 if (*__first2
< *__first1
)
5107 *__result
= *__first2
;
5112 *__result
= *__first1
;
5117 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5122 * @brief Merges two sorted ranges.
5123 * @param first1 An iterator.
5124 * @param first2 Another iterator.
5125 * @param last1 Another iterator.
5126 * @param last2 Another iterator.
5127 * @param result An iterator pointing to the end of the merged range.
5128 * @param comp A functor to use for comparisons.
5129 * @return An iterator pointing to the first element "not less
5132 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5133 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5134 * must be sorted, and the output range must not overlap with either of
5135 * the input ranges. The sort is @e stable, that is, for equivalent
5136 * elements in the two ranges, elements from the first range will always
5137 * come before elements from the second.
5139 * The comparison function should have the same effects on ordering as
5140 * the function used for the initial sort.
5142 template<typename _InputIterator1
, typename _InputIterator2
,
5143 typename _OutputIterator
, typename _Compare
>
5145 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5146 _InputIterator2 __first2
, _InputIterator2 __last2
,
5147 _OutputIterator __result
, _Compare __comp
)
5149 typedef typename iterator_traits
<_InputIterator1
>::value_type
5151 typedef typename iterator_traits
<_InputIterator2
>::value_type
5154 // concept requirements
5155 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5156 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5157 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5159 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5161 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5162 _ValueType2
, _ValueType1
>)
5163 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5164 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5166 while (__first1
!= __last1
&& __first2
!= __last2
)
5168 if (__comp(*__first2
, *__first1
))
5170 *__result
= *__first2
;
5175 *__result
= *__first1
;
5180 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5186 * @brief Sort the elements of a sequence, preserving the relative order
5187 * of equivalent elements.
5188 * @param first An iterator.
5189 * @param last Another iterator.
5192 * Sorts the elements in the range @p [first,last) in ascending order,
5193 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5194 * @p [first,last-1).
5196 * The relative ordering of equivalent elements is preserved, so any two
5197 * elements @p x and @p y in the range @p [first,last) such that
5198 * @p x<y is false and @p y<x is false will have the same relative
5199 * ordering after calling @p stable_sort().
5201 template<typename _RandomAccessIterator
>
5203 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5205 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5207 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5210 // concept requirements
5211 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5212 _RandomAccessIterator
>)
5213 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5214 __glibcxx_requires_valid_range(__first
, __last
);
5216 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5218 if (__buf
.begin() == 0)
5219 std::__inplace_stable_sort(__first
, __last
);
5221 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5222 _DistanceType(__buf
.size()));
5226 * @brief Sort the elements of a sequence using a predicate for comparison,
5227 * preserving the relative order of equivalent elements.
5228 * @param first An iterator.
5229 * @param last Another iterator.
5230 * @param comp A comparison functor.
5233 * Sorts the elements in the range @p [first,last) in ascending order,
5234 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5235 * range @p [first,last-1).
5237 * The relative ordering of equivalent elements is preserved, so any two
5238 * elements @p x and @p y in the range @p [first,last) such that
5239 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5240 * relative ordering after calling @p stable_sort().
5242 template<typename _RandomAccessIterator
, typename _Compare
>
5244 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5247 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5249 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5252 // concept requirements
5253 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5254 _RandomAccessIterator
>)
5255 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5258 __glibcxx_requires_valid_range(__first
, __last
);
5260 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5262 if (__buf
.begin() == 0)
5263 std::__inplace_stable_sort(__first
, __last
, __comp
);
5265 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5266 _DistanceType(__buf
.size()), __comp
);
5271 * @brief Return the union of two sorted ranges.
5272 * @param first1 Start of first range.
5273 * @param last1 End of first range.
5274 * @param first2 Start of second range.
5275 * @param last2 End of second range.
5276 * @return End of the output range.
5277 * @ingroup setoperations
5279 * This operation iterates over both ranges, copying elements present in
5280 * each range in order to the output range. Iterators increment for each
5281 * range. When the current element of one range is less than the other,
5282 * that element is copied and the iterator advanced. If an element is
5283 * contained in both ranges, the element from the first range is copied and
5284 * both ranges advance. The output range may not overlap either input
5287 template<typename _InputIterator1
, typename _InputIterator2
,
5288 typename _OutputIterator
>
5290 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5291 _InputIterator2 __first2
, _InputIterator2 __last2
,
5292 _OutputIterator __result
)
5294 typedef typename iterator_traits
<_InputIterator1
>::value_type
5296 typedef typename iterator_traits
<_InputIterator2
>::value_type
5299 // concept requirements
5300 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5301 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5302 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5304 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5306 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5307 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5308 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5309 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5311 while (__first1
!= __last1
&& __first2
!= __last2
)
5313 if (*__first1
< *__first2
)
5315 *__result
= *__first1
;
5318 else if (*__first2
< *__first1
)
5320 *__result
= *__first2
;
5325 *__result
= *__first1
;
5331 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5336 * @brief Return the union of two sorted ranges using a comparison functor.
5337 * @param first1 Start of first range.
5338 * @param last1 End of first range.
5339 * @param first2 Start of second range.
5340 * @param last2 End of second range.
5341 * @param comp The comparison functor.
5342 * @return End of the output range.
5343 * @ingroup setoperations
5345 * This operation iterates over both ranges, copying elements present in
5346 * each range in order to the output range. Iterators increment for each
5347 * range. When the current element of one range is less than the other
5348 * according to @a comp, that element is copied and the iterator advanced.
5349 * If an equivalent element according to @a comp is contained in both
5350 * ranges, the element from the first range is copied and both ranges
5351 * advance. The output range may not overlap either input range.
5353 template<typename _InputIterator1
, typename _InputIterator2
,
5354 typename _OutputIterator
, typename _Compare
>
5356 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5357 _InputIterator2 __first2
, _InputIterator2 __last2
,
5358 _OutputIterator __result
, _Compare __comp
)
5360 typedef typename iterator_traits
<_InputIterator1
>::value_type
5362 typedef typename iterator_traits
<_InputIterator2
>::value_type
5365 // concept requirements
5366 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5367 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5368 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5370 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5372 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5373 _ValueType1
, _ValueType2
>)
5374 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5375 _ValueType2
, _ValueType1
>)
5376 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5377 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5379 while (__first1
!= __last1
&& __first2
!= __last2
)
5381 if (__comp(*__first1
, *__first2
))
5383 *__result
= *__first1
;
5386 else if (__comp(*__first2
, *__first1
))
5388 *__result
= *__first2
;
5393 *__result
= *__first1
;
5399 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5404 * @brief Return the intersection of two sorted ranges.
5405 * @param first1 Start of first range.
5406 * @param last1 End of first range.
5407 * @param first2 Start of second range.
5408 * @param last2 End of second range.
5409 * @return End of the output range.
5410 * @ingroup setoperations
5412 * This operation iterates over both ranges, copying elements present in
5413 * both ranges in order to the output range. Iterators increment for each
5414 * range. When the current element of one range is less than the other,
5415 * that iterator advances. If an element is contained in both ranges, the
5416 * element from the first range is copied and both ranges advance. The
5417 * output range may not overlap either input range.
5419 template<typename _InputIterator1
, typename _InputIterator2
,
5420 typename _OutputIterator
>
5422 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5423 _InputIterator2 __first2
, _InputIterator2 __last2
,
5424 _OutputIterator __result
)
5426 typedef typename iterator_traits
<_InputIterator1
>::value_type
5428 typedef typename iterator_traits
<_InputIterator2
>::value_type
5431 // concept requirements
5432 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5433 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5434 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5436 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5437 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5438 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5439 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5441 while (__first1
!= __last1
&& __first2
!= __last2
)
5442 if (*__first1
< *__first2
)
5444 else if (*__first2
< *__first1
)
5448 *__result
= *__first1
;
5457 * @brief Return the intersection of two sorted ranges using comparison
5459 * @param first1 Start of first range.
5460 * @param last1 End of first range.
5461 * @param first2 Start of second range.
5462 * @param last2 End of second range.
5463 * @param comp The comparison functor.
5464 * @return End of the output range.
5465 * @ingroup setoperations
5467 * This operation iterates over both ranges, copying elements present in
5468 * both ranges in order to the output range. Iterators increment for each
5469 * range. When the current element of one range is less than the other
5470 * according to @a comp, that iterator advances. If an element is
5471 * contained in both ranges according to @a comp, the element from the
5472 * first range is copied and both ranges advance. The output range may not
5473 * overlap either input range.
5475 template<typename _InputIterator1
, typename _InputIterator2
,
5476 typename _OutputIterator
, typename _Compare
>
5478 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5479 _InputIterator2 __first2
, _InputIterator2 __last2
,
5480 _OutputIterator __result
, _Compare __comp
)
5482 typedef typename iterator_traits
<_InputIterator1
>::value_type
5484 typedef typename iterator_traits
<_InputIterator2
>::value_type
5487 // concept requirements
5488 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5489 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5490 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5492 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5493 _ValueType1
, _ValueType2
>)
5494 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5495 _ValueType2
, _ValueType1
>)
5496 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5497 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5499 while (__first1
!= __last1
&& __first2
!= __last2
)
5500 if (__comp(*__first1
, *__first2
))
5502 else if (__comp(*__first2
, *__first1
))
5506 *__result
= *__first1
;
5515 * @brief Return the difference of two sorted ranges.
5516 * @param first1 Start of first range.
5517 * @param last1 End of first range.
5518 * @param first2 Start of second range.
5519 * @param last2 End of second range.
5520 * @return End of the output range.
5521 * @ingroup setoperations
5523 * This operation iterates over both ranges, copying elements present in
5524 * the first range but not the second in order to the output range.
5525 * Iterators increment for each range. When the current element of the
5526 * first range is less than the second, that element is copied and the
5527 * iterator advances. If the current element of the second range is less,
5528 * the iterator advances, but no element is copied. If an element is
5529 * contained in both ranges, no elements are copied and both ranges
5530 * advance. The output range may not overlap either input range.
5532 template<typename _InputIterator1
, typename _InputIterator2
,
5533 typename _OutputIterator
>
5535 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5536 _InputIterator2 __first2
, _InputIterator2 __last2
,
5537 _OutputIterator __result
)
5539 typedef typename iterator_traits
<_InputIterator1
>::value_type
5541 typedef typename iterator_traits
<_InputIterator2
>::value_type
5544 // concept requirements
5545 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5546 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5547 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5549 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5550 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5551 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5552 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5554 while (__first1
!= __last1
&& __first2
!= __last2
)
5555 if (*__first1
< *__first2
)
5557 *__result
= *__first1
;
5561 else if (*__first2
< *__first1
)
5568 return std::copy(__first1
, __last1
, __result
);
5572 * @brief Return the difference of two sorted ranges using comparison
5574 * @param first1 Start of first range.
5575 * @param last1 End of first range.
5576 * @param first2 Start of second range.
5577 * @param last2 End of second range.
5578 * @param comp The comparison functor.
5579 * @return End of the output range.
5580 * @ingroup setoperations
5582 * This operation iterates over both ranges, copying elements present in
5583 * the first range but not the second in order to the output range.
5584 * Iterators increment for each range. When the current element of the
5585 * first range is less than the second according to @a comp, that element
5586 * is copied and the iterator advances. If the current element of the
5587 * second range is less, no element is copied and the iterator advances.
5588 * If an element is contained in both ranges according to @a comp, no
5589 * elements are copied and both ranges advance. The output range may not
5590 * overlap either input range.
5592 template<typename _InputIterator1
, typename _InputIterator2
,
5593 typename _OutputIterator
, typename _Compare
>
5595 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5596 _InputIterator2 __first2
, _InputIterator2 __last2
,
5597 _OutputIterator __result
, _Compare __comp
)
5599 typedef typename iterator_traits
<_InputIterator1
>::value_type
5601 typedef typename iterator_traits
<_InputIterator2
>::value_type
5604 // concept requirements
5605 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5606 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5607 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5609 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5610 _ValueType1
, _ValueType2
>)
5611 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5612 _ValueType2
, _ValueType1
>)
5613 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5614 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5616 while (__first1
!= __last1
&& __first2
!= __last2
)
5617 if (__comp(*__first1
, *__first2
))
5619 *__result
= *__first1
;
5623 else if (__comp(*__first2
, *__first1
))
5630 return std::copy(__first1
, __last1
, __result
);
5634 * @brief Return the symmetric difference of two sorted ranges.
5635 * @param first1 Start of first range.
5636 * @param last1 End of first range.
5637 * @param first2 Start of second range.
5638 * @param last2 End of second range.
5639 * @return End of the output range.
5640 * @ingroup setoperations
5642 * This operation iterates over both ranges, copying elements present in
5643 * one range but not the other in order to the output range. Iterators
5644 * increment for each range. When the current element of one range is less
5645 * than the other, that element is copied and the iterator advances. If an
5646 * element is contained in both ranges, no elements are copied and both
5647 * ranges advance. The output range may not overlap either input range.
5649 template<typename _InputIterator1
, typename _InputIterator2
,
5650 typename _OutputIterator
>
5652 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5653 _InputIterator2 __first2
, _InputIterator2 __last2
,
5654 _OutputIterator __result
)
5656 typedef typename iterator_traits
<_InputIterator1
>::value_type
5658 typedef typename iterator_traits
<_InputIterator2
>::value_type
5661 // concept requirements
5662 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5663 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5664 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5666 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5668 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5669 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5670 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5671 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5673 while (__first1
!= __last1
&& __first2
!= __last2
)
5674 if (*__first1
< *__first2
)
5676 *__result
= *__first1
;
5680 else if (*__first2
< *__first1
)
5682 *__result
= *__first2
;
5691 return std::copy(__first2
, __last2
, std::copy(__first1
,
5692 __last1
, __result
));
5696 * @brief Return the symmetric difference of two sorted ranges using
5697 * comparison functor.
5698 * @param first1 Start of first range.
5699 * @param last1 End of first range.
5700 * @param first2 Start of second range.
5701 * @param last2 End of second range.
5702 * @param comp The comparison functor.
5703 * @return End of the output range.
5704 * @ingroup setoperations
5706 * This operation iterates over both ranges, copying elements present in
5707 * one range but not the other in order to the output range. Iterators
5708 * increment for each range. When the current element of one range is less
5709 * than the other according to @a comp, that element is copied and the
5710 * iterator advances. If an element is contained in both ranges according
5711 * to @a comp, no elements are copied and both ranges advance. The output
5712 * range may not overlap either input range.
5714 template<typename _InputIterator1
, typename _InputIterator2
,
5715 typename _OutputIterator
, typename _Compare
>
5717 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5718 _InputIterator2 __first2
, _InputIterator2 __last2
,
5719 _OutputIterator __result
,
5722 typedef typename iterator_traits
<_InputIterator1
>::value_type
5724 typedef typename iterator_traits
<_InputIterator2
>::value_type
5727 // concept requirements
5728 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5729 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5730 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5732 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5734 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5735 _ValueType1
, _ValueType2
>)
5736 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5737 _ValueType2
, _ValueType1
>)
5738 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5739 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5741 while (__first1
!= __last1
&& __first2
!= __last2
)
5742 if (__comp(*__first1
, *__first2
))
5744 *__result
= *__first1
;
5748 else if (__comp(*__first2
, *__first1
))
5750 *__result
= *__first2
;
5759 return std::copy(__first2
, __last2
,
5760 std::copy(__first1
, __last1
, __result
));
5765 * @brief Return the minimum element in a range.
5766 * @param first Start of range.
5767 * @param last End of range.
5768 * @return Iterator referencing the first instance of the smallest value.
5770 template<typename _ForwardIterator
>
5772 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5774 // concept requirements
5775 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5776 __glibcxx_function_requires(_LessThanComparableConcept
<
5777 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5778 __glibcxx_requires_valid_range(__first
, __last
);
5780 if (__first
== __last
)
5782 _ForwardIterator __result
= __first
;
5783 while (++__first
!= __last
)
5784 if (*__first
< *__result
)
5790 * @brief Return the minimum element in a range using comparison functor.
5791 * @param first Start of range.
5792 * @param last End of range.
5793 * @param comp Comparison functor.
5794 * @return Iterator referencing the first instance of the smallest value
5795 * according to comp.
5797 template<typename _ForwardIterator
, typename _Compare
>
5799 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5802 // concept requirements
5803 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5804 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5805 typename iterator_traits
<_ForwardIterator
>::value_type
,
5806 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5807 __glibcxx_requires_valid_range(__first
, __last
);
5809 if (__first
== __last
)
5811 _ForwardIterator __result
= __first
;
5812 while (++__first
!= __last
)
5813 if (__comp(*__first
, *__result
))
5819 * @brief Return the maximum element in a range.
5820 * @param first Start of range.
5821 * @param last End of range.
5822 * @return Iterator referencing the first instance of the largest value.
5824 template<typename _ForwardIterator
>
5826 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5828 // concept requirements
5829 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5830 __glibcxx_function_requires(_LessThanComparableConcept
<
5831 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5832 __glibcxx_requires_valid_range(__first
, __last
);
5834 if (__first
== __last
)
5836 _ForwardIterator __result
= __first
;
5837 while (++__first
!= __last
)
5838 if (*__result
< *__first
)
5844 * @brief Return the maximum element in a range using comparison functor.
5845 * @param first Start of range.
5846 * @param last End of range.
5847 * @param comp Comparison functor.
5848 * @return Iterator referencing the first instance of the largest value
5849 * according to comp.
5851 template<typename _ForwardIterator
, typename _Compare
>
5853 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5856 // concept requirements
5857 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5858 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5859 typename iterator_traits
<_ForwardIterator
>::value_type
,
5860 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5861 __glibcxx_requires_valid_range(__first
, __last
);
5863 if (__first
== __last
) return __first
;
5864 _ForwardIterator __result
= __first
;
5865 while (++__first
!= __last
)
5866 if (__comp(*__result
, *__first
))
5871 _GLIBCXX_END_NESTED_NAMESPACE
5873 #endif /* _STL_ALGO_H */