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
))
265 // set_symmetric_difference
277 * This is an uglified
278 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
279 * overloaded for forward iterators.
281 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
283 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
284 _Integer __count
, const _Tp
& __val
,
285 std::forward_iterator_tag
)
287 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
288 while (__first
!= __last
)
290 typename iterator_traits
<_ForwardIterator
>::difference_type
292 _ForwardIterator __i
= __first
;
294 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
303 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
309 * This is an uglified
310 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
311 * overloaded for random access iterators.
313 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
315 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
316 _Integer __count
, const _Tp
& __val
,
317 std::random_access_iterator_tag
)
320 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
323 _DistanceType __tailSize
= __last
- __first
;
324 const _DistanceType __pattSize
= __count
;
326 if (__tailSize
< __pattSize
)
329 const _DistanceType __skipOffset
= __pattSize
- 1;
330 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
331 __tailSize
-= __pattSize
;
333 while (1) // the main loop...
335 // __lookAhead here is always pointing to the last element of next
337 while (!(*__lookAhead
== __val
)) // the skip loop...
339 if (__tailSize
< __pattSize
)
340 return __last
; // Failure
341 __lookAhead
+= __pattSize
;
342 __tailSize
-= __pattSize
;
344 _DistanceType __remainder
= __skipOffset
;
345 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
346 *__backTrack
== __val
; --__backTrack
)
348 if (--__remainder
== 0)
349 return (__lookAhead
- __skipOffset
); // Success
351 if (__remainder
> __tailSize
)
352 return __last
; // Failure
353 __lookAhead
+= __remainder
;
354 __tailSize
-= __remainder
;
361 * This is an uglified
362 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
364 * overloaded for forward iterators.
366 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
367 typename _BinaryPredicate
>
369 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
370 _Integer __count
, const _Tp
& __val
,
371 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
373 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
376 while (__first
!= __last
)
378 typename iterator_traits
<_ForwardIterator
>::difference_type
380 _ForwardIterator __i
= __first
;
382 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
392 while (__first
!= __last
393 && !bool(__binary_pred(*__first
, __val
)))
400 * This is an uglified
401 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
403 * overloaded for random access iterators.
405 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
406 typename _BinaryPredicate
>
408 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
409 _Integer __count
, const _Tp
& __val
,
410 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
413 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
416 _DistanceType __tailSize
= __last
- __first
;
417 const _DistanceType __pattSize
= __count
;
419 if (__tailSize
< __pattSize
)
422 const _DistanceType __skipOffset
= __pattSize
- 1;
423 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
424 __tailSize
-= __pattSize
;
426 while (1) // the main loop...
428 // __lookAhead here is always pointing to the last element of next
430 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
432 if (__tailSize
< __pattSize
)
433 return __last
; // Failure
434 __lookAhead
+= __pattSize
;
435 __tailSize
-= __pattSize
;
437 _DistanceType __remainder
= __skipOffset
;
438 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
439 __binary_pred(*__backTrack
, __val
); --__backTrack
)
441 if (--__remainder
== 0)
442 return (__lookAhead
- __skipOffset
); // Success
444 if (__remainder
> __tailSize
)
445 return __last
; // Failure
446 __lookAhead
+= __remainder
;
447 __tailSize
-= __remainder
;
451 // find_end for forward iterators.
452 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
454 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
455 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
456 forward_iterator_tag
, forward_iterator_tag
)
458 if (__first2
== __last2
)
462 _ForwardIterator1 __result
= __last1
;
465 _ForwardIterator1 __new_result
466 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
467 if (__new_result
== __last1
)
471 __result
= __new_result
;
472 __first1
= __new_result
;
479 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
480 typename _BinaryPredicate
>
482 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
483 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
484 forward_iterator_tag
, forward_iterator_tag
,
485 _BinaryPredicate __comp
)
487 if (__first2
== __last2
)
491 _ForwardIterator1 __result
= __last1
;
494 _ForwardIterator1 __new_result
495 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
497 if (__new_result
== __last1
)
501 __result
= __new_result
;
502 __first1
= __new_result
;
509 // find_end for bidirectional iterators (much faster).
510 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
511 _BidirectionalIterator1
512 __find_end(_BidirectionalIterator1 __first1
,
513 _BidirectionalIterator1 __last1
,
514 _BidirectionalIterator2 __first2
,
515 _BidirectionalIterator2 __last2
,
516 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
518 // concept requirements
519 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
520 _BidirectionalIterator1
>)
521 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
522 _BidirectionalIterator2
>)
524 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
525 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
527 _RevIterator1
__rlast1(__first1
);
528 _RevIterator2
__rlast2(__first2
);
529 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
531 _RevIterator2(__last2
),
534 if (__rresult
== __rlast1
)
538 _BidirectionalIterator1 __result
= __rresult
.base();
539 std::advance(__result
, -std::distance(__first2
, __last2
));
544 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
545 typename _BinaryPredicate
>
546 _BidirectionalIterator1
547 __find_end(_BidirectionalIterator1 __first1
,
548 _BidirectionalIterator1 __last1
,
549 _BidirectionalIterator2 __first2
,
550 _BidirectionalIterator2 __last2
,
551 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
552 _BinaryPredicate __comp
)
554 // concept requirements
555 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
556 _BidirectionalIterator1
>)
557 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
558 _BidirectionalIterator2
>)
560 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
561 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
563 _RevIterator1
__rlast1(__first1
);
564 _RevIterator2
__rlast2(__first2
);
565 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
566 _RevIterator2(__last2
), __rlast2
,
569 if (__rresult
== __rlast1
)
573 _BidirectionalIterator1 __result
= __rresult
.base();
574 std::advance(__result
, -std::distance(__first2
, __last2
));
580 * @brief Find last matching subsequence in a sequence.
581 * @param first1 Start of range to search.
582 * @param last1 End of range to search.
583 * @param first2 Start of sequence to match.
584 * @param last2 End of sequence to match.
585 * @return The last iterator @c i in the range
586 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
587 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
588 * such iterator exists.
590 * Searches the range @p [first1,last1) for a sub-sequence that compares
591 * equal value-by-value with the sequence given by @p [first2,last2) and
592 * returns an iterator to the first element of the sub-sequence, or
593 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
594 * last such subsequence contained in [first,last1).
596 * Because the sub-sequence must lie completely within the range
597 * @p [first1,last1) it must start at a position less than
598 * @p last1-(last2-first2) where @p last2-first2 is the length of the
600 * This means that the returned iterator @c i will be in the range
601 * @p [first1,last1-(last2-first2))
603 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
604 inline _ForwardIterator1
605 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
606 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
608 // concept requirements
609 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
610 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
611 __glibcxx_function_requires(_EqualOpConcept
<
612 typename iterator_traits
<_ForwardIterator1
>::value_type
,
613 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
614 __glibcxx_requires_valid_range(__first1
, __last1
);
615 __glibcxx_requires_valid_range(__first2
, __last2
);
617 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
618 std::__iterator_category(__first1
),
619 std::__iterator_category(__first2
));
623 * @brief Find last matching subsequence in a sequence using a predicate.
624 * @param first1 Start of range to search.
625 * @param last1 End of range to search.
626 * @param first2 Start of sequence to match.
627 * @param last2 End of sequence to match.
628 * @param comp The predicate to use.
629 * @return The last iterator @c i in the range
630 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
631 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
632 * @p last1 if no such iterator exists.
634 * Searches the range @p [first1,last1) for a sub-sequence that compares
635 * equal value-by-value with the sequence given by @p [first2,last2) using
636 * comp as a predicate and returns an iterator to the first element of the
637 * sub-sequence, or @p last1 if the sub-sequence is not found. The
638 * sub-sequence will be the last such subsequence contained in
641 * Because the sub-sequence must lie completely within the range
642 * @p [first1,last1) it must start at a position less than
643 * @p last1-(last2-first2) where @p last2-first2 is the length of the
645 * This means that the returned iterator @c i will be in the range
646 * @p [first1,last1-(last2-first2))
648 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
649 typename _BinaryPredicate
>
650 inline _ForwardIterator1
651 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
652 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
653 _BinaryPredicate __comp
)
655 // concept requirements
656 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
657 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
658 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
659 typename iterator_traits
<_ForwardIterator1
>::value_type
,
660 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
661 __glibcxx_requires_valid_range(__first1
, __last1
);
662 __glibcxx_requires_valid_range(__first2
, __last2
);
664 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
665 std::__iterator_category(__first1
),
666 std::__iterator_category(__first2
),
672 * @brief Copy a sequence, removing elements of a given value.
673 * @param first An input iterator.
674 * @param last An input iterator.
675 * @param result An output iterator.
676 * @param value The value to be removed.
677 * @return An iterator designating the end of the resulting sequence.
679 * Copies each element in the range @p [first,last) not equal to @p value
680 * to the range beginning at @p result.
681 * remove_copy() is stable, so the relative order of elements that are
682 * copied is unchanged.
684 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
686 remove_copy(_InputIterator __first
, _InputIterator __last
,
687 _OutputIterator __result
, const _Tp
& __value
)
689 // concept requirements
690 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
691 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
692 typename iterator_traits
<_InputIterator
>::value_type
>)
693 __glibcxx_function_requires(_EqualOpConcept
<
694 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
695 __glibcxx_requires_valid_range(__first
, __last
);
697 for (; __first
!= __last
; ++__first
)
698 if (!(*__first
== __value
))
700 *__result
= *__first
;
707 * @brief Copy a sequence, removing elements for which a predicate is true.
708 * @param first An input iterator.
709 * @param last An input iterator.
710 * @param result An output iterator.
711 * @param pred A predicate.
712 * @return An iterator designating the end of the resulting sequence.
714 * Copies each element in the range @p [first,last) for which
715 * @p pred returns true to the range beginning at @p result.
717 * remove_copy_if() is stable, so the relative order of elements that are
718 * copied is unchanged.
720 template<typename _InputIterator
, typename _OutputIterator
,
723 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
724 _OutputIterator __result
, _Predicate __pred
)
726 // concept requirements
727 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
728 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
729 typename iterator_traits
<_InputIterator
>::value_type
>)
730 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
731 typename iterator_traits
<_InputIterator
>::value_type
>)
732 __glibcxx_requires_valid_range(__first
, __last
);
734 for (; __first
!= __last
; ++__first
)
735 if (!bool(__pred(*__first
)))
737 *__result
= *__first
;
744 * @brief Remove elements from a sequence.
745 * @param first An input iterator.
746 * @param last An input iterator.
747 * @param value The value to be removed.
748 * @return An iterator designating the end of the resulting sequence.
750 * All elements equal to @p value are removed from the range
753 * remove() is stable, so the relative order of elements that are
754 * not removed is unchanged.
756 * Elements between the end of the resulting sequence and @p last
757 * are still present, but their value is unspecified.
759 template<typename _ForwardIterator
, typename _Tp
>
761 remove(_ForwardIterator __first
, _ForwardIterator __last
,
764 // concept requirements
765 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
767 __glibcxx_function_requires(_EqualOpConcept
<
768 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
769 __glibcxx_requires_valid_range(__first
, __last
);
771 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
772 if(__first
== __last
)
774 _ForwardIterator __result
= __first
;
776 for(; __first
!= __last
; ++__first
)
777 if(!(*__first
== __value
))
779 *__result
= _GLIBCXX_MOVE(*__first
);
786 * @brief Remove elements from a sequence using a predicate.
787 * @param first A forward iterator.
788 * @param last A forward iterator.
789 * @param pred A predicate.
790 * @return An iterator designating the end of the resulting sequence.
792 * All elements for which @p pred returns true are removed from the range
795 * remove_if() is stable, so the relative order of elements that are
796 * not removed is unchanged.
798 * Elements between the end of the resulting sequence and @p last
799 * are still present, but their value is unspecified.
801 template<typename _ForwardIterator
, typename _Predicate
>
803 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
806 // concept requirements
807 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
809 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
810 typename iterator_traits
<_ForwardIterator
>::value_type
>)
811 __glibcxx_requires_valid_range(__first
, __last
);
813 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
814 if(__first
== __last
)
816 _ForwardIterator __result
= __first
;
818 for(; __first
!= __last
; ++__first
)
819 if(!__pred(*__first
))
821 *__result
= _GLIBCXX_MOVE(*__first
);
828 * @brief Remove consecutive duplicate values from a sequence.
829 * @param first A forward iterator.
830 * @param last A forward iterator.
831 * @return An iterator designating the end of the resulting sequence.
833 * Removes all but the first element from each group of consecutive
834 * values that compare equal.
835 * unique() is stable, so the relative order of elements that are
836 * not removed is unchanged.
837 * Elements between the end of the resulting sequence and @p last
838 * are still present, but their value is unspecified.
840 template<typename _ForwardIterator
>
842 unique(_ForwardIterator __first
, _ForwardIterator __last
)
844 // concept requirements
845 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
847 __glibcxx_function_requires(_EqualityComparableConcept
<
848 typename iterator_traits
<_ForwardIterator
>::value_type
>)
849 __glibcxx_requires_valid_range(__first
, __last
);
851 // Skip the beginning, if already unique.
852 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
853 if (__first
== __last
)
856 // Do the real copy work.
857 _ForwardIterator __dest
= __first
;
859 while (++__first
!= __last
)
860 if (!(*__dest
== *__first
))
861 *++__dest
= _GLIBCXX_MOVE(*__first
);
866 * @brief Remove consecutive values from a sequence using a predicate.
867 * @param first A forward iterator.
868 * @param last A forward iterator.
869 * @param binary_pred A binary predicate.
870 * @return An iterator designating the end of the resulting sequence.
872 * Removes all but the first element from each group of consecutive
873 * values for which @p binary_pred returns true.
874 * unique() is stable, so the relative order of elements that are
875 * not removed is unchanged.
876 * Elements between the end of the resulting sequence and @p last
877 * are still present, but their value is unspecified.
879 template<typename _ForwardIterator
, typename _BinaryPredicate
>
881 unique(_ForwardIterator __first
, _ForwardIterator __last
,
882 _BinaryPredicate __binary_pred
)
884 // concept requirements
885 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
887 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
888 typename iterator_traits
<_ForwardIterator
>::value_type
,
889 typename iterator_traits
<_ForwardIterator
>::value_type
>)
890 __glibcxx_requires_valid_range(__first
, __last
);
892 // Skip the beginning, if already unique.
893 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
894 if (__first
== __last
)
897 // Do the real copy work.
898 _ForwardIterator __dest
= __first
;
900 while (++__first
!= __last
)
901 if (!bool(__binary_pred(*__dest
, *__first
)))
902 *++__dest
= _GLIBCXX_MOVE(*__first
);
907 * This is an uglified unique_copy(_InputIterator, _InputIterator,
909 * overloaded for forward iterators and output iterator as result.
911 template<typename _ForwardIterator
, typename _OutputIterator
>
913 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
914 _OutputIterator __result
,
915 forward_iterator_tag
, output_iterator_tag
)
917 // concept requirements -- taken care of in dispatching function
918 _ForwardIterator __next
= __first
;
919 *__result
= *__first
;
920 while (++__next
!= __last
)
921 if (!(*__first
== *__next
))
924 *++__result
= *__first
;
930 * This is an uglified unique_copy(_InputIterator, _InputIterator,
932 * overloaded for input iterators and output iterator as result.
934 template<typename _InputIterator
, typename _OutputIterator
>
936 __unique_copy(_InputIterator __first
, _InputIterator __last
,
937 _OutputIterator __result
,
938 input_iterator_tag
, output_iterator_tag
)
940 // concept requirements -- taken care of in dispatching function
941 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
943 while (++__first
!= __last
)
944 if (!(__value
== *__first
))
947 *++__result
= __value
;
953 * This is an uglified unique_copy(_InputIterator, _InputIterator,
955 * overloaded for input iterators and forward iterator as result.
957 template<typename _InputIterator
, typename _ForwardIterator
>
959 __unique_copy(_InputIterator __first
, _InputIterator __last
,
960 _ForwardIterator __result
,
961 input_iterator_tag
, forward_iterator_tag
)
963 // concept requirements -- taken care of in dispatching function
964 *__result
= *__first
;
965 while (++__first
!= __last
)
966 if (!(*__result
== *__first
))
967 *++__result
= *__first
;
972 * This is an uglified
973 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
975 * overloaded for forward iterators and output iterator as result.
977 template<typename _ForwardIterator
, typename _OutputIterator
,
978 typename _BinaryPredicate
>
980 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
981 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
982 forward_iterator_tag
, output_iterator_tag
)
984 // concept requirements -- iterators already checked
985 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
986 typename iterator_traits
<_ForwardIterator
>::value_type
,
987 typename iterator_traits
<_ForwardIterator
>::value_type
>)
989 _ForwardIterator __next
= __first
;
990 *__result
= *__first
;
991 while (++__next
!= __last
)
992 if (!bool(__binary_pred(*__first
, *__next
)))
995 *++__result
= *__first
;
1001 * This is an uglified
1002 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1004 * overloaded for input iterators and output iterator as result.
1006 template<typename _InputIterator
, typename _OutputIterator
,
1007 typename _BinaryPredicate
>
1009 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1010 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1011 input_iterator_tag
, output_iterator_tag
)
1013 // concept requirements -- iterators already checked
1014 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1015 typename iterator_traits
<_InputIterator
>::value_type
,
1016 typename iterator_traits
<_InputIterator
>::value_type
>)
1018 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1019 *__result
= __value
;
1020 while (++__first
!= __last
)
1021 if (!bool(__binary_pred(__value
, *__first
)))
1024 *++__result
= __value
;
1030 * This is an uglified
1031 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1033 * overloaded for input iterators and forward iterator as result.
1035 template<typename _InputIterator
, typename _ForwardIterator
,
1036 typename _BinaryPredicate
>
1038 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1039 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1040 input_iterator_tag
, forward_iterator_tag
)
1042 // concept requirements -- iterators already checked
1043 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1044 typename iterator_traits
<_ForwardIterator
>::value_type
,
1045 typename iterator_traits
<_InputIterator
>::value_type
>)
1047 *__result
= *__first
;
1048 while (++__first
!= __last
)
1049 if (!bool(__binary_pred(*__result
, *__first
)))
1050 *++__result
= *__first
;
1055 * This is an uglified reverse(_BidirectionalIterator,
1056 * _BidirectionalIterator)
1057 * overloaded for bidirectional iterators.
1059 template<typename _BidirectionalIterator
>
1061 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1062 bidirectional_iterator_tag
)
1065 if (__first
== __last
|| __first
== --__last
)
1069 std::iter_swap(__first
, __last
);
1075 * This is an uglified reverse(_BidirectionalIterator,
1076 * _BidirectionalIterator)
1077 * overloaded for random access iterators.
1079 template<typename _RandomAccessIterator
>
1081 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1082 random_access_iterator_tag
)
1084 if (__first
== __last
)
1087 while (__first
< __last
)
1089 std::iter_swap(__first
, __last
);
1096 * @brief Reverse a sequence.
1097 * @param first A bidirectional iterator.
1098 * @param last A bidirectional iterator.
1099 * @return reverse() returns no value.
1101 * Reverses the order of the elements in the range @p [first,last),
1102 * so that the first element becomes the last etc.
1103 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1104 * swaps @p *(first+i) and @p *(last-(i+1))
1106 template<typename _BidirectionalIterator
>
1108 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1110 // concept requirements
1111 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1112 _BidirectionalIterator
>)
1113 __glibcxx_requires_valid_range(__first
, __last
);
1114 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1118 * @brief Copy a sequence, reversing its elements.
1119 * @param first A bidirectional iterator.
1120 * @param last A bidirectional iterator.
1121 * @param result An output iterator.
1122 * @return An iterator designating the end of the resulting sequence.
1124 * Copies the elements in the range @p [first,last) to the range
1125 * @p [result,result+(last-first)) such that the order of the
1126 * elements is reversed.
1127 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1128 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1129 * The ranges @p [first,last) and @p [result,result+(last-first))
1132 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1134 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1135 _OutputIterator __result
)
1137 // concept requirements
1138 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1139 _BidirectionalIterator
>)
1140 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1141 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1142 __glibcxx_requires_valid_range(__first
, __last
);
1144 while (__first
!= __last
)
1147 *__result
= *__last
;
1154 * This is a helper function for the rotate algorithm specialized on RAIs.
1155 * It returns the greatest common divisor of two integer values.
1157 template<typename _EuclideanRingElement
>
1158 _EuclideanRingElement
1159 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1163 _EuclideanRingElement __t
= __m
% __n
;
1170 /// This is a helper function for the rotate algorithm.
1171 template<typename _ForwardIterator
>
1173 __rotate(_ForwardIterator __first
,
1174 _ForwardIterator __middle
,
1175 _ForwardIterator __last
,
1176 forward_iterator_tag
)
1178 if (__first
== __middle
|| __last
== __middle
)
1181 _ForwardIterator __first2
= __middle
;
1184 std::iter_swap(__first
, __first2
);
1187 if (__first
== __middle
)
1188 __middle
= __first2
;
1190 while (__first2
!= __last
);
1192 __first2
= __middle
;
1194 while (__first2
!= __last
)
1196 std::iter_swap(__first
, __first2
);
1199 if (__first
== __middle
)
1200 __middle
= __first2
;
1201 else if (__first2
== __last
)
1202 __first2
= __middle
;
1206 /// This is a helper function for the rotate algorithm.
1207 template<typename _BidirectionalIterator
>
1209 __rotate(_BidirectionalIterator __first
,
1210 _BidirectionalIterator __middle
,
1211 _BidirectionalIterator __last
,
1212 bidirectional_iterator_tag
)
1214 // concept requirements
1215 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1216 _BidirectionalIterator
>)
1218 if (__first
== __middle
|| __last
== __middle
)
1221 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1222 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1224 while (__first
!= __middle
&& __middle
!= __last
)
1226 std::iter_swap(__first
, --__last
);
1230 if (__first
== __middle
)
1231 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1233 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1236 /// This is a helper function for the rotate algorithm.
1237 template<typename _RandomAccessIterator
>
1239 __rotate(_RandomAccessIterator __first
,
1240 _RandomAccessIterator __middle
,
1241 _RandomAccessIterator __last
,
1242 random_access_iterator_tag
)
1244 // concept requirements
1245 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1246 _RandomAccessIterator
>)
1248 if (__first
== __middle
|| __last
== __middle
)
1251 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1253 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1256 const _Distance __n
= __last
- __first
;
1257 const _Distance __k
= __middle
- __first
;
1258 const _Distance __l
= __n
- __k
;
1262 std::swap_ranges(__first
, __middle
, __middle
);
1266 const _Distance __d
= std::__gcd(__n
, __k
);
1268 for (_Distance __i
= 0; __i
< __d
; __i
++)
1270 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1271 _RandomAccessIterator __p
= __first
;
1275 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1277 if (__p
> __first
+ __l
)
1279 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1283 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1289 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1291 if (__p
< __last
- __k
)
1293 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1296 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1301 *__p
= _GLIBCXX_MOVE(__tmp
);
1307 * @brief Rotate the elements of a sequence.
1308 * @param first A forward iterator.
1309 * @param middle A forward iterator.
1310 * @param last A forward iterator.
1313 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1314 * positions so that the element at @p middle is moved to @p first, the
1315 * element at @p middle+1 is moved to @first+1 and so on for each element
1316 * in the range @p [first,last).
1318 * This effectively swaps the ranges @p [first,middle) and
1321 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1322 * each @p n in the range @p [0,last-first).
1324 template<typename _ForwardIterator
>
1326 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1327 _ForwardIterator __last
)
1329 // concept requirements
1330 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1332 __glibcxx_requires_valid_range(__first
, __middle
);
1333 __glibcxx_requires_valid_range(__middle
, __last
);
1335 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1337 std::__rotate(__first
, __middle
, __last
, _IterType());
1341 * @brief Copy a sequence, rotating its elements.
1342 * @param first A forward iterator.
1343 * @param middle A forward iterator.
1344 * @param last A forward iterator.
1345 * @param result An output iterator.
1346 * @return An iterator designating the end of the resulting sequence.
1348 * Copies the elements of the range @p [first,last) to the range
1349 * beginning at @result, rotating the copied elements by @p (middle-first)
1350 * positions so that the element at @p middle is moved to @p result, the
1351 * element at @p middle+1 is moved to @result+1 and so on for each element
1352 * in the range @p [first,last).
1354 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1355 * each @p n in the range @p [0,last-first).
1357 template<typename _ForwardIterator
, typename _OutputIterator
>
1359 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1360 _ForwardIterator __last
, _OutputIterator __result
)
1362 // concept requirements
1363 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1364 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1365 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1366 __glibcxx_requires_valid_range(__first
, __middle
);
1367 __glibcxx_requires_valid_range(__middle
, __last
);
1369 return std::copy(__first
, __middle
,
1370 std::copy(__middle
, __last
, __result
));
1373 /// This is a helper function...
1374 template<typename _ForwardIterator
, typename _Predicate
>
1376 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1377 _Predicate __pred
, forward_iterator_tag
)
1379 if (__first
== __last
)
1382 while (__pred(*__first
))
1383 if (++__first
== __last
)
1386 _ForwardIterator __next
= __first
;
1388 while (++__next
!= __last
)
1389 if (__pred(*__next
))
1391 std::iter_swap(__first
, __next
);
1398 /// This is a helper function...
1399 template<typename _BidirectionalIterator
, typename _Predicate
>
1400 _BidirectionalIterator
1401 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1402 _Predicate __pred
, bidirectional_iterator_tag
)
1407 if (__first
== __last
)
1409 else if (__pred(*__first
))
1415 if (__first
== __last
)
1417 else if (!bool(__pred(*__last
)))
1421 std::iter_swap(__first
, __last
);
1428 /// This is a helper function...
1429 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1431 __inplace_stable_partition(_ForwardIterator __first
,
1432 _ForwardIterator __last
,
1433 _Predicate __pred
, _Distance __len
)
1436 return __pred(*__first
) ? __last
: __first
;
1437 _ForwardIterator __middle
= __first
;
1438 std::advance(__middle
, __len
/ 2);
1439 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1443 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1447 std::rotate(__begin
, __middle
, __end
);
1448 std::advance(__begin
, std::distance(__middle
, __end
));
1452 /// This is a helper function...
1453 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1456 __stable_partition_adaptive(_ForwardIterator __first
,
1457 _ForwardIterator __last
,
1458 _Predicate __pred
, _Distance __len
,
1460 _Distance __buffer_size
)
1462 if (__len
<= __buffer_size
)
1464 _ForwardIterator __result1
= __first
;
1465 _Pointer __result2
= __buffer
;
1466 for (; __first
!= __last
; ++__first
)
1467 if (__pred(*__first
))
1469 *__result1
= *__first
;
1474 *__result2
= *__first
;
1477 std::copy(__buffer
, __result2
, __result1
);
1482 _ForwardIterator __middle
= __first
;
1483 std::advance(__middle
, __len
/ 2);
1484 _ForwardIterator __begin
=
1485 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1486 __len
/ 2, __buffer
,
1488 _ForwardIterator __end
=
1489 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1491 __buffer
, __buffer_size
);
1492 std::rotate(__begin
, __middle
, __end
);
1493 std::advance(__begin
, std::distance(__middle
, __end
));
1499 * @brief Move elements for which a predicate is true to the beginning
1500 * of a sequence, preserving relative ordering.
1501 * @param first A forward iterator.
1502 * @param last A forward iterator.
1503 * @param pred A predicate functor.
1504 * @return An iterator @p middle such that @p pred(i) is true for each
1505 * iterator @p i in the range @p [first,middle) and false for each @p i
1506 * in the range @p [middle,last).
1508 * Performs the same function as @p partition() with the additional
1509 * guarantee that the relative ordering of elements in each group is
1510 * preserved, so any two elements @p x and @p y in the range
1511 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1512 * relative ordering after calling @p stable_partition().
1514 template<typename _ForwardIterator
, typename _Predicate
>
1516 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1519 // concept requirements
1520 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1522 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1523 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1524 __glibcxx_requires_valid_range(__first
, __last
);
1526 if (__first
== __last
)
1530 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1532 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1535 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1537 if (__buf
.size() > 0)
1539 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1540 _DistanceType(__buf
.requested_size()),
1542 _DistanceType(__buf
.size()));
1545 std::__inplace_stable_partition(__first
, __last
, __pred
,
1546 _DistanceType(__buf
.requested_size()));
1550 /// This is a helper function for the sort routines.
1551 template<typename _RandomAccessIterator
>
1553 __heap_select(_RandomAccessIterator __first
,
1554 _RandomAccessIterator __middle
,
1555 _RandomAccessIterator __last
)
1557 std::make_heap(__first
, __middle
);
1558 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1559 if (*__i
< *__first
)
1560 std::__pop_heap(__first
, __middle
, __i
);
1563 /// This is a helper function for the sort routines.
1564 template<typename _RandomAccessIterator
, typename _Compare
>
1566 __heap_select(_RandomAccessIterator __first
,
1567 _RandomAccessIterator __middle
,
1568 _RandomAccessIterator __last
, _Compare __comp
)
1570 std::make_heap(__first
, __middle
, __comp
);
1571 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1572 if (__comp(*__i
, *__first
))
1573 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1579 * @brief Copy the smallest elements of a sequence.
1580 * @param first An iterator.
1581 * @param last Another iterator.
1582 * @param result_first A random-access iterator.
1583 * @param result_last Another random-access iterator.
1584 * @return An iterator indicating the end of the resulting sequence.
1586 * Copies and sorts the smallest N values from the range @p [first,last)
1587 * to the range beginning at @p result_first, where the number of
1588 * elements to be copied, @p N, is the smaller of @p (last-first) and
1589 * @p (result_last-result_first).
1590 * After the sort if @p i and @j are iterators in the range
1591 * @p [result_first,result_first+N) such that @i precedes @j then
1592 * @p *j<*i is false.
1593 * The value returned is @p result_first+N.
1595 template<typename _InputIterator
, typename _RandomAccessIterator
>
1596 _RandomAccessIterator
1597 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1598 _RandomAccessIterator __result_first
,
1599 _RandomAccessIterator __result_last
)
1601 typedef typename iterator_traits
<_InputIterator
>::value_type
1603 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1605 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1608 // concept requirements
1609 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1610 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1612 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1614 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1615 __glibcxx_requires_valid_range(__first
, __last
);
1616 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1618 if (__result_first
== __result_last
)
1619 return __result_last
;
1620 _RandomAccessIterator __result_real_last
= __result_first
;
1621 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1623 *__result_real_last
= *__first
;
1624 ++__result_real_last
;
1627 std::make_heap(__result_first
, __result_real_last
);
1628 while (__first
!= __last
)
1630 if (*__first
< *__result_first
)
1631 std::__adjust_heap(__result_first
, _DistanceType(0),
1632 _DistanceType(__result_real_last
1634 _InputValueType(*__first
));
1637 std::sort_heap(__result_first
, __result_real_last
);
1638 return __result_real_last
;
1642 * @brief Copy the smallest elements of a sequence using a predicate for
1644 * @param first An input iterator.
1645 * @param last Another input iterator.
1646 * @param result_first A random-access iterator.
1647 * @param result_last Another random-access iterator.
1648 * @param comp A comparison functor.
1649 * @return An iterator indicating the end of the resulting sequence.
1651 * Copies and sorts the smallest N values from the range @p [first,last)
1652 * to the range beginning at @p result_first, where the number of
1653 * elements to be copied, @p N, is the smaller of @p (last-first) and
1654 * @p (result_last-result_first).
1655 * After the sort if @p i and @j are iterators in the range
1656 * @p [result_first,result_first+N) such that @i precedes @j then
1657 * @p comp(*j,*i) is false.
1658 * The value returned is @p result_first+N.
1660 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1661 _RandomAccessIterator
1662 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1663 _RandomAccessIterator __result_first
,
1664 _RandomAccessIterator __result_last
,
1667 typedef typename iterator_traits
<_InputIterator
>::value_type
1669 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1671 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1674 // concept requirements
1675 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1676 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1677 _RandomAccessIterator
>)
1678 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1680 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1681 _InputValueType
, _OutputValueType
>)
1682 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1683 _OutputValueType
, _OutputValueType
>)
1684 __glibcxx_requires_valid_range(__first
, __last
);
1685 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1687 if (__result_first
== __result_last
)
1688 return __result_last
;
1689 _RandomAccessIterator __result_real_last
= __result_first
;
1690 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1692 *__result_real_last
= *__first
;
1693 ++__result_real_last
;
1696 std::make_heap(__result_first
, __result_real_last
, __comp
);
1697 while (__first
!= __last
)
1699 if (__comp(*__first
, *__result_first
))
1700 std::__adjust_heap(__result_first
, _DistanceType(0),
1701 _DistanceType(__result_real_last
1703 _InputValueType(*__first
),
1707 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1708 return __result_real_last
;
1711 /// This is a helper function for the sort routine.
1712 template<typename _RandomAccessIterator
, typename _Tp
>
1714 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1716 _RandomAccessIterator __next
= __last
;
1718 while (__val
< *__next
)
1727 /// This is a helper function for the sort routine.
1728 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1730 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1733 _RandomAccessIterator __next
= __last
;
1735 while (__comp(__val
, *__next
))
1744 /// This is a helper function for the sort routine.
1745 template<typename _RandomAccessIterator
>
1747 __insertion_sort(_RandomAccessIterator __first
,
1748 _RandomAccessIterator __last
)
1750 if (__first
== __last
)
1753 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1755 typename iterator_traits
<_RandomAccessIterator
>::value_type
1757 if (__val
< *__first
)
1759 std::copy_backward(__first
, __i
, __i
+ 1);
1763 std::__unguarded_linear_insert(__i
, __val
);
1767 /// This is a helper function for the sort routine.
1768 template<typename _RandomAccessIterator
, typename _Compare
>
1770 __insertion_sort(_RandomAccessIterator __first
,
1771 _RandomAccessIterator __last
, _Compare __comp
)
1773 if (__first
== __last
) return;
1775 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1777 typename iterator_traits
<_RandomAccessIterator
>::value_type
1779 if (__comp(__val
, *__first
))
1781 std::copy_backward(__first
, __i
, __i
+ 1);
1785 std::__unguarded_linear_insert(__i
, __val
, __comp
);
1789 /// This is a helper function for the sort routine.
1790 template<typename _RandomAccessIterator
>
1792 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1793 _RandomAccessIterator __last
)
1795 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1798 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1799 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
1802 /// This is a helper function for the sort routine.
1803 template<typename _RandomAccessIterator
, typename _Compare
>
1805 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1806 _RandomAccessIterator __last
, _Compare __comp
)
1808 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1811 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1812 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
1817 * This controls some aspect of the sort routines.
1819 enum { _S_threshold
= 16 };
1821 /// This is a helper function for the sort routine.
1822 template<typename _RandomAccessIterator
>
1824 __final_insertion_sort(_RandomAccessIterator __first
,
1825 _RandomAccessIterator __last
)
1827 if (__last
- __first
> int(_S_threshold
))
1829 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
1830 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
1833 std::__insertion_sort(__first
, __last
);
1836 /// This is a helper function for the sort routine.
1837 template<typename _RandomAccessIterator
, typename _Compare
>
1839 __final_insertion_sort(_RandomAccessIterator __first
,
1840 _RandomAccessIterator __last
, _Compare __comp
)
1842 if (__last
- __first
> int(_S_threshold
))
1844 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1845 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1849 std::__insertion_sort(__first
, __last
, __comp
);
1852 /// This is a helper function...
1853 template<typename _RandomAccessIterator
, typename _Tp
>
1854 _RandomAccessIterator
1855 __unguarded_partition(_RandomAccessIterator __first
,
1856 _RandomAccessIterator __last
, _Tp __pivot
)
1860 while (*__first
< __pivot
)
1863 while (__pivot
< *__last
)
1865 if (!(__first
< __last
))
1867 std::iter_swap(__first
, __last
);
1872 /// This is a helper function...
1873 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1874 _RandomAccessIterator
1875 __unguarded_partition(_RandomAccessIterator __first
,
1876 _RandomAccessIterator __last
,
1877 _Tp __pivot
, _Compare __comp
)
1881 while (__comp(*__first
, __pivot
))
1884 while (__comp(__pivot
, *__last
))
1886 if (!(__first
< __last
))
1888 std::iter_swap(__first
, __last
);
1893 /// This is a helper function for the sort routine.
1894 template<typename _RandomAccessIterator
, typename _Size
>
1896 __introsort_loop(_RandomAccessIterator __first
,
1897 _RandomAccessIterator __last
,
1898 _Size __depth_limit
)
1900 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1903 while (__last
- __first
> int(_S_threshold
))
1905 if (__depth_limit
== 0)
1907 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
1911 _RandomAccessIterator __cut
=
1912 std::__unguarded_partition(__first
, __last
,
1913 _ValueType(std::__median(*__first
,
1920 std::__introsort_loop(__cut
, __last
, __depth_limit
);
1925 /// This is a helper function for the sort routine.
1926 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1928 __introsort_loop(_RandomAccessIterator __first
,
1929 _RandomAccessIterator __last
,
1930 _Size __depth_limit
, _Compare __comp
)
1932 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1935 while (__last
- __first
> int(_S_threshold
))
1937 if (__depth_limit
== 0)
1939 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
1943 _RandomAccessIterator __cut
=
1944 std::__unguarded_partition(__first
, __last
,
1945 _ValueType(std::__median(*__first
,
1953 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1958 /// This is a helper function for the sort routines. Precondition: __n > 0.
1959 template<typename _Size
>
1964 for (__k
= 0; __n
!= 0; __n
>>= 1)
1971 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1975 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1979 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1983 template<typename _RandomAccessIterator
, typename _Size
>
1985 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1986 _RandomAccessIterator __last
, _Size __depth_limit
)
1988 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1991 while (__last
- __first
> 3)
1993 if (__depth_limit
== 0)
1995 std::__heap_select(__first
, __nth
+ 1, __last
);
1997 // Place the nth largest element in its final position.
1998 std::iter_swap(__first
, __nth
);
2002 _RandomAccessIterator __cut
=
2003 std::__unguarded_partition(__first
, __last
,
2004 _ValueType(std::__median(*__first
,
2016 std::__insertion_sort(__first
, __last
);
2019 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2021 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2022 _RandomAccessIterator __last
, _Size __depth_limit
,
2025 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2028 while (__last
- __first
> 3)
2030 if (__depth_limit
== 0)
2032 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2033 // Place the nth largest element in its final position.
2034 std::iter_swap(__first
, __nth
);
2038 _RandomAccessIterator __cut
=
2039 std::__unguarded_partition(__first
, __last
,
2040 _ValueType(std::__median(*__first
,
2053 std::__insertion_sort(__first
, __last
, __comp
);
2059 * @brief Finds the first position in which @a val could be inserted
2060 * without changing the ordering.
2061 * @param first An iterator.
2062 * @param last Another iterator.
2063 * @param val The search term.
2064 * @return An iterator pointing to the first element "not less
2065 * than" @a val, or end() if every element is less than
2067 * @ingroup binarysearch
2069 template<typename _ForwardIterator
, typename _Tp
>
2071 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2074 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2076 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2079 // concept requirements
2080 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2081 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2082 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2084 _DistanceType __len
= std::distance(__first
, __last
);
2085 _DistanceType __half
;
2086 _ForwardIterator __middle
;
2090 __half
= __len
>> 1;
2092 std::advance(__middle
, __half
);
2093 if (*__middle
< __val
)
2097 __len
= __len
- __half
- 1;
2106 * @brief Finds the first position in which @a val could be inserted
2107 * without changing the ordering.
2108 * @param first An iterator.
2109 * @param last Another iterator.
2110 * @param val The search term.
2111 * @param comp A functor to use for comparisons.
2112 * @return An iterator pointing to the first element "not less than" @a val,
2113 * or end() if every element is less than @a val.
2114 * @ingroup binarysearch
2116 * The comparison function should have the same effects on ordering as
2117 * the function used for the initial sort.
2119 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2121 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2122 const _Tp
& __val
, _Compare __comp
)
2124 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2126 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2129 // concept requirements
2130 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2131 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2133 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2136 _DistanceType __len
= std::distance(__first
, __last
);
2137 _DistanceType __half
;
2138 _ForwardIterator __middle
;
2142 __half
= __len
>> 1;
2144 std::advance(__middle
, __half
);
2145 if (__comp(*__middle
, __val
))
2149 __len
= __len
- __half
- 1;
2158 * @brief Finds the last position in which @a val could be inserted
2159 * without changing the ordering.
2160 * @param first An iterator.
2161 * @param last Another iterator.
2162 * @param val The search term.
2163 * @return An iterator pointing to the first element greater than @a val,
2164 * or end() if no elements are greater than @a val.
2165 * @ingroup binarysearch
2167 template<typename _ForwardIterator
, typename _Tp
>
2169 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2172 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2174 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2177 // concept requirements
2178 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2179 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2180 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2182 _DistanceType __len
= std::distance(__first
, __last
);
2183 _DistanceType __half
;
2184 _ForwardIterator __middle
;
2188 __half
= __len
>> 1;
2190 std::advance(__middle
, __half
);
2191 if (__val
< *__middle
)
2197 __len
= __len
- __half
- 1;
2204 * @brief Finds the last position in which @a val could be inserted
2205 * without changing the ordering.
2206 * @param first An iterator.
2207 * @param last Another iterator.
2208 * @param val The search term.
2209 * @param comp A functor to use for comparisons.
2210 * @return An iterator pointing to the first element greater than @a val,
2211 * or end() if no elements are greater than @a val.
2212 * @ingroup binarysearch
2214 * The comparison function should have the same effects on ordering as
2215 * the function used for the initial sort.
2217 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2219 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2220 const _Tp
& __val
, _Compare __comp
)
2222 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2224 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2227 // concept requirements
2228 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2229 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2231 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2234 _DistanceType __len
= std::distance(__first
, __last
);
2235 _DistanceType __half
;
2236 _ForwardIterator __middle
;
2240 __half
= __len
>> 1;
2242 std::advance(__middle
, __half
);
2243 if (__comp(__val
, *__middle
))
2249 __len
= __len
- __half
- 1;
2256 * @brief Finds the largest subrange in which @a val could be inserted
2257 * at any place in it without changing the ordering.
2258 * @param first An iterator.
2259 * @param last Another iterator.
2260 * @param val The search term.
2261 * @return An pair of iterators defining the subrange.
2262 * @ingroup binarysearch
2264 * This is equivalent to
2266 * std::make_pair(lower_bound(first, last, val),
2267 * upper_bound(first, last, val))
2269 * but does not actually call those functions.
2271 template<typename _ForwardIterator
, typename _Tp
>
2272 pair
<_ForwardIterator
, _ForwardIterator
>
2273 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2276 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2278 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2281 // concept requirements
2282 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2283 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2284 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2285 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2286 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2288 _DistanceType __len
= std::distance(__first
, __last
);
2289 _DistanceType __half
;
2290 _ForwardIterator __middle
, __left
, __right
;
2294 __half
= __len
>> 1;
2296 std::advance(__middle
, __half
);
2297 if (*__middle
< __val
)
2301 __len
= __len
- __half
- 1;
2303 else if (__val
< *__middle
)
2307 __left
= std::lower_bound(__first
, __middle
, __val
);
2308 std::advance(__first
, __len
);
2309 __right
= std::upper_bound(++__middle
, __first
, __val
);
2310 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2313 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2317 * @brief Finds the largest subrange in which @a val could be inserted
2318 * at any place in it without changing the ordering.
2319 * @param first An iterator.
2320 * @param last Another iterator.
2321 * @param val The search term.
2322 * @param comp A functor to use for comparisons.
2323 * @return An pair of iterators defining the subrange.
2324 * @ingroup binarysearch
2326 * This is equivalent to
2328 * std::make_pair(lower_bound(first, last, val, comp),
2329 * upper_bound(first, last, val, comp))
2331 * but does not actually call those functions.
2333 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2334 pair
<_ForwardIterator
, _ForwardIterator
>
2335 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2339 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2341 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2344 // concept requirements
2345 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2346 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2348 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2350 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2352 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2355 _DistanceType __len
= std::distance(__first
, __last
);
2356 _DistanceType __half
;
2357 _ForwardIterator __middle
, __left
, __right
;
2361 __half
= __len
>> 1;
2363 std::advance(__middle
, __half
);
2364 if (__comp(*__middle
, __val
))
2368 __len
= __len
- __half
- 1;
2370 else if (__comp(__val
, *__middle
))
2374 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2375 std::advance(__first
, __len
);
2376 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2377 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2380 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2384 * @brief Determines whether an element exists in a range.
2385 * @param first An iterator.
2386 * @param last Another iterator.
2387 * @param val The search term.
2388 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2389 * @ingroup binarysearch
2391 * Note that this does not actually return an iterator to @a val. For
2392 * that, use std::find or a container's specialized find member functions.
2394 template<typename _ForwardIterator
, typename _Tp
>
2396 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2399 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2402 // concept requirements
2403 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2404 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2405 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2406 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2408 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2409 return __i
!= __last
&& !(__val
< *__i
);
2413 * @brief Determines whether an element exists in a range.
2414 * @param first An iterator.
2415 * @param last Another iterator.
2416 * @param val The search term.
2417 * @param comp A functor to use for comparisons.
2418 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2419 * @ingroup binarysearch
2421 * Note that this does not actually return an iterator to @a val. For
2422 * that, use std::find or a container's specialized find member functions.
2424 * The comparison function should have the same effects on ordering as
2425 * the function used for the initial sort.
2427 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2429 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2430 const _Tp
& __val
, _Compare __comp
)
2432 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2435 // concept requirements
2436 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2437 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2439 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2441 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2444 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2445 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2450 /// This is a helper function for the merge routines.
2451 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2452 typename _BidirectionalIterator3
>
2453 _BidirectionalIterator3
2454 __merge_backward(_BidirectionalIterator1 __first1
,
2455 _BidirectionalIterator1 __last1
,
2456 _BidirectionalIterator2 __first2
,
2457 _BidirectionalIterator2 __last2
,
2458 _BidirectionalIterator3 __result
)
2460 if (__first1
== __last1
)
2461 return std::copy_backward(__first2
, __last2
, __result
);
2462 if (__first2
== __last2
)
2463 return std::copy_backward(__first1
, __last1
, __result
);
2468 if (*__last2
< *__last1
)
2470 *--__result
= *__last1
;
2471 if (__first1
== __last1
)
2472 return std::copy_backward(__first2
, ++__last2
, __result
);
2477 *--__result
= *__last2
;
2478 if (__first2
== __last2
)
2479 return std::copy_backward(__first1
, ++__last1
, __result
);
2485 /// This is a helper function for the merge routines.
2486 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2487 typename _BidirectionalIterator3
, typename _Compare
>
2488 _BidirectionalIterator3
2489 __merge_backward(_BidirectionalIterator1 __first1
,
2490 _BidirectionalIterator1 __last1
,
2491 _BidirectionalIterator2 __first2
,
2492 _BidirectionalIterator2 __last2
,
2493 _BidirectionalIterator3 __result
,
2496 if (__first1
== __last1
)
2497 return std::copy_backward(__first2
, __last2
, __result
);
2498 if (__first2
== __last2
)
2499 return std::copy_backward(__first1
, __last1
, __result
);
2504 if (__comp(*__last2
, *__last1
))
2506 *--__result
= *__last1
;
2507 if (__first1
== __last1
)
2508 return std::copy_backward(__first2
, ++__last2
, __result
);
2513 *--__result
= *__last2
;
2514 if (__first2
== __last2
)
2515 return std::copy_backward(__first1
, ++__last1
, __result
);
2521 /// This is a helper function for the merge routines.
2522 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2524 _BidirectionalIterator1
2525 __rotate_adaptive(_BidirectionalIterator1 __first
,
2526 _BidirectionalIterator1 __middle
,
2527 _BidirectionalIterator1 __last
,
2528 _Distance __len1
, _Distance __len2
,
2529 _BidirectionalIterator2 __buffer
,
2530 _Distance __buffer_size
)
2532 _BidirectionalIterator2 __buffer_end
;
2533 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2535 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2536 std::copy_backward(__first
, __middle
, __last
);
2537 return std::copy(__buffer
, __buffer_end
, __first
);
2539 else if (__len1
<= __buffer_size
)
2541 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2542 std::copy(__middle
, __last
, __first
);
2543 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2547 std::rotate(__first
, __middle
, __last
);
2548 std::advance(__first
, std::distance(__middle
, __last
));
2553 /// This is a helper function for the merge routines.
2554 template<typename _BidirectionalIterator
, typename _Distance
,
2557 __merge_adaptive(_BidirectionalIterator __first
,
2558 _BidirectionalIterator __middle
,
2559 _BidirectionalIterator __last
,
2560 _Distance __len1
, _Distance __len2
,
2561 _Pointer __buffer
, _Distance __buffer_size
)
2563 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2565 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2566 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2569 else if (__len2
<= __buffer_size
)
2571 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2572 std::__merge_backward(__first
, __middle
, __buffer
,
2573 __buffer_end
, __last
);
2577 _BidirectionalIterator __first_cut
= __first
;
2578 _BidirectionalIterator __second_cut
= __middle
;
2579 _Distance __len11
= 0;
2580 _Distance __len22
= 0;
2581 if (__len1
> __len2
)
2583 __len11
= __len1
/ 2;
2584 std::advance(__first_cut
, __len11
);
2585 __second_cut
= std::lower_bound(__middle
, __last
,
2587 __len22
= std::distance(__middle
, __second_cut
);
2591 __len22
= __len2
/ 2;
2592 std::advance(__second_cut
, __len22
);
2593 __first_cut
= std::upper_bound(__first
, __middle
,
2595 __len11
= std::distance(__first
, __first_cut
);
2597 _BidirectionalIterator __new_middle
=
2598 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2599 __len1
- __len11
, __len22
, __buffer
,
2601 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2602 __len22
, __buffer
, __buffer_size
);
2603 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2605 __len2
- __len22
, __buffer
, __buffer_size
);
2609 /// This is a helper function for the merge routines.
2610 template<typename _BidirectionalIterator
, typename _Distance
,
2611 typename _Pointer
, typename _Compare
>
2613 __merge_adaptive(_BidirectionalIterator __first
,
2614 _BidirectionalIterator __middle
,
2615 _BidirectionalIterator __last
,
2616 _Distance __len1
, _Distance __len2
,
2617 _Pointer __buffer
, _Distance __buffer_size
,
2620 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2622 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2623 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2626 else if (__len2
<= __buffer_size
)
2628 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2629 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2634 _BidirectionalIterator __first_cut
= __first
;
2635 _BidirectionalIterator __second_cut
= __middle
;
2636 _Distance __len11
= 0;
2637 _Distance __len22
= 0;
2638 if (__len1
> __len2
)
2640 __len11
= __len1
/ 2;
2641 std::advance(__first_cut
, __len11
);
2642 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2644 __len22
= std::distance(__middle
, __second_cut
);
2648 __len22
= __len2
/ 2;
2649 std::advance(__second_cut
, __len22
);
2650 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2652 __len11
= std::distance(__first
, __first_cut
);
2654 _BidirectionalIterator __new_middle
=
2655 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2656 __len1
- __len11
, __len22
, __buffer
,
2658 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2659 __len22
, __buffer
, __buffer_size
, __comp
);
2660 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2662 __len2
- __len22
, __buffer
,
2663 __buffer_size
, __comp
);
2667 /// This is a helper function for the merge routines.
2668 template<typename _BidirectionalIterator
, typename _Distance
>
2670 __merge_without_buffer(_BidirectionalIterator __first
,
2671 _BidirectionalIterator __middle
,
2672 _BidirectionalIterator __last
,
2673 _Distance __len1
, _Distance __len2
)
2675 if (__len1
== 0 || __len2
== 0)
2677 if (__len1
+ __len2
== 2)
2679 if (*__middle
< *__first
)
2680 std::iter_swap(__first
, __middle
);
2683 _BidirectionalIterator __first_cut
= __first
;
2684 _BidirectionalIterator __second_cut
= __middle
;
2685 _Distance __len11
= 0;
2686 _Distance __len22
= 0;
2687 if (__len1
> __len2
)
2689 __len11
= __len1
/ 2;
2690 std::advance(__first_cut
, __len11
);
2691 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2692 __len22
= std::distance(__middle
, __second_cut
);
2696 __len22
= __len2
/ 2;
2697 std::advance(__second_cut
, __len22
);
2698 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2699 __len11
= std::distance(__first
, __first_cut
);
2701 std::rotate(__first_cut
, __middle
, __second_cut
);
2702 _BidirectionalIterator __new_middle
= __first_cut
;
2703 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2704 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2706 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2707 __len1
- __len11
, __len2
- __len22
);
2710 /// This is a helper function for the merge routines.
2711 template<typename _BidirectionalIterator
, typename _Distance
,
2714 __merge_without_buffer(_BidirectionalIterator __first
,
2715 _BidirectionalIterator __middle
,
2716 _BidirectionalIterator __last
,
2717 _Distance __len1
, _Distance __len2
,
2720 if (__len1
== 0 || __len2
== 0)
2722 if (__len1
+ __len2
== 2)
2724 if (__comp(*__middle
, *__first
))
2725 std::iter_swap(__first
, __middle
);
2728 _BidirectionalIterator __first_cut
= __first
;
2729 _BidirectionalIterator __second_cut
= __middle
;
2730 _Distance __len11
= 0;
2731 _Distance __len22
= 0;
2732 if (__len1
> __len2
)
2734 __len11
= __len1
/ 2;
2735 std::advance(__first_cut
, __len11
);
2736 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2738 __len22
= std::distance(__middle
, __second_cut
);
2742 __len22
= __len2
/ 2;
2743 std::advance(__second_cut
, __len22
);
2744 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2746 __len11
= std::distance(__first
, __first_cut
);
2748 std::rotate(__first_cut
, __middle
, __second_cut
);
2749 _BidirectionalIterator __new_middle
= __first_cut
;
2750 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2751 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2752 __len11
, __len22
, __comp
);
2753 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2754 __len1
- __len11
, __len2
- __len22
, __comp
);
2758 * @brief Merges two sorted ranges in place.
2759 * @param first An iterator.
2760 * @param middle Another iterator.
2761 * @param last Another iterator.
2764 * Merges two sorted and consecutive ranges, [first,middle) and
2765 * [middle,last), and puts the result in [first,last). The output will
2766 * be sorted. The sort is @e stable, that is, for equivalent
2767 * elements in the two ranges, elements from the first range will always
2768 * come before elements from the second.
2770 * If enough additional memory is available, this takes (last-first)-1
2771 * comparisons. Otherwise an NlogN algorithm is used, where N is
2772 * distance(first,last).
2774 template<typename _BidirectionalIterator
>
2776 inplace_merge(_BidirectionalIterator __first
,
2777 _BidirectionalIterator __middle
,
2778 _BidirectionalIterator __last
)
2780 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2782 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2785 // concept requirements
2786 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2787 _BidirectionalIterator
>)
2788 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2789 __glibcxx_requires_sorted(__first
, __middle
);
2790 __glibcxx_requires_sorted(__middle
, __last
);
2792 if (__first
== __middle
|| __middle
== __last
)
2795 _DistanceType __len1
= std::distance(__first
, __middle
);
2796 _DistanceType __len2
= std::distance(__middle
, __last
);
2798 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2800 if (__buf
.begin() == 0)
2801 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
2803 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2804 __buf
.begin(), _DistanceType(__buf
.size()));
2808 * @brief Merges two sorted ranges in place.
2809 * @param first An iterator.
2810 * @param middle Another iterator.
2811 * @param last Another iterator.
2812 * @param comp A functor to use for comparisons.
2815 * Merges two sorted and consecutive ranges, [first,middle) and
2816 * [middle,last), and puts the result in [first,last). The output will
2817 * be sorted. The sort is @e stable, that is, for equivalent
2818 * elements in the two ranges, elements from the first range will always
2819 * come before elements from the second.
2821 * If enough additional memory is available, this takes (last-first)-1
2822 * comparisons. Otherwise an NlogN algorithm is used, where N is
2823 * distance(first,last).
2825 * The comparison function should have the same effects on ordering as
2826 * the function used for the initial sort.
2828 template<typename _BidirectionalIterator
, typename _Compare
>
2830 inplace_merge(_BidirectionalIterator __first
,
2831 _BidirectionalIterator __middle
,
2832 _BidirectionalIterator __last
,
2835 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2837 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2840 // concept requirements
2841 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2842 _BidirectionalIterator
>)
2843 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2844 _ValueType
, _ValueType
>)
2845 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2846 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2848 if (__first
== __middle
|| __middle
== __last
)
2851 const _DistanceType __len1
= std::distance(__first
, __middle
);
2852 const _DistanceType __len2
= std::distance(__middle
, __last
);
2854 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2856 if (__buf
.begin() == 0)
2857 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
2860 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2861 __buf
.begin(), _DistanceType(__buf
.size()),
2865 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2868 __merge_sort_loop(_RandomAccessIterator1 __first
,
2869 _RandomAccessIterator1 __last
,
2870 _RandomAccessIterator2 __result
,
2871 _Distance __step_size
)
2873 const _Distance __two_step
= 2 * __step_size
;
2875 while (__last
- __first
>= __two_step
)
2877 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
2878 __first
+ __step_size
,
2879 __first
+ __two_step
,
2881 __first
+= __two_step
;
2884 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2885 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
2886 __first
+ __step_size
, __last
,
2890 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2891 typename _Distance
, typename _Compare
>
2893 __merge_sort_loop(_RandomAccessIterator1 __first
,
2894 _RandomAccessIterator1 __last
,
2895 _RandomAccessIterator2 __result
, _Distance __step_size
,
2898 const _Distance __two_step
= 2 * __step_size
;
2900 while (__last
- __first
>= __two_step
)
2902 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
2903 __first
+ __step_size
, __first
+ __two_step
,
2906 __first
+= __two_step
;
2908 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2910 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
2911 __first
+ __step_size
, __last
, __result
, __comp
);
2914 template<typename _RandomAccessIterator
, typename _Distance
>
2916 __chunk_insertion_sort(_RandomAccessIterator __first
,
2917 _RandomAccessIterator __last
,
2918 _Distance __chunk_size
)
2920 while (__last
- __first
>= __chunk_size
)
2922 std::__insertion_sort(__first
, __first
+ __chunk_size
);
2923 __first
+= __chunk_size
;
2925 std::__insertion_sort(__first
, __last
);
2928 template<typename _RandomAccessIterator
, typename _Distance
,
2931 __chunk_insertion_sort(_RandomAccessIterator __first
,
2932 _RandomAccessIterator __last
,
2933 _Distance __chunk_size
, _Compare __comp
)
2935 while (__last
- __first
>= __chunk_size
)
2937 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2938 __first
+= __chunk_size
;
2940 std::__insertion_sort(__first
, __last
, __comp
);
2943 enum { _S_chunk_size
= 7 };
2945 template<typename _RandomAccessIterator
, typename _Pointer
>
2947 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2948 _RandomAccessIterator __last
,
2951 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2954 const _Distance __len
= __last
- __first
;
2955 const _Pointer __buffer_last
= __buffer
+ __len
;
2957 _Distance __step_size
= _S_chunk_size
;
2958 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
2960 while (__step_size
< __len
)
2962 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
2964 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
2969 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2971 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2972 _RandomAccessIterator __last
,
2973 _Pointer __buffer
, _Compare __comp
)
2975 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2978 const _Distance __len
= __last
- __first
;
2979 const _Pointer __buffer_last
= __buffer
+ __len
;
2981 _Distance __step_size
= _S_chunk_size
;
2982 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2984 while (__step_size
< __len
)
2986 std::__merge_sort_loop(__first
, __last
, __buffer
,
2987 __step_size
, __comp
);
2989 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2990 __step_size
, __comp
);
2995 template<typename _RandomAccessIterator
, typename _Pointer
,
2998 __stable_sort_adaptive(_RandomAccessIterator __first
,
2999 _RandomAccessIterator __last
,
3000 _Pointer __buffer
, _Distance __buffer_size
)
3002 const _Distance __len
= (__last
- __first
+ 1) / 2;
3003 const _RandomAccessIterator __middle
= __first
+ __len
;
3004 if (__len
> __buffer_size
)
3006 std::__stable_sort_adaptive(__first
, __middle
,
3007 __buffer
, __buffer_size
);
3008 std::__stable_sort_adaptive(__middle
, __last
,
3009 __buffer
, __buffer_size
);
3013 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3014 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3016 std::__merge_adaptive(__first
, __middle
, __last
,
3017 _Distance(__middle
- __first
),
3018 _Distance(__last
- __middle
),
3019 __buffer
, __buffer_size
);
3022 template<typename _RandomAccessIterator
, typename _Pointer
,
3023 typename _Distance
, typename _Compare
>
3025 __stable_sort_adaptive(_RandomAccessIterator __first
,
3026 _RandomAccessIterator __last
,
3027 _Pointer __buffer
, _Distance __buffer_size
,
3030 const _Distance __len
= (__last
- __first
+ 1) / 2;
3031 const _RandomAccessIterator __middle
= __first
+ __len
;
3032 if (__len
> __buffer_size
)
3034 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3035 __buffer_size
, __comp
);
3036 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3037 __buffer_size
, __comp
);
3041 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3042 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3044 std::__merge_adaptive(__first
, __middle
, __last
,
3045 _Distance(__middle
- __first
),
3046 _Distance(__last
- __middle
),
3047 __buffer
, __buffer_size
,
3051 /// This is a helper function for the stable sorting routines.
3052 template<typename _RandomAccessIterator
>
3054 __inplace_stable_sort(_RandomAccessIterator __first
,
3055 _RandomAccessIterator __last
)
3057 if (__last
- __first
< 15)
3059 std::__insertion_sort(__first
, __last
);
3062 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3063 std::__inplace_stable_sort(__first
, __middle
);
3064 std::__inplace_stable_sort(__middle
, __last
);
3065 std::__merge_without_buffer(__first
, __middle
, __last
,
3070 /// This is a helper function for the stable sorting routines.
3071 template<typename _RandomAccessIterator
, typename _Compare
>
3073 __inplace_stable_sort(_RandomAccessIterator __first
,
3074 _RandomAccessIterator __last
, _Compare __comp
)
3076 if (__last
- __first
< 15)
3078 std::__insertion_sort(__first
, __last
, __comp
);
3081 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3082 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3083 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3084 std::__merge_without_buffer(__first
, __middle
, __last
,
3092 // Set algorithms: includes, set_union, set_intersection, set_difference,
3093 // set_symmetric_difference. All of these algorithms have the precondition
3094 // that their input ranges are sorted and the postcondition that their output
3095 // ranges are sorted.
3098 * @brief Determines whether all elements of a sequence exists in a range.
3099 * @param first1 Start of search range.
3100 * @param last1 End of search range.
3101 * @param first2 Start of sequence
3102 * @param last2 End of sequence.
3103 * @return True if each element in [first2,last2) is contained in order
3104 * within [first1,last1). False otherwise.
3105 * @ingroup setoperations
3107 * This operation expects both [first1,last1) and [first2,last2) to be
3108 * sorted. Searches for the presence of each element in [first2,last2)
3109 * within [first1,last1). The iterators over each range only move forward,
3110 * so this is a linear algorithm. If an element in [first2,last2) is not
3111 * found before the search iterator reaches @a last2, false is returned.
3113 template<typename _InputIterator1
, typename _InputIterator2
>
3115 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3116 _InputIterator2 __first2
, _InputIterator2 __last2
)
3118 typedef typename iterator_traits
<_InputIterator1
>::value_type
3120 typedef typename iterator_traits
<_InputIterator2
>::value_type
3123 // concept requirements
3124 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3125 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3126 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3127 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3128 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3129 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3131 while (__first1
!= __last1
&& __first2
!= __last2
)
3132 if (*__first2
< *__first1
)
3134 else if(*__first1
< *__first2
)
3137 ++__first1
, ++__first2
;
3139 return __first2
== __last2
;
3143 * @brief Determines whether all elements of a sequence exists in a range
3145 * @param first1 Start of search range.
3146 * @param last1 End of search range.
3147 * @param first2 Start of sequence
3148 * @param last2 End of sequence.
3149 * @param comp Comparison function to use.
3150 * @return True if each element in [first2,last2) is contained in order
3151 * within [first1,last1) according to comp. False otherwise.
3152 * @ingroup setoperations
3154 * This operation expects both [first1,last1) and [first2,last2) to be
3155 * sorted. Searches for the presence of each element in [first2,last2)
3156 * within [first1,last1), using comp to decide. The iterators over each
3157 * range only move forward, so this is a linear algorithm. If an element
3158 * in [first2,last2) is not found before the search iterator reaches @a
3159 * last2, false is returned.
3161 template<typename _InputIterator1
, typename _InputIterator2
,
3164 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3165 _InputIterator2 __first2
, _InputIterator2 __last2
,
3168 typedef typename iterator_traits
<_InputIterator1
>::value_type
3170 typedef typename iterator_traits
<_InputIterator2
>::value_type
3173 // concept requirements
3174 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3175 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3176 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3177 _ValueType1
, _ValueType2
>)
3178 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3179 _ValueType2
, _ValueType1
>)
3180 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3181 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3183 while (__first1
!= __last1
&& __first2
!= __last2
)
3184 if (__comp(*__first2
, *__first1
))
3186 else if(__comp(*__first1
, *__first2
))
3189 ++__first1
, ++__first2
;
3191 return __first2
== __last2
;
3200 // set_symmetric_difference
3205 * @brief Permute range into the next "dictionary" ordering.
3206 * @param first Start of range.
3207 * @param last End of range.
3208 * @return False if wrapped to first permutation, true otherwise.
3210 * Treats all permutations of the range as a set of "dictionary" sorted
3211 * sequences. Permutes the current sequence into the next one of this set.
3212 * Returns true if there are more sequences to generate. If the sequence
3213 * is the largest of the set, the smallest is generated and false returned.
3215 template<typename _BidirectionalIterator
>
3217 next_permutation(_BidirectionalIterator __first
,
3218 _BidirectionalIterator __last
)
3220 // concept requirements
3221 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3222 _BidirectionalIterator
>)
3223 __glibcxx_function_requires(_LessThanComparableConcept
<
3224 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3225 __glibcxx_requires_valid_range(__first
, __last
);
3227 if (__first
== __last
)
3229 _BidirectionalIterator __i
= __first
;
3238 _BidirectionalIterator __ii
= __i
;
3242 _BidirectionalIterator __j
= __last
;
3243 while (!(*__i
< *--__j
))
3245 std::iter_swap(__i
, __j
);
3246 std::reverse(__ii
, __last
);
3251 std::reverse(__first
, __last
);
3258 * @brief Permute range into the next "dictionary" ordering using
3259 * comparison functor.
3260 * @param first Start of range.
3261 * @param last End of range.
3262 * @param comp A comparison functor.
3263 * @return False if wrapped to first permutation, true otherwise.
3265 * Treats all permutations of the range [first,last) as a set of
3266 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3267 * sequence into the next one of this set. Returns true if there are more
3268 * sequences to generate. If the sequence is the largest of the set, the
3269 * smallest is generated and false returned.
3271 template<typename _BidirectionalIterator
, typename _Compare
>
3273 next_permutation(_BidirectionalIterator __first
,
3274 _BidirectionalIterator __last
, _Compare __comp
)
3276 // concept requirements
3277 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3278 _BidirectionalIterator
>)
3279 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3280 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3281 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3282 __glibcxx_requires_valid_range(__first
, __last
);
3284 if (__first
== __last
)
3286 _BidirectionalIterator __i
= __first
;
3295 _BidirectionalIterator __ii
= __i
;
3297 if (__comp(*__i
, *__ii
))
3299 _BidirectionalIterator __j
= __last
;
3300 while (!bool(__comp(*__i
, *--__j
)))
3302 std::iter_swap(__i
, __j
);
3303 std::reverse(__ii
, __last
);
3308 std::reverse(__first
, __last
);
3315 * @brief Permute range into the previous "dictionary" ordering.
3316 * @param first Start of range.
3317 * @param last End of range.
3318 * @return False if wrapped to last permutation, true otherwise.
3320 * Treats all permutations of the range as a set of "dictionary" sorted
3321 * sequences. Permutes the current sequence into the previous one of this
3322 * set. Returns true if there are more sequences to generate. If the
3323 * sequence is the smallest of the set, the largest is generated and false
3326 template<typename _BidirectionalIterator
>
3328 prev_permutation(_BidirectionalIterator __first
,
3329 _BidirectionalIterator __last
)
3331 // concept requirements
3332 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3333 _BidirectionalIterator
>)
3334 __glibcxx_function_requires(_LessThanComparableConcept
<
3335 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3336 __glibcxx_requires_valid_range(__first
, __last
);
3338 if (__first
== __last
)
3340 _BidirectionalIterator __i
= __first
;
3349 _BidirectionalIterator __ii
= __i
;
3353 _BidirectionalIterator __j
= __last
;
3354 while (!(*--__j
< *__i
))
3356 std::iter_swap(__i
, __j
);
3357 std::reverse(__ii
, __last
);
3362 std::reverse(__first
, __last
);
3369 * @brief Permute range into the previous "dictionary" ordering using
3370 * comparison functor.
3371 * @param first Start of range.
3372 * @param last End of range.
3373 * @param comp A comparison functor.
3374 * @return False if wrapped to last permutation, true otherwise.
3376 * Treats all permutations of the range [first,last) as a set of
3377 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3378 * sequence into the previous one of this set. Returns true if there are
3379 * more sequences to generate. If the sequence is the smallest of the set,
3380 * the largest is generated and false returned.
3382 template<typename _BidirectionalIterator
, typename _Compare
>
3384 prev_permutation(_BidirectionalIterator __first
,
3385 _BidirectionalIterator __last
, _Compare __comp
)
3387 // concept requirements
3388 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3389 _BidirectionalIterator
>)
3390 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3391 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3392 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3393 __glibcxx_requires_valid_range(__first
, __last
);
3395 if (__first
== __last
)
3397 _BidirectionalIterator __i
= __first
;
3406 _BidirectionalIterator __ii
= __i
;
3408 if (__comp(*__ii
, *__i
))
3410 _BidirectionalIterator __j
= __last
;
3411 while (!bool(__comp(*--__j
, *__i
)))
3413 std::iter_swap(__i
, __j
);
3414 std::reverse(__ii
, __last
);
3419 std::reverse(__first
, __last
);
3429 * @brief Copy a sequence, replacing each element of one value with another
3431 * @param first An input iterator.
3432 * @param last An input iterator.
3433 * @param result An output iterator.
3434 * @param old_value The value to be replaced.
3435 * @param new_value The replacement value.
3436 * @return The end of the output sequence, @p result+(last-first).
3438 * Copies each element in the input range @p [first,last) to the
3439 * output range @p [result,result+(last-first)) replacing elements
3440 * equal to @p old_value with @p new_value.
3442 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3444 replace_copy(_InputIterator __first
, _InputIterator __last
,
3445 _OutputIterator __result
,
3446 const _Tp
& __old_value
, const _Tp
& __new_value
)
3448 // concept requirements
3449 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3450 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3451 typename iterator_traits
<_InputIterator
>::value_type
>)
3452 __glibcxx_function_requires(_EqualOpConcept
<
3453 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3454 __glibcxx_requires_valid_range(__first
, __last
);
3456 for (; __first
!= __last
; ++__first
, ++__result
)
3457 if (*__first
== __old_value
)
3458 *__result
= __new_value
;
3460 *__result
= *__first
;
3465 * @brief Copy a sequence, replacing each value for which a predicate
3466 * returns true with another value.
3467 * @param first An input iterator.
3468 * @param last An input iterator.
3469 * @param result An output iterator.
3470 * @param pred A predicate.
3471 * @param new_value The replacement value.
3472 * @return The end of the output sequence, @p result+(last-first).
3474 * Copies each element in the range @p [first,last) to the range
3475 * @p [result,result+(last-first)) replacing elements for which
3476 * @p pred returns true with @p new_value.
3478 template<typename _InputIterator
, typename _OutputIterator
,
3479 typename _Predicate
, typename _Tp
>
3481 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3482 _OutputIterator __result
,
3483 _Predicate __pred
, const _Tp
& __new_value
)
3485 // concept requirements
3486 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3487 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3488 typename iterator_traits
<_InputIterator
>::value_type
>)
3489 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3490 typename iterator_traits
<_InputIterator
>::value_type
>)
3491 __glibcxx_requires_valid_range(__first
, __last
);
3493 for (; __first
!= __last
; ++__first
, ++__result
)
3494 if (__pred(*__first
))
3495 *__result
= __new_value
;
3497 *__result
= *__first
;
3501 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3503 * @brief Determines whether the elements of a sequence are sorted.
3504 * @param first An iterator.
3505 * @param last Another iterator.
3506 * @return True if the elements are sorted, false otherwise.
3508 template<typename _ForwardIterator
>
3510 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3511 { return std::is_sorted_until(__first
, __last
) == __last
; }
3514 * @brief Determines whether the elements of a sequence are sorted
3515 * according to a comparison functor.
3516 * @param first An iterator.
3517 * @param last Another iterator.
3518 * @param comp A comparison functor.
3519 * @return True if the elements are sorted, false otherwise.
3521 template<typename _ForwardIterator
, typename _Compare
>
3523 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3525 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3528 * @brief Determines the end of a sorted sequence.
3529 * @param first An iterator.
3530 * @param last Another iterator.
3531 * @return An iterator pointing to the last iterator i in [first, last)
3532 * for which the range [first, i) is sorted.
3534 template<typename _ForwardIterator
>
3536 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3538 // concept requirements
3539 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3540 __glibcxx_function_requires(_LessThanComparableConcept
<
3541 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3542 __glibcxx_requires_valid_range(__first
, __last
);
3544 if (__first
== __last
)
3547 _ForwardIterator __next
= __first
;
3548 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3549 if (*__next
< *__first
)
3555 * @brief Determines the end of a sorted sequence using comparison functor.
3556 * @param first An iterator.
3557 * @param last Another iterator.
3558 * @param comp A comparison functor.
3559 * @return An iterator pointing to the last iterator i in [first, last)
3560 * for which the range [first, i) is sorted.
3562 template<typename _ForwardIterator
, typename _Compare
>
3564 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3567 // concept requirements
3568 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3569 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3570 typename iterator_traits
<_ForwardIterator
>::value_type
,
3571 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3572 __glibcxx_requires_valid_range(__first
, __last
);
3574 if (__first
== __last
)
3577 _ForwardIterator __next
= __first
;
3578 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3579 if (__comp(*__next
, *__first
))
3585 * @brief Determines min and max at once as an ordered pair.
3586 * @param a A thing of arbitrary type.
3587 * @param b Another thing of arbitrary type.
3588 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3590 template<typename _Tp
>
3591 inline pair
<const _Tp
&, const _Tp
&>
3592 minmax(const _Tp
& __a
, const _Tp
& __b
)
3594 // concept requirements
3595 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3597 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3598 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3602 * @brief Determines min and max at once as an ordered pair.
3603 * @param a A thing of arbitrary type.
3604 * @param b Another thing of arbitrary type.
3605 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3606 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3608 template<typename _Tp
, typename _Compare
>
3609 inline pair
<const _Tp
&, const _Tp
&>
3610 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3612 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3613 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3617 * @brief Return a pair of iterators pointing to the minimum and maximum
3618 * elements in a range.
3619 * @param first Start of range.
3620 * @param last End of range.
3621 * @return make_pair(m, M), where m is the first iterator i in
3622 * [first, last) such that no other element in the range is
3623 * smaller, and where M is the last iterator i in [first, last)
3624 * such that no other element in the range is larger.
3626 template<typename _ForwardIterator
>
3627 pair
<_ForwardIterator
, _ForwardIterator
>
3628 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3630 // concept requirements
3631 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3632 __glibcxx_function_requires(_LessThanComparableConcept
<
3633 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3634 __glibcxx_requires_valid_range(__first
, __last
);
3636 _ForwardIterator __next
= __first
;
3637 if (__first
== __last
3638 || ++__next
== __last
)
3639 return std::make_pair(__first
, __first
);
3641 _ForwardIterator __min
, __max
;
3642 if (*__next
< *__first
)
3656 while (__first
!= __last
)
3659 if (++__next
== __last
)
3661 if (*__first
< *__min
)
3663 else if (!(*__first
< *__max
))
3668 if (*__next
< *__first
)
3670 if (*__next
< *__min
)
3672 if (!(*__first
< *__max
))
3677 if (*__first
< *__min
)
3679 if (!(*__next
< *__max
))
3687 return std::make_pair(__min
, __max
);
3691 * @brief Return a pair of iterators pointing to the minimum and maximum
3692 * elements in a range.
3693 * @param first Start of range.
3694 * @param last End of range.
3695 * @param comp Comparison functor.
3696 * @return make_pair(m, M), where m is the first iterator i in
3697 * [first, last) such that no other element in the range is
3698 * smaller, and where M is the last iterator i in [first, last)
3699 * such that no other element in the range is larger.
3701 template<typename _ForwardIterator
, typename _Compare
>
3702 pair
<_ForwardIterator
, _ForwardIterator
>
3703 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3706 // concept requirements
3707 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3708 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3709 typename iterator_traits
<_ForwardIterator
>::value_type
,
3710 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3711 __glibcxx_requires_valid_range(__first
, __last
);
3713 _ForwardIterator __next
= __first
;
3714 if (__first
== __last
3715 || ++__next
== __last
)
3716 return std::make_pair(__first
, __first
);
3718 _ForwardIterator __min
, __max
;
3719 if (__comp(*__next
, *__first
))
3733 while (__first
!= __last
)
3736 if (++__next
== __last
)
3738 if (__comp(*__first
, *__min
))
3740 else if (!__comp(*__first
, *__max
))
3745 if (__comp(*__next
, *__first
))
3747 if (__comp(*__next
, *__min
))
3749 if (!__comp(*__first
, *__max
))
3754 if (__comp(*__first
, *__min
))
3756 if (!__comp(*__next
, *__max
))
3764 return std::make_pair(__min
, __max
);
3766 #endif // __GXX_EXPERIMENTAL_CXX0X__
3768 _GLIBCXX_END_NAMESPACE
3770 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3773 * @brief Apply a function to every element of a sequence.
3774 * @param first An input iterator.
3775 * @param last An input iterator.
3776 * @param f A unary function object.
3779 * Applies the function object @p f to each element in the range
3780 * @p [first,last). @p f must not modify the order of the sequence.
3781 * If @p f has a return value it is ignored.
3783 template<typename _InputIterator
, typename _Function
>
3785 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3787 // concept requirements
3788 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3789 __glibcxx_requires_valid_range(__first
, __last
);
3790 for (; __first
!= __last
; ++__first
)
3796 * @brief Find the first occurrence of a value in a sequence.
3797 * @param first An input iterator.
3798 * @param last An input iterator.
3799 * @param val The value to find.
3800 * @return The first iterator @c i in the range @p [first,last)
3801 * such that @c *i == @p val, or @p last if no such iterator exists.
3803 template<typename _InputIterator
, typename _Tp
>
3804 inline _InputIterator
3805 find(_InputIterator __first
, _InputIterator __last
,
3808 // concept requirements
3809 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3810 __glibcxx_function_requires(_EqualOpConcept
<
3811 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3812 __glibcxx_requires_valid_range(__first
, __last
);
3813 return std::__find(__first
, __last
, __val
,
3814 std::__iterator_category(__first
));
3818 * @brief Find the first element in a sequence for which a
3819 * predicate is true.
3820 * @param first An input iterator.
3821 * @param last An input iterator.
3822 * @param pred A predicate.
3823 * @return The first iterator @c i in the range @p [first,last)
3824 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3826 template<typename _InputIterator
, typename _Predicate
>
3827 inline _InputIterator
3828 find_if(_InputIterator __first
, _InputIterator __last
,
3831 // concept requirements
3832 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3833 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3834 typename iterator_traits
<_InputIterator
>::value_type
>)
3835 __glibcxx_requires_valid_range(__first
, __last
);
3836 return std::__find_if(__first
, __last
, __pred
,
3837 std::__iterator_category(__first
));
3841 * @brief Find element from a set in a sequence.
3842 * @param first1 Start of range to search.
3843 * @param last1 End of range to search.
3844 * @param first2 Start of match candidates.
3845 * @param last2 End of match candidates.
3846 * @return The first iterator @c i in the range
3847 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
3848 * iterator in [first2,last2), or @p last1 if no such iterator exists.
3850 * Searches the range @p [first1,last1) for an element that is equal to
3851 * some element in the range [first2,last2). If found, returns an iterator
3852 * in the range [first1,last1), otherwise returns @p last1.
3854 template<typename _InputIterator
, typename _ForwardIterator
>
3856 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3857 _ForwardIterator __first2
, _ForwardIterator __last2
)
3859 // concept requirements
3860 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3861 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3862 __glibcxx_function_requires(_EqualOpConcept
<
3863 typename iterator_traits
<_InputIterator
>::value_type
,
3864 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3865 __glibcxx_requires_valid_range(__first1
, __last1
);
3866 __glibcxx_requires_valid_range(__first2
, __last2
);
3868 for (; __first1
!= __last1
; ++__first1
)
3869 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3870 if (*__first1
== *__iter
)
3876 * @brief Find element from a set in a sequence using a predicate.
3877 * @param first1 Start of range to search.
3878 * @param last1 End of range to search.
3879 * @param first2 Start of match candidates.
3880 * @param last2 End of match candidates.
3881 * @param comp Predicate to use.
3882 * @return The first iterator @c i in the range
3883 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
3884 * iterator in [first2,last2), or @p last1 if no such iterator exists.
3887 * Searches the range @p [first1,last1) for an element that is
3888 * equal to some element in the range [first2,last2). If found,
3889 * returns an iterator in the range [first1,last1), otherwise
3892 template<typename _InputIterator
, typename _ForwardIterator
,
3893 typename _BinaryPredicate
>
3895 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3896 _ForwardIterator __first2
, _ForwardIterator __last2
,
3897 _BinaryPredicate __comp
)
3899 // concept requirements
3900 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3901 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3902 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3903 typename iterator_traits
<_InputIterator
>::value_type
,
3904 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3905 __glibcxx_requires_valid_range(__first1
, __last1
);
3906 __glibcxx_requires_valid_range(__first2
, __last2
);
3908 for (; __first1
!= __last1
; ++__first1
)
3909 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3910 if (__comp(*__first1
, *__iter
))
3916 * @brief Find two adjacent values in a sequence that are equal.
3917 * @param first A forward iterator.
3918 * @param last A forward iterator.
3919 * @return The first iterator @c i such that @c i and @c i+1 are both
3920 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
3921 * or @p last if no such iterator exists.
3923 template<typename _ForwardIterator
>
3925 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3927 // concept requirements
3928 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3929 __glibcxx_function_requires(_EqualityComparableConcept
<
3930 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3931 __glibcxx_requires_valid_range(__first
, __last
);
3932 if (__first
== __last
)
3934 _ForwardIterator __next
= __first
;
3935 while(++__next
!= __last
)
3937 if (*__first
== *__next
)
3945 * @brief Find two adjacent values in a sequence using a predicate.
3946 * @param first A forward iterator.
3947 * @param last A forward iterator.
3948 * @param binary_pred A binary predicate.
3949 * @return The first iterator @c i such that @c i and @c i+1 are both
3950 * valid iterators in @p [first,last) and such that
3951 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
3954 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3956 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3957 _BinaryPredicate __binary_pred
)
3959 // concept requirements
3960 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3961 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3962 typename iterator_traits
<_ForwardIterator
>::value_type
,
3963 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3964 __glibcxx_requires_valid_range(__first
, __last
);
3965 if (__first
== __last
)
3967 _ForwardIterator __next
= __first
;
3968 while(++__next
!= __last
)
3970 if (__binary_pred(*__first
, *__next
))
3978 * @brief Count the number of copies of a value in a sequence.
3979 * @param first An input iterator.
3980 * @param last An input iterator.
3981 * @param value The value to be counted.
3982 * @return The number of iterators @c i in the range @p [first,last)
3983 * for which @c *i == @p value
3985 template<typename _InputIterator
, typename _Tp
>
3986 typename iterator_traits
<_InputIterator
>::difference_type
3987 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
3989 // concept requirements
3990 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3991 __glibcxx_function_requires(_EqualOpConcept
<
3992 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3993 __glibcxx_requires_valid_range(__first
, __last
);
3994 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3995 for (; __first
!= __last
; ++__first
)
3996 if (*__first
== __value
)
4002 * @brief Count the elements of a sequence for which a predicate is true.
4003 * @param first An input iterator.
4004 * @param last An input iterator.
4005 * @param pred A predicate.
4006 * @return The number of iterators @c i in the range @p [first,last)
4007 * for which @p pred(*i) is true.
4009 template<typename _InputIterator
, typename _Predicate
>
4010 typename iterator_traits
<_InputIterator
>::difference_type
4011 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4013 // concept requirements
4014 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4015 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4016 typename iterator_traits
<_InputIterator
>::value_type
>)
4017 __glibcxx_requires_valid_range(__first
, __last
);
4018 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4019 for (; __first
!= __last
; ++__first
)
4020 if (__pred(*__first
))
4026 * @brief Search a sequence for a matching sub-sequence.
4027 * @param first1 A forward iterator.
4028 * @param last1 A forward iterator.
4029 * @param first2 A forward iterator.
4030 * @param last2 A forward iterator.
4031 * @return The first iterator @c i in the range
4032 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4033 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4034 * such iterator exists.
4036 * Searches the range @p [first1,last1) for a sub-sequence that compares
4037 * equal value-by-value with the sequence given by @p [first2,last2) and
4038 * returns an iterator to the first element of the sub-sequence, or
4039 * @p last1 if the sub-sequence is not found.
4041 * Because the sub-sequence must lie completely within the range
4042 * @p [first1,last1) it must start at a position less than
4043 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4045 * This means that the returned iterator @c i will be in the range
4046 * @p [first1,last1-(last2-first2))
4048 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4050 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4051 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4053 // concept requirements
4054 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4055 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4056 __glibcxx_function_requires(_EqualOpConcept
<
4057 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4058 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4059 __glibcxx_requires_valid_range(__first1
, __last1
);
4060 __glibcxx_requires_valid_range(__first2
, __last2
);
4062 // Test for empty ranges
4063 if (__first1
== __last1
|| __first2
== __last2
)
4066 // Test for a pattern of length 1.
4067 _ForwardIterator2
__p1(__first2
);
4068 if (++__p1
== __last2
)
4069 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4072 _ForwardIterator2 __p
;
4073 _ForwardIterator1 __current
= __first1
;
4077 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4078 if (__first1
== __last1
)
4082 __current
= __first1
;
4083 if (++__current
== __last1
)
4086 while (*__current
== *__p
)
4088 if (++__p
== __last2
)
4090 if (++__current
== __last1
)
4099 * @brief Search a sequence for a matching sub-sequence using a predicate.
4100 * @param first1 A forward iterator.
4101 * @param last1 A forward iterator.
4102 * @param first2 A forward iterator.
4103 * @param last2 A forward iterator.
4104 * @param predicate A binary predicate.
4105 * @return The first iterator @c i in the range
4106 * @p [first1,last1-(last2-first2)) such that
4107 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4108 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4110 * Searches the range @p [first1,last1) for a sub-sequence that compares
4111 * equal value-by-value with the sequence given by @p [first2,last2),
4112 * using @p predicate to determine equality, and returns an iterator
4113 * to the first element of the sub-sequence, or @p last1 if no such
4116 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4118 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4119 typename _BinaryPredicate
>
4121 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4122 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4123 _BinaryPredicate __predicate
)
4125 // concept requirements
4126 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4127 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4128 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4129 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4130 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4131 __glibcxx_requires_valid_range(__first1
, __last1
);
4132 __glibcxx_requires_valid_range(__first2
, __last2
);
4134 // Test for empty ranges
4135 if (__first1
== __last1
|| __first2
== __last2
)
4138 // Test for a pattern of length 1.
4139 _ForwardIterator2
__p1(__first2
);
4140 if (++__p1
== __last2
)
4142 while (__first1
!= __last1
4143 && !bool(__predicate(*__first1
, *__first2
)))
4149 _ForwardIterator2 __p
;
4150 _ForwardIterator1 __current
= __first1
;
4154 while (__first1
!= __last1
4155 && !bool(__predicate(*__first1
, *__first2
)))
4157 if (__first1
== __last1
)
4161 __current
= __first1
;
4162 if (++__current
== __last1
)
4165 while (__predicate(*__current
, *__p
))
4167 if (++__p
== __last2
)
4169 if (++__current
== __last1
)
4179 * @brief Search a sequence for a number of consecutive values.
4180 * @param first A forward iterator.
4181 * @param last A forward iterator.
4182 * @param count The number of consecutive values.
4183 * @param val The value to find.
4184 * @return The first iterator @c i in the range @p [first,last-count)
4185 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4186 * or @p last if no such iterator exists.
4188 * Searches the range @p [first,last) for @p count consecutive elements
4191 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4193 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4194 _Integer __count
, const _Tp
& __val
)
4196 // concept requirements
4197 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4198 __glibcxx_function_requires(_EqualOpConcept
<
4199 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4200 __glibcxx_requires_valid_range(__first
, __last
);
4205 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4206 return std::__search_n(__first
, __last
, __count
, __val
,
4207 std::__iterator_category(__first
));
4212 * @brief Search a sequence for a number of consecutive values using a
4214 * @param first A forward iterator.
4215 * @param last A forward iterator.
4216 * @param count The number of consecutive values.
4217 * @param val The value to find.
4218 * @param binary_pred A binary predicate.
4219 * @return The first iterator @c i in the range @p [first,last-count)
4220 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4221 * range @p [0,count), or @p last if no such iterator exists.
4223 * Searches the range @p [first,last) for @p count consecutive elements
4224 * for which the predicate returns true.
4226 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4227 typename _BinaryPredicate
>
4229 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4230 _Integer __count
, const _Tp
& __val
,
4231 _BinaryPredicate __binary_pred
)
4233 // concept requirements
4234 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4235 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4236 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4237 __glibcxx_requires_valid_range(__first
, __last
);
4243 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4247 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4248 std::__iterator_category(__first
));
4253 * @brief Perform an operation on a sequence.
4254 * @param first An input iterator.
4255 * @param last An input iterator.
4256 * @param result An output iterator.
4257 * @param unary_op A unary operator.
4258 * @return An output iterator equal to @p result+(last-first).
4260 * Applies the operator to each element in the input range and assigns
4261 * the results to successive elements of the output sequence.
4262 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4263 * range @p [0,last-first).
4265 * @p unary_op must not alter its argument.
4267 template<typename _InputIterator
, typename _OutputIterator
,
4268 typename _UnaryOperation
>
4270 transform(_InputIterator __first
, _InputIterator __last
,
4271 _OutputIterator __result
, _UnaryOperation __unary_op
)
4273 // concept requirements
4274 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4275 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4276 // "the type returned by a _UnaryOperation"
4277 __typeof__(__unary_op(*__first
))>)
4278 __glibcxx_requires_valid_range(__first
, __last
);
4280 for (; __first
!= __last
; ++__first
, ++__result
)
4281 *__result
= __unary_op(*__first
);
4286 * @brief Perform an operation on corresponding elements of two sequences.
4287 * @param first1 An input iterator.
4288 * @param last1 An input iterator.
4289 * @param first2 An input iterator.
4290 * @param result An output iterator.
4291 * @param binary_op A binary operator.
4292 * @return An output iterator equal to @p result+(last-first).
4294 * Applies the operator to the corresponding elements in the two
4295 * input ranges and assigns the results to successive elements of the
4297 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4298 * @c N in the range @p [0,last1-first1).
4300 * @p binary_op must not alter either of its arguments.
4302 template<typename _InputIterator1
, typename _InputIterator2
,
4303 typename _OutputIterator
, typename _BinaryOperation
>
4305 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4306 _InputIterator2 __first2
, _OutputIterator __result
,
4307 _BinaryOperation __binary_op
)
4309 // concept requirements
4310 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4311 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4312 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4313 // "the type returned by a _BinaryOperation"
4314 __typeof__(__binary_op(*__first1
,*__first2
))>)
4315 __glibcxx_requires_valid_range(__first1
, __last1
);
4317 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4318 *__result
= __binary_op(*__first1
, *__first2
);
4323 * @brief Replace each occurrence of one value in a sequence with another
4325 * @param first A forward iterator.
4326 * @param last A forward iterator.
4327 * @param old_value The value to be replaced.
4328 * @param new_value The replacement value.
4329 * @return replace() returns no value.
4331 * For each iterator @c i in the range @p [first,last) if @c *i ==
4332 * @p old_value then the assignment @c *i = @p new_value is performed.
4334 template<typename _ForwardIterator
, typename _Tp
>
4336 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4337 const _Tp
& __old_value
, const _Tp
& __new_value
)
4339 // concept requirements
4340 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4342 __glibcxx_function_requires(_EqualOpConcept
<
4343 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4344 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4345 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4346 __glibcxx_requires_valid_range(__first
, __last
);
4348 for (; __first
!= __last
; ++__first
)
4349 if (*__first
== __old_value
)
4350 *__first
= __new_value
;
4354 * @brief Replace each value in a sequence for which a predicate returns
4355 * true with another value.
4356 * @param first A forward iterator.
4357 * @param last A forward iterator.
4358 * @param pred A predicate.
4359 * @param new_value The replacement value.
4360 * @return replace_if() returns no value.
4362 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4363 * is true then the assignment @c *i = @p new_value is performed.
4365 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4367 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4368 _Predicate __pred
, const _Tp
& __new_value
)
4370 // concept requirements
4371 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4373 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4374 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4375 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4376 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4377 __glibcxx_requires_valid_range(__first
, __last
);
4379 for (; __first
!= __last
; ++__first
)
4380 if (__pred(*__first
))
4381 *__first
= __new_value
;
4385 * @brief Assign the result of a function object to each value in a
4387 * @param first A forward iterator.
4388 * @param last A forward iterator.
4389 * @param gen A function object taking no arguments and returning
4390 * std::iterator_traits<_ForwardIterator>::value_type
4391 * @return generate() returns no value.
4393 * Performs the assignment @c *i = @p gen() for each @c i in the range
4396 template<typename _ForwardIterator
, typename _Generator
>
4398 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4401 // concept requirements
4402 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4403 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4404 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4405 __glibcxx_requires_valid_range(__first
, __last
);
4407 for (; __first
!= __last
; ++__first
)
4412 * @brief Assign the result of a function object to each value in a
4414 * @param first A forward iterator.
4415 * @param n The length of the sequence.
4416 * @param gen A function object taking no arguments and returning
4417 * std::iterator_traits<_ForwardIterator>::value_type
4418 * @return The end of the sequence, @p first+n
4420 * Performs the assignment @c *i = @p gen() for each @c i in the range
4421 * @p [first,first+n).
4423 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4425 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4427 // concept requirements
4428 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4429 // "the type returned by a _Generator"
4430 __typeof__(__gen())>)
4432 for (; __n
> 0; --__n
, ++__first
)
4439 * @brief Copy a sequence, removing consecutive duplicate values.
4440 * @param first An input iterator.
4441 * @param last An input iterator.
4442 * @param result An output iterator.
4443 * @return An iterator designating the end of the resulting sequence.
4445 * Copies each element in the range @p [first,last) to the range
4446 * beginning at @p result, except that only the first element is copied
4447 * from groups of consecutive elements that compare equal.
4448 * unique_copy() is stable, so the relative order of elements that are
4449 * copied is unchanged.
4451 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4452 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4454 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4455 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4458 template<typename _InputIterator
, typename _OutputIterator
>
4459 inline _OutputIterator
4460 unique_copy(_InputIterator __first
, _InputIterator __last
,
4461 _OutputIterator __result
)
4463 // concept requirements
4464 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4465 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4466 typename iterator_traits
<_InputIterator
>::value_type
>)
4467 __glibcxx_function_requires(_EqualityComparableConcept
<
4468 typename iterator_traits
<_InputIterator
>::value_type
>)
4469 __glibcxx_requires_valid_range(__first
, __last
);
4471 if (__first
== __last
)
4473 return std::__unique_copy(__first
, __last
, __result
,
4474 std::__iterator_category(__first
),
4475 std::__iterator_category(__result
));
4479 * @brief Copy a sequence, removing consecutive values using a predicate.
4480 * @param first An input iterator.
4481 * @param last An input iterator.
4482 * @param result An output iterator.
4483 * @param binary_pred A binary predicate.
4484 * @return An iterator designating the end of the resulting sequence.
4486 * Copies each element in the range @p [first,last) to the range
4487 * beginning at @p result, except that only the first element is copied
4488 * from groups of consecutive elements for which @p binary_pred returns
4490 * unique_copy() is stable, so the relative order of elements that are
4491 * copied is unchanged.
4493 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4494 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4496 template<typename _InputIterator
, typename _OutputIterator
,
4497 typename _BinaryPredicate
>
4498 inline _OutputIterator
4499 unique_copy(_InputIterator __first
, _InputIterator __last
,
4500 _OutputIterator __result
,
4501 _BinaryPredicate __binary_pred
)
4503 // concept requirements -- predicates checked later
4504 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4505 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4506 typename iterator_traits
<_InputIterator
>::value_type
>)
4507 __glibcxx_requires_valid_range(__first
, __last
);
4509 if (__first
== __last
)
4511 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4512 std::__iterator_category(__first
),
4513 std::__iterator_category(__result
));
4518 * @brief Randomly shuffle the elements of a sequence.
4519 * @param first A forward iterator.
4520 * @param last A forward iterator.
4523 * Reorder the elements in the range @p [first,last) using a random
4524 * distribution, so that every possible ordering of the sequence is
4527 template<typename _RandomAccessIterator
>
4529 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4531 // concept requirements
4532 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4533 _RandomAccessIterator
>)
4534 __glibcxx_requires_valid_range(__first
, __last
);
4536 if (__first
!= __last
)
4537 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4538 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4542 * @brief Shuffle the elements of a sequence using a random number
4544 * @param first A forward iterator.
4545 * @param last A forward iterator.
4546 * @param rand The RNG functor or function.
4549 * Reorders the elements in the range @p [first,last) using @p rand to
4550 * provide a random distribution. Calling @p rand(N) for a positive
4551 * integer @p N should return a randomly chosen integer from the
4554 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4556 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4557 _RandomNumberGenerator
& __rand
)
4559 // concept requirements
4560 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4561 _RandomAccessIterator
>)
4562 __glibcxx_requires_valid_range(__first
, __last
);
4564 if (__first
== __last
)
4566 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4567 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4572 * @brief Move elements for which a predicate is true to the beginning
4574 * @param first A forward iterator.
4575 * @param last A forward iterator.
4576 * @param pred A predicate functor.
4577 * @return An iterator @p middle such that @p pred(i) is true for each
4578 * iterator @p i in the range @p [first,middle) and false for each @p i
4579 * in the range @p [middle,last).
4581 * @p pred must not modify its operand. @p partition() does not preserve
4582 * the relative ordering of elements in each group, use
4583 * @p stable_partition() if this is needed.
4585 template<typename _ForwardIterator
, typename _Predicate
>
4586 inline _ForwardIterator
4587 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4590 // concept requirements
4591 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4593 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4594 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4595 __glibcxx_requires_valid_range(__first
, __last
);
4597 return std::__partition(__first
, __last
, __pred
,
4598 std::__iterator_category(__first
));
4604 * @brief Sort the smallest elements of a sequence.
4605 * @param first An iterator.
4606 * @param middle Another iterator.
4607 * @param last Another iterator.
4610 * Sorts the smallest @p (middle-first) elements in the range
4611 * @p [first,last) and moves them to the range @p [first,middle). The
4612 * order of the remaining elements in the range @p [middle,last) is
4614 * After the sort if @p i and @j are iterators in the range
4615 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4616 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4618 template<typename _RandomAccessIterator
>
4620 partial_sort(_RandomAccessIterator __first
,
4621 _RandomAccessIterator __middle
,
4622 _RandomAccessIterator __last
)
4624 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4627 // concept requirements
4628 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4629 _RandomAccessIterator
>)
4630 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4631 __glibcxx_requires_valid_range(__first
, __middle
);
4632 __glibcxx_requires_valid_range(__middle
, __last
);
4634 std::__heap_select(__first
, __middle
, __last
);
4635 std::sort_heap(__first
, __middle
);
4639 * @brief Sort the smallest elements of a sequence using a predicate
4641 * @param first An iterator.
4642 * @param middle Another iterator.
4643 * @param last Another iterator.
4644 * @param comp A comparison functor.
4647 * Sorts the smallest @p (middle-first) elements in the range
4648 * @p [first,last) and moves them to the range @p [first,middle). The
4649 * order of the remaining elements in the range @p [middle,last) is
4651 * After the sort if @p i and @j are iterators in the range
4652 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4653 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4656 template<typename _RandomAccessIterator
, typename _Compare
>
4658 partial_sort(_RandomAccessIterator __first
,
4659 _RandomAccessIterator __middle
,
4660 _RandomAccessIterator __last
,
4663 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4666 // concept requirements
4667 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4668 _RandomAccessIterator
>)
4669 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4670 _ValueType
, _ValueType
>)
4671 __glibcxx_requires_valid_range(__first
, __middle
);
4672 __glibcxx_requires_valid_range(__middle
, __last
);
4674 std::__heap_select(__first
, __middle
, __last
, __comp
);
4675 std::sort_heap(__first
, __middle
, __comp
);
4679 * @brief Sort a sequence just enough to find a particular position.
4680 * @param first An iterator.
4681 * @param nth Another iterator.
4682 * @param last Another iterator.
4685 * Rearranges the elements in the range @p [first,last) so that @p *nth
4686 * is the same element that would have been in that position had the
4687 * whole sequence been sorted.
4688 * whole sequence been sorted. The elements either side of @p *nth are
4689 * not completely sorted, but for any iterator @i in the range
4690 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4691 * holds that @p *j<*i is false.
4693 template<typename _RandomAccessIterator
>
4695 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4696 _RandomAccessIterator __last
)
4698 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4701 // concept requirements
4702 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4703 _RandomAccessIterator
>)
4704 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4705 __glibcxx_requires_valid_range(__first
, __nth
);
4706 __glibcxx_requires_valid_range(__nth
, __last
);
4708 if (__first
== __last
|| __nth
== __last
)
4711 std::__introselect(__first
, __nth
, __last
,
4712 std::__lg(__last
- __first
) * 2);
4716 * @brief Sort a sequence just enough to find a particular position
4717 * using a predicate for comparison.
4718 * @param first An iterator.
4719 * @param nth Another iterator.
4720 * @param last Another iterator.
4721 * @param comp A comparison functor.
4724 * Rearranges the elements in the range @p [first,last) so that @p *nth
4725 * is the same element that would have been in that position had the
4726 * whole sequence been sorted. The elements either side of @p *nth are
4727 * not completely sorted, but for any iterator @i in the range
4728 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4729 * holds that @p comp(*j,*i) is false.
4731 template<typename _RandomAccessIterator
, typename _Compare
>
4733 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4734 _RandomAccessIterator __last
, _Compare __comp
)
4736 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4739 // concept requirements
4740 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4741 _RandomAccessIterator
>)
4742 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4743 _ValueType
, _ValueType
>)
4744 __glibcxx_requires_valid_range(__first
, __nth
);
4745 __glibcxx_requires_valid_range(__nth
, __last
);
4747 if (__first
== __last
|| __nth
== __last
)
4750 std::__introselect(__first
, __nth
, __last
,
4751 std::__lg(__last
- __first
) * 2, __comp
);
4756 * @brief Sort the elements of a sequence.
4757 * @param first An iterator.
4758 * @param last Another iterator.
4761 * Sorts the elements in the range @p [first,last) in ascending order,
4762 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4763 * @p [first,last-1).
4765 * The relative ordering of equivalent elements is not preserved, use
4766 * @p stable_sort() if this is needed.
4768 template<typename _RandomAccessIterator
>
4770 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4772 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4775 // concept requirements
4776 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4777 _RandomAccessIterator
>)
4778 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4779 __glibcxx_requires_valid_range(__first
, __last
);
4781 if (__first
!= __last
)
4783 std::__introsort_loop(__first
, __last
,
4784 std::__lg(__last
- __first
) * 2);
4785 std::__final_insertion_sort(__first
, __last
);
4790 * @brief Sort the elements of a sequence using a predicate for comparison.
4791 * @param first An iterator.
4792 * @param last Another iterator.
4793 * @param comp A comparison functor.
4796 * Sorts the elements in the range @p [first,last) in ascending order,
4797 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4798 * range @p [first,last-1).
4800 * The relative ordering of equivalent elements is not preserved, use
4801 * @p stable_sort() if this is needed.
4803 template<typename _RandomAccessIterator
, typename _Compare
>
4805 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4808 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4811 // concept requirements
4812 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4813 _RandomAccessIterator
>)
4814 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
4816 __glibcxx_requires_valid_range(__first
, __last
);
4818 if (__first
!= __last
)
4820 std::__introsort_loop(__first
, __last
,
4821 std::__lg(__last
- __first
) * 2, __comp
);
4822 std::__final_insertion_sort(__first
, __last
, __comp
);
4827 * @brief Merges two sorted ranges.
4828 * @param first1 An iterator.
4829 * @param first2 Another iterator.
4830 * @param last1 Another iterator.
4831 * @param last2 Another iterator.
4832 * @param result An iterator pointing to the end of the merged range.
4833 * @return An iterator pointing to the first element "not less
4836 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4837 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4838 * must be sorted, and the output range must not overlap with either of
4839 * the input ranges. The sort is @e stable, that is, for equivalent
4840 * elements in the two ranges, elements from the first range will always
4841 * come before elements from the second.
4843 template<typename _InputIterator1
, typename _InputIterator2
,
4844 typename _OutputIterator
>
4846 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4847 _InputIterator2 __first2
, _InputIterator2 __last2
,
4848 _OutputIterator __result
)
4850 typedef typename iterator_traits
<_InputIterator1
>::value_type
4852 typedef typename iterator_traits
<_InputIterator2
>::value_type
4855 // concept requirements
4856 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4857 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4858 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4860 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4862 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4863 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4864 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4866 while (__first1
!= __last1
&& __first2
!= __last2
)
4868 if (*__first2
< *__first1
)
4870 *__result
= *__first2
;
4875 *__result
= *__first1
;
4880 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4885 * @brief Merges two sorted ranges.
4886 * @param first1 An iterator.
4887 * @param first2 Another iterator.
4888 * @param last1 Another iterator.
4889 * @param last2 Another iterator.
4890 * @param result An iterator pointing to the end of the merged range.
4891 * @param comp A functor to use for comparisons.
4892 * @return An iterator pointing to the first element "not less
4895 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4896 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4897 * must be sorted, and the output range must not overlap with either of
4898 * the input ranges. The sort is @e stable, that is, for equivalent
4899 * elements in the two ranges, elements from the first range will always
4900 * come before elements from the second.
4902 * The comparison function should have the same effects on ordering as
4903 * the function used for the initial sort.
4905 template<typename _InputIterator1
, typename _InputIterator2
,
4906 typename _OutputIterator
, typename _Compare
>
4908 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4909 _InputIterator2 __first2
, _InputIterator2 __last2
,
4910 _OutputIterator __result
, _Compare __comp
)
4912 typedef typename iterator_traits
<_InputIterator1
>::value_type
4914 typedef typename iterator_traits
<_InputIterator2
>::value_type
4917 // concept requirements
4918 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4919 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4920 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4922 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4924 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4925 _ValueType2
, _ValueType1
>)
4926 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4927 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4929 while (__first1
!= __last1
&& __first2
!= __last2
)
4931 if (__comp(*__first2
, *__first1
))
4933 *__result
= *__first2
;
4938 *__result
= *__first1
;
4943 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4949 * @brief Sort the elements of a sequence, preserving the relative order
4950 * of equivalent elements.
4951 * @param first An iterator.
4952 * @param last Another iterator.
4955 * Sorts the elements in the range @p [first,last) in ascending order,
4956 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4957 * @p [first,last-1).
4959 * The relative ordering of equivalent elements is preserved, so any two
4960 * elements @p x and @p y in the range @p [first,last) such that
4961 * @p x<y is false and @p y<x is false will have the same relative
4962 * ordering after calling @p stable_sort().
4964 template<typename _RandomAccessIterator
>
4966 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4968 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4970 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4973 // concept requirements
4974 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4975 _RandomAccessIterator
>)
4976 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4977 __glibcxx_requires_valid_range(__first
, __last
);
4979 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4981 if (__buf
.begin() == 0)
4982 std::__inplace_stable_sort(__first
, __last
);
4984 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4985 _DistanceType(__buf
.size()));
4989 * @brief Sort the elements of a sequence using a predicate for comparison,
4990 * preserving the relative order of equivalent elements.
4991 * @param first An iterator.
4992 * @param last Another iterator.
4993 * @param comp A comparison functor.
4996 * Sorts the elements in the range @p [first,last) in ascending order,
4997 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
4998 * range @p [first,last-1).
5000 * The relative ordering of equivalent elements is preserved, so any two
5001 * elements @p x and @p y in the range @p [first,last) such that
5002 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5003 * relative ordering after calling @p stable_sort().
5005 template<typename _RandomAccessIterator
, typename _Compare
>
5007 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5010 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5012 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5015 // concept requirements
5016 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5017 _RandomAccessIterator
>)
5018 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5021 __glibcxx_requires_valid_range(__first
, __last
);
5023 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5025 if (__buf
.begin() == 0)
5026 std::__inplace_stable_sort(__first
, __last
, __comp
);
5028 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5029 _DistanceType(__buf
.size()), __comp
);
5034 * @brief Return the union of two sorted ranges.
5035 * @param first1 Start of first range.
5036 * @param last1 End of first range.
5037 * @param first2 Start of second range.
5038 * @param last2 End of second range.
5039 * @return End of the output range.
5040 * @ingroup setoperations
5042 * This operation iterates over both ranges, copying elements present in
5043 * each range in order to the output range. Iterators increment for each
5044 * range. When the current element of one range is less than the other,
5045 * that element is copied and the iterator advanced. If an element is
5046 * contained in both ranges, the element from the first range is copied and
5047 * both ranges advance. The output range may not overlap either input
5050 template<typename _InputIterator1
, typename _InputIterator2
,
5051 typename _OutputIterator
>
5053 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5054 _InputIterator2 __first2
, _InputIterator2 __last2
,
5055 _OutputIterator __result
)
5057 typedef typename iterator_traits
<_InputIterator1
>::value_type
5059 typedef typename iterator_traits
<_InputIterator2
>::value_type
5062 // concept requirements
5063 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5064 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5065 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5067 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5069 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5070 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5071 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5072 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5074 while (__first1
!= __last1
&& __first2
!= __last2
)
5076 if (*__first1
< *__first2
)
5078 *__result
= *__first1
;
5081 else if (*__first2
< *__first1
)
5083 *__result
= *__first2
;
5088 *__result
= *__first1
;
5094 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5099 * @brief Return the union of two sorted ranges using a comparison functor.
5100 * @param first1 Start of first range.
5101 * @param last1 End of first range.
5102 * @param first2 Start of second range.
5103 * @param last2 End of second range.
5104 * @param comp The comparison functor.
5105 * @return End of the output range.
5106 * @ingroup setoperations
5108 * This operation iterates over both ranges, copying elements present in
5109 * each range in order to the output range. Iterators increment for each
5110 * range. When the current element of one range is less than the other
5111 * according to @a comp, that element is copied and the iterator advanced.
5112 * If an equivalent element according to @a comp is contained in both
5113 * ranges, the element from the first range is copied and both ranges
5114 * advance. The output range may not overlap either input range.
5116 template<typename _InputIterator1
, typename _InputIterator2
,
5117 typename _OutputIterator
, typename _Compare
>
5119 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5120 _InputIterator2 __first2
, _InputIterator2 __last2
,
5121 _OutputIterator __result
, _Compare __comp
)
5123 typedef typename iterator_traits
<_InputIterator1
>::value_type
5125 typedef typename iterator_traits
<_InputIterator2
>::value_type
5128 // concept requirements
5129 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5130 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5131 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5133 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5135 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5136 _ValueType1
, _ValueType2
>)
5137 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5138 _ValueType2
, _ValueType1
>)
5139 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5140 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5142 while (__first1
!= __last1
&& __first2
!= __last2
)
5144 if (__comp(*__first1
, *__first2
))
5146 *__result
= *__first1
;
5149 else if (__comp(*__first2
, *__first1
))
5151 *__result
= *__first2
;
5156 *__result
= *__first1
;
5162 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5167 * @brief Return the intersection of two sorted ranges.
5168 * @param first1 Start of first range.
5169 * @param last1 End of first range.
5170 * @param first2 Start of second range.
5171 * @param last2 End of second range.
5172 * @return End of the output range.
5173 * @ingroup setoperations
5175 * This operation iterates over both ranges, copying elements present in
5176 * both ranges in order to the output range. Iterators increment for each
5177 * range. When the current element of one range is less than the other,
5178 * that iterator advances. If an element is contained in both ranges, the
5179 * element from the first range is copied and both ranges advance. The
5180 * output range may not overlap either input range.
5182 template<typename _InputIterator1
, typename _InputIterator2
,
5183 typename _OutputIterator
>
5185 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5186 _InputIterator2 __first2
, _InputIterator2 __last2
,
5187 _OutputIterator __result
)
5189 typedef typename iterator_traits
<_InputIterator1
>::value_type
5191 typedef typename iterator_traits
<_InputIterator2
>::value_type
5194 // concept requirements
5195 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5196 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5197 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5199 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5200 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5201 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5202 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5204 while (__first1
!= __last1
&& __first2
!= __last2
)
5205 if (*__first1
< *__first2
)
5207 else if (*__first2
< *__first1
)
5211 *__result
= *__first1
;
5220 * @brief Return the intersection of two sorted ranges using comparison
5222 * @param first1 Start of first range.
5223 * @param last1 End of first range.
5224 * @param first2 Start of second range.
5225 * @param last2 End of second range.
5226 * @param comp The comparison functor.
5227 * @return End of the output range.
5228 * @ingroup setoperations
5230 * This operation iterates over both ranges, copying elements present in
5231 * both ranges in order to the output range. Iterators increment for each
5232 * range. When the current element of one range is less than the other
5233 * according to @a comp, that iterator advances. If an element is
5234 * contained in both ranges according to @a comp, the element from the
5235 * first range is copied and both ranges advance. The output range may not
5236 * overlap either input range.
5238 template<typename _InputIterator1
, typename _InputIterator2
,
5239 typename _OutputIterator
, typename _Compare
>
5241 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5242 _InputIterator2 __first2
, _InputIterator2 __last2
,
5243 _OutputIterator __result
, _Compare __comp
)
5245 typedef typename iterator_traits
<_InputIterator1
>::value_type
5247 typedef typename iterator_traits
<_InputIterator2
>::value_type
5250 // concept requirements
5251 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5252 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5253 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5255 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5256 _ValueType1
, _ValueType2
>)
5257 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5258 _ValueType2
, _ValueType1
>)
5259 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5260 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5262 while (__first1
!= __last1
&& __first2
!= __last2
)
5263 if (__comp(*__first1
, *__first2
))
5265 else if (__comp(*__first2
, *__first1
))
5269 *__result
= *__first1
;
5278 * @brief Return the difference of two sorted ranges.
5279 * @param first1 Start of first range.
5280 * @param last1 End of first range.
5281 * @param first2 Start of second range.
5282 * @param last2 End of second range.
5283 * @return End of the output range.
5284 * @ingroup setoperations
5286 * This operation iterates over both ranges, copying elements present in
5287 * the first range but not the second in order to the output range.
5288 * Iterators increment for each range. When the current element of the
5289 * first range is less than the second, that element is copied and the
5290 * iterator advances. If the current element of the second range is less,
5291 * the iterator advances, but no element is copied. If an element is
5292 * contained in both ranges, no elements are copied and both ranges
5293 * advance. The output range may not overlap either input range.
5295 template<typename _InputIterator1
, typename _InputIterator2
,
5296 typename _OutputIterator
>
5298 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5299 _InputIterator2 __first2
, _InputIterator2 __last2
,
5300 _OutputIterator __result
)
5302 typedef typename iterator_traits
<_InputIterator1
>::value_type
5304 typedef typename iterator_traits
<_InputIterator2
>::value_type
5307 // concept requirements
5308 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5309 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5310 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5312 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5313 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5314 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5315 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5317 while (__first1
!= __last1
&& __first2
!= __last2
)
5318 if (*__first1
< *__first2
)
5320 *__result
= *__first1
;
5324 else if (*__first2
< *__first1
)
5331 return std::copy(__first1
, __last1
, __result
);
5335 * @brief Return the difference of two sorted ranges using comparison
5337 * @param first1 Start of first range.
5338 * @param last1 End of first range.
5339 * @param first2 Start of second range.
5340 * @param last2 End of second range.
5341 * @param comp The comparison functor.
5342 * @return End of the output range.
5343 * @ingroup setoperations
5345 * This operation iterates over both ranges, copying elements present in
5346 * the first range but not the second in order to the output range.
5347 * Iterators increment for each range. When the current element of the
5348 * first range is less than the second according to @a comp, that element
5349 * is copied and the iterator advances. If the current element of the
5350 * second range is less, no element is copied and the iterator advances.
5351 * If an element is contained in both ranges according to @a comp, no
5352 * elements are copied and both ranges advance. The output range may not
5353 * overlap either input range.
5355 template<typename _InputIterator1
, typename _InputIterator2
,
5356 typename _OutputIterator
, typename _Compare
>
5358 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5359 _InputIterator2 __first2
, _InputIterator2 __last2
,
5360 _OutputIterator __result
, _Compare __comp
)
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(_BinaryPredicateConcept
<_Compare
,
5373 _ValueType1
, _ValueType2
>)
5374 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5375 _ValueType2
, _ValueType1
>)
5376 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5377 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5379 while (__first1
!= __last1
&& __first2
!= __last2
)
5380 if (__comp(*__first1
, *__first2
))
5382 *__result
= *__first1
;
5386 else if (__comp(*__first2
, *__first1
))
5393 return std::copy(__first1
, __last1
, __result
);
5397 * @brief Return the symmetric difference of two sorted ranges.
5398 * @param first1 Start of first range.
5399 * @param last1 End of first range.
5400 * @param first2 Start of second range.
5401 * @param last2 End of second range.
5402 * @return End of the output range.
5403 * @ingroup setoperations
5405 * This operation iterates over both ranges, copying elements present in
5406 * one range but not the other in order to the output range. Iterators
5407 * increment for each range. When the current element of one range is less
5408 * than the other, that element is copied and the iterator advances. If an
5409 * element is contained in both ranges, no elements are copied and both
5410 * ranges advance. The output range may not overlap either input range.
5412 template<typename _InputIterator1
, typename _InputIterator2
,
5413 typename _OutputIterator
>
5415 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5416 _InputIterator2 __first2
, _InputIterator2 __last2
,
5417 _OutputIterator __result
)
5419 typedef typename iterator_traits
<_InputIterator1
>::value_type
5421 typedef typename iterator_traits
<_InputIterator2
>::value_type
5424 // concept requirements
5425 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5426 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5427 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5429 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5431 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5432 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5433 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5434 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5436 while (__first1
!= __last1
&& __first2
!= __last2
)
5437 if (*__first1
< *__first2
)
5439 *__result
= *__first1
;
5443 else if (*__first2
< *__first1
)
5445 *__result
= *__first2
;
5454 return std::copy(__first2
, __last2
, std::copy(__first1
,
5455 __last1
, __result
));
5459 * @brief Return the symmetric difference of two sorted ranges using
5460 * comparison functor.
5461 * @param first1 Start of first range.
5462 * @param last1 End of first range.
5463 * @param first2 Start of second range.
5464 * @param last2 End of second range.
5465 * @param comp The comparison functor.
5466 * @return End of the output range.
5467 * @ingroup setoperations
5469 * This operation iterates over both ranges, copying elements present in
5470 * one range but not the other in order to the output range. Iterators
5471 * increment for each range. When the current element of one range is less
5472 * than the other according to @a comp, that element is copied and the
5473 * iterator advances. If an element is contained in both ranges according
5474 * to @a comp, no elements are copied and both ranges advance. The output
5475 * range may not overlap either input range.
5477 template<typename _InputIterator1
, typename _InputIterator2
,
5478 typename _OutputIterator
, typename _Compare
>
5480 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5481 _InputIterator2 __first2
, _InputIterator2 __last2
,
5482 _OutputIterator __result
,
5485 typedef typename iterator_traits
<_InputIterator1
>::value_type
5487 typedef typename iterator_traits
<_InputIterator2
>::value_type
5490 // concept requirements
5491 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5492 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5493 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5495 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5497 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5498 _ValueType1
, _ValueType2
>)
5499 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5500 _ValueType2
, _ValueType1
>)
5501 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5502 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5504 while (__first1
!= __last1
&& __first2
!= __last2
)
5505 if (__comp(*__first1
, *__first2
))
5507 *__result
= *__first1
;
5511 else if (__comp(*__first2
, *__first1
))
5513 *__result
= *__first2
;
5522 return std::copy(__first2
, __last2
,
5523 std::copy(__first1
, __last1
, __result
));
5528 * @brief Return the minimum element in a range.
5529 * @param first Start of range.
5530 * @param last End of range.
5531 * @return Iterator referencing the first instance of the smallest value.
5533 template<typename _ForwardIterator
>
5535 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5537 // concept requirements
5538 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5539 __glibcxx_function_requires(_LessThanComparableConcept
<
5540 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5541 __glibcxx_requires_valid_range(__first
, __last
);
5543 if (__first
== __last
)
5545 _ForwardIterator __result
= __first
;
5546 while (++__first
!= __last
)
5547 if (*__first
< *__result
)
5553 * @brief Return the minimum element in a range using comparison functor.
5554 * @param first Start of range.
5555 * @param last End of range.
5556 * @param comp Comparison functor.
5557 * @return Iterator referencing the first instance of the smallest value
5558 * according to comp.
5560 template<typename _ForwardIterator
, typename _Compare
>
5562 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5565 // concept requirements
5566 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5567 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5568 typename iterator_traits
<_ForwardIterator
>::value_type
,
5569 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5570 __glibcxx_requires_valid_range(__first
, __last
);
5572 if (__first
== __last
)
5574 _ForwardIterator __result
= __first
;
5575 while (++__first
!= __last
)
5576 if (__comp(*__first
, *__result
))
5582 * @brief Return the maximum element in a range.
5583 * @param first Start of range.
5584 * @param last End of range.
5585 * @return Iterator referencing the first instance of the largest value.
5587 template<typename _ForwardIterator
>
5589 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5591 // concept requirements
5592 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5593 __glibcxx_function_requires(_LessThanComparableConcept
<
5594 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5595 __glibcxx_requires_valid_range(__first
, __last
);
5597 if (__first
== __last
)
5599 _ForwardIterator __result
= __first
;
5600 while (++__first
!= __last
)
5601 if (*__result
< *__first
)
5607 * @brief Return the maximum element in a range using comparison functor.
5608 * @param first Start of range.
5609 * @param last End of range.
5610 * @param comp Comparison functor.
5611 * @return Iterator referencing the first instance of the largest value
5612 * according to comp.
5614 template<typename _ForwardIterator
, typename _Compare
>
5616 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5619 // concept requirements
5620 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5621 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5622 typename iterator_traits
<_ForwardIterator
>::value_type
,
5623 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5624 __glibcxx_requires_valid_range(__first
, __last
);
5626 if (__first
== __last
) return __first
;
5627 _ForwardIterator __result
= __first
;
5628 while (++__first
!= __last
)
5629 if (__comp(*__result
, *__first
))
5634 _GLIBCXX_END_NESTED_NAMESPACE
5636 #endif /* _STL_ALGO_H */