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
));
806 * @brief Copy a sequence, removing elements of a given value.
807 * @param first An input iterator.
808 * @param last An input iterator.
809 * @param result An output iterator.
810 * @param value The value to be removed.
811 * @return An iterator designating the end of the resulting sequence.
813 * Copies each element in the range @p [first,last) not equal to @p value
814 * to the range beginning at @p result.
815 * remove_copy() is stable, so the relative order of elements that are
816 * copied is unchanged.
818 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
820 remove_copy(_InputIterator __first
, _InputIterator __last
,
821 _OutputIterator __result
, const _Tp
& __value
)
823 // concept requirements
824 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
825 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
826 typename iterator_traits
<_InputIterator
>::value_type
>)
827 __glibcxx_function_requires(_EqualOpConcept
<
828 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
829 __glibcxx_requires_valid_range(__first
, __last
);
831 for (; __first
!= __last
; ++__first
)
832 if (!(*__first
== __value
))
834 *__result
= *__first
;
841 * @brief Copy a sequence, removing elements for which a predicate is true.
842 * @param first An input iterator.
843 * @param last An input iterator.
844 * @param result An output iterator.
845 * @param pred A predicate.
846 * @return An iterator designating the end of the resulting sequence.
848 * Copies each element in the range @p [first,last) for which
849 * @p pred returns false to the range beginning at @p result.
851 * remove_copy_if() is stable, so the relative order of elements that are
852 * copied is unchanged.
854 template<typename _InputIterator
, typename _OutputIterator
,
857 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
858 _OutputIterator __result
, _Predicate __pred
)
860 // concept requirements
861 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
862 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
863 typename iterator_traits
<_InputIterator
>::value_type
>)
864 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
865 typename iterator_traits
<_InputIterator
>::value_type
>)
866 __glibcxx_requires_valid_range(__first
, __last
);
868 for (; __first
!= __last
; ++__first
)
869 if (!bool(__pred(*__first
)))
871 *__result
= *__first
;
877 #ifdef __GXX_EXPERIMENTAL_CXX0X__
879 * @brief Copy the elements of a sequence for which a predicate is true.
880 * @param first An input iterator.
881 * @param last An input iterator.
882 * @param result An output iterator.
883 * @param pred A predicate.
884 * @return An iterator designating the end of the resulting sequence.
886 * Copies each element in the range @p [first,last) for which
887 * @p pred returns true to the range beginning at @p result.
889 * copy_if() is stable, so the relative order of elements that are
890 * copied is unchanged.
892 template<typename _InputIterator
, typename _OutputIterator
,
895 copy_if(_InputIterator __first
, _InputIterator __last
,
896 _OutputIterator __result
, _Predicate __pred
)
898 // concept requirements
899 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
900 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
901 typename iterator_traits
<_InputIterator
>::value_type
>)
902 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
903 typename iterator_traits
<_InputIterator
>::value_type
>)
904 __glibcxx_requires_valid_range(__first
, __last
);
906 for (; __first
!= __last
; ++__first
)
907 if (__pred(*__first
))
909 *__result
= *__first
;
916 * @brief Copy the elements of a sequence to separate output sequences
917 * depending on the truth value of a predicate.
918 * @param first An input iterator.
919 * @param last An input iterator.
920 * @param out_true An output iterator.
921 * @param out_false An output iterator.
922 * @param pred A predicate.
923 * @return A pair designating the ends of the resulting sequences.
925 * Copies each element in the range @p [first,last) for which
926 * @p pred returns true to the range beginning at @p out_true
927 * and each element for which @p pred returns false to @p out_false.
929 template<typename _InputIterator
, typename _OutputIterator1
,
930 typename _OutputIterator2
, typename _Predicate
>
931 pair
<_OutputIterator1
, _OutputIterator2
>
932 partition_copy(_InputIterator __first
, _InputIterator __last
,
933 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
936 // concept requirements
937 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
938 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
939 typename iterator_traits
<_InputIterator
>::value_type
>)
940 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
941 typename iterator_traits
<_InputIterator
>::value_type
>)
942 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
943 typename iterator_traits
<_InputIterator
>::value_type
>)
944 __glibcxx_requires_valid_range(__first
, __last
);
946 for (; __first
!= __last
; ++__first
)
947 if (__pred(*__first
))
949 *__out_true
= *__first
;
954 *__out_false
= *__first
;
958 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
963 * @brief Remove elements from a sequence.
964 * @param first An input iterator.
965 * @param last An input iterator.
966 * @param value The value to be removed.
967 * @return An iterator designating the end of the resulting sequence.
969 * All elements equal to @p value are removed from the range
972 * remove() is stable, so the relative order of elements that are
973 * not removed is unchanged.
975 * Elements between the end of the resulting sequence and @p last
976 * are still present, but their value is unspecified.
978 template<typename _ForwardIterator
, typename _Tp
>
980 remove(_ForwardIterator __first
, _ForwardIterator __last
,
983 // concept requirements
984 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
986 __glibcxx_function_requires(_EqualOpConcept
<
987 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
988 __glibcxx_requires_valid_range(__first
, __last
);
990 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
991 if(__first
== __last
)
993 _ForwardIterator __result
= __first
;
995 for(; __first
!= __last
; ++__first
)
996 if(!(*__first
== __value
))
998 *__result
= _GLIBCXX_MOVE(*__first
);
1005 * @brief Remove elements from a sequence using a predicate.
1006 * @param first A forward iterator.
1007 * @param last A forward iterator.
1008 * @param pred A predicate.
1009 * @return An iterator designating the end of the resulting sequence.
1011 * All elements for which @p pred returns true are removed from the range
1014 * remove_if() is stable, so the relative order of elements that are
1015 * not removed is unchanged.
1017 * Elements between the end of the resulting sequence and @p last
1018 * are still present, but their value is unspecified.
1020 template<typename _ForwardIterator
, typename _Predicate
>
1022 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1025 // concept requirements
1026 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1028 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1029 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1030 __glibcxx_requires_valid_range(__first
, __last
);
1032 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
1033 if(__first
== __last
)
1035 _ForwardIterator __result
= __first
;
1037 for(; __first
!= __last
; ++__first
)
1038 if(!bool(__pred(*__first
)))
1040 *__result
= _GLIBCXX_MOVE(*__first
);
1047 * @brief Remove consecutive duplicate values from a sequence.
1048 * @param first A forward iterator.
1049 * @param last A forward iterator.
1050 * @return An iterator designating the end of the resulting sequence.
1052 * Removes all but the first element from each group of consecutive
1053 * values that compare equal.
1054 * unique() is stable, so the relative order of elements that are
1055 * not removed is unchanged.
1056 * Elements between the end of the resulting sequence and @p last
1057 * are still present, but their value is unspecified.
1059 template<typename _ForwardIterator
>
1061 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1063 // concept requirements
1064 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1066 __glibcxx_function_requires(_EqualityComparableConcept
<
1067 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1068 __glibcxx_requires_valid_range(__first
, __last
);
1070 // Skip the beginning, if already unique.
1071 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1072 if (__first
== __last
)
1075 // Do the real copy work.
1076 _ForwardIterator __dest
= __first
;
1078 while (++__first
!= __last
)
1079 if (!(*__dest
== *__first
))
1080 *++__dest
= _GLIBCXX_MOVE(*__first
);
1085 * @brief Remove consecutive values from a sequence using a predicate.
1086 * @param first A forward iterator.
1087 * @param last A forward iterator.
1088 * @param binary_pred A binary predicate.
1089 * @return An iterator designating the end of the resulting sequence.
1091 * Removes all but the first element from each group of consecutive
1092 * values for which @p binary_pred returns true.
1093 * unique() is stable, so the relative order of elements that are
1094 * not removed is unchanged.
1095 * Elements between the end of the resulting sequence and @p last
1096 * are still present, but their value is unspecified.
1098 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1100 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1101 _BinaryPredicate __binary_pred
)
1103 // concept requirements
1104 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1106 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1107 typename iterator_traits
<_ForwardIterator
>::value_type
,
1108 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1109 __glibcxx_requires_valid_range(__first
, __last
);
1111 // Skip the beginning, if already unique.
1112 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1113 if (__first
== __last
)
1116 // Do the real copy work.
1117 _ForwardIterator __dest
= __first
;
1119 while (++__first
!= __last
)
1120 if (!bool(__binary_pred(*__dest
, *__first
)))
1121 *++__dest
= _GLIBCXX_MOVE(*__first
);
1126 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1128 * overloaded for forward iterators and output iterator as result.
1130 template<typename _ForwardIterator
, typename _OutputIterator
>
1132 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1133 _OutputIterator __result
,
1134 forward_iterator_tag
, output_iterator_tag
)
1136 // concept requirements -- taken care of in dispatching function
1137 _ForwardIterator __next
= __first
;
1138 *__result
= *__first
;
1139 while (++__next
!= __last
)
1140 if (!(*__first
== *__next
))
1143 *++__result
= *__first
;
1149 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1151 * overloaded for input iterators and output iterator as result.
1153 template<typename _InputIterator
, typename _OutputIterator
>
1155 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1156 _OutputIterator __result
,
1157 input_iterator_tag
, output_iterator_tag
)
1159 // concept requirements -- taken care of in dispatching function
1160 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1161 *__result
= __value
;
1162 while (++__first
!= __last
)
1163 if (!(__value
== *__first
))
1166 *++__result
= __value
;
1172 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1174 * overloaded for input iterators and forward iterator as result.
1176 template<typename _InputIterator
, typename _ForwardIterator
>
1178 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1179 _ForwardIterator __result
,
1180 input_iterator_tag
, forward_iterator_tag
)
1182 // concept requirements -- taken care of in dispatching function
1183 *__result
= *__first
;
1184 while (++__first
!= __last
)
1185 if (!(*__result
== *__first
))
1186 *++__result
= *__first
;
1191 * This is an uglified
1192 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1194 * overloaded for forward iterators and output iterator as result.
1196 template<typename _ForwardIterator
, typename _OutputIterator
,
1197 typename _BinaryPredicate
>
1199 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1200 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1201 forward_iterator_tag
, output_iterator_tag
)
1203 // concept requirements -- iterators already checked
1204 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1205 typename iterator_traits
<_ForwardIterator
>::value_type
,
1206 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1208 _ForwardIterator __next
= __first
;
1209 *__result
= *__first
;
1210 while (++__next
!= __last
)
1211 if (!bool(__binary_pred(*__first
, *__next
)))
1214 *++__result
= *__first
;
1220 * This is an uglified
1221 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1223 * overloaded for input iterators and output iterator as result.
1225 template<typename _InputIterator
, typename _OutputIterator
,
1226 typename _BinaryPredicate
>
1228 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1229 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1230 input_iterator_tag
, output_iterator_tag
)
1232 // concept requirements -- iterators already checked
1233 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1234 typename iterator_traits
<_InputIterator
>::value_type
,
1235 typename iterator_traits
<_InputIterator
>::value_type
>)
1237 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1238 *__result
= __value
;
1239 while (++__first
!= __last
)
1240 if (!bool(__binary_pred(__value
, *__first
)))
1243 *++__result
= __value
;
1249 * This is an uglified
1250 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1252 * overloaded for input iterators and forward iterator as result.
1254 template<typename _InputIterator
, typename _ForwardIterator
,
1255 typename _BinaryPredicate
>
1257 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1258 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1259 input_iterator_tag
, forward_iterator_tag
)
1261 // concept requirements -- iterators already checked
1262 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1263 typename iterator_traits
<_ForwardIterator
>::value_type
,
1264 typename iterator_traits
<_InputIterator
>::value_type
>)
1266 *__result
= *__first
;
1267 while (++__first
!= __last
)
1268 if (!bool(__binary_pred(*__result
, *__first
)))
1269 *++__result
= *__first
;
1274 * This is an uglified reverse(_BidirectionalIterator,
1275 * _BidirectionalIterator)
1276 * overloaded for bidirectional iterators.
1278 template<typename _BidirectionalIterator
>
1280 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1281 bidirectional_iterator_tag
)
1284 if (__first
== __last
|| __first
== --__last
)
1288 std::iter_swap(__first
, __last
);
1294 * This is an uglified reverse(_BidirectionalIterator,
1295 * _BidirectionalIterator)
1296 * overloaded for random access iterators.
1298 template<typename _RandomAccessIterator
>
1300 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1301 random_access_iterator_tag
)
1303 if (__first
== __last
)
1306 while (__first
< __last
)
1308 std::iter_swap(__first
, __last
);
1315 * @brief Reverse a sequence.
1316 * @param first A bidirectional iterator.
1317 * @param last A bidirectional iterator.
1318 * @return reverse() returns no value.
1320 * Reverses the order of the elements in the range @p [first,last),
1321 * so that the first element becomes the last etc.
1322 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1323 * swaps @p *(first+i) and @p *(last-(i+1))
1325 template<typename _BidirectionalIterator
>
1327 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1329 // concept requirements
1330 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1331 _BidirectionalIterator
>)
1332 __glibcxx_requires_valid_range(__first
, __last
);
1333 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1337 * @brief Copy a sequence, reversing its elements.
1338 * @param first A bidirectional iterator.
1339 * @param last A bidirectional iterator.
1340 * @param result An output iterator.
1341 * @return An iterator designating the end of the resulting sequence.
1343 * Copies the elements in the range @p [first,last) to the range
1344 * @p [result,result+(last-first)) such that the order of the
1345 * elements is reversed.
1346 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1347 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1348 * The ranges @p [first,last) and @p [result,result+(last-first))
1351 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1353 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1354 _OutputIterator __result
)
1356 // concept requirements
1357 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1358 _BidirectionalIterator
>)
1359 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1360 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1361 __glibcxx_requires_valid_range(__first
, __last
);
1363 while (__first
!= __last
)
1366 *__result
= *__last
;
1373 * This is a helper function for the rotate algorithm specialized on RAIs.
1374 * It returns the greatest common divisor of two integer values.
1376 template<typename _EuclideanRingElement
>
1377 _EuclideanRingElement
1378 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1382 _EuclideanRingElement __t
= __m
% __n
;
1389 /// This is a helper function for the rotate algorithm.
1390 template<typename _ForwardIterator
>
1392 __rotate(_ForwardIterator __first
,
1393 _ForwardIterator __middle
,
1394 _ForwardIterator __last
,
1395 forward_iterator_tag
)
1397 if (__first
== __middle
|| __last
== __middle
)
1400 _ForwardIterator __first2
= __middle
;
1403 std::iter_swap(__first
, __first2
);
1406 if (__first
== __middle
)
1407 __middle
= __first2
;
1409 while (__first2
!= __last
);
1411 __first2
= __middle
;
1413 while (__first2
!= __last
)
1415 std::iter_swap(__first
, __first2
);
1418 if (__first
== __middle
)
1419 __middle
= __first2
;
1420 else if (__first2
== __last
)
1421 __first2
= __middle
;
1425 /// This is a helper function for the rotate algorithm.
1426 template<typename _BidirectionalIterator
>
1428 __rotate(_BidirectionalIterator __first
,
1429 _BidirectionalIterator __middle
,
1430 _BidirectionalIterator __last
,
1431 bidirectional_iterator_tag
)
1433 // concept requirements
1434 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1435 _BidirectionalIterator
>)
1437 if (__first
== __middle
|| __last
== __middle
)
1440 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1441 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1443 while (__first
!= __middle
&& __middle
!= __last
)
1445 std::iter_swap(__first
, --__last
);
1449 if (__first
== __middle
)
1450 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1452 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1455 /// This is a helper function for the rotate algorithm.
1456 template<typename _RandomAccessIterator
>
1458 __rotate(_RandomAccessIterator __first
,
1459 _RandomAccessIterator __middle
,
1460 _RandomAccessIterator __last
,
1461 random_access_iterator_tag
)
1463 // concept requirements
1464 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1465 _RandomAccessIterator
>)
1467 if (__first
== __middle
|| __last
== __middle
)
1470 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1472 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1475 const _Distance __n
= __last
- __first
;
1476 const _Distance __k
= __middle
- __first
;
1477 const _Distance __l
= __n
- __k
;
1481 std::swap_ranges(__first
, __middle
, __middle
);
1485 const _Distance __d
= std::__gcd(__n
, __k
);
1487 for (_Distance __i
= 0; __i
< __d
; __i
++)
1489 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1490 _RandomAccessIterator __p
= __first
;
1494 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1496 if (__p
> __first
+ __l
)
1498 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1502 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1508 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1510 if (__p
< __last
- __k
)
1512 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1515 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1520 *__p
= _GLIBCXX_MOVE(__tmp
);
1526 * @brief Rotate the elements of a sequence.
1527 * @param first A forward iterator.
1528 * @param middle A forward iterator.
1529 * @param last A forward iterator.
1532 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1533 * positions so that the element at @p middle is moved to @p first, the
1534 * element at @p middle+1 is moved to @first+1 and so on for each element
1535 * in the range @p [first,last).
1537 * This effectively swaps the ranges @p [first,middle) and
1540 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1541 * each @p n in the range @p [0,last-first).
1543 template<typename _ForwardIterator
>
1545 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1546 _ForwardIterator __last
)
1548 // concept requirements
1549 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1551 __glibcxx_requires_valid_range(__first
, __middle
);
1552 __glibcxx_requires_valid_range(__middle
, __last
);
1554 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1556 std::__rotate(__first
, __middle
, __last
, _IterType());
1560 * @brief Copy a sequence, rotating its elements.
1561 * @param first A forward iterator.
1562 * @param middle A forward iterator.
1563 * @param last A forward iterator.
1564 * @param result An output iterator.
1565 * @return An iterator designating the end of the resulting sequence.
1567 * Copies the elements of the range @p [first,last) to the range
1568 * beginning at @result, rotating the copied elements by @p (middle-first)
1569 * positions so that the element at @p middle is moved to @p result, the
1570 * element at @p middle+1 is moved to @result+1 and so on for each element
1571 * in the range @p [first,last).
1573 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1574 * each @p n in the range @p [0,last-first).
1576 template<typename _ForwardIterator
, typename _OutputIterator
>
1578 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1579 _ForwardIterator __last
, _OutputIterator __result
)
1581 // concept requirements
1582 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1583 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1584 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1585 __glibcxx_requires_valid_range(__first
, __middle
);
1586 __glibcxx_requires_valid_range(__middle
, __last
);
1588 return std::copy(__first
, __middle
,
1589 std::copy(__middle
, __last
, __result
));
1592 /// This is a helper function...
1593 template<typename _ForwardIterator
, typename _Predicate
>
1595 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1596 _Predicate __pred
, forward_iterator_tag
)
1598 if (__first
== __last
)
1601 while (__pred(*__first
))
1602 if (++__first
== __last
)
1605 _ForwardIterator __next
= __first
;
1607 while (++__next
!= __last
)
1608 if (__pred(*__next
))
1610 std::iter_swap(__first
, __next
);
1617 /// This is a helper function...
1618 template<typename _BidirectionalIterator
, typename _Predicate
>
1619 _BidirectionalIterator
1620 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1621 _Predicate __pred
, bidirectional_iterator_tag
)
1626 if (__first
== __last
)
1628 else if (__pred(*__first
))
1634 if (__first
== __last
)
1636 else if (!bool(__pred(*__last
)))
1640 std::iter_swap(__first
, __last
);
1647 /// This is a helper function...
1648 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1650 __inplace_stable_partition(_ForwardIterator __first
,
1651 _ForwardIterator __last
,
1652 _Predicate __pred
, _Distance __len
)
1655 return __pred(*__first
) ? __last
: __first
;
1656 _ForwardIterator __middle
= __first
;
1657 std::advance(__middle
, __len
/ 2);
1658 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1662 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1666 std::rotate(__begin
, __middle
, __end
);
1667 std::advance(__begin
, std::distance(__middle
, __end
));
1671 /// This is a helper function...
1672 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1675 __stable_partition_adaptive(_ForwardIterator __first
,
1676 _ForwardIterator __last
,
1677 _Predicate __pred
, _Distance __len
,
1679 _Distance __buffer_size
)
1681 if (__len
<= __buffer_size
)
1683 _ForwardIterator __result1
= __first
;
1684 _Pointer __result2
= __buffer
;
1685 for (; __first
!= __last
; ++__first
)
1686 if (__pred(*__first
))
1688 *__result1
= *__first
;
1693 *__result2
= *__first
;
1696 std::copy(__buffer
, __result2
, __result1
);
1701 _ForwardIterator __middle
= __first
;
1702 std::advance(__middle
, __len
/ 2);
1703 _ForwardIterator __begin
=
1704 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1705 __len
/ 2, __buffer
,
1707 _ForwardIterator __end
=
1708 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1710 __buffer
, __buffer_size
);
1711 std::rotate(__begin
, __middle
, __end
);
1712 std::advance(__begin
, std::distance(__middle
, __end
));
1718 * @brief Move elements for which a predicate is true to the beginning
1719 * of a sequence, preserving relative ordering.
1720 * @param first A forward iterator.
1721 * @param last A forward iterator.
1722 * @param pred A predicate functor.
1723 * @return An iterator @p middle such that @p pred(i) is true for each
1724 * iterator @p i in the range @p [first,middle) and false for each @p i
1725 * in the range @p [middle,last).
1727 * Performs the same function as @p partition() with the additional
1728 * guarantee that the relative ordering of elements in each group is
1729 * preserved, so any two elements @p x and @p y in the range
1730 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1731 * relative ordering after calling @p stable_partition().
1733 template<typename _ForwardIterator
, typename _Predicate
>
1735 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1738 // concept requirements
1739 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1741 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1742 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1743 __glibcxx_requires_valid_range(__first
, __last
);
1745 if (__first
== __last
)
1749 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1751 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1754 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1756 if (__buf
.size() > 0)
1758 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1759 _DistanceType(__buf
.requested_size()),
1761 _DistanceType(__buf
.size()));
1764 std::__inplace_stable_partition(__first
, __last
, __pred
,
1765 _DistanceType(__buf
.requested_size()));
1769 /// This is a helper function for the sort routines.
1770 template<typename _RandomAccessIterator
>
1772 __heap_select(_RandomAccessIterator __first
,
1773 _RandomAccessIterator __middle
,
1774 _RandomAccessIterator __last
)
1776 std::make_heap(__first
, __middle
);
1777 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1778 if (*__i
< *__first
)
1779 std::__pop_heap(__first
, __middle
, __i
);
1782 /// This is a helper function for the sort routines.
1783 template<typename _RandomAccessIterator
, typename _Compare
>
1785 __heap_select(_RandomAccessIterator __first
,
1786 _RandomAccessIterator __middle
,
1787 _RandomAccessIterator __last
, _Compare __comp
)
1789 std::make_heap(__first
, __middle
, __comp
);
1790 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1791 if (__comp(*__i
, *__first
))
1792 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1798 * @brief Copy the smallest elements of a sequence.
1799 * @param first An iterator.
1800 * @param last Another iterator.
1801 * @param result_first A random-access iterator.
1802 * @param result_last Another random-access iterator.
1803 * @return An iterator indicating the end of the resulting sequence.
1805 * Copies and sorts the smallest N values from the range @p [first,last)
1806 * to the range beginning at @p result_first, where the number of
1807 * elements to be copied, @p N, is the smaller of @p (last-first) and
1808 * @p (result_last-result_first).
1809 * After the sort if @p i and @j are iterators in the range
1810 * @p [result_first,result_first+N) such that @i precedes @j then
1811 * @p *j<*i is false.
1812 * The value returned is @p result_first+N.
1814 template<typename _InputIterator
, typename _RandomAccessIterator
>
1815 _RandomAccessIterator
1816 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1817 _RandomAccessIterator __result_first
,
1818 _RandomAccessIterator __result_last
)
1820 typedef typename iterator_traits
<_InputIterator
>::value_type
1822 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1824 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1827 // concept requirements
1828 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1829 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1831 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1833 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1834 __glibcxx_requires_valid_range(__first
, __last
);
1835 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1837 if (__result_first
== __result_last
)
1838 return __result_last
;
1839 _RandomAccessIterator __result_real_last
= __result_first
;
1840 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1842 *__result_real_last
= *__first
;
1843 ++__result_real_last
;
1846 std::make_heap(__result_first
, __result_real_last
);
1847 while (__first
!= __last
)
1849 if (*__first
< *__result_first
)
1850 std::__adjust_heap(__result_first
, _DistanceType(0),
1851 _DistanceType(__result_real_last
1853 _InputValueType(*__first
));
1856 std::sort_heap(__result_first
, __result_real_last
);
1857 return __result_real_last
;
1861 * @brief Copy the smallest elements of a sequence using a predicate for
1863 * @param first An input iterator.
1864 * @param last Another input iterator.
1865 * @param result_first A random-access iterator.
1866 * @param result_last Another random-access iterator.
1867 * @param comp A comparison functor.
1868 * @return An iterator indicating the end of the resulting sequence.
1870 * Copies and sorts the smallest N values from the range @p [first,last)
1871 * to the range beginning at @p result_first, where the number of
1872 * elements to be copied, @p N, is the smaller of @p (last-first) and
1873 * @p (result_last-result_first).
1874 * After the sort if @p i and @j are iterators in the range
1875 * @p [result_first,result_first+N) such that @i precedes @j then
1876 * @p comp(*j,*i) is false.
1877 * The value returned is @p result_first+N.
1879 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1880 _RandomAccessIterator
1881 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1882 _RandomAccessIterator __result_first
,
1883 _RandomAccessIterator __result_last
,
1886 typedef typename iterator_traits
<_InputIterator
>::value_type
1888 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1890 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1893 // concept requirements
1894 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1895 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1896 _RandomAccessIterator
>)
1897 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1899 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1900 _InputValueType
, _OutputValueType
>)
1901 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1902 _OutputValueType
, _OutputValueType
>)
1903 __glibcxx_requires_valid_range(__first
, __last
);
1904 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1906 if (__result_first
== __result_last
)
1907 return __result_last
;
1908 _RandomAccessIterator __result_real_last
= __result_first
;
1909 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1911 *__result_real_last
= *__first
;
1912 ++__result_real_last
;
1915 std::make_heap(__result_first
, __result_real_last
, __comp
);
1916 while (__first
!= __last
)
1918 if (__comp(*__first
, *__result_first
))
1919 std::__adjust_heap(__result_first
, _DistanceType(0),
1920 _DistanceType(__result_real_last
1922 _InputValueType(*__first
),
1926 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1927 return __result_real_last
;
1930 /// This is a helper function for the sort routine.
1931 template<typename _RandomAccessIterator
, typename _Tp
>
1933 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1935 _RandomAccessIterator __next
= __last
;
1937 while (__val
< *__next
)
1946 /// This is a helper function for the sort routine.
1947 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1949 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1952 _RandomAccessIterator __next
= __last
;
1954 while (__comp(__val
, *__next
))
1963 /// This is a helper function for the sort routine.
1964 template<typename _RandomAccessIterator
>
1966 __insertion_sort(_RandomAccessIterator __first
,
1967 _RandomAccessIterator __last
)
1969 if (__first
== __last
)
1972 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1974 typename iterator_traits
<_RandomAccessIterator
>::value_type
1976 if (__val
< *__first
)
1978 std::copy_backward(__first
, __i
, __i
+ 1);
1982 std::__unguarded_linear_insert(__i
, __val
);
1986 /// This is a helper function for the sort routine.
1987 template<typename _RandomAccessIterator
, typename _Compare
>
1989 __insertion_sort(_RandomAccessIterator __first
,
1990 _RandomAccessIterator __last
, _Compare __comp
)
1992 if (__first
== __last
) return;
1994 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1996 typename iterator_traits
<_RandomAccessIterator
>::value_type
1998 if (__comp(__val
, *__first
))
2000 std::copy_backward(__first
, __i
, __i
+ 1);
2004 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2008 /// This is a helper function for the sort routine.
2009 template<typename _RandomAccessIterator
>
2011 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2012 _RandomAccessIterator __last
)
2014 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2017 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2018 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2021 /// This is a helper function for the sort routine.
2022 template<typename _RandomAccessIterator
, typename _Compare
>
2024 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2025 _RandomAccessIterator __last
, _Compare __comp
)
2027 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2030 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2031 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2036 * This controls some aspect of the sort routines.
2038 enum { _S_threshold
= 16 };
2040 /// This is a helper function for the sort routine.
2041 template<typename _RandomAccessIterator
>
2043 __final_insertion_sort(_RandomAccessIterator __first
,
2044 _RandomAccessIterator __last
)
2046 if (__last
- __first
> int(_S_threshold
))
2048 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2049 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2052 std::__insertion_sort(__first
, __last
);
2055 /// This is a helper function for the sort routine.
2056 template<typename _RandomAccessIterator
, typename _Compare
>
2058 __final_insertion_sort(_RandomAccessIterator __first
,
2059 _RandomAccessIterator __last
, _Compare __comp
)
2061 if (__last
- __first
> int(_S_threshold
))
2063 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2064 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2068 std::__insertion_sort(__first
, __last
, __comp
);
2071 /// This is a helper function...
2072 template<typename _RandomAccessIterator
, typename _Tp
>
2073 _RandomAccessIterator
2074 __unguarded_partition(_RandomAccessIterator __first
,
2075 _RandomAccessIterator __last
, _Tp __pivot
)
2079 while (*__first
< __pivot
)
2082 while (__pivot
< *__last
)
2084 if (!(__first
< __last
))
2086 std::iter_swap(__first
, __last
);
2091 /// This is a helper function...
2092 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2093 _RandomAccessIterator
2094 __unguarded_partition(_RandomAccessIterator __first
,
2095 _RandomAccessIterator __last
,
2096 _Tp __pivot
, _Compare __comp
)
2100 while (__comp(*__first
, __pivot
))
2103 while (__comp(__pivot
, *__last
))
2105 if (!(__first
< __last
))
2107 std::iter_swap(__first
, __last
);
2112 /// This is a helper function for the sort routine.
2113 template<typename _RandomAccessIterator
, typename _Size
>
2115 __introsort_loop(_RandomAccessIterator __first
,
2116 _RandomAccessIterator __last
,
2117 _Size __depth_limit
)
2119 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2122 while (__last
- __first
> int(_S_threshold
))
2124 if (__depth_limit
== 0)
2126 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2130 _RandomAccessIterator __cut
=
2131 std::__unguarded_partition(__first
, __last
,
2132 _ValueType(std::__median(*__first
,
2139 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2144 /// This is a helper function for the sort routine.
2145 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2147 __introsort_loop(_RandomAccessIterator __first
,
2148 _RandomAccessIterator __last
,
2149 _Size __depth_limit
, _Compare __comp
)
2151 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2154 while (__last
- __first
> int(_S_threshold
))
2156 if (__depth_limit
== 0)
2158 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2162 _RandomAccessIterator __cut
=
2163 std::__unguarded_partition(__first
, __last
,
2164 _ValueType(std::__median(*__first
,
2172 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2177 /// This is a helper function for the sort routines. Precondition: __n > 0.
2178 template<typename _Size
>
2183 for (__k
= 0; __n
!= 0; __n
>>= 1)
2190 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2194 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2198 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2202 template<typename _RandomAccessIterator
, typename _Size
>
2204 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2205 _RandomAccessIterator __last
, _Size __depth_limit
)
2207 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2210 while (__last
- __first
> 3)
2212 if (__depth_limit
== 0)
2214 std::__heap_select(__first
, __nth
+ 1, __last
);
2216 // Place the nth largest element in its final position.
2217 std::iter_swap(__first
, __nth
);
2221 _RandomAccessIterator __cut
=
2222 std::__unguarded_partition(__first
, __last
,
2223 _ValueType(std::__median(*__first
,
2235 std::__insertion_sort(__first
, __last
);
2238 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2240 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2241 _RandomAccessIterator __last
, _Size __depth_limit
,
2244 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2247 while (__last
- __first
> 3)
2249 if (__depth_limit
== 0)
2251 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2252 // Place the nth largest element in its final position.
2253 std::iter_swap(__first
, __nth
);
2257 _RandomAccessIterator __cut
=
2258 std::__unguarded_partition(__first
, __last
,
2259 _ValueType(std::__median(*__first
,
2272 std::__insertion_sort(__first
, __last
, __comp
);
2278 * @brief Finds the first position in which @a val could be inserted
2279 * without changing the ordering.
2280 * @param first An iterator.
2281 * @param last Another iterator.
2282 * @param val The search term.
2283 * @return An iterator pointing to the first element "not less
2284 * than" @a val, or end() if every element is less than
2286 * @ingroup binarysearch
2288 template<typename _ForwardIterator
, typename _Tp
>
2290 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2293 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2295 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2298 // concept requirements
2299 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2300 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2301 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2303 _DistanceType __len
= std::distance(__first
, __last
);
2304 _DistanceType __half
;
2305 _ForwardIterator __middle
;
2309 __half
= __len
>> 1;
2311 std::advance(__middle
, __half
);
2312 if (*__middle
< __val
)
2316 __len
= __len
- __half
- 1;
2325 * @brief Finds the first position in which @a val could be inserted
2326 * without changing the ordering.
2327 * @param first An iterator.
2328 * @param last Another iterator.
2329 * @param val The search term.
2330 * @param comp A functor to use for comparisons.
2331 * @return An iterator pointing to the first element "not less than" @a val,
2332 * or end() if every element is less than @a val.
2333 * @ingroup binarysearch
2335 * The comparison function should have the same effects on ordering as
2336 * the function used for the initial sort.
2338 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2340 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2341 const _Tp
& __val
, _Compare __comp
)
2343 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2345 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2348 // concept requirements
2349 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2350 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2352 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2355 _DistanceType __len
= std::distance(__first
, __last
);
2356 _DistanceType __half
;
2357 _ForwardIterator __middle
;
2361 __half
= __len
>> 1;
2363 std::advance(__middle
, __half
);
2364 if (__comp(*__middle
, __val
))
2368 __len
= __len
- __half
- 1;
2377 * @brief Finds the last position in which @a val could be inserted
2378 * without changing the ordering.
2379 * @param first An iterator.
2380 * @param last Another iterator.
2381 * @param val The search term.
2382 * @return An iterator pointing to the first element greater than @a val,
2383 * or end() if no elements are greater than @a val.
2384 * @ingroup binarysearch
2386 template<typename _ForwardIterator
, typename _Tp
>
2388 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2391 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2393 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2396 // concept requirements
2397 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2398 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2399 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2401 _DistanceType __len
= std::distance(__first
, __last
);
2402 _DistanceType __half
;
2403 _ForwardIterator __middle
;
2407 __half
= __len
>> 1;
2409 std::advance(__middle
, __half
);
2410 if (__val
< *__middle
)
2416 __len
= __len
- __half
- 1;
2423 * @brief Finds the last position in which @a val could be inserted
2424 * without changing the ordering.
2425 * @param first An iterator.
2426 * @param last Another iterator.
2427 * @param val The search term.
2428 * @param comp A functor to use for comparisons.
2429 * @return An iterator pointing to the first element greater than @a val,
2430 * or end() if no elements are greater than @a val.
2431 * @ingroup binarysearch
2433 * The comparison function should have the same effects on ordering as
2434 * the function used for the initial sort.
2436 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2438 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2439 const _Tp
& __val
, _Compare __comp
)
2441 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2443 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2446 // concept requirements
2447 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2448 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2450 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2453 _DistanceType __len
= std::distance(__first
, __last
);
2454 _DistanceType __half
;
2455 _ForwardIterator __middle
;
2459 __half
= __len
>> 1;
2461 std::advance(__middle
, __half
);
2462 if (__comp(__val
, *__middle
))
2468 __len
= __len
- __half
- 1;
2475 * @brief Finds the largest subrange in which @a val could be inserted
2476 * at any place in it without changing the ordering.
2477 * @param first An iterator.
2478 * @param last Another iterator.
2479 * @param val The search term.
2480 * @return An pair of iterators defining the subrange.
2481 * @ingroup binarysearch
2483 * This is equivalent to
2485 * std::make_pair(lower_bound(first, last, val),
2486 * upper_bound(first, last, val))
2488 * but does not actually call those functions.
2490 template<typename _ForwardIterator
, typename _Tp
>
2491 pair
<_ForwardIterator
, _ForwardIterator
>
2492 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2495 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2497 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2500 // concept requirements
2501 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2502 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2503 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2504 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2505 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2507 _DistanceType __len
= std::distance(__first
, __last
);
2508 _DistanceType __half
;
2509 _ForwardIterator __middle
, __left
, __right
;
2513 __half
= __len
>> 1;
2515 std::advance(__middle
, __half
);
2516 if (*__middle
< __val
)
2520 __len
= __len
- __half
- 1;
2522 else if (__val
< *__middle
)
2526 __left
= std::lower_bound(__first
, __middle
, __val
);
2527 std::advance(__first
, __len
);
2528 __right
= std::upper_bound(++__middle
, __first
, __val
);
2529 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2532 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2536 * @brief Finds the largest subrange in which @a val could be inserted
2537 * at any place in it without changing the ordering.
2538 * @param first An iterator.
2539 * @param last Another iterator.
2540 * @param val The search term.
2541 * @param comp A functor to use for comparisons.
2542 * @return An pair of iterators defining the subrange.
2543 * @ingroup binarysearch
2545 * This is equivalent to
2547 * std::make_pair(lower_bound(first, last, val, comp),
2548 * upper_bound(first, last, val, comp))
2550 * but does not actually call those functions.
2552 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2553 pair
<_ForwardIterator
, _ForwardIterator
>
2554 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2558 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2560 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2563 // concept requirements
2564 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2565 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2567 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2569 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2571 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2574 _DistanceType __len
= std::distance(__first
, __last
);
2575 _DistanceType __half
;
2576 _ForwardIterator __middle
, __left
, __right
;
2580 __half
= __len
>> 1;
2582 std::advance(__middle
, __half
);
2583 if (__comp(*__middle
, __val
))
2587 __len
= __len
- __half
- 1;
2589 else if (__comp(__val
, *__middle
))
2593 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2594 std::advance(__first
, __len
);
2595 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2596 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2599 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2603 * @brief Determines whether an element exists in a range.
2604 * @param first An iterator.
2605 * @param last Another iterator.
2606 * @param val The search term.
2607 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2608 * @ingroup binarysearch
2610 * Note that this does not actually return an iterator to @a val. For
2611 * that, use std::find or a container's specialized find member functions.
2613 template<typename _ForwardIterator
, typename _Tp
>
2615 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2618 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2621 // concept requirements
2622 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2623 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2624 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2625 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2627 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2628 return __i
!= __last
&& !(__val
< *__i
);
2632 * @brief Determines whether an element exists in a range.
2633 * @param first An iterator.
2634 * @param last Another iterator.
2635 * @param val The search term.
2636 * @param comp A functor to use for comparisons.
2637 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2638 * @ingroup binarysearch
2640 * Note that this does not actually return an iterator to @a val. For
2641 * that, use std::find or a container's specialized find member functions.
2643 * The comparison function should have the same effects on ordering as
2644 * the function used for the initial sort.
2646 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2648 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2649 const _Tp
& __val
, _Compare __comp
)
2651 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2654 // concept requirements
2655 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2656 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2658 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2660 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2663 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2664 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2669 /// This is a helper function for the merge routines.
2670 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2671 typename _BidirectionalIterator3
>
2672 _BidirectionalIterator3
2673 __merge_backward(_BidirectionalIterator1 __first1
,
2674 _BidirectionalIterator1 __last1
,
2675 _BidirectionalIterator2 __first2
,
2676 _BidirectionalIterator2 __last2
,
2677 _BidirectionalIterator3 __result
)
2679 if (__first1
== __last1
)
2680 return std::copy_backward(__first2
, __last2
, __result
);
2681 if (__first2
== __last2
)
2682 return std::copy_backward(__first1
, __last1
, __result
);
2687 if (*__last2
< *__last1
)
2689 *--__result
= *__last1
;
2690 if (__first1
== __last1
)
2691 return std::copy_backward(__first2
, ++__last2
, __result
);
2696 *--__result
= *__last2
;
2697 if (__first2
== __last2
)
2698 return std::copy_backward(__first1
, ++__last1
, __result
);
2704 /// This is a helper function for the merge routines.
2705 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2706 typename _BidirectionalIterator3
, typename _Compare
>
2707 _BidirectionalIterator3
2708 __merge_backward(_BidirectionalIterator1 __first1
,
2709 _BidirectionalIterator1 __last1
,
2710 _BidirectionalIterator2 __first2
,
2711 _BidirectionalIterator2 __last2
,
2712 _BidirectionalIterator3 __result
,
2715 if (__first1
== __last1
)
2716 return std::copy_backward(__first2
, __last2
, __result
);
2717 if (__first2
== __last2
)
2718 return std::copy_backward(__first1
, __last1
, __result
);
2723 if (__comp(*__last2
, *__last1
))
2725 *--__result
= *__last1
;
2726 if (__first1
== __last1
)
2727 return std::copy_backward(__first2
, ++__last2
, __result
);
2732 *--__result
= *__last2
;
2733 if (__first2
== __last2
)
2734 return std::copy_backward(__first1
, ++__last1
, __result
);
2740 /// This is a helper function for the merge routines.
2741 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2743 _BidirectionalIterator1
2744 __rotate_adaptive(_BidirectionalIterator1 __first
,
2745 _BidirectionalIterator1 __middle
,
2746 _BidirectionalIterator1 __last
,
2747 _Distance __len1
, _Distance __len2
,
2748 _BidirectionalIterator2 __buffer
,
2749 _Distance __buffer_size
)
2751 _BidirectionalIterator2 __buffer_end
;
2752 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2754 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2755 std::copy_backward(__first
, __middle
, __last
);
2756 return std::copy(__buffer
, __buffer_end
, __first
);
2758 else if (__len1
<= __buffer_size
)
2760 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2761 std::copy(__middle
, __last
, __first
);
2762 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2766 std::rotate(__first
, __middle
, __last
);
2767 std::advance(__first
, std::distance(__middle
, __last
));
2772 /// This is a helper function for the merge routines.
2773 template<typename _BidirectionalIterator
, typename _Distance
,
2776 __merge_adaptive(_BidirectionalIterator __first
,
2777 _BidirectionalIterator __middle
,
2778 _BidirectionalIterator __last
,
2779 _Distance __len1
, _Distance __len2
,
2780 _Pointer __buffer
, _Distance __buffer_size
)
2782 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2784 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2785 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2788 else if (__len2
<= __buffer_size
)
2790 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2791 std::__merge_backward(__first
, __middle
, __buffer
,
2792 __buffer_end
, __last
);
2796 _BidirectionalIterator __first_cut
= __first
;
2797 _BidirectionalIterator __second_cut
= __middle
;
2798 _Distance __len11
= 0;
2799 _Distance __len22
= 0;
2800 if (__len1
> __len2
)
2802 __len11
= __len1
/ 2;
2803 std::advance(__first_cut
, __len11
);
2804 __second_cut
= std::lower_bound(__middle
, __last
,
2806 __len22
= std::distance(__middle
, __second_cut
);
2810 __len22
= __len2
/ 2;
2811 std::advance(__second_cut
, __len22
);
2812 __first_cut
= std::upper_bound(__first
, __middle
,
2814 __len11
= std::distance(__first
, __first_cut
);
2816 _BidirectionalIterator __new_middle
=
2817 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2818 __len1
- __len11
, __len22
, __buffer
,
2820 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2821 __len22
, __buffer
, __buffer_size
);
2822 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2824 __len2
- __len22
, __buffer
, __buffer_size
);
2828 /// This is a helper function for the merge routines.
2829 template<typename _BidirectionalIterator
, typename _Distance
,
2830 typename _Pointer
, typename _Compare
>
2832 __merge_adaptive(_BidirectionalIterator __first
,
2833 _BidirectionalIterator __middle
,
2834 _BidirectionalIterator __last
,
2835 _Distance __len1
, _Distance __len2
,
2836 _Pointer __buffer
, _Distance __buffer_size
,
2839 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2841 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2842 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2845 else if (__len2
<= __buffer_size
)
2847 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2848 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2853 _BidirectionalIterator __first_cut
= __first
;
2854 _BidirectionalIterator __second_cut
= __middle
;
2855 _Distance __len11
= 0;
2856 _Distance __len22
= 0;
2857 if (__len1
> __len2
)
2859 __len11
= __len1
/ 2;
2860 std::advance(__first_cut
, __len11
);
2861 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2863 __len22
= std::distance(__middle
, __second_cut
);
2867 __len22
= __len2
/ 2;
2868 std::advance(__second_cut
, __len22
);
2869 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2871 __len11
= std::distance(__first
, __first_cut
);
2873 _BidirectionalIterator __new_middle
=
2874 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2875 __len1
- __len11
, __len22
, __buffer
,
2877 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2878 __len22
, __buffer
, __buffer_size
, __comp
);
2879 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2881 __len2
- __len22
, __buffer
,
2882 __buffer_size
, __comp
);
2886 /// This is a helper function for the merge routines.
2887 template<typename _BidirectionalIterator
, typename _Distance
>
2889 __merge_without_buffer(_BidirectionalIterator __first
,
2890 _BidirectionalIterator __middle
,
2891 _BidirectionalIterator __last
,
2892 _Distance __len1
, _Distance __len2
)
2894 if (__len1
== 0 || __len2
== 0)
2896 if (__len1
+ __len2
== 2)
2898 if (*__middle
< *__first
)
2899 std::iter_swap(__first
, __middle
);
2902 _BidirectionalIterator __first_cut
= __first
;
2903 _BidirectionalIterator __second_cut
= __middle
;
2904 _Distance __len11
= 0;
2905 _Distance __len22
= 0;
2906 if (__len1
> __len2
)
2908 __len11
= __len1
/ 2;
2909 std::advance(__first_cut
, __len11
);
2910 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2911 __len22
= std::distance(__middle
, __second_cut
);
2915 __len22
= __len2
/ 2;
2916 std::advance(__second_cut
, __len22
);
2917 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2918 __len11
= std::distance(__first
, __first_cut
);
2920 std::rotate(__first_cut
, __middle
, __second_cut
);
2921 _BidirectionalIterator __new_middle
= __first_cut
;
2922 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2923 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2925 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2926 __len1
- __len11
, __len2
- __len22
);
2929 /// This is a helper function for the merge routines.
2930 template<typename _BidirectionalIterator
, typename _Distance
,
2933 __merge_without_buffer(_BidirectionalIterator __first
,
2934 _BidirectionalIterator __middle
,
2935 _BidirectionalIterator __last
,
2936 _Distance __len1
, _Distance __len2
,
2939 if (__len1
== 0 || __len2
== 0)
2941 if (__len1
+ __len2
== 2)
2943 if (__comp(*__middle
, *__first
))
2944 std::iter_swap(__first
, __middle
);
2947 _BidirectionalIterator __first_cut
= __first
;
2948 _BidirectionalIterator __second_cut
= __middle
;
2949 _Distance __len11
= 0;
2950 _Distance __len22
= 0;
2951 if (__len1
> __len2
)
2953 __len11
= __len1
/ 2;
2954 std::advance(__first_cut
, __len11
);
2955 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2957 __len22
= std::distance(__middle
, __second_cut
);
2961 __len22
= __len2
/ 2;
2962 std::advance(__second_cut
, __len22
);
2963 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2965 __len11
= std::distance(__first
, __first_cut
);
2967 std::rotate(__first_cut
, __middle
, __second_cut
);
2968 _BidirectionalIterator __new_middle
= __first_cut
;
2969 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2970 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2971 __len11
, __len22
, __comp
);
2972 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2973 __len1
- __len11
, __len2
- __len22
, __comp
);
2977 * @brief Merges two sorted ranges in place.
2978 * @param first An iterator.
2979 * @param middle Another iterator.
2980 * @param last Another iterator.
2983 * Merges two sorted and consecutive ranges, [first,middle) and
2984 * [middle,last), and puts the result in [first,last). The output will
2985 * be sorted. The sort is @e stable, that is, for equivalent
2986 * elements in the two ranges, elements from the first range will always
2987 * come before elements from the second.
2989 * If enough additional memory is available, this takes (last-first)-1
2990 * comparisons. Otherwise an NlogN algorithm is used, where N is
2991 * distance(first,last).
2993 template<typename _BidirectionalIterator
>
2995 inplace_merge(_BidirectionalIterator __first
,
2996 _BidirectionalIterator __middle
,
2997 _BidirectionalIterator __last
)
2999 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3001 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3004 // concept requirements
3005 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3006 _BidirectionalIterator
>)
3007 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3008 __glibcxx_requires_sorted(__first
, __middle
);
3009 __glibcxx_requires_sorted(__middle
, __last
);
3011 if (__first
== __middle
|| __middle
== __last
)
3014 _DistanceType __len1
= std::distance(__first
, __middle
);
3015 _DistanceType __len2
= std::distance(__middle
, __last
);
3017 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3019 if (__buf
.begin() == 0)
3020 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3022 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3023 __buf
.begin(), _DistanceType(__buf
.size()));
3027 * @brief Merges two sorted ranges in place.
3028 * @param first An iterator.
3029 * @param middle Another iterator.
3030 * @param last Another iterator.
3031 * @param comp A functor to use for comparisons.
3034 * Merges two sorted and consecutive ranges, [first,middle) and
3035 * [middle,last), and puts the result in [first,last). The output will
3036 * be sorted. The sort is @e stable, that is, for equivalent
3037 * elements in the two ranges, elements from the first range will always
3038 * come before elements from the second.
3040 * If enough additional memory is available, this takes (last-first)-1
3041 * comparisons. Otherwise an NlogN algorithm is used, where N is
3042 * distance(first,last).
3044 * The comparison function should have the same effects on ordering as
3045 * the function used for the initial sort.
3047 template<typename _BidirectionalIterator
, typename _Compare
>
3049 inplace_merge(_BidirectionalIterator __first
,
3050 _BidirectionalIterator __middle
,
3051 _BidirectionalIterator __last
,
3054 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3056 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3059 // concept requirements
3060 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3061 _BidirectionalIterator
>)
3062 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3063 _ValueType
, _ValueType
>)
3064 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3065 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3067 if (__first
== __middle
|| __middle
== __last
)
3070 const _DistanceType __len1
= std::distance(__first
, __middle
);
3071 const _DistanceType __len2
= std::distance(__middle
, __last
);
3073 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3075 if (__buf
.begin() == 0)
3076 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3079 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3080 __buf
.begin(), _DistanceType(__buf
.size()),
3084 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3087 __merge_sort_loop(_RandomAccessIterator1 __first
,
3088 _RandomAccessIterator1 __last
,
3089 _RandomAccessIterator2 __result
,
3090 _Distance __step_size
)
3092 const _Distance __two_step
= 2 * __step_size
;
3094 while (__last
- __first
>= __two_step
)
3096 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3097 __first
+ __step_size
,
3098 __first
+ __two_step
,
3100 __first
+= __two_step
;
3103 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3104 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3105 __first
+ __step_size
, __last
,
3109 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3110 typename _Distance
, typename _Compare
>
3112 __merge_sort_loop(_RandomAccessIterator1 __first
,
3113 _RandomAccessIterator1 __last
,
3114 _RandomAccessIterator2 __result
, _Distance __step_size
,
3117 const _Distance __two_step
= 2 * __step_size
;
3119 while (__last
- __first
>= __two_step
)
3121 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3122 __first
+ __step_size
, __first
+ __two_step
,
3125 __first
+= __two_step
;
3127 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3129 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3130 __first
+ __step_size
, __last
, __result
, __comp
);
3133 template<typename _RandomAccessIterator
, typename _Distance
>
3135 __chunk_insertion_sort(_RandomAccessIterator __first
,
3136 _RandomAccessIterator __last
,
3137 _Distance __chunk_size
)
3139 while (__last
- __first
>= __chunk_size
)
3141 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3142 __first
+= __chunk_size
;
3144 std::__insertion_sort(__first
, __last
);
3147 template<typename _RandomAccessIterator
, typename _Distance
,
3150 __chunk_insertion_sort(_RandomAccessIterator __first
,
3151 _RandomAccessIterator __last
,
3152 _Distance __chunk_size
, _Compare __comp
)
3154 while (__last
- __first
>= __chunk_size
)
3156 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3157 __first
+= __chunk_size
;
3159 std::__insertion_sort(__first
, __last
, __comp
);
3162 enum { _S_chunk_size
= 7 };
3164 template<typename _RandomAccessIterator
, typename _Pointer
>
3166 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3167 _RandomAccessIterator __last
,
3170 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3173 const _Distance __len
= __last
- __first
;
3174 const _Pointer __buffer_last
= __buffer
+ __len
;
3176 _Distance __step_size
= _S_chunk_size
;
3177 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3179 while (__step_size
< __len
)
3181 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3183 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3188 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3190 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3191 _RandomAccessIterator __last
,
3192 _Pointer __buffer
, _Compare __comp
)
3194 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3197 const _Distance __len
= __last
- __first
;
3198 const _Pointer __buffer_last
= __buffer
+ __len
;
3200 _Distance __step_size
= _S_chunk_size
;
3201 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3203 while (__step_size
< __len
)
3205 std::__merge_sort_loop(__first
, __last
, __buffer
,
3206 __step_size
, __comp
);
3208 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3209 __step_size
, __comp
);
3214 template<typename _RandomAccessIterator
, typename _Pointer
,
3217 __stable_sort_adaptive(_RandomAccessIterator __first
,
3218 _RandomAccessIterator __last
,
3219 _Pointer __buffer
, _Distance __buffer_size
)
3221 const _Distance __len
= (__last
- __first
+ 1) / 2;
3222 const _RandomAccessIterator __middle
= __first
+ __len
;
3223 if (__len
> __buffer_size
)
3225 std::__stable_sort_adaptive(__first
, __middle
,
3226 __buffer
, __buffer_size
);
3227 std::__stable_sort_adaptive(__middle
, __last
,
3228 __buffer
, __buffer_size
);
3232 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3233 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3235 std::__merge_adaptive(__first
, __middle
, __last
,
3236 _Distance(__middle
- __first
),
3237 _Distance(__last
- __middle
),
3238 __buffer
, __buffer_size
);
3241 template<typename _RandomAccessIterator
, typename _Pointer
,
3242 typename _Distance
, typename _Compare
>
3244 __stable_sort_adaptive(_RandomAccessIterator __first
,
3245 _RandomAccessIterator __last
,
3246 _Pointer __buffer
, _Distance __buffer_size
,
3249 const _Distance __len
= (__last
- __first
+ 1) / 2;
3250 const _RandomAccessIterator __middle
= __first
+ __len
;
3251 if (__len
> __buffer_size
)
3253 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3254 __buffer_size
, __comp
);
3255 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3256 __buffer_size
, __comp
);
3260 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3261 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3263 std::__merge_adaptive(__first
, __middle
, __last
,
3264 _Distance(__middle
- __first
),
3265 _Distance(__last
- __middle
),
3266 __buffer
, __buffer_size
,
3270 /// This is a helper function for the stable sorting routines.
3271 template<typename _RandomAccessIterator
>
3273 __inplace_stable_sort(_RandomAccessIterator __first
,
3274 _RandomAccessIterator __last
)
3276 if (__last
- __first
< 15)
3278 std::__insertion_sort(__first
, __last
);
3281 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3282 std::__inplace_stable_sort(__first
, __middle
);
3283 std::__inplace_stable_sort(__middle
, __last
);
3284 std::__merge_without_buffer(__first
, __middle
, __last
,
3289 /// This is a helper function for the stable sorting routines.
3290 template<typename _RandomAccessIterator
, typename _Compare
>
3292 __inplace_stable_sort(_RandomAccessIterator __first
,
3293 _RandomAccessIterator __last
, _Compare __comp
)
3295 if (__last
- __first
< 15)
3297 std::__insertion_sort(__first
, __last
, __comp
);
3300 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3301 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3302 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3303 std::__merge_without_buffer(__first
, __middle
, __last
,
3311 // Set algorithms: includes, set_union, set_intersection, set_difference,
3312 // set_symmetric_difference. All of these algorithms have the precondition
3313 // that their input ranges are sorted and the postcondition that their output
3314 // ranges are sorted.
3317 * @brief Determines whether all elements of a sequence exists in a range.
3318 * @param first1 Start of search range.
3319 * @param last1 End of search range.
3320 * @param first2 Start of sequence
3321 * @param last2 End of sequence.
3322 * @return True if each element in [first2,last2) is contained in order
3323 * within [first1,last1). False otherwise.
3324 * @ingroup setoperations
3326 * This operation expects both [first1,last1) and [first2,last2) to be
3327 * sorted. Searches for the presence of each element in [first2,last2)
3328 * within [first1,last1). The iterators over each range only move forward,
3329 * so this is a linear algorithm. If an element in [first2,last2) is not
3330 * found before the search iterator reaches @a last2, false is returned.
3332 template<typename _InputIterator1
, typename _InputIterator2
>
3334 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3335 _InputIterator2 __first2
, _InputIterator2 __last2
)
3337 typedef typename iterator_traits
<_InputIterator1
>::value_type
3339 typedef typename iterator_traits
<_InputIterator2
>::value_type
3342 // concept requirements
3343 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3344 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3345 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3346 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3347 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3348 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3350 while (__first1
!= __last1
&& __first2
!= __last2
)
3351 if (*__first2
< *__first1
)
3353 else if(*__first1
< *__first2
)
3356 ++__first1
, ++__first2
;
3358 return __first2
== __last2
;
3362 * @brief Determines whether all elements of a sequence exists in a range
3364 * @param first1 Start of search range.
3365 * @param last1 End of search range.
3366 * @param first2 Start of sequence
3367 * @param last2 End of sequence.
3368 * @param comp Comparison function to use.
3369 * @return True if each element in [first2,last2) is contained in order
3370 * within [first1,last1) according to comp. False otherwise.
3371 * @ingroup setoperations
3373 * This operation expects both [first1,last1) and [first2,last2) to be
3374 * sorted. Searches for the presence of each element in [first2,last2)
3375 * within [first1,last1), using comp to decide. The iterators over each
3376 * range only move forward, so this is a linear algorithm. If an element
3377 * in [first2,last2) is not found before the search iterator reaches @a
3378 * last2, false is returned.
3380 template<typename _InputIterator1
, typename _InputIterator2
,
3383 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3384 _InputIterator2 __first2
, _InputIterator2 __last2
,
3387 typedef typename iterator_traits
<_InputIterator1
>::value_type
3389 typedef typename iterator_traits
<_InputIterator2
>::value_type
3392 // concept requirements
3393 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3394 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3395 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3396 _ValueType1
, _ValueType2
>)
3397 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3398 _ValueType2
, _ValueType1
>)
3399 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3400 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3402 while (__first1
!= __last1
&& __first2
!= __last2
)
3403 if (__comp(*__first2
, *__first1
))
3405 else if(__comp(*__first1
, *__first2
))
3408 ++__first1
, ++__first2
;
3410 return __first2
== __last2
;
3419 // set_symmetric_difference
3424 * @brief Permute range into the next "dictionary" ordering.
3425 * @param first Start of range.
3426 * @param last End of range.
3427 * @return False if wrapped to first permutation, true otherwise.
3429 * Treats all permutations of the range as a set of "dictionary" sorted
3430 * sequences. Permutes the current sequence into the next one of this set.
3431 * Returns true if there are more sequences to generate. If the sequence
3432 * is the largest of the set, the smallest is generated and false returned.
3434 template<typename _BidirectionalIterator
>
3436 next_permutation(_BidirectionalIterator __first
,
3437 _BidirectionalIterator __last
)
3439 // concept requirements
3440 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3441 _BidirectionalIterator
>)
3442 __glibcxx_function_requires(_LessThanComparableConcept
<
3443 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3444 __glibcxx_requires_valid_range(__first
, __last
);
3446 if (__first
== __last
)
3448 _BidirectionalIterator __i
= __first
;
3457 _BidirectionalIterator __ii
= __i
;
3461 _BidirectionalIterator __j
= __last
;
3462 while (!(*__i
< *--__j
))
3464 std::iter_swap(__i
, __j
);
3465 std::reverse(__ii
, __last
);
3470 std::reverse(__first
, __last
);
3477 * @brief Permute range into the next "dictionary" ordering using
3478 * comparison functor.
3479 * @param first Start of range.
3480 * @param last End of range.
3481 * @param comp A comparison functor.
3482 * @return False if wrapped to first permutation, true otherwise.
3484 * Treats all permutations of the range [first,last) as a set of
3485 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3486 * sequence into the next one of this set. Returns true if there are more
3487 * sequences to generate. If the sequence is the largest of the set, the
3488 * smallest is generated and false returned.
3490 template<typename _BidirectionalIterator
, typename _Compare
>
3492 next_permutation(_BidirectionalIterator __first
,
3493 _BidirectionalIterator __last
, _Compare __comp
)
3495 // concept requirements
3496 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3497 _BidirectionalIterator
>)
3498 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3499 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3500 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3501 __glibcxx_requires_valid_range(__first
, __last
);
3503 if (__first
== __last
)
3505 _BidirectionalIterator __i
= __first
;
3514 _BidirectionalIterator __ii
= __i
;
3516 if (__comp(*__i
, *__ii
))
3518 _BidirectionalIterator __j
= __last
;
3519 while (!bool(__comp(*__i
, *--__j
)))
3521 std::iter_swap(__i
, __j
);
3522 std::reverse(__ii
, __last
);
3527 std::reverse(__first
, __last
);
3534 * @brief Permute range into the previous "dictionary" ordering.
3535 * @param first Start of range.
3536 * @param last End of range.
3537 * @return False if wrapped to last permutation, true otherwise.
3539 * Treats all permutations of the range as a set of "dictionary" sorted
3540 * sequences. Permutes the current sequence into the previous one of this
3541 * set. Returns true if there are more sequences to generate. If the
3542 * sequence is the smallest of the set, the largest is generated and false
3545 template<typename _BidirectionalIterator
>
3547 prev_permutation(_BidirectionalIterator __first
,
3548 _BidirectionalIterator __last
)
3550 // concept requirements
3551 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3552 _BidirectionalIterator
>)
3553 __glibcxx_function_requires(_LessThanComparableConcept
<
3554 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3555 __glibcxx_requires_valid_range(__first
, __last
);
3557 if (__first
== __last
)
3559 _BidirectionalIterator __i
= __first
;
3568 _BidirectionalIterator __ii
= __i
;
3572 _BidirectionalIterator __j
= __last
;
3573 while (!(*--__j
< *__i
))
3575 std::iter_swap(__i
, __j
);
3576 std::reverse(__ii
, __last
);
3581 std::reverse(__first
, __last
);
3588 * @brief Permute range into the previous "dictionary" ordering using
3589 * comparison functor.
3590 * @param first Start of range.
3591 * @param last End of range.
3592 * @param comp A comparison functor.
3593 * @return False if wrapped to last permutation, true otherwise.
3595 * Treats all permutations of the range [first,last) as a set of
3596 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3597 * sequence into the previous one of this set. Returns true if there are
3598 * more sequences to generate. If the sequence is the smallest of the set,
3599 * the largest is generated and false returned.
3601 template<typename _BidirectionalIterator
, typename _Compare
>
3603 prev_permutation(_BidirectionalIterator __first
,
3604 _BidirectionalIterator __last
, _Compare __comp
)
3606 // concept requirements
3607 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3608 _BidirectionalIterator
>)
3609 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3610 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3611 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3612 __glibcxx_requires_valid_range(__first
, __last
);
3614 if (__first
== __last
)
3616 _BidirectionalIterator __i
= __first
;
3625 _BidirectionalIterator __ii
= __i
;
3627 if (__comp(*__ii
, *__i
))
3629 _BidirectionalIterator __j
= __last
;
3630 while (!bool(__comp(*--__j
, *__i
)))
3632 std::iter_swap(__i
, __j
);
3633 std::reverse(__ii
, __last
);
3638 std::reverse(__first
, __last
);
3648 * @brief Copy a sequence, replacing each element of one value with another
3650 * @param first An input iterator.
3651 * @param last An input iterator.
3652 * @param result An output iterator.
3653 * @param old_value The value to be replaced.
3654 * @param new_value The replacement value.
3655 * @return The end of the output sequence, @p result+(last-first).
3657 * Copies each element in the input range @p [first,last) to the
3658 * output range @p [result,result+(last-first)) replacing elements
3659 * equal to @p old_value with @p new_value.
3661 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3663 replace_copy(_InputIterator __first
, _InputIterator __last
,
3664 _OutputIterator __result
,
3665 const _Tp
& __old_value
, const _Tp
& __new_value
)
3667 // concept requirements
3668 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3669 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3670 typename iterator_traits
<_InputIterator
>::value_type
>)
3671 __glibcxx_function_requires(_EqualOpConcept
<
3672 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3673 __glibcxx_requires_valid_range(__first
, __last
);
3675 for (; __first
!= __last
; ++__first
, ++__result
)
3676 if (*__first
== __old_value
)
3677 *__result
= __new_value
;
3679 *__result
= *__first
;
3684 * @brief Copy a sequence, replacing each value for which a predicate
3685 * returns true with another value.
3686 * @param first An input iterator.
3687 * @param last An input iterator.
3688 * @param result An output iterator.
3689 * @param pred A predicate.
3690 * @param new_value The replacement value.
3691 * @return The end of the output sequence, @p result+(last-first).
3693 * Copies each element in the range @p [first,last) to the range
3694 * @p [result,result+(last-first)) replacing elements for which
3695 * @p pred returns true with @p new_value.
3697 template<typename _InputIterator
, typename _OutputIterator
,
3698 typename _Predicate
, typename _Tp
>
3700 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3701 _OutputIterator __result
,
3702 _Predicate __pred
, const _Tp
& __new_value
)
3704 // concept requirements
3705 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3706 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3707 typename iterator_traits
<_InputIterator
>::value_type
>)
3708 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3709 typename iterator_traits
<_InputIterator
>::value_type
>)
3710 __glibcxx_requires_valid_range(__first
, __last
);
3712 for (; __first
!= __last
; ++__first
, ++__result
)
3713 if (__pred(*__first
))
3714 *__result
= __new_value
;
3716 *__result
= *__first
;
3720 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3722 * @brief Determines whether the elements of a sequence are sorted.
3723 * @param first An iterator.
3724 * @param last Another iterator.
3725 * @return True if the elements are sorted, false otherwise.
3727 template<typename _ForwardIterator
>
3729 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3730 { return std::is_sorted_until(__first
, __last
) == __last
; }
3733 * @brief Determines whether the elements of a sequence are sorted
3734 * according to a comparison functor.
3735 * @param first An iterator.
3736 * @param last Another iterator.
3737 * @param comp A comparison functor.
3738 * @return True if the elements are sorted, false otherwise.
3740 template<typename _ForwardIterator
, typename _Compare
>
3742 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3744 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3747 * @brief Determines the end of a sorted sequence.
3748 * @param first An iterator.
3749 * @param last Another iterator.
3750 * @return An iterator pointing to the last iterator i in [first, last)
3751 * for which the range [first, i) is sorted.
3753 template<typename _ForwardIterator
>
3755 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3757 // concept requirements
3758 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3759 __glibcxx_function_requires(_LessThanComparableConcept
<
3760 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3761 __glibcxx_requires_valid_range(__first
, __last
);
3763 if (__first
== __last
)
3766 _ForwardIterator __next
= __first
;
3767 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3768 if (*__next
< *__first
)
3774 * @brief Determines the end of a sorted sequence using comparison functor.
3775 * @param first An iterator.
3776 * @param last Another iterator.
3777 * @param comp A comparison functor.
3778 * @return An iterator pointing to the last iterator i in [first, last)
3779 * for which the range [first, i) is sorted.
3781 template<typename _ForwardIterator
, typename _Compare
>
3783 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3786 // concept requirements
3787 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3788 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3789 typename iterator_traits
<_ForwardIterator
>::value_type
,
3790 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3791 __glibcxx_requires_valid_range(__first
, __last
);
3793 if (__first
== __last
)
3796 _ForwardIterator __next
= __first
;
3797 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3798 if (__comp(*__next
, *__first
))
3804 * @brief Determines min and max at once as an ordered pair.
3805 * @param a A thing of arbitrary type.
3806 * @param b Another thing of arbitrary type.
3807 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3809 template<typename _Tp
>
3810 inline pair
<const _Tp
&, const _Tp
&>
3811 minmax(const _Tp
& __a
, const _Tp
& __b
)
3813 // concept requirements
3814 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3816 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3817 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3821 * @brief Determines min and max at once as an ordered pair.
3822 * @param a A thing of arbitrary type.
3823 * @param b Another thing of arbitrary type.
3824 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3825 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3827 template<typename _Tp
, typename _Compare
>
3828 inline pair
<const _Tp
&, const _Tp
&>
3829 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3831 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3832 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3836 * @brief Return a pair of iterators pointing to the minimum and maximum
3837 * elements in a range.
3838 * @param first Start of range.
3839 * @param last End of range.
3840 * @return make_pair(m, M), where m is the first iterator i in
3841 * [first, last) such that no other element in the range is
3842 * smaller, and where M is the last iterator i in [first, last)
3843 * such that no other element in the range is larger.
3845 template<typename _ForwardIterator
>
3846 pair
<_ForwardIterator
, _ForwardIterator
>
3847 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3849 // concept requirements
3850 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3851 __glibcxx_function_requires(_LessThanComparableConcept
<
3852 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3853 __glibcxx_requires_valid_range(__first
, __last
);
3855 _ForwardIterator __next
= __first
;
3856 if (__first
== __last
3857 || ++__next
== __last
)
3858 return std::make_pair(__first
, __first
);
3860 _ForwardIterator __min
, __max
;
3861 if (*__next
< *__first
)
3875 while (__first
!= __last
)
3878 if (++__next
== __last
)
3880 if (*__first
< *__min
)
3882 else if (!(*__first
< *__max
))
3887 if (*__next
< *__first
)
3889 if (*__next
< *__min
)
3891 if (!(*__first
< *__max
))
3896 if (*__first
< *__min
)
3898 if (!(*__next
< *__max
))
3906 return std::make_pair(__min
, __max
);
3910 * @brief Return a pair of iterators pointing to the minimum and maximum
3911 * elements in a range.
3912 * @param first Start of range.
3913 * @param last End of range.
3914 * @param comp Comparison functor.
3915 * @return make_pair(m, M), where m is the first iterator i in
3916 * [first, last) such that no other element in the range is
3917 * smaller, and where M is the last iterator i in [first, last)
3918 * such that no other element in the range is larger.
3920 template<typename _ForwardIterator
, typename _Compare
>
3921 pair
<_ForwardIterator
, _ForwardIterator
>
3922 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3925 // concept requirements
3926 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3927 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3928 typename iterator_traits
<_ForwardIterator
>::value_type
,
3929 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3930 __glibcxx_requires_valid_range(__first
, __last
);
3932 _ForwardIterator __next
= __first
;
3933 if (__first
== __last
3934 || ++__next
== __last
)
3935 return std::make_pair(__first
, __first
);
3937 _ForwardIterator __min
, __max
;
3938 if (__comp(*__next
, *__first
))
3952 while (__first
!= __last
)
3955 if (++__next
== __last
)
3957 if (__comp(*__first
, *__min
))
3959 else if (!__comp(*__first
, *__max
))
3964 if (__comp(*__next
, *__first
))
3966 if (__comp(*__next
, *__min
))
3968 if (!__comp(*__first
, *__max
))
3973 if (__comp(*__first
, *__min
))
3975 if (!__comp(*__next
, *__max
))
3983 return std::make_pair(__min
, __max
);
3985 #endif // __GXX_EXPERIMENTAL_CXX0X__
3987 _GLIBCXX_END_NAMESPACE
3989 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3992 * @brief Apply a function to every element of a sequence.
3993 * @param first An input iterator.
3994 * @param last An input iterator.
3995 * @param f A unary function object.
3998 * Applies the function object @p f to each element in the range
3999 * @p [first,last). @p f must not modify the order of the sequence.
4000 * If @p f has a return value it is ignored.
4002 template<typename _InputIterator
, typename _Function
>
4004 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4006 // concept requirements
4007 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4008 __glibcxx_requires_valid_range(__first
, __last
);
4009 for (; __first
!= __last
; ++__first
)
4015 * @brief Find the first occurrence of a value in a sequence.
4016 * @param first An input iterator.
4017 * @param last An input iterator.
4018 * @param val The value to find.
4019 * @return The first iterator @c i in the range @p [first,last)
4020 * such that @c *i == @p val, or @p last if no such iterator exists.
4022 template<typename _InputIterator
, typename _Tp
>
4023 inline _InputIterator
4024 find(_InputIterator __first
, _InputIterator __last
,
4027 // concept requirements
4028 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4029 __glibcxx_function_requires(_EqualOpConcept
<
4030 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4031 __glibcxx_requires_valid_range(__first
, __last
);
4032 return std::__find(__first
, __last
, __val
,
4033 std::__iterator_category(__first
));
4037 * @brief Find the first element in a sequence for which a
4038 * predicate is true.
4039 * @param first An input iterator.
4040 * @param last An input iterator.
4041 * @param pred A predicate.
4042 * @return The first iterator @c i in the range @p [first,last)
4043 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4045 template<typename _InputIterator
, typename _Predicate
>
4046 inline _InputIterator
4047 find_if(_InputIterator __first
, _InputIterator __last
,
4050 // concept requirements
4051 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4052 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4053 typename iterator_traits
<_InputIterator
>::value_type
>)
4054 __glibcxx_requires_valid_range(__first
, __last
);
4055 return std::__find_if(__first
, __last
, __pred
,
4056 std::__iterator_category(__first
));
4060 * @brief Find element from a set in a sequence.
4061 * @param first1 Start of range to search.
4062 * @param last1 End of range to search.
4063 * @param first2 Start of match candidates.
4064 * @param last2 End of match candidates.
4065 * @return The first iterator @c i in the range
4066 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4067 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4069 * Searches the range @p [first1,last1) for an element that is equal to
4070 * some element in the range [first2,last2). If found, returns an iterator
4071 * in the range [first1,last1), otherwise returns @p last1.
4073 template<typename _InputIterator
, typename _ForwardIterator
>
4075 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4076 _ForwardIterator __first2
, _ForwardIterator __last2
)
4078 // concept requirements
4079 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4080 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4081 __glibcxx_function_requires(_EqualOpConcept
<
4082 typename iterator_traits
<_InputIterator
>::value_type
,
4083 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4084 __glibcxx_requires_valid_range(__first1
, __last1
);
4085 __glibcxx_requires_valid_range(__first2
, __last2
);
4087 for (; __first1
!= __last1
; ++__first1
)
4088 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4089 if (*__first1
== *__iter
)
4095 * @brief Find element from a set in a sequence using a predicate.
4096 * @param first1 Start of range to search.
4097 * @param last1 End of range to search.
4098 * @param first2 Start of match candidates.
4099 * @param last2 End of match candidates.
4100 * @param comp Predicate to use.
4101 * @return The first iterator @c i in the range
4102 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4103 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4106 * Searches the range @p [first1,last1) for an element that is
4107 * equal to some element in the range [first2,last2). If found,
4108 * returns an iterator in the range [first1,last1), otherwise
4111 template<typename _InputIterator
, typename _ForwardIterator
,
4112 typename _BinaryPredicate
>
4114 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4115 _ForwardIterator __first2
, _ForwardIterator __last2
,
4116 _BinaryPredicate __comp
)
4118 // concept requirements
4119 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4120 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4121 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4122 typename iterator_traits
<_InputIterator
>::value_type
,
4123 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4124 __glibcxx_requires_valid_range(__first1
, __last1
);
4125 __glibcxx_requires_valid_range(__first2
, __last2
);
4127 for (; __first1
!= __last1
; ++__first1
)
4128 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4129 if (__comp(*__first1
, *__iter
))
4135 * @brief Find two adjacent values in a sequence that are equal.
4136 * @param first A forward iterator.
4137 * @param last A forward iterator.
4138 * @return The first iterator @c i such that @c i and @c i+1 are both
4139 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4140 * or @p last if no such iterator exists.
4142 template<typename _ForwardIterator
>
4144 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4146 // concept requirements
4147 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4148 __glibcxx_function_requires(_EqualityComparableConcept
<
4149 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4150 __glibcxx_requires_valid_range(__first
, __last
);
4151 if (__first
== __last
)
4153 _ForwardIterator __next
= __first
;
4154 while(++__next
!= __last
)
4156 if (*__first
== *__next
)
4164 * @brief Find two adjacent values in a sequence using a predicate.
4165 * @param first A forward iterator.
4166 * @param last A forward iterator.
4167 * @param binary_pred A binary predicate.
4168 * @return The first iterator @c i such that @c i and @c i+1 are both
4169 * valid iterators in @p [first,last) and such that
4170 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4173 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4175 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4176 _BinaryPredicate __binary_pred
)
4178 // concept requirements
4179 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4180 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4181 typename iterator_traits
<_ForwardIterator
>::value_type
,
4182 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4183 __glibcxx_requires_valid_range(__first
, __last
);
4184 if (__first
== __last
)
4186 _ForwardIterator __next
= __first
;
4187 while(++__next
!= __last
)
4189 if (__binary_pred(*__first
, *__next
))
4197 * @brief Count the number of copies of a value in a sequence.
4198 * @param first An input iterator.
4199 * @param last An input iterator.
4200 * @param value The value to be counted.
4201 * @return The number of iterators @c i in the range @p [first,last)
4202 * for which @c *i == @p value
4204 template<typename _InputIterator
, typename _Tp
>
4205 typename iterator_traits
<_InputIterator
>::difference_type
4206 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4208 // concept requirements
4209 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4210 __glibcxx_function_requires(_EqualOpConcept
<
4211 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4212 __glibcxx_requires_valid_range(__first
, __last
);
4213 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4214 for (; __first
!= __last
; ++__first
)
4215 if (*__first
== __value
)
4221 * @brief Count the elements of a sequence for which a predicate is true.
4222 * @param first An input iterator.
4223 * @param last An input iterator.
4224 * @param pred A predicate.
4225 * @return The number of iterators @c i in the range @p [first,last)
4226 * for which @p pred(*i) is true.
4228 template<typename _InputIterator
, typename _Predicate
>
4229 typename iterator_traits
<_InputIterator
>::difference_type
4230 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4232 // concept requirements
4233 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4234 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4235 typename iterator_traits
<_InputIterator
>::value_type
>)
4236 __glibcxx_requires_valid_range(__first
, __last
);
4237 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4238 for (; __first
!= __last
; ++__first
)
4239 if (__pred(*__first
))
4245 * @brief Search a sequence for a matching sub-sequence.
4246 * @param first1 A forward iterator.
4247 * @param last1 A forward iterator.
4248 * @param first2 A forward iterator.
4249 * @param last2 A forward iterator.
4250 * @return The first iterator @c i in the range
4251 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4252 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4253 * such iterator exists.
4255 * Searches the range @p [first1,last1) for a sub-sequence that compares
4256 * equal value-by-value with the sequence given by @p [first2,last2) and
4257 * returns an iterator to the first element of the sub-sequence, or
4258 * @p last1 if the sub-sequence is not found.
4260 * Because the sub-sequence must lie completely within the range
4261 * @p [first1,last1) it must start at a position less than
4262 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4264 * This means that the returned iterator @c i will be in the range
4265 * @p [first1,last1-(last2-first2))
4267 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4269 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4270 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4272 // concept requirements
4273 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4274 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4275 __glibcxx_function_requires(_EqualOpConcept
<
4276 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4277 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4278 __glibcxx_requires_valid_range(__first1
, __last1
);
4279 __glibcxx_requires_valid_range(__first2
, __last2
);
4281 // Test for empty ranges
4282 if (__first1
== __last1
|| __first2
== __last2
)
4285 // Test for a pattern of length 1.
4286 _ForwardIterator2
__p1(__first2
);
4287 if (++__p1
== __last2
)
4288 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4291 _ForwardIterator2 __p
;
4292 _ForwardIterator1 __current
= __first1
;
4296 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4297 if (__first1
== __last1
)
4301 __current
= __first1
;
4302 if (++__current
== __last1
)
4305 while (*__current
== *__p
)
4307 if (++__p
== __last2
)
4309 if (++__current
== __last1
)
4318 * @brief Search a sequence for a matching sub-sequence using a predicate.
4319 * @param first1 A forward iterator.
4320 * @param last1 A forward iterator.
4321 * @param first2 A forward iterator.
4322 * @param last2 A forward iterator.
4323 * @param predicate A binary predicate.
4324 * @return The first iterator @c i in the range
4325 * @p [first1,last1-(last2-first2)) such that
4326 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4327 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4329 * Searches the range @p [first1,last1) for a sub-sequence that compares
4330 * equal value-by-value with the sequence given by @p [first2,last2),
4331 * using @p predicate to determine equality, and returns an iterator
4332 * to the first element of the sub-sequence, or @p last1 if no such
4335 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4337 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4338 typename _BinaryPredicate
>
4340 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4341 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4342 _BinaryPredicate __predicate
)
4344 // concept requirements
4345 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4346 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4347 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4348 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4349 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4350 __glibcxx_requires_valid_range(__first1
, __last1
);
4351 __glibcxx_requires_valid_range(__first2
, __last2
);
4353 // Test for empty ranges
4354 if (__first1
== __last1
|| __first2
== __last2
)
4357 // Test for a pattern of length 1.
4358 _ForwardIterator2
__p1(__first2
);
4359 if (++__p1
== __last2
)
4361 while (__first1
!= __last1
4362 && !bool(__predicate(*__first1
, *__first2
)))
4368 _ForwardIterator2 __p
;
4369 _ForwardIterator1 __current
= __first1
;
4373 while (__first1
!= __last1
4374 && !bool(__predicate(*__first1
, *__first2
)))
4376 if (__first1
== __last1
)
4380 __current
= __first1
;
4381 if (++__current
== __last1
)
4384 while (__predicate(*__current
, *__p
))
4386 if (++__p
== __last2
)
4388 if (++__current
== __last1
)
4398 * @brief Search a sequence for a number of consecutive values.
4399 * @param first A forward iterator.
4400 * @param last A forward iterator.
4401 * @param count The number of consecutive values.
4402 * @param val The value to find.
4403 * @return The first iterator @c i in the range @p [first,last-count)
4404 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4405 * or @p last if no such iterator exists.
4407 * Searches the range @p [first,last) for @p count consecutive elements
4410 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4412 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4413 _Integer __count
, const _Tp
& __val
)
4415 // concept requirements
4416 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4417 __glibcxx_function_requires(_EqualOpConcept
<
4418 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4419 __glibcxx_requires_valid_range(__first
, __last
);
4424 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4425 return std::__search_n(__first
, __last
, __count
, __val
,
4426 std::__iterator_category(__first
));
4431 * @brief Search a sequence for a number of consecutive values using a
4433 * @param first A forward iterator.
4434 * @param last A forward iterator.
4435 * @param count The number of consecutive values.
4436 * @param val The value to find.
4437 * @param binary_pred A binary predicate.
4438 * @return The first iterator @c i in the range @p [first,last-count)
4439 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4440 * range @p [0,count), or @p last if no such iterator exists.
4442 * Searches the range @p [first,last) for @p count consecutive elements
4443 * for which the predicate returns true.
4445 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4446 typename _BinaryPredicate
>
4448 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4449 _Integer __count
, const _Tp
& __val
,
4450 _BinaryPredicate __binary_pred
)
4452 // concept requirements
4453 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4454 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4455 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4456 __glibcxx_requires_valid_range(__first
, __last
);
4462 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4466 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4467 std::__iterator_category(__first
));
4472 * @brief Perform an operation on a sequence.
4473 * @param first An input iterator.
4474 * @param last An input iterator.
4475 * @param result An output iterator.
4476 * @param unary_op A unary operator.
4477 * @return An output iterator equal to @p result+(last-first).
4479 * Applies the operator to each element in the input range and assigns
4480 * the results to successive elements of the output sequence.
4481 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4482 * range @p [0,last-first).
4484 * @p unary_op must not alter its argument.
4486 template<typename _InputIterator
, typename _OutputIterator
,
4487 typename _UnaryOperation
>
4489 transform(_InputIterator __first
, _InputIterator __last
,
4490 _OutputIterator __result
, _UnaryOperation __unary_op
)
4492 // concept requirements
4493 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4494 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4495 // "the type returned by a _UnaryOperation"
4496 __typeof__(__unary_op(*__first
))>)
4497 __glibcxx_requires_valid_range(__first
, __last
);
4499 for (; __first
!= __last
; ++__first
, ++__result
)
4500 *__result
= __unary_op(*__first
);
4505 * @brief Perform an operation on corresponding elements of two sequences.
4506 * @param first1 An input iterator.
4507 * @param last1 An input iterator.
4508 * @param first2 An input iterator.
4509 * @param result An output iterator.
4510 * @param binary_op A binary operator.
4511 * @return An output iterator equal to @p result+(last-first).
4513 * Applies the operator to the corresponding elements in the two
4514 * input ranges and assigns the results to successive elements of the
4516 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4517 * @c N in the range @p [0,last1-first1).
4519 * @p binary_op must not alter either of its arguments.
4521 template<typename _InputIterator1
, typename _InputIterator2
,
4522 typename _OutputIterator
, typename _BinaryOperation
>
4524 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4525 _InputIterator2 __first2
, _OutputIterator __result
,
4526 _BinaryOperation __binary_op
)
4528 // concept requirements
4529 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4530 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4531 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4532 // "the type returned by a _BinaryOperation"
4533 __typeof__(__binary_op(*__first1
,*__first2
))>)
4534 __glibcxx_requires_valid_range(__first1
, __last1
);
4536 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4537 *__result
= __binary_op(*__first1
, *__first2
);
4542 * @brief Replace each occurrence of one value in a sequence with another
4544 * @param first A forward iterator.
4545 * @param last A forward iterator.
4546 * @param old_value The value to be replaced.
4547 * @param new_value The replacement value.
4548 * @return replace() returns no value.
4550 * For each iterator @c i in the range @p [first,last) if @c *i ==
4551 * @p old_value then the assignment @c *i = @p new_value is performed.
4553 template<typename _ForwardIterator
, typename _Tp
>
4555 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4556 const _Tp
& __old_value
, const _Tp
& __new_value
)
4558 // concept requirements
4559 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4561 __glibcxx_function_requires(_EqualOpConcept
<
4562 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4563 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4564 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4565 __glibcxx_requires_valid_range(__first
, __last
);
4567 for (; __first
!= __last
; ++__first
)
4568 if (*__first
== __old_value
)
4569 *__first
= __new_value
;
4573 * @brief Replace each value in a sequence for which a predicate returns
4574 * true with another value.
4575 * @param first A forward iterator.
4576 * @param last A forward iterator.
4577 * @param pred A predicate.
4578 * @param new_value The replacement value.
4579 * @return replace_if() returns no value.
4581 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4582 * is true then the assignment @c *i = @p new_value is performed.
4584 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4586 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4587 _Predicate __pred
, const _Tp
& __new_value
)
4589 // concept requirements
4590 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4592 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4593 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4594 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4595 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4596 __glibcxx_requires_valid_range(__first
, __last
);
4598 for (; __first
!= __last
; ++__first
)
4599 if (__pred(*__first
))
4600 *__first
= __new_value
;
4604 * @brief Assign the result of a function object to each value in a
4606 * @param first A forward iterator.
4607 * @param last A forward iterator.
4608 * @param gen A function object taking no arguments and returning
4609 * std::iterator_traits<_ForwardIterator>::value_type
4610 * @return generate() returns no value.
4612 * Performs the assignment @c *i = @p gen() for each @c i in the range
4615 template<typename _ForwardIterator
, typename _Generator
>
4617 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4620 // concept requirements
4621 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4622 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4623 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4624 __glibcxx_requires_valid_range(__first
, __last
);
4626 for (; __first
!= __last
; ++__first
)
4631 * @brief Assign the result of a function object to each value in a
4633 * @param first A forward iterator.
4634 * @param n The length of the sequence.
4635 * @param gen A function object taking no arguments and returning
4636 * std::iterator_traits<_ForwardIterator>::value_type
4637 * @return The end of the sequence, @p first+n
4639 * Performs the assignment @c *i = @p gen() for each @c i in the range
4640 * @p [first,first+n).
4642 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4644 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4646 // concept requirements
4647 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4648 // "the type returned by a _Generator"
4649 __typeof__(__gen())>)
4651 for (; __n
> 0; --__n
, ++__first
)
4658 * @brief Copy a sequence, removing consecutive duplicate values.
4659 * @param first An input iterator.
4660 * @param last An input iterator.
4661 * @param result An output iterator.
4662 * @return An iterator designating the end of the resulting sequence.
4664 * Copies each element in the range @p [first,last) to the range
4665 * beginning at @p result, except that only the first element is copied
4666 * from groups of consecutive elements that compare equal.
4667 * unique_copy() is stable, so the relative order of elements that are
4668 * copied is unchanged.
4670 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4671 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4673 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4674 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4677 template<typename _InputIterator
, typename _OutputIterator
>
4678 inline _OutputIterator
4679 unique_copy(_InputIterator __first
, _InputIterator __last
,
4680 _OutputIterator __result
)
4682 // concept requirements
4683 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4684 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4685 typename iterator_traits
<_InputIterator
>::value_type
>)
4686 __glibcxx_function_requires(_EqualityComparableConcept
<
4687 typename iterator_traits
<_InputIterator
>::value_type
>)
4688 __glibcxx_requires_valid_range(__first
, __last
);
4690 if (__first
== __last
)
4692 return std::__unique_copy(__first
, __last
, __result
,
4693 std::__iterator_category(__first
),
4694 std::__iterator_category(__result
));
4698 * @brief Copy a sequence, removing consecutive values using a predicate.
4699 * @param first An input iterator.
4700 * @param last An input iterator.
4701 * @param result An output iterator.
4702 * @param binary_pred A binary predicate.
4703 * @return An iterator designating the end of the resulting sequence.
4705 * Copies each element in the range @p [first,last) to the range
4706 * beginning at @p result, except that only the first element is copied
4707 * from groups of consecutive elements for which @p binary_pred returns
4709 * unique_copy() is stable, so the relative order of elements that are
4710 * copied is unchanged.
4712 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4713 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4715 template<typename _InputIterator
, typename _OutputIterator
,
4716 typename _BinaryPredicate
>
4717 inline _OutputIterator
4718 unique_copy(_InputIterator __first
, _InputIterator __last
,
4719 _OutputIterator __result
,
4720 _BinaryPredicate __binary_pred
)
4722 // concept requirements -- predicates checked later
4723 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4724 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4725 typename iterator_traits
<_InputIterator
>::value_type
>)
4726 __glibcxx_requires_valid_range(__first
, __last
);
4728 if (__first
== __last
)
4730 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4731 std::__iterator_category(__first
),
4732 std::__iterator_category(__result
));
4737 * @brief Randomly shuffle the elements of a sequence.
4738 * @param first A forward iterator.
4739 * @param last A forward iterator.
4742 * Reorder the elements in the range @p [first,last) using a random
4743 * distribution, so that every possible ordering of the sequence is
4746 template<typename _RandomAccessIterator
>
4748 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4750 // concept requirements
4751 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4752 _RandomAccessIterator
>)
4753 __glibcxx_requires_valid_range(__first
, __last
);
4755 if (__first
!= __last
)
4756 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4757 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4761 * @brief Shuffle the elements of a sequence using a random number
4763 * @param first A forward iterator.
4764 * @param last A forward iterator.
4765 * @param rand The RNG functor or function.
4768 * Reorders the elements in the range @p [first,last) using @p rand to
4769 * provide a random distribution. Calling @p rand(N) for a positive
4770 * integer @p N should return a randomly chosen integer from the
4773 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4775 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4776 _RandomNumberGenerator
& __rand
)
4778 // concept requirements
4779 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4780 _RandomAccessIterator
>)
4781 __glibcxx_requires_valid_range(__first
, __last
);
4783 if (__first
== __last
)
4785 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4786 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4791 * @brief Move elements for which a predicate is true to the beginning
4793 * @param first A forward iterator.
4794 * @param last A forward iterator.
4795 * @param pred A predicate functor.
4796 * @return An iterator @p middle such that @p pred(i) is true for each
4797 * iterator @p i in the range @p [first,middle) and false for each @p i
4798 * in the range @p [middle,last).
4800 * @p pred must not modify its operand. @p partition() does not preserve
4801 * the relative ordering of elements in each group, use
4802 * @p stable_partition() if this is needed.
4804 template<typename _ForwardIterator
, typename _Predicate
>
4805 inline _ForwardIterator
4806 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4809 // concept requirements
4810 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4812 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4813 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4814 __glibcxx_requires_valid_range(__first
, __last
);
4816 return std::__partition(__first
, __last
, __pred
,
4817 std::__iterator_category(__first
));
4823 * @brief Sort the smallest elements of a sequence.
4824 * @param first An iterator.
4825 * @param middle Another iterator.
4826 * @param last Another iterator.
4829 * Sorts the smallest @p (middle-first) elements in the range
4830 * @p [first,last) and moves them to the range @p [first,middle). The
4831 * order of the remaining elements in the range @p [middle,last) is
4833 * After the sort if @p i and @j are iterators in the range
4834 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4835 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4837 template<typename _RandomAccessIterator
>
4839 partial_sort(_RandomAccessIterator __first
,
4840 _RandomAccessIterator __middle
,
4841 _RandomAccessIterator __last
)
4843 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4846 // concept requirements
4847 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4848 _RandomAccessIterator
>)
4849 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4850 __glibcxx_requires_valid_range(__first
, __middle
);
4851 __glibcxx_requires_valid_range(__middle
, __last
);
4853 std::__heap_select(__first
, __middle
, __last
);
4854 std::sort_heap(__first
, __middle
);
4858 * @brief Sort the smallest elements of a sequence using a predicate
4860 * @param first An iterator.
4861 * @param middle Another iterator.
4862 * @param last Another iterator.
4863 * @param comp A comparison functor.
4866 * Sorts the smallest @p (middle-first) elements in the range
4867 * @p [first,last) and moves them to the range @p [first,middle). The
4868 * order of the remaining elements in the range @p [middle,last) is
4870 * After the sort if @p i and @j are iterators in the range
4871 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4872 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4875 template<typename _RandomAccessIterator
, typename _Compare
>
4877 partial_sort(_RandomAccessIterator __first
,
4878 _RandomAccessIterator __middle
,
4879 _RandomAccessIterator __last
,
4882 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4885 // concept requirements
4886 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4887 _RandomAccessIterator
>)
4888 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4889 _ValueType
, _ValueType
>)
4890 __glibcxx_requires_valid_range(__first
, __middle
);
4891 __glibcxx_requires_valid_range(__middle
, __last
);
4893 std::__heap_select(__first
, __middle
, __last
, __comp
);
4894 std::sort_heap(__first
, __middle
, __comp
);
4898 * @brief Sort a sequence just enough to find a particular position.
4899 * @param first An iterator.
4900 * @param nth Another iterator.
4901 * @param last Another iterator.
4904 * Rearranges the elements in the range @p [first,last) so that @p *nth
4905 * is the same element that would have been in that position had the
4906 * whole sequence been sorted.
4907 * whole sequence been sorted. The elements either side of @p *nth are
4908 * not completely sorted, but for any iterator @i in the range
4909 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4910 * holds that @p *j<*i is false.
4912 template<typename _RandomAccessIterator
>
4914 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4915 _RandomAccessIterator __last
)
4917 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4920 // concept requirements
4921 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4922 _RandomAccessIterator
>)
4923 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4924 __glibcxx_requires_valid_range(__first
, __nth
);
4925 __glibcxx_requires_valid_range(__nth
, __last
);
4927 if (__first
== __last
|| __nth
== __last
)
4930 std::__introselect(__first
, __nth
, __last
,
4931 std::__lg(__last
- __first
) * 2);
4935 * @brief Sort a sequence just enough to find a particular position
4936 * using a predicate for comparison.
4937 * @param first An iterator.
4938 * @param nth Another iterator.
4939 * @param last Another iterator.
4940 * @param comp A comparison functor.
4943 * Rearranges the elements in the range @p [first,last) so that @p *nth
4944 * is the same element that would have been in that position had the
4945 * whole sequence been sorted. The elements either side of @p *nth are
4946 * not completely sorted, but for any iterator @i in the range
4947 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4948 * holds that @p comp(*j,*i) is false.
4950 template<typename _RandomAccessIterator
, typename _Compare
>
4952 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4953 _RandomAccessIterator __last
, _Compare __comp
)
4955 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4958 // concept requirements
4959 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4960 _RandomAccessIterator
>)
4961 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4962 _ValueType
, _ValueType
>)
4963 __glibcxx_requires_valid_range(__first
, __nth
);
4964 __glibcxx_requires_valid_range(__nth
, __last
);
4966 if (__first
== __last
|| __nth
== __last
)
4969 std::__introselect(__first
, __nth
, __last
,
4970 std::__lg(__last
- __first
) * 2, __comp
);
4975 * @brief Sort the elements of a sequence.
4976 * @param first An iterator.
4977 * @param last Another iterator.
4980 * Sorts the elements in the range @p [first,last) in ascending order,
4981 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4982 * @p [first,last-1).
4984 * The relative ordering of equivalent elements is not preserved, use
4985 * @p stable_sort() if this is needed.
4987 template<typename _RandomAccessIterator
>
4989 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4991 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4994 // concept requirements
4995 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4996 _RandomAccessIterator
>)
4997 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4998 __glibcxx_requires_valid_range(__first
, __last
);
5000 if (__first
!= __last
)
5002 std::__introsort_loop(__first
, __last
,
5003 std::__lg(__last
- __first
) * 2);
5004 std::__final_insertion_sort(__first
, __last
);
5009 * @brief Sort the elements of a sequence using a predicate for comparison.
5010 * @param first An iterator.
5011 * @param last Another iterator.
5012 * @param comp A comparison functor.
5015 * Sorts the elements in the range @p [first,last) in ascending order,
5016 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5017 * range @p [first,last-1).
5019 * The relative ordering of equivalent elements is not preserved, use
5020 * @p stable_sort() if this is needed.
5022 template<typename _RandomAccessIterator
, typename _Compare
>
5024 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5027 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5030 // concept requirements
5031 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5032 _RandomAccessIterator
>)
5033 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5035 __glibcxx_requires_valid_range(__first
, __last
);
5037 if (__first
!= __last
)
5039 std::__introsort_loop(__first
, __last
,
5040 std::__lg(__last
- __first
) * 2, __comp
);
5041 std::__final_insertion_sort(__first
, __last
, __comp
);
5046 * @brief Merges two sorted ranges.
5047 * @param first1 An iterator.
5048 * @param first2 Another iterator.
5049 * @param last1 Another iterator.
5050 * @param last2 Another iterator.
5051 * @param result An iterator pointing to the end of the merged range.
5052 * @return An iterator pointing to the first element "not less
5055 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5056 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5057 * must be sorted, and the output range must not overlap with either of
5058 * the input ranges. The sort is @e stable, that is, for equivalent
5059 * elements in the two ranges, elements from the first range will always
5060 * come before elements from the second.
5062 template<typename _InputIterator1
, typename _InputIterator2
,
5063 typename _OutputIterator
>
5065 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5066 _InputIterator2 __first2
, _InputIterator2 __last2
,
5067 _OutputIterator __result
)
5069 typedef typename iterator_traits
<_InputIterator1
>::value_type
5071 typedef typename iterator_traits
<_InputIterator2
>::value_type
5074 // concept requirements
5075 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5076 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5077 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5079 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5081 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5082 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5083 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5085 while (__first1
!= __last1
&& __first2
!= __last2
)
5087 if (*__first2
< *__first1
)
5089 *__result
= *__first2
;
5094 *__result
= *__first1
;
5099 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5104 * @brief Merges two sorted ranges.
5105 * @param first1 An iterator.
5106 * @param first2 Another iterator.
5107 * @param last1 Another iterator.
5108 * @param last2 Another iterator.
5109 * @param result An iterator pointing to the end of the merged range.
5110 * @param comp A functor to use for comparisons.
5111 * @return An iterator pointing to the first element "not less
5114 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5115 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5116 * must be sorted, and the output range must not overlap with either of
5117 * the input ranges. The sort is @e stable, that is, for equivalent
5118 * elements in the two ranges, elements from the first range will always
5119 * come before elements from the second.
5121 * The comparison function should have the same effects on ordering as
5122 * the function used for the initial sort.
5124 template<typename _InputIterator1
, typename _InputIterator2
,
5125 typename _OutputIterator
, typename _Compare
>
5127 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5128 _InputIterator2 __first2
, _InputIterator2 __last2
,
5129 _OutputIterator __result
, _Compare __comp
)
5131 typedef typename iterator_traits
<_InputIterator1
>::value_type
5133 typedef typename iterator_traits
<_InputIterator2
>::value_type
5136 // concept requirements
5137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5138 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5139 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5141 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5143 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5144 _ValueType2
, _ValueType1
>)
5145 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5146 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5148 while (__first1
!= __last1
&& __first2
!= __last2
)
5150 if (__comp(*__first2
, *__first1
))
5152 *__result
= *__first2
;
5157 *__result
= *__first1
;
5162 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5168 * @brief Sort the elements of a sequence, preserving the relative order
5169 * of equivalent elements.
5170 * @param first An iterator.
5171 * @param last Another iterator.
5174 * Sorts the elements in the range @p [first,last) in ascending order,
5175 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5176 * @p [first,last-1).
5178 * The relative ordering of equivalent elements is preserved, so any two
5179 * elements @p x and @p y in the range @p [first,last) such that
5180 * @p x<y is false and @p y<x is false will have the same relative
5181 * ordering after calling @p stable_sort().
5183 template<typename _RandomAccessIterator
>
5185 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5187 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5189 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5192 // concept requirements
5193 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5194 _RandomAccessIterator
>)
5195 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5196 __glibcxx_requires_valid_range(__first
, __last
);
5198 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5200 if (__buf
.begin() == 0)
5201 std::__inplace_stable_sort(__first
, __last
);
5203 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5204 _DistanceType(__buf
.size()));
5208 * @brief Sort the elements of a sequence using a predicate for comparison,
5209 * preserving the relative order of equivalent elements.
5210 * @param first An iterator.
5211 * @param last Another iterator.
5212 * @param comp A comparison functor.
5215 * Sorts the elements in the range @p [first,last) in ascending order,
5216 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5217 * range @p [first,last-1).
5219 * The relative ordering of equivalent elements is preserved, so any two
5220 * elements @p x and @p y in the range @p [first,last) such that
5221 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5222 * relative ordering after calling @p stable_sort().
5224 template<typename _RandomAccessIterator
, typename _Compare
>
5226 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5229 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5231 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5234 // concept requirements
5235 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5236 _RandomAccessIterator
>)
5237 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5240 __glibcxx_requires_valid_range(__first
, __last
);
5242 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5244 if (__buf
.begin() == 0)
5245 std::__inplace_stable_sort(__first
, __last
, __comp
);
5247 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5248 _DistanceType(__buf
.size()), __comp
);
5253 * @brief Return the union of two sorted ranges.
5254 * @param first1 Start of first range.
5255 * @param last1 End of first range.
5256 * @param first2 Start of second range.
5257 * @param last2 End of second range.
5258 * @return End of the output range.
5259 * @ingroup setoperations
5261 * This operation iterates over both ranges, copying elements present in
5262 * each range in order to the output range. Iterators increment for each
5263 * range. When the current element of one range is less than the other,
5264 * that element is copied and the iterator advanced. If an element is
5265 * contained in both ranges, the element from the first range is copied and
5266 * both ranges advance. The output range may not overlap either input
5269 template<typename _InputIterator1
, typename _InputIterator2
,
5270 typename _OutputIterator
>
5272 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5273 _InputIterator2 __first2
, _InputIterator2 __last2
,
5274 _OutputIterator __result
)
5276 typedef typename iterator_traits
<_InputIterator1
>::value_type
5278 typedef typename iterator_traits
<_InputIterator2
>::value_type
5281 // concept requirements
5282 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5283 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5284 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5286 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5288 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5289 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5290 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5291 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5293 while (__first1
!= __last1
&& __first2
!= __last2
)
5295 if (*__first1
< *__first2
)
5297 *__result
= *__first1
;
5300 else if (*__first2
< *__first1
)
5302 *__result
= *__first2
;
5307 *__result
= *__first1
;
5313 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5318 * @brief Return the union of two sorted ranges using a comparison functor.
5319 * @param first1 Start of first range.
5320 * @param last1 End of first range.
5321 * @param first2 Start of second range.
5322 * @param last2 End of second range.
5323 * @param comp The comparison functor.
5324 * @return End of the output range.
5325 * @ingroup setoperations
5327 * This operation iterates over both ranges, copying elements present in
5328 * each range in order to the output range. Iterators increment for each
5329 * range. When the current element of one range is less than the other
5330 * according to @a comp, that element is copied and the iterator advanced.
5331 * If an equivalent element according to @a comp is contained in both
5332 * ranges, the element from the first range is copied and both ranges
5333 * advance. The output range may not overlap either input range.
5335 template<typename _InputIterator1
, typename _InputIterator2
,
5336 typename _OutputIterator
, typename _Compare
>
5338 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5339 _InputIterator2 __first2
, _InputIterator2 __last2
,
5340 _OutputIterator __result
, _Compare __comp
)
5342 typedef typename iterator_traits
<_InputIterator1
>::value_type
5344 typedef typename iterator_traits
<_InputIterator2
>::value_type
5347 // concept requirements
5348 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5349 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5350 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5352 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5354 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5355 _ValueType1
, _ValueType2
>)
5356 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5357 _ValueType2
, _ValueType1
>)
5358 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5359 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5361 while (__first1
!= __last1
&& __first2
!= __last2
)
5363 if (__comp(*__first1
, *__first2
))
5365 *__result
= *__first1
;
5368 else if (__comp(*__first2
, *__first1
))
5370 *__result
= *__first2
;
5375 *__result
= *__first1
;
5381 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5386 * @brief Return the intersection of two sorted ranges.
5387 * @param first1 Start of first range.
5388 * @param last1 End of first range.
5389 * @param first2 Start of second range.
5390 * @param last2 End of second range.
5391 * @return End of the output range.
5392 * @ingroup setoperations
5394 * This operation iterates over both ranges, copying elements present in
5395 * both ranges in order to the output range. Iterators increment for each
5396 * range. When the current element of one range is less than the other,
5397 * that iterator advances. If an element is contained in both ranges, the
5398 * element from the first range is copied and both ranges advance. The
5399 * output range may not overlap either input range.
5401 template<typename _InputIterator1
, typename _InputIterator2
,
5402 typename _OutputIterator
>
5404 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5405 _InputIterator2 __first2
, _InputIterator2 __last2
,
5406 _OutputIterator __result
)
5408 typedef typename iterator_traits
<_InputIterator1
>::value_type
5410 typedef typename iterator_traits
<_InputIterator2
>::value_type
5413 // concept requirements
5414 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5415 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5416 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5418 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5419 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5420 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5421 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5423 while (__first1
!= __last1
&& __first2
!= __last2
)
5424 if (*__first1
< *__first2
)
5426 else if (*__first2
< *__first1
)
5430 *__result
= *__first1
;
5439 * @brief Return the intersection of two sorted ranges using comparison
5441 * @param first1 Start of first range.
5442 * @param last1 End of first range.
5443 * @param first2 Start of second range.
5444 * @param last2 End of second range.
5445 * @param comp The comparison functor.
5446 * @return End of the output range.
5447 * @ingroup setoperations
5449 * This operation iterates over both ranges, copying elements present in
5450 * both ranges in order to the output range. Iterators increment for each
5451 * range. When the current element of one range is less than the other
5452 * according to @a comp, that iterator advances. If an element is
5453 * contained in both ranges according to @a comp, the element from the
5454 * first range is copied and both ranges advance. The output range may not
5455 * overlap either input range.
5457 template<typename _InputIterator1
, typename _InputIterator2
,
5458 typename _OutputIterator
, typename _Compare
>
5460 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5461 _InputIterator2 __first2
, _InputIterator2 __last2
,
5462 _OutputIterator __result
, _Compare __comp
)
5464 typedef typename iterator_traits
<_InputIterator1
>::value_type
5466 typedef typename iterator_traits
<_InputIterator2
>::value_type
5469 // concept requirements
5470 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5471 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5472 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5474 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5475 _ValueType1
, _ValueType2
>)
5476 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5477 _ValueType2
, _ValueType1
>)
5478 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5479 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5481 while (__first1
!= __last1
&& __first2
!= __last2
)
5482 if (__comp(*__first1
, *__first2
))
5484 else if (__comp(*__first2
, *__first1
))
5488 *__result
= *__first1
;
5497 * @brief Return the difference of two sorted ranges.
5498 * @param first1 Start of first range.
5499 * @param last1 End of first range.
5500 * @param first2 Start of second range.
5501 * @param last2 End of second range.
5502 * @return End of the output range.
5503 * @ingroup setoperations
5505 * This operation iterates over both ranges, copying elements present in
5506 * the first range but not the second in order to the output range.
5507 * Iterators increment for each range. When the current element of the
5508 * first range is less than the second, that element is copied and the
5509 * iterator advances. If the current element of the second range is less,
5510 * the iterator advances, but no element is copied. If an element is
5511 * contained in both ranges, no elements are copied and both ranges
5512 * advance. The output range may not overlap either input range.
5514 template<typename _InputIterator1
, typename _InputIterator2
,
5515 typename _OutputIterator
>
5517 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5518 _InputIterator2 __first2
, _InputIterator2 __last2
,
5519 _OutputIterator __result
)
5521 typedef typename iterator_traits
<_InputIterator1
>::value_type
5523 typedef typename iterator_traits
<_InputIterator2
>::value_type
5526 // concept requirements
5527 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5528 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5529 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5531 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5532 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5533 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5534 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5536 while (__first1
!= __last1
&& __first2
!= __last2
)
5537 if (*__first1
< *__first2
)
5539 *__result
= *__first1
;
5543 else if (*__first2
< *__first1
)
5550 return std::copy(__first1
, __last1
, __result
);
5554 * @brief Return the difference of two sorted ranges using comparison
5556 * @param first1 Start of first range.
5557 * @param last1 End of first range.
5558 * @param first2 Start of second range.
5559 * @param last2 End of second range.
5560 * @param comp The comparison functor.
5561 * @return End of the output range.
5562 * @ingroup setoperations
5564 * This operation iterates over both ranges, copying elements present in
5565 * the first range but not the second in order to the output range.
5566 * Iterators increment for each range. When the current element of the
5567 * first range is less than the second according to @a comp, that element
5568 * is copied and the iterator advances. If the current element of the
5569 * second range is less, no element is copied and the iterator advances.
5570 * If an element is contained in both ranges according to @a comp, no
5571 * elements are copied and both ranges advance. The output range may not
5572 * overlap either input range.
5574 template<typename _InputIterator1
, typename _InputIterator2
,
5575 typename _OutputIterator
, typename _Compare
>
5577 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5578 _InputIterator2 __first2
, _InputIterator2 __last2
,
5579 _OutputIterator __result
, _Compare __comp
)
5581 typedef typename iterator_traits
<_InputIterator1
>::value_type
5583 typedef typename iterator_traits
<_InputIterator2
>::value_type
5586 // concept requirements
5587 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5588 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5589 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5591 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5592 _ValueType1
, _ValueType2
>)
5593 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5594 _ValueType2
, _ValueType1
>)
5595 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5596 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5598 while (__first1
!= __last1
&& __first2
!= __last2
)
5599 if (__comp(*__first1
, *__first2
))
5601 *__result
= *__first1
;
5605 else if (__comp(*__first2
, *__first1
))
5612 return std::copy(__first1
, __last1
, __result
);
5616 * @brief Return the symmetric difference of two sorted ranges.
5617 * @param first1 Start of first range.
5618 * @param last1 End of first range.
5619 * @param first2 Start of second range.
5620 * @param last2 End of second range.
5621 * @return End of the output range.
5622 * @ingroup setoperations
5624 * This operation iterates over both ranges, copying elements present in
5625 * one range but not the other in order to the output range. Iterators
5626 * increment for each range. When the current element of one range is less
5627 * than the other, that element is copied and the iterator advances. If an
5628 * element is contained in both ranges, no elements are copied and both
5629 * ranges advance. The output range may not overlap either input range.
5631 template<typename _InputIterator1
, typename _InputIterator2
,
5632 typename _OutputIterator
>
5634 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5635 _InputIterator2 __first2
, _InputIterator2 __last2
,
5636 _OutputIterator __result
)
5638 typedef typename iterator_traits
<_InputIterator1
>::value_type
5640 typedef typename iterator_traits
<_InputIterator2
>::value_type
5643 // concept requirements
5644 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5645 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5646 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5648 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5650 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5651 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5652 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5653 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5655 while (__first1
!= __last1
&& __first2
!= __last2
)
5656 if (*__first1
< *__first2
)
5658 *__result
= *__first1
;
5662 else if (*__first2
< *__first1
)
5664 *__result
= *__first2
;
5673 return std::copy(__first2
, __last2
, std::copy(__first1
,
5674 __last1
, __result
));
5678 * @brief Return the symmetric difference of two sorted ranges using
5679 * comparison functor.
5680 * @param first1 Start of first range.
5681 * @param last1 End of first range.
5682 * @param first2 Start of second range.
5683 * @param last2 End of second range.
5684 * @param comp The comparison functor.
5685 * @return End of the output range.
5686 * @ingroup setoperations
5688 * This operation iterates over both ranges, copying elements present in
5689 * one range but not the other in order to the output range. Iterators
5690 * increment for each range. When the current element of one range is less
5691 * than the other according to @a comp, that element is copied and the
5692 * iterator advances. If an element is contained in both ranges according
5693 * to @a comp, no elements are copied and both ranges advance. The output
5694 * range may not overlap either input range.
5696 template<typename _InputIterator1
, typename _InputIterator2
,
5697 typename _OutputIterator
, typename _Compare
>
5699 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5700 _InputIterator2 __first2
, _InputIterator2 __last2
,
5701 _OutputIterator __result
,
5704 typedef typename iterator_traits
<_InputIterator1
>::value_type
5706 typedef typename iterator_traits
<_InputIterator2
>::value_type
5709 // concept requirements
5710 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5711 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5712 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5714 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5716 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5717 _ValueType1
, _ValueType2
>)
5718 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5719 _ValueType2
, _ValueType1
>)
5720 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5721 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5723 while (__first1
!= __last1
&& __first2
!= __last2
)
5724 if (__comp(*__first1
, *__first2
))
5726 *__result
= *__first1
;
5730 else if (__comp(*__first2
, *__first1
))
5732 *__result
= *__first2
;
5741 return std::copy(__first2
, __last2
,
5742 std::copy(__first1
, __last1
, __result
));
5747 * @brief Return the minimum element in a range.
5748 * @param first Start of range.
5749 * @param last End of range.
5750 * @return Iterator referencing the first instance of the smallest value.
5752 template<typename _ForwardIterator
>
5754 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5756 // concept requirements
5757 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5758 __glibcxx_function_requires(_LessThanComparableConcept
<
5759 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5760 __glibcxx_requires_valid_range(__first
, __last
);
5762 if (__first
== __last
)
5764 _ForwardIterator __result
= __first
;
5765 while (++__first
!= __last
)
5766 if (*__first
< *__result
)
5772 * @brief Return the minimum element in a range using comparison functor.
5773 * @param first Start of range.
5774 * @param last End of range.
5775 * @param comp Comparison functor.
5776 * @return Iterator referencing the first instance of the smallest value
5777 * according to comp.
5779 template<typename _ForwardIterator
, typename _Compare
>
5781 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5784 // concept requirements
5785 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5786 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5787 typename iterator_traits
<_ForwardIterator
>::value_type
,
5788 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5789 __glibcxx_requires_valid_range(__first
, __last
);
5791 if (__first
== __last
)
5793 _ForwardIterator __result
= __first
;
5794 while (++__first
!= __last
)
5795 if (__comp(*__first
, *__result
))
5801 * @brief Return the maximum element in a range.
5802 * @param first Start of range.
5803 * @param last End of range.
5804 * @return Iterator referencing the first instance of the largest value.
5806 template<typename _ForwardIterator
>
5808 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5810 // concept requirements
5811 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5812 __glibcxx_function_requires(_LessThanComparableConcept
<
5813 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5814 __glibcxx_requires_valid_range(__first
, __last
);
5816 if (__first
== __last
)
5818 _ForwardIterator __result
= __first
;
5819 while (++__first
!= __last
)
5820 if (*__result
< *__first
)
5826 * @brief Return the maximum element in a range using comparison functor.
5827 * @param first Start of range.
5828 * @param last End of range.
5829 * @param comp Comparison functor.
5830 * @return Iterator referencing the first instance of the largest value
5831 * according to comp.
5833 template<typename _ForwardIterator
, typename _Compare
>
5835 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5838 // concept requirements
5839 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5840 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5841 typename iterator_traits
<_ForwardIterator
>::value_type
,
5842 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5843 __glibcxx_requires_valid_range(__first
, __last
);
5845 if (__first
== __last
) return __first
;
5846 _ForwardIterator __result
= __first
;
5847 while (++__first
!= __last
)
5848 if (__comp(*__result
, *__first
))
5853 _GLIBCXX_END_NESTED_NAMESPACE
5855 #endif /* _STL_ALGO_H */