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/algorithmfwd.h>
67 #include <bits/stl_heap.h>
68 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
69 #include <debug/debug.h>
71 // See concept_check.h for the __glibcxx_*_requires macros.
73 _GLIBCXX_BEGIN_NAMESPACE(std
)
76 * @brief Find the median of three values.
80 * @return One of @p a, @p b or @p c.
82 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
83 * then the value returned will be @c m.
84 * This is an SGI extension.
85 * @ingroup SGIextensions
87 template<typename _Tp
>
89 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
91 // concept requirements
92 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
109 * @brief Find the median of three values using a predicate for comparison.
113 * @param comp A binary predicate.
114 * @return One of @p a, @p b or @p c.
116 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
117 * and @p comp(m,n) are both true then the value returned will be @c m.
118 * This is an SGI extension.
119 * @ingroup SGIextensions
121 template<typename _Tp
, typename _Compare
>
123 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
125 // concept requirements
126 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
128 if (__comp(__a
, __b
))
129 if (__comp(__b
, __c
))
131 else if (__comp(__a
, __c
))
135 else if (__comp(__a
, __c
))
137 else if (__comp(__b
, __c
))
147 * This is an overload used by find() for the Input Iterator case.
150 template<typename _InputIterator
, typename _Tp
>
151 inline _InputIterator
152 __find(_InputIterator __first
, _InputIterator __last
,
153 const _Tp
& __val
, input_iterator_tag
)
155 while (__first
!= __last
&& !(*__first
== __val
))
162 * This is an overload used by find_if() for the Input Iterator case.
165 template<typename _InputIterator
, typename _Predicate
>
166 inline _InputIterator
167 __find_if(_InputIterator __first
, _InputIterator __last
,
168 _Predicate __pred
, input_iterator_tag
)
170 while (__first
!= __last
&& !bool(__pred(*__first
)))
177 * This is an overload used by find() for the RAI case.
180 template<typename _RandomAccessIterator
, typename _Tp
>
181 _RandomAccessIterator
182 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
183 const _Tp
& __val
, random_access_iterator_tag
)
185 typename iterator_traits
<_RandomAccessIterator
>::difference_type
186 __trip_count
= (__last
- __first
) >> 2;
188 for (; __trip_count
> 0; --__trip_count
)
190 if (*__first
== __val
)
194 if (*__first
== __val
)
198 if (*__first
== __val
)
202 if (*__first
== __val
)
207 switch (__last
- __first
)
210 if (*__first
== __val
)
214 if (*__first
== __val
)
218 if (*__first
== __val
)
229 * This is an overload used by find_if() for the RAI case.
232 template<typename _RandomAccessIterator
, typename _Predicate
>
233 _RandomAccessIterator
234 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
235 _Predicate __pred
, random_access_iterator_tag
)
237 typename iterator_traits
<_RandomAccessIterator
>::difference_type
238 __trip_count
= (__last
- __first
) >> 2;
240 for (; __trip_count
> 0; --__trip_count
)
242 if (__pred(*__first
))
246 if (__pred(*__first
))
250 if (__pred(*__first
))
254 if (__pred(*__first
))
259 switch (__last
- __first
)
262 if (__pred(*__first
))
266 if (__pred(*__first
))
270 if (__pred(*__first
))
281 // set_symmetric_difference
294 * This is an uglified
295 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
296 * overloaded for forward iterators.
299 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
301 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
302 _Integer __count
, const _Tp
& __val
,
303 std::forward_iterator_tag
)
305 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
306 while (__first
!= __last
)
308 typename iterator_traits
<_ForwardIterator
>::difference_type
310 _ForwardIterator __i
= __first
;
312 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
321 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
328 * This is an uglified
329 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
330 * overloaded for random access iterators.
333 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
335 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
336 _Integer __count
, const _Tp
& __val
,
337 std::random_access_iterator_tag
)
340 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
343 _DistanceType __tailSize
= __last
- __first
;
344 const _DistanceType __pattSize
= __count
;
346 if (__tailSize
< __pattSize
)
349 const _DistanceType __skipOffset
= __pattSize
- 1;
350 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
351 __tailSize
-= __pattSize
;
353 while (1) // the main loop...
355 // __lookAhead here is always pointing to the last element of next
357 while (!(*__lookAhead
== __val
)) // the skip loop...
359 if (__tailSize
< __pattSize
)
360 return __last
; // Failure
361 __lookAhead
+= __pattSize
;
362 __tailSize
-= __pattSize
;
364 _DistanceType __remainder
= __skipOffset
;
365 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
366 *__backTrack
== __val
; --__backTrack
)
368 if (--__remainder
== 0)
369 return (__lookAhead
- __skipOffset
); // Success
371 if (__remainder
> __tailSize
)
372 return __last
; // Failure
373 __lookAhead
+= __remainder
;
374 __tailSize
-= __remainder
;
382 * This is an uglified
383 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
385 * overloaded for forward iterators.
388 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
389 typename _BinaryPredicate
>
391 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
392 _Integer __count
, const _Tp
& __val
,
393 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
395 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
398 while (__first
!= __last
)
400 typename iterator_traits
<_ForwardIterator
>::difference_type
402 _ForwardIterator __i
= __first
;
404 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
414 while (__first
!= __last
415 && !bool(__binary_pred(*__first
, __val
)))
423 * This is an uglified
424 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
426 * overloaded for random access iterators.
429 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
430 typename _BinaryPredicate
>
432 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
433 _Integer __count
, const _Tp
& __val
,
434 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
437 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
440 _DistanceType __tailSize
= __last
- __first
;
441 const _DistanceType __pattSize
= __count
;
443 if (__tailSize
< __pattSize
)
446 const _DistanceType __skipOffset
= __pattSize
- 1;
447 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
448 __tailSize
-= __pattSize
;
450 while (1) // the main loop...
452 // __lookAhead here is always pointing to the last element of next
454 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
456 if (__tailSize
< __pattSize
)
457 return __last
; // Failure
458 __lookAhead
+= __pattSize
;
459 __tailSize
-= __pattSize
;
461 _DistanceType __remainder
= __skipOffset
;
462 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
463 __binary_pred(*__backTrack
, __val
); --__backTrack
)
465 if (--__remainder
== 0)
466 return (__lookAhead
- __skipOffset
); // Success
468 if (__remainder
> __tailSize
)
469 return __last
; // Failure
470 __lookAhead
+= __remainder
;
471 __tailSize
-= __remainder
;
475 // find_end for forward iterators.
476 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
478 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
479 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
480 forward_iterator_tag
, forward_iterator_tag
)
482 if (__first2
== __last2
)
486 _ForwardIterator1 __result
= __last1
;
489 _ForwardIterator1 __new_result
490 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
491 if (__new_result
== __last1
)
495 __result
= __new_result
;
496 __first1
= __new_result
;
503 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
504 typename _BinaryPredicate
>
506 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
507 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
508 forward_iterator_tag
, forward_iterator_tag
,
509 _BinaryPredicate __comp
)
511 if (__first2
== __last2
)
515 _ForwardIterator1 __result
= __last1
;
518 _ForwardIterator1 __new_result
519 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
521 if (__new_result
== __last1
)
525 __result
= __new_result
;
526 __first1
= __new_result
;
533 // find_end for bidirectional iterators (much faster).
534 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
535 _BidirectionalIterator1
536 __find_end(_BidirectionalIterator1 __first1
,
537 _BidirectionalIterator1 __last1
,
538 _BidirectionalIterator2 __first2
,
539 _BidirectionalIterator2 __last2
,
540 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
542 // concept requirements
543 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
544 _BidirectionalIterator1
>)
545 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
546 _BidirectionalIterator2
>)
548 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
549 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
551 _RevIterator1
__rlast1(__first1
);
552 _RevIterator2
__rlast2(__first2
);
553 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
555 _RevIterator2(__last2
),
558 if (__rresult
== __rlast1
)
562 _BidirectionalIterator1 __result
= __rresult
.base();
563 std::advance(__result
, -std::distance(__first2
, __last2
));
568 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
569 typename _BinaryPredicate
>
570 _BidirectionalIterator1
571 __find_end(_BidirectionalIterator1 __first1
,
572 _BidirectionalIterator1 __last1
,
573 _BidirectionalIterator2 __first2
,
574 _BidirectionalIterator2 __last2
,
575 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
576 _BinaryPredicate __comp
)
578 // concept requirements
579 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
580 _BidirectionalIterator1
>)
581 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
582 _BidirectionalIterator2
>)
584 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
585 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
587 _RevIterator1
__rlast1(__first1
);
588 _RevIterator2
__rlast2(__first2
);
589 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
590 _RevIterator2(__last2
), __rlast2
,
593 if (__rresult
== __rlast1
)
597 _BidirectionalIterator1 __result
= __rresult
.base();
598 std::advance(__result
, -std::distance(__first2
, __last2
));
604 * @brief Find last matching subsequence in a sequence.
605 * @param first1 Start of range to search.
606 * @param last1 End of range to search.
607 * @param first2 Start of sequence to match.
608 * @param last2 End of sequence to match.
609 * @return The last iterator @c i in the range
610 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
611 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
612 * such iterator exists.
614 * Searches the range @p [first1,last1) for a sub-sequence that compares
615 * equal value-by-value with the sequence given by @p [first2,last2) and
616 * returns an iterator to the first element of the sub-sequence, or
617 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
618 * last such subsequence contained in [first,last1).
620 * Because the sub-sequence must lie completely within the range
621 * @p [first1,last1) it must start at a position less than
622 * @p last1-(last2-first2) where @p last2-first2 is the length of the
624 * This means that the returned iterator @c i will be in the range
625 * @p [first1,last1-(last2-first2))
627 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
628 inline _ForwardIterator1
629 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
630 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
632 // concept requirements
633 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
634 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
635 __glibcxx_function_requires(_EqualOpConcept
<
636 typename iterator_traits
<_ForwardIterator1
>::value_type
,
637 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
638 __glibcxx_requires_valid_range(__first1
, __last1
);
639 __glibcxx_requires_valid_range(__first2
, __last2
);
641 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
642 std::__iterator_category(__first1
),
643 std::__iterator_category(__first2
));
647 * @brief Find last matching subsequence in a sequence using a predicate.
648 * @param first1 Start of range to search.
649 * @param last1 End of range to search.
650 * @param first2 Start of sequence to match.
651 * @param last2 End of sequence to match.
652 * @param comp The predicate to use.
653 * @return The last iterator @c i in the range
654 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
655 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
656 * @p last1 if no such iterator exists.
658 * Searches the range @p [first1,last1) for a sub-sequence that compares
659 * equal value-by-value with the sequence given by @p [first2,last2) using
660 * comp as a predicate and returns an iterator to the first element of the
661 * sub-sequence, or @p last1 if the sub-sequence is not found. The
662 * sub-sequence will be the last such subsequence contained in
665 * Because the sub-sequence must lie completely within the range
666 * @p [first1,last1) it must start at a position less than
667 * @p last1-(last2-first2) where @p last2-first2 is the length of the
669 * This means that the returned iterator @c i will be in the range
670 * @p [first1,last1-(last2-first2))
672 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
673 typename _BinaryPredicate
>
674 inline _ForwardIterator1
675 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
676 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
677 _BinaryPredicate __comp
)
679 // concept requirements
680 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
681 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
682 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
683 typename iterator_traits
<_ForwardIterator1
>::value_type
,
684 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
685 __glibcxx_requires_valid_range(__first1
, __last1
);
686 __glibcxx_requires_valid_range(__first2
, __last2
);
688 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
689 std::__iterator_category(__first1
),
690 std::__iterator_category(__first2
),
696 * @brief Copy a sequence, removing elements of a given value.
697 * @param first An input iterator.
698 * @param last An input iterator.
699 * @param result An output iterator.
700 * @param value The value to be removed.
701 * @return An iterator designating the end of the resulting sequence.
703 * Copies each element in the range @p [first,last) not equal to @p value
704 * to the range beginning at @p result.
705 * remove_copy() is stable, so the relative order of elements that are
706 * copied is unchanged.
708 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
710 remove_copy(_InputIterator __first
, _InputIterator __last
,
711 _OutputIterator __result
, const _Tp
& __value
)
713 // concept requirements
714 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
715 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
716 typename iterator_traits
<_InputIterator
>::value_type
>)
717 __glibcxx_function_requires(_EqualOpConcept
<
718 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
719 __glibcxx_requires_valid_range(__first
, __last
);
721 for (; __first
!= __last
; ++__first
)
722 if (!(*__first
== __value
))
724 *__result
= *__first
;
731 * @brief Copy a sequence, removing elements for which a predicate is true.
732 * @param first An input iterator.
733 * @param last An input iterator.
734 * @param result An output iterator.
735 * @param pred A predicate.
736 * @return An iterator designating the end of the resulting sequence.
738 * Copies each element in the range @p [first,last) for which
739 * @p pred returns true to the range beginning at @p result.
741 * remove_copy_if() is stable, so the relative order of elements that are
742 * copied is unchanged.
744 template<typename _InputIterator
, typename _OutputIterator
,
747 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
748 _OutputIterator __result
, _Predicate __pred
)
750 // concept requirements
751 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
752 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
753 typename iterator_traits
<_InputIterator
>::value_type
>)
754 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
755 typename iterator_traits
<_InputIterator
>::value_type
>)
756 __glibcxx_requires_valid_range(__first
, __last
);
758 for (; __first
!= __last
; ++__first
)
759 if (!bool(__pred(*__first
)))
761 *__result
= *__first
;
768 * @brief Remove elements from a sequence.
769 * @param first An input iterator.
770 * @param last An input iterator.
771 * @param value The value to be removed.
772 * @return An iterator designating the end of the resulting sequence.
774 * All elements equal to @p value are removed from the range
777 * remove() is stable, so the relative order of elements that are
778 * not removed is unchanged.
780 * Elements between the end of the resulting sequence and @p last
781 * are still present, but their value is unspecified.
783 template<typename _ForwardIterator
, typename _Tp
>
785 remove(_ForwardIterator __first
, _ForwardIterator __last
,
788 // concept requirements
789 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
791 __glibcxx_function_requires(_EqualOpConcept
<
792 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
793 __glibcxx_requires_valid_range(__first
, __last
);
795 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
796 if(__first
== __last
)
798 _ForwardIterator __result
= __first
;
800 for(; __first
!= __last
; ++__first
)
801 if(!(*__first
== __value
))
803 *__result
= _GLIBCXX_MOVE(*__first
);
810 * @brief Remove elements from a sequence using a predicate.
811 * @param first A forward iterator.
812 * @param last A forward iterator.
813 * @param pred A predicate.
814 * @return An iterator designating the end of the resulting sequence.
816 * All elements for which @p pred returns true are removed from the range
819 * remove_if() is stable, so the relative order of elements that are
820 * not removed is unchanged.
822 * Elements between the end of the resulting sequence and @p last
823 * are still present, but their value is unspecified.
825 template<typename _ForwardIterator
, typename _Predicate
>
827 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
830 // concept requirements
831 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
833 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
834 typename iterator_traits
<_ForwardIterator
>::value_type
>)
835 __glibcxx_requires_valid_range(__first
, __last
);
837 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
838 if(__first
== __last
)
840 _ForwardIterator __result
= __first
;
842 for(; __first
!= __last
; ++__first
)
843 if(!__pred(*__first
))
845 *__result
= _GLIBCXX_MOVE(*__first
);
852 * @brief Remove consecutive duplicate values from a sequence.
853 * @param first A forward iterator.
854 * @param last A forward iterator.
855 * @return An iterator designating the end of the resulting sequence.
857 * Removes all but the first element from each group of consecutive
858 * values that compare equal.
859 * unique() is stable, so the relative order of elements that are
860 * not removed is unchanged.
861 * Elements between the end of the resulting sequence and @p last
862 * are still present, but their value is unspecified.
864 template<typename _ForwardIterator
>
866 unique(_ForwardIterator __first
, _ForwardIterator __last
)
868 // concept requirements
869 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
871 __glibcxx_function_requires(_EqualityComparableConcept
<
872 typename iterator_traits
<_ForwardIterator
>::value_type
>)
873 __glibcxx_requires_valid_range(__first
, __last
);
875 // Skip the beginning, if already unique.
876 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
877 if (__first
== __last
)
880 // Do the real copy work.
881 _ForwardIterator __dest
= __first
;
883 while (++__first
!= __last
)
884 if (!(*__dest
== *__first
))
885 *++__dest
= _GLIBCXX_MOVE(*__first
);
890 * @brief Remove consecutive values from a sequence using a predicate.
891 * @param first A forward iterator.
892 * @param last A forward iterator.
893 * @param binary_pred A binary predicate.
894 * @return An iterator designating the end of the resulting sequence.
896 * Removes all but the first element from each group of consecutive
897 * values for which @p binary_pred returns true.
898 * unique() is stable, so the relative order of elements that are
899 * not removed is unchanged.
900 * Elements between the end of the resulting sequence and @p last
901 * are still present, but their value is unspecified.
903 template<typename _ForwardIterator
, typename _BinaryPredicate
>
905 unique(_ForwardIterator __first
, _ForwardIterator __last
,
906 _BinaryPredicate __binary_pred
)
908 // concept requirements
909 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
911 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
912 typename iterator_traits
<_ForwardIterator
>::value_type
,
913 typename iterator_traits
<_ForwardIterator
>::value_type
>)
914 __glibcxx_requires_valid_range(__first
, __last
);
916 // Skip the beginning, if already unique.
917 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
918 if (__first
== __last
)
921 // Do the real copy work.
922 _ForwardIterator __dest
= __first
;
924 while (++__first
!= __last
)
925 if (!bool(__binary_pred(*__dest
, *__first
)))
926 *++__dest
= _GLIBCXX_MOVE(*__first
);
932 * This is an uglified unique_copy(_InputIterator, _InputIterator,
934 * overloaded for forward iterators and output iterator as result.
937 template<typename _ForwardIterator
, typename _OutputIterator
>
939 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
940 _OutputIterator __result
,
941 forward_iterator_tag
, output_iterator_tag
)
943 // concept requirements -- taken care of in dispatching function
944 _ForwardIterator __next
= __first
;
945 *__result
= *__first
;
946 while (++__next
!= __last
)
947 if (!(*__first
== *__next
))
950 *++__result
= *__first
;
957 * This is an uglified unique_copy(_InputIterator, _InputIterator,
959 * overloaded for input iterators and output iterator as result.
962 template<typename _InputIterator
, typename _OutputIterator
>
964 __unique_copy(_InputIterator __first
, _InputIterator __last
,
965 _OutputIterator __result
,
966 input_iterator_tag
, output_iterator_tag
)
968 // concept requirements -- taken care of in dispatching function
969 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
971 while (++__first
!= __last
)
972 if (!(__value
== *__first
))
975 *++__result
= __value
;
982 * This is an uglified unique_copy(_InputIterator, _InputIterator,
984 * overloaded for input iterators and forward iterator as result.
987 template<typename _InputIterator
, typename _ForwardIterator
>
989 __unique_copy(_InputIterator __first
, _InputIterator __last
,
990 _ForwardIterator __result
,
991 input_iterator_tag
, forward_iterator_tag
)
993 // concept requirements -- taken care of in dispatching function
994 *__result
= *__first
;
995 while (++__first
!= __last
)
996 if (!(*__result
== *__first
))
997 *++__result
= *__first
;
1003 * This is an uglified
1004 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1006 * overloaded for forward iterators and output iterator as result.
1009 template<typename _ForwardIterator
, typename _OutputIterator
,
1010 typename _BinaryPredicate
>
1012 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1013 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1014 forward_iterator_tag
, output_iterator_tag
)
1016 // concept requirements -- iterators already checked
1017 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1018 typename iterator_traits
<_ForwardIterator
>::value_type
,
1019 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1021 _ForwardIterator __next
= __first
;
1022 *__result
= *__first
;
1023 while (++__next
!= __last
)
1024 if (!bool(__binary_pred(*__first
, *__next
)))
1027 *++__result
= *__first
;
1034 * This is an uglified
1035 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1037 * overloaded for input iterators and output iterator as result.
1040 template<typename _InputIterator
, typename _OutputIterator
,
1041 typename _BinaryPredicate
>
1043 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1044 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1045 input_iterator_tag
, output_iterator_tag
)
1047 // concept requirements -- iterators already checked
1048 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1049 typename iterator_traits
<_InputIterator
>::value_type
,
1050 typename iterator_traits
<_InputIterator
>::value_type
>)
1052 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1053 *__result
= __value
;
1054 while (++__first
!= __last
)
1055 if (!bool(__binary_pred(__value
, *__first
)))
1058 *++__result
= __value
;
1065 * This is an uglified
1066 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1068 * overloaded for input iterators and forward iterator as result.
1071 template<typename _InputIterator
, typename _ForwardIterator
,
1072 typename _BinaryPredicate
>
1074 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1075 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1076 input_iterator_tag
, forward_iterator_tag
)
1078 // concept requirements -- iterators already checked
1079 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1080 typename iterator_traits
<_ForwardIterator
>::value_type
,
1081 typename iterator_traits
<_InputIterator
>::value_type
>)
1083 *__result
= *__first
;
1084 while (++__first
!= __last
)
1085 if (!bool(__binary_pred(*__result
, *__first
)))
1086 *++__result
= *__first
;
1092 * This is an uglified reverse(_BidirectionalIterator,
1093 * _BidirectionalIterator)
1094 * overloaded for bidirectional iterators.
1097 template<typename _BidirectionalIterator
>
1099 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1100 bidirectional_iterator_tag
)
1103 if (__first
== __last
|| __first
== --__last
)
1107 std::iter_swap(__first
, __last
);
1114 * This is an uglified reverse(_BidirectionalIterator,
1115 * _BidirectionalIterator)
1116 * overloaded for random access iterators.
1119 template<typename _RandomAccessIterator
>
1121 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1122 random_access_iterator_tag
)
1124 if (__first
== __last
)
1127 while (__first
< __last
)
1129 std::iter_swap(__first
, __last
);
1136 * @brief Reverse a sequence.
1137 * @param first A bidirectional iterator.
1138 * @param last A bidirectional iterator.
1139 * @return reverse() returns no value.
1141 * Reverses the order of the elements in the range @p [first,last),
1142 * so that the first element becomes the last etc.
1143 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1144 * swaps @p *(first+i) and @p *(last-(i+1))
1146 template<typename _BidirectionalIterator
>
1148 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1150 // concept requirements
1151 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1152 _BidirectionalIterator
>)
1153 __glibcxx_requires_valid_range(__first
, __last
);
1154 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1158 * @brief Copy a sequence, reversing its elements.
1159 * @param first A bidirectional iterator.
1160 * @param last A bidirectional iterator.
1161 * @param result An output iterator.
1162 * @return An iterator designating the end of the resulting sequence.
1164 * Copies the elements in the range @p [first,last) to the range
1165 * @p [result,result+(last-first)) such that the order of the
1166 * elements is reversed.
1167 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1168 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1169 * The ranges @p [first,last) and @p [result,result+(last-first))
1172 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1174 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1175 _OutputIterator __result
)
1177 // concept requirements
1178 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1179 _BidirectionalIterator
>)
1180 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1181 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1182 __glibcxx_requires_valid_range(__first
, __last
);
1184 while (__first
!= __last
)
1187 *__result
= *__last
;
1195 * This is a helper function for the rotate algorithm specialized on RAIs.
1196 * It returns the greatest common divisor of two integer values.
1199 template<typename _EuclideanRingElement
>
1200 _EuclideanRingElement
1201 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1205 _EuclideanRingElement __t
= __m
% __n
;
1214 * This is a helper function for the rotate algorithm.
1217 template<typename _ForwardIterator
>
1219 __rotate(_ForwardIterator __first
,
1220 _ForwardIterator __middle
,
1221 _ForwardIterator __last
,
1222 forward_iterator_tag
)
1224 if (__first
== __middle
|| __last
== __middle
)
1227 _ForwardIterator __first2
= __middle
;
1230 std::iter_swap(__first
, __first2
);
1233 if (__first
== __middle
)
1234 __middle
= __first2
;
1236 while (__first2
!= __last
);
1238 __first2
= __middle
;
1240 while (__first2
!= __last
)
1242 std::iter_swap(__first
, __first2
);
1245 if (__first
== __middle
)
1246 __middle
= __first2
;
1247 else if (__first2
== __last
)
1248 __first2
= __middle
;
1254 * This is a helper function for the rotate algorithm.
1257 template<typename _BidirectionalIterator
>
1259 __rotate(_BidirectionalIterator __first
,
1260 _BidirectionalIterator __middle
,
1261 _BidirectionalIterator __last
,
1262 bidirectional_iterator_tag
)
1264 // concept requirements
1265 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1266 _BidirectionalIterator
>)
1268 if (__first
== __middle
|| __last
== __middle
)
1271 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1272 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1274 while (__first
!= __middle
&& __middle
!= __last
)
1276 std::iter_swap(__first
, --__last
);
1280 if (__first
== __middle
)
1281 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1283 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1288 * This is a helper function for the rotate algorithm.
1291 template<typename _RandomAccessIterator
>
1293 __rotate(_RandomAccessIterator __first
,
1294 _RandomAccessIterator __middle
,
1295 _RandomAccessIterator __last
,
1296 random_access_iterator_tag
)
1298 // concept requirements
1299 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1300 _RandomAccessIterator
>)
1302 if (__first
== __middle
|| __last
== __middle
)
1305 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1307 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1310 const _Distance __n
= __last
- __first
;
1311 const _Distance __k
= __middle
- __first
;
1312 const _Distance __l
= __n
- __k
;
1316 std::swap_ranges(__first
, __middle
, __middle
);
1320 const _Distance __d
= std::__gcd(__n
, __k
);
1322 for (_Distance __i
= 0; __i
< __d
; __i
++)
1324 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1325 _RandomAccessIterator __p
= __first
;
1329 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1331 if (__p
> __first
+ __l
)
1333 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1337 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1343 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1345 if (__p
< __last
- __k
)
1347 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1350 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1355 *__p
= _GLIBCXX_MOVE(__tmp
);
1361 * @brief Rotate the elements of a sequence.
1362 * @param first A forward iterator.
1363 * @param middle A forward iterator.
1364 * @param last A forward iterator.
1367 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1368 * positions so that the element at @p middle is moved to @p first, the
1369 * element at @p middle+1 is moved to @first+1 and so on for each element
1370 * in the range @p [first,last).
1372 * This effectively swaps the ranges @p [first,middle) and
1375 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1376 * each @p n in the range @p [0,last-first).
1378 template<typename _ForwardIterator
>
1380 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1381 _ForwardIterator __last
)
1383 // concept requirements
1384 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1386 __glibcxx_requires_valid_range(__first
, __middle
);
1387 __glibcxx_requires_valid_range(__middle
, __last
);
1389 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1391 std::__rotate(__first
, __middle
, __last
, _IterType());
1395 * @brief Copy a sequence, rotating its elements.
1396 * @param first A forward iterator.
1397 * @param middle A forward iterator.
1398 * @param last A forward iterator.
1399 * @param result An output iterator.
1400 * @return An iterator designating the end of the resulting sequence.
1402 * Copies the elements of the range @p [first,last) to the range
1403 * beginning at @result, rotating the copied elements by @p (middle-first)
1404 * positions so that the element at @p middle is moved to @p result, the
1405 * element at @p middle+1 is moved to @result+1 and so on for each element
1406 * in the range @p [first,last).
1408 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1409 * each @p n in the range @p [0,last-first).
1411 template<typename _ForwardIterator
, typename _OutputIterator
>
1413 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1414 _ForwardIterator __last
, _OutputIterator __result
)
1416 // concept requirements
1417 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1418 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1419 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1420 __glibcxx_requires_valid_range(__first
, __middle
);
1421 __glibcxx_requires_valid_range(__middle
, __last
);
1423 return std::copy(__first
, __middle
,
1424 std::copy(__middle
, __last
, __result
));
1429 * This is a helper function...
1432 template<typename _ForwardIterator
, typename _Predicate
>
1434 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1435 _Predicate __pred
, forward_iterator_tag
)
1437 if (__first
== __last
)
1440 while (__pred(*__first
))
1441 if (++__first
== __last
)
1444 _ForwardIterator __next
= __first
;
1446 while (++__next
!= __last
)
1447 if (__pred(*__next
))
1449 std::iter_swap(__first
, __next
);
1458 * This is a helper function...
1461 template<typename _BidirectionalIterator
, typename _Predicate
>
1462 _BidirectionalIterator
1463 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1464 _Predicate __pred
, bidirectional_iterator_tag
)
1469 if (__first
== __last
)
1471 else if (__pred(*__first
))
1477 if (__first
== __last
)
1479 else if (!bool(__pred(*__last
)))
1483 std::iter_swap(__first
, __last
);
1492 * This is a helper function...
1495 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1497 __inplace_stable_partition(_ForwardIterator __first
,
1498 _ForwardIterator __last
,
1499 _Predicate __pred
, _Distance __len
)
1502 return __pred(*__first
) ? __last
: __first
;
1503 _ForwardIterator __middle
= __first
;
1504 std::advance(__middle
, __len
/ 2);
1505 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1509 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1513 std::rotate(__begin
, __middle
, __end
);
1514 std::advance(__begin
, std::distance(__middle
, __end
));
1520 * This is a helper function...
1523 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1526 __stable_partition_adaptive(_ForwardIterator __first
,
1527 _ForwardIterator __last
,
1528 _Predicate __pred
, _Distance __len
,
1530 _Distance __buffer_size
)
1532 if (__len
<= __buffer_size
)
1534 _ForwardIterator __result1
= __first
;
1535 _Pointer __result2
= __buffer
;
1536 for (; __first
!= __last
; ++__first
)
1537 if (__pred(*__first
))
1539 *__result1
= *__first
;
1544 *__result2
= *__first
;
1547 std::copy(__buffer
, __result2
, __result1
);
1552 _ForwardIterator __middle
= __first
;
1553 std::advance(__middle
, __len
/ 2);
1554 _ForwardIterator __begin
=
1555 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1556 __len
/ 2, __buffer
,
1558 _ForwardIterator __end
=
1559 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1561 __buffer
, __buffer_size
);
1562 std::rotate(__begin
, __middle
, __end
);
1563 std::advance(__begin
, std::distance(__middle
, __end
));
1569 * @brief Move elements for which a predicate is true to the beginning
1570 * of a sequence, preserving relative ordering.
1571 * @param first A forward iterator.
1572 * @param last A forward iterator.
1573 * @param pred A predicate functor.
1574 * @return An iterator @p middle such that @p pred(i) is true for each
1575 * iterator @p i in the range @p [first,middle) and false for each @p i
1576 * in the range @p [middle,last).
1578 * Performs the same function as @p partition() with the additional
1579 * guarantee that the relative ordering of elements in each group is
1580 * preserved, so any two elements @p x and @p y in the range
1581 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1582 * relative ordering after calling @p stable_partition().
1584 template<typename _ForwardIterator
, typename _Predicate
>
1586 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1589 // concept requirements
1590 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1592 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1593 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1594 __glibcxx_requires_valid_range(__first
, __last
);
1596 if (__first
== __last
)
1600 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1602 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1605 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1607 if (__buf
.size() > 0)
1609 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1610 _DistanceType(__buf
.requested_size()),
1612 _DistanceType(__buf
.size()));
1615 std::__inplace_stable_partition(__first
, __last
, __pred
,
1616 _DistanceType(__buf
.requested_size()));
1622 * This is a helper function for the sort routines.
1625 template<typename _RandomAccessIterator
>
1627 __heap_select(_RandomAccessIterator __first
,
1628 _RandomAccessIterator __middle
,
1629 _RandomAccessIterator __last
)
1631 std::make_heap(__first
, __middle
);
1632 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1633 if (*__i
< *__first
)
1634 std::__pop_heap(__first
, __middle
, __i
);
1639 * This is a helper function for the sort routines.
1642 template<typename _RandomAccessIterator
, typename _Compare
>
1644 __heap_select(_RandomAccessIterator __first
,
1645 _RandomAccessIterator __middle
,
1646 _RandomAccessIterator __last
, _Compare __comp
)
1648 std::make_heap(__first
, __middle
, __comp
);
1649 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1650 if (__comp(*__i
, *__first
))
1651 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1657 * @brief Copy the smallest elements of a sequence.
1658 * @param first An iterator.
1659 * @param last Another iterator.
1660 * @param result_first A random-access iterator.
1661 * @param result_last Another random-access iterator.
1662 * @return An iterator indicating the end of the resulting sequence.
1664 * Copies and sorts the smallest N values from the range @p [first,last)
1665 * to the range beginning at @p result_first, where the number of
1666 * elements to be copied, @p N, is the smaller of @p (last-first) and
1667 * @p (result_last-result_first).
1668 * After the sort if @p i and @j are iterators in the range
1669 * @p [result_first,result_first+N) such that @i precedes @j then
1670 * @p *j<*i is false.
1671 * The value returned is @p result_first+N.
1673 template<typename _InputIterator
, typename _RandomAccessIterator
>
1674 _RandomAccessIterator
1675 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1676 _RandomAccessIterator __result_first
,
1677 _RandomAccessIterator __result_last
)
1679 typedef typename iterator_traits
<_InputIterator
>::value_type
1681 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1683 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1686 // concept requirements
1687 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1688 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1690 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1692 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1693 __glibcxx_requires_valid_range(__first
, __last
);
1694 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1696 if (__result_first
== __result_last
)
1697 return __result_last
;
1698 _RandomAccessIterator __result_real_last
= __result_first
;
1699 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1701 *__result_real_last
= *__first
;
1702 ++__result_real_last
;
1705 std::make_heap(__result_first
, __result_real_last
);
1706 while (__first
!= __last
)
1708 if (*__first
< *__result_first
)
1709 std::__adjust_heap(__result_first
, _DistanceType(0),
1710 _DistanceType(__result_real_last
1712 _InputValueType(*__first
));
1715 std::sort_heap(__result_first
, __result_real_last
);
1716 return __result_real_last
;
1720 * @brief Copy the smallest elements of a sequence using a predicate for
1722 * @param first An input iterator.
1723 * @param last Another input iterator.
1724 * @param result_first A random-access iterator.
1725 * @param result_last Another random-access iterator.
1726 * @param comp A comparison functor.
1727 * @return An iterator indicating the end of the resulting sequence.
1729 * Copies and sorts the smallest N values from the range @p [first,last)
1730 * to the range beginning at @p result_first, where the number of
1731 * elements to be copied, @p N, is the smaller of @p (last-first) and
1732 * @p (result_last-result_first).
1733 * After the sort if @p i and @j are iterators in the range
1734 * @p [result_first,result_first+N) such that @i precedes @j then
1735 * @p comp(*j,*i) is false.
1736 * The value returned is @p result_first+N.
1738 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1739 _RandomAccessIterator
1740 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1741 _RandomAccessIterator __result_first
,
1742 _RandomAccessIterator __result_last
,
1745 typedef typename iterator_traits
<_InputIterator
>::value_type
1747 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1749 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1752 // concept requirements
1753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1754 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1755 _RandomAccessIterator
>)
1756 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1758 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1759 _InputValueType
, _OutputValueType
>)
1760 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1761 _OutputValueType
, _OutputValueType
>)
1762 __glibcxx_requires_valid_range(__first
, __last
);
1763 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1765 if (__result_first
== __result_last
)
1766 return __result_last
;
1767 _RandomAccessIterator __result_real_last
= __result_first
;
1768 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1770 *__result_real_last
= *__first
;
1771 ++__result_real_last
;
1774 std::make_heap(__result_first
, __result_real_last
, __comp
);
1775 while (__first
!= __last
)
1777 if (__comp(*__first
, *__result_first
))
1778 std::__adjust_heap(__result_first
, _DistanceType(0),
1779 _DistanceType(__result_real_last
1781 _InputValueType(*__first
),
1785 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1786 return __result_real_last
;
1791 * This is a helper function for the sort routine.
1794 template<typename _RandomAccessIterator
, typename _Tp
>
1796 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1798 _RandomAccessIterator __next
= __last
;
1800 while (__val
< *__next
)
1811 * This is a helper function for the sort routine.
1814 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1816 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1819 _RandomAccessIterator __next
= __last
;
1821 while (__comp(__val
, *__next
))
1832 * This is a helper function for the sort routine.
1835 template<typename _RandomAccessIterator
>
1837 __insertion_sort(_RandomAccessIterator __first
,
1838 _RandomAccessIterator __last
)
1840 if (__first
== __last
)
1843 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1845 typename iterator_traits
<_RandomAccessIterator
>::value_type
1847 if (__val
< *__first
)
1849 std::copy_backward(__first
, __i
, __i
+ 1);
1853 std::__unguarded_linear_insert(__i
, __val
);
1859 * This is a helper function for the sort routine.
1862 template<typename _RandomAccessIterator
, typename _Compare
>
1864 __insertion_sort(_RandomAccessIterator __first
,
1865 _RandomAccessIterator __last
, _Compare __comp
)
1867 if (__first
== __last
) return;
1869 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1871 typename iterator_traits
<_RandomAccessIterator
>::value_type
1873 if (__comp(__val
, *__first
))
1875 std::copy_backward(__first
, __i
, __i
+ 1);
1879 std::__unguarded_linear_insert(__i
, __val
, __comp
);
1885 * This is a helper function for the sort routine.
1888 template<typename _RandomAccessIterator
>
1890 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1891 _RandomAccessIterator __last
)
1893 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1896 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1897 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
1902 * This is a helper function for the sort routine.
1905 template<typename _RandomAccessIterator
, typename _Compare
>
1907 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1908 _RandomAccessIterator __last
, _Compare __comp
)
1910 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1913 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1914 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
1920 * This controls some aspect of the sort routines.
1923 enum { _S_threshold
= 16 };
1927 * This is a helper function for the sort routine.
1930 template<typename _RandomAccessIterator
>
1932 __final_insertion_sort(_RandomAccessIterator __first
,
1933 _RandomAccessIterator __last
)
1935 if (__last
- __first
> int(_S_threshold
))
1937 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
1938 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
1941 std::__insertion_sort(__first
, __last
);
1946 * This is a helper function for the sort routine.
1949 template<typename _RandomAccessIterator
, typename _Compare
>
1951 __final_insertion_sort(_RandomAccessIterator __first
,
1952 _RandomAccessIterator __last
, _Compare __comp
)
1954 if (__last
- __first
> int(_S_threshold
))
1956 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1957 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1961 std::__insertion_sort(__first
, __last
, __comp
);
1966 * This is a helper function...
1969 template<typename _RandomAccessIterator
, typename _Tp
>
1970 _RandomAccessIterator
1971 __unguarded_partition(_RandomAccessIterator __first
,
1972 _RandomAccessIterator __last
, _Tp __pivot
)
1976 while (*__first
< __pivot
)
1979 while (__pivot
< *__last
)
1981 if (!(__first
< __last
))
1983 std::iter_swap(__first
, __last
);
1990 * This is a helper function...
1993 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1994 _RandomAccessIterator
1995 __unguarded_partition(_RandomAccessIterator __first
,
1996 _RandomAccessIterator __last
,
1997 _Tp __pivot
, _Compare __comp
)
2001 while (__comp(*__first
, __pivot
))
2004 while (__comp(__pivot
, *__last
))
2006 if (!(__first
< __last
))
2008 std::iter_swap(__first
, __last
);
2015 * This is a helper function for the sort routine.
2018 template<typename _RandomAccessIterator
, typename _Size
>
2020 __introsort_loop(_RandomAccessIterator __first
,
2021 _RandomAccessIterator __last
,
2022 _Size __depth_limit
)
2024 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2027 while (__last
- __first
> int(_S_threshold
))
2029 if (__depth_limit
== 0)
2031 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
);
2035 _RandomAccessIterator __cut
=
2036 std::__unguarded_partition(__first
, __last
,
2037 _ValueType(std::__median(*__first
,
2044 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2051 * This is a helper function for the sort routine.
2054 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2056 __introsort_loop(_RandomAccessIterator __first
,
2057 _RandomAccessIterator __last
,
2058 _Size __depth_limit
, _Compare __comp
)
2060 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2063 while (__last
- __first
> int(_S_threshold
))
2065 if (__depth_limit
== 0)
2067 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2071 _RandomAccessIterator __cut
=
2072 std::__unguarded_partition(__first
, __last
,
2073 _ValueType(std::__median(*__first
,
2081 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2088 * This is a helper function for the sort routines. Precondition: __n > 0.
2091 template<typename _Size
>
2096 for (__k
= 0; __n
!= 0; __n
>>= 1)
2103 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
2107 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
2111 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
2115 template<typename _RandomAccessIterator
, typename _Size
>
2117 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2118 _RandomAccessIterator __last
, _Size __depth_limit
)
2120 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2123 while (__last
- __first
> 3)
2125 if (__depth_limit
== 0)
2127 std::__heap_select(__first
, __nth
+ 1, __last
);
2128 // Place the nth largest element in its final position.
2129 std::iter_swap(__first
, __nth
);
2133 _RandomAccessIterator __cut
=
2134 std::__unguarded_partition(__first
, __last
,
2135 _ValueType(std::__median(*__first
,
2147 std::__insertion_sort(__first
, __last
);
2150 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2152 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2153 _RandomAccessIterator __last
, _Size __depth_limit
,
2156 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2159 while (__last
- __first
> 3)
2161 if (__depth_limit
== 0)
2163 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2164 // Place the nth largest element in its final position.
2165 std::iter_swap(__first
, __nth
);
2169 _RandomAccessIterator __cut
=
2170 std::__unguarded_partition(__first
, __last
,
2171 _ValueType(std::__median(*__first
,
2184 std::__insertion_sort(__first
, __last
, __comp
);
2190 * @brief Finds the first position in which @a val could be inserted
2191 * without changing the ordering.
2192 * @param first An iterator.
2193 * @param last Another iterator.
2194 * @param val The search term.
2195 * @return An iterator pointing to the first element "not less
2196 * than" @a val, or end() if every element is less than
2198 * @ingroup binarysearch
2200 template<typename _ForwardIterator
, typename _Tp
>
2202 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2205 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2207 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2210 // concept requirements
2211 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2212 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2213 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2215 _DistanceType __len
= std::distance(__first
, __last
);
2216 _DistanceType __half
;
2217 _ForwardIterator __middle
;
2221 __half
= __len
>> 1;
2223 std::advance(__middle
, __half
);
2224 if (*__middle
< __val
)
2228 __len
= __len
- __half
- 1;
2237 * @brief Finds the first position in which @a val could be inserted
2238 * without changing the ordering.
2239 * @param first An iterator.
2240 * @param last Another iterator.
2241 * @param val The search term.
2242 * @param comp A functor to use for comparisons.
2243 * @return An iterator pointing to the first element "not less than" @a val,
2244 * or end() if every element is less than @a val.
2245 * @ingroup binarysearch
2247 * The comparison function should have the same effects on ordering as
2248 * the function used for the initial sort.
2250 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2252 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2253 const _Tp
& __val
, _Compare __comp
)
2255 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2257 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2260 // concept requirements
2261 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2262 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2264 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2267 _DistanceType __len
= std::distance(__first
, __last
);
2268 _DistanceType __half
;
2269 _ForwardIterator __middle
;
2273 __half
= __len
>> 1;
2275 std::advance(__middle
, __half
);
2276 if (__comp(*__middle
, __val
))
2280 __len
= __len
- __half
- 1;
2289 * @brief Finds the last position in which @a val could be inserted
2290 * without changing the ordering.
2291 * @param first An iterator.
2292 * @param last Another iterator.
2293 * @param val The search term.
2294 * @return An iterator pointing to the first element greater than @a val,
2295 * or end() if no elements are greater than @a val.
2296 * @ingroup binarysearch
2298 template<typename _ForwardIterator
, typename _Tp
>
2300 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2303 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2305 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2308 // concept requirements
2309 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2310 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2311 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2313 _DistanceType __len
= std::distance(__first
, __last
);
2314 _DistanceType __half
;
2315 _ForwardIterator __middle
;
2319 __half
= __len
>> 1;
2321 std::advance(__middle
, __half
);
2322 if (__val
< *__middle
)
2328 __len
= __len
- __half
- 1;
2335 * @brief Finds the last position in which @a val could be inserted
2336 * without changing the ordering.
2337 * @param first An iterator.
2338 * @param last Another iterator.
2339 * @param val The search term.
2340 * @param comp A functor to use for comparisons.
2341 * @return An iterator pointing to the first element greater than @a val,
2342 * or end() if no elements are greater than @a val.
2343 * @ingroup binarysearch
2345 * The comparison function should have the same effects on ordering as
2346 * the function used for the initial sort.
2348 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2350 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2351 const _Tp
& __val
, _Compare __comp
)
2353 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2355 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2358 // concept requirements
2359 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2360 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2362 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2365 _DistanceType __len
= std::distance(__first
, __last
);
2366 _DistanceType __half
;
2367 _ForwardIterator __middle
;
2371 __half
= __len
>> 1;
2373 std::advance(__middle
, __half
);
2374 if (__comp(__val
, *__middle
))
2380 __len
= __len
- __half
- 1;
2387 * @brief Finds the largest subrange in which @a val could be inserted
2388 * at any place in it without changing the ordering.
2389 * @param first An iterator.
2390 * @param last Another iterator.
2391 * @param val The search term.
2392 * @return An pair of iterators defining the subrange.
2393 * @ingroup binarysearch
2395 * This is equivalent to
2397 * std::make_pair(lower_bound(first, last, val),
2398 * upper_bound(first, last, val))
2400 * but does not actually call those functions.
2402 template<typename _ForwardIterator
, typename _Tp
>
2403 pair
<_ForwardIterator
, _ForwardIterator
>
2404 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2407 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2409 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2412 // concept requirements
2413 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2414 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2415 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2416 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2417 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2419 _DistanceType __len
= std::distance(__first
, __last
);
2420 _DistanceType __half
;
2421 _ForwardIterator __middle
, __left
, __right
;
2425 __half
= __len
>> 1;
2427 std::advance(__middle
, __half
);
2428 if (*__middle
< __val
)
2432 __len
= __len
- __half
- 1;
2434 else if (__val
< *__middle
)
2438 __left
= std::lower_bound(__first
, __middle
, __val
);
2439 std::advance(__first
, __len
);
2440 __right
= std::upper_bound(++__middle
, __first
, __val
);
2441 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2444 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2448 * @brief Finds the largest subrange in which @a val could be inserted
2449 * at any place in it without changing the ordering.
2450 * @param first An iterator.
2451 * @param last Another iterator.
2452 * @param val The search term.
2453 * @param comp A functor to use for comparisons.
2454 * @return An pair of iterators defining the subrange.
2455 * @ingroup binarysearch
2457 * This is equivalent to
2459 * std::make_pair(lower_bound(first, last, val, comp),
2460 * upper_bound(first, last, val, comp))
2462 * but does not actually call those functions.
2464 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2465 pair
<_ForwardIterator
, _ForwardIterator
>
2466 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2470 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2472 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2475 // concept requirements
2476 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2477 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2479 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2481 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2483 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2486 _DistanceType __len
= std::distance(__first
, __last
);
2487 _DistanceType __half
;
2488 _ForwardIterator __middle
, __left
, __right
;
2492 __half
= __len
>> 1;
2494 std::advance(__middle
, __half
);
2495 if (__comp(*__middle
, __val
))
2499 __len
= __len
- __half
- 1;
2501 else if (__comp(__val
, *__middle
))
2505 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2506 std::advance(__first
, __len
);
2507 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2508 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2511 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2515 * @brief Determines whether an element exists in a range.
2516 * @param first An iterator.
2517 * @param last Another iterator.
2518 * @param val The search term.
2519 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2520 * @ingroup binarysearch
2522 * Note that this does not actually return an iterator to @a val. For
2523 * that, use std::find or a container's specialized find member functions.
2525 template<typename _ForwardIterator
, typename _Tp
>
2527 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2530 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2533 // concept requirements
2534 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2535 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2536 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2537 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2539 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2540 return __i
!= __last
&& !(__val
< *__i
);
2544 * @brief Determines whether an element exists in a range.
2545 * @param first An iterator.
2546 * @param last Another iterator.
2547 * @param val The search term.
2548 * @param comp A functor to use for comparisons.
2549 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2550 * @ingroup binarysearch
2552 * Note that this does not actually return an iterator to @a val. For
2553 * that, use std::find or a container's specialized find member functions.
2555 * The comparison function should have the same effects on ordering as
2556 * the function used for the initial sort.
2558 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2560 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2561 const _Tp
& __val
, _Compare __comp
)
2563 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2566 // concept requirements
2567 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2568 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2570 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2572 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2575 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2576 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2583 * This is a helper function for the merge routines.
2586 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2587 typename _BidirectionalIterator3
>
2588 _BidirectionalIterator3
2589 __merge_backward(_BidirectionalIterator1 __first1
,
2590 _BidirectionalIterator1 __last1
,
2591 _BidirectionalIterator2 __first2
,
2592 _BidirectionalIterator2 __last2
,
2593 _BidirectionalIterator3 __result
)
2595 if (__first1
== __last1
)
2596 return std::copy_backward(__first2
, __last2
, __result
);
2597 if (__first2
== __last2
)
2598 return std::copy_backward(__first1
, __last1
, __result
);
2603 if (*__last2
< *__last1
)
2605 *--__result
= *__last1
;
2606 if (__first1
== __last1
)
2607 return std::copy_backward(__first2
, ++__last2
, __result
);
2612 *--__result
= *__last2
;
2613 if (__first2
== __last2
)
2614 return std::copy_backward(__first1
, ++__last1
, __result
);
2622 * This is a helper function for the merge routines.
2625 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2626 typename _BidirectionalIterator3
, typename _Compare
>
2627 _BidirectionalIterator3
2628 __merge_backward(_BidirectionalIterator1 __first1
,
2629 _BidirectionalIterator1 __last1
,
2630 _BidirectionalIterator2 __first2
,
2631 _BidirectionalIterator2 __last2
,
2632 _BidirectionalIterator3 __result
,
2635 if (__first1
== __last1
)
2636 return std::copy_backward(__first2
, __last2
, __result
);
2637 if (__first2
== __last2
)
2638 return std::copy_backward(__first1
, __last1
, __result
);
2643 if (__comp(*__last2
, *__last1
))
2645 *--__result
= *__last1
;
2646 if (__first1
== __last1
)
2647 return std::copy_backward(__first2
, ++__last2
, __result
);
2652 *--__result
= *__last2
;
2653 if (__first2
== __last2
)
2654 return std::copy_backward(__first1
, ++__last1
, __result
);
2662 * This is a helper function for the merge routines.
2665 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2667 _BidirectionalIterator1
2668 __rotate_adaptive(_BidirectionalIterator1 __first
,
2669 _BidirectionalIterator1 __middle
,
2670 _BidirectionalIterator1 __last
,
2671 _Distance __len1
, _Distance __len2
,
2672 _BidirectionalIterator2 __buffer
,
2673 _Distance __buffer_size
)
2675 _BidirectionalIterator2 __buffer_end
;
2676 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2678 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2679 std::copy_backward(__first
, __middle
, __last
);
2680 return std::copy(__buffer
, __buffer_end
, __first
);
2682 else if (__len1
<= __buffer_size
)
2684 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2685 std::copy(__middle
, __last
, __first
);
2686 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2690 std::rotate(__first
, __middle
, __last
);
2691 std::advance(__first
, std::distance(__middle
, __last
));
2698 * This is a helper function for the merge routines.
2701 template<typename _BidirectionalIterator
, typename _Distance
,
2704 __merge_adaptive(_BidirectionalIterator __first
,
2705 _BidirectionalIterator __middle
,
2706 _BidirectionalIterator __last
,
2707 _Distance __len1
, _Distance __len2
,
2708 _Pointer __buffer
, _Distance __buffer_size
)
2710 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2712 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2713 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2716 else if (__len2
<= __buffer_size
)
2718 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2719 std::__merge_backward(__first
, __middle
, __buffer
,
2720 __buffer_end
, __last
);
2724 _BidirectionalIterator __first_cut
= __first
;
2725 _BidirectionalIterator __second_cut
= __middle
;
2726 _Distance __len11
= 0;
2727 _Distance __len22
= 0;
2728 if (__len1
> __len2
)
2730 __len11
= __len1
/ 2;
2731 std::advance(__first_cut
, __len11
);
2732 __second_cut
= std::lower_bound(__middle
, __last
,
2734 __len22
= std::distance(__middle
, __second_cut
);
2738 __len22
= __len2
/ 2;
2739 std::advance(__second_cut
, __len22
);
2740 __first_cut
= std::upper_bound(__first
, __middle
,
2742 __len11
= std::distance(__first
, __first_cut
);
2744 _BidirectionalIterator __new_middle
=
2745 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2746 __len1
- __len11
, __len22
, __buffer
,
2748 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2749 __len22
, __buffer
, __buffer_size
);
2750 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2752 __len2
- __len22
, __buffer
, __buffer_size
);
2758 * This is a helper function for the merge routines.
2761 template<typename _BidirectionalIterator
, typename _Distance
,
2762 typename _Pointer
, typename _Compare
>
2764 __merge_adaptive(_BidirectionalIterator __first
,
2765 _BidirectionalIterator __middle
,
2766 _BidirectionalIterator __last
,
2767 _Distance __len1
, _Distance __len2
,
2768 _Pointer __buffer
, _Distance __buffer_size
,
2771 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2773 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2774 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2777 else if (__len2
<= __buffer_size
)
2779 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2780 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2785 _BidirectionalIterator __first_cut
= __first
;
2786 _BidirectionalIterator __second_cut
= __middle
;
2787 _Distance __len11
= 0;
2788 _Distance __len22
= 0;
2789 if (__len1
> __len2
)
2791 __len11
= __len1
/ 2;
2792 std::advance(__first_cut
, __len11
);
2793 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2795 __len22
= std::distance(__middle
, __second_cut
);
2799 __len22
= __len2
/ 2;
2800 std::advance(__second_cut
, __len22
);
2801 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2803 __len11
= std::distance(__first
, __first_cut
);
2805 _BidirectionalIterator __new_middle
=
2806 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2807 __len1
- __len11
, __len22
, __buffer
,
2809 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2810 __len22
, __buffer
, __buffer_size
, __comp
);
2811 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2813 __len2
- __len22
, __buffer
,
2814 __buffer_size
, __comp
);
2820 * This is a helper function for the merge routines.
2823 template<typename _BidirectionalIterator
, typename _Distance
>
2825 __merge_without_buffer(_BidirectionalIterator __first
,
2826 _BidirectionalIterator __middle
,
2827 _BidirectionalIterator __last
,
2828 _Distance __len1
, _Distance __len2
)
2830 if (__len1
== 0 || __len2
== 0)
2832 if (__len1
+ __len2
== 2)
2834 if (*__middle
< *__first
)
2835 std::iter_swap(__first
, __middle
);
2838 _BidirectionalIterator __first_cut
= __first
;
2839 _BidirectionalIterator __second_cut
= __middle
;
2840 _Distance __len11
= 0;
2841 _Distance __len22
= 0;
2842 if (__len1
> __len2
)
2844 __len11
= __len1
/ 2;
2845 std::advance(__first_cut
, __len11
);
2846 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2847 __len22
= std::distance(__middle
, __second_cut
);
2851 __len22
= __len2
/ 2;
2852 std::advance(__second_cut
, __len22
);
2853 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2854 __len11
= std::distance(__first
, __first_cut
);
2856 std::rotate(__first_cut
, __middle
, __second_cut
);
2857 _BidirectionalIterator __new_middle
= __first_cut
;
2858 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2859 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2861 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2862 __len1
- __len11
, __len2
- __len22
);
2867 * This is a helper function for the merge routines.
2870 template<typename _BidirectionalIterator
, typename _Distance
,
2873 __merge_without_buffer(_BidirectionalIterator __first
,
2874 _BidirectionalIterator __middle
,
2875 _BidirectionalIterator __last
,
2876 _Distance __len1
, _Distance __len2
,
2879 if (__len1
== 0 || __len2
== 0)
2881 if (__len1
+ __len2
== 2)
2883 if (__comp(*__middle
, *__first
))
2884 std::iter_swap(__first
, __middle
);
2887 _BidirectionalIterator __first_cut
= __first
;
2888 _BidirectionalIterator __second_cut
= __middle
;
2889 _Distance __len11
= 0;
2890 _Distance __len22
= 0;
2891 if (__len1
> __len2
)
2893 __len11
= __len1
/ 2;
2894 std::advance(__first_cut
, __len11
);
2895 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2897 __len22
= std::distance(__middle
, __second_cut
);
2901 __len22
= __len2
/ 2;
2902 std::advance(__second_cut
, __len22
);
2903 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2905 __len11
= std::distance(__first
, __first_cut
);
2907 std::rotate(__first_cut
, __middle
, __second_cut
);
2908 _BidirectionalIterator __new_middle
= __first_cut
;
2909 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2910 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2911 __len11
, __len22
, __comp
);
2912 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2913 __len1
- __len11
, __len2
- __len22
, __comp
);
2917 * @brief Merges two sorted ranges in place.
2918 * @param first An iterator.
2919 * @param middle Another iterator.
2920 * @param last Another iterator.
2923 * Merges two sorted and consecutive ranges, [first,middle) and
2924 * [middle,last), and puts the result in [first,last). The output will
2925 * be sorted. The sort is @e stable, that is, for equivalent
2926 * elements in the two ranges, elements from the first range will always
2927 * come before elements from the second.
2929 * If enough additional memory is available, this takes (last-first)-1
2930 * comparisons. Otherwise an NlogN algorithm is used, where N is
2931 * distance(first,last).
2933 template<typename _BidirectionalIterator
>
2935 inplace_merge(_BidirectionalIterator __first
,
2936 _BidirectionalIterator __middle
,
2937 _BidirectionalIterator __last
)
2939 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2941 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2944 // concept requirements
2945 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2946 _BidirectionalIterator
>)
2947 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2948 __glibcxx_requires_sorted(__first
, __middle
);
2949 __glibcxx_requires_sorted(__middle
, __last
);
2951 if (__first
== __middle
|| __middle
== __last
)
2954 _DistanceType __len1
= std::distance(__first
, __middle
);
2955 _DistanceType __len2
= std::distance(__middle
, __last
);
2957 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2959 if (__buf
.begin() == 0)
2960 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
2962 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2963 __buf
.begin(), _DistanceType(__buf
.size()));
2967 * @brief Merges two sorted ranges in place.
2968 * @param first An iterator.
2969 * @param middle Another iterator.
2970 * @param last Another iterator.
2971 * @param comp A functor to use for comparisons.
2974 * Merges two sorted and consecutive ranges, [first,middle) and
2975 * [middle,last), and puts the result in [first,last). The output will
2976 * be sorted. The sort is @e stable, that is, for equivalent
2977 * elements in the two ranges, elements from the first range will always
2978 * come before elements from the second.
2980 * If enough additional memory is available, this takes (last-first)-1
2981 * comparisons. Otherwise an NlogN algorithm is used, where N is
2982 * distance(first,last).
2984 * The comparison function should have the same effects on ordering as
2985 * the function used for the initial sort.
2987 template<typename _BidirectionalIterator
, typename _Compare
>
2989 inplace_merge(_BidirectionalIterator __first
,
2990 _BidirectionalIterator __middle
,
2991 _BidirectionalIterator __last
,
2994 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2996 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2999 // concept requirements
3000 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3001 _BidirectionalIterator
>)
3002 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3003 _ValueType
, _ValueType
>)
3004 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3005 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3007 if (__first
== __middle
|| __middle
== __last
)
3010 const _DistanceType __len1
= std::distance(__first
, __middle
);
3011 const _DistanceType __len2
= std::distance(__middle
, __last
);
3013 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3015 if (__buf
.begin() == 0)
3016 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3019 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3020 __buf
.begin(), _DistanceType(__buf
.size()),
3024 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3027 __merge_sort_loop(_RandomAccessIterator1 __first
,
3028 _RandomAccessIterator1 __last
,
3029 _RandomAccessIterator2 __result
,
3030 _Distance __step_size
)
3032 const _Distance __two_step
= 2 * __step_size
;
3034 while (__last
- __first
>= __two_step
)
3036 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3037 __first
+ __step_size
,
3038 __first
+ __two_step
,
3040 __first
+= __two_step
;
3043 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3044 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3045 __first
+ __step_size
, __last
,
3049 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3050 typename _Distance
, typename _Compare
>
3052 __merge_sort_loop(_RandomAccessIterator1 __first
,
3053 _RandomAccessIterator1 __last
,
3054 _RandomAccessIterator2 __result
, _Distance __step_size
,
3057 const _Distance __two_step
= 2 * __step_size
;
3059 while (__last
- __first
>= __two_step
)
3061 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3062 __first
+ __step_size
, __first
+ __two_step
,
3065 __first
+= __two_step
;
3067 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3069 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3070 __first
+ __step_size
, __last
, __result
, __comp
);
3073 template<typename _RandomAccessIterator
, typename _Distance
>
3075 __chunk_insertion_sort(_RandomAccessIterator __first
,
3076 _RandomAccessIterator __last
,
3077 _Distance __chunk_size
)
3079 while (__last
- __first
>= __chunk_size
)
3081 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3082 __first
+= __chunk_size
;
3084 std::__insertion_sort(__first
, __last
);
3087 template<typename _RandomAccessIterator
, typename _Distance
,
3090 __chunk_insertion_sort(_RandomAccessIterator __first
,
3091 _RandomAccessIterator __last
,
3092 _Distance __chunk_size
, _Compare __comp
)
3094 while (__last
- __first
>= __chunk_size
)
3096 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3097 __first
+= __chunk_size
;
3099 std::__insertion_sort(__first
, __last
, __comp
);
3102 enum { _S_chunk_size
= 7 };
3104 template<typename _RandomAccessIterator
, typename _Pointer
>
3106 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3107 _RandomAccessIterator __last
,
3110 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3113 const _Distance __len
= __last
- __first
;
3114 const _Pointer __buffer_last
= __buffer
+ __len
;
3116 _Distance __step_size
= _S_chunk_size
;
3117 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3119 while (__step_size
< __len
)
3121 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3123 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3128 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3130 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3131 _RandomAccessIterator __last
,
3132 _Pointer __buffer
, _Compare __comp
)
3134 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3137 const _Distance __len
= __last
- __first
;
3138 const _Pointer __buffer_last
= __buffer
+ __len
;
3140 _Distance __step_size
= _S_chunk_size
;
3141 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3143 while (__step_size
< __len
)
3145 std::__merge_sort_loop(__first
, __last
, __buffer
,
3146 __step_size
, __comp
);
3148 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3149 __step_size
, __comp
);
3154 template<typename _RandomAccessIterator
, typename _Pointer
,
3157 __stable_sort_adaptive(_RandomAccessIterator __first
,
3158 _RandomAccessIterator __last
,
3159 _Pointer __buffer
, _Distance __buffer_size
)
3161 const _Distance __len
= (__last
- __first
+ 1) / 2;
3162 const _RandomAccessIterator __middle
= __first
+ __len
;
3163 if (__len
> __buffer_size
)
3165 std::__stable_sort_adaptive(__first
, __middle
,
3166 __buffer
, __buffer_size
);
3167 std::__stable_sort_adaptive(__middle
, __last
,
3168 __buffer
, __buffer_size
);
3172 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3173 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3175 std::__merge_adaptive(__first
, __middle
, __last
,
3176 _Distance(__middle
- __first
),
3177 _Distance(__last
- __middle
),
3178 __buffer
, __buffer_size
);
3181 template<typename _RandomAccessIterator
, typename _Pointer
,
3182 typename _Distance
, typename _Compare
>
3184 __stable_sort_adaptive(_RandomAccessIterator __first
,
3185 _RandomAccessIterator __last
,
3186 _Pointer __buffer
, _Distance __buffer_size
,
3189 const _Distance __len
= (__last
- __first
+ 1) / 2;
3190 const _RandomAccessIterator __middle
= __first
+ __len
;
3191 if (__len
> __buffer_size
)
3193 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3194 __buffer_size
, __comp
);
3195 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3196 __buffer_size
, __comp
);
3200 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3201 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3203 std::__merge_adaptive(__first
, __middle
, __last
,
3204 _Distance(__middle
- __first
),
3205 _Distance(__last
- __middle
),
3206 __buffer
, __buffer_size
,
3212 * This is a helper function for the stable sorting routines.
3215 template<typename _RandomAccessIterator
>
3217 __inplace_stable_sort(_RandomAccessIterator __first
,
3218 _RandomAccessIterator __last
)
3220 if (__last
- __first
< 15)
3222 std::__insertion_sort(__first
, __last
);
3225 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3226 std::__inplace_stable_sort(__first
, __middle
);
3227 std::__inplace_stable_sort(__middle
, __last
);
3228 std::__merge_without_buffer(__first
, __middle
, __last
,
3235 * This is a helper function for the stable sorting routines.
3238 template<typename _RandomAccessIterator
, typename _Compare
>
3240 __inplace_stable_sort(_RandomAccessIterator __first
,
3241 _RandomAccessIterator __last
, _Compare __comp
)
3243 if (__last
- __first
< 15)
3245 std::__insertion_sort(__first
, __last
, __comp
);
3248 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3249 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3250 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3251 std::__merge_without_buffer(__first
, __middle
, __last
,
3259 // Set algorithms: includes, set_union, set_intersection, set_difference,
3260 // set_symmetric_difference. All of these algorithms have the precondition
3261 // that their input ranges are sorted and the postcondition that their output
3262 // ranges are sorted.
3265 * @brief Determines whether all elements of a sequence exists in a range.
3266 * @param first1 Start of search range.
3267 * @param last1 End of search range.
3268 * @param first2 Start of sequence
3269 * @param last2 End of sequence.
3270 * @return True if each element in [first2,last2) is contained in order
3271 * within [first1,last1). False otherwise.
3272 * @ingroup setoperations
3274 * This operation expects both [first1,last1) and [first2,last2) to be
3275 * sorted. Searches for the presence of each element in [first2,last2)
3276 * within [first1,last1). The iterators over each range only move forward,
3277 * so this is a linear algorithm. If an element in [first2,last2) is not
3278 * found before the search iterator reaches @a last2, false is returned.
3280 template<typename _InputIterator1
, typename _InputIterator2
>
3282 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3283 _InputIterator2 __first2
, _InputIterator2 __last2
)
3285 typedef typename iterator_traits
<_InputIterator1
>::value_type
3287 typedef typename iterator_traits
<_InputIterator2
>::value_type
3290 // concept requirements
3291 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3292 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3293 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3294 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3295 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3296 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3298 while (__first1
!= __last1
&& __first2
!= __last2
)
3299 if (*__first2
< *__first1
)
3301 else if(*__first1
< *__first2
)
3304 ++__first1
, ++__first2
;
3306 return __first2
== __last2
;
3310 * @brief Determines whether all elements of a sequence exists in a range
3312 * @param first1 Start of search range.
3313 * @param last1 End of search range.
3314 * @param first2 Start of sequence
3315 * @param last2 End of sequence.
3316 * @param comp Comparison function to use.
3317 * @return True if each element in [first2,last2) is contained in order
3318 * within [first1,last1) according to comp. False otherwise.
3319 * @ingroup setoperations
3321 * This operation expects both [first1,last1) and [first2,last2) to be
3322 * sorted. Searches for the presence of each element in [first2,last2)
3323 * within [first1,last1), using comp to decide. The iterators over each
3324 * range only move forward, so this is a linear algorithm. If an element
3325 * in [first2,last2) is not found before the search iterator reaches @a
3326 * last2, false is returned.
3328 template<typename _InputIterator1
, typename _InputIterator2
,
3331 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3332 _InputIterator2 __first2
, _InputIterator2 __last2
,
3335 typedef typename iterator_traits
<_InputIterator1
>::value_type
3337 typedef typename iterator_traits
<_InputIterator2
>::value_type
3340 // concept requirements
3341 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3342 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3343 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3344 _ValueType1
, _ValueType2
>)
3345 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3346 _ValueType2
, _ValueType1
>)
3347 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3348 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3350 while (__first1
!= __last1
&& __first2
!= __last2
)
3351 if (__comp(*__first2
, *__first1
))
3353 else if(__comp(*__first1
, *__first2
))
3356 ++__first1
, ++__first2
;
3358 return __first2
== __last2
;
3367 // set_symmetric_difference
3372 * @brief Permute range into the next "dictionary" ordering.
3373 * @param first Start of range.
3374 * @param last End of range.
3375 * @return False if wrapped to first permutation, true otherwise.
3377 * Treats all permutations of the range as a set of "dictionary" sorted
3378 * sequences. Permutes the current sequence into the next one of this set.
3379 * Returns true if there are more sequences to generate. If the sequence
3380 * is the largest of the set, the smallest is generated and false returned.
3382 template<typename _BidirectionalIterator
>
3384 next_permutation(_BidirectionalIterator __first
,
3385 _BidirectionalIterator __last
)
3387 // concept requirements
3388 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3389 _BidirectionalIterator
>)
3390 __glibcxx_function_requires(_LessThanComparableConcept
<
3391 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3392 __glibcxx_requires_valid_range(__first
, __last
);
3394 if (__first
== __last
)
3396 _BidirectionalIterator __i
= __first
;
3405 _BidirectionalIterator __ii
= __i
;
3409 _BidirectionalIterator __j
= __last
;
3410 while (!(*__i
< *--__j
))
3412 std::iter_swap(__i
, __j
);
3413 std::reverse(__ii
, __last
);
3418 std::reverse(__first
, __last
);
3425 * @brief Permute range into the next "dictionary" ordering using
3426 * comparison functor.
3427 * @param first Start of range.
3428 * @param last End of range.
3429 * @param comp A comparison functor.
3430 * @return False if wrapped to first permutation, true otherwise.
3432 * Treats all permutations of the range [first,last) as a set of
3433 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3434 * sequence into the next one of this set. Returns true if there are more
3435 * sequences to generate. If the sequence is the largest of the set, the
3436 * smallest is generated and false returned.
3438 template<typename _BidirectionalIterator
, typename _Compare
>
3440 next_permutation(_BidirectionalIterator __first
,
3441 _BidirectionalIterator __last
, _Compare __comp
)
3443 // concept requirements
3444 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3445 _BidirectionalIterator
>)
3446 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3447 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3448 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3449 __glibcxx_requires_valid_range(__first
, __last
);
3451 if (__first
== __last
)
3453 _BidirectionalIterator __i
= __first
;
3462 _BidirectionalIterator __ii
= __i
;
3464 if (__comp(*__i
, *__ii
))
3466 _BidirectionalIterator __j
= __last
;
3467 while (!bool(__comp(*__i
, *--__j
)))
3469 std::iter_swap(__i
, __j
);
3470 std::reverse(__ii
, __last
);
3475 std::reverse(__first
, __last
);
3482 * @brief Permute range into the previous "dictionary" ordering.
3483 * @param first Start of range.
3484 * @param last End of range.
3485 * @return False if wrapped to last permutation, true otherwise.
3487 * Treats all permutations of the range as a set of "dictionary" sorted
3488 * sequences. Permutes the current sequence into the previous one of this
3489 * set. Returns true if there are more sequences to generate. If the
3490 * sequence is the smallest of the set, the largest is generated and false
3493 template<typename _BidirectionalIterator
>
3495 prev_permutation(_BidirectionalIterator __first
,
3496 _BidirectionalIterator __last
)
3498 // concept requirements
3499 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3500 _BidirectionalIterator
>)
3501 __glibcxx_function_requires(_LessThanComparableConcept
<
3502 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3503 __glibcxx_requires_valid_range(__first
, __last
);
3505 if (__first
== __last
)
3507 _BidirectionalIterator __i
= __first
;
3516 _BidirectionalIterator __ii
= __i
;
3520 _BidirectionalIterator __j
= __last
;
3521 while (!(*--__j
< *__i
))
3523 std::iter_swap(__i
, __j
);
3524 std::reverse(__ii
, __last
);
3529 std::reverse(__first
, __last
);
3536 * @brief Permute range into the previous "dictionary" ordering using
3537 * comparison functor.
3538 * @param first Start of range.
3539 * @param last End of range.
3540 * @param comp A comparison functor.
3541 * @return False if wrapped to last permutation, true otherwise.
3543 * Treats all permutations of the range [first,last) as a set of
3544 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3545 * sequence into the previous one of this set. Returns true if there are
3546 * more sequences to generate. If the sequence is the smallest of the set,
3547 * the largest is generated and false returned.
3549 template<typename _BidirectionalIterator
, typename _Compare
>
3551 prev_permutation(_BidirectionalIterator __first
,
3552 _BidirectionalIterator __last
, _Compare __comp
)
3554 // concept requirements
3555 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3556 _BidirectionalIterator
>)
3557 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3558 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3559 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3560 __glibcxx_requires_valid_range(__first
, __last
);
3562 if (__first
== __last
)
3564 _BidirectionalIterator __i
= __first
;
3573 _BidirectionalIterator __ii
= __i
;
3575 if (__comp(*__ii
, *__i
))
3577 _BidirectionalIterator __j
= __last
;
3578 while (!bool(__comp(*--__j
, *__i
)))
3580 std::iter_swap(__i
, __j
);
3581 std::reverse(__ii
, __last
);
3586 std::reverse(__first
, __last
);
3596 * @brief Copy a sequence, replacing each element of one value with another
3598 * @param first An input iterator.
3599 * @param last An input iterator.
3600 * @param result An output iterator.
3601 * @param old_value The value to be replaced.
3602 * @param new_value The replacement value.
3603 * @return The end of the output sequence, @p result+(last-first).
3605 * Copies each element in the input range @p [first,last) to the
3606 * output range @p [result,result+(last-first)) replacing elements
3607 * equal to @p old_value with @p new_value.
3609 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3611 replace_copy(_InputIterator __first
, _InputIterator __last
,
3612 _OutputIterator __result
,
3613 const _Tp
& __old_value
, const _Tp
& __new_value
)
3615 // concept requirements
3616 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3617 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3618 typename iterator_traits
<_InputIterator
>::value_type
>)
3619 __glibcxx_function_requires(_EqualOpConcept
<
3620 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3621 __glibcxx_requires_valid_range(__first
, __last
);
3623 for (; __first
!= __last
; ++__first
, ++__result
)
3624 if (*__first
== __old_value
)
3625 *__result
= __new_value
;
3627 *__result
= *__first
;
3632 * @brief Copy a sequence, replacing each value for which a predicate
3633 * returns true with another value.
3634 * @param first An input iterator.
3635 * @param last An input iterator.
3636 * @param result An output iterator.
3637 * @param pred A predicate.
3638 * @param new_value The replacement value.
3639 * @return The end of the output sequence, @p result+(last-first).
3641 * Copies each element in the range @p [first,last) to the range
3642 * @p [result,result+(last-first)) replacing elements for which
3643 * @p pred returns true with @p new_value.
3645 template<typename _InputIterator
, typename _OutputIterator
,
3646 typename _Predicate
, typename _Tp
>
3648 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3649 _OutputIterator __result
,
3650 _Predicate __pred
, const _Tp
& __new_value
)
3652 // concept requirements
3653 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3654 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3655 typename iterator_traits
<_InputIterator
>::value_type
>)
3656 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3657 typename iterator_traits
<_InputIterator
>::value_type
>)
3658 __glibcxx_requires_valid_range(__first
, __last
);
3660 for (; __first
!= __last
; ++__first
, ++__result
)
3661 if (__pred(*__first
))
3662 *__result
= __new_value
;
3664 *__result
= *__first
;
3668 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3670 * @brief Determines whether the elements of a sequence are sorted.
3671 * @param first An iterator.
3672 * @param last Another iterator.
3673 * @return True if the elements are sorted, false otherwise.
3675 template<typename _ForwardIterator
>
3677 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3678 { return std::is_sorted_until(__first
, __last
) == __last
; }
3681 * @brief Determines whether the elements of a sequence are sorted
3682 * according to a comparison functor.
3683 * @param first An iterator.
3684 * @param last Another iterator.
3685 * @param comp A comparison functor.
3686 * @return True if the elements are sorted, false otherwise.
3688 template<typename _ForwardIterator
, typename _Compare
>
3690 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3692 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3695 * @brief Determines the end of a sorted sequence.
3696 * @param first An iterator.
3697 * @param last Another iterator.
3698 * @return An iterator pointing to the last iterator i in [first, last)
3699 * for which the range [first, i) is sorted.
3701 template<typename _ForwardIterator
>
3703 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3705 // concept requirements
3706 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3707 __glibcxx_function_requires(_LessThanComparableConcept
<
3708 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3709 __glibcxx_requires_valid_range(__first
, __last
);
3711 if (__first
== __last
)
3714 _ForwardIterator __next
= __first
;
3715 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3716 if (*__next
< *__first
)
3722 * @brief Determines the end of a sorted sequence using comparison functor.
3723 * @param first An iterator.
3724 * @param last Another iterator.
3725 * @param comp A comparison functor.
3726 * @return An iterator pointing to the last iterator i in [first, last)
3727 * for which the range [first, i) is sorted.
3729 template<typename _ForwardIterator
, typename _Compare
>
3731 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3734 // concept requirements
3735 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3736 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3737 typename iterator_traits
<_ForwardIterator
>::value_type
,
3738 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3739 __glibcxx_requires_valid_range(__first
, __last
);
3741 if (__first
== __last
)
3744 _ForwardIterator __next
= __first
;
3745 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3746 if (__comp(*__next
, *__first
))
3752 * @brief Determines min and max at once as an ordered pair.
3753 * @param a A thing of arbitrary type.
3754 * @param b Another thing of arbitrary type.
3755 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3757 template<typename _Tp
>
3758 inline pair
<const _Tp
&, const _Tp
&>
3759 minmax(const _Tp
& __a
, const _Tp
& __b
)
3761 // concept requirements
3762 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3764 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3765 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3769 * @brief Determines min and max at once as an ordered pair.
3770 * @param a A thing of arbitrary type.
3771 * @param b Another thing of arbitrary type.
3772 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3773 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3775 template<typename _Tp
, typename _Compare
>
3776 inline pair
<const _Tp
&, const _Tp
&>
3777 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3779 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3780 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3784 * @brief Return a pair of iterators pointing to the minimum and maximum
3785 * elements in a range.
3786 * @param first Start of range.
3787 * @param last End of range.
3788 * @return make_pair(m, M), where m is the first iterator i in
3789 * [first, last) such that no other element in the range is
3790 * smaller, and where M is the last iterator i in [first, last)
3791 * such that no other element in the range is larger.
3793 template<typename _ForwardIterator
>
3794 pair
<_ForwardIterator
, _ForwardIterator
>
3795 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3797 // concept requirements
3798 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3799 __glibcxx_function_requires(_LessThanComparableConcept
<
3800 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3801 __glibcxx_requires_valid_range(__first
, __last
);
3803 _ForwardIterator __next
= __first
;
3804 if (__first
== __last
3805 || ++__next
== __last
)
3806 return std::make_pair(__first
, __first
);
3808 _ForwardIterator __min
, __max
;
3809 if (*__next
< *__first
)
3823 while (__first
!= __last
)
3826 if (++__next
== __last
)
3828 if (*__first
< *__min
)
3830 else if (!(*__first
< *__max
))
3835 if (*__next
< *__first
)
3837 if (*__next
< *__min
)
3839 if (!(*__first
< *__max
))
3844 if (*__first
< *__min
)
3846 if (!(*__next
< *__max
))
3854 return std::make_pair(__min
, __max
);
3858 * @brief Return a pair of iterators pointing to the minimum and maximum
3859 * elements in a range.
3860 * @param first Start of range.
3861 * @param last End of range.
3862 * @param comp Comparison functor.
3863 * @return make_pair(m, M), where m is the first iterator i in
3864 * [first, last) such that no other element in the range is
3865 * smaller, and where M is the last iterator i in [first, last)
3866 * such that no other element in the range is larger.
3868 template<typename _ForwardIterator
, typename _Compare
>
3869 pair
<_ForwardIterator
, _ForwardIterator
>
3870 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3873 // concept requirements
3874 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3875 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3876 typename iterator_traits
<_ForwardIterator
>::value_type
,
3877 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3878 __glibcxx_requires_valid_range(__first
, __last
);
3880 _ForwardIterator __next
= __first
;
3881 if (__first
== __last
3882 || ++__next
== __last
)
3883 return std::make_pair(__first
, __first
);
3885 _ForwardIterator __min
, __max
;
3886 if (__comp(*__next
, *__first
))
3900 while (__first
!= __last
)
3903 if (++__next
== __last
)
3905 if (__comp(*__first
, *__min
))
3907 else if (!__comp(*__first
, *__max
))
3912 if (__comp(*__next
, *__first
))
3914 if (__comp(*__next
, *__min
))
3916 if (!__comp(*__first
, *__max
))
3921 if (__comp(*__first
, *__min
))
3923 if (!__comp(*__next
, *__max
))
3931 return std::make_pair(__min
, __max
);
3933 #endif // __GXX_EXPERIMENTAL_CXX0X__
3935 _GLIBCXX_END_NAMESPACE
3937 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3940 * @brief Apply a function to every element of a sequence.
3941 * @param first An input iterator.
3942 * @param last An input iterator.
3943 * @param f A unary function object.
3946 * Applies the function object @p f to each element in the range
3947 * @p [first,last). @p f must not modify the order of the sequence.
3948 * If @p f has a return value it is ignored.
3950 template<typename _InputIterator
, typename _Function
>
3952 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3954 // concept requirements
3955 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3956 __glibcxx_requires_valid_range(__first
, __last
);
3957 for (; __first
!= __last
; ++__first
)
3963 * @brief Find the first occurrence of a value in a sequence.
3964 * @param first An input iterator.
3965 * @param last An input iterator.
3966 * @param val The value to find.
3967 * @return The first iterator @c i in the range @p [first,last)
3968 * such that @c *i == @p val, or @p last if no such iterator exists.
3970 template<typename _InputIterator
, typename _Tp
>
3971 inline _InputIterator
3972 find(_InputIterator __first
, _InputIterator __last
,
3975 // concept requirements
3976 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3977 __glibcxx_function_requires(_EqualOpConcept
<
3978 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3979 __glibcxx_requires_valid_range(__first
, __last
);
3980 return std::__find(__first
, __last
, __val
,
3981 std::__iterator_category(__first
));
3985 * @brief Find the first element in a sequence for which a
3986 * predicate is true.
3987 * @param first An input iterator.
3988 * @param last An input iterator.
3989 * @param pred A predicate.
3990 * @return The first iterator @c i in the range @p [first,last)
3991 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3993 template<typename _InputIterator
, typename _Predicate
>
3994 inline _InputIterator
3995 find_if(_InputIterator __first
, _InputIterator __last
,
3998 // concept requirements
3999 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4000 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4001 typename iterator_traits
<_InputIterator
>::value_type
>)
4002 __glibcxx_requires_valid_range(__first
, __last
);
4003 return std::__find_if(__first
, __last
, __pred
,
4004 std::__iterator_category(__first
));
4008 * @brief Find element from a set in a sequence.
4009 * @param first1 Start of range to search.
4010 * @param last1 End of range to search.
4011 * @param first2 Start of match candidates.
4012 * @param last2 End of match candidates.
4013 * @return The first iterator @c i in the range
4014 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4015 * interator in [first2,last2), or @p last1 if no such iterator exists.
4017 * Searches the range @p [first1,last1) for an element that is equal to
4018 * some element in the range [first2,last2). If found, returns an iterator
4019 * in the range [first1,last1), otherwise returns @p last1.
4021 template<typename _InputIterator
, typename _ForwardIterator
>
4023 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4024 _ForwardIterator __first2
, _ForwardIterator __last2
)
4026 // concept requirements
4027 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4028 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4029 __glibcxx_function_requires(_EqualOpConcept
<
4030 typename iterator_traits
<_InputIterator
>::value_type
,
4031 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4032 __glibcxx_requires_valid_range(__first1
, __last1
);
4033 __glibcxx_requires_valid_range(__first2
, __last2
);
4035 for (; __first1
!= __last1
; ++__first1
)
4036 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4037 if (*__first1
== *__iter
)
4043 * @brief Find element from a set in a sequence using a predicate.
4044 * @param first1 Start of range to search.
4045 * @param last1 End of range to search.
4046 * @param first2 Start of match candidates.
4047 * @param last2 End of match candidates.
4048 * @param comp Predicate to use.
4049 * @return The first iterator @c i in the range
4050 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4051 * interator in [first2,last2), or @p last1 if no such iterator exists.
4054 * Searches the range @p [first1,last1) for an element that is
4055 * equal to some element in the range [first2,last2). If found,
4056 * returns an iterator in the range [first1,last1), otherwise
4059 template<typename _InputIterator
, typename _ForwardIterator
,
4060 typename _BinaryPredicate
>
4062 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4063 _ForwardIterator __first2
, _ForwardIterator __last2
,
4064 _BinaryPredicate __comp
)
4066 // concept requirements
4067 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4068 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4069 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4070 typename iterator_traits
<_InputIterator
>::value_type
,
4071 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4072 __glibcxx_requires_valid_range(__first1
, __last1
);
4073 __glibcxx_requires_valid_range(__first2
, __last2
);
4075 for (; __first1
!= __last1
; ++__first1
)
4076 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4077 if (__comp(*__first1
, *__iter
))
4083 * @brief Find two adjacent values in a sequence that are equal.
4084 * @param first A forward iterator.
4085 * @param last A forward iterator.
4086 * @return The first iterator @c i such that @c i and @c i+1 are both
4087 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4088 * or @p last if no such iterator exists.
4090 template<typename _ForwardIterator
>
4092 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4094 // concept requirements
4095 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4096 __glibcxx_function_requires(_EqualityComparableConcept
<
4097 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4098 __glibcxx_requires_valid_range(__first
, __last
);
4099 if (__first
== __last
)
4101 _ForwardIterator __next
= __first
;
4102 while(++__next
!= __last
)
4104 if (*__first
== *__next
)
4112 * @brief Find two adjacent values in a sequence using a predicate.
4113 * @param first A forward iterator.
4114 * @param last A forward iterator.
4115 * @param binary_pred A binary predicate.
4116 * @return The first iterator @c i such that @c i and @c i+1 are both
4117 * valid iterators in @p [first,last) and such that
4118 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4121 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4123 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4124 _BinaryPredicate __binary_pred
)
4126 // concept requirements
4127 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4128 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4129 typename iterator_traits
<_ForwardIterator
>::value_type
,
4130 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4131 __glibcxx_requires_valid_range(__first
, __last
);
4132 if (__first
== __last
)
4134 _ForwardIterator __next
= __first
;
4135 while(++__next
!= __last
)
4137 if (__binary_pred(*__first
, *__next
))
4145 * @brief Count the number of copies of a value in a sequence.
4146 * @param first An input iterator.
4147 * @param last An input iterator.
4148 * @param value The value to be counted.
4149 * @return The number of iterators @c i in the range @p [first,last)
4150 * for which @c *i == @p value
4152 template<typename _InputIterator
, typename _Tp
>
4153 typename iterator_traits
<_InputIterator
>::difference_type
4154 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4156 // concept requirements
4157 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4158 __glibcxx_function_requires(_EqualOpConcept
<
4159 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4160 __glibcxx_requires_valid_range(__first
, __last
);
4161 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4162 for (; __first
!= __last
; ++__first
)
4163 if (*__first
== __value
)
4169 * @brief Count the elements of a sequence for which a predicate is true.
4170 * @param first An input iterator.
4171 * @param last An input iterator.
4172 * @param pred A predicate.
4173 * @return The number of iterators @c i in the range @p [first,last)
4174 * for which @p pred(*i) is true.
4176 template<typename _InputIterator
, typename _Predicate
>
4177 typename iterator_traits
<_InputIterator
>::difference_type
4178 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4180 // concept requirements
4181 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4182 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4183 typename iterator_traits
<_InputIterator
>::value_type
>)
4184 __glibcxx_requires_valid_range(__first
, __last
);
4185 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4186 for (; __first
!= __last
; ++__first
)
4187 if (__pred(*__first
))
4193 * @brief Search a sequence for a matching sub-sequence.
4194 * @param first1 A forward iterator.
4195 * @param last1 A forward iterator.
4196 * @param first2 A forward iterator.
4197 * @param last2 A forward iterator.
4198 * @return The first iterator @c i in the range
4199 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4200 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4201 * such iterator exists.
4203 * Searches the range @p [first1,last1) for a sub-sequence that compares
4204 * equal value-by-value with the sequence given by @p [first2,last2) and
4205 * returns an iterator to the first element of the sub-sequence, or
4206 * @p last1 if the sub-sequence is not found.
4208 * Because the sub-sequence must lie completely within the range
4209 * @p [first1,last1) it must start at a position less than
4210 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4212 * This means that the returned iterator @c i will be in the range
4213 * @p [first1,last1-(last2-first2))
4215 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4217 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4218 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4220 // concept requirements
4221 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4222 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4223 __glibcxx_function_requires(_EqualOpConcept
<
4224 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4225 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4226 __glibcxx_requires_valid_range(__first1
, __last1
);
4227 __glibcxx_requires_valid_range(__first2
, __last2
);
4229 // Test for empty ranges
4230 if (__first1
== __last1
|| __first2
== __last2
)
4233 // Test for a pattern of length 1.
4234 _ForwardIterator2
__p1(__first2
);
4235 if (++__p1
== __last2
)
4236 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4239 _ForwardIterator2 __p
;
4240 _ForwardIterator1 __current
= __first1
;
4244 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4245 if (__first1
== __last1
)
4249 __current
= __first1
;
4250 if (++__current
== __last1
)
4253 while (*__current
== *__p
)
4255 if (++__p
== __last2
)
4257 if (++__current
== __last1
)
4266 * @brief Search a sequence for a matching sub-sequence using a predicate.
4267 * @param first1 A forward iterator.
4268 * @param last1 A forward iterator.
4269 * @param first2 A forward iterator.
4270 * @param last2 A forward iterator.
4271 * @param predicate A binary predicate.
4272 * @return The first iterator @c i in the range
4273 * @p [first1,last1-(last2-first2)) such that
4274 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4275 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4277 * Searches the range @p [first1,last1) for a sub-sequence that compares
4278 * equal value-by-value with the sequence given by @p [first2,last2),
4279 * using @p predicate to determine equality, and returns an iterator
4280 * to the first element of the sub-sequence, or @p last1 if no such
4283 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4285 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4286 typename _BinaryPredicate
>
4288 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4289 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4290 _BinaryPredicate __predicate
)
4292 // concept requirements
4293 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4294 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4295 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4296 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4297 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4298 __glibcxx_requires_valid_range(__first1
, __last1
);
4299 __glibcxx_requires_valid_range(__first2
, __last2
);
4301 // Test for empty ranges
4302 if (__first1
== __last1
|| __first2
== __last2
)
4305 // Test for a pattern of length 1.
4306 _ForwardIterator2
__p1(__first2
);
4307 if (++__p1
== __last2
)
4309 while (__first1
!= __last1
4310 && !bool(__predicate(*__first1
, *__first2
)))
4316 _ForwardIterator2 __p
;
4317 _ForwardIterator1 __current
= __first1
;
4321 while (__first1
!= __last1
4322 && !bool(__predicate(*__first1
, *__first2
)))
4324 if (__first1
== __last1
)
4328 __current
= __first1
;
4329 if (++__current
== __last1
)
4332 while (__predicate(*__current
, *__p
))
4334 if (++__p
== __last2
)
4336 if (++__current
== __last1
)
4346 * @brief Search a sequence for a number of consecutive values.
4347 * @param first A forward iterator.
4348 * @param last A forward iterator.
4349 * @param count The number of consecutive values.
4350 * @param val The value to find.
4351 * @return The first iterator @c i in the range @p [first,last-count)
4352 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4353 * or @p last if no such iterator exists.
4355 * Searches the range @p [first,last) for @p count consecutive elements
4358 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4360 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4361 _Integer __count
, const _Tp
& __val
)
4363 // concept requirements
4364 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4365 __glibcxx_function_requires(_EqualOpConcept
<
4366 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4367 __glibcxx_requires_valid_range(__first
, __last
);
4372 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4373 return std::__search_n(__first
, __last
, __count
, __val
,
4374 std::__iterator_category(__first
));
4379 * @brief Search a sequence for a number of consecutive values using a
4381 * @param first A forward iterator.
4382 * @param last A forward iterator.
4383 * @param count The number of consecutive values.
4384 * @param val The value to find.
4385 * @param binary_pred A binary predicate.
4386 * @return The first iterator @c i in the range @p [first,last-count)
4387 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4388 * range @p [0,count), or @p last if no such iterator exists.
4390 * Searches the range @p [first,last) for @p count consecutive elements
4391 * for which the predicate returns true.
4393 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4394 typename _BinaryPredicate
>
4396 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4397 _Integer __count
, const _Tp
& __val
,
4398 _BinaryPredicate __binary_pred
)
4400 // concept requirements
4401 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4402 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4403 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4404 __glibcxx_requires_valid_range(__first
, __last
);
4410 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4414 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4415 std::__iterator_category(__first
));
4420 * @brief Perform an operation on a sequence.
4421 * @param first An input iterator.
4422 * @param last An input iterator.
4423 * @param result An output iterator.
4424 * @param unary_op A unary operator.
4425 * @return An output iterator equal to @p result+(last-first).
4427 * Applies the operator to each element in the input range and assigns
4428 * the results to successive elements of the output sequence.
4429 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4430 * range @p [0,last-first).
4432 * @p unary_op must not alter its argument.
4434 template<typename _InputIterator
, typename _OutputIterator
,
4435 typename _UnaryOperation
>
4437 transform(_InputIterator __first
, _InputIterator __last
,
4438 _OutputIterator __result
, _UnaryOperation __unary_op
)
4440 // concept requirements
4441 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4442 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4443 // "the type returned by a _UnaryOperation"
4444 __typeof__(__unary_op(*__first
))>)
4445 __glibcxx_requires_valid_range(__first
, __last
);
4447 for (; __first
!= __last
; ++__first
, ++__result
)
4448 *__result
= __unary_op(*__first
);
4453 * @brief Perform an operation on corresponding elements of two sequences.
4454 * @param first1 An input iterator.
4455 * @param last1 An input iterator.
4456 * @param first2 An input iterator.
4457 * @param result An output iterator.
4458 * @param binary_op A binary operator.
4459 * @return An output iterator equal to @p result+(last-first).
4461 * Applies the operator to the corresponding elements in the two
4462 * input ranges and assigns the results to successive elements of the
4464 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4465 * @c N in the range @p [0,last1-first1).
4467 * @p binary_op must not alter either of its arguments.
4469 template<typename _InputIterator1
, typename _InputIterator2
,
4470 typename _OutputIterator
, typename _BinaryOperation
>
4472 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4473 _InputIterator2 __first2
, _OutputIterator __result
,
4474 _BinaryOperation __binary_op
)
4476 // concept requirements
4477 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4478 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4479 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4480 // "the type returned by a _BinaryOperation"
4481 __typeof__(__binary_op(*__first1
,*__first2
))>)
4482 __glibcxx_requires_valid_range(__first1
, __last1
);
4484 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4485 *__result
= __binary_op(*__first1
, *__first2
);
4490 * @brief Replace each occurrence of one value in a sequence with another
4492 * @param first A forward iterator.
4493 * @param last A forward iterator.
4494 * @param old_value The value to be replaced.
4495 * @param new_value The replacement value.
4496 * @return replace() returns no value.
4498 * For each iterator @c i in the range @p [first,last) if @c *i ==
4499 * @p old_value then the assignment @c *i = @p new_value is performed.
4501 template<typename _ForwardIterator
, typename _Tp
>
4503 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4504 const _Tp
& __old_value
, const _Tp
& __new_value
)
4506 // concept requirements
4507 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4509 __glibcxx_function_requires(_EqualOpConcept
<
4510 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4511 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4512 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4513 __glibcxx_requires_valid_range(__first
, __last
);
4515 for (; __first
!= __last
; ++__first
)
4516 if (*__first
== __old_value
)
4517 *__first
= __new_value
;
4521 * @brief Replace each value in a sequence for which a predicate returns
4522 * true with another value.
4523 * @param first A forward iterator.
4524 * @param last A forward iterator.
4525 * @param pred A predicate.
4526 * @param new_value The replacement value.
4527 * @return replace_if() returns no value.
4529 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4530 * is true then the assignment @c *i = @p new_value is performed.
4532 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4534 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4535 _Predicate __pred
, const _Tp
& __new_value
)
4537 // concept requirements
4538 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4540 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4541 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4542 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4543 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4544 __glibcxx_requires_valid_range(__first
, __last
);
4546 for (; __first
!= __last
; ++__first
)
4547 if (__pred(*__first
))
4548 *__first
= __new_value
;
4552 * @brief Assign the result of a function object to each value in a
4554 * @param first A forward iterator.
4555 * @param last A forward iterator.
4556 * @param gen A function object taking no arguments and returning
4557 * std::iterator_traits<_ForwardIterator>::value_type
4558 * @return generate() returns no value.
4560 * Performs the assignment @c *i = @p gen() for each @c i in the range
4563 template<typename _ForwardIterator
, typename _Generator
>
4565 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4568 // concept requirements
4569 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4570 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4571 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4572 __glibcxx_requires_valid_range(__first
, __last
);
4574 for (; __first
!= __last
; ++__first
)
4579 * @brief Assign the result of a function object to each value in a
4581 * @param first A forward iterator.
4582 * @param n The length of the sequence.
4583 * @param gen A function object taking no arguments and returning
4584 * std::iterator_traits<_ForwardIterator>::value_type
4585 * @return The end of the sequence, @p first+n
4587 * Performs the assignment @c *i = @p gen() for each @c i in the range
4588 * @p [first,first+n).
4590 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4592 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4594 // concept requirements
4595 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4596 // "the type returned by a _Generator"
4597 __typeof__(__gen())>)
4599 for (; __n
> 0; --__n
, ++__first
)
4606 * @brief Copy a sequence, removing consecutive duplicate values.
4607 * @param first An input iterator.
4608 * @param last An input iterator.
4609 * @param result An output iterator.
4610 * @return An iterator designating the end of the resulting sequence.
4612 * Copies each element in the range @p [first,last) to the range
4613 * beginning at @p result, except that only the first element is copied
4614 * from groups of consecutive elements that compare equal.
4615 * unique_copy() is stable, so the relative order of elements that are
4616 * copied is unchanged.
4619 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4620 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4622 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4623 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4627 template<typename _InputIterator
, typename _OutputIterator
>
4628 inline _OutputIterator
4629 unique_copy(_InputIterator __first
, _InputIterator __last
,
4630 _OutputIterator __result
)
4632 // concept requirements
4633 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4634 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4635 typename iterator_traits
<_InputIterator
>::value_type
>)
4636 __glibcxx_function_requires(_EqualityComparableConcept
<
4637 typename iterator_traits
<_InputIterator
>::value_type
>)
4638 __glibcxx_requires_valid_range(__first
, __last
);
4640 if (__first
== __last
)
4642 return std::__unique_copy(__first
, __last
, __result
,
4643 std::__iterator_category(__first
),
4644 std::__iterator_category(__result
));
4648 * @brief Copy a sequence, removing consecutive values using a predicate.
4649 * @param first An input iterator.
4650 * @param last An input iterator.
4651 * @param result An output iterator.
4652 * @param binary_pred A binary predicate.
4653 * @return An iterator designating the end of the resulting sequence.
4655 * Copies each element in the range @p [first,last) to the range
4656 * beginning at @p result, except that only the first element is copied
4657 * from groups of consecutive elements for which @p binary_pred returns
4659 * unique_copy() is stable, so the relative order of elements that are
4660 * copied is unchanged.
4663 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4664 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4667 template<typename _InputIterator
, typename _OutputIterator
,
4668 typename _BinaryPredicate
>
4669 inline _OutputIterator
4670 unique_copy(_InputIterator __first
, _InputIterator __last
,
4671 _OutputIterator __result
,
4672 _BinaryPredicate __binary_pred
)
4674 // concept requirements -- predicates checked later
4675 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4676 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4677 typename iterator_traits
<_InputIterator
>::value_type
>)
4678 __glibcxx_requires_valid_range(__first
, __last
);
4680 if (__first
== __last
)
4682 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4683 std::__iterator_category(__first
),
4684 std::__iterator_category(__result
));
4689 * @brief Randomly shuffle the elements of a sequence.
4690 * @param first A forward iterator.
4691 * @param last A forward iterator.
4694 * Reorder the elements in the range @p [first,last) using a random
4695 * distribution, so that every possible ordering of the sequence is
4698 template<typename _RandomAccessIterator
>
4700 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4702 // concept requirements
4703 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4704 _RandomAccessIterator
>)
4705 __glibcxx_requires_valid_range(__first
, __last
);
4707 if (__first
!= __last
)
4708 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4709 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4713 * @brief Shuffle the elements of a sequence using a random number
4715 * @param first A forward iterator.
4716 * @param last A forward iterator.
4717 * @param rand The RNG functor or function.
4720 * Reorders the elements in the range @p [first,last) using @p rand to
4721 * provide a random distribution. Calling @p rand(N) for a positive
4722 * integer @p N should return a randomly chosen integer from the
4725 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4727 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4728 _RandomNumberGenerator
& __rand
)
4730 // concept requirements
4731 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4732 _RandomAccessIterator
>)
4733 __glibcxx_requires_valid_range(__first
, __last
);
4735 if (__first
== __last
)
4737 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4738 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4743 * @brief Move elements for which a predicate is true to the beginning
4745 * @param first A forward iterator.
4746 * @param last A forward iterator.
4747 * @param pred A predicate functor.
4748 * @return An iterator @p middle such that @p pred(i) is true for each
4749 * iterator @p i in the range @p [first,middle) and false for each @p i
4750 * in the range @p [middle,last).
4752 * @p pred must not modify its operand. @p partition() does not preserve
4753 * the relative ordering of elements in each group, use
4754 * @p stable_partition() if this is needed.
4756 template<typename _ForwardIterator
, typename _Predicate
>
4757 inline _ForwardIterator
4758 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4761 // concept requirements
4762 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4764 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4765 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4766 __glibcxx_requires_valid_range(__first
, __last
);
4768 return std::__partition(__first
, __last
, __pred
,
4769 std::__iterator_category(__first
));
4775 * @brief Sort the smallest elements of a sequence.
4776 * @param first An iterator.
4777 * @param middle Another iterator.
4778 * @param last Another iterator.
4781 * Sorts the smallest @p (middle-first) elements in the range
4782 * @p [first,last) and moves them to the range @p [first,middle). The
4783 * order of the remaining elements in the range @p [middle,last) is
4785 * After the sort if @p i and @j are iterators in the range
4786 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4787 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4789 template<typename _RandomAccessIterator
>
4791 partial_sort(_RandomAccessIterator __first
,
4792 _RandomAccessIterator __middle
,
4793 _RandomAccessIterator __last
)
4795 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4798 // concept requirements
4799 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4800 _RandomAccessIterator
>)
4801 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4802 __glibcxx_requires_valid_range(__first
, __middle
);
4803 __glibcxx_requires_valid_range(__middle
, __last
);
4805 std::__heap_select(__first
, __middle
, __last
);
4806 std::sort_heap(__first
, __middle
);
4810 * @brief Sort the smallest elements of a sequence using a predicate
4812 * @param first An iterator.
4813 * @param middle Another iterator.
4814 * @param last Another iterator.
4815 * @param comp A comparison functor.
4818 * Sorts the smallest @p (middle-first) elements in the range
4819 * @p [first,last) and moves them to the range @p [first,middle). The
4820 * order of the remaining elements in the range @p [middle,last) is
4822 * After the sort if @p i and @j are iterators in the range
4823 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4824 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4827 template<typename _RandomAccessIterator
, typename _Compare
>
4829 partial_sort(_RandomAccessIterator __first
,
4830 _RandomAccessIterator __middle
,
4831 _RandomAccessIterator __last
,
4834 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4837 // concept requirements
4838 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4839 _RandomAccessIterator
>)
4840 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4841 _ValueType
, _ValueType
>)
4842 __glibcxx_requires_valid_range(__first
, __middle
);
4843 __glibcxx_requires_valid_range(__middle
, __last
);
4845 std::__heap_select(__first
, __middle
, __last
, __comp
);
4846 std::sort_heap(__first
, __middle
, __comp
);
4850 * @brief Sort a sequence just enough to find a particular position.
4851 * @param first An iterator.
4852 * @param nth Another iterator.
4853 * @param last Another iterator.
4856 * Rearranges the elements in the range @p [first,last) so that @p *nth
4857 * is the same element that would have been in that position had the
4858 * whole sequence been sorted.
4859 * whole sequence been sorted. The elements either side of @p *nth are
4860 * not completely sorted, but for any iterator @i in the range
4861 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4862 * holds that @p *j<*i is false.
4864 template<typename _RandomAccessIterator
>
4866 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4867 _RandomAccessIterator __last
)
4869 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4872 // concept requirements
4873 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4874 _RandomAccessIterator
>)
4875 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4876 __glibcxx_requires_valid_range(__first
, __nth
);
4877 __glibcxx_requires_valid_range(__nth
, __last
);
4879 if (__first
== __last
|| __nth
== __last
)
4882 std::__introselect(__first
, __nth
, __last
,
4883 std::__lg(__last
- __first
) * 2);
4887 * @brief Sort a sequence just enough to find a particular position
4888 * using a predicate for comparison.
4889 * @param first An iterator.
4890 * @param nth Another iterator.
4891 * @param last Another iterator.
4892 * @param comp A comparison functor.
4895 * Rearranges the elements in the range @p [first,last) so that @p *nth
4896 * is the same element that would have been in that position had the
4897 * whole sequence been sorted. The elements either side of @p *nth are
4898 * not completely sorted, but for any iterator @i in the range
4899 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4900 * holds that @p comp(*j,*i) is false.
4902 template<typename _RandomAccessIterator
, typename _Compare
>
4904 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4905 _RandomAccessIterator __last
, _Compare __comp
)
4907 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4910 // concept requirements
4911 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4912 _RandomAccessIterator
>)
4913 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4914 _ValueType
, _ValueType
>)
4915 __glibcxx_requires_valid_range(__first
, __nth
);
4916 __glibcxx_requires_valid_range(__nth
, __last
);
4918 if (__first
== __last
|| __nth
== __last
)
4921 std::__introselect(__first
, __nth
, __last
,
4922 std::__lg(__last
- __first
) * 2, __comp
);
4927 * @brief Sort the elements of a sequence.
4928 * @param first An iterator.
4929 * @param last Another iterator.
4932 * Sorts the elements in the range @p [first,last) in ascending order,
4933 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4934 * @p [first,last-1).
4936 * The relative ordering of equivalent elements is not preserved, use
4937 * @p stable_sort() if this is needed.
4939 template<typename _RandomAccessIterator
>
4941 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4943 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4946 // concept requirements
4947 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4948 _RandomAccessIterator
>)
4949 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4950 __glibcxx_requires_valid_range(__first
, __last
);
4952 if (__first
!= __last
)
4954 std::__introsort_loop(__first
, __last
,
4955 std::__lg(__last
- __first
) * 2);
4956 std::__final_insertion_sort(__first
, __last
);
4961 * @brief Sort the elements of a sequence using a predicate for comparison.
4962 * @param first An iterator.
4963 * @param last Another iterator.
4964 * @param comp A comparison functor.
4967 * Sorts the elements in the range @p [first,last) in ascending order,
4968 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4969 * range @p [first,last-1).
4971 * The relative ordering of equivalent elements is not preserved, use
4972 * @p stable_sort() if this is needed.
4974 template<typename _RandomAccessIterator
, typename _Compare
>
4976 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4979 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4982 // concept requirements
4983 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4984 _RandomAccessIterator
>)
4985 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
4987 __glibcxx_requires_valid_range(__first
, __last
);
4989 if (__first
!= __last
)
4991 std::__introsort_loop(__first
, __last
,
4992 std::__lg(__last
- __first
) * 2, __comp
);
4993 std::__final_insertion_sort(__first
, __last
, __comp
);
4998 * @brief Merges two sorted ranges.
4999 * @param first1 An iterator.
5000 * @param first2 Another iterator.
5001 * @param last1 Another iterator.
5002 * @param last2 Another iterator.
5003 * @param result An iterator pointing to the end of the merged range.
5004 * @return An iterator pointing to the first element "not less
5007 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5008 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5009 * must be sorted, and the output range must not overlap with either of
5010 * the input ranges. The sort is @e stable, that is, for equivalent
5011 * elements in the two ranges, elements from the first range will always
5012 * come before elements from the second.
5014 template<typename _InputIterator1
, typename _InputIterator2
,
5015 typename _OutputIterator
>
5017 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5018 _InputIterator2 __first2
, _InputIterator2 __last2
,
5019 _OutputIterator __result
)
5021 typedef typename iterator_traits
<_InputIterator1
>::value_type
5023 typedef typename iterator_traits
<_InputIterator2
>::value_type
5026 // concept requirements
5027 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5028 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5029 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5031 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5033 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5034 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5035 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5037 while (__first1
!= __last1
&& __first2
!= __last2
)
5039 if (*__first2
< *__first1
)
5041 *__result
= *__first2
;
5046 *__result
= *__first1
;
5051 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5056 * @brief Merges two sorted ranges.
5057 * @param first1 An iterator.
5058 * @param first2 Another iterator.
5059 * @param last1 Another iterator.
5060 * @param last2 Another iterator.
5061 * @param result An iterator pointing to the end of the merged range.
5062 * @param comp A functor to use for comparisons.
5063 * @return An iterator pointing to the first element "not less
5066 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5067 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5068 * must be sorted, and the output range must not overlap with either of
5069 * the input ranges. The sort is @e stable, that is, for equivalent
5070 * elements in the two ranges, elements from the first range will always
5071 * come before elements from the second.
5073 * The comparison function should have the same effects on ordering as
5074 * the function used for the initial sort.
5076 template<typename _InputIterator1
, typename _InputIterator2
,
5077 typename _OutputIterator
, typename _Compare
>
5079 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5080 _InputIterator2 __first2
, _InputIterator2 __last2
,
5081 _OutputIterator __result
, _Compare __comp
)
5083 typedef typename iterator_traits
<_InputIterator1
>::value_type
5085 typedef typename iterator_traits
<_InputIterator2
>::value_type
5088 // concept requirements
5089 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5090 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5091 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5093 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5095 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5096 _ValueType2
, _ValueType1
>)
5097 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5098 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5100 while (__first1
!= __last1
&& __first2
!= __last2
)
5102 if (__comp(*__first2
, *__first1
))
5104 *__result
= *__first2
;
5109 *__result
= *__first1
;
5114 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5120 * @brief Sort the elements of a sequence, preserving the relative order
5121 * of equivalent elements.
5122 * @param first An iterator.
5123 * @param last Another iterator.
5126 * Sorts the elements in the range @p [first,last) in ascending order,
5127 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5128 * @p [first,last-1).
5130 * The relative ordering of equivalent elements is preserved, so any two
5131 * elements @p x and @p y in the range @p [first,last) such that
5132 * @p x<y is false and @p y<x is false will have the same relative
5133 * ordering after calling @p stable_sort().
5135 template<typename _RandomAccessIterator
>
5137 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5139 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5141 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5144 // concept requirements
5145 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5146 _RandomAccessIterator
>)
5147 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5148 __glibcxx_requires_valid_range(__first
, __last
);
5150 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5152 if (__buf
.begin() == 0)
5153 std::__inplace_stable_sort(__first
, __last
);
5155 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5156 _DistanceType(__buf
.size()));
5160 * @brief Sort the elements of a sequence using a predicate for comparison,
5161 * preserving the relative order of equivalent elements.
5162 * @param first An iterator.
5163 * @param last Another iterator.
5164 * @param comp A comparison functor.
5167 * Sorts the elements in the range @p [first,last) in ascending order,
5168 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5169 * range @p [first,last-1).
5171 * The relative ordering of equivalent elements is preserved, so any two
5172 * elements @p x and @p y in the range @p [first,last) such that
5173 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5174 * relative ordering after calling @p stable_sort().
5176 template<typename _RandomAccessIterator
, typename _Compare
>
5178 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5181 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5183 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5186 // concept requirements
5187 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5188 _RandomAccessIterator
>)
5189 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5192 __glibcxx_requires_valid_range(__first
, __last
);
5194 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5196 if (__buf
.begin() == 0)
5197 std::__inplace_stable_sort(__first
, __last
, __comp
);
5199 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5200 _DistanceType(__buf
.size()), __comp
);
5205 * @brief Return the union of two sorted ranges.
5206 * @param first1 Start of first range.
5207 * @param last1 End of first range.
5208 * @param first2 Start of second range.
5209 * @param last2 End of second range.
5210 * @return End of the output range.
5211 * @ingroup setoperations
5213 * This operation iterates over both ranges, copying elements present in
5214 * each range in order to the output range. Iterators increment for each
5215 * range. When the current element of one range is less than the other,
5216 * that element is copied and the iterator advanced. If an element is
5217 * contained in both ranges, the element from the first range is copied and
5218 * both ranges advance. The output range may not overlap either input
5221 template<typename _InputIterator1
, typename _InputIterator2
,
5222 typename _OutputIterator
>
5224 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5225 _InputIterator2 __first2
, _InputIterator2 __last2
,
5226 _OutputIterator __result
)
5228 typedef typename iterator_traits
<_InputIterator1
>::value_type
5230 typedef typename iterator_traits
<_InputIterator2
>::value_type
5233 // concept requirements
5234 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5235 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5236 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5238 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5240 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5241 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5242 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5243 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5245 while (__first1
!= __last1
&& __first2
!= __last2
)
5247 if (*__first1
< *__first2
)
5249 *__result
= *__first1
;
5252 else if (*__first2
< *__first1
)
5254 *__result
= *__first2
;
5259 *__result
= *__first1
;
5265 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5270 * @brief Return the union of two sorted ranges using a comparison functor.
5271 * @param first1 Start of first range.
5272 * @param last1 End of first range.
5273 * @param first2 Start of second range.
5274 * @param last2 End of second range.
5275 * @param comp The comparison functor.
5276 * @return End of the output range.
5277 * @ingroup setoperations
5279 * This operation iterates over both ranges, copying elements present in
5280 * each range in order to the output range. Iterators increment for each
5281 * range. When the current element of one range is less than the other
5282 * according to @a comp, that element is copied and the iterator advanced.
5283 * If an equivalent element according to @a comp is contained in both
5284 * ranges, the element from the first range is copied and both ranges
5285 * advance. The output range may not overlap either input range.
5287 template<typename _InputIterator1
, typename _InputIterator2
,
5288 typename _OutputIterator
, typename _Compare
>
5290 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5291 _InputIterator2 __first2
, _InputIterator2 __last2
,
5292 _OutputIterator __result
, _Compare __comp
)
5294 typedef typename iterator_traits
<_InputIterator1
>::value_type
5296 typedef typename iterator_traits
<_InputIterator2
>::value_type
5299 // concept requirements
5300 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5301 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5302 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5304 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5306 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5307 _ValueType1
, _ValueType2
>)
5308 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5309 _ValueType2
, _ValueType1
>)
5310 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5311 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5313 while (__first1
!= __last1
&& __first2
!= __last2
)
5315 if (__comp(*__first1
, *__first2
))
5317 *__result
= *__first1
;
5320 else if (__comp(*__first2
, *__first1
))
5322 *__result
= *__first2
;
5327 *__result
= *__first1
;
5333 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5338 * @brief Return the intersection of two sorted ranges.
5339 * @param first1 Start of first range.
5340 * @param last1 End of first range.
5341 * @param first2 Start of second range.
5342 * @param last2 End of second range.
5343 * @return End of the output range.
5344 * @ingroup setoperations
5346 * This operation iterates over both ranges, copying elements present in
5347 * both ranges in order to the output range. Iterators increment for each
5348 * range. When the current element of one range is less than the other,
5349 * that iterator advances. If an element is contained in both ranges, the
5350 * element from the first range is copied and both ranges advance. The
5351 * output range may not overlap either input range.
5353 template<typename _InputIterator1
, typename _InputIterator2
,
5354 typename _OutputIterator
>
5356 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5357 _InputIterator2 __first2
, _InputIterator2 __last2
,
5358 _OutputIterator __result
)
5360 typedef typename iterator_traits
<_InputIterator1
>::value_type
5362 typedef typename iterator_traits
<_InputIterator2
>::value_type
5365 // concept requirements
5366 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5367 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5368 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5370 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5371 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5372 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5373 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5375 while (__first1
!= __last1
&& __first2
!= __last2
)
5376 if (*__first1
< *__first2
)
5378 else if (*__first2
< *__first1
)
5382 *__result
= *__first1
;
5391 * @brief Return the intersection of two sorted ranges using comparison
5393 * @param first1 Start of first range.
5394 * @param last1 End of first range.
5395 * @param first2 Start of second range.
5396 * @param last2 End of second range.
5397 * @param comp The comparison functor.
5398 * @return End of the output range.
5399 * @ingroup setoperations
5401 * This operation iterates over both ranges, copying elements present in
5402 * both ranges in order to the output range. Iterators increment for each
5403 * range. When the current element of one range is less than the other
5404 * according to @a comp, that iterator advances. If an element is
5405 * contained in both ranges according to @a comp, the element from the
5406 * first range is copied and both ranges advance. The output range may not
5407 * overlap either input range.
5409 template<typename _InputIterator1
, typename _InputIterator2
,
5410 typename _OutputIterator
, typename _Compare
>
5412 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5413 _InputIterator2 __first2
, _InputIterator2 __last2
,
5414 _OutputIterator __result
, _Compare __comp
)
5416 typedef typename iterator_traits
<_InputIterator1
>::value_type
5418 typedef typename iterator_traits
<_InputIterator2
>::value_type
5421 // concept requirements
5422 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5423 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5424 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5426 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5427 _ValueType1
, _ValueType2
>)
5428 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5429 _ValueType2
, _ValueType1
>)
5430 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5431 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5433 while (__first1
!= __last1
&& __first2
!= __last2
)
5434 if (__comp(*__first1
, *__first2
))
5436 else if (__comp(*__first2
, *__first1
))
5440 *__result
= *__first1
;
5449 * @brief Return the difference of two sorted ranges.
5450 * @param first1 Start of first range.
5451 * @param last1 End of first range.
5452 * @param first2 Start of second range.
5453 * @param last2 End of second range.
5454 * @return End of the output range.
5455 * @ingroup setoperations
5457 * This operation iterates over both ranges, copying elements present in
5458 * the first range but not the second in order to the output range.
5459 * Iterators increment for each range. When the current element of the
5460 * first range is less than the second, that element is copied and the
5461 * iterator advances. If the current element of the second range is less,
5462 * the iterator advances, but no element is copied. If an element is
5463 * contained in both ranges, no elements are copied and both ranges
5464 * advance. The output range may not overlap either input range.
5466 template<typename _InputIterator1
, typename _InputIterator2
,
5467 typename _OutputIterator
>
5469 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5470 _InputIterator2 __first2
, _InputIterator2 __last2
,
5471 _OutputIterator __result
)
5473 typedef typename iterator_traits
<_InputIterator1
>::value_type
5475 typedef typename iterator_traits
<_InputIterator2
>::value_type
5478 // concept requirements
5479 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5480 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5481 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5483 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5484 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5485 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5486 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5488 while (__first1
!= __last1
&& __first2
!= __last2
)
5489 if (*__first1
< *__first2
)
5491 *__result
= *__first1
;
5495 else if (*__first2
< *__first1
)
5502 return std::copy(__first1
, __last1
, __result
);
5506 * @brief Return the difference of two sorted ranges using comparison
5508 * @param first1 Start of first range.
5509 * @param last1 End of first range.
5510 * @param first2 Start of second range.
5511 * @param last2 End of second range.
5512 * @param comp The comparison functor.
5513 * @return End of the output range.
5514 * @ingroup setoperations
5516 * This operation iterates over both ranges, copying elements present in
5517 * the first range but not the second in order to the output range.
5518 * Iterators increment for each range. When the current element of the
5519 * first range is less than the second according to @a comp, that element
5520 * is copied and the iterator advances. If the current element of the
5521 * second range is less, no element is copied and the iterator advances.
5522 * If an element is contained in both ranges according to @a comp, no
5523 * elements are copied and both ranges advance. The output range may not
5524 * overlap either input range.
5526 template<typename _InputIterator1
, typename _InputIterator2
,
5527 typename _OutputIterator
, typename _Compare
>
5529 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5530 _InputIterator2 __first2
, _InputIterator2 __last2
,
5531 _OutputIterator __result
, _Compare __comp
)
5533 typedef typename iterator_traits
<_InputIterator1
>::value_type
5535 typedef typename iterator_traits
<_InputIterator2
>::value_type
5538 // concept requirements
5539 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5540 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5541 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5543 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5544 _ValueType1
, _ValueType2
>)
5545 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5546 _ValueType2
, _ValueType1
>)
5547 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5548 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5550 while (__first1
!= __last1
&& __first2
!= __last2
)
5551 if (__comp(*__first1
, *__first2
))
5553 *__result
= *__first1
;
5557 else if (__comp(*__first2
, *__first1
))
5564 return std::copy(__first1
, __last1
, __result
);
5568 * @brief Return the symmetric difference of two sorted ranges.
5569 * @param first1 Start of first range.
5570 * @param last1 End of first range.
5571 * @param first2 Start of second range.
5572 * @param last2 End of second range.
5573 * @return End of the output range.
5574 * @ingroup setoperations
5576 * This operation iterates over both ranges, copying elements present in
5577 * one range but not the other in order to the output range. Iterators
5578 * increment for each range. When the current element of one range is less
5579 * than the other, that element is copied and the iterator advances. If an
5580 * element is contained in both ranges, no elements are copied and both
5581 * ranges advance. The output range may not overlap either input range.
5583 template<typename _InputIterator1
, typename _InputIterator2
,
5584 typename _OutputIterator
>
5586 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5587 _InputIterator2 __first2
, _InputIterator2 __last2
,
5588 _OutputIterator __result
)
5590 typedef typename iterator_traits
<_InputIterator1
>::value_type
5592 typedef typename iterator_traits
<_InputIterator2
>::value_type
5595 // concept requirements
5596 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5597 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5598 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5600 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5602 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5603 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5604 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5605 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5607 while (__first1
!= __last1
&& __first2
!= __last2
)
5608 if (*__first1
< *__first2
)
5610 *__result
= *__first1
;
5614 else if (*__first2
< *__first1
)
5616 *__result
= *__first2
;
5625 return std::copy(__first2
, __last2
, std::copy(__first1
,
5626 __last1
, __result
));
5630 * @brief Return the symmetric difference of two sorted ranges using
5631 * comparison functor.
5632 * @param first1 Start of first range.
5633 * @param last1 End of first range.
5634 * @param first2 Start of second range.
5635 * @param last2 End of second range.
5636 * @param comp The comparison functor.
5637 * @return End of the output range.
5638 * @ingroup setoperations
5640 * This operation iterates over both ranges, copying elements present in
5641 * one range but not the other in order to the output range. Iterators
5642 * increment for each range. When the current element of one range is less
5643 * than the other according to @a comp, that element is copied and the
5644 * iterator advances. If an element is contained in both ranges according
5645 * to @a comp, no elements are copied and both ranges advance. The output
5646 * range may not overlap either input range.
5648 template<typename _InputIterator1
, typename _InputIterator2
,
5649 typename _OutputIterator
, typename _Compare
>
5651 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5652 _InputIterator2 __first2
, _InputIterator2 __last2
,
5653 _OutputIterator __result
,
5656 typedef typename iterator_traits
<_InputIterator1
>::value_type
5658 typedef typename iterator_traits
<_InputIterator2
>::value_type
5661 // concept requirements
5662 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5663 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5664 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5666 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5668 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5669 _ValueType1
, _ValueType2
>)
5670 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5671 _ValueType2
, _ValueType1
>)
5672 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5673 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5675 while (__first1
!= __last1
&& __first2
!= __last2
)
5676 if (__comp(*__first1
, *__first2
))
5678 *__result
= *__first1
;
5682 else if (__comp(*__first2
, *__first1
))
5684 *__result
= *__first2
;
5693 return std::copy(__first2
, __last2
,
5694 std::copy(__first1
, __last1
, __result
));
5699 * @brief Return the minimum element in a range.
5700 * @param first Start of range.
5701 * @param last End of range.
5702 * @return Iterator referencing the first instance of the smallest value.
5704 template<typename _ForwardIterator
>
5706 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5708 // concept requirements
5709 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5710 __glibcxx_function_requires(_LessThanComparableConcept
<
5711 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5712 __glibcxx_requires_valid_range(__first
, __last
);
5714 if (__first
== __last
)
5716 _ForwardIterator __result
= __first
;
5717 while (++__first
!= __last
)
5718 if (*__first
< *__result
)
5724 * @brief Return the minimum element in a range using comparison functor.
5725 * @param first Start of range.
5726 * @param last End of range.
5727 * @param comp Comparison functor.
5728 * @return Iterator referencing the first instance of the smallest value
5729 * according to comp.
5731 template<typename _ForwardIterator
, typename _Compare
>
5733 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5736 // concept requirements
5737 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5738 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5739 typename iterator_traits
<_ForwardIterator
>::value_type
,
5740 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5741 __glibcxx_requires_valid_range(__first
, __last
);
5743 if (__first
== __last
)
5745 _ForwardIterator __result
= __first
;
5746 while (++__first
!= __last
)
5747 if (__comp(*__first
, *__result
))
5753 * @brief Return the maximum element in a range.
5754 * @param first Start of range.
5755 * @param last End of range.
5756 * @return Iterator referencing the first instance of the largest value.
5758 template<typename _ForwardIterator
>
5760 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5762 // concept requirements
5763 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5764 __glibcxx_function_requires(_LessThanComparableConcept
<
5765 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5766 __glibcxx_requires_valid_range(__first
, __last
);
5768 if (__first
== __last
)
5770 _ForwardIterator __result
= __first
;
5771 while (++__first
!= __last
)
5772 if (*__result
< *__first
)
5778 * @brief Return the maximum element in a range using comparison functor.
5779 * @param first Start of range.
5780 * @param last End of range.
5781 * @param comp Comparison functor.
5782 * @return Iterator referencing the first instance of the largest value
5783 * according to comp.
5785 template<typename _ForwardIterator
, typename _Compare
>
5787 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5790 // concept requirements
5791 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5792 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5793 typename iterator_traits
<_ForwardIterator
>::value_type
,
5794 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5795 __glibcxx_requires_valid_range(__first
, __last
);
5797 if (__first
== __last
) return __first
;
5798 _ForwardIterator __result
= __first
;
5799 while (++__first
!= __last
)
5800 if (__comp(*__result
, *__first
))
5805 _GLIBCXX_END_NESTED_NAMESPACE
5807 #endif /* _STL_ALGO_H */