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
;
917 * @brief Remove elements from a sequence.
918 * @param first An input iterator.
919 * @param last An input iterator.
920 * @param value The value to be removed.
921 * @return An iterator designating the end of the resulting sequence.
923 * All elements equal to @p value are removed from the range
926 * remove() is stable, so the relative order of elements that are
927 * not removed is unchanged.
929 * Elements between the end of the resulting sequence and @p last
930 * are still present, but their value is unspecified.
932 template<typename _ForwardIterator
, typename _Tp
>
934 remove(_ForwardIterator __first
, _ForwardIterator __last
,
937 // concept requirements
938 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
940 __glibcxx_function_requires(_EqualOpConcept
<
941 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
942 __glibcxx_requires_valid_range(__first
, __last
);
944 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
945 if(__first
== __last
)
947 _ForwardIterator __result
= __first
;
949 for(; __first
!= __last
; ++__first
)
950 if(!(*__first
== __value
))
952 *__result
= _GLIBCXX_MOVE(*__first
);
959 * @brief Remove elements from a sequence using a predicate.
960 * @param first A forward iterator.
961 * @param last A forward iterator.
962 * @param pred A predicate.
963 * @return An iterator designating the end of the resulting sequence.
965 * All elements for which @p pred returns true are removed from the range
968 * remove_if() is stable, so the relative order of elements that are
969 * not removed is unchanged.
971 * Elements between the end of the resulting sequence and @p last
972 * are still present, but their value is unspecified.
974 template<typename _ForwardIterator
, typename _Predicate
>
976 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
979 // concept requirements
980 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
982 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
983 typename iterator_traits
<_ForwardIterator
>::value_type
>)
984 __glibcxx_requires_valid_range(__first
, __last
);
986 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
987 if(__first
== __last
)
989 _ForwardIterator __result
= __first
;
991 for(; __first
!= __last
; ++__first
)
992 if(!bool(__pred(*__first
)))
994 *__result
= _GLIBCXX_MOVE(*__first
);
1001 * @brief Remove consecutive duplicate values from a sequence.
1002 * @param first A forward iterator.
1003 * @param last A forward iterator.
1004 * @return An iterator designating the end of the resulting sequence.
1006 * Removes all but the first element from each group of consecutive
1007 * values that compare equal.
1008 * unique() is stable, so the relative order of elements that are
1009 * not removed is unchanged.
1010 * Elements between the end of the resulting sequence and @p last
1011 * are still present, but their value is unspecified.
1013 template<typename _ForwardIterator
>
1015 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1017 // concept requirements
1018 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1020 __glibcxx_function_requires(_EqualityComparableConcept
<
1021 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1022 __glibcxx_requires_valid_range(__first
, __last
);
1024 // Skip the beginning, if already unique.
1025 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
1026 if (__first
== __last
)
1029 // Do the real copy work.
1030 _ForwardIterator __dest
= __first
;
1032 while (++__first
!= __last
)
1033 if (!(*__dest
== *__first
))
1034 *++__dest
= _GLIBCXX_MOVE(*__first
);
1039 * @brief Remove consecutive values from a sequence using a predicate.
1040 * @param first A forward iterator.
1041 * @param last A forward iterator.
1042 * @param binary_pred A binary predicate.
1043 * @return An iterator designating the end of the resulting sequence.
1045 * Removes all but the first element from each group of consecutive
1046 * values for which @p binary_pred returns true.
1047 * unique() is stable, so the relative order of elements that are
1048 * not removed is unchanged.
1049 * Elements between the end of the resulting sequence and @p last
1050 * are still present, but their value is unspecified.
1052 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1054 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1055 _BinaryPredicate __binary_pred
)
1057 // concept requirements
1058 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1060 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1061 typename iterator_traits
<_ForwardIterator
>::value_type
,
1062 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1063 __glibcxx_requires_valid_range(__first
, __last
);
1065 // Skip the beginning, if already unique.
1066 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
1067 if (__first
== __last
)
1070 // Do the real copy work.
1071 _ForwardIterator __dest
= __first
;
1073 while (++__first
!= __last
)
1074 if (!bool(__binary_pred(*__dest
, *__first
)))
1075 *++__dest
= _GLIBCXX_MOVE(*__first
);
1080 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1082 * overloaded for forward iterators and output iterator as result.
1084 template<typename _ForwardIterator
, typename _OutputIterator
>
1086 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1087 _OutputIterator __result
,
1088 forward_iterator_tag
, output_iterator_tag
)
1090 // concept requirements -- taken care of in dispatching function
1091 _ForwardIterator __next
= __first
;
1092 *__result
= *__first
;
1093 while (++__next
!= __last
)
1094 if (!(*__first
== *__next
))
1097 *++__result
= *__first
;
1103 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1105 * overloaded for input iterators and output iterator as result.
1107 template<typename _InputIterator
, typename _OutputIterator
>
1109 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1110 _OutputIterator __result
,
1111 input_iterator_tag
, output_iterator_tag
)
1113 // concept requirements -- taken care of in dispatching function
1114 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1115 *__result
= __value
;
1116 while (++__first
!= __last
)
1117 if (!(__value
== *__first
))
1120 *++__result
= __value
;
1126 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1128 * overloaded for input iterators and forward iterator as result.
1130 template<typename _InputIterator
, typename _ForwardIterator
>
1132 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1133 _ForwardIterator __result
,
1134 input_iterator_tag
, forward_iterator_tag
)
1136 // concept requirements -- taken care of in dispatching function
1137 *__result
= *__first
;
1138 while (++__first
!= __last
)
1139 if (!(*__result
== *__first
))
1140 *++__result
= *__first
;
1145 * This is an uglified
1146 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1148 * overloaded for forward iterators and output iterator as result.
1150 template<typename _ForwardIterator
, typename _OutputIterator
,
1151 typename _BinaryPredicate
>
1153 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1154 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1155 forward_iterator_tag
, output_iterator_tag
)
1157 // concept requirements -- iterators already checked
1158 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1159 typename iterator_traits
<_ForwardIterator
>::value_type
,
1160 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1162 _ForwardIterator __next
= __first
;
1163 *__result
= *__first
;
1164 while (++__next
!= __last
)
1165 if (!bool(__binary_pred(*__first
, *__next
)))
1168 *++__result
= *__first
;
1174 * This is an uglified
1175 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1177 * overloaded for input iterators and output iterator as result.
1179 template<typename _InputIterator
, typename _OutputIterator
,
1180 typename _BinaryPredicate
>
1182 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1183 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1184 input_iterator_tag
, output_iterator_tag
)
1186 // concept requirements -- iterators already checked
1187 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1188 typename iterator_traits
<_InputIterator
>::value_type
,
1189 typename iterator_traits
<_InputIterator
>::value_type
>)
1191 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1192 *__result
= __value
;
1193 while (++__first
!= __last
)
1194 if (!bool(__binary_pred(__value
, *__first
)))
1197 *++__result
= __value
;
1203 * This is an uglified
1204 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1206 * overloaded for input iterators and forward iterator as result.
1208 template<typename _InputIterator
, typename _ForwardIterator
,
1209 typename _BinaryPredicate
>
1211 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1212 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1213 input_iterator_tag
, forward_iterator_tag
)
1215 // concept requirements -- iterators already checked
1216 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1217 typename iterator_traits
<_ForwardIterator
>::value_type
,
1218 typename iterator_traits
<_InputIterator
>::value_type
>)
1220 *__result
= *__first
;
1221 while (++__first
!= __last
)
1222 if (!bool(__binary_pred(*__result
, *__first
)))
1223 *++__result
= *__first
;
1228 * This is an uglified reverse(_BidirectionalIterator,
1229 * _BidirectionalIterator)
1230 * overloaded for bidirectional iterators.
1232 template<typename _BidirectionalIterator
>
1234 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1235 bidirectional_iterator_tag
)
1238 if (__first
== __last
|| __first
== --__last
)
1242 std::iter_swap(__first
, __last
);
1248 * This is an uglified reverse(_BidirectionalIterator,
1249 * _BidirectionalIterator)
1250 * overloaded for random access iterators.
1252 template<typename _RandomAccessIterator
>
1254 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1255 random_access_iterator_tag
)
1257 if (__first
== __last
)
1260 while (__first
< __last
)
1262 std::iter_swap(__first
, __last
);
1269 * @brief Reverse a sequence.
1270 * @param first A bidirectional iterator.
1271 * @param last A bidirectional iterator.
1272 * @return reverse() returns no value.
1274 * Reverses the order of the elements in the range @p [first,last),
1275 * so that the first element becomes the last etc.
1276 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1277 * swaps @p *(first+i) and @p *(last-(i+1))
1279 template<typename _BidirectionalIterator
>
1281 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1283 // concept requirements
1284 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1285 _BidirectionalIterator
>)
1286 __glibcxx_requires_valid_range(__first
, __last
);
1287 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1291 * @brief Copy a sequence, reversing its elements.
1292 * @param first A bidirectional iterator.
1293 * @param last A bidirectional iterator.
1294 * @param result An output iterator.
1295 * @return An iterator designating the end of the resulting sequence.
1297 * Copies the elements in the range @p [first,last) to the range
1298 * @p [result,result+(last-first)) such that the order of the
1299 * elements is reversed.
1300 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1301 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1302 * The ranges @p [first,last) and @p [result,result+(last-first))
1305 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1307 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1308 _OutputIterator __result
)
1310 // concept requirements
1311 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1312 _BidirectionalIterator
>)
1313 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1314 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1315 __glibcxx_requires_valid_range(__first
, __last
);
1317 while (__first
!= __last
)
1320 *__result
= *__last
;
1327 * This is a helper function for the rotate algorithm specialized on RAIs.
1328 * It returns the greatest common divisor of two integer values.
1330 template<typename _EuclideanRingElement
>
1331 _EuclideanRingElement
1332 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1336 _EuclideanRingElement __t
= __m
% __n
;
1343 /// This is a helper function for the rotate algorithm.
1344 template<typename _ForwardIterator
>
1346 __rotate(_ForwardIterator __first
,
1347 _ForwardIterator __middle
,
1348 _ForwardIterator __last
,
1349 forward_iterator_tag
)
1351 if (__first
== __middle
|| __last
== __middle
)
1354 _ForwardIterator __first2
= __middle
;
1357 std::iter_swap(__first
, __first2
);
1360 if (__first
== __middle
)
1361 __middle
= __first2
;
1363 while (__first2
!= __last
);
1365 __first2
= __middle
;
1367 while (__first2
!= __last
)
1369 std::iter_swap(__first
, __first2
);
1372 if (__first
== __middle
)
1373 __middle
= __first2
;
1374 else if (__first2
== __last
)
1375 __first2
= __middle
;
1379 /// This is a helper function for the rotate algorithm.
1380 template<typename _BidirectionalIterator
>
1382 __rotate(_BidirectionalIterator __first
,
1383 _BidirectionalIterator __middle
,
1384 _BidirectionalIterator __last
,
1385 bidirectional_iterator_tag
)
1387 // concept requirements
1388 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1389 _BidirectionalIterator
>)
1391 if (__first
== __middle
|| __last
== __middle
)
1394 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1395 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1397 while (__first
!= __middle
&& __middle
!= __last
)
1399 std::iter_swap(__first
, --__last
);
1403 if (__first
== __middle
)
1404 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1406 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1409 /// This is a helper function for the rotate algorithm.
1410 template<typename _RandomAccessIterator
>
1412 __rotate(_RandomAccessIterator __first
,
1413 _RandomAccessIterator __middle
,
1414 _RandomAccessIterator __last
,
1415 random_access_iterator_tag
)
1417 // concept requirements
1418 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1419 _RandomAccessIterator
>)
1421 if (__first
== __middle
|| __last
== __middle
)
1424 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1426 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1429 const _Distance __n
= __last
- __first
;
1430 const _Distance __k
= __middle
- __first
;
1431 const _Distance __l
= __n
- __k
;
1435 std::swap_ranges(__first
, __middle
, __middle
);
1439 const _Distance __d
= std::__gcd(__n
, __k
);
1441 for (_Distance __i
= 0; __i
< __d
; __i
++)
1443 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1444 _RandomAccessIterator __p
= __first
;
1448 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1450 if (__p
> __first
+ __l
)
1452 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1456 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1462 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1464 if (__p
< __last
- __k
)
1466 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1469 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1474 *__p
= _GLIBCXX_MOVE(__tmp
);
1480 * @brief Rotate the elements of a sequence.
1481 * @param first A forward iterator.
1482 * @param middle A forward iterator.
1483 * @param last A forward iterator.
1486 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1487 * positions so that the element at @p middle is moved to @p first, the
1488 * element at @p middle+1 is moved to @first+1 and so on for each element
1489 * in the range @p [first,last).
1491 * This effectively swaps the ranges @p [first,middle) and
1494 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1495 * each @p n in the range @p [0,last-first).
1497 template<typename _ForwardIterator
>
1499 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1500 _ForwardIterator __last
)
1502 // concept requirements
1503 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1505 __glibcxx_requires_valid_range(__first
, __middle
);
1506 __glibcxx_requires_valid_range(__middle
, __last
);
1508 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1510 std::__rotate(__first
, __middle
, __last
, _IterType());
1514 * @brief Copy a sequence, rotating its elements.
1515 * @param first A forward iterator.
1516 * @param middle A forward iterator.
1517 * @param last A forward iterator.
1518 * @param result An output iterator.
1519 * @return An iterator designating the end of the resulting sequence.
1521 * Copies the elements of the range @p [first,last) to the range
1522 * beginning at @result, rotating the copied elements by @p (middle-first)
1523 * positions so that the element at @p middle is moved to @p result, the
1524 * element at @p middle+1 is moved to @result+1 and so on for each element
1525 * in the range @p [first,last).
1527 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1528 * each @p n in the range @p [0,last-first).
1530 template<typename _ForwardIterator
, typename _OutputIterator
>
1532 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1533 _ForwardIterator __last
, _OutputIterator __result
)
1535 // concept requirements
1536 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1537 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1538 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1539 __glibcxx_requires_valid_range(__first
, __middle
);
1540 __glibcxx_requires_valid_range(__middle
, __last
);
1542 return std::copy(__first
, __middle
,
1543 std::copy(__middle
, __last
, __result
));
1546 /// This is a helper function...
1547 template<typename _ForwardIterator
, typename _Predicate
>
1549 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1550 _Predicate __pred
, forward_iterator_tag
)
1552 if (__first
== __last
)
1555 while (__pred(*__first
))
1556 if (++__first
== __last
)
1559 _ForwardIterator __next
= __first
;
1561 while (++__next
!= __last
)
1562 if (__pred(*__next
))
1564 std::iter_swap(__first
, __next
);
1571 /// This is a helper function...
1572 template<typename _BidirectionalIterator
, typename _Predicate
>
1573 _BidirectionalIterator
1574 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1575 _Predicate __pred
, bidirectional_iterator_tag
)
1580 if (__first
== __last
)
1582 else if (__pred(*__first
))
1588 if (__first
== __last
)
1590 else if (!bool(__pred(*__last
)))
1594 std::iter_swap(__first
, __last
);
1601 /// This is a helper function...
1602 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1604 __inplace_stable_partition(_ForwardIterator __first
,
1605 _ForwardIterator __last
,
1606 _Predicate __pred
, _Distance __len
)
1609 return __pred(*__first
) ? __last
: __first
;
1610 _ForwardIterator __middle
= __first
;
1611 std::advance(__middle
, __len
/ 2);
1612 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1616 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1620 std::rotate(__begin
, __middle
, __end
);
1621 std::advance(__begin
, std::distance(__middle
, __end
));
1625 /// This is a helper function...
1626 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1629 __stable_partition_adaptive(_ForwardIterator __first
,
1630 _ForwardIterator __last
,
1631 _Predicate __pred
, _Distance __len
,
1633 _Distance __buffer_size
)
1635 if (__len
<= __buffer_size
)
1637 _ForwardIterator __result1
= __first
;
1638 _Pointer __result2
= __buffer
;
1639 for (; __first
!= __last
; ++__first
)
1640 if (__pred(*__first
))
1642 *__result1
= *__first
;
1647 *__result2
= *__first
;
1650 std::copy(__buffer
, __result2
, __result1
);
1655 _ForwardIterator __middle
= __first
;
1656 std::advance(__middle
, __len
/ 2);
1657 _ForwardIterator __begin
=
1658 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1659 __len
/ 2, __buffer
,
1661 _ForwardIterator __end
=
1662 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1664 __buffer
, __buffer_size
);
1665 std::rotate(__begin
, __middle
, __end
);
1666 std::advance(__begin
, std::distance(__middle
, __end
));
1672 * @brief Move elements for which a predicate is true to the beginning
1673 * of a sequence, preserving relative ordering.
1674 * @param first A forward iterator.
1675 * @param last A forward iterator.
1676 * @param pred A predicate functor.
1677 * @return An iterator @p middle such that @p pred(i) is true for each
1678 * iterator @p i in the range @p [first,middle) and false for each @p i
1679 * in the range @p [middle,last).
1681 * Performs the same function as @p partition() with the additional
1682 * guarantee that the relative ordering of elements in each group is
1683 * preserved, so any two elements @p x and @p y in the range
1684 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1685 * relative ordering after calling @p stable_partition().
1687 template<typename _ForwardIterator
, typename _Predicate
>
1689 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1692 // concept requirements
1693 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1695 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1696 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1697 __glibcxx_requires_valid_range(__first
, __last
);
1699 if (__first
== __last
)
1703 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1705 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1708 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1710 if (__buf
.size() > 0)
1712 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1713 _DistanceType(__buf
.requested_size()),
1715 _DistanceType(__buf
.size()));
1718 std::__inplace_stable_partition(__first
, __last
, __pred
,
1719 _DistanceType(__buf
.requested_size()));
1723 /// This is a helper function for the sort routines.
1724 template<typename _RandomAccessIterator
>
1726 __heap_select(_RandomAccessIterator __first
,
1727 _RandomAccessIterator __middle
,
1728 _RandomAccessIterator __last
)
1730 std::make_heap(__first
, __middle
);
1731 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1732 if (*__i
< *__first
)
1733 std::__pop_heap(__first
, __middle
, __i
);
1736 /// This is a helper function for the sort routines.
1737 template<typename _RandomAccessIterator
, typename _Compare
>
1739 __heap_select(_RandomAccessIterator __first
,
1740 _RandomAccessIterator __middle
,
1741 _RandomAccessIterator __last
, _Compare __comp
)
1743 std::make_heap(__first
, __middle
, __comp
);
1744 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1745 if (__comp(*__i
, *__first
))
1746 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1752 * @brief Copy the smallest elements of a sequence.
1753 * @param first An iterator.
1754 * @param last Another iterator.
1755 * @param result_first A random-access iterator.
1756 * @param result_last Another random-access iterator.
1757 * @return An iterator indicating the end of the resulting sequence.
1759 * Copies and sorts the smallest N values from the range @p [first,last)
1760 * to the range beginning at @p result_first, where the number of
1761 * elements to be copied, @p N, is the smaller of @p (last-first) and
1762 * @p (result_last-result_first).
1763 * After the sort if @p i and @j are iterators in the range
1764 * @p [result_first,result_first+N) such that @i precedes @j then
1765 * @p *j<*i is false.
1766 * The value returned is @p result_first+N.
1768 template<typename _InputIterator
, typename _RandomAccessIterator
>
1769 _RandomAccessIterator
1770 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1771 _RandomAccessIterator __result_first
,
1772 _RandomAccessIterator __result_last
)
1774 typedef typename iterator_traits
<_InputIterator
>::value_type
1776 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1778 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1781 // concept requirements
1782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1783 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1785 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1787 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1788 __glibcxx_requires_valid_range(__first
, __last
);
1789 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1791 if (__result_first
== __result_last
)
1792 return __result_last
;
1793 _RandomAccessIterator __result_real_last
= __result_first
;
1794 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1796 *__result_real_last
= *__first
;
1797 ++__result_real_last
;
1800 std::make_heap(__result_first
, __result_real_last
);
1801 while (__first
!= __last
)
1803 if (*__first
< *__result_first
)
1804 std::__adjust_heap(__result_first
, _DistanceType(0),
1805 _DistanceType(__result_real_last
1807 _InputValueType(*__first
));
1810 std::sort_heap(__result_first
, __result_real_last
);
1811 return __result_real_last
;
1815 * @brief Copy the smallest elements of a sequence using a predicate for
1817 * @param first An input iterator.
1818 * @param last Another input iterator.
1819 * @param result_first A random-access iterator.
1820 * @param result_last Another random-access iterator.
1821 * @param comp A comparison functor.
1822 * @return An iterator indicating the end of the resulting sequence.
1824 * Copies and sorts the smallest N values from the range @p [first,last)
1825 * to the range beginning at @p result_first, where the number of
1826 * elements to be copied, @p N, is the smaller of @p (last-first) and
1827 * @p (result_last-result_first).
1828 * After the sort if @p i and @j are iterators in the range
1829 * @p [result_first,result_first+N) such that @i precedes @j then
1830 * @p comp(*j,*i) is false.
1831 * The value returned is @p result_first+N.
1833 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1834 _RandomAccessIterator
1835 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1836 _RandomAccessIterator __result_first
,
1837 _RandomAccessIterator __result_last
,
1840 typedef typename iterator_traits
<_InputIterator
>::value_type
1842 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1844 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1847 // concept requirements
1848 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1849 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1850 _RandomAccessIterator
>)
1851 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1853 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1854 _InputValueType
, _OutputValueType
>)
1855 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1856 _OutputValueType
, _OutputValueType
>)
1857 __glibcxx_requires_valid_range(__first
, __last
);
1858 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1860 if (__result_first
== __result_last
)
1861 return __result_last
;
1862 _RandomAccessIterator __result_real_last
= __result_first
;
1863 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1865 *__result_real_last
= *__first
;
1866 ++__result_real_last
;
1869 std::make_heap(__result_first
, __result_real_last
, __comp
);
1870 while (__first
!= __last
)
1872 if (__comp(*__first
, *__result_first
))
1873 std::__adjust_heap(__result_first
, _DistanceType(0),
1874 _DistanceType(__result_real_last
1876 _InputValueType(*__first
),
1880 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1881 return __result_real_last
;
1884 /// This is a helper function for the sort routine.
1885 template<typename _RandomAccessIterator
, typename _Tp
>
1887 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1889 _RandomAccessIterator __next
= __last
;
1891 while (__val
< *__next
)
1900 /// This is a helper function for the sort routine.
1901 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1903 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1906 _RandomAccessIterator __next
= __last
;
1908 while (__comp(__val
, *__next
))
1917 /// This is a helper function for the sort routine.
1918 template<typename _RandomAccessIterator
>
1920 __insertion_sort(_RandomAccessIterator __first
,
1921 _RandomAccessIterator __last
)
1923 if (__first
== __last
)
1926 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1928 typename iterator_traits
<_RandomAccessIterator
>::value_type
1930 if (__val
< *__first
)
1932 std::copy_backward(__first
, __i
, __i
+ 1);
1936 std::__unguarded_linear_insert(__i
, __val
);
1940 /// This is a helper function for the sort routine.
1941 template<typename _RandomAccessIterator
, typename _Compare
>
1943 __insertion_sort(_RandomAccessIterator __first
,
1944 _RandomAccessIterator __last
, _Compare __comp
)
1946 if (__first
== __last
) return;
1948 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1950 typename iterator_traits
<_RandomAccessIterator
>::value_type
1952 if (__comp(__val
, *__first
))
1954 std::copy_backward(__first
, __i
, __i
+ 1);
1958 std::__unguarded_linear_insert(__i
, __val
, __comp
);
1962 /// This is a helper function for the sort routine.
1963 template<typename _RandomAccessIterator
>
1965 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1966 _RandomAccessIterator __last
)
1968 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1971 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1972 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
1975 /// This is a helper function for the sort routine.
1976 template<typename _RandomAccessIterator
, typename _Compare
>
1978 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1979 _RandomAccessIterator __last
, _Compare __comp
)
1981 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1984 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1985 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
1990 * This controls some aspect of the sort routines.
1992 enum { _S_threshold
= 16 };
1994 /// This is a helper function for the sort routine.
1995 template<typename _RandomAccessIterator
>
1997 __final_insertion_sort(_RandomAccessIterator __first
,
1998 _RandomAccessIterator __last
)
2000 if (__last
- __first
> int(_S_threshold
))
2002 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2003 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2006 std::__insertion_sort(__first
, __last
);
2009 /// This is a helper function for the sort routine.
2010 template<typename _RandomAccessIterator
, typename _Compare
>
2012 __final_insertion_sort(_RandomAccessIterator __first
,
2013 _RandomAccessIterator __last
, _Compare __comp
)
2015 if (__last
- __first
> int(_S_threshold
))
2017 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2018 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2022 std::__insertion_sort(__first
, __last
, __comp
);
2025 /// This is a helper function...
2026 template<typename _RandomAccessIterator
, typename _Tp
>
2027 _RandomAccessIterator
2028 __unguarded_partition(_RandomAccessIterator __first
,
2029 _RandomAccessIterator __last
, _Tp __pivot
)
2033 while (*__first
< __pivot
)
2036 while (__pivot
< *__last
)
2038 if (!(__first
< __last
))
2040 std::iter_swap(__first
, __last
);
2045 /// This is a helper function...
2046 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2047 _RandomAccessIterator
2048 __unguarded_partition(_RandomAccessIterator __first
,
2049 _RandomAccessIterator __last
,
2050 _Tp __pivot
, _Compare __comp
)
2054 while (__comp(*__first
, __pivot
))
2057 while (__comp(__pivot
, *__last
))
2059 if (!(__first
< __last
))
2061 std::iter_swap(__first
, __last
);
2066 /// This is a helper function for the sort routine.
2067 template<typename _RandomAccessIterator
, typename _Size
>
2069 __introsort_loop(_RandomAccessIterator __first
,
2070 _RandomAccessIterator __last
,
2071 _Size __depth_limit
)
2073 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2076 while (__last
- __first
> int(_S_threshold
))
2078 if (__depth_limit
== 0)
2080 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2084 _RandomAccessIterator __cut
=
2085 std::__unguarded_partition(__first
, __last
,
2086 _ValueType(std::__median(*__first
,
2093 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2098 /// This is a helper function for the sort routine.
2099 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2101 __introsort_loop(_RandomAccessIterator __first
,
2102 _RandomAccessIterator __last
,
2103 _Size __depth_limit
, _Compare __comp
)
2105 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2108 while (__last
- __first
> int(_S_threshold
))
2110 if (__depth_limit
== 0)
2112 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2116 _RandomAccessIterator __cut
=
2117 std::__unguarded_partition(__first
, __last
,
2118 _ValueType(std::__median(*__first
,
2126 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2131 /// This is a helper function for the sort routines. Precondition: __n > 0.
2132 template<typename _Size
>
2137 for (__k
= 0; __n
!= 0; __n
>>= 1)
2144 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2148 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2152 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2156 template<typename _RandomAccessIterator
, typename _Size
>
2158 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2159 _RandomAccessIterator __last
, _Size __depth_limit
)
2161 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2164 while (__last
- __first
> 3)
2166 if (__depth_limit
== 0)
2168 std::__heap_select(__first
, __nth
+ 1, __last
);
2170 // Place the nth largest element in its final position.
2171 std::iter_swap(__first
, __nth
);
2175 _RandomAccessIterator __cut
=
2176 std::__unguarded_partition(__first
, __last
,
2177 _ValueType(std::__median(*__first
,
2189 std::__insertion_sort(__first
, __last
);
2192 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2194 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2195 _RandomAccessIterator __last
, _Size __depth_limit
,
2198 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2201 while (__last
- __first
> 3)
2203 if (__depth_limit
== 0)
2205 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2206 // Place the nth largest element in its final position.
2207 std::iter_swap(__first
, __nth
);
2211 _RandomAccessIterator __cut
=
2212 std::__unguarded_partition(__first
, __last
,
2213 _ValueType(std::__median(*__first
,
2226 std::__insertion_sort(__first
, __last
, __comp
);
2232 * @brief Finds the first position in which @a val could be inserted
2233 * without changing the ordering.
2234 * @param first An iterator.
2235 * @param last Another iterator.
2236 * @param val The search term.
2237 * @return An iterator pointing to the first element "not less
2238 * than" @a val, or end() if every element is less than
2240 * @ingroup binarysearch
2242 template<typename _ForwardIterator
, typename _Tp
>
2244 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2247 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2249 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2252 // concept requirements
2253 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2254 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2255 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2257 _DistanceType __len
= std::distance(__first
, __last
);
2258 _DistanceType __half
;
2259 _ForwardIterator __middle
;
2263 __half
= __len
>> 1;
2265 std::advance(__middle
, __half
);
2266 if (*__middle
< __val
)
2270 __len
= __len
- __half
- 1;
2279 * @brief Finds the first position in which @a val could be inserted
2280 * without changing the ordering.
2281 * @param first An iterator.
2282 * @param last Another iterator.
2283 * @param val The search term.
2284 * @param comp A functor to use for comparisons.
2285 * @return An iterator pointing to the first element "not less than" @a val,
2286 * or end() if every element is less than @a val.
2287 * @ingroup binarysearch
2289 * The comparison function should have the same effects on ordering as
2290 * the function used for the initial sort.
2292 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2294 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2295 const _Tp
& __val
, _Compare __comp
)
2297 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2299 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2302 // concept requirements
2303 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2304 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2306 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2309 _DistanceType __len
= std::distance(__first
, __last
);
2310 _DistanceType __half
;
2311 _ForwardIterator __middle
;
2315 __half
= __len
>> 1;
2317 std::advance(__middle
, __half
);
2318 if (__comp(*__middle
, __val
))
2322 __len
= __len
- __half
- 1;
2331 * @brief Finds the last position in which @a val could be inserted
2332 * without changing the ordering.
2333 * @param first An iterator.
2334 * @param last Another iterator.
2335 * @param val The search term.
2336 * @return An iterator pointing to the first element greater than @a val,
2337 * or end() if no elements are greater than @a val.
2338 * @ingroup binarysearch
2340 template<typename _ForwardIterator
, typename _Tp
>
2342 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2345 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2347 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2350 // concept requirements
2351 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2352 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2353 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2355 _DistanceType __len
= std::distance(__first
, __last
);
2356 _DistanceType __half
;
2357 _ForwardIterator __middle
;
2361 __half
= __len
>> 1;
2363 std::advance(__middle
, __half
);
2364 if (__val
< *__middle
)
2370 __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 * @param comp A functor to use for comparisons.
2383 * @return An iterator pointing to the first element greater than @a val,
2384 * or end() if no elements are greater than @a val.
2385 * @ingroup binarysearch
2387 * The comparison function should have the same effects on ordering as
2388 * the function used for the initial sort.
2390 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2392 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2393 const _Tp
& __val
, _Compare __comp
)
2395 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2397 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2400 // concept requirements
2401 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2402 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2404 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2407 _DistanceType __len
= std::distance(__first
, __last
);
2408 _DistanceType __half
;
2409 _ForwardIterator __middle
;
2413 __half
= __len
>> 1;
2415 std::advance(__middle
, __half
);
2416 if (__comp(__val
, *__middle
))
2422 __len
= __len
- __half
- 1;
2429 * @brief Finds the largest subrange in which @a val could be inserted
2430 * at any place in it without changing the ordering.
2431 * @param first An iterator.
2432 * @param last Another iterator.
2433 * @param val The search term.
2434 * @return An pair of iterators defining the subrange.
2435 * @ingroup binarysearch
2437 * This is equivalent to
2439 * std::make_pair(lower_bound(first, last, val),
2440 * upper_bound(first, last, val))
2442 * but does not actually call those functions.
2444 template<typename _ForwardIterator
, typename _Tp
>
2445 pair
<_ForwardIterator
, _ForwardIterator
>
2446 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2449 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2451 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2454 // concept requirements
2455 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2456 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2457 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2458 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2459 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2461 _DistanceType __len
= std::distance(__first
, __last
);
2462 _DistanceType __half
;
2463 _ForwardIterator __middle
, __left
, __right
;
2467 __half
= __len
>> 1;
2469 std::advance(__middle
, __half
);
2470 if (*__middle
< __val
)
2474 __len
= __len
- __half
- 1;
2476 else if (__val
< *__middle
)
2480 __left
= std::lower_bound(__first
, __middle
, __val
);
2481 std::advance(__first
, __len
);
2482 __right
= std::upper_bound(++__middle
, __first
, __val
);
2483 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2486 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2490 * @brief Finds the largest subrange in which @a val could be inserted
2491 * at any place in it without changing the ordering.
2492 * @param first An iterator.
2493 * @param last Another iterator.
2494 * @param val The search term.
2495 * @param comp A functor to use for comparisons.
2496 * @return An pair of iterators defining the subrange.
2497 * @ingroup binarysearch
2499 * This is equivalent to
2501 * std::make_pair(lower_bound(first, last, val, comp),
2502 * upper_bound(first, last, val, comp))
2504 * but does not actually call those functions.
2506 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2507 pair
<_ForwardIterator
, _ForwardIterator
>
2508 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2512 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2514 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2517 // concept requirements
2518 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2519 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2521 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2523 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2525 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2528 _DistanceType __len
= std::distance(__first
, __last
);
2529 _DistanceType __half
;
2530 _ForwardIterator __middle
, __left
, __right
;
2534 __half
= __len
>> 1;
2536 std::advance(__middle
, __half
);
2537 if (__comp(*__middle
, __val
))
2541 __len
= __len
- __half
- 1;
2543 else if (__comp(__val
, *__middle
))
2547 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2548 std::advance(__first
, __len
);
2549 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2550 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2553 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2557 * @brief Determines whether an element exists in a range.
2558 * @param first An iterator.
2559 * @param last Another iterator.
2560 * @param val The search term.
2561 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2562 * @ingroup binarysearch
2564 * Note that this does not actually return an iterator to @a val. For
2565 * that, use std::find or a container's specialized find member functions.
2567 template<typename _ForwardIterator
, typename _Tp
>
2569 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2572 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2575 // concept requirements
2576 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2577 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2578 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2579 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2581 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2582 return __i
!= __last
&& !(__val
< *__i
);
2586 * @brief Determines whether an element exists in a range.
2587 * @param first An iterator.
2588 * @param last Another iterator.
2589 * @param val The search term.
2590 * @param comp A functor to use for comparisons.
2591 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2592 * @ingroup binarysearch
2594 * Note that this does not actually return an iterator to @a val. For
2595 * that, use std::find or a container's specialized find member functions.
2597 * The comparison function should have the same effects on ordering as
2598 * the function used for the initial sort.
2600 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2602 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2603 const _Tp
& __val
, _Compare __comp
)
2605 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2608 // concept requirements
2609 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2610 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2612 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2614 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2617 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2618 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2623 /// This is a helper function for the merge routines.
2624 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2625 typename _BidirectionalIterator3
>
2626 _BidirectionalIterator3
2627 __merge_backward(_BidirectionalIterator1 __first1
,
2628 _BidirectionalIterator1 __last1
,
2629 _BidirectionalIterator2 __first2
,
2630 _BidirectionalIterator2 __last2
,
2631 _BidirectionalIterator3 __result
)
2633 if (__first1
== __last1
)
2634 return std::copy_backward(__first2
, __last2
, __result
);
2635 if (__first2
== __last2
)
2636 return std::copy_backward(__first1
, __last1
, __result
);
2641 if (*__last2
< *__last1
)
2643 *--__result
= *__last1
;
2644 if (__first1
== __last1
)
2645 return std::copy_backward(__first2
, ++__last2
, __result
);
2650 *--__result
= *__last2
;
2651 if (__first2
== __last2
)
2652 return std::copy_backward(__first1
, ++__last1
, __result
);
2658 /// This is a helper function for the merge routines.
2659 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2660 typename _BidirectionalIterator3
, typename _Compare
>
2661 _BidirectionalIterator3
2662 __merge_backward(_BidirectionalIterator1 __first1
,
2663 _BidirectionalIterator1 __last1
,
2664 _BidirectionalIterator2 __first2
,
2665 _BidirectionalIterator2 __last2
,
2666 _BidirectionalIterator3 __result
,
2669 if (__first1
== __last1
)
2670 return std::copy_backward(__first2
, __last2
, __result
);
2671 if (__first2
== __last2
)
2672 return std::copy_backward(__first1
, __last1
, __result
);
2677 if (__comp(*__last2
, *__last1
))
2679 *--__result
= *__last1
;
2680 if (__first1
== __last1
)
2681 return std::copy_backward(__first2
, ++__last2
, __result
);
2686 *--__result
= *__last2
;
2687 if (__first2
== __last2
)
2688 return std::copy_backward(__first1
, ++__last1
, __result
);
2694 /// This is a helper function for the merge routines.
2695 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2697 _BidirectionalIterator1
2698 __rotate_adaptive(_BidirectionalIterator1 __first
,
2699 _BidirectionalIterator1 __middle
,
2700 _BidirectionalIterator1 __last
,
2701 _Distance __len1
, _Distance __len2
,
2702 _BidirectionalIterator2 __buffer
,
2703 _Distance __buffer_size
)
2705 _BidirectionalIterator2 __buffer_end
;
2706 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2708 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2709 std::copy_backward(__first
, __middle
, __last
);
2710 return std::copy(__buffer
, __buffer_end
, __first
);
2712 else if (__len1
<= __buffer_size
)
2714 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2715 std::copy(__middle
, __last
, __first
);
2716 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2720 std::rotate(__first
, __middle
, __last
);
2721 std::advance(__first
, std::distance(__middle
, __last
));
2726 /// This is a helper function for the merge routines.
2727 template<typename _BidirectionalIterator
, typename _Distance
,
2730 __merge_adaptive(_BidirectionalIterator __first
,
2731 _BidirectionalIterator __middle
,
2732 _BidirectionalIterator __last
,
2733 _Distance __len1
, _Distance __len2
,
2734 _Pointer __buffer
, _Distance __buffer_size
)
2736 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2738 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2739 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2742 else if (__len2
<= __buffer_size
)
2744 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2745 std::__merge_backward(__first
, __middle
, __buffer
,
2746 __buffer_end
, __last
);
2750 _BidirectionalIterator __first_cut
= __first
;
2751 _BidirectionalIterator __second_cut
= __middle
;
2752 _Distance __len11
= 0;
2753 _Distance __len22
= 0;
2754 if (__len1
> __len2
)
2756 __len11
= __len1
/ 2;
2757 std::advance(__first_cut
, __len11
);
2758 __second_cut
= std::lower_bound(__middle
, __last
,
2760 __len22
= std::distance(__middle
, __second_cut
);
2764 __len22
= __len2
/ 2;
2765 std::advance(__second_cut
, __len22
);
2766 __first_cut
= std::upper_bound(__first
, __middle
,
2768 __len11
= std::distance(__first
, __first_cut
);
2770 _BidirectionalIterator __new_middle
=
2771 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2772 __len1
- __len11
, __len22
, __buffer
,
2774 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2775 __len22
, __buffer
, __buffer_size
);
2776 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2778 __len2
- __len22
, __buffer
, __buffer_size
);
2782 /// This is a helper function for the merge routines.
2783 template<typename _BidirectionalIterator
, typename _Distance
,
2784 typename _Pointer
, typename _Compare
>
2786 __merge_adaptive(_BidirectionalIterator __first
,
2787 _BidirectionalIterator __middle
,
2788 _BidirectionalIterator __last
,
2789 _Distance __len1
, _Distance __len2
,
2790 _Pointer __buffer
, _Distance __buffer_size
,
2793 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2795 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2796 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2799 else if (__len2
<= __buffer_size
)
2801 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2802 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2807 _BidirectionalIterator __first_cut
= __first
;
2808 _BidirectionalIterator __second_cut
= __middle
;
2809 _Distance __len11
= 0;
2810 _Distance __len22
= 0;
2811 if (__len1
> __len2
)
2813 __len11
= __len1
/ 2;
2814 std::advance(__first_cut
, __len11
);
2815 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2817 __len22
= std::distance(__middle
, __second_cut
);
2821 __len22
= __len2
/ 2;
2822 std::advance(__second_cut
, __len22
);
2823 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2825 __len11
= std::distance(__first
, __first_cut
);
2827 _BidirectionalIterator __new_middle
=
2828 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2829 __len1
- __len11
, __len22
, __buffer
,
2831 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2832 __len22
, __buffer
, __buffer_size
, __comp
);
2833 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2835 __len2
- __len22
, __buffer
,
2836 __buffer_size
, __comp
);
2840 /// This is a helper function for the merge routines.
2841 template<typename _BidirectionalIterator
, typename _Distance
>
2843 __merge_without_buffer(_BidirectionalIterator __first
,
2844 _BidirectionalIterator __middle
,
2845 _BidirectionalIterator __last
,
2846 _Distance __len1
, _Distance __len2
)
2848 if (__len1
== 0 || __len2
== 0)
2850 if (__len1
+ __len2
== 2)
2852 if (*__middle
< *__first
)
2853 std::iter_swap(__first
, __middle
);
2856 _BidirectionalIterator __first_cut
= __first
;
2857 _BidirectionalIterator __second_cut
= __middle
;
2858 _Distance __len11
= 0;
2859 _Distance __len22
= 0;
2860 if (__len1
> __len2
)
2862 __len11
= __len1
/ 2;
2863 std::advance(__first_cut
, __len11
);
2864 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2865 __len22
= std::distance(__middle
, __second_cut
);
2869 __len22
= __len2
/ 2;
2870 std::advance(__second_cut
, __len22
);
2871 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2872 __len11
= std::distance(__first
, __first_cut
);
2874 std::rotate(__first_cut
, __middle
, __second_cut
);
2875 _BidirectionalIterator __new_middle
= __first_cut
;
2876 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2877 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2879 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2880 __len1
- __len11
, __len2
- __len22
);
2883 /// This is a helper function for the merge routines.
2884 template<typename _BidirectionalIterator
, typename _Distance
,
2887 __merge_without_buffer(_BidirectionalIterator __first
,
2888 _BidirectionalIterator __middle
,
2889 _BidirectionalIterator __last
,
2890 _Distance __len1
, _Distance __len2
,
2893 if (__len1
== 0 || __len2
== 0)
2895 if (__len1
+ __len2
== 2)
2897 if (__comp(*__middle
, *__first
))
2898 std::iter_swap(__first
, __middle
);
2901 _BidirectionalIterator __first_cut
= __first
;
2902 _BidirectionalIterator __second_cut
= __middle
;
2903 _Distance __len11
= 0;
2904 _Distance __len22
= 0;
2905 if (__len1
> __len2
)
2907 __len11
= __len1
/ 2;
2908 std::advance(__first_cut
, __len11
);
2909 __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
,
2919 __len11
= std::distance(__first
, __first_cut
);
2921 std::rotate(__first_cut
, __middle
, __second_cut
);
2922 _BidirectionalIterator __new_middle
= __first_cut
;
2923 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2924 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2925 __len11
, __len22
, __comp
);
2926 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2927 __len1
- __len11
, __len2
- __len22
, __comp
);
2931 * @brief Merges two sorted ranges in place.
2932 * @param first An iterator.
2933 * @param middle Another iterator.
2934 * @param last Another iterator.
2937 * Merges two sorted and consecutive ranges, [first,middle) and
2938 * [middle,last), and puts the result in [first,last). The output will
2939 * be sorted. The sort is @e stable, that is, for equivalent
2940 * elements in the two ranges, elements from the first range will always
2941 * come before elements from the second.
2943 * If enough additional memory is available, this takes (last-first)-1
2944 * comparisons. Otherwise an NlogN algorithm is used, where N is
2945 * distance(first,last).
2947 template<typename _BidirectionalIterator
>
2949 inplace_merge(_BidirectionalIterator __first
,
2950 _BidirectionalIterator __middle
,
2951 _BidirectionalIterator __last
)
2953 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2955 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2958 // concept requirements
2959 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2960 _BidirectionalIterator
>)
2961 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2962 __glibcxx_requires_sorted(__first
, __middle
);
2963 __glibcxx_requires_sorted(__middle
, __last
);
2965 if (__first
== __middle
|| __middle
== __last
)
2968 _DistanceType __len1
= std::distance(__first
, __middle
);
2969 _DistanceType __len2
= std::distance(__middle
, __last
);
2971 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2973 if (__buf
.begin() == 0)
2974 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
2976 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2977 __buf
.begin(), _DistanceType(__buf
.size()));
2981 * @brief Merges two sorted ranges in place.
2982 * @param first An iterator.
2983 * @param middle Another iterator.
2984 * @param last Another iterator.
2985 * @param comp A functor to use for comparisons.
2988 * Merges two sorted and consecutive ranges, [first,middle) and
2989 * [middle,last), and puts the result in [first,last). The output will
2990 * be sorted. The sort is @e stable, that is, for equivalent
2991 * elements in the two ranges, elements from the first range will always
2992 * come before elements from the second.
2994 * If enough additional memory is available, this takes (last-first)-1
2995 * comparisons. Otherwise an NlogN algorithm is used, where N is
2996 * distance(first,last).
2998 * The comparison function should have the same effects on ordering as
2999 * the function used for the initial sort.
3001 template<typename _BidirectionalIterator
, typename _Compare
>
3003 inplace_merge(_BidirectionalIterator __first
,
3004 _BidirectionalIterator __middle
,
3005 _BidirectionalIterator __last
,
3008 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3010 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3013 // concept requirements
3014 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3015 _BidirectionalIterator
>)
3016 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3017 _ValueType
, _ValueType
>)
3018 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3019 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3021 if (__first
== __middle
|| __middle
== __last
)
3024 const _DistanceType __len1
= std::distance(__first
, __middle
);
3025 const _DistanceType __len2
= std::distance(__middle
, __last
);
3027 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3029 if (__buf
.begin() == 0)
3030 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3033 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3034 __buf
.begin(), _DistanceType(__buf
.size()),
3038 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3041 __merge_sort_loop(_RandomAccessIterator1 __first
,
3042 _RandomAccessIterator1 __last
,
3043 _RandomAccessIterator2 __result
,
3044 _Distance __step_size
)
3046 const _Distance __two_step
= 2 * __step_size
;
3048 while (__last
- __first
>= __two_step
)
3050 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3051 __first
+ __step_size
,
3052 __first
+ __two_step
,
3054 __first
+= __two_step
;
3057 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3058 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3059 __first
+ __step_size
, __last
,
3063 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3064 typename _Distance
, typename _Compare
>
3066 __merge_sort_loop(_RandomAccessIterator1 __first
,
3067 _RandomAccessIterator1 __last
,
3068 _RandomAccessIterator2 __result
, _Distance __step_size
,
3071 const _Distance __two_step
= 2 * __step_size
;
3073 while (__last
- __first
>= __two_step
)
3075 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3076 __first
+ __step_size
, __first
+ __two_step
,
3079 __first
+= __two_step
;
3081 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3083 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3084 __first
+ __step_size
, __last
, __result
, __comp
);
3087 template<typename _RandomAccessIterator
, typename _Distance
>
3089 __chunk_insertion_sort(_RandomAccessIterator __first
,
3090 _RandomAccessIterator __last
,
3091 _Distance __chunk_size
)
3093 while (__last
- __first
>= __chunk_size
)
3095 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3096 __first
+= __chunk_size
;
3098 std::__insertion_sort(__first
, __last
);
3101 template<typename _RandomAccessIterator
, typename _Distance
,
3104 __chunk_insertion_sort(_RandomAccessIterator __first
,
3105 _RandomAccessIterator __last
,
3106 _Distance __chunk_size
, _Compare __comp
)
3108 while (__last
- __first
>= __chunk_size
)
3110 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3111 __first
+= __chunk_size
;
3113 std::__insertion_sort(__first
, __last
, __comp
);
3116 enum { _S_chunk_size
= 7 };
3118 template<typename _RandomAccessIterator
, typename _Pointer
>
3120 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3121 _RandomAccessIterator __last
,
3124 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3127 const _Distance __len
= __last
- __first
;
3128 const _Pointer __buffer_last
= __buffer
+ __len
;
3130 _Distance __step_size
= _S_chunk_size
;
3131 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3133 while (__step_size
< __len
)
3135 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3137 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3142 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3144 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3145 _RandomAccessIterator __last
,
3146 _Pointer __buffer
, _Compare __comp
)
3148 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3151 const _Distance __len
= __last
- __first
;
3152 const _Pointer __buffer_last
= __buffer
+ __len
;
3154 _Distance __step_size
= _S_chunk_size
;
3155 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3157 while (__step_size
< __len
)
3159 std::__merge_sort_loop(__first
, __last
, __buffer
,
3160 __step_size
, __comp
);
3162 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3163 __step_size
, __comp
);
3168 template<typename _RandomAccessIterator
, typename _Pointer
,
3171 __stable_sort_adaptive(_RandomAccessIterator __first
,
3172 _RandomAccessIterator __last
,
3173 _Pointer __buffer
, _Distance __buffer_size
)
3175 const _Distance __len
= (__last
- __first
+ 1) / 2;
3176 const _RandomAccessIterator __middle
= __first
+ __len
;
3177 if (__len
> __buffer_size
)
3179 std::__stable_sort_adaptive(__first
, __middle
,
3180 __buffer
, __buffer_size
);
3181 std::__stable_sort_adaptive(__middle
, __last
,
3182 __buffer
, __buffer_size
);
3186 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3187 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3189 std::__merge_adaptive(__first
, __middle
, __last
,
3190 _Distance(__middle
- __first
),
3191 _Distance(__last
- __middle
),
3192 __buffer
, __buffer_size
);
3195 template<typename _RandomAccessIterator
, typename _Pointer
,
3196 typename _Distance
, typename _Compare
>
3198 __stable_sort_adaptive(_RandomAccessIterator __first
,
3199 _RandomAccessIterator __last
,
3200 _Pointer __buffer
, _Distance __buffer_size
,
3203 const _Distance __len
= (__last
- __first
+ 1) / 2;
3204 const _RandomAccessIterator __middle
= __first
+ __len
;
3205 if (__len
> __buffer_size
)
3207 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3208 __buffer_size
, __comp
);
3209 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3210 __buffer_size
, __comp
);
3214 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3215 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3217 std::__merge_adaptive(__first
, __middle
, __last
,
3218 _Distance(__middle
- __first
),
3219 _Distance(__last
- __middle
),
3220 __buffer
, __buffer_size
,
3224 /// This is a helper function for the stable sorting routines.
3225 template<typename _RandomAccessIterator
>
3227 __inplace_stable_sort(_RandomAccessIterator __first
,
3228 _RandomAccessIterator __last
)
3230 if (__last
- __first
< 15)
3232 std::__insertion_sort(__first
, __last
);
3235 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3236 std::__inplace_stable_sort(__first
, __middle
);
3237 std::__inplace_stable_sort(__middle
, __last
);
3238 std::__merge_without_buffer(__first
, __middle
, __last
,
3243 /// This is a helper function for the stable sorting routines.
3244 template<typename _RandomAccessIterator
, typename _Compare
>
3246 __inplace_stable_sort(_RandomAccessIterator __first
,
3247 _RandomAccessIterator __last
, _Compare __comp
)
3249 if (__last
- __first
< 15)
3251 std::__insertion_sort(__first
, __last
, __comp
);
3254 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3255 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3256 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3257 std::__merge_without_buffer(__first
, __middle
, __last
,
3265 // Set algorithms: includes, set_union, set_intersection, set_difference,
3266 // set_symmetric_difference. All of these algorithms have the precondition
3267 // that their input ranges are sorted and the postcondition that their output
3268 // ranges are sorted.
3271 * @brief Determines whether all elements of a sequence exists in a range.
3272 * @param first1 Start of search range.
3273 * @param last1 End of search range.
3274 * @param first2 Start of sequence
3275 * @param last2 End of sequence.
3276 * @return True if each element in [first2,last2) is contained in order
3277 * within [first1,last1). False otherwise.
3278 * @ingroup setoperations
3280 * This operation expects both [first1,last1) and [first2,last2) to be
3281 * sorted. Searches for the presence of each element in [first2,last2)
3282 * within [first1,last1). The iterators over each range only move forward,
3283 * so this is a linear algorithm. If an element in [first2,last2) is not
3284 * found before the search iterator reaches @a last2, false is returned.
3286 template<typename _InputIterator1
, typename _InputIterator2
>
3288 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3289 _InputIterator2 __first2
, _InputIterator2 __last2
)
3291 typedef typename iterator_traits
<_InputIterator1
>::value_type
3293 typedef typename iterator_traits
<_InputIterator2
>::value_type
3296 // concept requirements
3297 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3298 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3299 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3300 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3301 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3302 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3304 while (__first1
!= __last1
&& __first2
!= __last2
)
3305 if (*__first2
< *__first1
)
3307 else if(*__first1
< *__first2
)
3310 ++__first1
, ++__first2
;
3312 return __first2
== __last2
;
3316 * @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 * @param comp Comparison function to use.
3323 * @return True if each element in [first2,last2) is contained in order
3324 * within [first1,last1) according to comp. False otherwise.
3325 * @ingroup setoperations
3327 * This operation expects both [first1,last1) and [first2,last2) to be
3328 * sorted. Searches for the presence of each element in [first2,last2)
3329 * within [first1,last1), using comp to decide. The iterators over each
3330 * range only move forward, so this is a linear algorithm. If an element
3331 * in [first2,last2) is not found before the search iterator reaches @a
3332 * last2, false is returned.
3334 template<typename _InputIterator1
, typename _InputIterator2
,
3337 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3338 _InputIterator2 __first2
, _InputIterator2 __last2
,
3341 typedef typename iterator_traits
<_InputIterator1
>::value_type
3343 typedef typename iterator_traits
<_InputIterator2
>::value_type
3346 // concept requirements
3347 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3348 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3349 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3350 _ValueType1
, _ValueType2
>)
3351 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3352 _ValueType2
, _ValueType1
>)
3353 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3354 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3356 while (__first1
!= __last1
&& __first2
!= __last2
)
3357 if (__comp(*__first2
, *__first1
))
3359 else if(__comp(*__first1
, *__first2
))
3362 ++__first1
, ++__first2
;
3364 return __first2
== __last2
;
3373 // set_symmetric_difference
3378 * @brief Permute range into the next "dictionary" ordering.
3379 * @param first Start of range.
3380 * @param last End of range.
3381 * @return False if wrapped to first permutation, true otherwise.
3383 * Treats all permutations of the range as a set of "dictionary" sorted
3384 * sequences. Permutes the current sequence into the next one of this set.
3385 * Returns true if there are more sequences to generate. If the sequence
3386 * is the largest of the set, the smallest is generated and false returned.
3388 template<typename _BidirectionalIterator
>
3390 next_permutation(_BidirectionalIterator __first
,
3391 _BidirectionalIterator __last
)
3393 // concept requirements
3394 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3395 _BidirectionalIterator
>)
3396 __glibcxx_function_requires(_LessThanComparableConcept
<
3397 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3398 __glibcxx_requires_valid_range(__first
, __last
);
3400 if (__first
== __last
)
3402 _BidirectionalIterator __i
= __first
;
3411 _BidirectionalIterator __ii
= __i
;
3415 _BidirectionalIterator __j
= __last
;
3416 while (!(*__i
< *--__j
))
3418 std::iter_swap(__i
, __j
);
3419 std::reverse(__ii
, __last
);
3424 std::reverse(__first
, __last
);
3431 * @brief Permute range into the next "dictionary" ordering using
3432 * comparison functor.
3433 * @param first Start of range.
3434 * @param last End of range.
3435 * @param comp A comparison functor.
3436 * @return False if wrapped to first permutation, true otherwise.
3438 * Treats all permutations of the range [first,last) as a set of
3439 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3440 * sequence into the next one of this set. Returns true if there are more
3441 * sequences to generate. If the sequence is the largest of the set, the
3442 * smallest is generated and false returned.
3444 template<typename _BidirectionalIterator
, typename _Compare
>
3446 next_permutation(_BidirectionalIterator __first
,
3447 _BidirectionalIterator __last
, _Compare __comp
)
3449 // concept requirements
3450 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3451 _BidirectionalIterator
>)
3452 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3453 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3454 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3455 __glibcxx_requires_valid_range(__first
, __last
);
3457 if (__first
== __last
)
3459 _BidirectionalIterator __i
= __first
;
3468 _BidirectionalIterator __ii
= __i
;
3470 if (__comp(*__i
, *__ii
))
3472 _BidirectionalIterator __j
= __last
;
3473 while (!bool(__comp(*__i
, *--__j
)))
3475 std::iter_swap(__i
, __j
);
3476 std::reverse(__ii
, __last
);
3481 std::reverse(__first
, __last
);
3488 * @brief Permute range into the previous "dictionary" ordering.
3489 * @param first Start of range.
3490 * @param last End of range.
3491 * @return False if wrapped to last permutation, true otherwise.
3493 * Treats all permutations of the range as a set of "dictionary" sorted
3494 * sequences. Permutes the current sequence into the previous one of this
3495 * set. Returns true if there are more sequences to generate. If the
3496 * sequence is the smallest of the set, the largest is generated and false
3499 template<typename _BidirectionalIterator
>
3501 prev_permutation(_BidirectionalIterator __first
,
3502 _BidirectionalIterator __last
)
3504 // concept requirements
3505 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3506 _BidirectionalIterator
>)
3507 __glibcxx_function_requires(_LessThanComparableConcept
<
3508 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3509 __glibcxx_requires_valid_range(__first
, __last
);
3511 if (__first
== __last
)
3513 _BidirectionalIterator __i
= __first
;
3522 _BidirectionalIterator __ii
= __i
;
3526 _BidirectionalIterator __j
= __last
;
3527 while (!(*--__j
< *__i
))
3529 std::iter_swap(__i
, __j
);
3530 std::reverse(__ii
, __last
);
3535 std::reverse(__first
, __last
);
3542 * @brief Permute range into the previous "dictionary" ordering using
3543 * comparison functor.
3544 * @param first Start of range.
3545 * @param last End of range.
3546 * @param comp A comparison functor.
3547 * @return False if wrapped to last permutation, true otherwise.
3549 * Treats all permutations of the range [first,last) as a set of
3550 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3551 * sequence into the previous one of this set. Returns true if there are
3552 * more sequences to generate. If the sequence is the smallest of the set,
3553 * the largest is generated and false returned.
3555 template<typename _BidirectionalIterator
, typename _Compare
>
3557 prev_permutation(_BidirectionalIterator __first
,
3558 _BidirectionalIterator __last
, _Compare __comp
)
3560 // concept requirements
3561 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3562 _BidirectionalIterator
>)
3563 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3564 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3565 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3566 __glibcxx_requires_valid_range(__first
, __last
);
3568 if (__first
== __last
)
3570 _BidirectionalIterator __i
= __first
;
3579 _BidirectionalIterator __ii
= __i
;
3581 if (__comp(*__ii
, *__i
))
3583 _BidirectionalIterator __j
= __last
;
3584 while (!bool(__comp(*--__j
, *__i
)))
3586 std::iter_swap(__i
, __j
);
3587 std::reverse(__ii
, __last
);
3592 std::reverse(__first
, __last
);
3602 * @brief Copy a sequence, replacing each element of one value with another
3604 * @param first An input iterator.
3605 * @param last An input iterator.
3606 * @param result An output iterator.
3607 * @param old_value The value to be replaced.
3608 * @param new_value The replacement value.
3609 * @return The end of the output sequence, @p result+(last-first).
3611 * Copies each element in the input range @p [first,last) to the
3612 * output range @p [result,result+(last-first)) replacing elements
3613 * equal to @p old_value with @p new_value.
3615 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3617 replace_copy(_InputIterator __first
, _InputIterator __last
,
3618 _OutputIterator __result
,
3619 const _Tp
& __old_value
, const _Tp
& __new_value
)
3621 // concept requirements
3622 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3623 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3624 typename iterator_traits
<_InputIterator
>::value_type
>)
3625 __glibcxx_function_requires(_EqualOpConcept
<
3626 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3627 __glibcxx_requires_valid_range(__first
, __last
);
3629 for (; __first
!= __last
; ++__first
, ++__result
)
3630 if (*__first
== __old_value
)
3631 *__result
= __new_value
;
3633 *__result
= *__first
;
3638 * @brief Copy a sequence, replacing each value for which a predicate
3639 * returns true with another value.
3640 * @param first An input iterator.
3641 * @param last An input iterator.
3642 * @param result An output iterator.
3643 * @param pred A predicate.
3644 * @param new_value The replacement value.
3645 * @return The end of the output sequence, @p result+(last-first).
3647 * Copies each element in the range @p [first,last) to the range
3648 * @p [result,result+(last-first)) replacing elements for which
3649 * @p pred returns true with @p new_value.
3651 template<typename _InputIterator
, typename _OutputIterator
,
3652 typename _Predicate
, typename _Tp
>
3654 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3655 _OutputIterator __result
,
3656 _Predicate __pred
, const _Tp
& __new_value
)
3658 // concept requirements
3659 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3660 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3661 typename iterator_traits
<_InputIterator
>::value_type
>)
3662 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3663 typename iterator_traits
<_InputIterator
>::value_type
>)
3664 __glibcxx_requires_valid_range(__first
, __last
);
3666 for (; __first
!= __last
; ++__first
, ++__result
)
3667 if (__pred(*__first
))
3668 *__result
= __new_value
;
3670 *__result
= *__first
;
3674 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3676 * @brief Determines whether the elements of a sequence are sorted.
3677 * @param first An iterator.
3678 * @param last Another iterator.
3679 * @return True if the elements are sorted, false otherwise.
3681 template<typename _ForwardIterator
>
3683 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3684 { return std::is_sorted_until(__first
, __last
) == __last
; }
3687 * @brief Determines whether the elements of a sequence are sorted
3688 * according to a comparison functor.
3689 * @param first An iterator.
3690 * @param last Another iterator.
3691 * @param comp A comparison functor.
3692 * @return True if the elements are sorted, false otherwise.
3694 template<typename _ForwardIterator
, typename _Compare
>
3696 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3698 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3701 * @brief Determines the end of a sorted sequence.
3702 * @param first An iterator.
3703 * @param last Another iterator.
3704 * @return An iterator pointing to the last iterator i in [first, last)
3705 * for which the range [first, i) is sorted.
3707 template<typename _ForwardIterator
>
3709 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3711 // concept requirements
3712 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3713 __glibcxx_function_requires(_LessThanComparableConcept
<
3714 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3715 __glibcxx_requires_valid_range(__first
, __last
);
3717 if (__first
== __last
)
3720 _ForwardIterator __next
= __first
;
3721 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3722 if (*__next
< *__first
)
3728 * @brief Determines the end of a sorted sequence using comparison functor.
3729 * @param first An iterator.
3730 * @param last Another iterator.
3731 * @param comp A comparison functor.
3732 * @return An iterator pointing to the last iterator i in [first, last)
3733 * for which the range [first, i) is sorted.
3735 template<typename _ForwardIterator
, typename _Compare
>
3737 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3740 // concept requirements
3741 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3742 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3743 typename iterator_traits
<_ForwardIterator
>::value_type
,
3744 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3745 __glibcxx_requires_valid_range(__first
, __last
);
3747 if (__first
== __last
)
3750 _ForwardIterator __next
= __first
;
3751 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3752 if (__comp(*__next
, *__first
))
3758 * @brief Determines min and max at once as an ordered pair.
3759 * @param a A thing of arbitrary type.
3760 * @param b Another thing of arbitrary type.
3761 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3763 template<typename _Tp
>
3764 inline pair
<const _Tp
&, const _Tp
&>
3765 minmax(const _Tp
& __a
, const _Tp
& __b
)
3767 // concept requirements
3768 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3770 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3771 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3775 * @brief Determines min and max at once as an ordered pair.
3776 * @param a A thing of arbitrary type.
3777 * @param b Another thing of arbitrary type.
3778 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3779 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3781 template<typename _Tp
, typename _Compare
>
3782 inline pair
<const _Tp
&, const _Tp
&>
3783 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3785 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3786 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3790 * @brief Return a pair of iterators pointing to the minimum and maximum
3791 * elements in a range.
3792 * @param first Start of range.
3793 * @param last End of range.
3794 * @return make_pair(m, M), where m is the first iterator i in
3795 * [first, last) such that no other element in the range is
3796 * smaller, and where M is the last iterator i in [first, last)
3797 * such that no other element in the range is larger.
3799 template<typename _ForwardIterator
>
3800 pair
<_ForwardIterator
, _ForwardIterator
>
3801 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3803 // concept requirements
3804 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3805 __glibcxx_function_requires(_LessThanComparableConcept
<
3806 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3807 __glibcxx_requires_valid_range(__first
, __last
);
3809 _ForwardIterator __next
= __first
;
3810 if (__first
== __last
3811 || ++__next
== __last
)
3812 return std::make_pair(__first
, __first
);
3814 _ForwardIterator __min
, __max
;
3815 if (*__next
< *__first
)
3829 while (__first
!= __last
)
3832 if (++__next
== __last
)
3834 if (*__first
< *__min
)
3836 else if (!(*__first
< *__max
))
3841 if (*__next
< *__first
)
3843 if (*__next
< *__min
)
3845 if (!(*__first
< *__max
))
3850 if (*__first
< *__min
)
3852 if (!(*__next
< *__max
))
3860 return std::make_pair(__min
, __max
);
3864 * @brief Return a pair of iterators pointing to the minimum and maximum
3865 * elements in a range.
3866 * @param first Start of range.
3867 * @param last End of range.
3868 * @param comp Comparison functor.
3869 * @return make_pair(m, M), where m is the first iterator i in
3870 * [first, last) such that no other element in the range is
3871 * smaller, and where M is the last iterator i in [first, last)
3872 * such that no other element in the range is larger.
3874 template<typename _ForwardIterator
, typename _Compare
>
3875 pair
<_ForwardIterator
, _ForwardIterator
>
3876 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3879 // concept requirements
3880 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3881 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3882 typename iterator_traits
<_ForwardIterator
>::value_type
,
3883 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3884 __glibcxx_requires_valid_range(__first
, __last
);
3886 _ForwardIterator __next
= __first
;
3887 if (__first
== __last
3888 || ++__next
== __last
)
3889 return std::make_pair(__first
, __first
);
3891 _ForwardIterator __min
, __max
;
3892 if (__comp(*__next
, *__first
))
3906 while (__first
!= __last
)
3909 if (++__next
== __last
)
3911 if (__comp(*__first
, *__min
))
3913 else if (!__comp(*__first
, *__max
))
3918 if (__comp(*__next
, *__first
))
3920 if (__comp(*__next
, *__min
))
3922 if (!__comp(*__first
, *__max
))
3927 if (__comp(*__first
, *__min
))
3929 if (!__comp(*__next
, *__max
))
3937 return std::make_pair(__min
, __max
);
3939 #endif // __GXX_EXPERIMENTAL_CXX0X__
3941 _GLIBCXX_END_NAMESPACE
3943 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3946 * @brief Apply a function to every element of a sequence.
3947 * @param first An input iterator.
3948 * @param last An input iterator.
3949 * @param f A unary function object.
3952 * Applies the function object @p f to each element in the range
3953 * @p [first,last). @p f must not modify the order of the sequence.
3954 * If @p f has a return value it is ignored.
3956 template<typename _InputIterator
, typename _Function
>
3958 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3960 // concept requirements
3961 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3962 __glibcxx_requires_valid_range(__first
, __last
);
3963 for (; __first
!= __last
; ++__first
)
3969 * @brief Find the first occurrence of a value in a sequence.
3970 * @param first An input iterator.
3971 * @param last An input iterator.
3972 * @param val The value to find.
3973 * @return The first iterator @c i in the range @p [first,last)
3974 * such that @c *i == @p val, or @p last if no such iterator exists.
3976 template<typename _InputIterator
, typename _Tp
>
3977 inline _InputIterator
3978 find(_InputIterator __first
, _InputIterator __last
,
3981 // concept requirements
3982 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3983 __glibcxx_function_requires(_EqualOpConcept
<
3984 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3985 __glibcxx_requires_valid_range(__first
, __last
);
3986 return std::__find(__first
, __last
, __val
,
3987 std::__iterator_category(__first
));
3991 * @brief Find the first element in a sequence for which a
3992 * predicate is true.
3993 * @param first An input iterator.
3994 * @param last An input iterator.
3995 * @param pred A predicate.
3996 * @return The first iterator @c i in the range @p [first,last)
3997 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3999 template<typename _InputIterator
, typename _Predicate
>
4000 inline _InputIterator
4001 find_if(_InputIterator __first
, _InputIterator __last
,
4004 // concept requirements
4005 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4006 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4007 typename iterator_traits
<_InputIterator
>::value_type
>)
4008 __glibcxx_requires_valid_range(__first
, __last
);
4009 return std::__find_if(__first
, __last
, __pred
,
4010 std::__iterator_category(__first
));
4014 * @brief Find element from a set in a sequence.
4015 * @param first1 Start of range to search.
4016 * @param last1 End of range to search.
4017 * @param first2 Start of match candidates.
4018 * @param last2 End of match candidates.
4019 * @return The first iterator @c i in the range
4020 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4021 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4023 * Searches the range @p [first1,last1) for an element that is equal to
4024 * some element in the range [first2,last2). If found, returns an iterator
4025 * in the range [first1,last1), otherwise returns @p last1.
4027 template<typename _InputIterator
, typename _ForwardIterator
>
4029 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4030 _ForwardIterator __first2
, _ForwardIterator __last2
)
4032 // concept requirements
4033 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4034 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4035 __glibcxx_function_requires(_EqualOpConcept
<
4036 typename iterator_traits
<_InputIterator
>::value_type
,
4037 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4038 __glibcxx_requires_valid_range(__first1
, __last1
);
4039 __glibcxx_requires_valid_range(__first2
, __last2
);
4041 for (; __first1
!= __last1
; ++__first1
)
4042 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4043 if (*__first1
== *__iter
)
4049 * @brief Find element from a set in a sequence using a predicate.
4050 * @param first1 Start of range to search.
4051 * @param last1 End of range to search.
4052 * @param first2 Start of match candidates.
4053 * @param last2 End of match candidates.
4054 * @param comp Predicate to use.
4055 * @return The first iterator @c i in the range
4056 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4057 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4060 * Searches the range @p [first1,last1) for an element that is
4061 * equal to some element in the range [first2,last2). If found,
4062 * returns an iterator in the range [first1,last1), otherwise
4065 template<typename _InputIterator
, typename _ForwardIterator
,
4066 typename _BinaryPredicate
>
4068 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4069 _ForwardIterator __first2
, _ForwardIterator __last2
,
4070 _BinaryPredicate __comp
)
4072 // concept requirements
4073 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4074 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4075 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4076 typename iterator_traits
<_InputIterator
>::value_type
,
4077 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4078 __glibcxx_requires_valid_range(__first1
, __last1
);
4079 __glibcxx_requires_valid_range(__first2
, __last2
);
4081 for (; __first1
!= __last1
; ++__first1
)
4082 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4083 if (__comp(*__first1
, *__iter
))
4089 * @brief Find two adjacent values in a sequence that are equal.
4090 * @param first A forward iterator.
4091 * @param last A forward iterator.
4092 * @return The first iterator @c i such that @c i and @c i+1 are both
4093 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4094 * or @p last if no such iterator exists.
4096 template<typename _ForwardIterator
>
4098 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4100 // concept requirements
4101 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4102 __glibcxx_function_requires(_EqualityComparableConcept
<
4103 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4104 __glibcxx_requires_valid_range(__first
, __last
);
4105 if (__first
== __last
)
4107 _ForwardIterator __next
= __first
;
4108 while(++__next
!= __last
)
4110 if (*__first
== *__next
)
4118 * @brief Find two adjacent values in a sequence using a predicate.
4119 * @param first A forward iterator.
4120 * @param last A forward iterator.
4121 * @param binary_pred A binary predicate.
4122 * @return The first iterator @c i such that @c i and @c i+1 are both
4123 * valid iterators in @p [first,last) and such that
4124 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4127 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4129 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4130 _BinaryPredicate __binary_pred
)
4132 // concept requirements
4133 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4134 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4135 typename iterator_traits
<_ForwardIterator
>::value_type
,
4136 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4137 __glibcxx_requires_valid_range(__first
, __last
);
4138 if (__first
== __last
)
4140 _ForwardIterator __next
= __first
;
4141 while(++__next
!= __last
)
4143 if (__binary_pred(*__first
, *__next
))
4151 * @brief Count the number of copies of a value in a sequence.
4152 * @param first An input iterator.
4153 * @param last An input iterator.
4154 * @param value The value to be counted.
4155 * @return The number of iterators @c i in the range @p [first,last)
4156 * for which @c *i == @p value
4158 template<typename _InputIterator
, typename _Tp
>
4159 typename iterator_traits
<_InputIterator
>::difference_type
4160 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4162 // concept requirements
4163 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4164 __glibcxx_function_requires(_EqualOpConcept
<
4165 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4166 __glibcxx_requires_valid_range(__first
, __last
);
4167 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4168 for (; __first
!= __last
; ++__first
)
4169 if (*__first
== __value
)
4175 * @brief Count the elements of a sequence for which a predicate is true.
4176 * @param first An input iterator.
4177 * @param last An input iterator.
4178 * @param pred A predicate.
4179 * @return The number of iterators @c i in the range @p [first,last)
4180 * for which @p pred(*i) is true.
4182 template<typename _InputIterator
, typename _Predicate
>
4183 typename iterator_traits
<_InputIterator
>::difference_type
4184 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4186 // concept requirements
4187 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4188 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4189 typename iterator_traits
<_InputIterator
>::value_type
>)
4190 __glibcxx_requires_valid_range(__first
, __last
);
4191 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4192 for (; __first
!= __last
; ++__first
)
4193 if (__pred(*__first
))
4199 * @brief Search a sequence for a matching sub-sequence.
4200 * @param first1 A forward iterator.
4201 * @param last1 A forward iterator.
4202 * @param first2 A forward iterator.
4203 * @param last2 A forward iterator.
4204 * @return The first iterator @c i in the range
4205 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4206 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4207 * such iterator exists.
4209 * Searches the range @p [first1,last1) for a sub-sequence that compares
4210 * equal value-by-value with the sequence given by @p [first2,last2) and
4211 * returns an iterator to the first element of the sub-sequence, or
4212 * @p last1 if the sub-sequence is not found.
4214 * Because the sub-sequence must lie completely within the range
4215 * @p [first1,last1) it must start at a position less than
4216 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4218 * This means that the returned iterator @c i will be in the range
4219 * @p [first1,last1-(last2-first2))
4221 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4223 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4224 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4226 // concept requirements
4227 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4228 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4229 __glibcxx_function_requires(_EqualOpConcept
<
4230 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4231 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4232 __glibcxx_requires_valid_range(__first1
, __last1
);
4233 __glibcxx_requires_valid_range(__first2
, __last2
);
4235 // Test for empty ranges
4236 if (__first1
== __last1
|| __first2
== __last2
)
4239 // Test for a pattern of length 1.
4240 _ForwardIterator2
__p1(__first2
);
4241 if (++__p1
== __last2
)
4242 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4245 _ForwardIterator2 __p
;
4246 _ForwardIterator1 __current
= __first1
;
4250 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4251 if (__first1
== __last1
)
4255 __current
= __first1
;
4256 if (++__current
== __last1
)
4259 while (*__current
== *__p
)
4261 if (++__p
== __last2
)
4263 if (++__current
== __last1
)
4272 * @brief Search a sequence for a matching sub-sequence using a predicate.
4273 * @param first1 A forward iterator.
4274 * @param last1 A forward iterator.
4275 * @param first2 A forward iterator.
4276 * @param last2 A forward iterator.
4277 * @param predicate A binary predicate.
4278 * @return The first iterator @c i in the range
4279 * @p [first1,last1-(last2-first2)) such that
4280 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4281 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4283 * Searches the range @p [first1,last1) for a sub-sequence that compares
4284 * equal value-by-value with the sequence given by @p [first2,last2),
4285 * using @p predicate to determine equality, and returns an iterator
4286 * to the first element of the sub-sequence, or @p last1 if no such
4289 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4291 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4292 typename _BinaryPredicate
>
4294 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4295 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4296 _BinaryPredicate __predicate
)
4298 // concept requirements
4299 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4300 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4301 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4302 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4303 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4304 __glibcxx_requires_valid_range(__first1
, __last1
);
4305 __glibcxx_requires_valid_range(__first2
, __last2
);
4307 // Test for empty ranges
4308 if (__first1
== __last1
|| __first2
== __last2
)
4311 // Test for a pattern of length 1.
4312 _ForwardIterator2
__p1(__first2
);
4313 if (++__p1
== __last2
)
4315 while (__first1
!= __last1
4316 && !bool(__predicate(*__first1
, *__first2
)))
4322 _ForwardIterator2 __p
;
4323 _ForwardIterator1 __current
= __first1
;
4327 while (__first1
!= __last1
4328 && !bool(__predicate(*__first1
, *__first2
)))
4330 if (__first1
== __last1
)
4334 __current
= __first1
;
4335 if (++__current
== __last1
)
4338 while (__predicate(*__current
, *__p
))
4340 if (++__p
== __last2
)
4342 if (++__current
== __last1
)
4352 * @brief Search a sequence for a number of consecutive values.
4353 * @param first A forward iterator.
4354 * @param last A forward iterator.
4355 * @param count The number of consecutive values.
4356 * @param val The value to find.
4357 * @return The first iterator @c i in the range @p [first,last-count)
4358 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4359 * or @p last if no such iterator exists.
4361 * Searches the range @p [first,last) for @p count consecutive elements
4364 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4366 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4367 _Integer __count
, const _Tp
& __val
)
4369 // concept requirements
4370 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4371 __glibcxx_function_requires(_EqualOpConcept
<
4372 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4373 __glibcxx_requires_valid_range(__first
, __last
);
4378 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4379 return std::__search_n(__first
, __last
, __count
, __val
,
4380 std::__iterator_category(__first
));
4385 * @brief Search a sequence for a number of consecutive values using a
4387 * @param first A forward iterator.
4388 * @param last A forward iterator.
4389 * @param count The number of consecutive values.
4390 * @param val The value to find.
4391 * @param binary_pred A binary predicate.
4392 * @return The first iterator @c i in the range @p [first,last-count)
4393 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4394 * range @p [0,count), or @p last if no such iterator exists.
4396 * Searches the range @p [first,last) for @p count consecutive elements
4397 * for which the predicate returns true.
4399 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4400 typename _BinaryPredicate
>
4402 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4403 _Integer __count
, const _Tp
& __val
,
4404 _BinaryPredicate __binary_pred
)
4406 // concept requirements
4407 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4408 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4409 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4410 __glibcxx_requires_valid_range(__first
, __last
);
4416 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4420 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4421 std::__iterator_category(__first
));
4426 * @brief Perform an operation on a sequence.
4427 * @param first An input iterator.
4428 * @param last An input iterator.
4429 * @param result An output iterator.
4430 * @param unary_op A unary operator.
4431 * @return An output iterator equal to @p result+(last-first).
4433 * Applies the operator to each element in the input range and assigns
4434 * the results to successive elements of the output sequence.
4435 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4436 * range @p [0,last-first).
4438 * @p unary_op must not alter its argument.
4440 template<typename _InputIterator
, typename _OutputIterator
,
4441 typename _UnaryOperation
>
4443 transform(_InputIterator __first
, _InputIterator __last
,
4444 _OutputIterator __result
, _UnaryOperation __unary_op
)
4446 // concept requirements
4447 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4448 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4449 // "the type returned by a _UnaryOperation"
4450 __typeof__(__unary_op(*__first
))>)
4451 __glibcxx_requires_valid_range(__first
, __last
);
4453 for (; __first
!= __last
; ++__first
, ++__result
)
4454 *__result
= __unary_op(*__first
);
4459 * @brief Perform an operation on corresponding elements of two sequences.
4460 * @param first1 An input iterator.
4461 * @param last1 An input iterator.
4462 * @param first2 An input iterator.
4463 * @param result An output iterator.
4464 * @param binary_op A binary operator.
4465 * @return An output iterator equal to @p result+(last-first).
4467 * Applies the operator to the corresponding elements in the two
4468 * input ranges and assigns the results to successive elements of the
4470 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4471 * @c N in the range @p [0,last1-first1).
4473 * @p binary_op must not alter either of its arguments.
4475 template<typename _InputIterator1
, typename _InputIterator2
,
4476 typename _OutputIterator
, typename _BinaryOperation
>
4478 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4479 _InputIterator2 __first2
, _OutputIterator __result
,
4480 _BinaryOperation __binary_op
)
4482 // concept requirements
4483 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4484 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4485 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4486 // "the type returned by a _BinaryOperation"
4487 __typeof__(__binary_op(*__first1
,*__first2
))>)
4488 __glibcxx_requires_valid_range(__first1
, __last1
);
4490 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4491 *__result
= __binary_op(*__first1
, *__first2
);
4496 * @brief Replace each occurrence of one value in a sequence with another
4498 * @param first A forward iterator.
4499 * @param last A forward iterator.
4500 * @param old_value The value to be replaced.
4501 * @param new_value The replacement value.
4502 * @return replace() returns no value.
4504 * For each iterator @c i in the range @p [first,last) if @c *i ==
4505 * @p old_value then the assignment @c *i = @p new_value is performed.
4507 template<typename _ForwardIterator
, typename _Tp
>
4509 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4510 const _Tp
& __old_value
, const _Tp
& __new_value
)
4512 // concept requirements
4513 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4515 __glibcxx_function_requires(_EqualOpConcept
<
4516 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4517 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4518 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4519 __glibcxx_requires_valid_range(__first
, __last
);
4521 for (; __first
!= __last
; ++__first
)
4522 if (*__first
== __old_value
)
4523 *__first
= __new_value
;
4527 * @brief Replace each value in a sequence for which a predicate returns
4528 * true with another value.
4529 * @param first A forward iterator.
4530 * @param last A forward iterator.
4531 * @param pred A predicate.
4532 * @param new_value The replacement value.
4533 * @return replace_if() returns no value.
4535 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4536 * is true then the assignment @c *i = @p new_value is performed.
4538 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4540 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4541 _Predicate __pred
, const _Tp
& __new_value
)
4543 // concept requirements
4544 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4546 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4547 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4548 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4549 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4550 __glibcxx_requires_valid_range(__first
, __last
);
4552 for (; __first
!= __last
; ++__first
)
4553 if (__pred(*__first
))
4554 *__first
= __new_value
;
4558 * @brief Assign the result of a function object to each value in a
4560 * @param first A forward iterator.
4561 * @param last A forward iterator.
4562 * @param gen A function object taking no arguments and returning
4563 * std::iterator_traits<_ForwardIterator>::value_type
4564 * @return generate() returns no value.
4566 * Performs the assignment @c *i = @p gen() for each @c i in the range
4569 template<typename _ForwardIterator
, typename _Generator
>
4571 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4574 // concept requirements
4575 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4576 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4577 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4578 __glibcxx_requires_valid_range(__first
, __last
);
4580 for (; __first
!= __last
; ++__first
)
4585 * @brief Assign the result of a function object to each value in a
4587 * @param first A forward iterator.
4588 * @param n The length of the sequence.
4589 * @param gen A function object taking no arguments and returning
4590 * std::iterator_traits<_ForwardIterator>::value_type
4591 * @return The end of the sequence, @p first+n
4593 * Performs the assignment @c *i = @p gen() for each @c i in the range
4594 * @p [first,first+n).
4596 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4598 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4600 // concept requirements
4601 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4602 // "the type returned by a _Generator"
4603 __typeof__(__gen())>)
4605 for (; __n
> 0; --__n
, ++__first
)
4612 * @brief Copy a sequence, removing consecutive duplicate values.
4613 * @param first An input iterator.
4614 * @param last An input iterator.
4615 * @param result An output iterator.
4616 * @return An iterator designating the end of the resulting sequence.
4618 * Copies each element in the range @p [first,last) to the range
4619 * beginning at @p result, except that only the first element is copied
4620 * from groups of consecutive elements that compare equal.
4621 * unique_copy() is stable, so the relative order of elements that are
4622 * copied is unchanged.
4624 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4625 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4627 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4628 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4631 template<typename _InputIterator
, typename _OutputIterator
>
4632 inline _OutputIterator
4633 unique_copy(_InputIterator __first
, _InputIterator __last
,
4634 _OutputIterator __result
)
4636 // concept requirements
4637 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4638 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4639 typename iterator_traits
<_InputIterator
>::value_type
>)
4640 __glibcxx_function_requires(_EqualityComparableConcept
<
4641 typename iterator_traits
<_InputIterator
>::value_type
>)
4642 __glibcxx_requires_valid_range(__first
, __last
);
4644 if (__first
== __last
)
4646 return std::__unique_copy(__first
, __last
, __result
,
4647 std::__iterator_category(__first
),
4648 std::__iterator_category(__result
));
4652 * @brief Copy a sequence, removing consecutive values using a predicate.
4653 * @param first An input iterator.
4654 * @param last An input iterator.
4655 * @param result An output iterator.
4656 * @param binary_pred A binary predicate.
4657 * @return An iterator designating the end of the resulting sequence.
4659 * Copies each element in the range @p [first,last) to the range
4660 * beginning at @p result, except that only the first element is copied
4661 * from groups of consecutive elements for which @p binary_pred returns
4663 * unique_copy() is stable, so the relative order of elements that are
4664 * copied is unchanged.
4666 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4667 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4669 template<typename _InputIterator
, typename _OutputIterator
,
4670 typename _BinaryPredicate
>
4671 inline _OutputIterator
4672 unique_copy(_InputIterator __first
, _InputIterator __last
,
4673 _OutputIterator __result
,
4674 _BinaryPredicate __binary_pred
)
4676 // concept requirements -- predicates checked later
4677 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4678 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4679 typename iterator_traits
<_InputIterator
>::value_type
>)
4680 __glibcxx_requires_valid_range(__first
, __last
);
4682 if (__first
== __last
)
4684 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4685 std::__iterator_category(__first
),
4686 std::__iterator_category(__result
));
4691 * @brief Randomly shuffle the elements of a sequence.
4692 * @param first A forward iterator.
4693 * @param last A forward iterator.
4696 * Reorder the elements in the range @p [first,last) using a random
4697 * distribution, so that every possible ordering of the sequence is
4700 template<typename _RandomAccessIterator
>
4702 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4704 // concept requirements
4705 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4706 _RandomAccessIterator
>)
4707 __glibcxx_requires_valid_range(__first
, __last
);
4709 if (__first
!= __last
)
4710 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4711 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4715 * @brief Shuffle the elements of a sequence using a random number
4717 * @param first A forward iterator.
4718 * @param last A forward iterator.
4719 * @param rand The RNG functor or function.
4722 * Reorders the elements in the range @p [first,last) using @p rand to
4723 * provide a random distribution. Calling @p rand(N) for a positive
4724 * integer @p N should return a randomly chosen integer from the
4727 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4729 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4730 _RandomNumberGenerator
& __rand
)
4732 // concept requirements
4733 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4734 _RandomAccessIterator
>)
4735 __glibcxx_requires_valid_range(__first
, __last
);
4737 if (__first
== __last
)
4739 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4740 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4745 * @brief Move elements for which a predicate is true to the beginning
4747 * @param first A forward iterator.
4748 * @param last A forward iterator.
4749 * @param pred A predicate functor.
4750 * @return An iterator @p middle such that @p pred(i) is true for each
4751 * iterator @p i in the range @p [first,middle) and false for each @p i
4752 * in the range @p [middle,last).
4754 * @p pred must not modify its operand. @p partition() does not preserve
4755 * the relative ordering of elements in each group, use
4756 * @p stable_partition() if this is needed.
4758 template<typename _ForwardIterator
, typename _Predicate
>
4759 inline _ForwardIterator
4760 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4763 // concept requirements
4764 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4766 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4767 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4768 __glibcxx_requires_valid_range(__first
, __last
);
4770 return std::__partition(__first
, __last
, __pred
,
4771 std::__iterator_category(__first
));
4777 * @brief Sort the smallest elements of a sequence.
4778 * @param first An iterator.
4779 * @param middle Another iterator.
4780 * @param last Another iterator.
4783 * Sorts the smallest @p (middle-first) elements in the range
4784 * @p [first,last) and moves them to the range @p [first,middle). The
4785 * order of the remaining elements in the range @p [middle,last) is
4787 * After the sort if @p i and @j are iterators in the range
4788 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4789 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4791 template<typename _RandomAccessIterator
>
4793 partial_sort(_RandomAccessIterator __first
,
4794 _RandomAccessIterator __middle
,
4795 _RandomAccessIterator __last
)
4797 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4800 // concept requirements
4801 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4802 _RandomAccessIterator
>)
4803 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4804 __glibcxx_requires_valid_range(__first
, __middle
);
4805 __glibcxx_requires_valid_range(__middle
, __last
);
4807 std::__heap_select(__first
, __middle
, __last
);
4808 std::sort_heap(__first
, __middle
);
4812 * @brief Sort the smallest elements of a sequence using a predicate
4814 * @param first An iterator.
4815 * @param middle Another iterator.
4816 * @param last Another iterator.
4817 * @param comp A comparison functor.
4820 * Sorts the smallest @p (middle-first) elements in the range
4821 * @p [first,last) and moves them to the range @p [first,middle). The
4822 * order of the remaining elements in the range @p [middle,last) is
4824 * After the sort if @p i and @j are iterators in the range
4825 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4826 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4829 template<typename _RandomAccessIterator
, typename _Compare
>
4831 partial_sort(_RandomAccessIterator __first
,
4832 _RandomAccessIterator __middle
,
4833 _RandomAccessIterator __last
,
4836 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4839 // concept requirements
4840 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4841 _RandomAccessIterator
>)
4842 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4843 _ValueType
, _ValueType
>)
4844 __glibcxx_requires_valid_range(__first
, __middle
);
4845 __glibcxx_requires_valid_range(__middle
, __last
);
4847 std::__heap_select(__first
, __middle
, __last
, __comp
);
4848 std::sort_heap(__first
, __middle
, __comp
);
4852 * @brief Sort a sequence just enough to find a particular position.
4853 * @param first An iterator.
4854 * @param nth Another iterator.
4855 * @param last Another iterator.
4858 * Rearranges the elements in the range @p [first,last) so that @p *nth
4859 * is the same element that would have been in that position had the
4860 * whole sequence been sorted.
4861 * whole sequence been sorted. The elements either side of @p *nth are
4862 * not completely sorted, but for any iterator @i in the range
4863 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4864 * holds that @p *j<*i is false.
4866 template<typename _RandomAccessIterator
>
4868 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4869 _RandomAccessIterator __last
)
4871 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4874 // concept requirements
4875 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4876 _RandomAccessIterator
>)
4877 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4878 __glibcxx_requires_valid_range(__first
, __nth
);
4879 __glibcxx_requires_valid_range(__nth
, __last
);
4881 if (__first
== __last
|| __nth
== __last
)
4884 std::__introselect(__first
, __nth
, __last
,
4885 std::__lg(__last
- __first
) * 2);
4889 * @brief Sort a sequence just enough to find a particular position
4890 * using a predicate for comparison.
4891 * @param first An iterator.
4892 * @param nth Another iterator.
4893 * @param last Another iterator.
4894 * @param comp A comparison functor.
4897 * Rearranges the elements in the range @p [first,last) so that @p *nth
4898 * is the same element that would have been in that position had the
4899 * whole sequence been sorted. The elements either side of @p *nth are
4900 * not completely sorted, but for any iterator @i in the range
4901 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4902 * holds that @p comp(*j,*i) is false.
4904 template<typename _RandomAccessIterator
, typename _Compare
>
4906 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4907 _RandomAccessIterator __last
, _Compare __comp
)
4909 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4912 // concept requirements
4913 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4914 _RandomAccessIterator
>)
4915 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4916 _ValueType
, _ValueType
>)
4917 __glibcxx_requires_valid_range(__first
, __nth
);
4918 __glibcxx_requires_valid_range(__nth
, __last
);
4920 if (__first
== __last
|| __nth
== __last
)
4923 std::__introselect(__first
, __nth
, __last
,
4924 std::__lg(__last
- __first
) * 2, __comp
);
4929 * @brief Sort the elements of a sequence.
4930 * @param first An iterator.
4931 * @param last Another iterator.
4934 * Sorts the elements in the range @p [first,last) in ascending order,
4935 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4936 * @p [first,last-1).
4938 * The relative ordering of equivalent elements is not preserved, use
4939 * @p stable_sort() if this is needed.
4941 template<typename _RandomAccessIterator
>
4943 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4945 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4948 // concept requirements
4949 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4950 _RandomAccessIterator
>)
4951 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4952 __glibcxx_requires_valid_range(__first
, __last
);
4954 if (__first
!= __last
)
4956 std::__introsort_loop(__first
, __last
,
4957 std::__lg(__last
- __first
) * 2);
4958 std::__final_insertion_sort(__first
, __last
);
4963 * @brief Sort the elements of a sequence using a predicate for comparison.
4964 * @param first An iterator.
4965 * @param last Another iterator.
4966 * @param comp A comparison functor.
4969 * Sorts the elements in the range @p [first,last) in ascending order,
4970 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4971 * range @p [first,last-1).
4973 * The relative ordering of equivalent elements is not preserved, use
4974 * @p stable_sort() if this is needed.
4976 template<typename _RandomAccessIterator
, typename _Compare
>
4978 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4981 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4984 // concept requirements
4985 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4986 _RandomAccessIterator
>)
4987 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
4989 __glibcxx_requires_valid_range(__first
, __last
);
4991 if (__first
!= __last
)
4993 std::__introsort_loop(__first
, __last
,
4994 std::__lg(__last
- __first
) * 2, __comp
);
4995 std::__final_insertion_sort(__first
, __last
, __comp
);
5000 * @brief Merges two sorted ranges.
5001 * @param first1 An iterator.
5002 * @param first2 Another iterator.
5003 * @param last1 Another iterator.
5004 * @param last2 Another iterator.
5005 * @param result An iterator pointing to the end of the merged range.
5006 * @return An iterator pointing to the first element "not less
5009 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5010 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5011 * must be sorted, and the output range must not overlap with either of
5012 * the input ranges. The sort is @e stable, that is, for equivalent
5013 * elements in the two ranges, elements from the first range will always
5014 * come before elements from the second.
5016 template<typename _InputIterator1
, typename _InputIterator2
,
5017 typename _OutputIterator
>
5019 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5020 _InputIterator2 __first2
, _InputIterator2 __last2
,
5021 _OutputIterator __result
)
5023 typedef typename iterator_traits
<_InputIterator1
>::value_type
5025 typedef typename iterator_traits
<_InputIterator2
>::value_type
5028 // concept requirements
5029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5030 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5031 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5033 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5035 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5036 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5037 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5039 while (__first1
!= __last1
&& __first2
!= __last2
)
5041 if (*__first2
< *__first1
)
5043 *__result
= *__first2
;
5048 *__result
= *__first1
;
5053 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5058 * @brief Merges two sorted ranges.
5059 * @param first1 An iterator.
5060 * @param first2 Another iterator.
5061 * @param last1 Another iterator.
5062 * @param last2 Another iterator.
5063 * @param result An iterator pointing to the end of the merged range.
5064 * @param comp A functor to use for comparisons.
5065 * @return An iterator pointing to the first element "not less
5068 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5069 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5070 * must be sorted, and the output range must not overlap with either of
5071 * the input ranges. The sort is @e stable, that is, for equivalent
5072 * elements in the two ranges, elements from the first range will always
5073 * come before elements from the second.
5075 * The comparison function should have the same effects on ordering as
5076 * the function used for the initial sort.
5078 template<typename _InputIterator1
, typename _InputIterator2
,
5079 typename _OutputIterator
, typename _Compare
>
5081 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5082 _InputIterator2 __first2
, _InputIterator2 __last2
,
5083 _OutputIterator __result
, _Compare __comp
)
5085 typedef typename iterator_traits
<_InputIterator1
>::value_type
5087 typedef typename iterator_traits
<_InputIterator2
>::value_type
5090 // concept requirements
5091 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5092 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5093 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5095 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5097 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5098 _ValueType2
, _ValueType1
>)
5099 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5100 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5102 while (__first1
!= __last1
&& __first2
!= __last2
)
5104 if (__comp(*__first2
, *__first1
))
5106 *__result
= *__first2
;
5111 *__result
= *__first1
;
5116 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5122 * @brief Sort the elements of a sequence, preserving the relative order
5123 * of equivalent elements.
5124 * @param first An iterator.
5125 * @param last Another iterator.
5128 * Sorts the elements in the range @p [first,last) in ascending order,
5129 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5130 * @p [first,last-1).
5132 * The relative ordering of equivalent elements is preserved, so any two
5133 * elements @p x and @p y in the range @p [first,last) such that
5134 * @p x<y is false and @p y<x is false will have the same relative
5135 * ordering after calling @p stable_sort().
5137 template<typename _RandomAccessIterator
>
5139 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5141 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5143 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5146 // concept requirements
5147 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5148 _RandomAccessIterator
>)
5149 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5150 __glibcxx_requires_valid_range(__first
, __last
);
5152 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5154 if (__buf
.begin() == 0)
5155 std::__inplace_stable_sort(__first
, __last
);
5157 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5158 _DistanceType(__buf
.size()));
5162 * @brief Sort the elements of a sequence using a predicate for comparison,
5163 * preserving the relative order of equivalent elements.
5164 * @param first An iterator.
5165 * @param last Another iterator.
5166 * @param comp A comparison functor.
5169 * Sorts the elements in the range @p [first,last) in ascending order,
5170 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5171 * range @p [first,last-1).
5173 * The relative ordering of equivalent elements is preserved, so any two
5174 * elements @p x and @p y in the range @p [first,last) such that
5175 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5176 * relative ordering after calling @p stable_sort().
5178 template<typename _RandomAccessIterator
, typename _Compare
>
5180 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5183 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5185 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5188 // concept requirements
5189 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5190 _RandomAccessIterator
>)
5191 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5194 __glibcxx_requires_valid_range(__first
, __last
);
5196 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5198 if (__buf
.begin() == 0)
5199 std::__inplace_stable_sort(__first
, __last
, __comp
);
5201 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5202 _DistanceType(__buf
.size()), __comp
);
5207 * @brief Return the union of two sorted ranges.
5208 * @param first1 Start of first range.
5209 * @param last1 End of first range.
5210 * @param first2 Start of second range.
5211 * @param last2 End of second range.
5212 * @return End of the output range.
5213 * @ingroup setoperations
5215 * This operation iterates over both ranges, copying elements present in
5216 * each range in order to the output range. Iterators increment for each
5217 * range. When the current element of one range is less than the other,
5218 * that element is copied and the iterator advanced. If an element is
5219 * contained in both ranges, the element from the first range is copied and
5220 * both ranges advance. The output range may not overlap either input
5223 template<typename _InputIterator1
, typename _InputIterator2
,
5224 typename _OutputIterator
>
5226 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5227 _InputIterator2 __first2
, _InputIterator2 __last2
,
5228 _OutputIterator __result
)
5230 typedef typename iterator_traits
<_InputIterator1
>::value_type
5232 typedef typename iterator_traits
<_InputIterator2
>::value_type
5235 // concept requirements
5236 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5237 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5238 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5240 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5242 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5243 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5244 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5245 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5247 while (__first1
!= __last1
&& __first2
!= __last2
)
5249 if (*__first1
< *__first2
)
5251 *__result
= *__first1
;
5254 else if (*__first2
< *__first1
)
5256 *__result
= *__first2
;
5261 *__result
= *__first1
;
5267 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5272 * @brief Return the union of two sorted ranges using a comparison functor.
5273 * @param first1 Start of first range.
5274 * @param last1 End of first range.
5275 * @param first2 Start of second range.
5276 * @param last2 End of second range.
5277 * @param comp The comparison functor.
5278 * @return End of the output range.
5279 * @ingroup setoperations
5281 * This operation iterates over both ranges, copying elements present in
5282 * each range in order to the output range. Iterators increment for each
5283 * range. When the current element of one range is less than the other
5284 * according to @a comp, that element is copied and the iterator advanced.
5285 * If an equivalent element according to @a comp is contained in both
5286 * ranges, the element from the first range is copied and both ranges
5287 * advance. The output range may not overlap either input range.
5289 template<typename _InputIterator1
, typename _InputIterator2
,
5290 typename _OutputIterator
, typename _Compare
>
5292 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5293 _InputIterator2 __first2
, _InputIterator2 __last2
,
5294 _OutputIterator __result
, _Compare __comp
)
5296 typedef typename iterator_traits
<_InputIterator1
>::value_type
5298 typedef typename iterator_traits
<_InputIterator2
>::value_type
5301 // concept requirements
5302 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5303 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5304 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5306 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5308 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5309 _ValueType1
, _ValueType2
>)
5310 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5311 _ValueType2
, _ValueType1
>)
5312 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5313 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5315 while (__first1
!= __last1
&& __first2
!= __last2
)
5317 if (__comp(*__first1
, *__first2
))
5319 *__result
= *__first1
;
5322 else if (__comp(*__first2
, *__first1
))
5324 *__result
= *__first2
;
5329 *__result
= *__first1
;
5335 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5340 * @brief Return the intersection of two sorted ranges.
5341 * @param first1 Start of first range.
5342 * @param last1 End of first range.
5343 * @param first2 Start of second range.
5344 * @param last2 End of second range.
5345 * @return End of the output range.
5346 * @ingroup setoperations
5348 * This operation iterates over both ranges, copying elements present in
5349 * both ranges in order to the output range. Iterators increment for each
5350 * range. When the current element of one range is less than the other,
5351 * that iterator advances. If an element is contained in both ranges, the
5352 * element from the first range is copied and both ranges advance. The
5353 * output range may not overlap either input range.
5355 template<typename _InputIterator1
, typename _InputIterator2
,
5356 typename _OutputIterator
>
5358 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5359 _InputIterator2 __first2
, _InputIterator2 __last2
,
5360 _OutputIterator __result
)
5362 typedef typename iterator_traits
<_InputIterator1
>::value_type
5364 typedef typename iterator_traits
<_InputIterator2
>::value_type
5367 // concept requirements
5368 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5369 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5370 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5372 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5373 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5374 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5375 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5377 while (__first1
!= __last1
&& __first2
!= __last2
)
5378 if (*__first1
< *__first2
)
5380 else if (*__first2
< *__first1
)
5384 *__result
= *__first1
;
5393 * @brief Return the intersection of two sorted ranges using comparison
5395 * @param first1 Start of first range.
5396 * @param last1 End of first range.
5397 * @param first2 Start of second range.
5398 * @param last2 End of second range.
5399 * @param comp The comparison functor.
5400 * @return End of the output range.
5401 * @ingroup setoperations
5403 * This operation iterates over both ranges, copying elements present in
5404 * both ranges in order to the output range. Iterators increment for each
5405 * range. When the current element of one range is less than the other
5406 * according to @a comp, that iterator advances. If an element is
5407 * contained in both ranges according to @a comp, the element from the
5408 * first range is copied and both ranges advance. The output range may not
5409 * overlap either input range.
5411 template<typename _InputIterator1
, typename _InputIterator2
,
5412 typename _OutputIterator
, typename _Compare
>
5414 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5415 _InputIterator2 __first2
, _InputIterator2 __last2
,
5416 _OutputIterator __result
, _Compare __comp
)
5418 typedef typename iterator_traits
<_InputIterator1
>::value_type
5420 typedef typename iterator_traits
<_InputIterator2
>::value_type
5423 // concept requirements
5424 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5425 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5426 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5428 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5429 _ValueType1
, _ValueType2
>)
5430 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5431 _ValueType2
, _ValueType1
>)
5432 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5433 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5435 while (__first1
!= __last1
&& __first2
!= __last2
)
5436 if (__comp(*__first1
, *__first2
))
5438 else if (__comp(*__first2
, *__first1
))
5442 *__result
= *__first1
;
5451 * @brief Return the difference of two sorted ranges.
5452 * @param first1 Start of first range.
5453 * @param last1 End of first range.
5454 * @param first2 Start of second range.
5455 * @param last2 End of second range.
5456 * @return End of the output range.
5457 * @ingroup setoperations
5459 * This operation iterates over both ranges, copying elements present in
5460 * the first range but not the second in order to the output range.
5461 * Iterators increment for each range. When the current element of the
5462 * first range is less than the second, that element is copied and the
5463 * iterator advances. If the current element of the second range is less,
5464 * the iterator advances, but no element is copied. If an element is
5465 * contained in both ranges, no elements are copied and both ranges
5466 * advance. The output range may not overlap either input range.
5468 template<typename _InputIterator1
, typename _InputIterator2
,
5469 typename _OutputIterator
>
5471 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5472 _InputIterator2 __first2
, _InputIterator2 __last2
,
5473 _OutputIterator __result
)
5475 typedef typename iterator_traits
<_InputIterator1
>::value_type
5477 typedef typename iterator_traits
<_InputIterator2
>::value_type
5480 // concept requirements
5481 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5482 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5483 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5485 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5486 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5487 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5488 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5490 while (__first1
!= __last1
&& __first2
!= __last2
)
5491 if (*__first1
< *__first2
)
5493 *__result
= *__first1
;
5497 else if (*__first2
< *__first1
)
5504 return std::copy(__first1
, __last1
, __result
);
5508 * @brief Return the difference of two sorted ranges using comparison
5510 * @param first1 Start of first range.
5511 * @param last1 End of first range.
5512 * @param first2 Start of second range.
5513 * @param last2 End of second range.
5514 * @param comp The comparison functor.
5515 * @return End of the output range.
5516 * @ingroup setoperations
5518 * This operation iterates over both ranges, copying elements present in
5519 * the first range but not the second in order to the output range.
5520 * Iterators increment for each range. When the current element of the
5521 * first range is less than the second according to @a comp, that element
5522 * is copied and the iterator advances. If the current element of the
5523 * second range is less, no element is copied and the iterator advances.
5524 * If an element is contained in both ranges according to @a comp, no
5525 * elements are copied and both ranges advance. The output range may not
5526 * overlap either input range.
5528 template<typename _InputIterator1
, typename _InputIterator2
,
5529 typename _OutputIterator
, typename _Compare
>
5531 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5532 _InputIterator2 __first2
, _InputIterator2 __last2
,
5533 _OutputIterator __result
, _Compare __comp
)
5535 typedef typename iterator_traits
<_InputIterator1
>::value_type
5537 typedef typename iterator_traits
<_InputIterator2
>::value_type
5540 // concept requirements
5541 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5542 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5543 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5545 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5546 _ValueType1
, _ValueType2
>)
5547 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5548 _ValueType2
, _ValueType1
>)
5549 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5550 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5552 while (__first1
!= __last1
&& __first2
!= __last2
)
5553 if (__comp(*__first1
, *__first2
))
5555 *__result
= *__first1
;
5559 else if (__comp(*__first2
, *__first1
))
5566 return std::copy(__first1
, __last1
, __result
);
5570 * @brief Return the symmetric difference of two sorted ranges.
5571 * @param first1 Start of first range.
5572 * @param last1 End of first range.
5573 * @param first2 Start of second range.
5574 * @param last2 End of second range.
5575 * @return End of the output range.
5576 * @ingroup setoperations
5578 * This operation iterates over both ranges, copying elements present in
5579 * one range but not the other in order to the output range. Iterators
5580 * increment for each range. When the current element of one range is less
5581 * than the other, that element is copied and the iterator advances. If an
5582 * element is contained in both ranges, no elements are copied and both
5583 * ranges advance. The output range may not overlap either input range.
5585 template<typename _InputIterator1
, typename _InputIterator2
,
5586 typename _OutputIterator
>
5588 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5589 _InputIterator2 __first2
, _InputIterator2 __last2
,
5590 _OutputIterator __result
)
5592 typedef typename iterator_traits
<_InputIterator1
>::value_type
5594 typedef typename iterator_traits
<_InputIterator2
>::value_type
5597 // concept requirements
5598 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5599 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5600 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5602 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5604 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5605 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5606 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5607 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5609 while (__first1
!= __last1
&& __first2
!= __last2
)
5610 if (*__first1
< *__first2
)
5612 *__result
= *__first1
;
5616 else if (*__first2
< *__first1
)
5618 *__result
= *__first2
;
5627 return std::copy(__first2
, __last2
, std::copy(__first1
,
5628 __last1
, __result
));
5632 * @brief Return the symmetric difference of two sorted ranges using
5633 * comparison functor.
5634 * @param first1 Start of first range.
5635 * @param last1 End of first range.
5636 * @param first2 Start of second range.
5637 * @param last2 End of second range.
5638 * @param comp The comparison functor.
5639 * @return End of the output range.
5640 * @ingroup setoperations
5642 * This operation iterates over both ranges, copying elements present in
5643 * one range but not the other in order to the output range. Iterators
5644 * increment for each range. When the current element of one range is less
5645 * than the other according to @a comp, that element is copied and the
5646 * iterator advances. If an element is contained in both ranges according
5647 * to @a comp, no elements are copied and both ranges advance. The output
5648 * range may not overlap either input range.
5650 template<typename _InputIterator1
, typename _InputIterator2
,
5651 typename _OutputIterator
, typename _Compare
>
5653 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5654 _InputIterator2 __first2
, _InputIterator2 __last2
,
5655 _OutputIterator __result
,
5658 typedef typename iterator_traits
<_InputIterator1
>::value_type
5660 typedef typename iterator_traits
<_InputIterator2
>::value_type
5663 // concept requirements
5664 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5665 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5666 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5668 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5670 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5671 _ValueType1
, _ValueType2
>)
5672 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5673 _ValueType2
, _ValueType1
>)
5674 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5675 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5677 while (__first1
!= __last1
&& __first2
!= __last2
)
5678 if (__comp(*__first1
, *__first2
))
5680 *__result
= *__first1
;
5684 else if (__comp(*__first2
, *__first1
))
5686 *__result
= *__first2
;
5695 return std::copy(__first2
, __last2
,
5696 std::copy(__first1
, __last1
, __result
));
5701 * @brief Return the minimum element in a range.
5702 * @param first Start of range.
5703 * @param last End of range.
5704 * @return Iterator referencing the first instance of the smallest value.
5706 template<typename _ForwardIterator
>
5708 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5710 // concept requirements
5711 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5712 __glibcxx_function_requires(_LessThanComparableConcept
<
5713 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5714 __glibcxx_requires_valid_range(__first
, __last
);
5716 if (__first
== __last
)
5718 _ForwardIterator __result
= __first
;
5719 while (++__first
!= __last
)
5720 if (*__first
< *__result
)
5726 * @brief Return the minimum element in a range using comparison functor.
5727 * @param first Start of range.
5728 * @param last End of range.
5729 * @param comp Comparison functor.
5730 * @return Iterator referencing the first instance of the smallest value
5731 * according to comp.
5733 template<typename _ForwardIterator
, typename _Compare
>
5735 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5738 // concept requirements
5739 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5740 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5741 typename iterator_traits
<_ForwardIterator
>::value_type
,
5742 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5743 __glibcxx_requires_valid_range(__first
, __last
);
5745 if (__first
== __last
)
5747 _ForwardIterator __result
= __first
;
5748 while (++__first
!= __last
)
5749 if (__comp(*__first
, *__result
))
5755 * @brief Return the maximum element in a range.
5756 * @param first Start of range.
5757 * @param last End of range.
5758 * @return Iterator referencing the first instance of the largest value.
5760 template<typename _ForwardIterator
>
5762 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5764 // concept requirements
5765 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5766 __glibcxx_function_requires(_LessThanComparableConcept
<
5767 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5768 __glibcxx_requires_valid_range(__first
, __last
);
5770 if (__first
== __last
)
5772 _ForwardIterator __result
= __first
;
5773 while (++__first
!= __last
)
5774 if (*__result
< *__first
)
5780 * @brief Return the maximum element in a range using comparison functor.
5781 * @param first Start of range.
5782 * @param last End of range.
5783 * @param comp Comparison functor.
5784 * @return Iterator referencing the first instance of the largest value
5785 * according to comp.
5787 template<typename _ForwardIterator
, typename _Compare
>
5789 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5792 // concept requirements
5793 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5794 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5795 typename iterator_traits
<_ForwardIterator
>::value_type
,
5796 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5797 __glibcxx_requires_valid_range(__first
, __last
);
5799 if (__first
== __last
) return __first
;
5800 _ForwardIterator __result
= __first
;
5801 while (++__first
!= __last
)
5802 if (__comp(*__result
, *__first
))
5807 _GLIBCXX_END_NESTED_NAMESPACE
5809 #endif /* _STL_ALGO_H */