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 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1634 std::make_heap(__first
, __middle
);
1635 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1636 if (*__i
< *__first
)
1637 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
1642 * This is a helper function for the sort routines.
1645 template<typename _RandomAccessIterator
, typename _Compare
>
1647 __heap_select(_RandomAccessIterator __first
,
1648 _RandomAccessIterator __middle
,
1649 _RandomAccessIterator __last
, _Compare __comp
)
1651 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1654 std::make_heap(__first
, __middle
, __comp
);
1655 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1656 if (__comp(*__i
, *__first
))
1657 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
1663 * @brief Copy the smallest elements of a sequence.
1664 * @param first An iterator.
1665 * @param last Another iterator.
1666 * @param result_first A random-access iterator.
1667 * @param result_last Another random-access iterator.
1668 * @return An iterator indicating the end of the resulting sequence.
1670 * Copies and sorts the smallest N values from the range @p [first,last)
1671 * to the range beginning at @p result_first, where the number of
1672 * elements to be copied, @p N, is the smaller of @p (last-first) and
1673 * @p (result_last-result_first).
1674 * After the sort if @p i and @j are iterators in the range
1675 * @p [result_first,result_first+N) such that @i precedes @j then
1676 * @p *j<*i is false.
1677 * The value returned is @p result_first+N.
1679 template<typename _InputIterator
, typename _RandomAccessIterator
>
1680 _RandomAccessIterator
1681 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1682 _RandomAccessIterator __result_first
,
1683 _RandomAccessIterator __result_last
)
1685 typedef typename iterator_traits
<_InputIterator
>::value_type
1687 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1689 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1692 // concept requirements
1693 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1694 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1696 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1698 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1699 __glibcxx_requires_valid_range(__first
, __last
);
1700 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1702 if (__result_first
== __result_last
)
1703 return __result_last
;
1704 _RandomAccessIterator __result_real_last
= __result_first
;
1705 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1707 *__result_real_last
= *__first
;
1708 ++__result_real_last
;
1711 std::make_heap(__result_first
, __result_real_last
);
1712 while (__first
!= __last
)
1714 if (*__first
< *__result_first
)
1715 std::__adjust_heap(__result_first
, _DistanceType(0),
1716 _DistanceType(__result_real_last
1718 _InputValueType(*__first
));
1721 std::sort_heap(__result_first
, __result_real_last
);
1722 return __result_real_last
;
1726 * @brief Copy the smallest elements of a sequence using a predicate for
1728 * @param first An input iterator.
1729 * @param last Another input iterator.
1730 * @param result_first A random-access iterator.
1731 * @param result_last Another random-access iterator.
1732 * @param comp A comparison functor.
1733 * @return An iterator indicating the end of the resulting sequence.
1735 * Copies and sorts the smallest N values from the range @p [first,last)
1736 * to the range beginning at @p result_first, where the number of
1737 * elements to be copied, @p N, is the smaller of @p (last-first) and
1738 * @p (result_last-result_first).
1739 * After the sort if @p i and @j are iterators in the range
1740 * @p [result_first,result_first+N) such that @i precedes @j then
1741 * @p comp(*j,*i) is false.
1742 * The value returned is @p result_first+N.
1744 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1745 _RandomAccessIterator
1746 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1747 _RandomAccessIterator __result_first
,
1748 _RandomAccessIterator __result_last
,
1751 typedef typename iterator_traits
<_InputIterator
>::value_type
1753 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1755 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1758 // concept requirements
1759 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1760 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1761 _RandomAccessIterator
>)
1762 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1764 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1765 _InputValueType
, _OutputValueType
>)
1766 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1767 _OutputValueType
, _OutputValueType
>)
1768 __glibcxx_requires_valid_range(__first
, __last
);
1769 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1771 if (__result_first
== __result_last
)
1772 return __result_last
;
1773 _RandomAccessIterator __result_real_last
= __result_first
;
1774 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1776 *__result_real_last
= *__first
;
1777 ++__result_real_last
;
1780 std::make_heap(__result_first
, __result_real_last
, __comp
);
1781 while (__first
!= __last
)
1783 if (__comp(*__first
, *__result_first
))
1784 std::__adjust_heap(__result_first
, _DistanceType(0),
1785 _DistanceType(__result_real_last
1787 _InputValueType(*__first
),
1791 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1792 return __result_real_last
;
1797 * This is a helper function for the sort routine.
1800 template<typename _RandomAccessIterator
, typename _Tp
>
1802 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1804 _RandomAccessIterator __next
= __last
;
1806 while (__val
< *__next
)
1817 * This is a helper function for the sort routine.
1820 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1822 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1825 _RandomAccessIterator __next
= __last
;
1827 while (__comp(__val
, *__next
))
1838 * This is a helper function for the sort routine.
1841 template<typename _RandomAccessIterator
>
1843 __insertion_sort(_RandomAccessIterator __first
,
1844 _RandomAccessIterator __last
)
1846 if (__first
== __last
)
1849 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1851 typename iterator_traits
<_RandomAccessIterator
>::value_type
1853 if (__val
< *__first
)
1855 std::copy_backward(__first
, __i
, __i
+ 1);
1859 std::__unguarded_linear_insert(__i
, __val
);
1865 * This is a helper function for the sort routine.
1868 template<typename _RandomAccessIterator
, typename _Compare
>
1870 __insertion_sort(_RandomAccessIterator __first
,
1871 _RandomAccessIterator __last
, _Compare __comp
)
1873 if (__first
== __last
) return;
1875 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1877 typename iterator_traits
<_RandomAccessIterator
>::value_type
1879 if (__comp(__val
, *__first
))
1881 std::copy_backward(__first
, __i
, __i
+ 1);
1885 std::__unguarded_linear_insert(__i
, __val
, __comp
);
1891 * This is a helper function for the sort routine.
1894 template<typename _RandomAccessIterator
>
1896 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1897 _RandomAccessIterator __last
)
1899 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1902 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1903 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
1908 * This is a helper function for the sort routine.
1911 template<typename _RandomAccessIterator
, typename _Compare
>
1913 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1914 _RandomAccessIterator __last
, _Compare __comp
)
1916 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1919 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1920 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
1926 * This controls some aspect of the sort routines.
1929 enum { _S_threshold
= 16 };
1933 * This is a helper function for the sort routine.
1936 template<typename _RandomAccessIterator
>
1938 __final_insertion_sort(_RandomAccessIterator __first
,
1939 _RandomAccessIterator __last
)
1941 if (__last
- __first
> int(_S_threshold
))
1943 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
1944 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
1947 std::__insertion_sort(__first
, __last
);
1952 * This is a helper function for the sort routine.
1955 template<typename _RandomAccessIterator
, typename _Compare
>
1957 __final_insertion_sort(_RandomAccessIterator __first
,
1958 _RandomAccessIterator __last
, _Compare __comp
)
1960 if (__last
- __first
> int(_S_threshold
))
1962 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1963 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1967 std::__insertion_sort(__first
, __last
, __comp
);
1972 * This is a helper function...
1975 template<typename _RandomAccessIterator
, typename _Tp
>
1976 _RandomAccessIterator
1977 __unguarded_partition(_RandomAccessIterator __first
,
1978 _RandomAccessIterator __last
, _Tp __pivot
)
1982 while (*__first
< __pivot
)
1985 while (__pivot
< *__last
)
1987 if (!(__first
< __last
))
1989 std::iter_swap(__first
, __last
);
1996 * This is a helper function...
1999 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2000 _RandomAccessIterator
2001 __unguarded_partition(_RandomAccessIterator __first
,
2002 _RandomAccessIterator __last
,
2003 _Tp __pivot
, _Compare __comp
)
2007 while (__comp(*__first
, __pivot
))
2010 while (__comp(__pivot
, *__last
))
2012 if (!(__first
< __last
))
2014 std::iter_swap(__first
, __last
);
2021 * This is a helper function for the sort routine.
2024 template<typename _RandomAccessIterator
, typename _Size
>
2026 __introsort_loop(_RandomAccessIterator __first
,
2027 _RandomAccessIterator __last
,
2028 _Size __depth_limit
)
2030 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2033 while (__last
- __first
> int(_S_threshold
))
2035 if (__depth_limit
== 0)
2037 _GLIBCXX_STD_P
:partial_sort(__first
, __last
, __last
);
2041 _RandomAccessIterator __cut
=
2042 std::__unguarded_partition(__first
, __last
,
2043 _ValueType(std::__median(*__first
,
2050 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2057 * This is a helper function for the sort routine.
2060 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2062 __introsort_loop(_RandomAccessIterator __first
,
2063 _RandomAccessIterator __last
,
2064 _Size __depth_limit
, _Compare __comp
)
2066 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2069 while (__last
- __first
> int(_S_threshold
))
2071 if (__depth_limit
== 0)
2073 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2077 _RandomAccessIterator __cut
=
2078 std::__unguarded_partition(__first
, __last
,
2079 _ValueType(std::__median(*__first
,
2087 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2094 * This is a helper function for the sort routines.
2097 template<typename _Size
>
2102 for (__k
= 0; __n
!= 1; __n
>>= 1)
2109 template<typename _RandomAccessIterator
, typename _Size
>
2111 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2112 _RandomAccessIterator __last
, _Size __depth_limit
)
2114 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2117 while (__last
- __first
> 3)
2119 if (__depth_limit
== 0)
2121 std::__heap_select(__first
, __nth
+ 1, __last
);
2122 // Place the nth largest element in its final position.
2123 std::iter_swap(__first
, __nth
);
2127 _RandomAccessIterator __cut
=
2128 std::__unguarded_partition(__first
, __last
,
2129 _ValueType(std::__median(*__first
,
2141 std::__insertion_sort(__first
, __last
);
2144 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2146 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2147 _RandomAccessIterator __last
, _Size __depth_limit
,
2150 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2153 while (__last
- __first
> 3)
2155 if (__depth_limit
== 0)
2157 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2158 // Place the nth largest element in its final position.
2159 std::iter_swap(__first
, __nth
);
2163 _RandomAccessIterator __cut
=
2164 std::__unguarded_partition(__first
, __last
,
2165 _ValueType(std::__median(*__first
,
2178 std::__insertion_sort(__first
, __last
, __comp
);
2184 * @brief Finds the first position in which @a val could be inserted
2185 * without changing the ordering.
2186 * @param first An iterator.
2187 * @param last Another iterator.
2188 * @param val The search term.
2189 * @return An iterator pointing to the first element "not less
2190 * than" @a val, or end() if every element is less than
2192 * @ingroup binarysearch
2194 template<typename _ForwardIterator
, typename _Tp
>
2196 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2199 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2201 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2204 // concept requirements
2205 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2206 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2207 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2209 _DistanceType __len
= std::distance(__first
, __last
);
2210 _DistanceType __half
;
2211 _ForwardIterator __middle
;
2215 __half
= __len
>> 1;
2217 std::advance(__middle
, __half
);
2218 if (*__middle
< __val
)
2222 __len
= __len
- __half
- 1;
2231 * @brief Finds the first position in which @a val could be inserted
2232 * without changing the ordering.
2233 * @param first An iterator.
2234 * @param last Another iterator.
2235 * @param val The search term.
2236 * @param comp A functor to use for comparisons.
2237 * @return An iterator pointing to the first element "not less than" @a val,
2238 * or end() if every element is less than @a val.
2239 * @ingroup binarysearch
2241 * The comparison function should have the same effects on ordering as
2242 * the function used for the initial sort.
2244 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2246 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2247 const _Tp
& __val
, _Compare __comp
)
2249 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2251 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2254 // concept requirements
2255 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2256 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2258 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2261 _DistanceType __len
= std::distance(__first
, __last
);
2262 _DistanceType __half
;
2263 _ForwardIterator __middle
;
2267 __half
= __len
>> 1;
2269 std::advance(__middle
, __half
);
2270 if (__comp(*__middle
, __val
))
2274 __len
= __len
- __half
- 1;
2283 * @brief Finds the last position in which @a val could be inserted
2284 * without changing the ordering.
2285 * @param first An iterator.
2286 * @param last Another iterator.
2287 * @param val The search term.
2288 * @return An iterator pointing to the first element greater than @a val,
2289 * or end() if no elements are greater than @a val.
2290 * @ingroup binarysearch
2292 template<typename _ForwardIterator
, typename _Tp
>
2294 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2297 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2299 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2302 // concept requirements
2303 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2304 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2305 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2307 _DistanceType __len
= std::distance(__first
, __last
);
2308 _DistanceType __half
;
2309 _ForwardIterator __middle
;
2313 __half
= __len
>> 1;
2315 std::advance(__middle
, __half
);
2316 if (__val
< *__middle
)
2322 __len
= __len
- __half
- 1;
2329 * @brief Finds the last position in which @a val could be inserted
2330 * without changing the ordering.
2331 * @param first An iterator.
2332 * @param last Another iterator.
2333 * @param val The search term.
2334 * @param comp A functor to use for comparisons.
2335 * @return An iterator pointing to the first element greater than @a val,
2336 * or end() if no elements are greater than @a val.
2337 * @ingroup binarysearch
2339 * The comparison function should have the same effects on ordering as
2340 * the function used for the initial sort.
2342 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2344 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2345 const _Tp
& __val
, _Compare __comp
)
2347 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2349 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2352 // concept requirements
2353 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2354 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2356 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2359 _DistanceType __len
= std::distance(__first
, __last
);
2360 _DistanceType __half
;
2361 _ForwardIterator __middle
;
2365 __half
= __len
>> 1;
2367 std::advance(__middle
, __half
);
2368 if (__comp(__val
, *__middle
))
2374 __len
= __len
- __half
- 1;
2381 * @brief Finds the largest subrange in which @a val could be inserted
2382 * at any place in it without changing the ordering.
2383 * @param first An iterator.
2384 * @param last Another iterator.
2385 * @param val The search term.
2386 * @return An pair of iterators defining the subrange.
2387 * @ingroup binarysearch
2389 * This is equivalent to
2391 * std::make_pair(lower_bound(first, last, val),
2392 * upper_bound(first, last, val))
2394 * but does not actually call those functions.
2396 template<typename _ForwardIterator
, typename _Tp
>
2397 pair
<_ForwardIterator
, _ForwardIterator
>
2398 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2401 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2403 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2406 // concept requirements
2407 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2408 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2409 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2410 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2411 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2413 _DistanceType __len
= std::distance(__first
, __last
);
2414 _DistanceType __half
;
2415 _ForwardIterator __middle
, __left
, __right
;
2419 __half
= __len
>> 1;
2421 std::advance(__middle
, __half
);
2422 if (*__middle
< __val
)
2426 __len
= __len
- __half
- 1;
2428 else if (__val
< *__middle
)
2432 __left
= std::lower_bound(__first
, __middle
, __val
);
2433 std::advance(__first
, __len
);
2434 __right
= std::upper_bound(++__middle
, __first
, __val
);
2435 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2438 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2442 * @brief Finds the largest subrange in which @a val could be inserted
2443 * at any place in it without changing the ordering.
2444 * @param first An iterator.
2445 * @param last Another iterator.
2446 * @param val The search term.
2447 * @param comp A functor to use for comparisons.
2448 * @return An pair of iterators defining the subrange.
2449 * @ingroup binarysearch
2451 * This is equivalent to
2453 * std::make_pair(lower_bound(first, last, val, comp),
2454 * upper_bound(first, last, val, comp))
2456 * but does not actually call those functions.
2458 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2459 pair
<_ForwardIterator
, _ForwardIterator
>
2460 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2464 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2466 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2469 // concept requirements
2470 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2471 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2473 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2475 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2477 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2480 _DistanceType __len
= std::distance(__first
, __last
);
2481 _DistanceType __half
;
2482 _ForwardIterator __middle
, __left
, __right
;
2486 __half
= __len
>> 1;
2488 std::advance(__middle
, __half
);
2489 if (__comp(*__middle
, __val
))
2493 __len
= __len
- __half
- 1;
2495 else if (__comp(__val
, *__middle
))
2499 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2500 std::advance(__first
, __len
);
2501 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2502 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2505 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2509 * @brief Determines whether an element exists in a range.
2510 * @param first An iterator.
2511 * @param last Another iterator.
2512 * @param val The search term.
2513 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2514 * @ingroup binarysearch
2516 * Note that this does not actually return an iterator to @a val. For
2517 * that, use std::find or a container's specialized find member functions.
2519 template<typename _ForwardIterator
, typename _Tp
>
2521 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2524 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2527 // concept requirements
2528 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2529 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2530 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2531 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2533 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2534 return __i
!= __last
&& !(__val
< *__i
);
2538 * @brief Determines whether an element exists in a range.
2539 * @param first An iterator.
2540 * @param last Another iterator.
2541 * @param val The search term.
2542 * @param comp A functor to use for comparisons.
2543 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2544 * @ingroup binarysearch
2546 * Note that this does not actually return an iterator to @a val. For
2547 * that, use std::find or a container's specialized find member functions.
2549 * The comparison function should have the same effects on ordering as
2550 * the function used for the initial sort.
2552 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2554 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2555 const _Tp
& __val
, _Compare __comp
)
2557 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2560 // concept requirements
2561 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2562 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2564 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2566 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2569 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2570 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2577 * This is a helper function for the merge routines.
2580 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2581 typename _BidirectionalIterator3
>
2582 _BidirectionalIterator3
2583 __merge_backward(_BidirectionalIterator1 __first1
,
2584 _BidirectionalIterator1 __last1
,
2585 _BidirectionalIterator2 __first2
,
2586 _BidirectionalIterator2 __last2
,
2587 _BidirectionalIterator3 __result
)
2589 if (__first1
== __last1
)
2590 return std::copy_backward(__first2
, __last2
, __result
);
2591 if (__first2
== __last2
)
2592 return std::copy_backward(__first1
, __last1
, __result
);
2597 if (*__last2
< *__last1
)
2599 *--__result
= *__last1
;
2600 if (__first1
== __last1
)
2601 return std::copy_backward(__first2
, ++__last2
, __result
);
2606 *--__result
= *__last2
;
2607 if (__first2
== __last2
)
2608 return std::copy_backward(__first1
, ++__last1
, __result
);
2616 * This is a helper function for the merge routines.
2619 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2620 typename _BidirectionalIterator3
, typename _Compare
>
2621 _BidirectionalIterator3
2622 __merge_backward(_BidirectionalIterator1 __first1
,
2623 _BidirectionalIterator1 __last1
,
2624 _BidirectionalIterator2 __first2
,
2625 _BidirectionalIterator2 __last2
,
2626 _BidirectionalIterator3 __result
,
2629 if (__first1
== __last1
)
2630 return std::copy_backward(__first2
, __last2
, __result
);
2631 if (__first2
== __last2
)
2632 return std::copy_backward(__first1
, __last1
, __result
);
2637 if (__comp(*__last2
, *__last1
))
2639 *--__result
= *__last1
;
2640 if (__first1
== __last1
)
2641 return std::copy_backward(__first2
, ++__last2
, __result
);
2646 *--__result
= *__last2
;
2647 if (__first2
== __last2
)
2648 return std::copy_backward(__first1
, ++__last1
, __result
);
2656 * This is a helper function for the merge routines.
2659 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2661 _BidirectionalIterator1
2662 __rotate_adaptive(_BidirectionalIterator1 __first
,
2663 _BidirectionalIterator1 __middle
,
2664 _BidirectionalIterator1 __last
,
2665 _Distance __len1
, _Distance __len2
,
2666 _BidirectionalIterator2 __buffer
,
2667 _Distance __buffer_size
)
2669 _BidirectionalIterator2 __buffer_end
;
2670 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2672 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2673 std::copy_backward(__first
, __middle
, __last
);
2674 return std::copy(__buffer
, __buffer_end
, __first
);
2676 else if (__len1
<= __buffer_size
)
2678 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2679 std::copy(__middle
, __last
, __first
);
2680 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2684 std::rotate(__first
, __middle
, __last
);
2685 std::advance(__first
, std::distance(__middle
, __last
));
2692 * This is a helper function for the merge routines.
2695 template<typename _BidirectionalIterator
, typename _Distance
,
2698 __merge_adaptive(_BidirectionalIterator __first
,
2699 _BidirectionalIterator __middle
,
2700 _BidirectionalIterator __last
,
2701 _Distance __len1
, _Distance __len2
,
2702 _Pointer __buffer
, _Distance __buffer_size
)
2704 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2706 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2707 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2710 else if (__len2
<= __buffer_size
)
2712 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2713 std::__merge_backward(__first
, __middle
, __buffer
,
2714 __buffer_end
, __last
);
2718 _BidirectionalIterator __first_cut
= __first
;
2719 _BidirectionalIterator __second_cut
= __middle
;
2720 _Distance __len11
= 0;
2721 _Distance __len22
= 0;
2722 if (__len1
> __len2
)
2724 __len11
= __len1
/ 2;
2725 std::advance(__first_cut
, __len11
);
2726 __second_cut
= std::lower_bound(__middle
, __last
,
2728 __len22
= std::distance(__middle
, __second_cut
);
2732 __len22
= __len2
/ 2;
2733 std::advance(__second_cut
, __len22
);
2734 __first_cut
= std::upper_bound(__first
, __middle
,
2736 __len11
= std::distance(__first
, __first_cut
);
2738 _BidirectionalIterator __new_middle
=
2739 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2740 __len1
- __len11
, __len22
, __buffer
,
2742 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2743 __len22
, __buffer
, __buffer_size
);
2744 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2746 __len2
- __len22
, __buffer
, __buffer_size
);
2752 * This is a helper function for the merge routines.
2755 template<typename _BidirectionalIterator
, typename _Distance
,
2756 typename _Pointer
, typename _Compare
>
2758 __merge_adaptive(_BidirectionalIterator __first
,
2759 _BidirectionalIterator __middle
,
2760 _BidirectionalIterator __last
,
2761 _Distance __len1
, _Distance __len2
,
2762 _Pointer __buffer
, _Distance __buffer_size
,
2765 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2767 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2768 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2771 else if (__len2
<= __buffer_size
)
2773 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2774 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2779 _BidirectionalIterator __first_cut
= __first
;
2780 _BidirectionalIterator __second_cut
= __middle
;
2781 _Distance __len11
= 0;
2782 _Distance __len22
= 0;
2783 if (__len1
> __len2
)
2785 __len11
= __len1
/ 2;
2786 std::advance(__first_cut
, __len11
);
2787 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2789 __len22
= std::distance(__middle
, __second_cut
);
2793 __len22
= __len2
/ 2;
2794 std::advance(__second_cut
, __len22
);
2795 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2797 __len11
= std::distance(__first
, __first_cut
);
2799 _BidirectionalIterator __new_middle
=
2800 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2801 __len1
- __len11
, __len22
, __buffer
,
2803 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2804 __len22
, __buffer
, __buffer_size
, __comp
);
2805 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2807 __len2
- __len22
, __buffer
,
2808 __buffer_size
, __comp
);
2814 * This is a helper function for the merge routines.
2817 template<typename _BidirectionalIterator
, typename _Distance
>
2819 __merge_without_buffer(_BidirectionalIterator __first
,
2820 _BidirectionalIterator __middle
,
2821 _BidirectionalIterator __last
,
2822 _Distance __len1
, _Distance __len2
)
2824 if (__len1
== 0 || __len2
== 0)
2826 if (__len1
+ __len2
== 2)
2828 if (*__middle
< *__first
)
2829 std::iter_swap(__first
, __middle
);
2832 _BidirectionalIterator __first_cut
= __first
;
2833 _BidirectionalIterator __second_cut
= __middle
;
2834 _Distance __len11
= 0;
2835 _Distance __len22
= 0;
2836 if (__len1
> __len2
)
2838 __len11
= __len1
/ 2;
2839 std::advance(__first_cut
, __len11
);
2840 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2841 __len22
= std::distance(__middle
, __second_cut
);
2845 __len22
= __len2
/ 2;
2846 std::advance(__second_cut
, __len22
);
2847 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2848 __len11
= std::distance(__first
, __first_cut
);
2850 std::rotate(__first_cut
, __middle
, __second_cut
);
2851 _BidirectionalIterator __new_middle
= __first_cut
;
2852 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2853 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2855 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2856 __len1
- __len11
, __len2
- __len22
);
2861 * This is a helper function for the merge routines.
2864 template<typename _BidirectionalIterator
, typename _Distance
,
2867 __merge_without_buffer(_BidirectionalIterator __first
,
2868 _BidirectionalIterator __middle
,
2869 _BidirectionalIterator __last
,
2870 _Distance __len1
, _Distance __len2
,
2873 if (__len1
== 0 || __len2
== 0)
2875 if (__len1
+ __len2
== 2)
2877 if (__comp(*__middle
, *__first
))
2878 std::iter_swap(__first
, __middle
);
2881 _BidirectionalIterator __first_cut
= __first
;
2882 _BidirectionalIterator __second_cut
= __middle
;
2883 _Distance __len11
= 0;
2884 _Distance __len22
= 0;
2885 if (__len1
> __len2
)
2887 __len11
= __len1
/ 2;
2888 std::advance(__first_cut
, __len11
);
2889 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2891 __len22
= std::distance(__middle
, __second_cut
);
2895 __len22
= __len2
/ 2;
2896 std::advance(__second_cut
, __len22
);
2897 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2899 __len11
= std::distance(__first
, __first_cut
);
2901 std::rotate(__first_cut
, __middle
, __second_cut
);
2902 _BidirectionalIterator __new_middle
= __first_cut
;
2903 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2904 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2905 __len11
, __len22
, __comp
);
2906 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2907 __len1
- __len11
, __len2
- __len22
, __comp
);
2911 * @brief Merges two sorted ranges in place.
2912 * @param first An iterator.
2913 * @param middle Another iterator.
2914 * @param last Another iterator.
2917 * Merges two sorted and consecutive ranges, [first,middle) and
2918 * [middle,last), and puts the result in [first,last). The output will
2919 * be sorted. The sort is @e stable, that is, for equivalent
2920 * elements in the two ranges, elements from the first range will always
2921 * come before elements from the second.
2923 * If enough additional memory is available, this takes (last-first)-1
2924 * comparisons. Otherwise an NlogN algorithm is used, where N is
2925 * distance(first,last).
2927 template<typename _BidirectionalIterator
>
2929 inplace_merge(_BidirectionalIterator __first
,
2930 _BidirectionalIterator __middle
,
2931 _BidirectionalIterator __last
)
2933 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2935 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2938 // concept requirements
2939 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2940 _BidirectionalIterator
>)
2941 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2942 __glibcxx_requires_sorted(__first
, __middle
);
2943 __glibcxx_requires_sorted(__middle
, __last
);
2945 if (__first
== __middle
|| __middle
== __last
)
2948 _DistanceType __len1
= std::distance(__first
, __middle
);
2949 _DistanceType __len2
= std::distance(__middle
, __last
);
2951 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2953 if (__buf
.begin() == 0)
2954 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
2956 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2957 __buf
.begin(), _DistanceType(__buf
.size()));
2961 * @brief Merges two sorted ranges in place.
2962 * @param first An iterator.
2963 * @param middle Another iterator.
2964 * @param last Another iterator.
2965 * @param comp A functor to use for comparisons.
2968 * Merges two sorted and consecutive ranges, [first,middle) and
2969 * [middle,last), and puts the result in [first,last). The output will
2970 * be sorted. The sort is @e stable, that is, for equivalent
2971 * elements in the two ranges, elements from the first range will always
2972 * come before elements from the second.
2974 * If enough additional memory is available, this takes (last-first)-1
2975 * comparisons. Otherwise an NlogN algorithm is used, where N is
2976 * distance(first,last).
2978 * The comparison function should have the same effects on ordering as
2979 * the function used for the initial sort.
2981 template<typename _BidirectionalIterator
, typename _Compare
>
2983 inplace_merge(_BidirectionalIterator __first
,
2984 _BidirectionalIterator __middle
,
2985 _BidirectionalIterator __last
,
2988 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2990 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2993 // concept requirements
2994 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2995 _BidirectionalIterator
>)
2996 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2997 _ValueType
, _ValueType
>)
2998 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2999 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3001 if (__first
== __middle
|| __middle
== __last
)
3004 const _DistanceType __len1
= std::distance(__first
, __middle
);
3005 const _DistanceType __len2
= std::distance(__middle
, __last
);
3007 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3009 if (__buf
.begin() == 0)
3010 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3013 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3014 __buf
.begin(), _DistanceType(__buf
.size()),
3018 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3021 __merge_sort_loop(_RandomAccessIterator1 __first
,
3022 _RandomAccessIterator1 __last
,
3023 _RandomAccessIterator2 __result
,
3024 _Distance __step_size
)
3026 const _Distance __two_step
= 2 * __step_size
;
3028 while (__last
- __first
>= __two_step
)
3030 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3031 __first
+ __step_size
, __first
+ __two_step
,
3033 __first
+= __two_step
;
3036 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3037 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3038 __first
+ __step_size
, __last
,
3042 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3043 typename _Distance
, typename _Compare
>
3045 __merge_sort_loop(_RandomAccessIterator1 __first
,
3046 _RandomAccessIterator1 __last
,
3047 _RandomAccessIterator2 __result
, _Distance __step_size
,
3050 const _Distance __two_step
= 2 * __step_size
;
3052 while (__last
- __first
>= __two_step
)
3054 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3055 __first
+ __step_size
, __first
+ __two_step
,
3058 __first
+= __two_step
;
3060 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3062 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3063 __first
+ __step_size
, __last
, __result
, __comp
);
3066 template<typename _RandomAccessIterator
, typename _Distance
>
3068 __chunk_insertion_sort(_RandomAccessIterator __first
,
3069 _RandomAccessIterator __last
,
3070 _Distance __chunk_size
)
3072 while (__last
- __first
>= __chunk_size
)
3074 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3075 __first
+= __chunk_size
;
3077 std::__insertion_sort(__first
, __last
);
3080 template<typename _RandomAccessIterator
, typename _Distance
,
3083 __chunk_insertion_sort(_RandomAccessIterator __first
,
3084 _RandomAccessIterator __last
,
3085 _Distance __chunk_size
, _Compare __comp
)
3087 while (__last
- __first
>= __chunk_size
)
3089 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3090 __first
+= __chunk_size
;
3092 std::__insertion_sort(__first
, __last
, __comp
);
3095 enum { _S_chunk_size
= 7 };
3097 template<typename _RandomAccessIterator
, typename _Pointer
>
3099 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3100 _RandomAccessIterator __last
,
3103 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3106 const _Distance __len
= __last
- __first
;
3107 const _Pointer __buffer_last
= __buffer
+ __len
;
3109 _Distance __step_size
= _S_chunk_size
;
3110 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3112 while (__step_size
< __len
)
3114 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3116 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3121 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3123 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3124 _RandomAccessIterator __last
,
3125 _Pointer __buffer
, _Compare __comp
)
3127 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3130 const _Distance __len
= __last
- __first
;
3131 const _Pointer __buffer_last
= __buffer
+ __len
;
3133 _Distance __step_size
= _S_chunk_size
;
3134 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3136 while (__step_size
< __len
)
3138 std::__merge_sort_loop(__first
, __last
, __buffer
,
3139 __step_size
, __comp
);
3141 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3142 __step_size
, __comp
);
3147 template<typename _RandomAccessIterator
, typename _Pointer
,
3150 __stable_sort_adaptive(_RandomAccessIterator __first
,
3151 _RandomAccessIterator __last
,
3152 _Pointer __buffer
, _Distance __buffer_size
)
3154 const _Distance __len
= (__last
- __first
+ 1) / 2;
3155 const _RandomAccessIterator __middle
= __first
+ __len
;
3156 if (__len
> __buffer_size
)
3158 std::__stable_sort_adaptive(__first
, __middle
,
3159 __buffer
, __buffer_size
);
3160 std::__stable_sort_adaptive(__middle
, __last
,
3161 __buffer
, __buffer_size
);
3165 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3166 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3168 std::__merge_adaptive(__first
, __middle
, __last
,
3169 _Distance(__middle
- __first
),
3170 _Distance(__last
- __middle
),
3171 __buffer
, __buffer_size
);
3174 template<typename _RandomAccessIterator
, typename _Pointer
,
3175 typename _Distance
, typename _Compare
>
3177 __stable_sort_adaptive(_RandomAccessIterator __first
,
3178 _RandomAccessIterator __last
,
3179 _Pointer __buffer
, _Distance __buffer_size
,
3182 const _Distance __len
= (__last
- __first
+ 1) / 2;
3183 const _RandomAccessIterator __middle
= __first
+ __len
;
3184 if (__len
> __buffer_size
)
3186 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3187 __buffer_size
, __comp
);
3188 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3189 __buffer_size
, __comp
);
3193 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3194 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3196 std::__merge_adaptive(__first
, __middle
, __last
,
3197 _Distance(__middle
- __first
),
3198 _Distance(__last
- __middle
),
3199 __buffer
, __buffer_size
,
3205 * This is a helper function for the stable sorting routines.
3208 template<typename _RandomAccessIterator
>
3210 __inplace_stable_sort(_RandomAccessIterator __first
,
3211 _RandomAccessIterator __last
)
3213 if (__last
- __first
< 15)
3215 std::__insertion_sort(__first
, __last
);
3218 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3219 std::__inplace_stable_sort(__first
, __middle
);
3220 std::__inplace_stable_sort(__middle
, __last
);
3221 std::__merge_without_buffer(__first
, __middle
, __last
,
3228 * This is a helper function for the stable sorting routines.
3231 template<typename _RandomAccessIterator
, typename _Compare
>
3233 __inplace_stable_sort(_RandomAccessIterator __first
,
3234 _RandomAccessIterator __last
, _Compare __comp
)
3236 if (__last
- __first
< 15)
3238 std::__insertion_sort(__first
, __last
, __comp
);
3241 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3242 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3243 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3244 std::__merge_without_buffer(__first
, __middle
, __last
,
3252 // Set algorithms: includes, set_union, set_intersection, set_difference,
3253 // set_symmetric_difference. All of these algorithms have the precondition
3254 // that their input ranges are sorted and the postcondition that their output
3255 // ranges are sorted.
3258 * @brief Determines whether all elements of a sequence exists in a range.
3259 * @param first1 Start of search range.
3260 * @param last1 End of search range.
3261 * @param first2 Start of sequence
3262 * @param last2 End of sequence.
3263 * @return True if each element in [first2,last2) is contained in order
3264 * within [first1,last1). False otherwise.
3265 * @ingroup setoperations
3267 * This operation expects both [first1,last1) and [first2,last2) to be
3268 * sorted. Searches for the presence of each element in [first2,last2)
3269 * within [first1,last1). The iterators over each range only move forward,
3270 * so this is a linear algorithm. If an element in [first2,last2) is not
3271 * found before the search iterator reaches @a last2, false is returned.
3273 template<typename _InputIterator1
, typename _InputIterator2
>
3275 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3276 _InputIterator2 __first2
, _InputIterator2 __last2
)
3278 typedef typename iterator_traits
<_InputIterator1
>::value_type
3280 typedef typename iterator_traits
<_InputIterator2
>::value_type
3283 // concept requirements
3284 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3285 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3286 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3287 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3288 __glibcxx_requires_sorted(__first1
, __last1
);
3289 __glibcxx_requires_sorted(__first2
, __last2
);
3291 while (__first1
!= __last1
&& __first2
!= __last2
)
3292 if (*__first2
< *__first1
)
3294 else if(*__first1
< *__first2
)
3297 ++__first1
, ++__first2
;
3299 return __first2
== __last2
;
3303 * @brief Determines whether all elements of a sequence exists in a range
3305 * @param first1 Start of search range.
3306 * @param last1 End of search range.
3307 * @param first2 Start of sequence
3308 * @param last2 End of sequence.
3309 * @param comp Comparison function to use.
3310 * @return True if each element in [first2,last2) is contained in order
3311 * within [first1,last1) according to comp. False otherwise.
3312 * @ingroup setoperations
3314 * This operation expects both [first1,last1) and [first2,last2) to be
3315 * sorted. Searches for the presence of each element in [first2,last2)
3316 * within [first1,last1), using comp to decide. The iterators over each
3317 * range only move forward, so this is a linear algorithm. If an element
3318 * in [first2,last2) is not found before the search iterator reaches @a
3319 * last2, false is returned.
3321 template<typename _InputIterator1
, typename _InputIterator2
,
3324 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3325 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
3327 typedef typename iterator_traits
<_InputIterator1
>::value_type
3329 typedef typename iterator_traits
<_InputIterator2
>::value_type
3332 // concept requirements
3333 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3334 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3335 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3336 _ValueType1
, _ValueType2
>)
3337 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3338 _ValueType2
, _ValueType1
>)
3339 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3340 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3342 while (__first1
!= __last1
&& __first2
!= __last2
)
3343 if (__comp(*__first2
, *__first1
))
3345 else if(__comp(*__first1
, *__first2
))
3348 ++__first1
, ++__first2
;
3350 return __first2
== __last2
;
3359 // set_symmetric_difference
3364 * @brief Permute range into the next "dictionary" ordering.
3365 * @param first Start of range.
3366 * @param last End of range.
3367 * @return False if wrapped to first permutation, true otherwise.
3369 * Treats all permutations of the range as a set of "dictionary" sorted
3370 * sequences. Permutes the current sequence into the next one of this set.
3371 * Returns true if there are more sequences to generate. If the sequence
3372 * is the largest of the set, the smallest is generated and false returned.
3374 template<typename _BidirectionalIterator
>
3376 next_permutation(_BidirectionalIterator __first
,
3377 _BidirectionalIterator __last
)
3379 // concept requirements
3380 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3381 _BidirectionalIterator
>)
3382 __glibcxx_function_requires(_LessThanComparableConcept
<
3383 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3384 __glibcxx_requires_valid_range(__first
, __last
);
3386 if (__first
== __last
)
3388 _BidirectionalIterator __i
= __first
;
3397 _BidirectionalIterator __ii
= __i
;
3401 _BidirectionalIterator __j
= __last
;
3402 while (!(*__i
< *--__j
))
3404 std::iter_swap(__i
, __j
);
3405 std::reverse(__ii
, __last
);
3410 std::reverse(__first
, __last
);
3417 * @brief Permute range into the next "dictionary" ordering using
3418 * comparison functor.
3419 * @param first Start of range.
3420 * @param last End of range.
3422 * @return False if wrapped to first permutation, true otherwise.
3424 * Treats all permutations of the range [first,last) as a set of
3425 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3426 * sequence into the next one of this set. Returns true if there are more
3427 * sequences to generate. If the sequence is the largest of the set, the
3428 * smallest is generated and false returned.
3430 template<typename _BidirectionalIterator
, typename _Compare
>
3432 next_permutation(_BidirectionalIterator __first
,
3433 _BidirectionalIterator __last
, _Compare __comp
)
3435 // concept requirements
3436 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3437 _BidirectionalIterator
>)
3438 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3439 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3440 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3441 __glibcxx_requires_valid_range(__first
, __last
);
3443 if (__first
== __last
)
3445 _BidirectionalIterator __i
= __first
;
3454 _BidirectionalIterator __ii
= __i
;
3456 if (__comp(*__i
, *__ii
))
3458 _BidirectionalIterator __j
= __last
;
3459 while (!bool(__comp(*__i
, *--__j
)))
3461 std::iter_swap(__i
, __j
);
3462 std::reverse(__ii
, __last
);
3467 std::reverse(__first
, __last
);
3474 * @brief Permute range into the previous "dictionary" ordering.
3475 * @param first Start of range.
3476 * @param last End of range.
3477 * @return False if wrapped to last permutation, true otherwise.
3479 * Treats all permutations of the range as a set of "dictionary" sorted
3480 * sequences. Permutes the current sequence into the previous one of this
3481 * set. Returns true if there are more sequences to generate. If the
3482 * sequence is the smallest of the set, the largest is generated and false
3485 template<typename _BidirectionalIterator
>
3487 prev_permutation(_BidirectionalIterator __first
,
3488 _BidirectionalIterator __last
)
3490 // concept requirements
3491 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3492 _BidirectionalIterator
>)
3493 __glibcxx_function_requires(_LessThanComparableConcept
<
3494 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3495 __glibcxx_requires_valid_range(__first
, __last
);
3497 if (__first
== __last
)
3499 _BidirectionalIterator __i
= __first
;
3508 _BidirectionalIterator __ii
= __i
;
3512 _BidirectionalIterator __j
= __last
;
3513 while (!(*--__j
< *__i
))
3515 std::iter_swap(__i
, __j
);
3516 std::reverse(__ii
, __last
);
3521 std::reverse(__first
, __last
);
3528 * @brief Permute range into the previous "dictionary" ordering using
3529 * comparison functor.
3530 * @param first Start of range.
3531 * @param last End of range.
3533 * @return False if wrapped to last permutation, true otherwise.
3535 * Treats all permutations of the range [first,last) as a set of
3536 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3537 * sequence into the previous one of this set. Returns true if there are
3538 * more sequences to generate. If the sequence is the smallest of the set,
3539 * the largest is generated and false returned.
3541 template<typename _BidirectionalIterator
, typename _Compare
>
3543 prev_permutation(_BidirectionalIterator __first
,
3544 _BidirectionalIterator __last
, _Compare __comp
)
3546 // concept requirements
3547 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3548 _BidirectionalIterator
>)
3549 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3550 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3551 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3552 __glibcxx_requires_valid_range(__first
, __last
);
3554 if (__first
== __last
)
3556 _BidirectionalIterator __i
= __first
;
3565 _BidirectionalIterator __ii
= __i
;
3567 if (__comp(*__ii
, *__i
))
3569 _BidirectionalIterator __j
= __last
;
3570 while (!bool(__comp(*--__j
, *__i
)))
3572 std::iter_swap(__i
, __j
);
3573 std::reverse(__ii
, __last
);
3578 std::reverse(__first
, __last
);
3588 * @brief Copy a sequence, replacing each element of one value with another
3590 * @param first An input iterator.
3591 * @param last An input iterator.
3592 * @param result An output iterator.
3593 * @param old_value The value to be replaced.
3594 * @param new_value The replacement value.
3595 * @return The end of the output sequence, @p result+(last-first).
3597 * Copies each element in the input range @p [first,last) to the
3598 * output range @p [result,result+(last-first)) replacing elements
3599 * equal to @p old_value with @p new_value.
3601 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3603 replace_copy(_InputIterator __first
, _InputIterator __last
,
3604 _OutputIterator __result
,
3605 const _Tp
& __old_value
, const _Tp
& __new_value
)
3607 // concept requirements
3608 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3609 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3610 typename iterator_traits
<_InputIterator
>::value_type
>)
3611 __glibcxx_function_requires(_EqualOpConcept
<
3612 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3613 __glibcxx_requires_valid_range(__first
, __last
);
3615 for (; __first
!= __last
; ++__first
, ++__result
)
3616 if (*__first
== __old_value
)
3617 *__result
= __new_value
;
3619 *__result
= *__first
;
3624 * @brief Copy a sequence, replacing each value for which a predicate
3625 * returns true with another value.
3626 * @param first An input iterator.
3627 * @param last An input iterator.
3628 * @param result An output iterator.
3629 * @param pred A predicate.
3630 * @param new_value The replacement value.
3631 * @return The end of the output sequence, @p result+(last-first).
3633 * Copies each element in the range @p [first,last) to the range
3634 * @p [result,result+(last-first)) replacing elements for which
3635 * @p pred returns true with @p new_value.
3637 template<typename _InputIterator
, typename _OutputIterator
,
3638 typename _Predicate
, typename _Tp
>
3640 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3641 _OutputIterator __result
,
3642 _Predicate __pred
, const _Tp
& __new_value
)
3644 // concept requirements
3645 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3646 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3647 typename iterator_traits
<_InputIterator
>::value_type
>)
3648 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3649 typename iterator_traits
<_InputIterator
>::value_type
>)
3650 __glibcxx_requires_valid_range(__first
, __last
);
3652 for (; __first
!= __last
; ++__first
, ++__result
)
3653 if (__pred(*__first
))
3654 *__result
= __new_value
;
3656 *__result
= *__first
;
3660 _GLIBCXX_END_NAMESPACE
3662 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3665 * @brief Apply a function to every element of a sequence.
3666 * @param first An input iterator.
3667 * @param last An input iterator.
3668 * @param f A unary function object.
3671 * Applies the function object @p f to each element in the range
3672 * @p [first,last). @p f must not modify the order of the sequence.
3673 * If @p f has a return value it is ignored.
3675 template<typename _InputIterator
, typename _Function
>
3677 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3679 // concept requirements
3680 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3681 __glibcxx_requires_valid_range(__first
, __last
);
3682 for (; __first
!= __last
; ++__first
)
3688 * @brief Find the first occurrence of a value in a sequence.
3689 * @param first An input iterator.
3690 * @param last An input iterator.
3691 * @param val The value to find.
3692 * @return The first iterator @c i in the range @p [first,last)
3693 * such that @c *i == @p val, or @p last if no such iterator exists.
3695 template<typename _InputIterator
, typename _Tp
>
3696 inline _InputIterator
3697 find(_InputIterator __first
, _InputIterator __last
,
3700 // concept requirements
3701 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3702 __glibcxx_function_requires(_EqualOpConcept
<
3703 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3704 __glibcxx_requires_valid_range(__first
, __last
);
3705 return std::__find(__first
, __last
, __val
,
3706 std::__iterator_category(__first
));
3710 * @brief Find the first element in a sequence for which a
3711 * predicate is true.
3712 * @param first An input iterator.
3713 * @param last An input iterator.
3714 * @param pred A predicate.
3715 * @return The first iterator @c i in the range @p [first,last)
3716 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3718 template<typename _InputIterator
, typename _Predicate
>
3719 inline _InputIterator
3720 find_if(_InputIterator __first
, _InputIterator __last
,
3723 // concept requirements
3724 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3725 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3726 typename iterator_traits
<_InputIterator
>::value_type
>)
3727 __glibcxx_requires_valid_range(__first
, __last
);
3728 return std::__find_if(__first
, __last
, __pred
,
3729 std::__iterator_category(__first
));
3733 * @brief Find element from a set in a sequence.
3734 * @param first1 Start of range to search.
3735 * @param last1 End of range to search.
3736 * @param first2 Start of match candidates.
3737 * @param last2 End of match candidates.
3738 * @return The first iterator @c i in the range
3739 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
3740 * interator in [first2,last2), or @p last1 if no such iterator exists.
3742 * Searches the range @p [first1,last1) for an element that is equal to
3743 * some element in the range [first2,last2). If found, returns an iterator
3744 * in the range [first1,last1), otherwise returns @p last1.
3746 template<typename _InputIterator
, typename _ForwardIterator
>
3748 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3749 _ForwardIterator __first2
, _ForwardIterator __last2
)
3751 // concept requirements
3752 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3753 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3754 __glibcxx_function_requires(_EqualOpConcept
<
3755 typename iterator_traits
<_InputIterator
>::value_type
,
3756 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3757 __glibcxx_requires_valid_range(__first1
, __last1
);
3758 __glibcxx_requires_valid_range(__first2
, __last2
);
3760 for (; __first1
!= __last1
; ++__first1
)
3761 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3762 if (*__first1
== *__iter
)
3768 * @brief Find element from a set in a sequence using a predicate.
3769 * @param first1 Start of range to search.
3770 * @param last1 End of range to search.
3771 * @param first2 Start of match candidates.
3772 * @param last2 End of match candidates.
3773 * @param comp Predicate to use.
3774 * @return The first iterator @c i in the range
3775 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
3776 * interator in [first2,last2), or @p last1 if no such iterator exists.
3779 * Searches the range @p [first1,last1) for an element that is
3780 * equal to some element in the range [first2,last2). If found,
3781 * returns an iterator in the range [first1,last1), otherwise
3784 template<typename _InputIterator
, typename _ForwardIterator
,
3785 typename _BinaryPredicate
>
3787 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3788 _ForwardIterator __first2
, _ForwardIterator __last2
,
3789 _BinaryPredicate __comp
)
3791 // concept requirements
3792 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3793 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3794 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3795 typename iterator_traits
<_InputIterator
>::value_type
,
3796 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3797 __glibcxx_requires_valid_range(__first1
, __last1
);
3798 __glibcxx_requires_valid_range(__first2
, __last2
);
3800 for (; __first1
!= __last1
; ++__first1
)
3801 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3802 if (__comp(*__first1
, *__iter
))
3808 * @brief Find two adjacent values in a sequence that are equal.
3809 * @param first A forward iterator.
3810 * @param last A forward iterator.
3811 * @return The first iterator @c i such that @c i and @c i+1 are both
3812 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
3813 * or @p last if no such iterator exists.
3815 template<typename _ForwardIterator
>
3817 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3819 // concept requirements
3820 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3821 __glibcxx_function_requires(_EqualityComparableConcept
<
3822 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3823 __glibcxx_requires_valid_range(__first
, __last
);
3824 if (__first
== __last
)
3826 _ForwardIterator __next
= __first
;
3827 while(++__next
!= __last
)
3829 if (*__first
== *__next
)
3837 * @brief Find two adjacent values in a sequence using a predicate.
3838 * @param first A forward iterator.
3839 * @param last A forward iterator.
3840 * @param binary_pred A binary predicate.
3841 * @return The first iterator @c i such that @c i and @c i+1 are both
3842 * valid iterators in @p [first,last) and such that
3843 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
3846 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3848 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3849 _BinaryPredicate __binary_pred
)
3851 // concept requirements
3852 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3853 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3854 typename iterator_traits
<_ForwardIterator
>::value_type
,
3855 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3856 __glibcxx_requires_valid_range(__first
, __last
);
3857 if (__first
== __last
)
3859 _ForwardIterator __next
= __first
;
3860 while(++__next
!= __last
)
3862 if (__binary_pred(*__first
, *__next
))
3870 * @brief Count the number of copies of a value in a sequence.
3871 * @param first An input iterator.
3872 * @param last An input iterator.
3873 * @param value The value to be counted.
3874 * @return The number of iterators @c i in the range @p [first,last)
3875 * for which @c *i == @p value
3877 template<typename _InputIterator
, typename _Tp
>
3878 typename iterator_traits
<_InputIterator
>::difference_type
3879 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
3881 // concept requirements
3882 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3883 __glibcxx_function_requires(_EqualOpConcept
<
3884 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3885 __glibcxx_requires_valid_range(__first
, __last
);
3886 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3887 for (; __first
!= __last
; ++__first
)
3888 if (*__first
== __value
)
3894 * @brief Count the elements of a sequence for which a predicate is true.
3895 * @param first An input iterator.
3896 * @param last An input iterator.
3897 * @param pred A predicate.
3898 * @return The number of iterators @c i in the range @p [first,last)
3899 * for which @p pred(*i) is true.
3901 template<typename _InputIterator
, typename _Predicate
>
3902 typename iterator_traits
<_InputIterator
>::difference_type
3903 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3905 // concept requirements
3906 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3907 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3908 typename iterator_traits
<_InputIterator
>::value_type
>)
3909 __glibcxx_requires_valid_range(__first
, __last
);
3910 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3911 for (; __first
!= __last
; ++__first
)
3912 if (__pred(*__first
))
3918 * @brief Search a sequence for a matching sub-sequence.
3919 * @param first1 A forward iterator.
3920 * @param last1 A forward iterator.
3921 * @param first2 A forward iterator.
3922 * @param last2 A forward iterator.
3923 * @return The first iterator @c i in the range
3924 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
3925 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
3926 * such iterator exists.
3928 * Searches the range @p [first1,last1) for a sub-sequence that compares
3929 * equal value-by-value with the sequence given by @p [first2,last2) and
3930 * returns an iterator to the first element of the sub-sequence, or
3931 * @p last1 if the sub-sequence is not found.
3933 * Because the sub-sequence must lie completely within the range
3934 * @p [first1,last1) it must start at a position less than
3935 * @p last1-(last2-first2) where @p last2-first2 is the length of the
3937 * This means that the returned iterator @c i will be in the range
3938 * @p [first1,last1-(last2-first2))
3940 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3942 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3943 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3945 // concept requirements
3946 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3947 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3948 __glibcxx_function_requires(_EqualOpConcept
<
3949 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3950 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3951 __glibcxx_requires_valid_range(__first1
, __last1
);
3952 __glibcxx_requires_valid_range(__first2
, __last2
);
3954 // Test for empty ranges
3955 if (__first1
== __last1
|| __first2
== __last2
)
3958 // Test for a pattern of length 1.
3959 _ForwardIterator2
__p1(__first2
);
3960 if (++__p1
== __last2
)
3961 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
3964 _ForwardIterator2 __p
;
3965 _ForwardIterator1 __current
= __first1
;
3969 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
3970 if (__first1
== __last1
)
3974 __current
= __first1
;
3975 if (++__current
== __last1
)
3978 while (*__current
== *__p
)
3980 if (++__p
== __last2
)
3982 if (++__current
== __last1
)
3991 * @brief Search a sequence for a matching sub-sequence using a predicate.
3992 * @param first1 A forward iterator.
3993 * @param last1 A forward iterator.
3994 * @param first2 A forward iterator.
3995 * @param last2 A forward iterator.
3996 * @param predicate A binary predicate.
3997 * @return The first iterator @c i in the range
3998 * @p [first1,last1-(last2-first2)) such that
3999 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4000 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4002 * Searches the range @p [first1,last1) for a sub-sequence that compares
4003 * equal value-by-value with the sequence given by @p [first2,last2),
4004 * using @p predicate to determine equality, and returns an iterator
4005 * to the first element of the sub-sequence, or @p last1 if no such
4008 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4010 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4011 typename _BinaryPredicate
>
4013 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4014 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4015 _BinaryPredicate __predicate
)
4017 // concept requirements
4018 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4019 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4020 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4021 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4022 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4023 __glibcxx_requires_valid_range(__first1
, __last1
);
4024 __glibcxx_requires_valid_range(__first2
, __last2
);
4026 // Test for empty ranges
4027 if (__first1
== __last1
|| __first2
== __last2
)
4030 // Test for a pattern of length 1.
4031 _ForwardIterator2
__p1(__first2
);
4032 if (++__p1
== __last2
)
4034 while (__first1
!= __last1
4035 && !bool(__predicate(*__first1
, *__first2
)))
4041 _ForwardIterator2 __p
;
4042 _ForwardIterator1 __current
= __first1
;
4046 while (__first1
!= __last1
4047 && !bool(__predicate(*__first1
, *__first2
)))
4049 if (__first1
== __last1
)
4053 __current
= __first1
;
4054 if (++__current
== __last1
)
4057 while (__predicate(*__current
, *__p
))
4059 if (++__p
== __last2
)
4061 if (++__current
== __last1
)
4071 * @brief Search a sequence for a number of consecutive values.
4072 * @param first A forward iterator.
4073 * @param last A forward iterator.
4074 * @param count The number of consecutive values.
4075 * @param val The value to find.
4076 * @return The first iterator @c i in the range @p [first,last-count)
4077 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4078 * or @p last if no such iterator exists.
4080 * Searches the range @p [first,last) for @p count consecutive elements
4083 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4085 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4086 _Integer __count
, const _Tp
& __val
)
4088 // concept requirements
4089 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4090 __glibcxx_function_requires(_EqualOpConcept
<
4091 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4092 __glibcxx_requires_valid_range(__first
, __last
);
4097 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4098 return std::__search_n(__first
, __last
, __count
, __val
,
4099 std::__iterator_category(__first
));
4104 * @brief Search a sequence for a number of consecutive values using a
4106 * @param first A forward iterator.
4107 * @param last A forward iterator.
4108 * @param count The number of consecutive values.
4109 * @param val The value to find.
4110 * @param binary_pred A binary predicate.
4111 * @return The first iterator @c i in the range @p [first,last-count)
4112 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4113 * range @p [0,count), or @p last if no such iterator exists.
4115 * Searches the range @p [first,last) for @p count consecutive elements
4116 * for which the predicate returns true.
4118 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4119 typename _BinaryPredicate
>
4121 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4122 _Integer __count
, const _Tp
& __val
,
4123 _BinaryPredicate __binary_pred
)
4125 // concept requirements
4126 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4127 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4128 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4129 __glibcxx_requires_valid_range(__first
, __last
);
4135 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4139 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4140 std::__iterator_category(__first
));
4145 * @brief Perform an operation on a sequence.
4146 * @param first An input iterator.
4147 * @param last An input iterator.
4148 * @param result An output iterator.
4149 * @param unary_op A unary operator.
4150 * @return An output iterator equal to @p result+(last-first).
4152 * Applies the operator to each element in the input range and assigns
4153 * the results to successive elements of the output sequence.
4154 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4155 * range @p [0,last-first).
4157 * @p unary_op must not alter its argument.
4159 template<typename _InputIterator
, typename _OutputIterator
,
4160 typename _UnaryOperation
>
4162 transform(_InputIterator __first
, _InputIterator __last
,
4163 _OutputIterator __result
, _UnaryOperation __unary_op
)
4165 // concept requirements
4166 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4167 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4168 // "the type returned by a _UnaryOperation"
4169 __typeof__(__unary_op(*__first
))>)
4170 __glibcxx_requires_valid_range(__first
, __last
);
4172 for (; __first
!= __last
; ++__first
, ++__result
)
4173 *__result
= __unary_op(*__first
);
4178 * @brief Perform an operation on corresponding elements of two sequences.
4179 * @param first1 An input iterator.
4180 * @param last1 An input iterator.
4181 * @param first2 An input iterator.
4182 * @param result An output iterator.
4183 * @param binary_op A binary operator.
4184 * @return An output iterator equal to @p result+(last-first).
4186 * Applies the operator to the corresponding elements in the two
4187 * input ranges and assigns the results to successive elements of the
4189 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4190 * @c N in the range @p [0,last1-first1).
4192 * @p binary_op must not alter either of its arguments.
4194 template<typename _InputIterator1
, typename _InputIterator2
,
4195 typename _OutputIterator
, typename _BinaryOperation
>
4197 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4198 _InputIterator2 __first2
, _OutputIterator __result
,
4199 _BinaryOperation __binary_op
)
4201 // concept requirements
4202 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4203 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4204 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4205 // "the type returned by a _BinaryOperation"
4206 __typeof__(__binary_op(*__first1
,*__first2
))>)
4207 __glibcxx_requires_valid_range(__first1
, __last1
);
4209 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4210 *__result
= __binary_op(*__first1
, *__first2
);
4215 * @brief Replace each occurrence of one value in a sequence with another
4217 * @param first A forward iterator.
4218 * @param last A forward iterator.
4219 * @param old_value The value to be replaced.
4220 * @param new_value The replacement value.
4221 * @return replace() returns no value.
4223 * For each iterator @c i in the range @p [first,last) if @c *i ==
4224 * @p old_value then the assignment @c *i = @p new_value is performed.
4226 template<typename _ForwardIterator
, typename _Tp
>
4228 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4229 const _Tp
& __old_value
, const _Tp
& __new_value
)
4231 // concept requirements
4232 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4234 __glibcxx_function_requires(_EqualOpConcept
<
4235 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4236 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4237 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4238 __glibcxx_requires_valid_range(__first
, __last
);
4240 for (; __first
!= __last
; ++__first
)
4241 if (*__first
== __old_value
)
4242 *__first
= __new_value
;
4246 * @brief Replace each value in a sequence for which a predicate returns
4247 * true with another value.
4248 * @param first A forward iterator.
4249 * @param last A forward iterator.
4250 * @param pred A predicate.
4251 * @param new_value The replacement value.
4252 * @return replace_if() returns no value.
4254 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4255 * is true then the assignment @c *i = @p new_value is performed.
4257 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4259 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4260 _Predicate __pred
, const _Tp
& __new_value
)
4262 // concept requirements
4263 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4265 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4266 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4267 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4268 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4269 __glibcxx_requires_valid_range(__first
, __last
);
4271 for (; __first
!= __last
; ++__first
)
4272 if (__pred(*__first
))
4273 *__first
= __new_value
;
4277 * @brief Assign the result of a function object to each value in a
4279 * @param first A forward iterator.
4280 * @param last A forward iterator.
4281 * @param gen A function object taking no arguments and returning
4282 * std::iterator_traits<_ForwardIterator>::value_type
4283 * @return generate() returns no value.
4285 * Performs the assignment @c *i = @p gen() for each @c i in the range
4288 template<typename _ForwardIterator
, typename _Generator
>
4290 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4293 // concept requirements
4294 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4295 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4296 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4297 __glibcxx_requires_valid_range(__first
, __last
);
4299 for (; __first
!= __last
; ++__first
)
4304 * @brief Assign the result of a function object to each value in a
4306 * @param first A forward iterator.
4307 * @param n The length of the sequence.
4308 * @param gen A function object taking no arguments and returning
4309 * std::iterator_traits<_ForwardIterator>::value_type
4310 * @return The end of the sequence, @p first+n
4312 * Performs the assignment @c *i = @p gen() for each @c i in the range
4313 * @p [first,first+n).
4315 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4317 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4319 // concept requirements
4320 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4321 // "the type returned by a _Generator"
4322 __typeof__(__gen())>)
4324 for (; __n
> 0; --__n
, ++__first
)
4331 * @brief Copy a sequence, removing consecutive duplicate values.
4332 * @param first An input iterator.
4333 * @param last An input iterator.
4334 * @param result An output iterator.
4335 * @return An iterator designating the end of the resulting sequence.
4337 * Copies each element in the range @p [first,last) to the range
4338 * beginning at @p result, except that only the first element is copied
4339 * from groups of consecutive elements that compare equal.
4340 * unique_copy() is stable, so the relative order of elements that are
4341 * copied is unchanged.
4344 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4345 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4347 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4348 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4352 template<typename _InputIterator
, typename _OutputIterator
>
4353 inline _OutputIterator
4354 unique_copy(_InputIterator __first
, _InputIterator __last
,
4355 _OutputIterator __result
)
4357 // concept requirements
4358 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4359 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4360 typename iterator_traits
<_InputIterator
>::value_type
>)
4361 __glibcxx_function_requires(_EqualityComparableConcept
<
4362 typename iterator_traits
<_InputIterator
>::value_type
>)
4363 __glibcxx_requires_valid_range(__first
, __last
);
4365 if (__first
== __last
)
4367 return std::__unique_copy(__first
, __last
, __result
,
4368 std::__iterator_category(__first
),
4369 std::__iterator_category(__result
));
4373 * @brief Copy a sequence, removing consecutive values using a predicate.
4374 * @param first An input iterator.
4375 * @param last An input iterator.
4376 * @param result An output iterator.
4377 * @param binary_pred A binary predicate.
4378 * @return An iterator designating the end of the resulting sequence.
4380 * Copies each element in the range @p [first,last) to the range
4381 * beginning at @p result, except that only the first element is copied
4382 * from groups of consecutive elements for which @p binary_pred returns
4384 * unique_copy() is stable, so the relative order of elements that are
4385 * copied is unchanged.
4388 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4389 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4392 template<typename _InputIterator
, typename _OutputIterator
,
4393 typename _BinaryPredicate
>
4394 inline _OutputIterator
4395 unique_copy(_InputIterator __first
, _InputIterator __last
,
4396 _OutputIterator __result
,
4397 _BinaryPredicate __binary_pred
)
4399 // concept requirements -- predicates checked later
4400 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4401 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4402 typename iterator_traits
<_InputIterator
>::value_type
>)
4403 __glibcxx_requires_valid_range(__first
, __last
);
4405 if (__first
== __last
)
4407 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4408 std::__iterator_category(__first
),
4409 std::__iterator_category(__result
));
4414 * @brief Randomly shuffle the elements of a sequence.
4415 * @param first A forward iterator.
4416 * @param last A forward iterator.
4419 * Reorder the elements in the range @p [first,last) using a random
4420 * distribution, so that every possible ordering of the sequence is
4423 template<typename _RandomAccessIterator
>
4425 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4427 // concept requirements
4428 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4429 _RandomAccessIterator
>)
4430 __glibcxx_requires_valid_range(__first
, __last
);
4432 if (__first
!= __last
)
4433 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4434 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4438 * @brief Shuffle the elements of a sequence using a random number
4440 * @param first A forward iterator.
4441 * @param last A forward iterator.
4442 * @param rand The RNG functor or function.
4445 * Reorders the elements in the range @p [first,last) using @p rand to
4446 * provide a random distribution. Calling @p rand(N) for a positive
4447 * integer @p N should return a randomly chosen integer from the
4450 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4452 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4453 _RandomNumberGenerator
& __rand
)
4455 // concept requirements
4456 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4457 _RandomAccessIterator
>)
4458 __glibcxx_requires_valid_range(__first
, __last
);
4460 if (__first
== __last
)
4462 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4463 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4468 * @brief Move elements for which a predicate is true to the beginning
4470 * @param first A forward iterator.
4471 * @param last A forward iterator.
4472 * @param pred A predicate functor.
4473 * @return An iterator @p middle such that @p pred(i) is true for each
4474 * iterator @p i in the range @p [first,middle) and false for each @p i
4475 * in the range @p [middle,last).
4477 * @p pred must not modify its operand. @p partition() does not preserve
4478 * the relative ordering of elements in each group, use
4479 * @p stable_partition() if this is needed.
4481 template<typename _ForwardIterator
, typename _Predicate
>
4482 inline _ForwardIterator
4483 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4486 // concept requirements
4487 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4489 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4490 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4491 __glibcxx_requires_valid_range(__first
, __last
);
4493 return std::__partition(__first
, __last
, __pred
,
4494 std::__iterator_category(__first
));
4500 * @brief Sort the smallest elements of a sequence.
4501 * @param first An iterator.
4502 * @param middle Another iterator.
4503 * @param last Another iterator.
4506 * Sorts the smallest @p (middle-first) elements in the range
4507 * @p [first,last) and moves them to the range @p [first,middle). The
4508 * order of the remaining elements in the range @p [middle,last) is
4510 * After the sort if @p i and @j are iterators in the range
4511 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4512 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4514 template<typename _RandomAccessIterator
>
4516 partial_sort(_RandomAccessIterator __first
,
4517 _RandomAccessIterator __middle
,
4518 _RandomAccessIterator __last
)
4520 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4523 // concept requirements
4524 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4525 _RandomAccessIterator
>)
4526 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4527 __glibcxx_requires_valid_range(__first
, __middle
);
4528 __glibcxx_requires_valid_range(__middle
, __last
);
4530 std::__heap_select(__first
, __middle
, __last
);
4531 std::sort_heap(__first
, __middle
);
4535 * @brief Sort the smallest elements of a sequence using a predicate
4537 * @param first An iterator.
4538 * @param middle Another iterator.
4539 * @param last Another iterator.
4540 * @param comp A comparison functor.
4543 * Sorts the smallest @p (middle-first) elements in the range
4544 * @p [first,last) and moves them to the range @p [first,middle). The
4545 * order of the remaining elements in the range @p [middle,last) is
4547 * After the sort if @p i and @j are iterators in the range
4548 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4549 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4552 template<typename _RandomAccessIterator
, typename _Compare
>
4554 partial_sort(_RandomAccessIterator __first
,
4555 _RandomAccessIterator __middle
,
4556 _RandomAccessIterator __last
,
4559 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4562 // concept requirements
4563 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4564 _RandomAccessIterator
>)
4565 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4566 _ValueType
, _ValueType
>)
4567 __glibcxx_requires_valid_range(__first
, __middle
);
4568 __glibcxx_requires_valid_range(__middle
, __last
);
4570 std::__heap_select(__first
, __middle
, __last
, __comp
);
4571 std::sort_heap(__first
, __middle
, __comp
);
4575 * @brief Sort a sequence just enough to find a particular position.
4576 * @param first An iterator.
4577 * @param nth Another iterator.
4578 * @param last Another iterator.
4581 * Rearranges the elements in the range @p [first,last) so that @p *nth
4582 * is the same element that would have been in that position had the
4583 * whole sequence been sorted.
4584 * whole sequence been sorted. The elements either side of @p *nth are
4585 * not completely sorted, but for any iterator @i in the range
4586 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4587 * holds that @p *j<*i is false.
4589 template<typename _RandomAccessIterator
>
4591 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4592 _RandomAccessIterator __last
)
4594 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4597 // concept requirements
4598 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4599 _RandomAccessIterator
>)
4600 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4601 __glibcxx_requires_valid_range(__first
, __nth
);
4602 __glibcxx_requires_valid_range(__nth
, __last
);
4604 if (__first
== __last
|| __nth
== __last
)
4607 std::__introselect(__first
, __nth
, __last
,
4608 std::__lg(__last
- __first
) * 2);
4612 * @brief Sort a sequence just enough to find a particular position
4613 * using a predicate for comparison.
4614 * @param first An iterator.
4615 * @param nth Another iterator.
4616 * @param last Another iterator.
4617 * @param comp A comparison functor.
4620 * Rearranges the elements in the range @p [first,last) so that @p *nth
4621 * is the same element that would have been in that position had the
4622 * whole sequence been sorted. The elements either side of @p *nth are
4623 * not completely sorted, but for any iterator @i in the range
4624 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4625 * holds that @p comp(*j,*i) is false.
4627 template<typename _RandomAccessIterator
, typename _Compare
>
4629 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4630 _RandomAccessIterator __last
, _Compare __comp
)
4632 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4635 // concept requirements
4636 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4637 _RandomAccessIterator
>)
4638 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4639 _ValueType
, _ValueType
>)
4640 __glibcxx_requires_valid_range(__first
, __nth
);
4641 __glibcxx_requires_valid_range(__nth
, __last
);
4643 if (__first
== __last
|| __nth
== __last
)
4646 std::__introselect(__first
, __nth
, __last
,
4647 std::__lg(__last
- __first
) * 2, __comp
);
4652 * @brief Sort the elements of a sequence.
4653 * @param first An iterator.
4654 * @param last Another iterator.
4657 * Sorts the elements in the range @p [first,last) in ascending order,
4658 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4659 * @p [first,last-1).
4661 * The relative ordering of equivalent elements is not preserved, use
4662 * @p stable_sort() if this is needed.
4664 template<typename _RandomAccessIterator
>
4666 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4668 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4671 // concept requirements
4672 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4673 _RandomAccessIterator
>)
4674 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4675 __glibcxx_requires_valid_range(__first
, __last
);
4677 if (__first
!= __last
)
4679 std::__introsort_loop(__first
, __last
,
4680 std::__lg(__last
- __first
) * 2);
4681 std::__final_insertion_sort(__first
, __last
);
4686 * @brief Sort the elements of a sequence using a predicate for comparison.
4687 * @param first An iterator.
4688 * @param last Another iterator.
4689 * @param comp A comparison functor.
4692 * Sorts the elements in the range @p [first,last) in ascending order,
4693 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4694 * range @p [first,last-1).
4696 * The relative ordering of equivalent elements is not preserved, use
4697 * @p stable_sort() if this is needed.
4699 template<typename _RandomAccessIterator
, typename _Compare
>
4701 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4704 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4707 // concept requirements
4708 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4709 _RandomAccessIterator
>)
4710 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
4712 __glibcxx_requires_valid_range(__first
, __last
);
4714 if (__first
!= __last
)
4716 std::__introsort_loop(__first
, __last
,
4717 std::__lg(__last
- __first
) * 2, __comp
);
4718 std::__final_insertion_sort(__first
, __last
, __comp
);
4723 * @brief Merges two sorted ranges.
4724 * @param first1 An iterator.
4725 * @param first2 Another iterator.
4726 * @param last1 Another iterator.
4727 * @param last2 Another iterator.
4728 * @param result An iterator pointing to the end of the merged range.
4729 * @return An iterator pointing to the first element "not less
4732 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4733 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4734 * must be sorted, and the output range must not overlap with either of
4735 * the input ranges. The sort is @e stable, that is, for equivalent
4736 * elements in the two ranges, elements from the first range will always
4737 * come before elements from the second.
4739 template<typename _InputIterator1
, typename _InputIterator2
,
4740 typename _OutputIterator
>
4742 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4743 _InputIterator2 __first2
, _InputIterator2 __last2
,
4744 _OutputIterator __result
)
4746 typedef typename iterator_traits
<_InputIterator1
>::value_type
4748 typedef typename iterator_traits
<_InputIterator2
>::value_type
4751 // concept requirements
4752 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4754 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4756 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4758 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4759 __glibcxx_requires_sorted(__first1
, __last1
);
4760 __glibcxx_requires_sorted(__first2
, __last2
);
4762 while (__first1
!= __last1
&& __first2
!= __last2
)
4764 if (*__first2
< *__first1
)
4766 *__result
= *__first2
;
4771 *__result
= *__first1
;
4776 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4781 * @brief Merges two sorted ranges.
4782 * @param first1 An iterator.
4783 * @param first2 Another iterator.
4784 * @param last1 Another iterator.
4785 * @param last2 Another iterator.
4786 * @param result An iterator pointing to the end of the merged range.
4787 * @param comp A functor to use for comparisons.
4788 * @return An iterator pointing to the first element "not less
4791 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4792 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4793 * must be sorted, and the output range must not overlap with either of
4794 * the input ranges. The sort is @e stable, that is, for equivalent
4795 * elements in the two ranges, elements from the first range will always
4796 * come before elements from the second.
4798 * The comparison function should have the same effects on ordering as
4799 * the function used for the initial sort.
4801 template<typename _InputIterator1
, typename _InputIterator2
,
4802 typename _OutputIterator
, typename _Compare
>
4804 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4805 _InputIterator2 __first2
, _InputIterator2 __last2
,
4806 _OutputIterator __result
, _Compare __comp
)
4808 typedef typename iterator_traits
<_InputIterator1
>::value_type
4810 typedef typename iterator_traits
<_InputIterator2
>::value_type
4813 // concept requirements
4814 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4815 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4816 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4818 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4820 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4821 _ValueType2
, _ValueType1
>)
4822 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4823 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4825 while (__first1
!= __last1
&& __first2
!= __last2
)
4827 if (__comp(*__first2
, *__first1
))
4829 *__result
= *__first2
;
4834 *__result
= *__first1
;
4839 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4845 * @brief Sort the elements of a sequence, preserving the relative order
4846 * of equivalent elements.
4847 * @param first An iterator.
4848 * @param last Another iterator.
4851 * Sorts the elements in the range @p [first,last) in ascending order,
4852 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4853 * @p [first,last-1).
4855 * The relative ordering of equivalent elements is preserved, so any two
4856 * elements @p x and @p y in the range @p [first,last) such that
4857 * @p x<y is false and @p y<x is false will have the same relative
4858 * ordering after calling @p stable_sort().
4860 template<typename _RandomAccessIterator
>
4862 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4864 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4866 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4869 // concept requirements
4870 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4871 _RandomAccessIterator
>)
4872 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4873 __glibcxx_requires_valid_range(__first
, __last
);
4875 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4877 if (__buf
.begin() == 0)
4878 std::__inplace_stable_sort(__first
, __last
);
4880 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4881 _DistanceType(__buf
.size()));
4885 * @brief Sort the elements of a sequence using a predicate for comparison,
4886 * preserving the relative order of equivalent elements.
4887 * @param first An iterator.
4888 * @param last Another iterator.
4889 * @param comp A comparison functor.
4892 * Sorts the elements in the range @p [first,last) in ascending order,
4893 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
4894 * range @p [first,last-1).
4896 * The relative ordering of equivalent elements is preserved, so any two
4897 * elements @p x and @p y in the range @p [first,last) such that
4898 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
4899 * relative ordering after calling @p stable_sort().
4901 template<typename _RandomAccessIterator
, typename _Compare
>
4903 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4906 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4908 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4911 // concept requirements
4912 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4913 _RandomAccessIterator
>)
4914 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4917 __glibcxx_requires_valid_range(__first
, __last
);
4919 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4921 if (__buf
.begin() == 0)
4922 std::__inplace_stable_sort(__first
, __last
, __comp
);
4924 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4925 _DistanceType(__buf
.size()), __comp
);
4930 * @brief Return the union of two sorted ranges.
4931 * @param first1 Start of first range.
4932 * @param last1 End of first range.
4933 * @param first2 Start of second range.
4934 * @param last2 End of second range.
4935 * @return End of the output range.
4936 * @ingroup setoperations
4938 * This operation iterates over both ranges, copying elements present in
4939 * each range in order to the output range. Iterators increment for each
4940 * range. When the current element of one range is less than the other,
4941 * that element is copied and the iterator advanced. If an element is
4942 * contained in both ranges, the element from the first range is copied and
4943 * both ranges advance. The output range may not overlap either input
4946 template<typename _InputIterator1
, typename _InputIterator2
,
4947 typename _OutputIterator
>
4949 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4950 _InputIterator2 __first2
, _InputIterator2 __last2
,
4951 _OutputIterator __result
)
4953 typedef typename iterator_traits
<_InputIterator1
>::value_type
4955 typedef typename iterator_traits
<_InputIterator2
>::value_type
4958 // concept requirements
4959 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4960 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4961 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4963 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4965 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4966 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4967 __glibcxx_requires_sorted(__first1
, __last1
);
4968 __glibcxx_requires_sorted(__first2
, __last2
);
4970 while (__first1
!= __last1
&& __first2
!= __last2
)
4972 if (*__first1
< *__first2
)
4974 *__result
= *__first1
;
4977 else if (*__first2
< *__first1
)
4979 *__result
= *__first2
;
4984 *__result
= *__first1
;
4990 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4995 * @brief Return the union of two sorted ranges using a comparison functor.
4996 * @param first1 Start of first range.
4997 * @param last1 End of first range.
4998 * @param first2 Start of second range.
4999 * @param last2 End of second range.
5000 * @param comp The comparison functor.
5001 * @return End of the output range.
5002 * @ingroup setoperations
5004 * This operation iterates over both ranges, copying elements present in
5005 * each range in order to the output range. Iterators increment for each
5006 * range. When the current element of one range is less than the other
5007 * according to @a comp, that element is copied and the iterator advanced.
5008 * If an equivalent element according to @a comp is contained in both
5009 * ranges, the element from the first range is copied and both ranges
5010 * advance. The output range may not overlap either input range.
5012 template<typename _InputIterator1
, typename _InputIterator2
,
5013 typename _OutputIterator
, typename _Compare
>
5015 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5016 _InputIterator2 __first2
, _InputIterator2 __last2
,
5017 _OutputIterator __result
, _Compare __comp
)
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(_BinaryPredicateConcept
<_Compare
,
5032 _ValueType1
, _ValueType2
>)
5033 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5034 _ValueType2
, _ValueType1
>)
5035 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5036 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5038 while (__first1
!= __last1
&& __first2
!= __last2
)
5040 if (__comp(*__first1
, *__first2
))
5042 *__result
= *__first1
;
5045 else if (__comp(*__first2
, *__first1
))
5047 *__result
= *__first2
;
5052 *__result
= *__first1
;
5058 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5063 * @brief Return the intersection of two sorted ranges.
5064 * @param first1 Start of first range.
5065 * @param last1 End of first range.
5066 * @param first2 Start of second range.
5067 * @param last2 End of second range.
5068 * @return End of the output range.
5069 * @ingroup setoperations
5071 * This operation iterates over both ranges, copying elements present in
5072 * both ranges in order to the output range. Iterators increment for each
5073 * range. When the current element of one range is less than the other,
5074 * that iterator advances. If an element is contained in both ranges, the
5075 * element from the first range is copied and both ranges advance. The
5076 * output range may not overlap either input range.
5078 template<typename _InputIterator1
, typename _InputIterator2
,
5079 typename _OutputIterator
>
5081 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5082 _InputIterator2 __first2
, _InputIterator2 __last2
,
5083 _OutputIterator __result
)
5085 typedef typename iterator_traits
<_InputIterator1
>::value_type
5087 typedef typename iterator_traits
<_InputIterator2
>::value_type
5090 // concept requirements
5091 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5092 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5093 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5095 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5096 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5097 __glibcxx_requires_sorted(__first1
, __last1
);
5098 __glibcxx_requires_sorted(__first2
, __last2
);
5100 while (__first1
!= __last1
&& __first2
!= __last2
)
5101 if (*__first1
< *__first2
)
5103 else if (*__first2
< *__first1
)
5107 *__result
= *__first1
;
5116 * @brief Return the intersection of two sorted ranges using comparison
5118 * @param first1 Start of first range.
5119 * @param last1 End of first range.
5120 * @param first2 Start of second range.
5121 * @param last2 End of second range.
5122 * @param comp The comparison functor.
5123 * @return End of the output range.
5124 * @ingroup setoperations
5126 * This operation iterates over both ranges, copying elements present in
5127 * both ranges in order to the output range. Iterators increment for each
5128 * range. When the current element of one range is less than the other
5129 * according to @a comp, that iterator advances. If an element is
5130 * contained in both ranges according to @a comp, the element from the
5131 * first range is copied and both ranges advance. The output range may not
5132 * overlap either input range.
5134 template<typename _InputIterator1
, typename _InputIterator2
,
5135 typename _OutputIterator
, typename _Compare
>
5137 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5138 _InputIterator2 __first2
, _InputIterator2 __last2
,
5139 _OutputIterator __result
, _Compare __comp
)
5141 typedef typename iterator_traits
<_InputIterator1
>::value_type
5143 typedef typename iterator_traits
<_InputIterator2
>::value_type
5146 // concept requirements
5147 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5148 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5149 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5151 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5152 _ValueType1
, _ValueType2
>)
5153 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5154 _ValueType2
, _ValueType1
>)
5155 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5156 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5158 while (__first1
!= __last1
&& __first2
!= __last2
)
5159 if (__comp(*__first1
, *__first2
))
5161 else if (__comp(*__first2
, *__first1
))
5165 *__result
= *__first1
;
5174 * @brief Return the difference of two sorted ranges.
5175 * @param first1 Start of first range.
5176 * @param last1 End of first range.
5177 * @param first2 Start of second range.
5178 * @param last2 End of second range.
5179 * @return End of the output range.
5180 * @ingroup setoperations
5182 * This operation iterates over both ranges, copying elements present in
5183 * the first range but not the second in order to the output range.
5184 * Iterators increment for each range. When the current element of the
5185 * first range is less than the second, that element is copied and the
5186 * iterator advances. If the current element of the second range is less,
5187 * the iterator advances, but no element is copied. If an element is
5188 * contained in both ranges, no elements are copied and both ranges
5189 * advance. The output range may not overlap either input range.
5191 template<typename _InputIterator1
, typename _InputIterator2
,
5192 typename _OutputIterator
>
5194 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5195 _InputIterator2 __first2
, _InputIterator2 __last2
,
5196 _OutputIterator __result
)
5198 typedef typename iterator_traits
<_InputIterator1
>::value_type
5200 typedef typename iterator_traits
<_InputIterator2
>::value_type
5203 // concept requirements
5204 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5205 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5206 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5208 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5209 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5210 __glibcxx_requires_sorted(__first1
, __last1
);
5211 __glibcxx_requires_sorted(__first2
, __last2
);
5213 while (__first1
!= __last1
&& __first2
!= __last2
)
5214 if (*__first1
< *__first2
)
5216 *__result
= *__first1
;
5220 else if (*__first2
< *__first1
)
5227 return std::copy(__first1
, __last1
, __result
);
5231 * @brief Return the difference of two sorted ranges using comparison
5233 * @param first1 Start of first range.
5234 * @param last1 End of first range.
5235 * @param first2 Start of second range.
5236 * @param last2 End of second range.
5237 * @param comp The comparison functor.
5238 * @return End of the output range.
5239 * @ingroup setoperations
5241 * This operation iterates over both ranges, copying elements present in
5242 * the first range but not the second in order to the output range.
5243 * Iterators increment for each range. When the current element of the
5244 * first range is less than the second according to @a comp, that element
5245 * is copied and the iterator advances. If the current element of the
5246 * second range is less, no element is copied and the iterator advances.
5247 * If an element is contained in both ranges according to @a comp, no
5248 * elements are copied and both ranges advance. The output range may not
5249 * overlap either input range.
5251 template<typename _InputIterator1
, typename _InputIterator2
,
5252 typename _OutputIterator
, typename _Compare
>
5254 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5255 _InputIterator2 __first2
, _InputIterator2 __last2
,
5256 _OutputIterator __result
, _Compare __comp
)
5258 typedef typename iterator_traits
<_InputIterator1
>::value_type
5260 typedef typename iterator_traits
<_InputIterator2
>::value_type
5263 // concept requirements
5264 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5265 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5266 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5268 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5269 _ValueType1
, _ValueType2
>)
5270 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5271 _ValueType2
, _ValueType1
>)
5272 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5273 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5275 while (__first1
!= __last1
&& __first2
!= __last2
)
5276 if (__comp(*__first1
, *__first2
))
5278 *__result
= *__first1
;
5282 else if (__comp(*__first2
, *__first1
))
5289 return std::copy(__first1
, __last1
, __result
);
5293 * @brief Return the symmetric difference of two sorted ranges.
5294 * @param first1 Start of first range.
5295 * @param last1 End of first range.
5296 * @param first2 Start of second range.
5297 * @param last2 End of second range.
5298 * @return End of the output range.
5299 * @ingroup setoperations
5301 * This operation iterates over both ranges, copying elements present in
5302 * one range but not the other in order to the output range. Iterators
5303 * increment for each range. When the current element of one range is less
5304 * than the other, that element is copied and the iterator advances. If an
5305 * element is contained in both ranges, no elements are copied and both
5306 * ranges advance. The output range may not overlap either input range.
5308 template<typename _InputIterator1
, typename _InputIterator2
,
5309 typename _OutputIterator
>
5311 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5312 _InputIterator2 __first2
, _InputIterator2 __last2
,
5313 _OutputIterator __result
)
5315 typedef typename iterator_traits
<_InputIterator1
>::value_type
5317 typedef typename iterator_traits
<_InputIterator2
>::value_type
5320 // concept requirements
5321 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5322 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5323 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5325 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5327 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5328 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5329 __glibcxx_requires_sorted(__first1
, __last1
);
5330 __glibcxx_requires_sorted(__first2
, __last2
);
5332 while (__first1
!= __last1
&& __first2
!= __last2
)
5333 if (*__first1
< *__first2
)
5335 *__result
= *__first1
;
5339 else if (*__first2
< *__first1
)
5341 *__result
= *__first2
;
5350 return std::copy(__first2
, __last2
, std::copy(__first1
,
5351 __last1
, __result
));
5355 * @brief Return the symmetric difference of two sorted ranges using
5356 * comparison functor.
5357 * @param first1 Start of first range.
5358 * @param last1 End of first range.
5359 * @param first2 Start of second range.
5360 * @param last2 End of second range.
5361 * @param comp The comparison functor.
5362 * @return End of the output range.
5363 * @ingroup setoperations
5365 * This operation iterates over both ranges, copying elements present in
5366 * one range but not the other in order to the output range. Iterators
5367 * increment for each range. When the current element of one range is less
5368 * than the other according to @a comp, that element is copied and the
5369 * iterator advances. If an element is contained in both ranges according
5370 * to @a comp, no elements are copied and both ranges advance. The output
5371 * range may not overlap either input range.
5373 template<typename _InputIterator1
, typename _InputIterator2
,
5374 typename _OutputIterator
, typename _Compare
>
5376 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5377 _InputIterator2 __first2
, _InputIterator2 __last2
,
5378 _OutputIterator __result
,
5381 typedef typename iterator_traits
<_InputIterator1
>::value_type
5383 typedef typename iterator_traits
<_InputIterator2
>::value_type
5386 // concept requirements
5387 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5388 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5389 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5391 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5393 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5394 _ValueType1
, _ValueType2
>)
5395 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5396 _ValueType2
, _ValueType1
>)
5397 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5398 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5400 while (__first1
!= __last1
&& __first2
!= __last2
)
5401 if (__comp(*__first1
, *__first2
))
5403 *__result
= *__first1
;
5407 else if (__comp(*__first2
, *__first1
))
5409 *__result
= *__first2
;
5418 return std::copy(__first2
, __last2
,
5419 std::copy(__first1
, __last1
, __result
));
5424 * @brief Return the minimum element in a range.
5425 * @param first Start of range.
5426 * @param last End of range.
5427 * @return Iterator referencing the first instance of the smallest value.
5429 template<typename _ForwardIterator
>
5431 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5433 // concept requirements
5434 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5435 __glibcxx_function_requires(_LessThanComparableConcept
<
5436 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5437 __glibcxx_requires_valid_range(__first
, __last
);
5439 if (__first
== __last
)
5441 _ForwardIterator __result
= __first
;
5442 while (++__first
!= __last
)
5443 if (*__first
< *__result
)
5449 * @brief Return the minimum element in a range using comparison functor.
5450 * @param first Start of range.
5451 * @param last End of range.
5452 * @param comp Comparison functor.
5453 * @return Iterator referencing the first instance of the smallest value
5454 * according to comp.
5456 template<typename _ForwardIterator
, typename _Compare
>
5458 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5461 // concept requirements
5462 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5463 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5464 typename iterator_traits
<_ForwardIterator
>::value_type
,
5465 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5466 __glibcxx_requires_valid_range(__first
, __last
);
5468 if (__first
== __last
)
5470 _ForwardIterator __result
= __first
;
5471 while (++__first
!= __last
)
5472 if (__comp(*__first
, *__result
))
5478 * @brief Return the maximum element in a range.
5479 * @param first Start of range.
5480 * @param last End of range.
5481 * @return Iterator referencing the first instance of the largest value.
5483 template<typename _ForwardIterator
>
5485 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5487 // concept requirements
5488 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5489 __glibcxx_function_requires(_LessThanComparableConcept
<
5490 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5491 __glibcxx_requires_valid_range(__first
, __last
);
5493 if (__first
== __last
)
5495 _ForwardIterator __result
= __first
;
5496 while (++__first
!= __last
)
5497 if (*__result
< *__first
)
5503 * @brief Return the maximum element in a range using comparison functor.
5504 * @param first Start of range.
5505 * @param last End of range.
5506 * @param comp Comparison functor.
5507 * @return Iterator referencing the first instance of the largest value
5508 * according to comp.
5510 template<typename _ForwardIterator
, typename _Compare
>
5512 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5515 // concept requirements
5516 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5517 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5518 typename iterator_traits
<_ForwardIterator
>::value_type
,
5519 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5520 __glibcxx_requires_valid_range(__first
, __last
);
5522 if (__first
== __last
) return __first
;
5523 _ForwardIterator __result
= __first
;
5524 while (++__first
!= __last
)
5525 if (__comp(*__result
, *__first
))
5530 _GLIBCXX_END_NESTED_NAMESPACE
5532 #endif /* _STL_ALGO_H */