1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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/stl_heap.h>
67 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
68 #include <bits/algorithmfwd.h>
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
))
147 * This is an overload used by find() for the Input Iterator case.
150 template<typename _InputIterator
, typename _Tp
>
151 inline _InputIterator
152 __find(_InputIterator __first
, _InputIterator __last
,
153 const _Tp
& __val
, input_iterator_tag
)
155 while (__first
!= __last
&& !(*__first
== __val
))
162 * This is an overload used by find_if() for the Input Iterator case.
165 template<typename _InputIterator
, typename _Predicate
>
166 inline _InputIterator
167 __find_if(_InputIterator __first
, _InputIterator __last
,
168 _Predicate __pred
, input_iterator_tag
)
170 while (__first
!= __last
&& !bool(__pred(*__first
)))
177 * This is an overload used by find() for the RAI case.
180 template<typename _RandomAccessIterator
, typename _Tp
>
181 _RandomAccessIterator
182 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
183 const _Tp
& __val
, random_access_iterator_tag
)
185 typename iterator_traits
<_RandomAccessIterator
>::difference_type
186 __trip_count
= (__last
- __first
) >> 2;
188 for (; __trip_count
> 0; --__trip_count
)
190 if (*__first
== __val
)
194 if (*__first
== __val
)
198 if (*__first
== __val
)
202 if (*__first
== __val
)
207 switch (__last
- __first
)
210 if (*__first
== __val
)
214 if (*__first
== __val
)
218 if (*__first
== __val
)
229 * This is an overload used by find_if() for the RAI case.
232 template<typename _RandomAccessIterator
, typename _Predicate
>
233 _RandomAccessIterator
234 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
235 _Predicate __pred
, random_access_iterator_tag
)
237 typename iterator_traits
<_RandomAccessIterator
>::difference_type
238 __trip_count
= (__last
- __first
) >> 2;
240 for (; __trip_count
> 0; --__trip_count
)
242 if (__pred(*__first
))
246 if (__pred(*__first
))
250 if (__pred(*__first
))
254 if (__pred(*__first
))
259 switch (__last
- __first
)
262 if (__pred(*__first
))
266 if (__pred(*__first
))
270 if (__pred(*__first
))
281 // set_symmetric_difference
294 * This is an uglified
295 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
296 * overloaded for forward iterators.
299 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
301 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
302 _Integer __count
, const _Tp
& __val
,
303 std::forward_iterator_tag
)
305 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
306 while (__first
!= __last
)
308 typename iterator_traits
<_ForwardIterator
>::difference_type
310 _ForwardIterator __i
= __first
;
312 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
321 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
328 * This is an uglified
329 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
330 * overloaded for random access iterators.
333 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
335 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
336 _Integer __count
, const _Tp
& __val
,
337 std::random_access_iterator_tag
)
340 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
343 _DistanceType __tailSize
= __last
- __first
;
344 const _DistanceType __pattSize
= __count
;
346 if (__tailSize
< __pattSize
)
349 const _DistanceType __skipOffset
= __pattSize
- 1;
350 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
351 __tailSize
-= __pattSize
;
353 while (1) // the main loop...
355 // __lookAhead here is always pointing to the last element of next
357 while (!(*__lookAhead
== __val
)) // the skip loop...
359 if (__tailSize
< __pattSize
)
360 return __last
; // Failure
361 __lookAhead
+= __pattSize
;
362 __tailSize
-= __pattSize
;
364 _DistanceType __remainder
= __skipOffset
;
365 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
366 *__backTrack
== __val
; --__backTrack
)
368 if (--__remainder
== 0)
369 return (__lookAhead
- __skipOffset
); // Success
371 if (__remainder
> __tailSize
)
372 return __last
; // Failure
373 __lookAhead
+= __remainder
;
374 __tailSize
-= __remainder
;
382 * This is an uglified
383 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
385 * overloaded for forward iterators.
388 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
389 typename _BinaryPredicate
>
391 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
392 _Integer __count
, const _Tp
& __val
,
393 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
395 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
398 while (__first
!= __last
)
400 typename iterator_traits
<_ForwardIterator
>::difference_type
402 _ForwardIterator __i
= __first
;
404 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
414 while (__first
!= __last
415 && !bool(__binary_pred(*__first
, __val
)))
423 * This is an uglified
424 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
426 * overloaded for random access iterators.
429 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
430 typename _BinaryPredicate
>
432 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
433 _Integer __count
, const _Tp
& __val
,
434 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
437 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
440 _DistanceType __tailSize
= __last
- __first
;
441 const _DistanceType __pattSize
= __count
;
443 if (__tailSize
< __pattSize
)
446 const _DistanceType __skipOffset
= __pattSize
- 1;
447 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
448 __tailSize
-= __pattSize
;
450 while (1) // the main loop...
452 // __lookAhead here is always pointing to the last element of next
454 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
456 if (__tailSize
< __pattSize
)
457 return __last
; // Failure
458 __lookAhead
+= __pattSize
;
459 __tailSize
-= __pattSize
;
461 _DistanceType __remainder
= __skipOffset
;
462 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
463 __binary_pred(*__backTrack
, __val
); --__backTrack
)
465 if (--__remainder
== 0)
466 return (__lookAhead
- __skipOffset
); // Success
468 if (__remainder
> __tailSize
)
469 return __last
; // Failure
470 __lookAhead
+= __remainder
;
471 __tailSize
-= __remainder
;
475 // find_end for forward iterators.
476 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
478 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
479 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
480 forward_iterator_tag
, forward_iterator_tag
)
482 if (__first2
== __last2
)
486 _ForwardIterator1 __result
= __last1
;
489 _ForwardIterator1 __new_result
490 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
491 if (__new_result
== __last1
)
495 __result
= __new_result
;
496 __first1
= __new_result
;
503 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
504 typename _BinaryPredicate
>
506 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
507 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
508 forward_iterator_tag
, forward_iterator_tag
,
509 _BinaryPredicate __comp
)
511 if (__first2
== __last2
)
515 _ForwardIterator1 __result
= __last1
;
518 _ForwardIterator1 __new_result
519 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
521 if (__new_result
== __last1
)
525 __result
= __new_result
;
526 __first1
= __new_result
;
533 // find_end for bidirectional iterators (much faster).
534 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
535 _BidirectionalIterator1
536 __find_end(_BidirectionalIterator1 __first1
,
537 _BidirectionalIterator1 __last1
,
538 _BidirectionalIterator2 __first2
,
539 _BidirectionalIterator2 __last2
,
540 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
542 // concept requirements
543 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
544 _BidirectionalIterator1
>)
545 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
546 _BidirectionalIterator2
>)
548 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
549 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
551 _RevIterator1
__rlast1(__first1
);
552 _RevIterator2
__rlast2(__first2
);
553 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
555 _RevIterator2(__last2
),
558 if (__rresult
== __rlast1
)
562 _BidirectionalIterator1 __result
= __rresult
.base();
563 std::advance(__result
, -std::distance(__first2
, __last2
));
568 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
569 typename _BinaryPredicate
>
570 _BidirectionalIterator1
571 __find_end(_BidirectionalIterator1 __first1
,
572 _BidirectionalIterator1 __last1
,
573 _BidirectionalIterator2 __first2
,
574 _BidirectionalIterator2 __last2
,
575 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
576 _BinaryPredicate __comp
)
578 // concept requirements
579 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
580 _BidirectionalIterator1
>)
581 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
582 _BidirectionalIterator2
>)
584 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
585 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
587 _RevIterator1
__rlast1(__first1
);
588 _RevIterator2
__rlast2(__first2
);
589 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
590 _RevIterator2(__last2
), __rlast2
,
593 if (__rresult
== __rlast1
)
597 _BidirectionalIterator1 __result
= __rresult
.base();
598 std::advance(__result
, -std::distance(__first2
, __last2
));
604 * @brief Find last matching subsequence in a sequence.
605 * @param first1 Start of range to search.
606 * @param last1 End of range to search.
607 * @param first2 Start of sequence to match.
608 * @param last2 End of sequence to match.
609 * @return The last iterator @c i in the range
610 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
611 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
612 * such iterator exists.
614 * Searches the range @p [first1,last1) for a sub-sequence that compares
615 * equal value-by-value with the sequence given by @p [first2,last2) and
616 * returns an iterator to the first element of the sub-sequence, or
617 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
618 * last such subsequence contained in [first,last1).
620 * Because the sub-sequence must lie completely within the range
621 * @p [first1,last1) it must start at a position less than
622 * @p last1-(last2-first2) where @p last2-first2 is the length of the
624 * This means that the returned iterator @c i will be in the range
625 * @p [first1,last1-(last2-first2))
627 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
628 inline _ForwardIterator1
629 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
630 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
632 // concept requirements
633 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
634 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
635 __glibcxx_function_requires(_EqualOpConcept
<
636 typename iterator_traits
<_ForwardIterator1
>::value_type
,
637 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
638 __glibcxx_requires_valid_range(__first1
, __last1
);
639 __glibcxx_requires_valid_range(__first2
, __last2
);
641 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
642 std::__iterator_category(__first1
),
643 std::__iterator_category(__first2
));
647 * @brief Find last matching subsequence in a sequence using a predicate.
648 * @param first1 Start of range to search.
649 * @param last1 End of range to search.
650 * @param first2 Start of sequence to match.
651 * @param last2 End of sequence to match.
652 * @param comp The predicate to use.
653 * @return The last iterator @c i in the range
654 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
655 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
656 * @p last1 if no such iterator exists.
658 * Searches the range @p [first1,last1) for a sub-sequence that compares
659 * equal value-by-value with the sequence given by @p [first2,last2) using
660 * comp as a predicate and returns an iterator to the first element of the
661 * sub-sequence, or @p last1 if the sub-sequence is not found. The
662 * sub-sequence will be the last such subsequence contained in
665 * Because the sub-sequence must lie completely within the range
666 * @p [first1,last1) it must start at a position less than
667 * @p last1-(last2-first2) where @p last2-first2 is the length of the
669 * This means that the returned iterator @c i will be in the range
670 * @p [first1,last1-(last2-first2))
672 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
673 typename _BinaryPredicate
>
674 inline _ForwardIterator1
675 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
676 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
677 _BinaryPredicate __comp
)
679 // concept requirements
680 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
681 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
682 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
683 typename iterator_traits
<_ForwardIterator1
>::value_type
,
684 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
685 __glibcxx_requires_valid_range(__first1
, __last1
);
686 __glibcxx_requires_valid_range(__first2
, __last2
);
688 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
689 std::__iterator_category(__first1
),
690 std::__iterator_category(__first2
),
696 * @brief Copy a sequence, removing elements of a given value.
697 * @param first An input iterator.
698 * @param last An input iterator.
699 * @param result An output iterator.
700 * @param value The value to be removed.
701 * @return An iterator designating the end of the resulting sequence.
703 * Copies each element in the range @p [first,last) not equal to @p value
704 * to the range beginning at @p result.
705 * remove_copy() is stable, so the relative order of elements that are
706 * copied is unchanged.
708 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
710 remove_copy(_InputIterator __first
, _InputIterator __last
,
711 _OutputIterator __result
, const _Tp
& __value
)
713 // concept requirements
714 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
715 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
716 typename iterator_traits
<_InputIterator
>::value_type
>)
717 __glibcxx_function_requires(_EqualOpConcept
<
718 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
719 __glibcxx_requires_valid_range(__first
, __last
);
721 for (; __first
!= __last
; ++__first
)
722 if (!(*__first
== __value
))
724 *__result
= *__first
;
731 * @brief Copy a sequence, removing elements for which a predicate is true.
732 * @param first An input iterator.
733 * @param last An input iterator.
734 * @param result An output iterator.
735 * @param pred A predicate.
736 * @return An iterator designating the end of the resulting sequence.
738 * Copies each element in the range @p [first,last) for which
739 * @p pred returns true to the range beginning at @p result.
741 * remove_copy_if() is stable, so the relative order of elements that are
742 * copied is unchanged.
744 template<typename _InputIterator
, typename _OutputIterator
,
747 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
748 _OutputIterator __result
, _Predicate __pred
)
750 // concept requirements
751 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
752 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
753 typename iterator_traits
<_InputIterator
>::value_type
>)
754 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
755 typename iterator_traits
<_InputIterator
>::value_type
>)
756 __glibcxx_requires_valid_range(__first
, __last
);
758 for (; __first
!= __last
; ++__first
)
759 if (!bool(__pred(*__first
)))
761 *__result
= *__first
;
768 * @brief Remove elements from a sequence.
769 * @param first An input iterator.
770 * @param last An input iterator.
771 * @param value The value to be removed.
772 * @return An iterator designating the end of the resulting sequence.
774 * All elements equal to @p value are removed from the range
777 * remove() is stable, so the relative order of elements that are
778 * not removed is unchanged.
780 * Elements between the end of the resulting sequence and @p last
781 * are still present, but their value is unspecified.
783 template<typename _ForwardIterator
, typename _Tp
>
785 remove(_ForwardIterator __first
, _ForwardIterator __last
,
788 // concept requirements
789 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
791 __glibcxx_function_requires(_EqualOpConcept
<
792 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
793 __glibcxx_requires_valid_range(__first
, __last
);
795 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
796 if(__first
== __last
)
798 _ForwardIterator __result
= __first
;
800 for(; __first
!= __last
; ++__first
)
801 if(!(*__first
== __value
))
803 *__result
= _GLIBCXX_MOVE(*__first
);
810 * @brief Remove elements from a sequence using a predicate.
811 * @param first A forward iterator.
812 * @param last A forward iterator.
813 * @param pred A predicate.
814 * @return An iterator designating the end of the resulting sequence.
816 * All elements for which @p pred returns true are removed from the range
819 * remove_if() is stable, so the relative order of elements that are
820 * not removed is unchanged.
822 * Elements between the end of the resulting sequence and @p last
823 * are still present, but their value is unspecified.
825 template<typename _ForwardIterator
, typename _Predicate
>
827 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
830 // concept requirements
831 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
833 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
834 typename iterator_traits
<_ForwardIterator
>::value_type
>)
835 __glibcxx_requires_valid_range(__first
, __last
);
837 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
838 if(__first
== __last
)
840 _ForwardIterator __result
= __first
;
842 for(; __first
!= __last
; ++__first
)
843 if(!__pred(*__first
))
845 *__result
= _GLIBCXX_MOVE(*__first
);
852 * @brief Remove consecutive duplicate values from a sequence.
853 * @param first A forward iterator.
854 * @param last A forward iterator.
855 * @return An iterator designating the end of the resulting sequence.
857 * Removes all but the first element from each group of consecutive
858 * values that compare equal.
859 * unique() is stable, so the relative order of elements that are
860 * not removed is unchanged.
861 * Elements between the end of the resulting sequence and @p last
862 * are still present, but their value is unspecified.
864 template<typename _ForwardIterator
>
866 unique(_ForwardIterator __first
, _ForwardIterator __last
)
868 // concept requirements
869 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
871 __glibcxx_function_requires(_EqualityComparableConcept
<
872 typename iterator_traits
<_ForwardIterator
>::value_type
>)
873 __glibcxx_requires_valid_range(__first
, __last
);
875 // Skip the beginning, if already unique.
876 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
877 if (__first
== __last
)
880 // Do the real copy work.
881 _ForwardIterator __dest
= __first
;
883 while (++__first
!= __last
)
884 if (!(*__dest
== *__first
))
885 *++__dest
= _GLIBCXX_MOVE(*__first
);
890 * @brief Remove consecutive values from a sequence using a predicate.
891 * @param first A forward iterator.
892 * @param last A forward iterator.
893 * @param binary_pred A binary predicate.
894 * @return An iterator designating the end of the resulting sequence.
896 * Removes all but the first element from each group of consecutive
897 * values for which @p binary_pred returns true.
898 * unique() is stable, so the relative order of elements that are
899 * not removed is unchanged.
900 * Elements between the end of the resulting sequence and @p last
901 * are still present, but their value is unspecified.
903 template<typename _ForwardIterator
, typename _BinaryPredicate
>
905 unique(_ForwardIterator __first
, _ForwardIterator __last
,
906 _BinaryPredicate __binary_pred
)
908 // concept requirements
909 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
911 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
912 typename iterator_traits
<_ForwardIterator
>::value_type
,
913 typename iterator_traits
<_ForwardIterator
>::value_type
>)
914 __glibcxx_requires_valid_range(__first
, __last
);
916 // Skip the beginning, if already unique.
917 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
918 if (__first
== __last
)
921 // Do the real copy work.
922 _ForwardIterator __dest
= __first
;
924 while (++__first
!= __last
)
925 if (!bool(__binary_pred(*__dest
, *__first
)))
926 *++__dest
= _GLIBCXX_MOVE(*__first
);
932 * This is an uglified unique_copy(_InputIterator, _InputIterator,
934 * overloaded for forward iterators and output iterator as result.
937 template<typename _ForwardIterator
, typename _OutputIterator
>
939 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
940 _OutputIterator __result
,
941 forward_iterator_tag
, output_iterator_tag
)
943 // concept requirements -- taken care of in dispatching function
944 _ForwardIterator __next
= __first
;
945 *__result
= *__first
;
946 while (++__next
!= __last
)
947 if (!(*__first
== *__next
))
950 *++__result
= *__first
;
957 * This is an uglified unique_copy(_InputIterator, _InputIterator,
959 * overloaded for input iterators and output iterator as result.
962 template<typename _InputIterator
, typename _OutputIterator
>
964 __unique_copy(_InputIterator __first
, _InputIterator __last
,
965 _OutputIterator __result
,
966 input_iterator_tag
, output_iterator_tag
)
968 // concept requirements -- taken care of in dispatching function
969 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
971 while (++__first
!= __last
)
972 if (!(__value
== *__first
))
975 *++__result
= __value
;
982 * This is an uglified unique_copy(_InputIterator, _InputIterator,
984 * overloaded for input iterators and forward iterator as result.
987 template<typename _InputIterator
, typename _ForwardIterator
>
989 __unique_copy(_InputIterator __first
, _InputIterator __last
,
990 _ForwardIterator __result
,
991 input_iterator_tag
, forward_iterator_tag
)
993 // concept requirements -- taken care of in dispatching function
994 *__result
= *__first
;
995 while (++__first
!= __last
)
996 if (!(*__result
== *__first
))
997 *++__result
= *__first
;
1003 * This is an uglified
1004 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1006 * overloaded for forward iterators and output iterator as result.
1009 template<typename _ForwardIterator
, typename _OutputIterator
,
1010 typename _BinaryPredicate
>
1012 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1013 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1014 forward_iterator_tag
, output_iterator_tag
)
1016 // concept requirements -- iterators already checked
1017 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1018 typename iterator_traits
<_ForwardIterator
>::value_type
,
1019 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1021 _ForwardIterator __next
= __first
;
1022 *__result
= *__first
;
1023 while (++__next
!= __last
)
1024 if (!bool(__binary_pred(*__first
, *__next
)))
1027 *++__result
= *__first
;
1034 * This is an uglified
1035 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1037 * overloaded for input iterators and output iterator as result.
1040 template<typename _InputIterator
, typename _OutputIterator
,
1041 typename _BinaryPredicate
>
1043 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1044 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1045 input_iterator_tag
, output_iterator_tag
)
1047 // concept requirements -- iterators already checked
1048 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1049 typename iterator_traits
<_InputIterator
>::value_type
,
1050 typename iterator_traits
<_InputIterator
>::value_type
>)
1052 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1053 *__result
= __value
;
1054 while (++__first
!= __last
)
1055 if (!bool(__binary_pred(__value
, *__first
)))
1058 *++__result
= __value
;
1065 * This is an uglified
1066 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1068 * overloaded for input iterators and forward iterator as result.
1071 template<typename _InputIterator
, typename _ForwardIterator
,
1072 typename _BinaryPredicate
>
1074 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1075 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1076 input_iterator_tag
, forward_iterator_tag
)
1078 // concept requirements -- iterators already checked
1079 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1080 typename iterator_traits
<_ForwardIterator
>::value_type
,
1081 typename iterator_traits
<_InputIterator
>::value_type
>)
1083 *__result
= *__first
;
1084 while (++__first
!= __last
)
1085 if (!bool(__binary_pred(*__result
, *__first
)))
1086 *++__result
= *__first
;
1092 * This is an uglified reverse(_BidirectionalIterator,
1093 * _BidirectionalIterator)
1094 * overloaded for bidirectional iterators.
1097 template<typename _BidirectionalIterator
>
1099 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1100 bidirectional_iterator_tag
)
1103 if (__first
== __last
|| __first
== --__last
)
1107 std::iter_swap(__first
, __last
);
1114 * This is an uglified reverse(_BidirectionalIterator,
1115 * _BidirectionalIterator)
1116 * overloaded for random access iterators.
1119 template<typename _RandomAccessIterator
>
1121 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1122 random_access_iterator_tag
)
1124 if (__first
== __last
)
1127 while (__first
< __last
)
1129 std::iter_swap(__first
, __last
);
1136 * @brief Reverse a sequence.
1137 * @param first A bidirectional iterator.
1138 * @param last A bidirectional iterator.
1139 * @return reverse() returns no value.
1141 * Reverses the order of the elements in the range @p [first,last),
1142 * so that the first element becomes the last etc.
1143 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1144 * swaps @p *(first+i) and @p *(last-(i+1))
1146 template<typename _BidirectionalIterator
>
1148 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1150 // concept requirements
1151 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1152 _BidirectionalIterator
>)
1153 __glibcxx_requires_valid_range(__first
, __last
);
1154 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1158 * @brief Copy a sequence, reversing its elements.
1159 * @param first A bidirectional iterator.
1160 * @param last A bidirectional iterator.
1161 * @param result An output iterator.
1162 * @return An iterator designating the end of the resulting sequence.
1164 * Copies the elements in the range @p [first,last) to the range
1165 * @p [result,result+(last-first)) such that the order of the
1166 * elements is reversed.
1167 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1168 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1169 * The ranges @p [first,last) and @p [result,result+(last-first))
1172 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1174 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1175 _OutputIterator __result
)
1177 // concept requirements
1178 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1179 _BidirectionalIterator
>)
1180 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1181 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1182 __glibcxx_requires_valid_range(__first
, __last
);
1184 while (__first
!= __last
)
1187 *__result
= *__last
;
1195 * This is a helper function for the rotate algorithm specialized on RAIs.
1196 * It returns the greatest common divisor of two integer values.
1199 template<typename _EuclideanRingElement
>
1200 _EuclideanRingElement
1201 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1205 _EuclideanRingElement __t
= __m
% __n
;
1214 * This is a helper function for the rotate algorithm.
1217 template<typename _ForwardIterator
>
1219 __rotate(_ForwardIterator __first
,
1220 _ForwardIterator __middle
,
1221 _ForwardIterator __last
,
1222 forward_iterator_tag
)
1224 if (__first
== __middle
|| __last
== __middle
)
1227 _ForwardIterator __first2
= __middle
;
1230 std::iter_swap(__first
, __first2
);
1233 if (__first
== __middle
)
1234 __middle
= __first2
;
1236 while (__first2
!= __last
);
1238 __first2
= __middle
;
1240 while (__first2
!= __last
)
1242 std::iter_swap(__first
, __first2
);
1245 if (__first
== __middle
)
1246 __middle
= __first2
;
1247 else if (__first2
== __last
)
1248 __first2
= __middle
;
1254 * This is a helper function for the rotate algorithm.
1257 template<typename _BidirectionalIterator
>
1259 __rotate(_BidirectionalIterator __first
,
1260 _BidirectionalIterator __middle
,
1261 _BidirectionalIterator __last
,
1262 bidirectional_iterator_tag
)
1264 // concept requirements
1265 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1266 _BidirectionalIterator
>)
1268 if (__first
== __middle
|| __last
== __middle
)
1271 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1272 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1274 while (__first
!= __middle
&& __middle
!= __last
)
1276 std::iter_swap(__first
, --__last
);
1280 if (__first
== __middle
)
1281 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1283 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1288 * This is a helper function for the rotate algorithm.
1291 template<typename _RandomAccessIterator
>
1293 __rotate(_RandomAccessIterator __first
,
1294 _RandomAccessIterator __middle
,
1295 _RandomAccessIterator __last
,
1296 random_access_iterator_tag
)
1298 // concept requirements
1299 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1300 _RandomAccessIterator
>)
1302 if (__first
== __middle
|| __last
== __middle
)
1305 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1307 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1310 const _Distance __n
= __last
- __first
;
1311 const _Distance __k
= __middle
- __first
;
1312 const _Distance __l
= __n
- __k
;
1316 std::swap_ranges(__first
, __middle
, __middle
);
1320 const _Distance __d
= std::__gcd(__n
, __k
);
1322 for (_Distance __i
= 0; __i
< __d
; __i
++)
1324 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1325 _RandomAccessIterator __p
= __first
;
1329 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1331 if (__p
> __first
+ __l
)
1333 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1337 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1343 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1345 if (__p
< __last
- __k
)
1347 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1350 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1355 *__p
= _GLIBCXX_MOVE(__tmp
);
1361 * @brief Rotate the elements of a sequence.
1362 * @param first A forward iterator.
1363 * @param middle A forward iterator.
1364 * @param last A forward iterator.
1367 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1368 * positions so that the element at @p middle is moved to @p first, the
1369 * element at @p middle+1 is moved to @first+1 and so on for each element
1370 * in the range @p [first,last).
1372 * This effectively swaps the ranges @p [first,middle) and
1375 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1376 * each @p n in the range @p [0,last-first).
1378 template<typename _ForwardIterator
>
1380 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1381 _ForwardIterator __last
)
1383 // concept requirements
1384 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1386 __glibcxx_requires_valid_range(__first
, __middle
);
1387 __glibcxx_requires_valid_range(__middle
, __last
);
1389 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1391 std::__rotate(__first
, __middle
, __last
, _IterType());
1395 * @brief Copy a sequence, rotating its elements.
1396 * @param first A forward iterator.
1397 * @param middle A forward iterator.
1398 * @param last A forward iterator.
1399 * @param result An output iterator.
1400 * @return An iterator designating the end of the resulting sequence.
1402 * Copies the elements of the range @p [first,last) to the range
1403 * beginning at @result, rotating the copied elements by @p (middle-first)
1404 * positions so that the element at @p middle is moved to @p result, the
1405 * element at @p middle+1 is moved to @result+1 and so on for each element
1406 * in the range @p [first,last).
1408 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1409 * each @p n in the range @p [0,last-first).
1411 template<typename _ForwardIterator
, typename _OutputIterator
>
1413 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1414 _ForwardIterator __last
, _OutputIterator __result
)
1416 // concept requirements
1417 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1418 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1419 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1420 __glibcxx_requires_valid_range(__first
, __middle
);
1421 __glibcxx_requires_valid_range(__middle
, __last
);
1423 return std::copy(__first
, __middle
,
1424 std::copy(__middle
, __last
, __result
));
1429 * This is a helper function...
1432 template<typename _ForwardIterator
, typename _Predicate
>
1434 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1435 _Predicate __pred
, forward_iterator_tag
)
1437 if (__first
== __last
)
1440 while (__pred(*__first
))
1441 if (++__first
== __last
)
1444 _ForwardIterator __next
= __first
;
1446 while (++__next
!= __last
)
1447 if (__pred(*__next
))
1449 std::iter_swap(__first
, __next
);
1458 * This is a helper function...
1461 template<typename _BidirectionalIterator
, typename _Predicate
>
1462 _BidirectionalIterator
1463 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1464 _Predicate __pred
, bidirectional_iterator_tag
)
1469 if (__first
== __last
)
1471 else if (__pred(*__first
))
1477 if (__first
== __last
)
1479 else if (!bool(__pred(*__last
)))
1483 std::iter_swap(__first
, __last
);
1492 * This is a helper function...
1495 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1497 __inplace_stable_partition(_ForwardIterator __first
,
1498 _ForwardIterator __last
,
1499 _Predicate __pred
, _Distance __len
)
1502 return __pred(*__first
) ? __last
: __first
;
1503 _ForwardIterator __middle
= __first
;
1504 std::advance(__middle
, __len
/ 2);
1505 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1509 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1513 std::rotate(__begin
, __middle
, __end
);
1514 std::advance(__begin
, std::distance(__middle
, __end
));
1520 * This is a helper function...
1523 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1526 __stable_partition_adaptive(_ForwardIterator __first
,
1527 _ForwardIterator __last
,
1528 _Predicate __pred
, _Distance __len
,
1530 _Distance __buffer_size
)
1532 if (__len
<= __buffer_size
)
1534 _ForwardIterator __result1
= __first
;
1535 _Pointer __result2
= __buffer
;
1536 for (; __first
!= __last
; ++__first
)
1537 if (__pred(*__first
))
1539 *__result1
= *__first
;
1544 *__result2
= *__first
;
1547 std::copy(__buffer
, __result2
, __result1
);
1552 _ForwardIterator __middle
= __first
;
1553 std::advance(__middle
, __len
/ 2);
1554 _ForwardIterator __begin
=
1555 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1556 __len
/ 2, __buffer
,
1558 _ForwardIterator __end
=
1559 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1561 __buffer
, __buffer_size
);
1562 std::rotate(__begin
, __middle
, __end
);
1563 std::advance(__begin
, std::distance(__middle
, __end
));
1569 * @brief Move elements for which a predicate is true to the beginning
1570 * of a sequence, preserving relative ordering.
1571 * @param first A forward iterator.
1572 * @param last A forward iterator.
1573 * @param pred A predicate functor.
1574 * @return An iterator @p middle such that @p pred(i) is true for each
1575 * iterator @p i in the range @p [first,middle) and false for each @p i
1576 * in the range @p [middle,last).
1578 * Performs the same function as @p partition() with the additional
1579 * guarantee that the relative ordering of elements in each group is
1580 * preserved, so any two elements @p x and @p y in the range
1581 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1582 * relative ordering after calling @p stable_partition().
1584 template<typename _ForwardIterator
, typename _Predicate
>
1586 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1589 // concept requirements
1590 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1592 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1593 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1594 __glibcxx_requires_valid_range(__first
, __last
);
1596 if (__first
== __last
)
1600 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1602 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1605 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1607 if (__buf
.size() > 0)
1609 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1610 _DistanceType(__buf
.requested_size()),
1612 _DistanceType(__buf
.size()));
1615 std::__inplace_stable_partition(__first
, __last
, __pred
,
1616 _DistanceType(__buf
.requested_size()));
1622 * This is a helper function for the sort routines.
1625 template<typename _RandomAccessIterator
>
1627 __heap_select(_RandomAccessIterator __first
,
1628 _RandomAccessIterator __middle
,
1629 _RandomAccessIterator __last
)
1631 std::make_heap(__first
, __middle
);
1632 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1633 if (*__i
< *__first
)
1634 std::__pop_heap(__first
, __middle
, __i
);
1639 * This is a helper function for the sort routines.
1642 template<typename _RandomAccessIterator
, typename _Compare
>
1644 __heap_select(_RandomAccessIterator __first
,
1645 _RandomAccessIterator __middle
,
1646 _RandomAccessIterator __last
, _Compare __comp
)
1648 std::make_heap(__first
, __middle
, __comp
);
1649 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1650 if (__comp(*__i
, *__first
))
1651 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1657 * @brief Copy the smallest elements of a sequence.
1658 * @param first An iterator.
1659 * @param last Another iterator.
1660 * @param result_first A random-access iterator.
1661 * @param result_last Another random-access iterator.
1662 * @return An iterator indicating the end of the resulting sequence.
1664 * Copies and sorts the smallest N values from the range @p [first,last)
1665 * to the range beginning at @p result_first, where the number of
1666 * elements to be copied, @p N, is the smaller of @p (last-first) and
1667 * @p (result_last-result_first).
1668 * After the sort if @p i and @j are iterators in the range
1669 * @p [result_first,result_first+N) such that @i precedes @j then
1670 * @p *j<*i is false.
1671 * The value returned is @p result_first+N.
1673 template<typename _InputIterator
, typename _RandomAccessIterator
>
1674 _RandomAccessIterator
1675 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1676 _RandomAccessIterator __result_first
,
1677 _RandomAccessIterator __result_last
)
1679 typedef typename iterator_traits
<_InputIterator
>::value_type
1681 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1683 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1686 // concept requirements
1687 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1688 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1690 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1692 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1693 __glibcxx_requires_valid_range(__first
, __last
);
1694 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1696 if (__result_first
== __result_last
)
1697 return __result_last
;
1698 _RandomAccessIterator __result_real_last
= __result_first
;
1699 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1701 *__result_real_last
= *__first
;
1702 ++__result_real_last
;
1705 std::make_heap(__result_first
, __result_real_last
);
1706 while (__first
!= __last
)
1708 if (*__first
< *__result_first
)
1709 std::__adjust_heap(__result_first
, _DistanceType(0),
1710 _DistanceType(__result_real_last
1712 _InputValueType(*__first
));
1715 std::sort_heap(__result_first
, __result_real_last
);
1716 return __result_real_last
;
1720 * @brief Copy the smallest elements of a sequence using a predicate for
1722 * @param first An input iterator.
1723 * @param last Another input iterator.
1724 * @param result_first A random-access iterator.
1725 * @param result_last Another random-access iterator.
1726 * @param comp A comparison functor.
1727 * @return An iterator indicating the end of the resulting sequence.
1729 * Copies and sorts the smallest N values from the range @p [first,last)
1730 * to the range beginning at @p result_first, where the number of
1731 * elements to be copied, @p N, is the smaller of @p (last-first) and
1732 * @p (result_last-result_first).
1733 * After the sort if @p i and @j are iterators in the range
1734 * @p [result_first,result_first+N) such that @i precedes @j then
1735 * @p comp(*j,*i) is false.
1736 * The value returned is @p result_first+N.
1738 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1739 _RandomAccessIterator
1740 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1741 _RandomAccessIterator __result_first
,
1742 _RandomAccessIterator __result_last
,
1745 typedef typename iterator_traits
<_InputIterator
>::value_type
1747 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1749 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1752 // concept requirements
1753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1754 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1755 _RandomAccessIterator
>)
1756 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1758 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1759 _InputValueType
, _OutputValueType
>)
1760 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1761 _OutputValueType
, _OutputValueType
>)
1762 __glibcxx_requires_valid_range(__first
, __last
);
1763 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1765 if (__result_first
== __result_last
)
1766 return __result_last
;
1767 _RandomAccessIterator __result_real_last
= __result_first
;
1768 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1770 *__result_real_last
= *__first
;
1771 ++__result_real_last
;
1774 std::make_heap(__result_first
, __result_real_last
, __comp
);
1775 while (__first
!= __last
)
1777 if (__comp(*__first
, *__result_first
))
1778 std::__adjust_heap(__result_first
, _DistanceType(0),
1779 _DistanceType(__result_real_last
1781 _InputValueType(*__first
),
1785 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1786 return __result_real_last
;
1791 * This is a helper function for the sort routine.
1794 template<typename _RandomAccessIterator
, typename _Tp
>
1796 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1798 _RandomAccessIterator __next
= __last
;
1800 while (__val
< *__next
)
1811 * This is a helper function for the sort routine.
1814 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1816 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1819 _RandomAccessIterator __next
= __last
;
1821 while (__comp(__val
, *__next
))
1832 * This is a helper function for the sort routine.
1835 template<typename _RandomAccessIterator
>
1837 __insertion_sort(_RandomAccessIterator __first
,
1838 _RandomAccessIterator __last
)
1840 if (__first
== __last
)
1843 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1845 typename iterator_traits
<_RandomAccessIterator
>::value_type
1847 if (__val
< *__first
)
1849 std::copy_backward(__first
, __i
, __i
+ 1);
1853 std::__unguarded_linear_insert(__i
, __val
);
1859 * This is a helper function for the sort routine.
1862 template<typename _RandomAccessIterator
, typename _Compare
>
1864 __insertion_sort(_RandomAccessIterator __first
,
1865 _RandomAccessIterator __last
, _Compare __comp
)
1867 if (__first
== __last
) return;
1869 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1871 typename iterator_traits
<_RandomAccessIterator
>::value_type
1873 if (__comp(__val
, *__first
))
1875 std::copy_backward(__first
, __i
, __i
+ 1);
1879 std::__unguarded_linear_insert(__i
, __val
, __comp
);
1885 * This is a helper function for the sort routine.
1888 template<typename _RandomAccessIterator
>
1890 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1891 _RandomAccessIterator __last
)
1893 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1896 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1897 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
1902 * This is a helper function for the sort routine.
1905 template<typename _RandomAccessIterator
, typename _Compare
>
1907 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1908 _RandomAccessIterator __last
, _Compare __comp
)
1910 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1913 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1914 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
1920 * This controls some aspect of the sort routines.
1923 enum { _S_threshold
= 16 };
1927 * This is a helper function for the sort routine.
1930 template<typename _RandomAccessIterator
>
1932 __final_insertion_sort(_RandomAccessIterator __first
,
1933 _RandomAccessIterator __last
)
1935 if (__last
- __first
> int(_S_threshold
))
1937 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
1938 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
1941 std::__insertion_sort(__first
, __last
);
1946 * This is a helper function for the sort routine.
1949 template<typename _RandomAccessIterator
, typename _Compare
>
1951 __final_insertion_sort(_RandomAccessIterator __first
,
1952 _RandomAccessIterator __last
, _Compare __comp
)
1954 if (__last
- __first
> int(_S_threshold
))
1956 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1957 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1961 std::__insertion_sort(__first
, __last
, __comp
);
1966 * This is a helper function...
1969 template<typename _RandomAccessIterator
, typename _Tp
>
1970 _RandomAccessIterator
1971 __unguarded_partition(_RandomAccessIterator __first
,
1972 _RandomAccessIterator __last
, _Tp __pivot
)
1976 while (*__first
< __pivot
)
1979 while (__pivot
< *__last
)
1981 if (!(__first
< __last
))
1983 std::iter_swap(__first
, __last
);
1990 * This is a helper function...
1993 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1994 _RandomAccessIterator
1995 __unguarded_partition(_RandomAccessIterator __first
,
1996 _RandomAccessIterator __last
,
1997 _Tp __pivot
, _Compare __comp
)
2001 while (__comp(*__first
, __pivot
))
2004 while (__comp(__pivot
, *__last
))
2006 if (!(__first
< __last
))
2008 std::iter_swap(__first
, __last
);
2015 * This is a helper function for the sort routine.
2018 template<typename _RandomAccessIterator
, typename _Size
>
2020 __introsort_loop(_RandomAccessIterator __first
,
2021 _RandomAccessIterator __last
,
2022 _Size __depth_limit
)
2024 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2027 while (__last
- __first
> int(_S_threshold
))
2029 if (__depth_limit
== 0)
2031 _GLIBCXX_STD_P
:partial_sort(__first
, __last
, __last
);
2035 _RandomAccessIterator __cut
=
2036 std::__unguarded_partition(__first
, __last
,
2037 _ValueType(std::__median(*__first
,
2044 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2051 * This is a helper function for the sort routine.
2054 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2056 __introsort_loop(_RandomAccessIterator __first
,
2057 _RandomAccessIterator __last
,
2058 _Size __depth_limit
, _Compare __comp
)
2060 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2063 while (__last
- __first
> int(_S_threshold
))
2065 if (__depth_limit
== 0)
2067 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2071 _RandomAccessIterator __cut
=
2072 std::__unguarded_partition(__first
, __last
,
2073 _ValueType(std::__median(*__first
,
2081 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2088 * This is a helper function for the sort routines. Precondition: __n > 0.
2091 template<typename _Size
>
2096 for (__k
= 0; __n
!= 0; __n
>>= 1)
2103 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2107 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2111 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2115 template<typename _RandomAccessIterator
, typename _Size
>
2117 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2118 _RandomAccessIterator __last
, _Size __depth_limit
)
2120 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2123 while (__last
- __first
> 3)
2125 if (__depth_limit
== 0)
2127 std::__heap_select(__first
, __nth
+ 1, __last
);
2128 // Place the nth largest element in its final position.
2129 std::iter_swap(__first
, __nth
);
2133 _RandomAccessIterator __cut
=
2134 std::__unguarded_partition(__first
, __last
,
2135 _ValueType(std::__median(*__first
,
2147 std::__insertion_sort(__first
, __last
);
2150 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2152 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2153 _RandomAccessIterator __last
, _Size __depth_limit
,
2156 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2159 while (__last
- __first
> 3)
2161 if (__depth_limit
== 0)
2163 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2164 // Place the nth largest element in its final position.
2165 std::iter_swap(__first
, __nth
);
2169 _RandomAccessIterator __cut
=
2170 std::__unguarded_partition(__first
, __last
,
2171 _ValueType(std::__median(*__first
,
2184 std::__insertion_sort(__first
, __last
, __comp
);
2190 * @brief Finds the first position in which @a val could be inserted
2191 * without changing the ordering.
2192 * @param first An iterator.
2193 * @param last Another iterator.
2194 * @param val The search term.
2195 * @return An iterator pointing to the first element "not less
2196 * than" @a val, or end() if every element is less than
2198 * @ingroup binarysearch
2200 template<typename _ForwardIterator
, typename _Tp
>
2202 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2205 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2207 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2210 // concept requirements
2211 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2212 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2213 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2215 _DistanceType __len
= std::distance(__first
, __last
);
2216 _DistanceType __half
;
2217 _ForwardIterator __middle
;
2221 __half
= __len
>> 1;
2223 std::advance(__middle
, __half
);
2224 if (*__middle
< __val
)
2228 __len
= __len
- __half
- 1;
2237 * @brief Finds the first position in which @a val could be inserted
2238 * without changing the ordering.
2239 * @param first An iterator.
2240 * @param last Another iterator.
2241 * @param val The search term.
2242 * @param comp A functor to use for comparisons.
2243 * @return An iterator pointing to the first element "not less than" @a val,
2244 * or end() if every element is less than @a val.
2245 * @ingroup binarysearch
2247 * The comparison function should have the same effects on ordering as
2248 * the function used for the initial sort.
2250 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2252 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2253 const _Tp
& __val
, _Compare __comp
)
2255 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2257 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2260 // concept requirements
2261 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2262 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2264 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2267 _DistanceType __len
= std::distance(__first
, __last
);
2268 _DistanceType __half
;
2269 _ForwardIterator __middle
;
2273 __half
= __len
>> 1;
2275 std::advance(__middle
, __half
);
2276 if (__comp(*__middle
, __val
))
2280 __len
= __len
- __half
- 1;
2289 * @brief Finds the last position in which @a val could be inserted
2290 * without changing the ordering.
2291 * @param first An iterator.
2292 * @param last Another iterator.
2293 * @param val The search term.
2294 * @return An iterator pointing to the first element greater than @a val,
2295 * or end() if no elements are greater than @a val.
2296 * @ingroup binarysearch
2298 template<typename _ForwardIterator
, typename _Tp
>
2300 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2303 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2305 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2308 // concept requirements
2309 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2310 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2311 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2313 _DistanceType __len
= std::distance(__first
, __last
);
2314 _DistanceType __half
;
2315 _ForwardIterator __middle
;
2319 __half
= __len
>> 1;
2321 std::advance(__middle
, __half
);
2322 if (__val
< *__middle
)
2328 __len
= __len
- __half
- 1;
2335 * @brief Finds the last position in which @a val could be inserted
2336 * without changing the ordering.
2337 * @param first An iterator.
2338 * @param last Another iterator.
2339 * @param val The search term.
2340 * @param comp A functor to use for comparisons.
2341 * @return An iterator pointing to the first element greater than @a val,
2342 * or end() if no elements are greater than @a val.
2343 * @ingroup binarysearch
2345 * The comparison function should have the same effects on ordering as
2346 * the function used for the initial sort.
2348 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2350 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2351 const _Tp
& __val
, _Compare __comp
)
2353 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2355 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2358 // concept requirements
2359 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2360 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2362 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2365 _DistanceType __len
= std::distance(__first
, __last
);
2366 _DistanceType __half
;
2367 _ForwardIterator __middle
;
2371 __half
= __len
>> 1;
2373 std::advance(__middle
, __half
);
2374 if (__comp(__val
, *__middle
))
2380 __len
= __len
- __half
- 1;
2387 * @brief Finds the largest subrange in which @a val could be inserted
2388 * at any place in it without changing the ordering.
2389 * @param first An iterator.
2390 * @param last Another iterator.
2391 * @param val The search term.
2392 * @return An pair of iterators defining the subrange.
2393 * @ingroup binarysearch
2395 * This is equivalent to
2397 * std::make_pair(lower_bound(first, last, val),
2398 * upper_bound(first, last, val))
2400 * but does not actually call those functions.
2402 template<typename _ForwardIterator
, typename _Tp
>
2403 pair
<_ForwardIterator
, _ForwardIterator
>
2404 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2407 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2409 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2412 // concept requirements
2413 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2414 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2415 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2416 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2417 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2419 _DistanceType __len
= std::distance(__first
, __last
);
2420 _DistanceType __half
;
2421 _ForwardIterator __middle
, __left
, __right
;
2425 __half
= __len
>> 1;
2427 std::advance(__middle
, __half
);
2428 if (*__middle
< __val
)
2432 __len
= __len
- __half
- 1;
2434 else if (__val
< *__middle
)
2438 __left
= std::lower_bound(__first
, __middle
, __val
);
2439 std::advance(__first
, __len
);
2440 __right
= std::upper_bound(++__middle
, __first
, __val
);
2441 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2444 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2448 * @brief Finds the largest subrange in which @a val could be inserted
2449 * at any place in it without changing the ordering.
2450 * @param first An iterator.
2451 * @param last Another iterator.
2452 * @param val The search term.
2453 * @param comp A functor to use for comparisons.
2454 * @return An pair of iterators defining the subrange.
2455 * @ingroup binarysearch
2457 * This is equivalent to
2459 * std::make_pair(lower_bound(first, last, val, comp),
2460 * upper_bound(first, last, val, comp))
2462 * but does not actually call those functions.
2464 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2465 pair
<_ForwardIterator
, _ForwardIterator
>
2466 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2470 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2472 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2475 // concept requirements
2476 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2477 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2479 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2481 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2483 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2486 _DistanceType __len
= std::distance(__first
, __last
);
2487 _DistanceType __half
;
2488 _ForwardIterator __middle
, __left
, __right
;
2492 __half
= __len
>> 1;
2494 std::advance(__middle
, __half
);
2495 if (__comp(*__middle
, __val
))
2499 __len
= __len
- __half
- 1;
2501 else if (__comp(__val
, *__middle
))
2505 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2506 std::advance(__first
, __len
);
2507 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2508 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2511 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2515 * @brief Determines whether an element exists in a range.
2516 * @param first An iterator.
2517 * @param last Another iterator.
2518 * @param val The search term.
2519 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2520 * @ingroup binarysearch
2522 * Note that this does not actually return an iterator to @a val. For
2523 * that, use std::find or a container's specialized find member functions.
2525 template<typename _ForwardIterator
, typename _Tp
>
2527 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2530 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2533 // concept requirements
2534 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2535 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2536 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2537 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2539 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2540 return __i
!= __last
&& !(__val
< *__i
);
2544 * @brief Determines whether an element exists in a range.
2545 * @param first An iterator.
2546 * @param last Another iterator.
2547 * @param val The search term.
2548 * @param comp A functor to use for comparisons.
2549 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2550 * @ingroup binarysearch
2552 * Note that this does not actually return an iterator to @a val. For
2553 * that, use std::find or a container's specialized find member functions.
2555 * The comparison function should have the same effects on ordering as
2556 * the function used for the initial sort.
2558 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2560 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2561 const _Tp
& __val
, _Compare __comp
)
2563 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2566 // concept requirements
2567 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2568 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2570 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2572 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2575 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2576 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2583 * This is a helper function for the merge routines.
2586 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2587 typename _BidirectionalIterator3
>
2588 _BidirectionalIterator3
2589 __merge_backward(_BidirectionalIterator1 __first1
,
2590 _BidirectionalIterator1 __last1
,
2591 _BidirectionalIterator2 __first2
,
2592 _BidirectionalIterator2 __last2
,
2593 _BidirectionalIterator3 __result
)
2595 if (__first1
== __last1
)
2596 return std::copy_backward(__first2
, __last2
, __result
);
2597 if (__first2
== __last2
)
2598 return std::copy_backward(__first1
, __last1
, __result
);
2603 if (*__last2
< *__last1
)
2605 *--__result
= *__last1
;
2606 if (__first1
== __last1
)
2607 return std::copy_backward(__first2
, ++__last2
, __result
);
2612 *--__result
= *__last2
;
2613 if (__first2
== __last2
)
2614 return std::copy_backward(__first1
, ++__last1
, __result
);
2622 * This is a helper function for the merge routines.
2625 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2626 typename _BidirectionalIterator3
, typename _Compare
>
2627 _BidirectionalIterator3
2628 __merge_backward(_BidirectionalIterator1 __first1
,
2629 _BidirectionalIterator1 __last1
,
2630 _BidirectionalIterator2 __first2
,
2631 _BidirectionalIterator2 __last2
,
2632 _BidirectionalIterator3 __result
,
2635 if (__first1
== __last1
)
2636 return std::copy_backward(__first2
, __last2
, __result
);
2637 if (__first2
== __last2
)
2638 return std::copy_backward(__first1
, __last1
, __result
);
2643 if (__comp(*__last2
, *__last1
))
2645 *--__result
= *__last1
;
2646 if (__first1
== __last1
)
2647 return std::copy_backward(__first2
, ++__last2
, __result
);
2652 *--__result
= *__last2
;
2653 if (__first2
== __last2
)
2654 return std::copy_backward(__first1
, ++__last1
, __result
);
2662 * This is a helper function for the merge routines.
2665 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2667 _BidirectionalIterator1
2668 __rotate_adaptive(_BidirectionalIterator1 __first
,
2669 _BidirectionalIterator1 __middle
,
2670 _BidirectionalIterator1 __last
,
2671 _Distance __len1
, _Distance __len2
,
2672 _BidirectionalIterator2 __buffer
,
2673 _Distance __buffer_size
)
2675 _BidirectionalIterator2 __buffer_end
;
2676 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2678 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2679 std::copy_backward(__first
, __middle
, __last
);
2680 return std::copy(__buffer
, __buffer_end
, __first
);
2682 else if (__len1
<= __buffer_size
)
2684 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2685 std::copy(__middle
, __last
, __first
);
2686 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2690 std::rotate(__first
, __middle
, __last
);
2691 std::advance(__first
, std::distance(__middle
, __last
));
2698 * This is a helper function for the merge routines.
2701 template<typename _BidirectionalIterator
, typename _Distance
,
2704 __merge_adaptive(_BidirectionalIterator __first
,
2705 _BidirectionalIterator __middle
,
2706 _BidirectionalIterator __last
,
2707 _Distance __len1
, _Distance __len2
,
2708 _Pointer __buffer
, _Distance __buffer_size
)
2710 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2712 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2713 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2716 else if (__len2
<= __buffer_size
)
2718 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2719 std::__merge_backward(__first
, __middle
, __buffer
,
2720 __buffer_end
, __last
);
2724 _BidirectionalIterator __first_cut
= __first
;
2725 _BidirectionalIterator __second_cut
= __middle
;
2726 _Distance __len11
= 0;
2727 _Distance __len22
= 0;
2728 if (__len1
> __len2
)
2730 __len11
= __len1
/ 2;
2731 std::advance(__first_cut
, __len11
);
2732 __second_cut
= std::lower_bound(__middle
, __last
,
2734 __len22
= std::distance(__middle
, __second_cut
);
2738 __len22
= __len2
/ 2;
2739 std::advance(__second_cut
, __len22
);
2740 __first_cut
= std::upper_bound(__first
, __middle
,
2742 __len11
= std::distance(__first
, __first_cut
);
2744 _BidirectionalIterator __new_middle
=
2745 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2746 __len1
- __len11
, __len22
, __buffer
,
2748 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2749 __len22
, __buffer
, __buffer_size
);
2750 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2752 __len2
- __len22
, __buffer
, __buffer_size
);
2758 * This is a helper function for the merge routines.
2761 template<typename _BidirectionalIterator
, typename _Distance
,
2762 typename _Pointer
, typename _Compare
>
2764 __merge_adaptive(_BidirectionalIterator __first
,
2765 _BidirectionalIterator __middle
,
2766 _BidirectionalIterator __last
,
2767 _Distance __len1
, _Distance __len2
,
2768 _Pointer __buffer
, _Distance __buffer_size
,
2771 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2773 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2774 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2777 else if (__len2
<= __buffer_size
)
2779 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2780 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2785 _BidirectionalIterator __first_cut
= __first
;
2786 _BidirectionalIterator __second_cut
= __middle
;
2787 _Distance __len11
= 0;
2788 _Distance __len22
= 0;
2789 if (__len1
> __len2
)
2791 __len11
= __len1
/ 2;
2792 std::advance(__first_cut
, __len11
);
2793 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2795 __len22
= std::distance(__middle
, __second_cut
);
2799 __len22
= __len2
/ 2;
2800 std::advance(__second_cut
, __len22
);
2801 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2803 __len11
= std::distance(__first
, __first_cut
);
2805 _BidirectionalIterator __new_middle
=
2806 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2807 __len1
- __len11
, __len22
, __buffer
,
2809 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2810 __len22
, __buffer
, __buffer_size
, __comp
);
2811 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2813 __len2
- __len22
, __buffer
,
2814 __buffer_size
, __comp
);
2820 * This is a helper function for the merge routines.
2823 template<typename _BidirectionalIterator
, typename _Distance
>
2825 __merge_without_buffer(_BidirectionalIterator __first
,
2826 _BidirectionalIterator __middle
,
2827 _BidirectionalIterator __last
,
2828 _Distance __len1
, _Distance __len2
)
2830 if (__len1
== 0 || __len2
== 0)
2832 if (__len1
+ __len2
== 2)
2834 if (*__middle
< *__first
)
2835 std::iter_swap(__first
, __middle
);
2838 _BidirectionalIterator __first_cut
= __first
;
2839 _BidirectionalIterator __second_cut
= __middle
;
2840 _Distance __len11
= 0;
2841 _Distance __len22
= 0;
2842 if (__len1
> __len2
)
2844 __len11
= __len1
/ 2;
2845 std::advance(__first_cut
, __len11
);
2846 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2847 __len22
= std::distance(__middle
, __second_cut
);
2851 __len22
= __len2
/ 2;
2852 std::advance(__second_cut
, __len22
);
2853 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2854 __len11
= std::distance(__first
, __first_cut
);
2856 std::rotate(__first_cut
, __middle
, __second_cut
);
2857 _BidirectionalIterator __new_middle
= __first_cut
;
2858 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2859 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2861 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2862 __len1
- __len11
, __len2
- __len22
);
2867 * This is a helper function for the merge routines.
2870 template<typename _BidirectionalIterator
, typename _Distance
,
2873 __merge_without_buffer(_BidirectionalIterator __first
,
2874 _BidirectionalIterator __middle
,
2875 _BidirectionalIterator __last
,
2876 _Distance __len1
, _Distance __len2
,
2879 if (__len1
== 0 || __len2
== 0)
2881 if (__len1
+ __len2
== 2)
2883 if (__comp(*__middle
, *__first
))
2884 std::iter_swap(__first
, __middle
);
2887 _BidirectionalIterator __first_cut
= __first
;
2888 _BidirectionalIterator __second_cut
= __middle
;
2889 _Distance __len11
= 0;
2890 _Distance __len22
= 0;
2891 if (__len1
> __len2
)
2893 __len11
= __len1
/ 2;
2894 std::advance(__first_cut
, __len11
);
2895 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2897 __len22
= std::distance(__middle
, __second_cut
);
2901 __len22
= __len2
/ 2;
2902 std::advance(__second_cut
, __len22
);
2903 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2905 __len11
= std::distance(__first
, __first_cut
);
2907 std::rotate(__first_cut
, __middle
, __second_cut
);
2908 _BidirectionalIterator __new_middle
= __first_cut
;
2909 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2910 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2911 __len11
, __len22
, __comp
);
2912 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2913 __len1
- __len11
, __len2
- __len22
, __comp
);
2917 * @brief Merges two sorted ranges in place.
2918 * @param first An iterator.
2919 * @param middle Another iterator.
2920 * @param last Another iterator.
2923 * Merges two sorted and consecutive ranges, [first,middle) and
2924 * [middle,last), and puts the result in [first,last). The output will
2925 * be sorted. The sort is @e stable, that is, for equivalent
2926 * elements in the two ranges, elements from the first range will always
2927 * come before elements from the second.
2929 * If enough additional memory is available, this takes (last-first)-1
2930 * comparisons. Otherwise an NlogN algorithm is used, where N is
2931 * distance(first,last).
2933 template<typename _BidirectionalIterator
>
2935 inplace_merge(_BidirectionalIterator __first
,
2936 _BidirectionalIterator __middle
,
2937 _BidirectionalIterator __last
)
2939 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2941 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2944 // concept requirements
2945 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2946 _BidirectionalIterator
>)
2947 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2948 __glibcxx_requires_sorted(__first
, __middle
);
2949 __glibcxx_requires_sorted(__middle
, __last
);
2951 if (__first
== __middle
|| __middle
== __last
)
2954 _DistanceType __len1
= std::distance(__first
, __middle
);
2955 _DistanceType __len2
= std::distance(__middle
, __last
);
2957 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2959 if (__buf
.begin() == 0)
2960 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
2962 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2963 __buf
.begin(), _DistanceType(__buf
.size()));
2967 * @brief Merges two sorted ranges in place.
2968 * @param first An iterator.
2969 * @param middle Another iterator.
2970 * @param last Another iterator.
2971 * @param comp A functor to use for comparisons.
2974 * Merges two sorted and consecutive ranges, [first,middle) and
2975 * [middle,last), and puts the result in [first,last). The output will
2976 * be sorted. The sort is @e stable, that is, for equivalent
2977 * elements in the two ranges, elements from the first range will always
2978 * come before elements from the second.
2980 * If enough additional memory is available, this takes (last-first)-1
2981 * comparisons. Otherwise an NlogN algorithm is used, where N is
2982 * distance(first,last).
2984 * The comparison function should have the same effects on ordering as
2985 * the function used for the initial sort.
2987 template<typename _BidirectionalIterator
, typename _Compare
>
2989 inplace_merge(_BidirectionalIterator __first
,
2990 _BidirectionalIterator __middle
,
2991 _BidirectionalIterator __last
,
2994 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2996 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2999 // concept requirements
3000 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3001 _BidirectionalIterator
>)
3002 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3003 _ValueType
, _ValueType
>)
3004 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3005 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3007 if (__first
== __middle
|| __middle
== __last
)
3010 const _DistanceType __len1
= std::distance(__first
, __middle
);
3011 const _DistanceType __len2
= std::distance(__middle
, __last
);
3013 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3015 if (__buf
.begin() == 0)
3016 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3019 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3020 __buf
.begin(), _DistanceType(__buf
.size()),
3024 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3027 __merge_sort_loop(_RandomAccessIterator1 __first
,
3028 _RandomAccessIterator1 __last
,
3029 _RandomAccessIterator2 __result
,
3030 _Distance __step_size
)
3032 const _Distance __two_step
= 2 * __step_size
;
3034 while (__last
- __first
>= __two_step
)
3036 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3037 __first
+ __step_size
, __first
+ __two_step
,
3039 __first
+= __two_step
;
3042 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3043 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3044 __first
+ __step_size
, __last
,
3048 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3049 typename _Distance
, typename _Compare
>
3051 __merge_sort_loop(_RandomAccessIterator1 __first
,
3052 _RandomAccessIterator1 __last
,
3053 _RandomAccessIterator2 __result
, _Distance __step_size
,
3056 const _Distance __two_step
= 2 * __step_size
;
3058 while (__last
- __first
>= __two_step
)
3060 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3061 __first
+ __step_size
, __first
+ __two_step
,
3064 __first
+= __two_step
;
3066 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3068 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3069 __first
+ __step_size
, __last
, __result
, __comp
);
3072 template<typename _RandomAccessIterator
, typename _Distance
>
3074 __chunk_insertion_sort(_RandomAccessIterator __first
,
3075 _RandomAccessIterator __last
,
3076 _Distance __chunk_size
)
3078 while (__last
- __first
>= __chunk_size
)
3080 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3081 __first
+= __chunk_size
;
3083 std::__insertion_sort(__first
, __last
);
3086 template<typename _RandomAccessIterator
, typename _Distance
,
3089 __chunk_insertion_sort(_RandomAccessIterator __first
,
3090 _RandomAccessIterator __last
,
3091 _Distance __chunk_size
, _Compare __comp
)
3093 while (__last
- __first
>= __chunk_size
)
3095 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3096 __first
+= __chunk_size
;
3098 std::__insertion_sort(__first
, __last
, __comp
);
3101 enum { _S_chunk_size
= 7 };
3103 template<typename _RandomAccessIterator
, typename _Pointer
>
3105 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3106 _RandomAccessIterator __last
,
3109 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3112 const _Distance __len
= __last
- __first
;
3113 const _Pointer __buffer_last
= __buffer
+ __len
;
3115 _Distance __step_size
= _S_chunk_size
;
3116 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3118 while (__step_size
< __len
)
3120 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3122 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3127 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3129 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3130 _RandomAccessIterator __last
,
3131 _Pointer __buffer
, _Compare __comp
)
3133 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3136 const _Distance __len
= __last
- __first
;
3137 const _Pointer __buffer_last
= __buffer
+ __len
;
3139 _Distance __step_size
= _S_chunk_size
;
3140 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3142 while (__step_size
< __len
)
3144 std::__merge_sort_loop(__first
, __last
, __buffer
,
3145 __step_size
, __comp
);
3147 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3148 __step_size
, __comp
);
3153 template<typename _RandomAccessIterator
, typename _Pointer
,
3156 __stable_sort_adaptive(_RandomAccessIterator __first
,
3157 _RandomAccessIterator __last
,
3158 _Pointer __buffer
, _Distance __buffer_size
)
3160 const _Distance __len
= (__last
- __first
+ 1) / 2;
3161 const _RandomAccessIterator __middle
= __first
+ __len
;
3162 if (__len
> __buffer_size
)
3164 std::__stable_sort_adaptive(__first
, __middle
,
3165 __buffer
, __buffer_size
);
3166 std::__stable_sort_adaptive(__middle
, __last
,
3167 __buffer
, __buffer_size
);
3171 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3172 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3174 std::__merge_adaptive(__first
, __middle
, __last
,
3175 _Distance(__middle
- __first
),
3176 _Distance(__last
- __middle
),
3177 __buffer
, __buffer_size
);
3180 template<typename _RandomAccessIterator
, typename _Pointer
,
3181 typename _Distance
, typename _Compare
>
3183 __stable_sort_adaptive(_RandomAccessIterator __first
,
3184 _RandomAccessIterator __last
,
3185 _Pointer __buffer
, _Distance __buffer_size
,
3188 const _Distance __len
= (__last
- __first
+ 1) / 2;
3189 const _RandomAccessIterator __middle
= __first
+ __len
;
3190 if (__len
> __buffer_size
)
3192 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3193 __buffer_size
, __comp
);
3194 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3195 __buffer_size
, __comp
);
3199 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3200 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3202 std::__merge_adaptive(__first
, __middle
, __last
,
3203 _Distance(__middle
- __first
),
3204 _Distance(__last
- __middle
),
3205 __buffer
, __buffer_size
,
3211 * This is a helper function for the stable sorting routines.
3214 template<typename _RandomAccessIterator
>
3216 __inplace_stable_sort(_RandomAccessIterator __first
,
3217 _RandomAccessIterator __last
)
3219 if (__last
- __first
< 15)
3221 std::__insertion_sort(__first
, __last
);
3224 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3225 std::__inplace_stable_sort(__first
, __middle
);
3226 std::__inplace_stable_sort(__middle
, __last
);
3227 std::__merge_without_buffer(__first
, __middle
, __last
,
3234 * This is a helper function for the stable sorting routines.
3237 template<typename _RandomAccessIterator
, typename _Compare
>
3239 __inplace_stable_sort(_RandomAccessIterator __first
,
3240 _RandomAccessIterator __last
, _Compare __comp
)
3242 if (__last
- __first
< 15)
3244 std::__insertion_sort(__first
, __last
, __comp
);
3247 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3248 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3249 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3250 std::__merge_without_buffer(__first
, __middle
, __last
,
3258 // Set algorithms: includes, set_union, set_intersection, set_difference,
3259 // set_symmetric_difference. All of these algorithms have the precondition
3260 // that their input ranges are sorted and the postcondition that their output
3261 // ranges are sorted.
3264 * @brief Determines whether all elements of a sequence exists in a range.
3265 * @param first1 Start of search range.
3266 * @param last1 End of search range.
3267 * @param first2 Start of sequence
3268 * @param last2 End of sequence.
3269 * @return True if each element in [first2,last2) is contained in order
3270 * within [first1,last1). False otherwise.
3271 * @ingroup setoperations
3273 * This operation expects both [first1,last1) and [first2,last2) to be
3274 * sorted. Searches for the presence of each element in [first2,last2)
3275 * within [first1,last1). The iterators over each range only move forward,
3276 * so this is a linear algorithm. If an element in [first2,last2) is not
3277 * found before the search iterator reaches @a last2, false is returned.
3279 template<typename _InputIterator1
, typename _InputIterator2
>
3281 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3282 _InputIterator2 __first2
, _InputIterator2 __last2
)
3284 typedef typename iterator_traits
<_InputIterator1
>::value_type
3286 typedef typename iterator_traits
<_InputIterator2
>::value_type
3289 // concept requirements
3290 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3291 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3292 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3293 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3294 __glibcxx_requires_sorted(__first1
, __last1
);
3295 __glibcxx_requires_sorted(__first2
, __last2
);
3297 while (__first1
!= __last1
&& __first2
!= __last2
)
3298 if (*__first2
< *__first1
)
3300 else if(*__first1
< *__first2
)
3303 ++__first1
, ++__first2
;
3305 return __first2
== __last2
;
3309 * @brief Determines whether all elements of a sequence exists in a range
3311 * @param first1 Start of search range.
3312 * @param last1 End of search range.
3313 * @param first2 Start of sequence
3314 * @param last2 End of sequence.
3315 * @param comp Comparison function to use.
3316 * @return True if each element in [first2,last2) is contained in order
3317 * within [first1,last1) according to comp. False otherwise.
3318 * @ingroup setoperations
3320 * This operation expects both [first1,last1) and [first2,last2) to be
3321 * sorted. Searches for the presence of each element in [first2,last2)
3322 * within [first1,last1), using comp to decide. The iterators over each
3323 * range only move forward, so this is a linear algorithm. If an element
3324 * in [first2,last2) is not found before the search iterator reaches @a
3325 * last2, false is returned.
3327 template<typename _InputIterator1
, typename _InputIterator2
,
3330 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3331 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
3333 typedef typename iterator_traits
<_InputIterator1
>::value_type
3335 typedef typename iterator_traits
<_InputIterator2
>::value_type
3338 // concept requirements
3339 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3340 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3341 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3342 _ValueType1
, _ValueType2
>)
3343 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3344 _ValueType2
, _ValueType1
>)
3345 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3346 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3348 while (__first1
!= __last1
&& __first2
!= __last2
)
3349 if (__comp(*__first2
, *__first1
))
3351 else if(__comp(*__first1
, *__first2
))
3354 ++__first1
, ++__first2
;
3356 return __first2
== __last2
;
3365 // set_symmetric_difference
3370 * @brief Permute range into the next "dictionary" ordering.
3371 * @param first Start of range.
3372 * @param last End of range.
3373 * @return False if wrapped to first permutation, true otherwise.
3375 * Treats all permutations of the range as a set of "dictionary" sorted
3376 * sequences. Permutes the current sequence into the next one of this set.
3377 * Returns true if there are more sequences to generate. If the sequence
3378 * is the largest of the set, the smallest is generated and false returned.
3380 template<typename _BidirectionalIterator
>
3382 next_permutation(_BidirectionalIterator __first
,
3383 _BidirectionalIterator __last
)
3385 // concept requirements
3386 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3387 _BidirectionalIterator
>)
3388 __glibcxx_function_requires(_LessThanComparableConcept
<
3389 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3390 __glibcxx_requires_valid_range(__first
, __last
);
3392 if (__first
== __last
)
3394 _BidirectionalIterator __i
= __first
;
3403 _BidirectionalIterator __ii
= __i
;
3407 _BidirectionalIterator __j
= __last
;
3408 while (!(*__i
< *--__j
))
3410 std::iter_swap(__i
, __j
);
3411 std::reverse(__ii
, __last
);
3416 std::reverse(__first
, __last
);
3423 * @brief Permute range into the next "dictionary" ordering using
3424 * comparison functor.
3425 * @param first Start of range.
3426 * @param last End of range.
3427 * @param comp A comparison functor.
3428 * @return False if wrapped to first permutation, true otherwise.
3430 * Treats all permutations of the range [first,last) as a set of
3431 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3432 * sequence into the next one of this set. Returns true if there are more
3433 * sequences to generate. If the sequence is the largest of the set, the
3434 * smallest is generated and false returned.
3436 template<typename _BidirectionalIterator
, typename _Compare
>
3438 next_permutation(_BidirectionalIterator __first
,
3439 _BidirectionalIterator __last
, _Compare __comp
)
3441 // concept requirements
3442 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3443 _BidirectionalIterator
>)
3444 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3445 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3446 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3447 __glibcxx_requires_valid_range(__first
, __last
);
3449 if (__first
== __last
)
3451 _BidirectionalIterator __i
= __first
;
3460 _BidirectionalIterator __ii
= __i
;
3462 if (__comp(*__i
, *__ii
))
3464 _BidirectionalIterator __j
= __last
;
3465 while (!bool(__comp(*__i
, *--__j
)))
3467 std::iter_swap(__i
, __j
);
3468 std::reverse(__ii
, __last
);
3473 std::reverse(__first
, __last
);
3480 * @brief Permute range into the previous "dictionary" ordering.
3481 * @param first Start of range.
3482 * @param last End of range.
3483 * @return False if wrapped to last permutation, true otherwise.
3485 * Treats all permutations of the range as a set of "dictionary" sorted
3486 * sequences. Permutes the current sequence into the previous one of this
3487 * set. Returns true if there are more sequences to generate. If the
3488 * sequence is the smallest of the set, the largest is generated and false
3491 template<typename _BidirectionalIterator
>
3493 prev_permutation(_BidirectionalIterator __first
,
3494 _BidirectionalIterator __last
)
3496 // concept requirements
3497 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3498 _BidirectionalIterator
>)
3499 __glibcxx_function_requires(_LessThanComparableConcept
<
3500 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3501 __glibcxx_requires_valid_range(__first
, __last
);
3503 if (__first
== __last
)
3505 _BidirectionalIterator __i
= __first
;
3514 _BidirectionalIterator __ii
= __i
;
3518 _BidirectionalIterator __j
= __last
;
3519 while (!(*--__j
< *__i
))
3521 std::iter_swap(__i
, __j
);
3522 std::reverse(__ii
, __last
);
3527 std::reverse(__first
, __last
);
3534 * @brief Permute range into the previous "dictionary" ordering using
3535 * comparison functor.
3536 * @param first Start of range.
3537 * @param last End of range.
3538 * @param comp A comparison functor.
3539 * @return False if wrapped to last permutation, true otherwise.
3541 * Treats all permutations of the range [first,last) as a set of
3542 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3543 * sequence into the previous one of this set. Returns true if there are
3544 * more sequences to generate. If the sequence is the smallest of the set,
3545 * the largest is generated and false returned.
3547 template<typename _BidirectionalIterator
, typename _Compare
>
3549 prev_permutation(_BidirectionalIterator __first
,
3550 _BidirectionalIterator __last
, _Compare __comp
)
3552 // concept requirements
3553 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3554 _BidirectionalIterator
>)
3555 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3556 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3557 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3558 __glibcxx_requires_valid_range(__first
, __last
);
3560 if (__first
== __last
)
3562 _BidirectionalIterator __i
= __first
;
3571 _BidirectionalIterator __ii
= __i
;
3573 if (__comp(*__ii
, *__i
))
3575 _BidirectionalIterator __j
= __last
;
3576 while (!bool(__comp(*--__j
, *__i
)))
3578 std::iter_swap(__i
, __j
);
3579 std::reverse(__ii
, __last
);
3584 std::reverse(__first
, __last
);
3594 * @brief Copy a sequence, replacing each element of one value with another
3596 * @param first An input iterator.
3597 * @param last An input iterator.
3598 * @param result An output iterator.
3599 * @param old_value The value to be replaced.
3600 * @param new_value The replacement value.
3601 * @return The end of the output sequence, @p result+(last-first).
3603 * Copies each element in the input range @p [first,last) to the
3604 * output range @p [result,result+(last-first)) replacing elements
3605 * equal to @p old_value with @p new_value.
3607 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3609 replace_copy(_InputIterator __first
, _InputIterator __last
,
3610 _OutputIterator __result
,
3611 const _Tp
& __old_value
, const _Tp
& __new_value
)
3613 // concept requirements
3614 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3615 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3616 typename iterator_traits
<_InputIterator
>::value_type
>)
3617 __glibcxx_function_requires(_EqualOpConcept
<
3618 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3619 __glibcxx_requires_valid_range(__first
, __last
);
3621 for (; __first
!= __last
; ++__first
, ++__result
)
3622 if (*__first
== __old_value
)
3623 *__result
= __new_value
;
3625 *__result
= *__first
;
3630 * @brief Copy a sequence, replacing each value for which a predicate
3631 * returns true with another value.
3632 * @param first An input iterator.
3633 * @param last An input iterator.
3634 * @param result An output iterator.
3635 * @param pred A predicate.
3636 * @param new_value The replacement value.
3637 * @return The end of the output sequence, @p result+(last-first).
3639 * Copies each element in the range @p [first,last) to the range
3640 * @p [result,result+(last-first)) replacing elements for which
3641 * @p pred returns true with @p new_value.
3643 template<typename _InputIterator
, typename _OutputIterator
,
3644 typename _Predicate
, typename _Tp
>
3646 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3647 _OutputIterator __result
,
3648 _Predicate __pred
, const _Tp
& __new_value
)
3650 // concept requirements
3651 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3652 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3653 typename iterator_traits
<_InputIterator
>::value_type
>)
3654 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3655 typename iterator_traits
<_InputIterator
>::value_type
>)
3656 __glibcxx_requires_valid_range(__first
, __last
);
3658 for (; __first
!= __last
; ++__first
, ++__result
)
3659 if (__pred(*__first
))
3660 *__result
= __new_value
;
3662 *__result
= *__first
;
3666 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3668 * @brief Determines whether the elements of a sequence are sorted.
3669 * @param first An iterator.
3670 * @param last Another iterator.
3671 * @return True if the elements are sorted, false otherwise.
3673 template<typename _ForwardIterator
>
3675 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3676 { return std::is_sorted_until(__first
, __last
) == __last
; }
3679 * @brief Determines whether the elements of a sequence are sorted
3680 * according to a comparison functor.
3681 * @param first An iterator.
3682 * @param last Another iterator.
3683 * @param comp A comparison functor.
3684 * @return True if the elements are sorted, false otherwise.
3686 template<typename _ForwardIterator
, typename _Compare
>
3688 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3690 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3693 * @brief Determines the end of a sorted sequence.
3694 * @param first An iterator.
3695 * @param last Another iterator.
3696 * @return An iterator pointing to the last iterator i in [first, last)
3697 * for which the range [first, i) is sorted.
3699 template<typename _ForwardIterator
>
3701 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3703 // concept requirements
3704 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3705 __glibcxx_function_requires(_LessThanComparableConcept
<
3706 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3707 __glibcxx_requires_valid_range(__first
, __last
);
3709 if (__first
== __last
)
3712 _ForwardIterator __next
= __first
;
3713 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3714 if (*__next
< *__first
)
3720 * @brief Determines the end of a sorted sequence using comparison functor.
3721 * @param first An iterator.
3722 * @param last Another iterator.
3723 * @param comp A comparison functor.
3724 * @return An iterator pointing to the last iterator i in [first, last)
3725 * for which the range [first, i) is sorted.
3727 template<typename _ForwardIterator
, typename _Compare
>
3729 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3732 // concept requirements
3733 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3734 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3735 typename iterator_traits
<_ForwardIterator
>::value_type
,
3736 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3737 __glibcxx_requires_valid_range(__first
, __last
);
3739 if (__first
== __last
)
3742 _ForwardIterator __next
= __first
;
3743 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3744 if (__comp(*__next
, *__first
))
3750 * @brief Determines min and max at once as an ordered pair.
3751 * @param a A thing of arbitrary type.
3752 * @param b Another thing of arbitrary type.
3753 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3755 template<typename _Tp
>
3756 inline pair
<const _Tp
&, const _Tp
&>
3757 minmax(const _Tp
& __a
, const _Tp
& __b
)
3759 // concept requirements
3760 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3762 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3763 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3767 * @brief Determines min and max at once as an ordered pair.
3768 * @param a A thing of arbitrary type.
3769 * @param b Another thing of arbitrary type.
3770 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3771 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3773 template<typename _Tp
, typename _Compare
>
3774 inline pair
<const _Tp
&, const _Tp
&>
3775 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3777 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3778 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3782 * @brief Return a pair of iterators pointing to the minimum and maximum
3783 * elements in a range.
3784 * @param first Start of range.
3785 * @param last End of range.
3786 * @return make_pair(m, M), where m is the first iterator i in
3787 * [first, last) such that no other element in the range is
3788 * smaller, and where M is the last iterator i in [first, last)
3789 * such that no other element in the range is larger.
3791 template<typename _ForwardIterator
>
3792 pair
<_ForwardIterator
, _ForwardIterator
>
3793 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3795 // concept requirements
3796 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3797 __glibcxx_function_requires(_LessThanComparableConcept
<
3798 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3799 __glibcxx_requires_valid_range(__first
, __last
);
3801 _ForwardIterator __next
= __first
;
3802 if (__first
== __last
3803 || ++__next
== __last
)
3804 return std::make_pair(__first
, __first
);
3806 _ForwardIterator __min
, __max
;
3807 if (*__next
< *__first
)
3821 while (__first
!= __last
)
3824 if (++__next
== __last
)
3826 if (*__first
< *__min
)
3828 else if (!(*__first
< *__max
))
3833 if (*__next
< *__first
)
3835 if (*__next
< *__min
)
3837 if (!(*__first
< *__max
))
3842 if (*__first
< *__min
)
3844 if (!(*__next
< *__max
))
3852 return std::make_pair(__min
, __max
);
3856 * @brief Return a pair of iterators pointing to the minimum and maximum
3857 * elements in a range.
3858 * @param first Start of range.
3859 * @param last End of range.
3860 * @param comp Comparison functor.
3861 * @return make_pair(m, M), where m is the first iterator i in
3862 * [first, last) such that no other element in the range is
3863 * smaller, and where M is the last iterator i in [first, last)
3864 * such that no other element in the range is larger.
3866 template<typename _ForwardIterator
, typename _Compare
>
3867 pair
<_ForwardIterator
, _ForwardIterator
>
3868 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3871 // concept requirements
3872 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3873 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3874 typename iterator_traits
<_ForwardIterator
>::value_type
,
3875 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3876 __glibcxx_requires_valid_range(__first
, __last
);
3878 _ForwardIterator __next
= __first
;
3879 if (__first
== __last
3880 || ++__next
== __last
)
3881 return std::make_pair(__first
, __first
);
3883 _ForwardIterator __min
, __max
;
3884 if (__comp(*__next
, *__first
))
3898 while (__first
!= __last
)
3901 if (++__next
== __last
)
3903 if (__comp(*__first
, *__min
))
3905 else if (!__comp(*__first
, *__max
))
3910 if (__comp(*__next
, *__first
))
3912 if (__comp(*__next
, *__min
))
3914 if (!__comp(*__first
, *__max
))
3919 if (__comp(*__first
, *__min
))
3921 if (!__comp(*__next
, *__max
))
3929 return std::make_pair(__min
, __max
);
3931 #endif // __GXX_EXPERIMENTAL_CXX0X__
3933 _GLIBCXX_END_NAMESPACE
3935 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3938 * @brief Apply a function to every element of a sequence.
3939 * @param first An input iterator.
3940 * @param last An input iterator.
3941 * @param f A unary function object.
3944 * Applies the function object @p f to each element in the range
3945 * @p [first,last). @p f must not modify the order of the sequence.
3946 * If @p f has a return value it is ignored.
3948 template<typename _InputIterator
, typename _Function
>
3950 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3952 // concept requirements
3953 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3954 __glibcxx_requires_valid_range(__first
, __last
);
3955 for (; __first
!= __last
; ++__first
)
3961 * @brief Find the first occurrence of a value in a sequence.
3962 * @param first An input iterator.
3963 * @param last An input iterator.
3964 * @param val The value to find.
3965 * @return The first iterator @c i in the range @p [first,last)
3966 * such that @c *i == @p val, or @p last if no such iterator exists.
3968 template<typename _InputIterator
, typename _Tp
>
3969 inline _InputIterator
3970 find(_InputIterator __first
, _InputIterator __last
,
3973 // concept requirements
3974 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3975 __glibcxx_function_requires(_EqualOpConcept
<
3976 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3977 __glibcxx_requires_valid_range(__first
, __last
);
3978 return std::__find(__first
, __last
, __val
,
3979 std::__iterator_category(__first
));
3983 * @brief Find the first element in a sequence for which a
3984 * predicate is true.
3985 * @param first An input iterator.
3986 * @param last An input iterator.
3987 * @param pred A predicate.
3988 * @return The first iterator @c i in the range @p [first,last)
3989 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3991 template<typename _InputIterator
, typename _Predicate
>
3992 inline _InputIterator
3993 find_if(_InputIterator __first
, _InputIterator __last
,
3996 // concept requirements
3997 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3998 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3999 typename iterator_traits
<_InputIterator
>::value_type
>)
4000 __glibcxx_requires_valid_range(__first
, __last
);
4001 return std::__find_if(__first
, __last
, __pred
,
4002 std::__iterator_category(__first
));
4006 * @brief Find element from a set in a sequence.
4007 * @param first1 Start of range to search.
4008 * @param last1 End of range to search.
4009 * @param first2 Start of match candidates.
4010 * @param last2 End of match candidates.
4011 * @return The first iterator @c i in the range
4012 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4013 * interator in [first2,last2), or @p last1 if no such iterator exists.
4015 * Searches the range @p [first1,last1) for an element that is equal to
4016 * some element in the range [first2,last2). If found, returns an iterator
4017 * in the range [first1,last1), otherwise returns @p last1.
4019 template<typename _InputIterator
, typename _ForwardIterator
>
4021 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4022 _ForwardIterator __first2
, _ForwardIterator __last2
)
4024 // concept requirements
4025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4026 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4027 __glibcxx_function_requires(_EqualOpConcept
<
4028 typename iterator_traits
<_InputIterator
>::value_type
,
4029 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4030 __glibcxx_requires_valid_range(__first1
, __last1
);
4031 __glibcxx_requires_valid_range(__first2
, __last2
);
4033 for (; __first1
!= __last1
; ++__first1
)
4034 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4035 if (*__first1
== *__iter
)
4041 * @brief Find element from a set in a sequence using a predicate.
4042 * @param first1 Start of range to search.
4043 * @param last1 End of range to search.
4044 * @param first2 Start of match candidates.
4045 * @param last2 End of match candidates.
4046 * @param comp Predicate to use.
4047 * @return The first iterator @c i in the range
4048 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4049 * interator in [first2,last2), or @p last1 if no such iterator exists.
4052 * Searches the range @p [first1,last1) for an element that is
4053 * equal to some element in the range [first2,last2). If found,
4054 * returns an iterator in the range [first1,last1), otherwise
4057 template<typename _InputIterator
, typename _ForwardIterator
,
4058 typename _BinaryPredicate
>
4060 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4061 _ForwardIterator __first2
, _ForwardIterator __last2
,
4062 _BinaryPredicate __comp
)
4064 // concept requirements
4065 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4066 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4067 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4068 typename iterator_traits
<_InputIterator
>::value_type
,
4069 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4070 __glibcxx_requires_valid_range(__first1
, __last1
);
4071 __glibcxx_requires_valid_range(__first2
, __last2
);
4073 for (; __first1
!= __last1
; ++__first1
)
4074 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4075 if (__comp(*__first1
, *__iter
))
4081 * @brief Find two adjacent values in a sequence that are equal.
4082 * @param first A forward iterator.
4083 * @param last A forward iterator.
4084 * @return The first iterator @c i such that @c i and @c i+1 are both
4085 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4086 * or @p last if no such iterator exists.
4088 template<typename _ForwardIterator
>
4090 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4092 // concept requirements
4093 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4094 __glibcxx_function_requires(_EqualityComparableConcept
<
4095 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4096 __glibcxx_requires_valid_range(__first
, __last
);
4097 if (__first
== __last
)
4099 _ForwardIterator __next
= __first
;
4100 while(++__next
!= __last
)
4102 if (*__first
== *__next
)
4110 * @brief Find two adjacent values in a sequence using a predicate.
4111 * @param first A forward iterator.
4112 * @param last A forward iterator.
4113 * @param binary_pred A binary predicate.
4114 * @return The first iterator @c i such that @c i and @c i+1 are both
4115 * valid iterators in @p [first,last) and such that
4116 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4119 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4121 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4122 _BinaryPredicate __binary_pred
)
4124 // concept requirements
4125 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4126 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4127 typename iterator_traits
<_ForwardIterator
>::value_type
,
4128 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4129 __glibcxx_requires_valid_range(__first
, __last
);
4130 if (__first
== __last
)
4132 _ForwardIterator __next
= __first
;
4133 while(++__next
!= __last
)
4135 if (__binary_pred(*__first
, *__next
))
4143 * @brief Count the number of copies of a value in a sequence.
4144 * @param first An input iterator.
4145 * @param last An input iterator.
4146 * @param value The value to be counted.
4147 * @return The number of iterators @c i in the range @p [first,last)
4148 * for which @c *i == @p value
4150 template<typename _InputIterator
, typename _Tp
>
4151 typename iterator_traits
<_InputIterator
>::difference_type
4152 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4154 // concept requirements
4155 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4156 __glibcxx_function_requires(_EqualOpConcept
<
4157 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4158 __glibcxx_requires_valid_range(__first
, __last
);
4159 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4160 for (; __first
!= __last
; ++__first
)
4161 if (*__first
== __value
)
4167 * @brief Count the elements of a sequence for which a predicate is true.
4168 * @param first An input iterator.
4169 * @param last An input iterator.
4170 * @param pred A predicate.
4171 * @return The number of iterators @c i in the range @p [first,last)
4172 * for which @p pred(*i) is true.
4174 template<typename _InputIterator
, typename _Predicate
>
4175 typename iterator_traits
<_InputIterator
>::difference_type
4176 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4178 // concept requirements
4179 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4180 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4181 typename iterator_traits
<_InputIterator
>::value_type
>)
4182 __glibcxx_requires_valid_range(__first
, __last
);
4183 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4184 for (; __first
!= __last
; ++__first
)
4185 if (__pred(*__first
))
4191 * @brief Search a sequence for a matching sub-sequence.
4192 * @param first1 A forward iterator.
4193 * @param last1 A forward iterator.
4194 * @param first2 A forward iterator.
4195 * @param last2 A forward iterator.
4196 * @return The first iterator @c i in the range
4197 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4198 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4199 * such iterator exists.
4201 * Searches the range @p [first1,last1) for a sub-sequence that compares
4202 * equal value-by-value with the sequence given by @p [first2,last2) and
4203 * returns an iterator to the first element of the sub-sequence, or
4204 * @p last1 if the sub-sequence is not found.
4206 * Because the sub-sequence must lie completely within the range
4207 * @p [first1,last1) it must start at a position less than
4208 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4210 * This means that the returned iterator @c i will be in the range
4211 * @p [first1,last1-(last2-first2))
4213 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4215 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4216 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4218 // concept requirements
4219 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4220 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4221 __glibcxx_function_requires(_EqualOpConcept
<
4222 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4223 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4224 __glibcxx_requires_valid_range(__first1
, __last1
);
4225 __glibcxx_requires_valid_range(__first2
, __last2
);
4227 // Test for empty ranges
4228 if (__first1
== __last1
|| __first2
== __last2
)
4231 // Test for a pattern of length 1.
4232 _ForwardIterator2
__p1(__first2
);
4233 if (++__p1
== __last2
)
4234 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4237 _ForwardIterator2 __p
;
4238 _ForwardIterator1 __current
= __first1
;
4242 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4243 if (__first1
== __last1
)
4247 __current
= __first1
;
4248 if (++__current
== __last1
)
4251 while (*__current
== *__p
)
4253 if (++__p
== __last2
)
4255 if (++__current
== __last1
)
4264 * @brief Search a sequence for a matching sub-sequence using a predicate.
4265 * @param first1 A forward iterator.
4266 * @param last1 A forward iterator.
4267 * @param first2 A forward iterator.
4268 * @param last2 A forward iterator.
4269 * @param predicate A binary predicate.
4270 * @return The first iterator @c i in the range
4271 * @p [first1,last1-(last2-first2)) such that
4272 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4273 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4275 * Searches the range @p [first1,last1) for a sub-sequence that compares
4276 * equal value-by-value with the sequence given by @p [first2,last2),
4277 * using @p predicate to determine equality, and returns an iterator
4278 * to the first element of the sub-sequence, or @p last1 if no such
4281 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4283 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4284 typename _BinaryPredicate
>
4286 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4287 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4288 _BinaryPredicate __predicate
)
4290 // concept requirements
4291 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4292 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4293 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4294 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4295 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4296 __glibcxx_requires_valid_range(__first1
, __last1
);
4297 __glibcxx_requires_valid_range(__first2
, __last2
);
4299 // Test for empty ranges
4300 if (__first1
== __last1
|| __first2
== __last2
)
4303 // Test for a pattern of length 1.
4304 _ForwardIterator2
__p1(__first2
);
4305 if (++__p1
== __last2
)
4307 while (__first1
!= __last1
4308 && !bool(__predicate(*__first1
, *__first2
)))
4314 _ForwardIterator2 __p
;
4315 _ForwardIterator1 __current
= __first1
;
4319 while (__first1
!= __last1
4320 && !bool(__predicate(*__first1
, *__first2
)))
4322 if (__first1
== __last1
)
4326 __current
= __first1
;
4327 if (++__current
== __last1
)
4330 while (__predicate(*__current
, *__p
))
4332 if (++__p
== __last2
)
4334 if (++__current
== __last1
)
4344 * @brief Search a sequence for a number of consecutive values.
4345 * @param first A forward iterator.
4346 * @param last A forward iterator.
4347 * @param count The number of consecutive values.
4348 * @param val The value to find.
4349 * @return The first iterator @c i in the range @p [first,last-count)
4350 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4351 * or @p last if no such iterator exists.
4353 * Searches the range @p [first,last) for @p count consecutive elements
4356 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4358 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4359 _Integer __count
, const _Tp
& __val
)
4361 // concept requirements
4362 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4363 __glibcxx_function_requires(_EqualOpConcept
<
4364 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4365 __glibcxx_requires_valid_range(__first
, __last
);
4370 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4371 return std::__search_n(__first
, __last
, __count
, __val
,
4372 std::__iterator_category(__first
));
4377 * @brief Search a sequence for a number of consecutive values using a
4379 * @param first A forward iterator.
4380 * @param last A forward iterator.
4381 * @param count The number of consecutive values.
4382 * @param val The value to find.
4383 * @param binary_pred A binary predicate.
4384 * @return The first iterator @c i in the range @p [first,last-count)
4385 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4386 * range @p [0,count), or @p last if no such iterator exists.
4388 * Searches the range @p [first,last) for @p count consecutive elements
4389 * for which the predicate returns true.
4391 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4392 typename _BinaryPredicate
>
4394 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4395 _Integer __count
, const _Tp
& __val
,
4396 _BinaryPredicate __binary_pred
)
4398 // concept requirements
4399 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4400 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4401 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4402 __glibcxx_requires_valid_range(__first
, __last
);
4408 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4412 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4413 std::__iterator_category(__first
));
4418 * @brief Perform an operation on a sequence.
4419 * @param first An input iterator.
4420 * @param last An input iterator.
4421 * @param result An output iterator.
4422 * @param unary_op A unary operator.
4423 * @return An output iterator equal to @p result+(last-first).
4425 * Applies the operator to each element in the input range and assigns
4426 * the results to successive elements of the output sequence.
4427 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4428 * range @p [0,last-first).
4430 * @p unary_op must not alter its argument.
4432 template<typename _InputIterator
, typename _OutputIterator
,
4433 typename _UnaryOperation
>
4435 transform(_InputIterator __first
, _InputIterator __last
,
4436 _OutputIterator __result
, _UnaryOperation __unary_op
)
4438 // concept requirements
4439 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4440 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4441 // "the type returned by a _UnaryOperation"
4442 __typeof__(__unary_op(*__first
))>)
4443 __glibcxx_requires_valid_range(__first
, __last
);
4445 for (; __first
!= __last
; ++__first
, ++__result
)
4446 *__result
= __unary_op(*__first
);
4451 * @brief Perform an operation on corresponding elements of two sequences.
4452 * @param first1 An input iterator.
4453 * @param last1 An input iterator.
4454 * @param first2 An input iterator.
4455 * @param result An output iterator.
4456 * @param binary_op A binary operator.
4457 * @return An output iterator equal to @p result+(last-first).
4459 * Applies the operator to the corresponding elements in the two
4460 * input ranges and assigns the results to successive elements of the
4462 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4463 * @c N in the range @p [0,last1-first1).
4465 * @p binary_op must not alter either of its arguments.
4467 template<typename _InputIterator1
, typename _InputIterator2
,
4468 typename _OutputIterator
, typename _BinaryOperation
>
4470 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4471 _InputIterator2 __first2
, _OutputIterator __result
,
4472 _BinaryOperation __binary_op
)
4474 // concept requirements
4475 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4476 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4477 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4478 // "the type returned by a _BinaryOperation"
4479 __typeof__(__binary_op(*__first1
,*__first2
))>)
4480 __glibcxx_requires_valid_range(__first1
, __last1
);
4482 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4483 *__result
= __binary_op(*__first1
, *__first2
);
4488 * @brief Replace each occurrence of one value in a sequence with another
4490 * @param first A forward iterator.
4491 * @param last A forward iterator.
4492 * @param old_value The value to be replaced.
4493 * @param new_value The replacement value.
4494 * @return replace() returns no value.
4496 * For each iterator @c i in the range @p [first,last) if @c *i ==
4497 * @p old_value then the assignment @c *i = @p new_value is performed.
4499 template<typename _ForwardIterator
, typename _Tp
>
4501 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4502 const _Tp
& __old_value
, const _Tp
& __new_value
)
4504 // concept requirements
4505 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4507 __glibcxx_function_requires(_EqualOpConcept
<
4508 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4509 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4510 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4511 __glibcxx_requires_valid_range(__first
, __last
);
4513 for (; __first
!= __last
; ++__first
)
4514 if (*__first
== __old_value
)
4515 *__first
= __new_value
;
4519 * @brief Replace each value in a sequence for which a predicate returns
4520 * true with another value.
4521 * @param first A forward iterator.
4522 * @param last A forward iterator.
4523 * @param pred A predicate.
4524 * @param new_value The replacement value.
4525 * @return replace_if() returns no value.
4527 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4528 * is true then the assignment @c *i = @p new_value is performed.
4530 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4532 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4533 _Predicate __pred
, const _Tp
& __new_value
)
4535 // concept requirements
4536 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4538 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4539 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4540 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4541 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4542 __glibcxx_requires_valid_range(__first
, __last
);
4544 for (; __first
!= __last
; ++__first
)
4545 if (__pred(*__first
))
4546 *__first
= __new_value
;
4550 * @brief Assign the result of a function object to each value in a
4552 * @param first A forward iterator.
4553 * @param last A forward iterator.
4554 * @param gen A function object taking no arguments and returning
4555 * std::iterator_traits<_ForwardIterator>::value_type
4556 * @return generate() returns no value.
4558 * Performs the assignment @c *i = @p gen() for each @c i in the range
4561 template<typename _ForwardIterator
, typename _Generator
>
4563 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4566 // concept requirements
4567 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4568 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4569 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4570 __glibcxx_requires_valid_range(__first
, __last
);
4572 for (; __first
!= __last
; ++__first
)
4577 * @brief Assign the result of a function object to each value in a
4579 * @param first A forward iterator.
4580 * @param n The length of the sequence.
4581 * @param gen A function object taking no arguments and returning
4582 * std::iterator_traits<_ForwardIterator>::value_type
4583 * @return The end of the sequence, @p first+n
4585 * Performs the assignment @c *i = @p gen() for each @c i in the range
4586 * @p [first,first+n).
4588 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4590 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4592 // concept requirements
4593 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4594 // "the type returned by a _Generator"
4595 __typeof__(__gen())>)
4597 for (; __n
> 0; --__n
, ++__first
)
4604 * @brief Copy a sequence, removing consecutive duplicate values.
4605 * @param first An input iterator.
4606 * @param last An input iterator.
4607 * @param result An output iterator.
4608 * @return An iterator designating the end of the resulting sequence.
4610 * Copies each element in the range @p [first,last) to the range
4611 * beginning at @p result, except that only the first element is copied
4612 * from groups of consecutive elements that compare equal.
4613 * unique_copy() is stable, so the relative order of elements that are
4614 * copied is unchanged.
4617 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4618 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4620 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4621 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4625 template<typename _InputIterator
, typename _OutputIterator
>
4626 inline _OutputIterator
4627 unique_copy(_InputIterator __first
, _InputIterator __last
,
4628 _OutputIterator __result
)
4630 // concept requirements
4631 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4632 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4633 typename iterator_traits
<_InputIterator
>::value_type
>)
4634 __glibcxx_function_requires(_EqualityComparableConcept
<
4635 typename iterator_traits
<_InputIterator
>::value_type
>)
4636 __glibcxx_requires_valid_range(__first
, __last
);
4638 if (__first
== __last
)
4640 return std::__unique_copy(__first
, __last
, __result
,
4641 std::__iterator_category(__first
),
4642 std::__iterator_category(__result
));
4646 * @brief Copy a sequence, removing consecutive values using a predicate.
4647 * @param first An input iterator.
4648 * @param last An input iterator.
4649 * @param result An output iterator.
4650 * @param binary_pred A binary predicate.
4651 * @return An iterator designating the end of the resulting sequence.
4653 * Copies each element in the range @p [first,last) to the range
4654 * beginning at @p result, except that only the first element is copied
4655 * from groups of consecutive elements for which @p binary_pred returns
4657 * unique_copy() is stable, so the relative order of elements that are
4658 * copied is unchanged.
4661 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4662 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4665 template<typename _InputIterator
, typename _OutputIterator
,
4666 typename _BinaryPredicate
>
4667 inline _OutputIterator
4668 unique_copy(_InputIterator __first
, _InputIterator __last
,
4669 _OutputIterator __result
,
4670 _BinaryPredicate __binary_pred
)
4672 // concept requirements -- predicates checked later
4673 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4674 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4675 typename iterator_traits
<_InputIterator
>::value_type
>)
4676 __glibcxx_requires_valid_range(__first
, __last
);
4678 if (__first
== __last
)
4680 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4681 std::__iterator_category(__first
),
4682 std::__iterator_category(__result
));
4687 * @brief Randomly shuffle the elements of a sequence.
4688 * @param first A forward iterator.
4689 * @param last A forward iterator.
4692 * Reorder the elements in the range @p [first,last) using a random
4693 * distribution, so that every possible ordering of the sequence is
4696 template<typename _RandomAccessIterator
>
4698 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4700 // concept requirements
4701 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4702 _RandomAccessIterator
>)
4703 __glibcxx_requires_valid_range(__first
, __last
);
4705 if (__first
!= __last
)
4706 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4707 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4711 * @brief Shuffle the elements of a sequence using a random number
4713 * @param first A forward iterator.
4714 * @param last A forward iterator.
4715 * @param rand The RNG functor or function.
4718 * Reorders the elements in the range @p [first,last) using @p rand to
4719 * provide a random distribution. Calling @p rand(N) for a positive
4720 * integer @p N should return a randomly chosen integer from the
4723 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4725 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4726 _RandomNumberGenerator
& __rand
)
4728 // concept requirements
4729 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4730 _RandomAccessIterator
>)
4731 __glibcxx_requires_valid_range(__first
, __last
);
4733 if (__first
== __last
)
4735 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4736 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4741 * @brief Move elements for which a predicate is true to the beginning
4743 * @param first A forward iterator.
4744 * @param last A forward iterator.
4745 * @param pred A predicate functor.
4746 * @return An iterator @p middle such that @p pred(i) is true for each
4747 * iterator @p i in the range @p [first,middle) and false for each @p i
4748 * in the range @p [middle,last).
4750 * @p pred must not modify its operand. @p partition() does not preserve
4751 * the relative ordering of elements in each group, use
4752 * @p stable_partition() if this is needed.
4754 template<typename _ForwardIterator
, typename _Predicate
>
4755 inline _ForwardIterator
4756 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4759 // concept requirements
4760 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4762 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4763 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4764 __glibcxx_requires_valid_range(__first
, __last
);
4766 return std::__partition(__first
, __last
, __pred
,
4767 std::__iterator_category(__first
));
4773 * @brief Sort the smallest elements of a sequence.
4774 * @param first An iterator.
4775 * @param middle Another iterator.
4776 * @param last Another iterator.
4779 * Sorts the smallest @p (middle-first) elements in the range
4780 * @p [first,last) and moves them to the range @p [first,middle). The
4781 * order of the remaining elements in the range @p [middle,last) is
4783 * After the sort if @p i and @j are iterators in the range
4784 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4785 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4787 template<typename _RandomAccessIterator
>
4789 partial_sort(_RandomAccessIterator __first
,
4790 _RandomAccessIterator __middle
,
4791 _RandomAccessIterator __last
)
4793 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4796 // concept requirements
4797 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4798 _RandomAccessIterator
>)
4799 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4800 __glibcxx_requires_valid_range(__first
, __middle
);
4801 __glibcxx_requires_valid_range(__middle
, __last
);
4803 std::__heap_select(__first
, __middle
, __last
);
4804 std::sort_heap(__first
, __middle
);
4808 * @brief Sort the smallest elements of a sequence using a predicate
4810 * @param first An iterator.
4811 * @param middle Another iterator.
4812 * @param last Another iterator.
4813 * @param comp A comparison functor.
4816 * Sorts the smallest @p (middle-first) elements in the range
4817 * @p [first,last) and moves them to the range @p [first,middle). The
4818 * order of the remaining elements in the range @p [middle,last) is
4820 * After the sort if @p i and @j are iterators in the range
4821 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4822 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4825 template<typename _RandomAccessIterator
, typename _Compare
>
4827 partial_sort(_RandomAccessIterator __first
,
4828 _RandomAccessIterator __middle
,
4829 _RandomAccessIterator __last
,
4832 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4835 // concept requirements
4836 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4837 _RandomAccessIterator
>)
4838 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4839 _ValueType
, _ValueType
>)
4840 __glibcxx_requires_valid_range(__first
, __middle
);
4841 __glibcxx_requires_valid_range(__middle
, __last
);
4843 std::__heap_select(__first
, __middle
, __last
, __comp
);
4844 std::sort_heap(__first
, __middle
, __comp
);
4848 * @brief Sort a sequence just enough to find a particular position.
4849 * @param first An iterator.
4850 * @param nth Another iterator.
4851 * @param last Another iterator.
4854 * Rearranges the elements in the range @p [first,last) so that @p *nth
4855 * is the same element that would have been in that position had the
4856 * whole sequence been sorted.
4857 * whole sequence been sorted. The elements either side of @p *nth are
4858 * not completely sorted, but for any iterator @i in the range
4859 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4860 * holds that @p *j<*i is false.
4862 template<typename _RandomAccessIterator
>
4864 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4865 _RandomAccessIterator __last
)
4867 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4870 // concept requirements
4871 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4872 _RandomAccessIterator
>)
4873 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4874 __glibcxx_requires_valid_range(__first
, __nth
);
4875 __glibcxx_requires_valid_range(__nth
, __last
);
4877 if (__first
== __last
|| __nth
== __last
)
4880 std::__introselect(__first
, __nth
, __last
,
4881 std::__lg(__last
- __first
) * 2);
4885 * @brief Sort a sequence just enough to find a particular position
4886 * using a predicate for comparison.
4887 * @param first An iterator.
4888 * @param nth Another iterator.
4889 * @param last Another iterator.
4890 * @param comp A comparison functor.
4893 * Rearranges the elements in the range @p [first,last) so that @p *nth
4894 * is the same element that would have been in that position had the
4895 * whole sequence been sorted. The elements either side of @p *nth are
4896 * not completely sorted, but for any iterator @i in the range
4897 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4898 * holds that @p comp(*j,*i) is false.
4900 template<typename _RandomAccessIterator
, typename _Compare
>
4902 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4903 _RandomAccessIterator __last
, _Compare __comp
)
4905 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4908 // concept requirements
4909 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4910 _RandomAccessIterator
>)
4911 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4912 _ValueType
, _ValueType
>)
4913 __glibcxx_requires_valid_range(__first
, __nth
);
4914 __glibcxx_requires_valid_range(__nth
, __last
);
4916 if (__first
== __last
|| __nth
== __last
)
4919 std::__introselect(__first
, __nth
, __last
,
4920 std::__lg(__last
- __first
) * 2, __comp
);
4925 * @brief Sort the elements of a sequence.
4926 * @param first An iterator.
4927 * @param last Another iterator.
4930 * Sorts the elements in the range @p [first,last) in ascending order,
4931 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4932 * @p [first,last-1).
4934 * The relative ordering of equivalent elements is not preserved, use
4935 * @p stable_sort() if this is needed.
4937 template<typename _RandomAccessIterator
>
4939 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4941 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4944 // concept requirements
4945 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4946 _RandomAccessIterator
>)
4947 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4948 __glibcxx_requires_valid_range(__first
, __last
);
4950 if (__first
!= __last
)
4952 std::__introsort_loop(__first
, __last
,
4953 std::__lg(__last
- __first
) * 2);
4954 std::__final_insertion_sort(__first
, __last
);
4959 * @brief Sort the elements of a sequence using a predicate for comparison.
4960 * @param first An iterator.
4961 * @param last Another iterator.
4962 * @param comp A comparison functor.
4965 * Sorts the elements in the range @p [first,last) in ascending order,
4966 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4967 * range @p [first,last-1).
4969 * The relative ordering of equivalent elements is not preserved, use
4970 * @p stable_sort() if this is needed.
4972 template<typename _RandomAccessIterator
, typename _Compare
>
4974 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4977 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4980 // concept requirements
4981 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4982 _RandomAccessIterator
>)
4983 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
4985 __glibcxx_requires_valid_range(__first
, __last
);
4987 if (__first
!= __last
)
4989 std::__introsort_loop(__first
, __last
,
4990 std::__lg(__last
- __first
) * 2, __comp
);
4991 std::__final_insertion_sort(__first
, __last
, __comp
);
4996 * @brief Merges two sorted ranges.
4997 * @param first1 An iterator.
4998 * @param first2 Another iterator.
4999 * @param last1 Another iterator.
5000 * @param last2 Another iterator.
5001 * @param result An iterator pointing to the end of the merged range.
5002 * @return An iterator pointing to the first element "not less
5005 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5006 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5007 * must be sorted, and the output range must not overlap with either of
5008 * the input ranges. The sort is @e stable, that is, for equivalent
5009 * elements in the two ranges, elements from the first range will always
5010 * come before elements from the second.
5012 template<typename _InputIterator1
, typename _InputIterator2
,
5013 typename _OutputIterator
>
5015 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5016 _InputIterator2 __first2
, _InputIterator2 __last2
,
5017 _OutputIterator __result
)
5019 typedef typename iterator_traits
<_InputIterator1
>::value_type
5021 typedef typename iterator_traits
<_InputIterator2
>::value_type
5024 // concept requirements
5025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5026 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5027 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5029 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5031 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5032 __glibcxx_requires_sorted(__first1
, __last1
);
5033 __glibcxx_requires_sorted(__first2
, __last2
);
5035 while (__first1
!= __last1
&& __first2
!= __last2
)
5037 if (*__first2
< *__first1
)
5039 *__result
= *__first2
;
5044 *__result
= *__first1
;
5049 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5054 * @brief Merges two sorted ranges.
5055 * @param first1 An iterator.
5056 * @param first2 Another iterator.
5057 * @param last1 Another iterator.
5058 * @param last2 Another iterator.
5059 * @param result An iterator pointing to the end of the merged range.
5060 * @param comp A functor to use for comparisons.
5061 * @return An iterator pointing to the first element "not less
5064 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5065 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5066 * must be sorted, and the output range must not overlap with either of
5067 * the input ranges. The sort is @e stable, that is, for equivalent
5068 * elements in the two ranges, elements from the first range will always
5069 * come before elements from the second.
5071 * The comparison function should have the same effects on ordering as
5072 * the function used for the initial sort.
5074 template<typename _InputIterator1
, typename _InputIterator2
,
5075 typename _OutputIterator
, typename _Compare
>
5077 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5078 _InputIterator2 __first2
, _InputIterator2 __last2
,
5079 _OutputIterator __result
, _Compare __comp
)
5081 typedef typename iterator_traits
<_InputIterator1
>::value_type
5083 typedef typename iterator_traits
<_InputIterator2
>::value_type
5086 // concept requirements
5087 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5088 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5089 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5091 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5093 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5094 _ValueType2
, _ValueType1
>)
5095 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5096 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5098 while (__first1
!= __last1
&& __first2
!= __last2
)
5100 if (__comp(*__first2
, *__first1
))
5102 *__result
= *__first2
;
5107 *__result
= *__first1
;
5112 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5118 * @brief Sort the elements of a sequence, preserving the relative order
5119 * of equivalent elements.
5120 * @param first An iterator.
5121 * @param last Another iterator.
5124 * Sorts the elements in the range @p [first,last) in ascending order,
5125 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5126 * @p [first,last-1).
5128 * The relative ordering of equivalent elements is preserved, so any two
5129 * elements @p x and @p y in the range @p [first,last) such that
5130 * @p x<y is false and @p y<x is false will have the same relative
5131 * ordering after calling @p stable_sort().
5133 template<typename _RandomAccessIterator
>
5135 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5137 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5139 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5142 // concept requirements
5143 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5144 _RandomAccessIterator
>)
5145 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5146 __glibcxx_requires_valid_range(__first
, __last
);
5148 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5150 if (__buf
.begin() == 0)
5151 std::__inplace_stable_sort(__first
, __last
);
5153 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5154 _DistanceType(__buf
.size()));
5158 * @brief Sort the elements of a sequence using a predicate for comparison,
5159 * preserving the relative order of equivalent elements.
5160 * @param first An iterator.
5161 * @param last Another iterator.
5162 * @param comp A comparison functor.
5165 * Sorts the elements in the range @p [first,last) in ascending order,
5166 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5167 * range @p [first,last-1).
5169 * The relative ordering of equivalent elements is preserved, so any two
5170 * elements @p x and @p y in the range @p [first,last) such that
5171 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5172 * relative ordering after calling @p stable_sort().
5174 template<typename _RandomAccessIterator
, typename _Compare
>
5176 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5179 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5181 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5184 // concept requirements
5185 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5186 _RandomAccessIterator
>)
5187 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5190 __glibcxx_requires_valid_range(__first
, __last
);
5192 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5194 if (__buf
.begin() == 0)
5195 std::__inplace_stable_sort(__first
, __last
, __comp
);
5197 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5198 _DistanceType(__buf
.size()), __comp
);
5203 * @brief Return the union of two sorted ranges.
5204 * @param first1 Start of first range.
5205 * @param last1 End of first range.
5206 * @param first2 Start of second range.
5207 * @param last2 End of second range.
5208 * @return End of the output range.
5209 * @ingroup setoperations
5211 * This operation iterates over both ranges, copying elements present in
5212 * each range in order to the output range. Iterators increment for each
5213 * range. When the current element of one range is less than the other,
5214 * that element is copied and the iterator advanced. If an element is
5215 * contained in both ranges, the element from the first range is copied and
5216 * both ranges advance. The output range may not overlap either input
5219 template<typename _InputIterator1
, typename _InputIterator2
,
5220 typename _OutputIterator
>
5222 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5223 _InputIterator2 __first2
, _InputIterator2 __last2
,
5224 _OutputIterator __result
)
5226 typedef typename iterator_traits
<_InputIterator1
>::value_type
5228 typedef typename iterator_traits
<_InputIterator2
>::value_type
5231 // concept requirements
5232 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5233 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5234 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5236 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5238 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5239 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5240 __glibcxx_requires_sorted(__first1
, __last1
);
5241 __glibcxx_requires_sorted(__first2
, __last2
);
5243 while (__first1
!= __last1
&& __first2
!= __last2
)
5245 if (*__first1
< *__first2
)
5247 *__result
= *__first1
;
5250 else if (*__first2
< *__first1
)
5252 *__result
= *__first2
;
5257 *__result
= *__first1
;
5263 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5268 * @brief Return the union of two sorted ranges using a comparison functor.
5269 * @param first1 Start of first range.
5270 * @param last1 End of first range.
5271 * @param first2 Start of second range.
5272 * @param last2 End of second range.
5273 * @param comp The comparison functor.
5274 * @return End of the output range.
5275 * @ingroup setoperations
5277 * This operation iterates over both ranges, copying elements present in
5278 * each range in order to the output range. Iterators increment for each
5279 * range. When the current element of one range is less than the other
5280 * according to @a comp, that element is copied and the iterator advanced.
5281 * If an equivalent element according to @a comp is contained in both
5282 * ranges, the element from the first range is copied and both ranges
5283 * advance. The output range may not overlap either input range.
5285 template<typename _InputIterator1
, typename _InputIterator2
,
5286 typename _OutputIterator
, typename _Compare
>
5288 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5289 _InputIterator2 __first2
, _InputIterator2 __last2
,
5290 _OutputIterator __result
, _Compare __comp
)
5292 typedef typename iterator_traits
<_InputIterator1
>::value_type
5294 typedef typename iterator_traits
<_InputIterator2
>::value_type
5297 // concept requirements
5298 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5299 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5300 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5302 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5304 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5305 _ValueType1
, _ValueType2
>)
5306 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5307 _ValueType2
, _ValueType1
>)
5308 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5309 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5311 while (__first1
!= __last1
&& __first2
!= __last2
)
5313 if (__comp(*__first1
, *__first2
))
5315 *__result
= *__first1
;
5318 else if (__comp(*__first2
, *__first1
))
5320 *__result
= *__first2
;
5325 *__result
= *__first1
;
5331 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5336 * @brief Return the intersection of two sorted ranges.
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 * @return End of the output range.
5342 * @ingroup setoperations
5344 * This operation iterates over both ranges, copying elements present in
5345 * both ranges in order to the output range. Iterators increment for each
5346 * range. When the current element of one range is less than the other,
5347 * that iterator advances. If an element is contained in both ranges, the
5348 * element from the first range is copied and both ranges advance. The
5349 * output range may not overlap either input range.
5351 template<typename _InputIterator1
, typename _InputIterator2
,
5352 typename _OutputIterator
>
5354 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5355 _InputIterator2 __first2
, _InputIterator2 __last2
,
5356 _OutputIterator __result
)
5358 typedef typename iterator_traits
<_InputIterator1
>::value_type
5360 typedef typename iterator_traits
<_InputIterator2
>::value_type
5363 // concept requirements
5364 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5365 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5366 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5368 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5369 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5370 __glibcxx_requires_sorted(__first1
, __last1
);
5371 __glibcxx_requires_sorted(__first2
, __last2
);
5373 while (__first1
!= __last1
&& __first2
!= __last2
)
5374 if (*__first1
< *__first2
)
5376 else if (*__first2
< *__first1
)
5380 *__result
= *__first1
;
5389 * @brief Return the intersection of two sorted ranges using comparison
5391 * @param first1 Start of first range.
5392 * @param last1 End of first range.
5393 * @param first2 Start of second range.
5394 * @param last2 End of second range.
5395 * @param comp The comparison functor.
5396 * @return End of the output range.
5397 * @ingroup setoperations
5399 * This operation iterates over both ranges, copying elements present in
5400 * both ranges in order to the output range. Iterators increment for each
5401 * range. When the current element of one range is less than the other
5402 * according to @a comp, that iterator advances. If an element is
5403 * contained in both ranges according to @a comp, the element from the
5404 * first range is copied and both ranges advance. The output range may not
5405 * overlap either input range.
5407 template<typename _InputIterator1
, typename _InputIterator2
,
5408 typename _OutputIterator
, typename _Compare
>
5410 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5411 _InputIterator2 __first2
, _InputIterator2 __last2
,
5412 _OutputIterator __result
, _Compare __comp
)
5414 typedef typename iterator_traits
<_InputIterator1
>::value_type
5416 typedef typename iterator_traits
<_InputIterator2
>::value_type
5419 // concept requirements
5420 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5421 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5422 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5424 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5425 _ValueType1
, _ValueType2
>)
5426 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5427 _ValueType2
, _ValueType1
>)
5428 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5429 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5431 while (__first1
!= __last1
&& __first2
!= __last2
)
5432 if (__comp(*__first1
, *__first2
))
5434 else if (__comp(*__first2
, *__first1
))
5438 *__result
= *__first1
;
5447 * @brief Return the difference of two sorted ranges.
5448 * @param first1 Start of first range.
5449 * @param last1 End of first range.
5450 * @param first2 Start of second range.
5451 * @param last2 End of second range.
5452 * @return End of the output range.
5453 * @ingroup setoperations
5455 * This operation iterates over both ranges, copying elements present in
5456 * the first range but not the second in order to the output range.
5457 * Iterators increment for each range. When the current element of the
5458 * first range is less than the second, that element is copied and the
5459 * iterator advances. If the current element of the second range is less,
5460 * the iterator advances, but no element is copied. If an element is
5461 * contained in both ranges, no elements are copied and both ranges
5462 * advance. The output range may not overlap either input range.
5464 template<typename _InputIterator1
, typename _InputIterator2
,
5465 typename _OutputIterator
>
5467 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5468 _InputIterator2 __first2
, _InputIterator2 __last2
,
5469 _OutputIterator __result
)
5471 typedef typename iterator_traits
<_InputIterator1
>::value_type
5473 typedef typename iterator_traits
<_InputIterator2
>::value_type
5476 // concept requirements
5477 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5478 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5479 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5481 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5482 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5483 __glibcxx_requires_sorted(__first1
, __last1
);
5484 __glibcxx_requires_sorted(__first2
, __last2
);
5486 while (__first1
!= __last1
&& __first2
!= __last2
)
5487 if (*__first1
< *__first2
)
5489 *__result
= *__first1
;
5493 else if (*__first2
< *__first1
)
5500 return std::copy(__first1
, __last1
, __result
);
5504 * @brief Return the difference of two sorted ranges using comparison
5506 * @param first1 Start of first range.
5507 * @param last1 End of first range.
5508 * @param first2 Start of second range.
5509 * @param last2 End of second range.
5510 * @param comp The comparison functor.
5511 * @return End of the output range.
5512 * @ingroup setoperations
5514 * This operation iterates over both ranges, copying elements present in
5515 * the first range but not the second in order to the output range.
5516 * Iterators increment for each range. When the current element of the
5517 * first range is less than the second according to @a comp, that element
5518 * is copied and the iterator advances. If the current element of the
5519 * second range is less, no element is copied and the iterator advances.
5520 * If an element is contained in both ranges according to @a comp, no
5521 * elements are copied and both ranges advance. The output range may not
5522 * overlap either input range.
5524 template<typename _InputIterator1
, typename _InputIterator2
,
5525 typename _OutputIterator
, typename _Compare
>
5527 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5528 _InputIterator2 __first2
, _InputIterator2 __last2
,
5529 _OutputIterator __result
, _Compare __comp
)
5531 typedef typename iterator_traits
<_InputIterator1
>::value_type
5533 typedef typename iterator_traits
<_InputIterator2
>::value_type
5536 // concept requirements
5537 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5538 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5539 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5541 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5542 _ValueType1
, _ValueType2
>)
5543 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5544 _ValueType2
, _ValueType1
>)
5545 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5546 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5548 while (__first1
!= __last1
&& __first2
!= __last2
)
5549 if (__comp(*__first1
, *__first2
))
5551 *__result
= *__first1
;
5555 else if (__comp(*__first2
, *__first1
))
5562 return std::copy(__first1
, __last1
, __result
);
5566 * @brief Return the symmetric difference of two sorted ranges.
5567 * @param first1 Start of first range.
5568 * @param last1 End of first range.
5569 * @param first2 Start of second range.
5570 * @param last2 End of second range.
5571 * @return End of the output range.
5572 * @ingroup setoperations
5574 * This operation iterates over both ranges, copying elements present in
5575 * one range but not the other in order to the output range. Iterators
5576 * increment for each range. When the current element of one range is less
5577 * than the other, that element is copied and the iterator advances. If an
5578 * element is contained in both ranges, no elements are copied and both
5579 * ranges advance. The output range may not overlap either input range.
5581 template<typename _InputIterator1
, typename _InputIterator2
,
5582 typename _OutputIterator
>
5584 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5585 _InputIterator2 __first2
, _InputIterator2 __last2
,
5586 _OutputIterator __result
)
5588 typedef typename iterator_traits
<_InputIterator1
>::value_type
5590 typedef typename iterator_traits
<_InputIterator2
>::value_type
5593 // concept requirements
5594 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5595 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5596 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5598 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5600 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5601 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5602 __glibcxx_requires_sorted(__first1
, __last1
);
5603 __glibcxx_requires_sorted(__first2
, __last2
);
5605 while (__first1
!= __last1
&& __first2
!= __last2
)
5606 if (*__first1
< *__first2
)
5608 *__result
= *__first1
;
5612 else if (*__first2
< *__first1
)
5614 *__result
= *__first2
;
5623 return std::copy(__first2
, __last2
, std::copy(__first1
,
5624 __last1
, __result
));
5628 * @brief Return the symmetric difference of two sorted ranges using
5629 * comparison functor.
5630 * @param first1 Start of first range.
5631 * @param last1 End of first range.
5632 * @param first2 Start of second range.
5633 * @param last2 End of second range.
5634 * @param comp The comparison functor.
5635 * @return End of the output range.
5636 * @ingroup setoperations
5638 * This operation iterates over both ranges, copying elements present in
5639 * one range but not the other in order to the output range. Iterators
5640 * increment for each range. When the current element of one range is less
5641 * than the other according to @a comp, that element is copied and the
5642 * iterator advances. If an element is contained in both ranges according
5643 * to @a comp, no elements are copied and both ranges advance. The output
5644 * range may not overlap either input range.
5646 template<typename _InputIterator1
, typename _InputIterator2
,
5647 typename _OutputIterator
, typename _Compare
>
5649 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5650 _InputIterator2 __first2
, _InputIterator2 __last2
,
5651 _OutputIterator __result
,
5654 typedef typename iterator_traits
<_InputIterator1
>::value_type
5656 typedef typename iterator_traits
<_InputIterator2
>::value_type
5659 // concept requirements
5660 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5661 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5662 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5664 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5666 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5667 _ValueType1
, _ValueType2
>)
5668 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5669 _ValueType2
, _ValueType1
>)
5670 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5671 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5673 while (__first1
!= __last1
&& __first2
!= __last2
)
5674 if (__comp(*__first1
, *__first2
))
5676 *__result
= *__first1
;
5680 else if (__comp(*__first2
, *__first1
))
5682 *__result
= *__first2
;
5691 return std::copy(__first2
, __last2
,
5692 std::copy(__first1
, __last1
, __result
));
5697 * @brief Return the minimum element in a range.
5698 * @param first Start of range.
5699 * @param last End of range.
5700 * @return Iterator referencing the first instance of the smallest value.
5702 template<typename _ForwardIterator
>
5704 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5706 // concept requirements
5707 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5708 __glibcxx_function_requires(_LessThanComparableConcept
<
5709 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5710 __glibcxx_requires_valid_range(__first
, __last
);
5712 if (__first
== __last
)
5714 _ForwardIterator __result
= __first
;
5715 while (++__first
!= __last
)
5716 if (*__first
< *__result
)
5722 * @brief Return the minimum element in a range using comparison functor.
5723 * @param first Start of range.
5724 * @param last End of range.
5725 * @param comp Comparison functor.
5726 * @return Iterator referencing the first instance of the smallest value
5727 * according to comp.
5729 template<typename _ForwardIterator
, typename _Compare
>
5731 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5734 // concept requirements
5735 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5736 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5737 typename iterator_traits
<_ForwardIterator
>::value_type
,
5738 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5739 __glibcxx_requires_valid_range(__first
, __last
);
5741 if (__first
== __last
)
5743 _ForwardIterator __result
= __first
;
5744 while (++__first
!= __last
)
5745 if (__comp(*__first
, *__result
))
5751 * @brief Return the maximum element in a range.
5752 * @param first Start of range.
5753 * @param last End of range.
5754 * @return Iterator referencing the first instance of the largest value.
5756 template<typename _ForwardIterator
>
5758 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5760 // concept requirements
5761 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5762 __glibcxx_function_requires(_LessThanComparableConcept
<
5763 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5764 __glibcxx_requires_valid_range(__first
, __last
);
5766 if (__first
== __last
)
5768 _ForwardIterator __result
= __first
;
5769 while (++__first
!= __last
)
5770 if (*__result
< *__first
)
5776 * @brief Return the maximum element in a range using comparison functor.
5777 * @param first Start of range.
5778 * @param last End of range.
5779 * @param comp Comparison functor.
5780 * @return Iterator referencing the first instance of the largest value
5781 * according to comp.
5783 template<typename _ForwardIterator
, typename _Compare
>
5785 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5788 // concept requirements
5789 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5790 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5791 typename iterator_traits
<_ForwardIterator
>::value_type
,
5792 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5793 __glibcxx_requires_valid_range(__first
, __last
);
5795 if (__first
== __last
) return __first
;
5796 _ForwardIterator __result
= __first
;
5797 while (++__first
!= __last
)
5798 if (__comp(*__result
, *__first
))
5803 _GLIBCXX_END_NESTED_NAMESPACE
5805 #endif /* _STL_ALGO_H */