1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <cstdlib> // for rand
66 #include <bits/stl_heap.h>
67 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
68 #include <bits/algorithmfwd.h>
69 #include <debug/debug.h>
71 // See concept_check.h for the __glibcxx_*_requires macros.
73 _GLIBCXX_BEGIN_NAMESPACE(std
)
76 * @brief Find the median of three values.
80 * @return One of @p a, @p b or @p c.
82 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
83 * then the value returned will be @c m.
84 * This is an SGI extension.
85 * @ingroup SGIextensions
87 template<typename _Tp
>
89 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
91 // concept requirements
92 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
109 * @brief Find the median of three values using a predicate for comparison.
113 * @param comp A binary predicate.
114 * @return One of @p a, @p b or @p c.
116 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
117 * and @p comp(m,n) are both true then the value returned will be @c m.
118 * This is an SGI extension.
119 * @ingroup SGIextensions
121 template<typename _Tp
, typename _Compare
>
123 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
125 // concept requirements
126 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
128 if (__comp(__a
, __b
))
129 if (__comp(__b
, __c
))
131 else if (__comp(__a
, __c
))
135 else if (__comp(__a
, __c
))
137 else if (__comp(__b
, __c
))
147 * This is an overload used by find() for the Input Iterator case.
150 template<typename _InputIterator
, typename _Tp
>
151 inline _InputIterator
152 __find(_InputIterator __first
, _InputIterator __last
,
153 const _Tp
& __val
, input_iterator_tag
)
155 while (__first
!= __last
&& !(*__first
== __val
))
162 * This is an overload used by find_if() for the Input Iterator case.
165 template<typename _InputIterator
, typename _Predicate
>
166 inline _InputIterator
167 __find_if(_InputIterator __first
, _InputIterator __last
,
168 _Predicate __pred
, input_iterator_tag
)
170 while (__first
!= __last
&& !bool(__pred(*__first
)))
177 * This is an overload used by find() for the RAI case.
180 template<typename _RandomAccessIterator
, typename _Tp
>
181 _RandomAccessIterator
182 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
183 const _Tp
& __val
, random_access_iterator_tag
)
185 typename iterator_traits
<_RandomAccessIterator
>::difference_type
186 __trip_count
= (__last
- __first
) >> 2;
188 for (; __trip_count
> 0; --__trip_count
)
190 if (*__first
== __val
)
194 if (*__first
== __val
)
198 if (*__first
== __val
)
202 if (*__first
== __val
)
207 switch (__last
- __first
)
210 if (*__first
== __val
)
214 if (*__first
== __val
)
218 if (*__first
== __val
)
229 * This is an overload used by find_if() for the RAI case.
232 template<typename _RandomAccessIterator
, typename _Predicate
>
233 _RandomAccessIterator
234 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
235 _Predicate __pred
, random_access_iterator_tag
)
237 typename iterator_traits
<_RandomAccessIterator
>::difference_type
238 __trip_count
= (__last
- __first
) >> 2;
240 for (; __trip_count
> 0; --__trip_count
)
242 if (__pred(*__first
))
246 if (__pred(*__first
))
250 if (__pred(*__first
))
254 if (__pred(*__first
))
259 switch (__last
- __first
)
262 if (__pred(*__first
))
266 if (__pred(*__first
))
270 if (__pred(*__first
))
281 // set_symmetric_difference
294 * This is an uglified
295 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
296 * overloaded for forward iterators.
299 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
301 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
302 _Integer __count
, const _Tp
& __val
,
303 std::forward_iterator_tag
)
305 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __val
);
306 while (__first
!= __last
)
308 typename iterator_traits
<_ForwardIterator
>::difference_type
310 _ForwardIterator __i
= __first
;
312 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
321 __first
= _GLIBCXX_STD_P::find(++__i
, __last
, __val
);
328 * This is an uglified
329 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
330 * overloaded for random access iterators.
333 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
335 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
336 _Integer __count
, const _Tp
& __val
,
337 std::random_access_iterator_tag
)
340 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
343 _DistanceType __tailSize
= __last
- __first
;
344 const _DistanceType __pattSize
= __count
;
346 if (__tailSize
< __pattSize
)
349 const _DistanceType __skipOffset
= __pattSize
- 1;
350 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
351 __tailSize
-= __pattSize
;
353 while (1) // the main loop...
355 // __lookAhead here is always pointing to the last element of next
357 while (!(*__lookAhead
== __val
)) // the skip loop...
359 if (__tailSize
< __pattSize
)
360 return __last
; // Failure
361 __lookAhead
+= __pattSize
;
362 __tailSize
-= __pattSize
;
364 _DistanceType __remainder
= __skipOffset
;
365 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
366 *__backTrack
== __val
; --__backTrack
)
368 if (--__remainder
== 0)
369 return (__lookAhead
- __skipOffset
); // Success
371 if (__remainder
> __tailSize
)
372 return __last
; // Failure
373 __lookAhead
+= __remainder
;
374 __tailSize
-= __remainder
;
382 * This is an uglified
383 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
385 * overloaded for forward iterators.
388 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
389 typename _BinaryPredicate
>
391 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
392 _Integer __count
, const _Tp
& __val
,
393 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
395 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
398 while (__first
!= __last
)
400 typename iterator_traits
<_ForwardIterator
>::difference_type
402 _ForwardIterator __i
= __first
;
404 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
414 while (__first
!= __last
415 && !bool(__binary_pred(*__first
, __val
)))
423 * This is an uglified
424 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
426 * overloaded for random access iterators.
429 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
430 typename _BinaryPredicate
>
432 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
433 _Integer __count
, const _Tp
& __val
,
434 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
437 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
440 _DistanceType __tailSize
= __last
- __first
;
441 const _DistanceType __pattSize
= __count
;
443 if (__tailSize
< __pattSize
)
446 const _DistanceType __skipOffset
= __pattSize
- 1;
447 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
448 __tailSize
-= __pattSize
;
450 while (1) // the main loop...
452 // __lookAhead here is always pointing to the last element of next
454 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
456 if (__tailSize
< __pattSize
)
457 return __last
; // Failure
458 __lookAhead
+= __pattSize
;
459 __tailSize
-= __pattSize
;
461 _DistanceType __remainder
= __skipOffset
;
462 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
463 __binary_pred(*__backTrack
, __val
); --__backTrack
)
465 if (--__remainder
== 0)
466 return (__lookAhead
- __skipOffset
); // Success
468 if (__remainder
> __tailSize
)
469 return __last
; // Failure
470 __lookAhead
+= __remainder
;
471 __tailSize
-= __remainder
;
475 // find_end for forward iterators.
476 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
478 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
479 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
480 forward_iterator_tag
, forward_iterator_tag
)
482 if (__first2
== __last2
)
486 _ForwardIterator1 __result
= __last1
;
489 _ForwardIterator1 __new_result
490 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
, __last2
);
491 if (__new_result
== __last1
)
495 __result
= __new_result
;
496 __first1
= __new_result
;
503 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
504 typename _BinaryPredicate
>
506 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
507 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
508 forward_iterator_tag
, forward_iterator_tag
,
509 _BinaryPredicate __comp
)
511 if (__first2
== __last2
)
515 _ForwardIterator1 __result
= __last1
;
518 _ForwardIterator1 __new_result
519 = _GLIBCXX_STD_P::search(__first1
, __last1
, __first2
,
521 if (__new_result
== __last1
)
525 __result
= __new_result
;
526 __first1
= __new_result
;
533 // find_end for bidirectional iterators (much faster).
534 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
535 _BidirectionalIterator1
536 __find_end(_BidirectionalIterator1 __first1
,
537 _BidirectionalIterator1 __last1
,
538 _BidirectionalIterator2 __first2
,
539 _BidirectionalIterator2 __last2
,
540 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
542 // concept requirements
543 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
544 _BidirectionalIterator1
>)
545 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
546 _BidirectionalIterator2
>)
548 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
549 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
551 _RevIterator1
__rlast1(__first1
);
552 _RevIterator2
__rlast2(__first2
);
553 _RevIterator1 __rresult
= _GLIBCXX_STD_P::search(_RevIterator1(__last1
),
555 _RevIterator2(__last2
),
558 if (__rresult
== __rlast1
)
562 _BidirectionalIterator1 __result
= __rresult
.base();
563 std::advance(__result
, -std::distance(__first2
, __last2
));
568 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
569 typename _BinaryPredicate
>
570 _BidirectionalIterator1
571 __find_end(_BidirectionalIterator1 __first1
,
572 _BidirectionalIterator1 __last1
,
573 _BidirectionalIterator2 __first2
,
574 _BidirectionalIterator2 __last2
,
575 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
576 _BinaryPredicate __comp
)
578 // concept requirements
579 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
580 _BidirectionalIterator1
>)
581 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
582 _BidirectionalIterator2
>)
584 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
585 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
587 _RevIterator1
__rlast1(__first1
);
588 _RevIterator2
__rlast2(__first2
);
589 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
590 _RevIterator2(__last2
), __rlast2
,
593 if (__rresult
== __rlast1
)
597 _BidirectionalIterator1 __result
= __rresult
.base();
598 std::advance(__result
, -std::distance(__first2
, __last2
));
604 * @brief Find last matching subsequence in a sequence.
605 * @param first1 Start of range to search.
606 * @param last1 End of range to search.
607 * @param first2 Start of sequence to match.
608 * @param last2 End of sequence to match.
609 * @return The last iterator @c i in the range
610 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
611 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
612 * such iterator exists.
614 * Searches the range @p [first1,last1) for a sub-sequence that compares
615 * equal value-by-value with the sequence given by @p [first2,last2) and
616 * returns an iterator to the first element of the sub-sequence, or
617 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
618 * last such subsequence contained in [first,last1).
620 * Because the sub-sequence must lie completely within the range
621 * @p [first1,last1) it must start at a position less than
622 * @p last1-(last2-first2) where @p last2-first2 is the length of the
624 * This means that the returned iterator @c i will be in the range
625 * @p [first1,last1-(last2-first2))
627 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
628 inline _ForwardIterator1
629 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
630 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
632 // concept requirements
633 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
634 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
635 __glibcxx_function_requires(_EqualOpConcept
<
636 typename iterator_traits
<_ForwardIterator1
>::value_type
,
637 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
638 __glibcxx_requires_valid_range(__first1
, __last1
);
639 __glibcxx_requires_valid_range(__first2
, __last2
);
641 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
642 std::__iterator_category(__first1
),
643 std::__iterator_category(__first2
));
647 * @brief Find last matching subsequence in a sequence using a predicate.
648 * @param first1 Start of range to search.
649 * @param last1 End of range to search.
650 * @param first2 Start of sequence to match.
651 * @param last2 End of sequence to match.
652 * @param comp The predicate to use.
653 * @return The last iterator @c i in the range
654 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
655 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
656 * @p last1 if no such iterator exists.
658 * Searches the range @p [first1,last1) for a sub-sequence that compares
659 * equal value-by-value with the sequence given by @p [first2,last2) using
660 * comp as a predicate and returns an iterator to the first element of the
661 * sub-sequence, or @p last1 if the sub-sequence is not found. The
662 * sub-sequence will be the last such subsequence contained in
665 * Because the sub-sequence must lie completely within the range
666 * @p [first1,last1) it must start at a position less than
667 * @p last1-(last2-first2) where @p last2-first2 is the length of the
669 * This means that the returned iterator @c i will be in the range
670 * @p [first1,last1-(last2-first2))
672 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
673 typename _BinaryPredicate
>
674 inline _ForwardIterator1
675 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
676 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
677 _BinaryPredicate __comp
)
679 // concept requirements
680 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
681 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
682 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
683 typename iterator_traits
<_ForwardIterator1
>::value_type
,
684 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
685 __glibcxx_requires_valid_range(__first1
, __last1
);
686 __glibcxx_requires_valid_range(__first2
, __last2
);
688 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
689 std::__iterator_category(__first1
),
690 std::__iterator_category(__first2
),
696 * @brief Copy a sequence, removing elements of a given value.
697 * @param first An input iterator.
698 * @param last An input iterator.
699 * @param result An output iterator.
700 * @param value The value to be removed.
701 * @return An iterator designating the end of the resulting sequence.
703 * Copies each element in the range @p [first,last) not equal to @p value
704 * to the range beginning at @p result.
705 * remove_copy() is stable, so the relative order of elements that are
706 * copied is unchanged.
708 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
710 remove_copy(_InputIterator __first
, _InputIterator __last
,
711 _OutputIterator __result
, const _Tp
& __value
)
713 // concept requirements
714 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
715 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
716 typename iterator_traits
<_InputIterator
>::value_type
>)
717 __glibcxx_function_requires(_EqualOpConcept
<
718 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
719 __glibcxx_requires_valid_range(__first
, __last
);
721 for (; __first
!= __last
; ++__first
)
722 if (!(*__first
== __value
))
724 *__result
= *__first
;
731 * @brief Copy a sequence, removing elements for which a predicate is true.
732 * @param first An input iterator.
733 * @param last An input iterator.
734 * @param result An output iterator.
735 * @param pred A predicate.
736 * @return An iterator designating the end of the resulting sequence.
738 * Copies each element in the range @p [first,last) for which
739 * @p pred returns true to the range beginning at @p result.
741 * remove_copy_if() is stable, so the relative order of elements that are
742 * copied is unchanged.
744 template<typename _InputIterator
, typename _OutputIterator
,
747 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
748 _OutputIterator __result
, _Predicate __pred
)
750 // concept requirements
751 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
752 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
753 typename iterator_traits
<_InputIterator
>::value_type
>)
754 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
755 typename iterator_traits
<_InputIterator
>::value_type
>)
756 __glibcxx_requires_valid_range(__first
, __last
);
758 for (; __first
!= __last
; ++__first
)
759 if (!bool(__pred(*__first
)))
761 *__result
= *__first
;
768 * @brief Remove elements from a sequence.
769 * @param first An input iterator.
770 * @param last An input iterator.
771 * @param value The value to be removed.
772 * @return An iterator designating the end of the resulting sequence.
774 * All elements equal to @p value are removed from the range
777 * remove() is stable, so the relative order of elements that are
778 * not removed is unchanged.
780 * Elements between the end of the resulting sequence and @p last
781 * are still present, but their value is unspecified.
783 template<typename _ForwardIterator
, typename _Tp
>
785 remove(_ForwardIterator __first
, _ForwardIterator __last
,
788 // concept requirements
789 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
791 __glibcxx_function_requires(_EqualOpConcept
<
792 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
793 __glibcxx_requires_valid_range(__first
, __last
);
795 __first
= _GLIBCXX_STD_P::find(__first
, __last
, __value
);
796 if(__first
== __last
)
798 _ForwardIterator __result
= __first
;
800 for(; __first
!= __last
; ++__first
)
801 if(!(*__first
== __value
))
803 *__result
= _GLIBCXX_MOVE(*__first
);
810 * @brief Remove elements from a sequence using a predicate.
811 * @param first A forward iterator.
812 * @param last A forward iterator.
813 * @param pred A predicate.
814 * @return An iterator designating the end of the resulting sequence.
816 * All elements for which @p pred returns true are removed from the range
819 * remove_if() is stable, so the relative order of elements that are
820 * not removed is unchanged.
822 * Elements between the end of the resulting sequence and @p last
823 * are still present, but their value is unspecified.
825 template<typename _ForwardIterator
, typename _Predicate
>
827 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
830 // concept requirements
831 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
833 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
834 typename iterator_traits
<_ForwardIterator
>::value_type
>)
835 __glibcxx_requires_valid_range(__first
, __last
);
837 __first
= _GLIBCXX_STD_P::find_if(__first
, __last
, __pred
);
838 if(__first
== __last
)
840 _ForwardIterator __result
= __first
;
842 for(; __first
!= __last
; ++__first
)
843 if(!__pred(*__first
))
845 *__result
= _GLIBCXX_MOVE(*__first
);
852 * @brief Remove consecutive duplicate values from a sequence.
853 * @param first A forward iterator.
854 * @param last A forward iterator.
855 * @return An iterator designating the end of the resulting sequence.
857 * Removes all but the first element from each group of consecutive
858 * values that compare equal.
859 * unique() is stable, so the relative order of elements that are
860 * not removed is unchanged.
861 * Elements between the end of the resulting sequence and @p last
862 * are still present, but their value is unspecified.
864 template<typename _ForwardIterator
>
866 unique(_ForwardIterator __first
, _ForwardIterator __last
)
868 // concept requirements
869 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
871 __glibcxx_function_requires(_EqualityComparableConcept
<
872 typename iterator_traits
<_ForwardIterator
>::value_type
>)
873 __glibcxx_requires_valid_range(__first
, __last
);
875 // Skip the beginning, if already unique.
876 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
);
877 if (__first
== __last
)
880 // Do the real copy work.
881 _ForwardIterator __dest
= __first
;
883 while (++__first
!= __last
)
884 if (!(*__dest
== *__first
))
885 *++__dest
= _GLIBCXX_MOVE(*__first
);
890 * @brief Remove consecutive values from a sequence using a predicate.
891 * @param first A forward iterator.
892 * @param last A forward iterator.
893 * @param binary_pred A binary predicate.
894 * @return An iterator designating the end of the resulting sequence.
896 * Removes all but the first element from each group of consecutive
897 * values for which @p binary_pred returns true.
898 * unique() is stable, so the relative order of elements that are
899 * not removed is unchanged.
900 * Elements between the end of the resulting sequence and @p last
901 * are still present, but their value is unspecified.
903 template<typename _ForwardIterator
, typename _BinaryPredicate
>
905 unique(_ForwardIterator __first
, _ForwardIterator __last
,
906 _BinaryPredicate __binary_pred
)
908 // concept requirements
909 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
911 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
912 typename iterator_traits
<_ForwardIterator
>::value_type
,
913 typename iterator_traits
<_ForwardIterator
>::value_type
>)
914 __glibcxx_requires_valid_range(__first
, __last
);
916 // Skip the beginning, if already unique.
917 __first
= _GLIBCXX_STD_P::adjacent_find(__first
, __last
, __binary_pred
);
918 if (__first
== __last
)
921 // Do the real copy work.
922 _ForwardIterator __dest
= __first
;
924 while (++__first
!= __last
)
925 if (!bool(__binary_pred(*__dest
, *__first
)))
926 *++__dest
= _GLIBCXX_MOVE(*__first
);
932 * This is an uglified unique_copy(_InputIterator, _InputIterator,
934 * overloaded for forward iterators and output iterator as result.
937 template<typename _ForwardIterator
, typename _OutputIterator
>
939 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
940 _OutputIterator __result
,
941 forward_iterator_tag
, output_iterator_tag
)
943 // concept requirements -- taken care of in dispatching function
944 _ForwardIterator __next
= __first
;
945 *__result
= *__first
;
946 while (++__next
!= __last
)
947 if (!(*__first
== *__next
))
950 *++__result
= *__first
;
957 * This is an uglified unique_copy(_InputIterator, _InputIterator,
959 * overloaded for input iterators and output iterator as result.
962 template<typename _InputIterator
, typename _OutputIterator
>
964 __unique_copy(_InputIterator __first
, _InputIterator __last
,
965 _OutputIterator __result
,
966 input_iterator_tag
, output_iterator_tag
)
968 // concept requirements -- taken care of in dispatching function
969 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
971 while (++__first
!= __last
)
972 if (!(__value
== *__first
))
975 *++__result
= __value
;
982 * This is an uglified unique_copy(_InputIterator, _InputIterator,
984 * overloaded for input iterators and forward iterator as result.
987 template<typename _InputIterator
, typename _ForwardIterator
>
989 __unique_copy(_InputIterator __first
, _InputIterator __last
,
990 _ForwardIterator __result
,
991 input_iterator_tag
, forward_iterator_tag
)
993 // concept requirements -- taken care of in dispatching function
994 *__result
= *__first
;
995 while (++__first
!= __last
)
996 if (!(*__result
== *__first
))
997 *++__result
= *__first
;
1003 * This is an uglified
1004 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1006 * overloaded for forward iterators and output iterator as result.
1009 template<typename _ForwardIterator
, typename _OutputIterator
,
1010 typename _BinaryPredicate
>
1012 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1013 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1014 forward_iterator_tag
, output_iterator_tag
)
1016 // concept requirements -- iterators already checked
1017 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1018 typename iterator_traits
<_ForwardIterator
>::value_type
,
1019 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1021 _ForwardIterator __next
= __first
;
1022 *__result
= *__first
;
1023 while (++__next
!= __last
)
1024 if (!bool(__binary_pred(*__first
, *__next
)))
1027 *++__result
= *__first
;
1034 * This is an uglified
1035 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1037 * overloaded for input iterators and output iterator as result.
1040 template<typename _InputIterator
, typename _OutputIterator
,
1041 typename _BinaryPredicate
>
1043 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1044 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1045 input_iterator_tag
, output_iterator_tag
)
1047 // concept requirements -- iterators already checked
1048 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1049 typename iterator_traits
<_InputIterator
>::value_type
,
1050 typename iterator_traits
<_InputIterator
>::value_type
>)
1052 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1053 *__result
= __value
;
1054 while (++__first
!= __last
)
1055 if (!bool(__binary_pred(__value
, *__first
)))
1058 *++__result
= __value
;
1065 * This is an uglified
1066 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1068 * overloaded for input iterators and forward iterator as result.
1071 template<typename _InputIterator
, typename _ForwardIterator
,
1072 typename _BinaryPredicate
>
1074 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1075 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1076 input_iterator_tag
, forward_iterator_tag
)
1078 // concept requirements -- iterators already checked
1079 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1080 typename iterator_traits
<_ForwardIterator
>::value_type
,
1081 typename iterator_traits
<_InputIterator
>::value_type
>)
1083 *__result
= *__first
;
1084 while (++__first
!= __last
)
1085 if (!bool(__binary_pred(*__result
, *__first
)))
1086 *++__result
= *__first
;
1092 * This is an uglified reverse(_BidirectionalIterator,
1093 * _BidirectionalIterator)
1094 * overloaded for bidirectional iterators.
1097 template<typename _BidirectionalIterator
>
1099 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1100 bidirectional_iterator_tag
)
1103 if (__first
== __last
|| __first
== --__last
)
1107 std::iter_swap(__first
, __last
);
1114 * This is an uglified reverse(_BidirectionalIterator,
1115 * _BidirectionalIterator)
1116 * overloaded for random access iterators.
1119 template<typename _RandomAccessIterator
>
1121 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1122 random_access_iterator_tag
)
1124 if (__first
== __last
)
1127 while (__first
< __last
)
1129 std::iter_swap(__first
, __last
);
1136 * @brief Reverse a sequence.
1137 * @param first A bidirectional iterator.
1138 * @param last A bidirectional iterator.
1139 * @return reverse() returns no value.
1141 * Reverses the order of the elements in the range @p [first,last),
1142 * so that the first element becomes the last etc.
1143 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1144 * swaps @p *(first+i) and @p *(last-(i+1))
1146 template<typename _BidirectionalIterator
>
1148 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1150 // concept requirements
1151 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1152 _BidirectionalIterator
>)
1153 __glibcxx_requires_valid_range(__first
, __last
);
1154 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1158 * @brief Copy a sequence, reversing its elements.
1159 * @param first A bidirectional iterator.
1160 * @param last A bidirectional iterator.
1161 * @param result An output iterator.
1162 * @return An iterator designating the end of the resulting sequence.
1164 * Copies the elements in the range @p [first,last) to the range
1165 * @p [result,result+(last-first)) such that the order of the
1166 * elements is reversed.
1167 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1168 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1169 * The ranges @p [first,last) and @p [result,result+(last-first))
1172 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1174 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1175 _OutputIterator __result
)
1177 // concept requirements
1178 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1179 _BidirectionalIterator
>)
1180 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1181 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1182 __glibcxx_requires_valid_range(__first
, __last
);
1184 while (__first
!= __last
)
1187 *__result
= *__last
;
1195 * This is a helper function for the rotate algorithm specialized on RAIs.
1196 * It returns the greatest common divisor of two integer values.
1199 template<typename _EuclideanRingElement
>
1200 _EuclideanRingElement
1201 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1205 _EuclideanRingElement __t
= __m
% __n
;
1214 * This is a helper function for the rotate algorithm.
1217 template<typename _ForwardIterator
>
1219 __rotate(_ForwardIterator __first
,
1220 _ForwardIterator __middle
,
1221 _ForwardIterator __last
,
1222 forward_iterator_tag
)
1224 if (__first
== __middle
|| __last
== __middle
)
1227 _ForwardIterator __first2
= __middle
;
1230 std::iter_swap(__first
, __first2
);
1233 if (__first
== __middle
)
1234 __middle
= __first2
;
1236 while (__first2
!= __last
);
1238 __first2
= __middle
;
1240 while (__first2
!= __last
)
1242 std::iter_swap(__first
, __first2
);
1245 if (__first
== __middle
)
1246 __middle
= __first2
;
1247 else if (__first2
== __last
)
1248 __first2
= __middle
;
1254 * This is a helper function for the rotate algorithm.
1257 template<typename _BidirectionalIterator
>
1259 __rotate(_BidirectionalIterator __first
,
1260 _BidirectionalIterator __middle
,
1261 _BidirectionalIterator __last
,
1262 bidirectional_iterator_tag
)
1264 // concept requirements
1265 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1266 _BidirectionalIterator
>)
1268 if (__first
== __middle
|| __last
== __middle
)
1271 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1272 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1274 while (__first
!= __middle
&& __middle
!= __last
)
1276 std::iter_swap(__first
, --__last
);
1280 if (__first
== __middle
)
1281 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1283 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1288 * This is a helper function for the rotate algorithm.
1291 template<typename _RandomAccessIterator
>
1293 __rotate(_RandomAccessIterator __first
,
1294 _RandomAccessIterator __middle
,
1295 _RandomAccessIterator __last
,
1296 random_access_iterator_tag
)
1298 // concept requirements
1299 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1300 _RandomAccessIterator
>)
1302 if (__first
== __middle
|| __last
== __middle
)
1305 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1307 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1310 const _Distance __n
= __last
- __first
;
1311 const _Distance __k
= __middle
- __first
;
1312 const _Distance __l
= __n
- __k
;
1316 std::swap_ranges(__first
, __middle
, __middle
);
1320 const _Distance __d
= std::__gcd(__n
, __k
);
1322 for (_Distance __i
= 0; __i
< __d
; __i
++)
1324 _ValueType __tmp
= _GLIBCXX_MOVE(*__first
);
1325 _RandomAccessIterator __p
= __first
;
1329 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1331 if (__p
> __first
+ __l
)
1333 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1337 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1343 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1345 if (__p
< __last
- __k
)
1347 *__p
= _GLIBCXX_MOVE(*(__p
+ __k
));
1350 *__p
= _GLIBCXX_MOVE(*(__p
- __l
));
1355 *__p
= _GLIBCXX_MOVE(__tmp
);
1361 * @brief Rotate the elements of a sequence.
1362 * @param first A forward iterator.
1363 * @param middle A forward iterator.
1364 * @param last A forward iterator.
1367 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1368 * positions so that the element at @p middle is moved to @p first, the
1369 * element at @p middle+1 is moved to @first+1 and so on for each element
1370 * in the range @p [first,last).
1372 * This effectively swaps the ranges @p [first,middle) and
1375 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1376 * each @p n in the range @p [0,last-first).
1378 template<typename _ForwardIterator
>
1380 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1381 _ForwardIterator __last
)
1383 // concept requirements
1384 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1386 __glibcxx_requires_valid_range(__first
, __middle
);
1387 __glibcxx_requires_valid_range(__middle
, __last
);
1389 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1391 std::__rotate(__first
, __middle
, __last
, _IterType());
1395 * @brief Copy a sequence, rotating its elements.
1396 * @param first A forward iterator.
1397 * @param middle A forward iterator.
1398 * @param last A forward iterator.
1399 * @param result An output iterator.
1400 * @return An iterator designating the end of the resulting sequence.
1402 * Copies the elements of the range @p [first,last) to the range
1403 * beginning at @result, rotating the copied elements by @p (middle-first)
1404 * positions so that the element at @p middle is moved to @p result, the
1405 * element at @p middle+1 is moved to @result+1 and so on for each element
1406 * in the range @p [first,last).
1408 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1409 * each @p n in the range @p [0,last-first).
1411 template<typename _ForwardIterator
, typename _OutputIterator
>
1413 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1414 _ForwardIterator __last
, _OutputIterator __result
)
1416 // concept requirements
1417 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1418 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1419 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1420 __glibcxx_requires_valid_range(__first
, __middle
);
1421 __glibcxx_requires_valid_range(__middle
, __last
);
1423 return std::copy(__first
, __middle
,
1424 std::copy(__middle
, __last
, __result
));
1429 * This is a helper function...
1432 template<typename _ForwardIterator
, typename _Predicate
>
1434 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1435 _Predicate __pred
, forward_iterator_tag
)
1437 if (__first
== __last
)
1440 while (__pred(*__first
))
1441 if (++__first
== __last
)
1444 _ForwardIterator __next
= __first
;
1446 while (++__next
!= __last
)
1447 if (__pred(*__next
))
1449 std::iter_swap(__first
, __next
);
1458 * This is a helper function...
1461 template<typename _BidirectionalIterator
, typename _Predicate
>
1462 _BidirectionalIterator
1463 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1464 _Predicate __pred
, bidirectional_iterator_tag
)
1469 if (__first
== __last
)
1471 else if (__pred(*__first
))
1477 if (__first
== __last
)
1479 else if (!bool(__pred(*__last
)))
1483 std::iter_swap(__first
, __last
);
1492 * This is a helper function...
1495 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1497 __inplace_stable_partition(_ForwardIterator __first
,
1498 _ForwardIterator __last
,
1499 _Predicate __pred
, _Distance __len
)
1502 return __pred(*__first
) ? __last
: __first
;
1503 _ForwardIterator __middle
= __first
;
1504 std::advance(__middle
, __len
/ 2);
1505 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1509 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1513 std::rotate(__begin
, __middle
, __end
);
1514 std::advance(__begin
, std::distance(__middle
, __end
));
1520 * This is a helper function...
1523 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1526 __stable_partition_adaptive(_ForwardIterator __first
,
1527 _ForwardIterator __last
,
1528 _Predicate __pred
, _Distance __len
,
1530 _Distance __buffer_size
)
1532 if (__len
<= __buffer_size
)
1534 _ForwardIterator __result1
= __first
;
1535 _Pointer __result2
= __buffer
;
1536 for (; __first
!= __last
; ++__first
)
1537 if (__pred(*__first
))
1539 *__result1
= *__first
;
1544 *__result2
= *__first
;
1547 std::copy(__buffer
, __result2
, __result1
);
1552 _ForwardIterator __middle
= __first
;
1553 std::advance(__middle
, __len
/ 2);
1554 _ForwardIterator __begin
=
1555 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1556 __len
/ 2, __buffer
,
1558 _ForwardIterator __end
=
1559 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1561 __buffer
, __buffer_size
);
1562 std::rotate(__begin
, __middle
, __end
);
1563 std::advance(__begin
, std::distance(__middle
, __end
));
1569 * @brief Move elements for which a predicate is true to the beginning
1570 * of a sequence, preserving relative ordering.
1571 * @param first A forward iterator.
1572 * @param last A forward iterator.
1573 * @param pred A predicate functor.
1574 * @return An iterator @p middle such that @p pred(i) is true for each
1575 * iterator @p i in the range @p [first,middle) and false for each @p i
1576 * in the range @p [middle,last).
1578 * Performs the same function as @p partition() with the additional
1579 * guarantee that the relative ordering of elements in each group is
1580 * preserved, so any two elements @p x and @p y in the range
1581 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1582 * relative ordering after calling @p stable_partition().
1584 template<typename _ForwardIterator
, typename _Predicate
>
1586 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1589 // concept requirements
1590 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1592 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1593 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1594 __glibcxx_requires_valid_range(__first
, __last
);
1596 if (__first
== __last
)
1600 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1602 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1605 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1607 if (__buf
.size() > 0)
1609 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1610 _DistanceType(__buf
.requested_size()),
1612 _DistanceType(__buf
.size()));
1615 std::__inplace_stable_partition(__first
, __last
, __pred
,
1616 _DistanceType(__buf
.requested_size()));
1622 * This is a helper function for the sort routines.
1625 template<typename _RandomAccessIterator
>
1627 __heap_select(_RandomAccessIterator __first
,
1628 _RandomAccessIterator __middle
,
1629 _RandomAccessIterator __last
)
1631 std::make_heap(__first
, __middle
);
1632 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1633 if (*__i
< *__first
)
1634 std::__pop_heap(__first
, __middle
, __i
);
1639 * This is a helper function for the sort routines.
1642 template<typename _RandomAccessIterator
, typename _Compare
>
1644 __heap_select(_RandomAccessIterator __first
,
1645 _RandomAccessIterator __middle
,
1646 _RandomAccessIterator __last
, _Compare __comp
)
1648 std::make_heap(__first
, __middle
, __comp
);
1649 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1650 if (__comp(*__i
, *__first
))
1651 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1657 * @brief Copy the smallest elements of a sequence.
1658 * @param first An iterator.
1659 * @param last Another iterator.
1660 * @param result_first A random-access iterator.
1661 * @param result_last Another random-access iterator.
1662 * @return An iterator indicating the end of the resulting sequence.
1664 * Copies and sorts the smallest N values from the range @p [first,last)
1665 * to the range beginning at @p result_first, where the number of
1666 * elements to be copied, @p N, is the smaller of @p (last-first) and
1667 * @p (result_last-result_first).
1668 * After the sort if @p i and @j are iterators in the range
1669 * @p [result_first,result_first+N) such that @i precedes @j then
1670 * @p *j<*i is false.
1671 * The value returned is @p result_first+N.
1673 template<typename _InputIterator
, typename _RandomAccessIterator
>
1674 _RandomAccessIterator
1675 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1676 _RandomAccessIterator __result_first
,
1677 _RandomAccessIterator __result_last
)
1679 typedef typename iterator_traits
<_InputIterator
>::value_type
1681 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1683 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1686 // concept requirements
1687 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1688 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1690 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1692 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1693 __glibcxx_requires_valid_range(__first
, __last
);
1694 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1696 if (__result_first
== __result_last
)
1697 return __result_last
;
1698 _RandomAccessIterator __result_real_last
= __result_first
;
1699 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1701 *__result_real_last
= *__first
;
1702 ++__result_real_last
;
1705 std::make_heap(__result_first
, __result_real_last
);
1706 while (__first
!= __last
)
1708 if (*__first
< *__result_first
)
1709 std::__adjust_heap(__result_first
, _DistanceType(0),
1710 _DistanceType(__result_real_last
1712 _InputValueType(*__first
));
1715 std::sort_heap(__result_first
, __result_real_last
);
1716 return __result_real_last
;
1720 * @brief Copy the smallest elements of a sequence using a predicate for
1722 * @param first An input iterator.
1723 * @param last Another input iterator.
1724 * @param result_first A random-access iterator.
1725 * @param result_last Another random-access iterator.
1726 * @param comp A comparison functor.
1727 * @return An iterator indicating the end of the resulting sequence.
1729 * Copies and sorts the smallest N values from the range @p [first,last)
1730 * to the range beginning at @p result_first, where the number of
1731 * elements to be copied, @p N, is the smaller of @p (last-first) and
1732 * @p (result_last-result_first).
1733 * After the sort if @p i and @j are iterators in the range
1734 * @p [result_first,result_first+N) such that @i precedes @j then
1735 * @p comp(*j,*i) is false.
1736 * The value returned is @p result_first+N.
1738 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
1739 _RandomAccessIterator
1740 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1741 _RandomAccessIterator __result_first
,
1742 _RandomAccessIterator __result_last
,
1745 typedef typename iterator_traits
<_InputIterator
>::value_type
1747 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1749 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1752 // concept requirements
1753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1754 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1755 _RandomAccessIterator
>)
1756 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1758 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1759 _InputValueType
, _OutputValueType
>)
1760 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1761 _OutputValueType
, _OutputValueType
>)
1762 __glibcxx_requires_valid_range(__first
, __last
);
1763 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1765 if (__result_first
== __result_last
)
1766 return __result_last
;
1767 _RandomAccessIterator __result_real_last
= __result_first
;
1768 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1770 *__result_real_last
= *__first
;
1771 ++__result_real_last
;
1774 std::make_heap(__result_first
, __result_real_last
, __comp
);
1775 while (__first
!= __last
)
1777 if (__comp(*__first
, *__result_first
))
1778 std::__adjust_heap(__result_first
, _DistanceType(0),
1779 _DistanceType(__result_real_last
1781 _InputValueType(*__first
),
1785 std::sort_heap(__result_first
, __result_real_last
, __comp
);
1786 return __result_real_last
;
1791 * This is a helper function for the sort routine.
1794 template<typename _RandomAccessIterator
, typename _Tp
>
1796 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1798 _RandomAccessIterator __next
= __last
;
1800 while (__val
< *__next
)
1811 * This is a helper function for the sort routine.
1814 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1816 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
1819 _RandomAccessIterator __next
= __last
;
1821 while (__comp(__val
, *__next
))
1832 * This is a helper function for the sort routine.
1835 template<typename _RandomAccessIterator
>
1837 __insertion_sort(_RandomAccessIterator __first
,
1838 _RandomAccessIterator __last
)
1840 if (__first
== __last
)
1843 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1845 typename iterator_traits
<_RandomAccessIterator
>::value_type
1847 if (__val
< *__first
)
1849 std::copy_backward(__first
, __i
, __i
+ 1);
1853 std::__unguarded_linear_insert(__i
, __val
);
1859 * This is a helper function for the sort routine.
1862 template<typename _RandomAccessIterator
, typename _Compare
>
1864 __insertion_sort(_RandomAccessIterator __first
,
1865 _RandomAccessIterator __last
, _Compare __comp
)
1867 if (__first
== __last
) return;
1869 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1871 typename iterator_traits
<_RandomAccessIterator
>::value_type
1873 if (__comp(__val
, *__first
))
1875 std::copy_backward(__first
, __i
, __i
+ 1);
1879 std::__unguarded_linear_insert(__i
, __val
, __comp
);
1885 * This is a helper function for the sort routine.
1888 template<typename _RandomAccessIterator
>
1890 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1891 _RandomAccessIterator __last
)
1893 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1896 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1897 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
1902 * This is a helper function for the sort routine.
1905 template<typename _RandomAccessIterator
, typename _Compare
>
1907 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1908 _RandomAccessIterator __last
, _Compare __comp
)
1910 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1913 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1914 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
1920 * This controls some aspect of the sort routines.
1923 enum { _S_threshold
= 16 };
1927 * This is a helper function for the sort routine.
1930 template<typename _RandomAccessIterator
>
1932 __final_insertion_sort(_RandomAccessIterator __first
,
1933 _RandomAccessIterator __last
)
1935 if (__last
- __first
> int(_S_threshold
))
1937 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
1938 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
1941 std::__insertion_sort(__first
, __last
);
1946 * This is a helper function for the sort routine.
1949 template<typename _RandomAccessIterator
, typename _Compare
>
1951 __final_insertion_sort(_RandomAccessIterator __first
,
1952 _RandomAccessIterator __last
, _Compare __comp
)
1954 if (__last
- __first
> int(_S_threshold
))
1956 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1957 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1961 std::__insertion_sort(__first
, __last
, __comp
);
1966 * This is a helper function...
1969 template<typename _RandomAccessIterator
, typename _Tp
>
1970 _RandomAccessIterator
1971 __unguarded_partition(_RandomAccessIterator __first
,
1972 _RandomAccessIterator __last
, _Tp __pivot
)
1976 while (*__first
< __pivot
)
1979 while (__pivot
< *__last
)
1981 if (!(__first
< __last
))
1983 std::iter_swap(__first
, __last
);
1990 * This is a helper function...
1993 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1994 _RandomAccessIterator
1995 __unguarded_partition(_RandomAccessIterator __first
,
1996 _RandomAccessIterator __last
,
1997 _Tp __pivot
, _Compare __comp
)
2001 while (__comp(*__first
, __pivot
))
2004 while (__comp(__pivot
, *__last
))
2006 if (!(__first
< __last
))
2008 std::iter_swap(__first
, __last
);
2015 * This is a helper function for the sort routine.
2018 template<typename _RandomAccessIterator
, typename _Size
>
2020 __introsort_loop(_RandomAccessIterator __first
,
2021 _RandomAccessIterator __last
,
2022 _Size __depth_limit
)
2024 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2027 while (__last
- __first
> int(_S_threshold
))
2029 if (__depth_limit
== 0)
2031 _GLIBCXX_STD_P
:partial_sort(__first
, __last
, __last
);
2035 _RandomAccessIterator __cut
=
2036 std::__unguarded_partition(__first
, __last
,
2037 _ValueType(std::__median(*__first
,
2044 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2051 * This is a helper function for the sort routine.
2054 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2056 __introsort_loop(_RandomAccessIterator __first
,
2057 _RandomAccessIterator __last
,
2058 _Size __depth_limit
, _Compare __comp
)
2060 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2063 while (__last
- __first
> int(_S_threshold
))
2065 if (__depth_limit
== 0)
2067 _GLIBCXX_STD_P::partial_sort(__first
, __last
, __last
, __comp
);
2071 _RandomAccessIterator __cut
=
2072 std::__unguarded_partition(__first
, __last
,
2073 _ValueType(std::__median(*__first
,
2081 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2088 * This is a helper function for the sort routines.
2091 template<typename _Size
>
2096 for (__k
= 0; __n
!= 1; __n
>>= 1)
2103 template<typename _RandomAccessIterator
, typename _Size
>
2105 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2106 _RandomAccessIterator __last
, _Size __depth_limit
)
2108 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2111 while (__last
- __first
> 3)
2113 if (__depth_limit
== 0)
2115 std::__heap_select(__first
, __nth
+ 1, __last
);
2116 // Place the nth largest element in its final position.
2117 std::iter_swap(__first
, __nth
);
2121 _RandomAccessIterator __cut
=
2122 std::__unguarded_partition(__first
, __last
,
2123 _ValueType(std::__median(*__first
,
2135 std::__insertion_sort(__first
, __last
);
2138 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2140 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2141 _RandomAccessIterator __last
, _Size __depth_limit
,
2144 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2147 while (__last
- __first
> 3)
2149 if (__depth_limit
== 0)
2151 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2152 // Place the nth largest element in its final position.
2153 std::iter_swap(__first
, __nth
);
2157 _RandomAccessIterator __cut
=
2158 std::__unguarded_partition(__first
, __last
,
2159 _ValueType(std::__median(*__first
,
2172 std::__insertion_sort(__first
, __last
, __comp
);
2178 * @brief Finds the first position in which @a val could be inserted
2179 * without changing the ordering.
2180 * @param first An iterator.
2181 * @param last Another iterator.
2182 * @param val The search term.
2183 * @return An iterator pointing to the first element "not less
2184 * than" @a val, or end() if every element is less than
2186 * @ingroup binarysearch
2188 template<typename _ForwardIterator
, typename _Tp
>
2190 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2193 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2195 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2198 // concept requirements
2199 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2200 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2201 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2203 _DistanceType __len
= std::distance(__first
, __last
);
2204 _DistanceType __half
;
2205 _ForwardIterator __middle
;
2209 __half
= __len
>> 1;
2211 std::advance(__middle
, __half
);
2212 if (*__middle
< __val
)
2216 __len
= __len
- __half
- 1;
2225 * @brief Finds the first position in which @a val could be inserted
2226 * without changing the ordering.
2227 * @param first An iterator.
2228 * @param last Another iterator.
2229 * @param val The search term.
2230 * @param comp A functor to use for comparisons.
2231 * @return An iterator pointing to the first element "not less than" @a val,
2232 * or end() if every element is less than @a val.
2233 * @ingroup binarysearch
2235 * The comparison function should have the same effects on ordering as
2236 * the function used for the initial sort.
2238 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2240 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2241 const _Tp
& __val
, _Compare __comp
)
2243 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2245 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2248 // concept requirements
2249 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2250 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2252 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2255 _DistanceType __len
= std::distance(__first
, __last
);
2256 _DistanceType __half
;
2257 _ForwardIterator __middle
;
2261 __half
= __len
>> 1;
2263 std::advance(__middle
, __half
);
2264 if (__comp(*__middle
, __val
))
2268 __len
= __len
- __half
- 1;
2277 * @brief Finds the last position in which @a val could be inserted
2278 * without changing the ordering.
2279 * @param first An iterator.
2280 * @param last Another iterator.
2281 * @param val The search term.
2282 * @return An iterator pointing to the first element greater than @a val,
2283 * or end() if no elements are greater than @a val.
2284 * @ingroup binarysearch
2286 template<typename _ForwardIterator
, typename _Tp
>
2288 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2291 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2293 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2296 // concept requirements
2297 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2298 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2299 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2301 _DistanceType __len
= std::distance(__first
, __last
);
2302 _DistanceType __half
;
2303 _ForwardIterator __middle
;
2307 __half
= __len
>> 1;
2309 std::advance(__middle
, __half
);
2310 if (__val
< *__middle
)
2316 __len
= __len
- __half
- 1;
2323 * @brief Finds the last position in which @a val could be inserted
2324 * without changing the ordering.
2325 * @param first An iterator.
2326 * @param last Another iterator.
2327 * @param val The search term.
2328 * @param comp A functor to use for comparisons.
2329 * @return An iterator pointing to the first element greater than @a val,
2330 * or end() if no elements are greater than @a val.
2331 * @ingroup binarysearch
2333 * The comparison function should have the same effects on ordering as
2334 * the function used for the initial sort.
2336 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2338 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2339 const _Tp
& __val
, _Compare __comp
)
2341 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2343 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2346 // concept requirements
2347 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2348 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2350 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2353 _DistanceType __len
= std::distance(__first
, __last
);
2354 _DistanceType __half
;
2355 _ForwardIterator __middle
;
2359 __half
= __len
>> 1;
2361 std::advance(__middle
, __half
);
2362 if (__comp(__val
, *__middle
))
2368 __len
= __len
- __half
- 1;
2375 * @brief Finds the largest subrange in which @a val could be inserted
2376 * at any place in it without changing the ordering.
2377 * @param first An iterator.
2378 * @param last Another iterator.
2379 * @param val The search term.
2380 * @return An pair of iterators defining the subrange.
2381 * @ingroup binarysearch
2383 * This is equivalent to
2385 * std::make_pair(lower_bound(first, last, val),
2386 * upper_bound(first, last, val))
2388 * but does not actually call those functions.
2390 template<typename _ForwardIterator
, typename _Tp
>
2391 pair
<_ForwardIterator
, _ForwardIterator
>
2392 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2395 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2397 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2400 // concept requirements
2401 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2402 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2403 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2404 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2405 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2407 _DistanceType __len
= std::distance(__first
, __last
);
2408 _DistanceType __half
;
2409 _ForwardIterator __middle
, __left
, __right
;
2413 __half
= __len
>> 1;
2415 std::advance(__middle
, __half
);
2416 if (*__middle
< __val
)
2420 __len
= __len
- __half
- 1;
2422 else if (__val
< *__middle
)
2426 __left
= std::lower_bound(__first
, __middle
, __val
);
2427 std::advance(__first
, __len
);
2428 __right
= std::upper_bound(++__middle
, __first
, __val
);
2429 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2432 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2436 * @brief Finds the largest subrange in which @a val could be inserted
2437 * at any place in it without changing the ordering.
2438 * @param first An iterator.
2439 * @param last Another iterator.
2440 * @param val The search term.
2441 * @param comp A functor to use for comparisons.
2442 * @return An pair of iterators defining the subrange.
2443 * @ingroup binarysearch
2445 * This is equivalent to
2447 * std::make_pair(lower_bound(first, last, val, comp),
2448 * upper_bound(first, last, val, comp))
2450 * but does not actually call those functions.
2452 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2453 pair
<_ForwardIterator
, _ForwardIterator
>
2454 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2458 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2460 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2463 // concept requirements
2464 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2465 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2467 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2469 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2471 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2474 _DistanceType __len
= std::distance(__first
, __last
);
2475 _DistanceType __half
;
2476 _ForwardIterator __middle
, __left
, __right
;
2480 __half
= __len
>> 1;
2482 std::advance(__middle
, __half
);
2483 if (__comp(*__middle
, __val
))
2487 __len
= __len
- __half
- 1;
2489 else if (__comp(__val
, *__middle
))
2493 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
2494 std::advance(__first
, __len
);
2495 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
2496 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2499 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2503 * @brief Determines whether an element exists in a range.
2504 * @param first An iterator.
2505 * @param last Another iterator.
2506 * @param val The search term.
2507 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2508 * @ingroup binarysearch
2510 * Note that this does not actually return an iterator to @a val. For
2511 * that, use std::find or a container's specialized find member functions.
2513 template<typename _ForwardIterator
, typename _Tp
>
2515 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2518 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2521 // concept requirements
2522 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2523 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2524 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2525 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2527 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2528 return __i
!= __last
&& !(__val
< *__i
);
2532 * @brief Determines whether an element exists in a range.
2533 * @param first An iterator.
2534 * @param last Another iterator.
2535 * @param val The search term.
2536 * @param comp A functor to use for comparisons.
2537 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2538 * @ingroup binarysearch
2540 * Note that this does not actually return an iterator to @a val. For
2541 * that, use std::find or a container's specialized find member functions.
2543 * The comparison function should have the same effects on ordering as
2544 * the function used for the initial sort.
2546 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2548 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2549 const _Tp
& __val
, _Compare __comp
)
2551 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2554 // concept requirements
2555 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2556 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2558 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2560 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2563 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2564 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2571 * This is a helper function for the merge routines.
2574 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2575 typename _BidirectionalIterator3
>
2576 _BidirectionalIterator3
2577 __merge_backward(_BidirectionalIterator1 __first1
,
2578 _BidirectionalIterator1 __last1
,
2579 _BidirectionalIterator2 __first2
,
2580 _BidirectionalIterator2 __last2
,
2581 _BidirectionalIterator3 __result
)
2583 if (__first1
== __last1
)
2584 return std::copy_backward(__first2
, __last2
, __result
);
2585 if (__first2
== __last2
)
2586 return std::copy_backward(__first1
, __last1
, __result
);
2591 if (*__last2
< *__last1
)
2593 *--__result
= *__last1
;
2594 if (__first1
== __last1
)
2595 return std::copy_backward(__first2
, ++__last2
, __result
);
2600 *--__result
= *__last2
;
2601 if (__first2
== __last2
)
2602 return std::copy_backward(__first1
, ++__last1
, __result
);
2610 * This is a helper function for the merge routines.
2613 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2614 typename _BidirectionalIterator3
, typename _Compare
>
2615 _BidirectionalIterator3
2616 __merge_backward(_BidirectionalIterator1 __first1
,
2617 _BidirectionalIterator1 __last1
,
2618 _BidirectionalIterator2 __first2
,
2619 _BidirectionalIterator2 __last2
,
2620 _BidirectionalIterator3 __result
,
2623 if (__first1
== __last1
)
2624 return std::copy_backward(__first2
, __last2
, __result
);
2625 if (__first2
== __last2
)
2626 return std::copy_backward(__first1
, __last1
, __result
);
2631 if (__comp(*__last2
, *__last1
))
2633 *--__result
= *__last1
;
2634 if (__first1
== __last1
)
2635 return std::copy_backward(__first2
, ++__last2
, __result
);
2640 *--__result
= *__last2
;
2641 if (__first2
== __last2
)
2642 return std::copy_backward(__first1
, ++__last1
, __result
);
2650 * This is a helper function for the merge routines.
2653 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2655 _BidirectionalIterator1
2656 __rotate_adaptive(_BidirectionalIterator1 __first
,
2657 _BidirectionalIterator1 __middle
,
2658 _BidirectionalIterator1 __last
,
2659 _Distance __len1
, _Distance __len2
,
2660 _BidirectionalIterator2 __buffer
,
2661 _Distance __buffer_size
)
2663 _BidirectionalIterator2 __buffer_end
;
2664 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2666 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2667 std::copy_backward(__first
, __middle
, __last
);
2668 return std::copy(__buffer
, __buffer_end
, __first
);
2670 else if (__len1
<= __buffer_size
)
2672 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2673 std::copy(__middle
, __last
, __first
);
2674 return std::copy_backward(__buffer
, __buffer_end
, __last
);
2678 std::rotate(__first
, __middle
, __last
);
2679 std::advance(__first
, std::distance(__middle
, __last
));
2686 * This is a helper function for the merge routines.
2689 template<typename _BidirectionalIterator
, typename _Distance
,
2692 __merge_adaptive(_BidirectionalIterator __first
,
2693 _BidirectionalIterator __middle
,
2694 _BidirectionalIterator __last
,
2695 _Distance __len1
, _Distance __len2
,
2696 _Pointer __buffer
, _Distance __buffer_size
)
2698 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2700 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2701 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2704 else if (__len2
<= __buffer_size
)
2706 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2707 std::__merge_backward(__first
, __middle
, __buffer
,
2708 __buffer_end
, __last
);
2712 _BidirectionalIterator __first_cut
= __first
;
2713 _BidirectionalIterator __second_cut
= __middle
;
2714 _Distance __len11
= 0;
2715 _Distance __len22
= 0;
2716 if (__len1
> __len2
)
2718 __len11
= __len1
/ 2;
2719 std::advance(__first_cut
, __len11
);
2720 __second_cut
= std::lower_bound(__middle
, __last
,
2722 __len22
= std::distance(__middle
, __second_cut
);
2726 __len22
= __len2
/ 2;
2727 std::advance(__second_cut
, __len22
);
2728 __first_cut
= std::upper_bound(__first
, __middle
,
2730 __len11
= std::distance(__first
, __first_cut
);
2732 _BidirectionalIterator __new_middle
=
2733 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2734 __len1
- __len11
, __len22
, __buffer
,
2736 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2737 __len22
, __buffer
, __buffer_size
);
2738 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2740 __len2
- __len22
, __buffer
, __buffer_size
);
2746 * This is a helper function for the merge routines.
2749 template<typename _BidirectionalIterator
, typename _Distance
,
2750 typename _Pointer
, typename _Compare
>
2752 __merge_adaptive(_BidirectionalIterator __first
,
2753 _BidirectionalIterator __middle
,
2754 _BidirectionalIterator __last
,
2755 _Distance __len1
, _Distance __len2
,
2756 _Pointer __buffer
, _Distance __buffer_size
,
2759 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2761 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
2762 _GLIBCXX_STD_P::merge(__buffer
, __buffer_end
, __middle
, __last
,
2765 else if (__len2
<= __buffer_size
)
2767 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
2768 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
2773 _BidirectionalIterator __first_cut
= __first
;
2774 _BidirectionalIterator __second_cut
= __middle
;
2775 _Distance __len11
= 0;
2776 _Distance __len22
= 0;
2777 if (__len1
> __len2
)
2779 __len11
= __len1
/ 2;
2780 std::advance(__first_cut
, __len11
);
2781 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2783 __len22
= std::distance(__middle
, __second_cut
);
2787 __len22
= __len2
/ 2;
2788 std::advance(__second_cut
, __len22
);
2789 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2791 __len11
= std::distance(__first
, __first_cut
);
2793 _BidirectionalIterator __new_middle
=
2794 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2795 __len1
- __len11
, __len22
, __buffer
,
2797 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2798 __len22
, __buffer
, __buffer_size
, __comp
);
2799 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2801 __len2
- __len22
, __buffer
,
2802 __buffer_size
, __comp
);
2808 * This is a helper function for the merge routines.
2811 template<typename _BidirectionalIterator
, typename _Distance
>
2813 __merge_without_buffer(_BidirectionalIterator __first
,
2814 _BidirectionalIterator __middle
,
2815 _BidirectionalIterator __last
,
2816 _Distance __len1
, _Distance __len2
)
2818 if (__len1
== 0 || __len2
== 0)
2820 if (__len1
+ __len2
== 2)
2822 if (*__middle
< *__first
)
2823 std::iter_swap(__first
, __middle
);
2826 _BidirectionalIterator __first_cut
= __first
;
2827 _BidirectionalIterator __second_cut
= __middle
;
2828 _Distance __len11
= 0;
2829 _Distance __len22
= 0;
2830 if (__len1
> __len2
)
2832 __len11
= __len1
/ 2;
2833 std::advance(__first_cut
, __len11
);
2834 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2835 __len22
= std::distance(__middle
, __second_cut
);
2839 __len22
= __len2
/ 2;
2840 std::advance(__second_cut
, __len22
);
2841 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2842 __len11
= std::distance(__first
, __first_cut
);
2844 std::rotate(__first_cut
, __middle
, __second_cut
);
2845 _BidirectionalIterator __new_middle
= __first_cut
;
2846 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2847 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2849 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2850 __len1
- __len11
, __len2
- __len22
);
2855 * This is a helper function for the merge routines.
2858 template<typename _BidirectionalIterator
, typename _Distance
,
2861 __merge_without_buffer(_BidirectionalIterator __first
,
2862 _BidirectionalIterator __middle
,
2863 _BidirectionalIterator __last
,
2864 _Distance __len1
, _Distance __len2
,
2867 if (__len1
== 0 || __len2
== 0)
2869 if (__len1
+ __len2
== 2)
2871 if (__comp(*__middle
, *__first
))
2872 std::iter_swap(__first
, __middle
);
2875 _BidirectionalIterator __first_cut
= __first
;
2876 _BidirectionalIterator __second_cut
= __middle
;
2877 _Distance __len11
= 0;
2878 _Distance __len22
= 0;
2879 if (__len1
> __len2
)
2881 __len11
= __len1
/ 2;
2882 std::advance(__first_cut
, __len11
);
2883 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2885 __len22
= std::distance(__middle
, __second_cut
);
2889 __len22
= __len2
/ 2;
2890 std::advance(__second_cut
, __len22
);
2891 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2893 __len11
= std::distance(__first
, __first_cut
);
2895 std::rotate(__first_cut
, __middle
, __second_cut
);
2896 _BidirectionalIterator __new_middle
= __first_cut
;
2897 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2898 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2899 __len11
, __len22
, __comp
);
2900 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2901 __len1
- __len11
, __len2
- __len22
, __comp
);
2905 * @brief Merges two sorted ranges in place.
2906 * @param first An iterator.
2907 * @param middle Another iterator.
2908 * @param last Another iterator.
2911 * Merges two sorted and consecutive ranges, [first,middle) and
2912 * [middle,last), and puts the result in [first,last). The output will
2913 * be sorted. The sort is @e stable, that is, for equivalent
2914 * elements in the two ranges, elements from the first range will always
2915 * come before elements from the second.
2917 * If enough additional memory is available, this takes (last-first)-1
2918 * comparisons. Otherwise an NlogN algorithm is used, where N is
2919 * distance(first,last).
2921 template<typename _BidirectionalIterator
>
2923 inplace_merge(_BidirectionalIterator __first
,
2924 _BidirectionalIterator __middle
,
2925 _BidirectionalIterator __last
)
2927 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2929 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2932 // concept requirements
2933 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2934 _BidirectionalIterator
>)
2935 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2936 __glibcxx_requires_sorted(__first
, __middle
);
2937 __glibcxx_requires_sorted(__middle
, __last
);
2939 if (__first
== __middle
|| __middle
== __last
)
2942 _DistanceType __len1
= std::distance(__first
, __middle
);
2943 _DistanceType __len2
= std::distance(__middle
, __last
);
2945 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
2947 if (__buf
.begin() == 0)
2948 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
2950 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
2951 __buf
.begin(), _DistanceType(__buf
.size()));
2955 * @brief Merges two sorted ranges in place.
2956 * @param first An iterator.
2957 * @param middle Another iterator.
2958 * @param last Another iterator.
2959 * @param comp A functor to use for comparisons.
2962 * Merges two sorted and consecutive ranges, [first,middle) and
2963 * [middle,last), and puts the result in [first,last). The output will
2964 * be sorted. The sort is @e stable, that is, for equivalent
2965 * elements in the two ranges, elements from the first range will always
2966 * come before elements from the second.
2968 * If enough additional memory is available, this takes (last-first)-1
2969 * comparisons. Otherwise an NlogN algorithm is used, where N is
2970 * distance(first,last).
2972 * The comparison function should have the same effects on ordering as
2973 * the function used for the initial sort.
2975 template<typename _BidirectionalIterator
, typename _Compare
>
2977 inplace_merge(_BidirectionalIterator __first
,
2978 _BidirectionalIterator __middle
,
2979 _BidirectionalIterator __last
,
2982 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2984 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2987 // concept requirements
2988 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2989 _BidirectionalIterator
>)
2990 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2991 _ValueType
, _ValueType
>)
2992 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2993 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2995 if (__first
== __middle
|| __middle
== __last
)
2998 const _DistanceType __len1
= std::distance(__first
, __middle
);
2999 const _DistanceType __len2
= std::distance(__middle
, __last
);
3001 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3003 if (__buf
.begin() == 0)
3004 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3007 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3008 __buf
.begin(), _DistanceType(__buf
.size()),
3012 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3015 __merge_sort_loop(_RandomAccessIterator1 __first
,
3016 _RandomAccessIterator1 __last
,
3017 _RandomAccessIterator2 __result
,
3018 _Distance __step_size
)
3020 const _Distance __two_step
= 2 * __step_size
;
3022 while (__last
- __first
>= __two_step
)
3024 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3025 __first
+ __step_size
, __first
+ __two_step
,
3027 __first
+= __two_step
;
3030 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3031 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3032 __first
+ __step_size
, __last
,
3036 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3037 typename _Distance
, typename _Compare
>
3039 __merge_sort_loop(_RandomAccessIterator1 __first
,
3040 _RandomAccessIterator1 __last
,
3041 _RandomAccessIterator2 __result
, _Distance __step_size
,
3044 const _Distance __two_step
= 2 * __step_size
;
3046 while (__last
- __first
>= __two_step
)
3048 __result
= _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3049 __first
+ __step_size
, __first
+ __two_step
,
3052 __first
+= __two_step
;
3054 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3056 _GLIBCXX_STD_P::merge(__first
, __first
+ __step_size
,
3057 __first
+ __step_size
, __last
, __result
, __comp
);
3060 template<typename _RandomAccessIterator
, typename _Distance
>
3062 __chunk_insertion_sort(_RandomAccessIterator __first
,
3063 _RandomAccessIterator __last
,
3064 _Distance __chunk_size
)
3066 while (__last
- __first
>= __chunk_size
)
3068 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3069 __first
+= __chunk_size
;
3071 std::__insertion_sort(__first
, __last
);
3074 template<typename _RandomAccessIterator
, typename _Distance
,
3077 __chunk_insertion_sort(_RandomAccessIterator __first
,
3078 _RandomAccessIterator __last
,
3079 _Distance __chunk_size
, _Compare __comp
)
3081 while (__last
- __first
>= __chunk_size
)
3083 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3084 __first
+= __chunk_size
;
3086 std::__insertion_sort(__first
, __last
, __comp
);
3089 enum { _S_chunk_size
= 7 };
3091 template<typename _RandomAccessIterator
, typename _Pointer
>
3093 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3094 _RandomAccessIterator __last
,
3097 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3100 const _Distance __len
= __last
- __first
;
3101 const _Pointer __buffer_last
= __buffer
+ __len
;
3103 _Distance __step_size
= _S_chunk_size
;
3104 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3106 while (__step_size
< __len
)
3108 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3110 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3115 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3117 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3118 _RandomAccessIterator __last
,
3119 _Pointer __buffer
, _Compare __comp
)
3121 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3124 const _Distance __len
= __last
- __first
;
3125 const _Pointer __buffer_last
= __buffer
+ __len
;
3127 _Distance __step_size
= _S_chunk_size
;
3128 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3130 while (__step_size
< __len
)
3132 std::__merge_sort_loop(__first
, __last
, __buffer
,
3133 __step_size
, __comp
);
3135 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3136 __step_size
, __comp
);
3141 template<typename _RandomAccessIterator
, typename _Pointer
,
3144 __stable_sort_adaptive(_RandomAccessIterator __first
,
3145 _RandomAccessIterator __last
,
3146 _Pointer __buffer
, _Distance __buffer_size
)
3148 const _Distance __len
= (__last
- __first
+ 1) / 2;
3149 const _RandomAccessIterator __middle
= __first
+ __len
;
3150 if (__len
> __buffer_size
)
3152 std::__stable_sort_adaptive(__first
, __middle
,
3153 __buffer
, __buffer_size
);
3154 std::__stable_sort_adaptive(__middle
, __last
,
3155 __buffer
, __buffer_size
);
3159 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3160 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3162 std::__merge_adaptive(__first
, __middle
, __last
,
3163 _Distance(__middle
- __first
),
3164 _Distance(__last
- __middle
),
3165 __buffer
, __buffer_size
);
3168 template<typename _RandomAccessIterator
, typename _Pointer
,
3169 typename _Distance
, typename _Compare
>
3171 __stable_sort_adaptive(_RandomAccessIterator __first
,
3172 _RandomAccessIterator __last
,
3173 _Pointer __buffer
, _Distance __buffer_size
,
3176 const _Distance __len
= (__last
- __first
+ 1) / 2;
3177 const _RandomAccessIterator __middle
= __first
+ __len
;
3178 if (__len
> __buffer_size
)
3180 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3181 __buffer_size
, __comp
);
3182 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3183 __buffer_size
, __comp
);
3187 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3188 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3190 std::__merge_adaptive(__first
, __middle
, __last
,
3191 _Distance(__middle
- __first
),
3192 _Distance(__last
- __middle
),
3193 __buffer
, __buffer_size
,
3199 * This is a helper function for the stable sorting routines.
3202 template<typename _RandomAccessIterator
>
3204 __inplace_stable_sort(_RandomAccessIterator __first
,
3205 _RandomAccessIterator __last
)
3207 if (__last
- __first
< 15)
3209 std::__insertion_sort(__first
, __last
);
3212 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3213 std::__inplace_stable_sort(__first
, __middle
);
3214 std::__inplace_stable_sort(__middle
, __last
);
3215 std::__merge_without_buffer(__first
, __middle
, __last
,
3222 * This is a helper function for the stable sorting routines.
3225 template<typename _RandomAccessIterator
, typename _Compare
>
3227 __inplace_stable_sort(_RandomAccessIterator __first
,
3228 _RandomAccessIterator __last
, _Compare __comp
)
3230 if (__last
- __first
< 15)
3232 std::__insertion_sort(__first
, __last
, __comp
);
3235 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3236 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3237 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3238 std::__merge_without_buffer(__first
, __middle
, __last
,
3246 // Set algorithms: includes, set_union, set_intersection, set_difference,
3247 // set_symmetric_difference. All of these algorithms have the precondition
3248 // that their input ranges are sorted and the postcondition that their output
3249 // ranges are sorted.
3252 * @brief Determines whether all elements of a sequence exists in a range.
3253 * @param first1 Start of search range.
3254 * @param last1 End of search range.
3255 * @param first2 Start of sequence
3256 * @param last2 End of sequence.
3257 * @return True if each element in [first2,last2) is contained in order
3258 * within [first1,last1). False otherwise.
3259 * @ingroup setoperations
3261 * This operation expects both [first1,last1) and [first2,last2) to be
3262 * sorted. Searches for the presence of each element in [first2,last2)
3263 * within [first1,last1). The iterators over each range only move forward,
3264 * so this is a linear algorithm. If an element in [first2,last2) is not
3265 * found before the search iterator reaches @a last2, false is returned.
3267 template<typename _InputIterator1
, typename _InputIterator2
>
3269 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3270 _InputIterator2 __first2
, _InputIterator2 __last2
)
3272 typedef typename iterator_traits
<_InputIterator1
>::value_type
3274 typedef typename iterator_traits
<_InputIterator2
>::value_type
3277 // concept requirements
3278 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3279 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3280 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3281 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3282 __glibcxx_requires_sorted(__first1
, __last1
);
3283 __glibcxx_requires_sorted(__first2
, __last2
);
3285 while (__first1
!= __last1
&& __first2
!= __last2
)
3286 if (*__first2
< *__first1
)
3288 else if(*__first1
< *__first2
)
3291 ++__first1
, ++__first2
;
3293 return __first2
== __last2
;
3297 * @brief Determines whether all elements of a sequence exists in a range
3299 * @param first1 Start of search range.
3300 * @param last1 End of search range.
3301 * @param first2 Start of sequence
3302 * @param last2 End of sequence.
3303 * @param comp Comparison function to use.
3304 * @return True if each element in [first2,last2) is contained in order
3305 * within [first1,last1) according to comp. False otherwise.
3306 * @ingroup setoperations
3308 * This operation expects both [first1,last1) and [first2,last2) to be
3309 * sorted. Searches for the presence of each element in [first2,last2)
3310 * within [first1,last1), using comp to decide. The iterators over each
3311 * range only move forward, so this is a linear algorithm. If an element
3312 * in [first2,last2) is not found before the search iterator reaches @a
3313 * last2, false is returned.
3315 template<typename _InputIterator1
, typename _InputIterator2
,
3318 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3319 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
3321 typedef typename iterator_traits
<_InputIterator1
>::value_type
3323 typedef typename iterator_traits
<_InputIterator2
>::value_type
3326 // concept requirements
3327 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3328 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3329 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3330 _ValueType1
, _ValueType2
>)
3331 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3332 _ValueType2
, _ValueType1
>)
3333 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3334 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3336 while (__first1
!= __last1
&& __first2
!= __last2
)
3337 if (__comp(*__first2
, *__first1
))
3339 else if(__comp(*__first1
, *__first2
))
3342 ++__first1
, ++__first2
;
3344 return __first2
== __last2
;
3353 // set_symmetric_difference
3358 * @brief Permute range into the next "dictionary" ordering.
3359 * @param first Start of range.
3360 * @param last End of range.
3361 * @return False if wrapped to first permutation, true otherwise.
3363 * Treats all permutations of the range as a set of "dictionary" sorted
3364 * sequences. Permutes the current sequence into the next one of this set.
3365 * Returns true if there are more sequences to generate. If the sequence
3366 * is the largest of the set, the smallest is generated and false returned.
3368 template<typename _BidirectionalIterator
>
3370 next_permutation(_BidirectionalIterator __first
,
3371 _BidirectionalIterator __last
)
3373 // concept requirements
3374 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3375 _BidirectionalIterator
>)
3376 __glibcxx_function_requires(_LessThanComparableConcept
<
3377 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3378 __glibcxx_requires_valid_range(__first
, __last
);
3380 if (__first
== __last
)
3382 _BidirectionalIterator __i
= __first
;
3391 _BidirectionalIterator __ii
= __i
;
3395 _BidirectionalIterator __j
= __last
;
3396 while (!(*__i
< *--__j
))
3398 std::iter_swap(__i
, __j
);
3399 std::reverse(__ii
, __last
);
3404 std::reverse(__first
, __last
);
3411 * @brief Permute range into the next "dictionary" ordering using
3412 * comparison functor.
3413 * @param first Start of range.
3414 * @param last End of range.
3415 * @param comp A comparison functor.
3416 * @return False if wrapped to first permutation, true otherwise.
3418 * Treats all permutations of the range [first,last) as a set of
3419 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3420 * sequence into the next one of this set. Returns true if there are more
3421 * sequences to generate. If the sequence is the largest of the set, the
3422 * smallest is generated and false returned.
3424 template<typename _BidirectionalIterator
, typename _Compare
>
3426 next_permutation(_BidirectionalIterator __first
,
3427 _BidirectionalIterator __last
, _Compare __comp
)
3429 // concept requirements
3430 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3431 _BidirectionalIterator
>)
3432 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3433 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3434 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3435 __glibcxx_requires_valid_range(__first
, __last
);
3437 if (__first
== __last
)
3439 _BidirectionalIterator __i
= __first
;
3448 _BidirectionalIterator __ii
= __i
;
3450 if (__comp(*__i
, *__ii
))
3452 _BidirectionalIterator __j
= __last
;
3453 while (!bool(__comp(*__i
, *--__j
)))
3455 std::iter_swap(__i
, __j
);
3456 std::reverse(__ii
, __last
);
3461 std::reverse(__first
, __last
);
3468 * @brief Permute range into the previous "dictionary" ordering.
3469 * @param first Start of range.
3470 * @param last End of range.
3471 * @return False if wrapped to last permutation, true otherwise.
3473 * Treats all permutations of the range as a set of "dictionary" sorted
3474 * sequences. Permutes the current sequence into the previous one of this
3475 * set. Returns true if there are more sequences to generate. If the
3476 * sequence is the smallest of the set, the largest is generated and false
3479 template<typename _BidirectionalIterator
>
3481 prev_permutation(_BidirectionalIterator __first
,
3482 _BidirectionalIterator __last
)
3484 // concept requirements
3485 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3486 _BidirectionalIterator
>)
3487 __glibcxx_function_requires(_LessThanComparableConcept
<
3488 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3489 __glibcxx_requires_valid_range(__first
, __last
);
3491 if (__first
== __last
)
3493 _BidirectionalIterator __i
= __first
;
3502 _BidirectionalIterator __ii
= __i
;
3506 _BidirectionalIterator __j
= __last
;
3507 while (!(*--__j
< *__i
))
3509 std::iter_swap(__i
, __j
);
3510 std::reverse(__ii
, __last
);
3515 std::reverse(__first
, __last
);
3522 * @brief Permute range into the previous "dictionary" ordering using
3523 * comparison functor.
3524 * @param first Start of range.
3525 * @param last End of range.
3526 * @param comp A comparison functor.
3527 * @return False if wrapped to last permutation, true otherwise.
3529 * Treats all permutations of the range [first,last) as a set of
3530 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3531 * sequence into the previous one of this set. Returns true if there are
3532 * more sequences to generate. If the sequence is the smallest of the set,
3533 * the largest is generated and false returned.
3535 template<typename _BidirectionalIterator
, typename _Compare
>
3537 prev_permutation(_BidirectionalIterator __first
,
3538 _BidirectionalIterator __last
, _Compare __comp
)
3540 // concept requirements
3541 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3542 _BidirectionalIterator
>)
3543 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3544 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3545 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3546 __glibcxx_requires_valid_range(__first
, __last
);
3548 if (__first
== __last
)
3550 _BidirectionalIterator __i
= __first
;
3559 _BidirectionalIterator __ii
= __i
;
3561 if (__comp(*__ii
, *__i
))
3563 _BidirectionalIterator __j
= __last
;
3564 while (!bool(__comp(*--__j
, *__i
)))
3566 std::iter_swap(__i
, __j
);
3567 std::reverse(__ii
, __last
);
3572 std::reverse(__first
, __last
);
3582 * @brief Copy a sequence, replacing each element of one value with another
3584 * @param first An input iterator.
3585 * @param last An input iterator.
3586 * @param result An output iterator.
3587 * @param old_value The value to be replaced.
3588 * @param new_value The replacement value.
3589 * @return The end of the output sequence, @p result+(last-first).
3591 * Copies each element in the input range @p [first,last) to the
3592 * output range @p [result,result+(last-first)) replacing elements
3593 * equal to @p old_value with @p new_value.
3595 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3597 replace_copy(_InputIterator __first
, _InputIterator __last
,
3598 _OutputIterator __result
,
3599 const _Tp
& __old_value
, const _Tp
& __new_value
)
3601 // concept requirements
3602 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3603 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3604 typename iterator_traits
<_InputIterator
>::value_type
>)
3605 __glibcxx_function_requires(_EqualOpConcept
<
3606 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3607 __glibcxx_requires_valid_range(__first
, __last
);
3609 for (; __first
!= __last
; ++__first
, ++__result
)
3610 if (*__first
== __old_value
)
3611 *__result
= __new_value
;
3613 *__result
= *__first
;
3618 * @brief Copy a sequence, replacing each value for which a predicate
3619 * returns true with another value.
3620 * @param first An input iterator.
3621 * @param last An input iterator.
3622 * @param result An output iterator.
3623 * @param pred A predicate.
3624 * @param new_value The replacement value.
3625 * @return The end of the output sequence, @p result+(last-first).
3627 * Copies each element in the range @p [first,last) to the range
3628 * @p [result,result+(last-first)) replacing elements for which
3629 * @p pred returns true with @p new_value.
3631 template<typename _InputIterator
, typename _OutputIterator
,
3632 typename _Predicate
, typename _Tp
>
3634 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3635 _OutputIterator __result
,
3636 _Predicate __pred
, const _Tp
& __new_value
)
3638 // concept requirements
3639 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3640 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3641 typename iterator_traits
<_InputIterator
>::value_type
>)
3642 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3643 typename iterator_traits
<_InputIterator
>::value_type
>)
3644 __glibcxx_requires_valid_range(__first
, __last
);
3646 for (; __first
!= __last
; ++__first
, ++__result
)
3647 if (__pred(*__first
))
3648 *__result
= __new_value
;
3650 *__result
= *__first
;
3654 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3656 * @brief Determines whether the elements of a sequence are sorted.
3657 * @param first An iterator.
3658 * @param last Another iterator.
3659 * @return True if the elements are sorted, false otherwise.
3661 template<typename _ForwardIterator
>
3663 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3664 { return std::is_sorted_until(__first
, __last
) == __last
; }
3667 * @brief Determines whether the elements of a sequence are sorted
3668 * according to a comparison functor.
3669 * @param first An iterator.
3670 * @param last Another iterator.
3671 * @param comp A comparison functor.
3672 * @return True if the elements are sorted, false otherwise.
3674 template<typename _ForwardIterator
, typename _Compare
>
3676 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3678 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3681 * @brief Determines the end of a sorted sequence.
3682 * @param first An iterator.
3683 * @param last Another iterator.
3684 * @return An iterator pointing to the last iterator i in [first, last)
3685 * for which the range [first, i) is sorted.
3687 template<typename _ForwardIterator
>
3689 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3691 // concept requirements
3692 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3693 __glibcxx_function_requires(_LessThanComparableConcept
<
3694 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3695 __glibcxx_requires_valid_range(__first
, __last
);
3697 if (__first
== __last
)
3700 _ForwardIterator __next
= __first
;
3701 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3702 if (*__next
< *__first
)
3708 * @brief Determines the end of a sorted sequence using comparison functor.
3709 * @param first An iterator.
3710 * @param last Another iterator.
3711 * @param comp A comparison functor.
3712 * @return An iterator pointing to the last iterator i in [first, last)
3713 * for which the range [first, i) is sorted.
3715 template<typename _ForwardIterator
, typename _Compare
>
3717 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3720 // concept requirements
3721 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3722 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3723 typename iterator_traits
<_ForwardIterator
>::value_type
,
3724 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3725 __glibcxx_requires_valid_range(__first
, __last
);
3727 if (__first
== __last
)
3730 _ForwardIterator __next
= __first
;
3731 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3732 if (__comp(*__next
, *__first
))
3736 #endif // __GXX_EXPERIMENTAL_CXX0X__
3738 _GLIBCXX_END_NAMESPACE
3740 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
3743 * @brief Apply a function to every element of a sequence.
3744 * @param first An input iterator.
3745 * @param last An input iterator.
3746 * @param f A unary function object.
3749 * Applies the function object @p f to each element in the range
3750 * @p [first,last). @p f must not modify the order of the sequence.
3751 * If @p f has a return value it is ignored.
3753 template<typename _InputIterator
, typename _Function
>
3755 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3757 // concept requirements
3758 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3759 __glibcxx_requires_valid_range(__first
, __last
);
3760 for (; __first
!= __last
; ++__first
)
3766 * @brief Find the first occurrence of a value in a sequence.
3767 * @param first An input iterator.
3768 * @param last An input iterator.
3769 * @param val The value to find.
3770 * @return The first iterator @c i in the range @p [first,last)
3771 * such that @c *i == @p val, or @p last if no such iterator exists.
3773 template<typename _InputIterator
, typename _Tp
>
3774 inline _InputIterator
3775 find(_InputIterator __first
, _InputIterator __last
,
3778 // concept requirements
3779 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3780 __glibcxx_function_requires(_EqualOpConcept
<
3781 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3782 __glibcxx_requires_valid_range(__first
, __last
);
3783 return std::__find(__first
, __last
, __val
,
3784 std::__iterator_category(__first
));
3788 * @brief Find the first element in a sequence for which a
3789 * predicate is true.
3790 * @param first An input iterator.
3791 * @param last An input iterator.
3792 * @param pred A predicate.
3793 * @return The first iterator @c i in the range @p [first,last)
3794 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3796 template<typename _InputIterator
, typename _Predicate
>
3797 inline _InputIterator
3798 find_if(_InputIterator __first
, _InputIterator __last
,
3801 // concept requirements
3802 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3803 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3804 typename iterator_traits
<_InputIterator
>::value_type
>)
3805 __glibcxx_requires_valid_range(__first
, __last
);
3806 return std::__find_if(__first
, __last
, __pred
,
3807 std::__iterator_category(__first
));
3811 * @brief Find element from a set in a sequence.
3812 * @param first1 Start of range to search.
3813 * @param last1 End of range to search.
3814 * @param first2 Start of match candidates.
3815 * @param last2 End of match candidates.
3816 * @return The first iterator @c i in the range
3817 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
3818 * interator in [first2,last2), or @p last1 if no such iterator exists.
3820 * Searches the range @p [first1,last1) for an element that is equal to
3821 * some element in the range [first2,last2). If found, returns an iterator
3822 * in the range [first1,last1), otherwise returns @p last1.
3824 template<typename _InputIterator
, typename _ForwardIterator
>
3826 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3827 _ForwardIterator __first2
, _ForwardIterator __last2
)
3829 // concept requirements
3830 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3831 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3832 __glibcxx_function_requires(_EqualOpConcept
<
3833 typename iterator_traits
<_InputIterator
>::value_type
,
3834 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3835 __glibcxx_requires_valid_range(__first1
, __last1
);
3836 __glibcxx_requires_valid_range(__first2
, __last2
);
3838 for (; __first1
!= __last1
; ++__first1
)
3839 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3840 if (*__first1
== *__iter
)
3846 * @brief Find element from a set in a sequence using a predicate.
3847 * @param first1 Start of range to search.
3848 * @param last1 End of range to search.
3849 * @param first2 Start of match candidates.
3850 * @param last2 End of match candidates.
3851 * @param comp Predicate to use.
3852 * @return The first iterator @c i in the range
3853 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
3854 * interator in [first2,last2), or @p last1 if no such iterator exists.
3857 * Searches the range @p [first1,last1) for an element that is
3858 * equal to some element in the range [first2,last2). If found,
3859 * returns an iterator in the range [first1,last1), otherwise
3862 template<typename _InputIterator
, typename _ForwardIterator
,
3863 typename _BinaryPredicate
>
3865 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3866 _ForwardIterator __first2
, _ForwardIterator __last2
,
3867 _BinaryPredicate __comp
)
3869 // concept requirements
3870 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3871 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3872 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3873 typename iterator_traits
<_InputIterator
>::value_type
,
3874 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3875 __glibcxx_requires_valid_range(__first1
, __last1
);
3876 __glibcxx_requires_valid_range(__first2
, __last2
);
3878 for (; __first1
!= __last1
; ++__first1
)
3879 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3880 if (__comp(*__first1
, *__iter
))
3886 * @brief Find two adjacent values in a sequence that are equal.
3887 * @param first A forward iterator.
3888 * @param last A forward iterator.
3889 * @return The first iterator @c i such that @c i and @c i+1 are both
3890 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
3891 * or @p last if no such iterator exists.
3893 template<typename _ForwardIterator
>
3895 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3897 // concept requirements
3898 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3899 __glibcxx_function_requires(_EqualityComparableConcept
<
3900 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3901 __glibcxx_requires_valid_range(__first
, __last
);
3902 if (__first
== __last
)
3904 _ForwardIterator __next
= __first
;
3905 while(++__next
!= __last
)
3907 if (*__first
== *__next
)
3915 * @brief Find two adjacent values in a sequence using a predicate.
3916 * @param first A forward iterator.
3917 * @param last A forward iterator.
3918 * @param binary_pred A binary predicate.
3919 * @return The first iterator @c i such that @c i and @c i+1 are both
3920 * valid iterators in @p [first,last) and such that
3921 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
3924 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3926 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3927 _BinaryPredicate __binary_pred
)
3929 // concept requirements
3930 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3931 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3932 typename iterator_traits
<_ForwardIterator
>::value_type
,
3933 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3934 __glibcxx_requires_valid_range(__first
, __last
);
3935 if (__first
== __last
)
3937 _ForwardIterator __next
= __first
;
3938 while(++__next
!= __last
)
3940 if (__binary_pred(*__first
, *__next
))
3948 * @brief Count the number of copies of a value in a sequence.
3949 * @param first An input iterator.
3950 * @param last An input iterator.
3951 * @param value The value to be counted.
3952 * @return The number of iterators @c i in the range @p [first,last)
3953 * for which @c *i == @p value
3955 template<typename _InputIterator
, typename _Tp
>
3956 typename iterator_traits
<_InputIterator
>::difference_type
3957 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
3959 // concept requirements
3960 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3961 __glibcxx_function_requires(_EqualOpConcept
<
3962 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3963 __glibcxx_requires_valid_range(__first
, __last
);
3964 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3965 for (; __first
!= __last
; ++__first
)
3966 if (*__first
== __value
)
3972 * @brief Count the elements of a sequence for which a predicate is true.
3973 * @param first An input iterator.
3974 * @param last An input iterator.
3975 * @param pred A predicate.
3976 * @return The number of iterators @c i in the range @p [first,last)
3977 * for which @p pred(*i) is true.
3979 template<typename _InputIterator
, typename _Predicate
>
3980 typename iterator_traits
<_InputIterator
>::difference_type
3981 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3983 // concept requirements
3984 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3985 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3986 typename iterator_traits
<_InputIterator
>::value_type
>)
3987 __glibcxx_requires_valid_range(__first
, __last
);
3988 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3989 for (; __first
!= __last
; ++__first
)
3990 if (__pred(*__first
))
3996 * @brief Search a sequence for a matching sub-sequence.
3997 * @param first1 A forward iterator.
3998 * @param last1 A forward iterator.
3999 * @param first2 A forward iterator.
4000 * @param last2 A forward iterator.
4001 * @return The first iterator @c i in the range
4002 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4003 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4004 * such iterator exists.
4006 * Searches the range @p [first1,last1) for a sub-sequence that compares
4007 * equal value-by-value with the sequence given by @p [first2,last2) and
4008 * returns an iterator to the first element of the sub-sequence, or
4009 * @p last1 if the sub-sequence is not found.
4011 * Because the sub-sequence must lie completely within the range
4012 * @p [first1,last1) it must start at a position less than
4013 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4015 * This means that the returned iterator @c i will be in the range
4016 * @p [first1,last1-(last2-first2))
4018 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4020 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4021 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4023 // concept requirements
4024 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4025 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4026 __glibcxx_function_requires(_EqualOpConcept
<
4027 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4028 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4029 __glibcxx_requires_valid_range(__first1
, __last1
);
4030 __glibcxx_requires_valid_range(__first2
, __last2
);
4032 // Test for empty ranges
4033 if (__first1
== __last1
|| __first2
== __last2
)
4036 // Test for a pattern of length 1.
4037 _ForwardIterator2
__p1(__first2
);
4038 if (++__p1
== __last2
)
4039 return _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4042 _ForwardIterator2 __p
;
4043 _ForwardIterator1 __current
= __first1
;
4047 __first1
= _GLIBCXX_STD_P::find(__first1
, __last1
, *__first2
);
4048 if (__first1
== __last1
)
4052 __current
= __first1
;
4053 if (++__current
== __last1
)
4056 while (*__current
== *__p
)
4058 if (++__p
== __last2
)
4060 if (++__current
== __last1
)
4069 * @brief Search a sequence for a matching sub-sequence using a predicate.
4070 * @param first1 A forward iterator.
4071 * @param last1 A forward iterator.
4072 * @param first2 A forward iterator.
4073 * @param last2 A forward iterator.
4074 * @param predicate A binary predicate.
4075 * @return The first iterator @c i in the range
4076 * @p [first1,last1-(last2-first2)) such that
4077 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4078 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4080 * Searches the range @p [first1,last1) for a sub-sequence that compares
4081 * equal value-by-value with the sequence given by @p [first2,last2),
4082 * using @p predicate to determine equality, and returns an iterator
4083 * to the first element of the sub-sequence, or @p last1 if no such
4086 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4088 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4089 typename _BinaryPredicate
>
4091 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4092 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4093 _BinaryPredicate __predicate
)
4095 // concept requirements
4096 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4097 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4098 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4099 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4100 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4101 __glibcxx_requires_valid_range(__first1
, __last1
);
4102 __glibcxx_requires_valid_range(__first2
, __last2
);
4104 // Test for empty ranges
4105 if (__first1
== __last1
|| __first2
== __last2
)
4108 // Test for a pattern of length 1.
4109 _ForwardIterator2
__p1(__first2
);
4110 if (++__p1
== __last2
)
4112 while (__first1
!= __last1
4113 && !bool(__predicate(*__first1
, *__first2
)))
4119 _ForwardIterator2 __p
;
4120 _ForwardIterator1 __current
= __first1
;
4124 while (__first1
!= __last1
4125 && !bool(__predicate(*__first1
, *__first2
)))
4127 if (__first1
== __last1
)
4131 __current
= __first1
;
4132 if (++__current
== __last1
)
4135 while (__predicate(*__current
, *__p
))
4137 if (++__p
== __last2
)
4139 if (++__current
== __last1
)
4149 * @brief Search a sequence for a number of consecutive values.
4150 * @param first A forward iterator.
4151 * @param last A forward iterator.
4152 * @param count The number of consecutive values.
4153 * @param val The value to find.
4154 * @return The first iterator @c i in the range @p [first,last-count)
4155 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4156 * or @p last if no such iterator exists.
4158 * Searches the range @p [first,last) for @p count consecutive elements
4161 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4163 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4164 _Integer __count
, const _Tp
& __val
)
4166 // concept requirements
4167 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4168 __glibcxx_function_requires(_EqualOpConcept
<
4169 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4170 __glibcxx_requires_valid_range(__first
, __last
);
4175 return _GLIBCXX_STD_P::find(__first
, __last
, __val
);
4176 return std::__search_n(__first
, __last
, __count
, __val
,
4177 std::__iterator_category(__first
));
4182 * @brief Search a sequence for a number of consecutive values using a
4184 * @param first A forward iterator.
4185 * @param last A forward iterator.
4186 * @param count The number of consecutive values.
4187 * @param val The value to find.
4188 * @param binary_pred A binary predicate.
4189 * @return The first iterator @c i in the range @p [first,last-count)
4190 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4191 * range @p [0,count), or @p last if no such iterator exists.
4193 * Searches the range @p [first,last) for @p count consecutive elements
4194 * for which the predicate returns true.
4196 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4197 typename _BinaryPredicate
>
4199 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4200 _Integer __count
, const _Tp
& __val
,
4201 _BinaryPredicate __binary_pred
)
4203 // concept requirements
4204 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4205 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4206 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4207 __glibcxx_requires_valid_range(__first
, __last
);
4213 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
4217 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
4218 std::__iterator_category(__first
));
4223 * @brief Perform an operation on a sequence.
4224 * @param first An input iterator.
4225 * @param last An input iterator.
4226 * @param result An output iterator.
4227 * @param unary_op A unary operator.
4228 * @return An output iterator equal to @p result+(last-first).
4230 * Applies the operator to each element in the input range and assigns
4231 * the results to successive elements of the output sequence.
4232 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4233 * range @p [0,last-first).
4235 * @p unary_op must not alter its argument.
4237 template<typename _InputIterator
, typename _OutputIterator
,
4238 typename _UnaryOperation
>
4240 transform(_InputIterator __first
, _InputIterator __last
,
4241 _OutputIterator __result
, _UnaryOperation __unary_op
)
4243 // concept requirements
4244 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4245 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4246 // "the type returned by a _UnaryOperation"
4247 __typeof__(__unary_op(*__first
))>)
4248 __glibcxx_requires_valid_range(__first
, __last
);
4250 for (; __first
!= __last
; ++__first
, ++__result
)
4251 *__result
= __unary_op(*__first
);
4256 * @brief Perform an operation on corresponding elements of two sequences.
4257 * @param first1 An input iterator.
4258 * @param last1 An input iterator.
4259 * @param first2 An input iterator.
4260 * @param result An output iterator.
4261 * @param binary_op A binary operator.
4262 * @return An output iterator equal to @p result+(last-first).
4264 * Applies the operator to the corresponding elements in the two
4265 * input ranges and assigns the results to successive elements of the
4267 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4268 * @c N in the range @p [0,last1-first1).
4270 * @p binary_op must not alter either of its arguments.
4272 template<typename _InputIterator1
, typename _InputIterator2
,
4273 typename _OutputIterator
, typename _BinaryOperation
>
4275 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4276 _InputIterator2 __first2
, _OutputIterator __result
,
4277 _BinaryOperation __binary_op
)
4279 // concept requirements
4280 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4281 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4282 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4283 // "the type returned by a _BinaryOperation"
4284 __typeof__(__binary_op(*__first1
,*__first2
))>)
4285 __glibcxx_requires_valid_range(__first1
, __last1
);
4287 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4288 *__result
= __binary_op(*__first1
, *__first2
);
4293 * @brief Replace each occurrence of one value in a sequence with another
4295 * @param first A forward iterator.
4296 * @param last A forward iterator.
4297 * @param old_value The value to be replaced.
4298 * @param new_value The replacement value.
4299 * @return replace() returns no value.
4301 * For each iterator @c i in the range @p [first,last) if @c *i ==
4302 * @p old_value then the assignment @c *i = @p new_value is performed.
4304 template<typename _ForwardIterator
, typename _Tp
>
4306 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4307 const _Tp
& __old_value
, const _Tp
& __new_value
)
4309 // concept requirements
4310 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4312 __glibcxx_function_requires(_EqualOpConcept
<
4313 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4314 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4315 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4316 __glibcxx_requires_valid_range(__first
, __last
);
4318 for (; __first
!= __last
; ++__first
)
4319 if (*__first
== __old_value
)
4320 *__first
= __new_value
;
4324 * @brief Replace each value in a sequence for which a predicate returns
4325 * true with another value.
4326 * @param first A forward iterator.
4327 * @param last A forward iterator.
4328 * @param pred A predicate.
4329 * @param new_value The replacement value.
4330 * @return replace_if() returns no value.
4332 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4333 * is true then the assignment @c *i = @p new_value is performed.
4335 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4337 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4338 _Predicate __pred
, const _Tp
& __new_value
)
4340 // concept requirements
4341 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4343 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4344 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4345 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4346 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4347 __glibcxx_requires_valid_range(__first
, __last
);
4349 for (; __first
!= __last
; ++__first
)
4350 if (__pred(*__first
))
4351 *__first
= __new_value
;
4355 * @brief Assign the result of a function object to each value in a
4357 * @param first A forward iterator.
4358 * @param last A forward iterator.
4359 * @param gen A function object taking no arguments and returning
4360 * std::iterator_traits<_ForwardIterator>::value_type
4361 * @return generate() returns no value.
4363 * Performs the assignment @c *i = @p gen() for each @c i in the range
4366 template<typename _ForwardIterator
, typename _Generator
>
4368 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4371 // concept requirements
4372 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4373 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4374 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4375 __glibcxx_requires_valid_range(__first
, __last
);
4377 for (; __first
!= __last
; ++__first
)
4382 * @brief Assign the result of a function object to each value in a
4384 * @param first A forward iterator.
4385 * @param n The length of the sequence.
4386 * @param gen A function object taking no arguments and returning
4387 * std::iterator_traits<_ForwardIterator>::value_type
4388 * @return The end of the sequence, @p first+n
4390 * Performs the assignment @c *i = @p gen() for each @c i in the range
4391 * @p [first,first+n).
4393 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4395 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4397 // concept requirements
4398 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4399 // "the type returned by a _Generator"
4400 __typeof__(__gen())>)
4402 for (; __n
> 0; --__n
, ++__first
)
4409 * @brief Copy a sequence, removing consecutive duplicate values.
4410 * @param first An input iterator.
4411 * @param last An input iterator.
4412 * @param result An output iterator.
4413 * @return An iterator designating the end of the resulting sequence.
4415 * Copies each element in the range @p [first,last) to the range
4416 * beginning at @p result, except that only the first element is copied
4417 * from groups of consecutive elements that compare equal.
4418 * unique_copy() is stable, so the relative order of elements that are
4419 * copied is unchanged.
4422 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4423 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4425 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4426 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4430 template<typename _InputIterator
, typename _OutputIterator
>
4431 inline _OutputIterator
4432 unique_copy(_InputIterator __first
, _InputIterator __last
,
4433 _OutputIterator __result
)
4435 // concept requirements
4436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4437 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4438 typename iterator_traits
<_InputIterator
>::value_type
>)
4439 __glibcxx_function_requires(_EqualityComparableConcept
<
4440 typename iterator_traits
<_InputIterator
>::value_type
>)
4441 __glibcxx_requires_valid_range(__first
, __last
);
4443 if (__first
== __last
)
4445 return std::__unique_copy(__first
, __last
, __result
,
4446 std::__iterator_category(__first
),
4447 std::__iterator_category(__result
));
4451 * @brief Copy a sequence, removing consecutive values using a predicate.
4452 * @param first An input iterator.
4453 * @param last An input iterator.
4454 * @param result An output iterator.
4455 * @param binary_pred A binary predicate.
4456 * @return An iterator designating the end of the resulting sequence.
4458 * Copies each element in the range @p [first,last) to the range
4459 * beginning at @p result, except that only the first element is copied
4460 * from groups of consecutive elements for which @p binary_pred returns
4462 * unique_copy() is stable, so the relative order of elements that are
4463 * copied is unchanged.
4466 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4467 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4470 template<typename _InputIterator
, typename _OutputIterator
,
4471 typename _BinaryPredicate
>
4472 inline _OutputIterator
4473 unique_copy(_InputIterator __first
, _InputIterator __last
,
4474 _OutputIterator __result
,
4475 _BinaryPredicate __binary_pred
)
4477 // concept requirements -- predicates checked later
4478 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4479 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4480 typename iterator_traits
<_InputIterator
>::value_type
>)
4481 __glibcxx_requires_valid_range(__first
, __last
);
4483 if (__first
== __last
)
4485 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
4486 std::__iterator_category(__first
),
4487 std::__iterator_category(__result
));
4492 * @brief Randomly shuffle the elements of a sequence.
4493 * @param first A forward iterator.
4494 * @param last A forward iterator.
4497 * Reorder the elements in the range @p [first,last) using a random
4498 * distribution, so that every possible ordering of the sequence is
4501 template<typename _RandomAccessIterator
>
4503 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4505 // concept requirements
4506 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4507 _RandomAccessIterator
>)
4508 __glibcxx_requires_valid_range(__first
, __last
);
4510 if (__first
!= __last
)
4511 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4512 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4516 * @brief Shuffle the elements of a sequence using a random number
4518 * @param first A forward iterator.
4519 * @param last A forward iterator.
4520 * @param rand The RNG functor or function.
4523 * Reorders the elements in the range @p [first,last) using @p rand to
4524 * provide a random distribution. Calling @p rand(N) for a positive
4525 * integer @p N should return a randomly chosen integer from the
4528 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4530 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4531 _RandomNumberGenerator
& __rand
)
4533 // concept requirements
4534 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4535 _RandomAccessIterator
>)
4536 __glibcxx_requires_valid_range(__first
, __last
);
4538 if (__first
== __last
)
4540 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4541 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4546 * @brief Move elements for which a predicate is true to the beginning
4548 * @param first A forward iterator.
4549 * @param last A forward iterator.
4550 * @param pred A predicate functor.
4551 * @return An iterator @p middle such that @p pred(i) is true for each
4552 * iterator @p i in the range @p [first,middle) and false for each @p i
4553 * in the range @p [middle,last).
4555 * @p pred must not modify its operand. @p partition() does not preserve
4556 * the relative ordering of elements in each group, use
4557 * @p stable_partition() if this is needed.
4559 template<typename _ForwardIterator
, typename _Predicate
>
4560 inline _ForwardIterator
4561 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4564 // concept requirements
4565 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4567 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4568 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4569 __glibcxx_requires_valid_range(__first
, __last
);
4571 return std::__partition(__first
, __last
, __pred
,
4572 std::__iterator_category(__first
));
4578 * @brief Sort the smallest elements of a sequence.
4579 * @param first An iterator.
4580 * @param middle Another iterator.
4581 * @param last Another iterator.
4584 * Sorts the smallest @p (middle-first) elements in the range
4585 * @p [first,last) and moves them to the range @p [first,middle). The
4586 * order of the remaining elements in the range @p [middle,last) is
4588 * After the sort if @p i and @j are iterators in the range
4589 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4590 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4592 template<typename _RandomAccessIterator
>
4594 partial_sort(_RandomAccessIterator __first
,
4595 _RandomAccessIterator __middle
,
4596 _RandomAccessIterator __last
)
4598 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4601 // concept requirements
4602 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4603 _RandomAccessIterator
>)
4604 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4605 __glibcxx_requires_valid_range(__first
, __middle
);
4606 __glibcxx_requires_valid_range(__middle
, __last
);
4608 std::__heap_select(__first
, __middle
, __last
);
4609 std::sort_heap(__first
, __middle
);
4613 * @brief Sort the smallest elements of a sequence using a predicate
4615 * @param first An iterator.
4616 * @param middle Another iterator.
4617 * @param last Another iterator.
4618 * @param comp A comparison functor.
4621 * Sorts the smallest @p (middle-first) elements in the range
4622 * @p [first,last) and moves them to the range @p [first,middle). The
4623 * order of the remaining elements in the range @p [middle,last) is
4625 * After the sort if @p i and @j are iterators in the range
4626 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4627 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4630 template<typename _RandomAccessIterator
, typename _Compare
>
4632 partial_sort(_RandomAccessIterator __first
,
4633 _RandomAccessIterator __middle
,
4634 _RandomAccessIterator __last
,
4637 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4640 // concept requirements
4641 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4642 _RandomAccessIterator
>)
4643 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4644 _ValueType
, _ValueType
>)
4645 __glibcxx_requires_valid_range(__first
, __middle
);
4646 __glibcxx_requires_valid_range(__middle
, __last
);
4648 std::__heap_select(__first
, __middle
, __last
, __comp
);
4649 std::sort_heap(__first
, __middle
, __comp
);
4653 * @brief Sort a sequence just enough to find a particular position.
4654 * @param first An iterator.
4655 * @param nth Another iterator.
4656 * @param last Another iterator.
4659 * Rearranges the elements in the range @p [first,last) so that @p *nth
4660 * is the same element that would have been in that position had the
4661 * whole sequence been sorted.
4662 * whole sequence been sorted. The elements either side of @p *nth are
4663 * not completely sorted, but for any iterator @i in the range
4664 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4665 * holds that @p *j<*i is false.
4667 template<typename _RandomAccessIterator
>
4669 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4670 _RandomAccessIterator __last
)
4672 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4675 // concept requirements
4676 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4677 _RandomAccessIterator
>)
4678 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4679 __glibcxx_requires_valid_range(__first
, __nth
);
4680 __glibcxx_requires_valid_range(__nth
, __last
);
4682 if (__first
== __last
|| __nth
== __last
)
4685 std::__introselect(__first
, __nth
, __last
,
4686 std::__lg(__last
- __first
) * 2);
4690 * @brief Sort a sequence just enough to find a particular position
4691 * using a predicate for comparison.
4692 * @param first An iterator.
4693 * @param nth Another iterator.
4694 * @param last Another iterator.
4695 * @param comp A comparison functor.
4698 * Rearranges the elements in the range @p [first,last) so that @p *nth
4699 * is the same element that would have been in that position had the
4700 * whole sequence been sorted. The elements either side of @p *nth are
4701 * not completely sorted, but for any iterator @i in the range
4702 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4703 * holds that @p comp(*j,*i) is false.
4705 template<typename _RandomAccessIterator
, typename _Compare
>
4707 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4708 _RandomAccessIterator __last
, _Compare __comp
)
4710 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4713 // concept requirements
4714 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4715 _RandomAccessIterator
>)
4716 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4717 _ValueType
, _ValueType
>)
4718 __glibcxx_requires_valid_range(__first
, __nth
);
4719 __glibcxx_requires_valid_range(__nth
, __last
);
4721 if (__first
== __last
|| __nth
== __last
)
4724 std::__introselect(__first
, __nth
, __last
,
4725 std::__lg(__last
- __first
) * 2, __comp
);
4730 * @brief Sort the elements of a sequence.
4731 * @param first An iterator.
4732 * @param last Another iterator.
4735 * Sorts the elements in the range @p [first,last) in ascending order,
4736 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4737 * @p [first,last-1).
4739 * The relative ordering of equivalent elements is not preserved, use
4740 * @p stable_sort() if this is needed.
4742 template<typename _RandomAccessIterator
>
4744 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4746 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4749 // concept requirements
4750 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4751 _RandomAccessIterator
>)
4752 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4753 __glibcxx_requires_valid_range(__first
, __last
);
4755 if (__first
!= __last
)
4757 std::__introsort_loop(__first
, __last
,
4758 std::__lg(__last
- __first
) * 2);
4759 std::__final_insertion_sort(__first
, __last
);
4764 * @brief Sort the elements of a sequence using a predicate for comparison.
4765 * @param first An iterator.
4766 * @param last Another iterator.
4767 * @param comp A comparison functor.
4770 * Sorts the elements in the range @p [first,last) in ascending order,
4771 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4772 * range @p [first,last-1).
4774 * The relative ordering of equivalent elements is not preserved, use
4775 * @p stable_sort() if this is needed.
4777 template<typename _RandomAccessIterator
, typename _Compare
>
4779 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4782 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4785 // concept requirements
4786 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4787 _RandomAccessIterator
>)
4788 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
4790 __glibcxx_requires_valid_range(__first
, __last
);
4792 if (__first
!= __last
)
4794 std::__introsort_loop(__first
, __last
,
4795 std::__lg(__last
- __first
) * 2, __comp
);
4796 std::__final_insertion_sort(__first
, __last
, __comp
);
4801 * @brief Merges two sorted ranges.
4802 * @param first1 An iterator.
4803 * @param first2 Another iterator.
4804 * @param last1 Another iterator.
4805 * @param last2 Another iterator.
4806 * @param result An iterator pointing to the end of the merged range.
4807 * @return An iterator pointing to the first element "not less
4810 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4811 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4812 * must be sorted, and the output range must not overlap with either of
4813 * the input ranges. The sort is @e stable, that is, for equivalent
4814 * elements in the two ranges, elements from the first range will always
4815 * come before elements from the second.
4817 template<typename _InputIterator1
, typename _InputIterator2
,
4818 typename _OutputIterator
>
4820 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4821 _InputIterator2 __first2
, _InputIterator2 __last2
,
4822 _OutputIterator __result
)
4824 typedef typename iterator_traits
<_InputIterator1
>::value_type
4826 typedef typename iterator_traits
<_InputIterator2
>::value_type
4829 // concept requirements
4830 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4831 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4832 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4834 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4836 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4837 __glibcxx_requires_sorted(__first1
, __last1
);
4838 __glibcxx_requires_sorted(__first2
, __last2
);
4840 while (__first1
!= __last1
&& __first2
!= __last2
)
4842 if (*__first2
< *__first1
)
4844 *__result
= *__first2
;
4849 *__result
= *__first1
;
4854 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4859 * @brief Merges two sorted ranges.
4860 * @param first1 An iterator.
4861 * @param first2 Another iterator.
4862 * @param last1 Another iterator.
4863 * @param last2 Another iterator.
4864 * @param result An iterator pointing to the end of the merged range.
4865 * @param comp A functor to use for comparisons.
4866 * @return An iterator pointing to the first element "not less
4869 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4870 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4871 * must be sorted, and the output range must not overlap with either of
4872 * the input ranges. The sort is @e stable, that is, for equivalent
4873 * elements in the two ranges, elements from the first range will always
4874 * come before elements from the second.
4876 * The comparison function should have the same effects on ordering as
4877 * the function used for the initial sort.
4879 template<typename _InputIterator1
, typename _InputIterator2
,
4880 typename _OutputIterator
, typename _Compare
>
4882 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4883 _InputIterator2 __first2
, _InputIterator2 __last2
,
4884 _OutputIterator __result
, _Compare __comp
)
4886 typedef typename iterator_traits
<_InputIterator1
>::value_type
4888 typedef typename iterator_traits
<_InputIterator2
>::value_type
4891 // concept requirements
4892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4893 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4894 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4896 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4898 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4899 _ValueType2
, _ValueType1
>)
4900 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4901 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4903 while (__first1
!= __last1
&& __first2
!= __last2
)
4905 if (__comp(*__first2
, *__first1
))
4907 *__result
= *__first2
;
4912 *__result
= *__first1
;
4917 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4923 * @brief Sort the elements of a sequence, preserving the relative order
4924 * of equivalent elements.
4925 * @param first An iterator.
4926 * @param last Another iterator.
4929 * Sorts the elements in the range @p [first,last) in ascending order,
4930 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4931 * @p [first,last-1).
4933 * The relative ordering of equivalent elements is preserved, so any two
4934 * elements @p x and @p y in the range @p [first,last) such that
4935 * @p x<y is false and @p y<x is false will have the same relative
4936 * ordering after calling @p stable_sort().
4938 template<typename _RandomAccessIterator
>
4940 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4942 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4944 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4947 // concept requirements
4948 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4949 _RandomAccessIterator
>)
4950 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4951 __glibcxx_requires_valid_range(__first
, __last
);
4953 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4955 if (__buf
.begin() == 0)
4956 std::__inplace_stable_sort(__first
, __last
);
4958 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4959 _DistanceType(__buf
.size()));
4963 * @brief Sort the elements of a sequence using a predicate for comparison,
4964 * preserving the relative order of equivalent elements.
4965 * @param first An iterator.
4966 * @param last Another iterator.
4967 * @param comp A comparison functor.
4970 * Sorts the elements in the range @p [first,last) in ascending order,
4971 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
4972 * range @p [first,last-1).
4974 * The relative ordering of equivalent elements is preserved, so any two
4975 * elements @p x and @p y in the range @p [first,last) such that
4976 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
4977 * relative ordering after calling @p stable_sort().
4979 template<typename _RandomAccessIterator
, typename _Compare
>
4981 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4984 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4986 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4989 // concept requirements
4990 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4991 _RandomAccessIterator
>)
4992 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4995 __glibcxx_requires_valid_range(__first
, __last
);
4997 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4999 if (__buf
.begin() == 0)
5000 std::__inplace_stable_sort(__first
, __last
, __comp
);
5002 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5003 _DistanceType(__buf
.size()), __comp
);
5008 * @brief Return the union of two sorted ranges.
5009 * @param first1 Start of first range.
5010 * @param last1 End of first range.
5011 * @param first2 Start of second range.
5012 * @param last2 End of second range.
5013 * @return End of the output range.
5014 * @ingroup setoperations
5016 * This operation iterates over both ranges, copying elements present in
5017 * each range in order to the output range. Iterators increment for each
5018 * range. When the current element of one range is less than the other,
5019 * that element is copied and the iterator advanced. If an element is
5020 * contained in both ranges, the element from the first range is copied and
5021 * both ranges advance. The output range may not overlap either input
5024 template<typename _InputIterator1
, typename _InputIterator2
,
5025 typename _OutputIterator
>
5027 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5028 _InputIterator2 __first2
, _InputIterator2 __last2
,
5029 _OutputIterator __result
)
5031 typedef typename iterator_traits
<_InputIterator1
>::value_type
5033 typedef typename iterator_traits
<_InputIterator2
>::value_type
5036 // concept requirements
5037 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5038 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5039 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5041 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5043 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5044 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5045 __glibcxx_requires_sorted(__first1
, __last1
);
5046 __glibcxx_requires_sorted(__first2
, __last2
);
5048 while (__first1
!= __last1
&& __first2
!= __last2
)
5050 if (*__first1
< *__first2
)
5052 *__result
= *__first1
;
5055 else if (*__first2
< *__first1
)
5057 *__result
= *__first2
;
5062 *__result
= *__first1
;
5068 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5073 * @brief Return the union of two sorted ranges using a comparison functor.
5074 * @param first1 Start of first range.
5075 * @param last1 End of first range.
5076 * @param first2 Start of second range.
5077 * @param last2 End of second range.
5078 * @param comp The comparison functor.
5079 * @return End of the output range.
5080 * @ingroup setoperations
5082 * This operation iterates over both ranges, copying elements present in
5083 * each range in order to the output range. Iterators increment for each
5084 * range. When the current element of one range is less than the other
5085 * according to @a comp, that element is copied and the iterator advanced.
5086 * If an equivalent element according to @a comp is contained in both
5087 * ranges, the element from the first range is copied and both ranges
5088 * advance. The output range may not overlap either input range.
5090 template<typename _InputIterator1
, typename _InputIterator2
,
5091 typename _OutputIterator
, typename _Compare
>
5093 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5094 _InputIterator2 __first2
, _InputIterator2 __last2
,
5095 _OutputIterator __result
, _Compare __comp
)
5097 typedef typename iterator_traits
<_InputIterator1
>::value_type
5099 typedef typename iterator_traits
<_InputIterator2
>::value_type
5102 // concept requirements
5103 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5104 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5105 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5107 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5109 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5110 _ValueType1
, _ValueType2
>)
5111 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5112 _ValueType2
, _ValueType1
>)
5113 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5114 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5116 while (__first1
!= __last1
&& __first2
!= __last2
)
5118 if (__comp(*__first1
, *__first2
))
5120 *__result
= *__first1
;
5123 else if (__comp(*__first2
, *__first1
))
5125 *__result
= *__first2
;
5130 *__result
= *__first1
;
5136 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5141 * @brief Return the intersection of two sorted ranges.
5142 * @param first1 Start of first range.
5143 * @param last1 End of first range.
5144 * @param first2 Start of second range.
5145 * @param last2 End of second range.
5146 * @return End of the output range.
5147 * @ingroup setoperations
5149 * This operation iterates over both ranges, copying elements present in
5150 * both ranges in order to the output range. Iterators increment for each
5151 * range. When the current element of one range is less than the other,
5152 * that iterator advances. If an element is contained in both ranges, the
5153 * element from the first range is copied and both ranges advance. The
5154 * output range may not overlap either input range.
5156 template<typename _InputIterator1
, typename _InputIterator2
,
5157 typename _OutputIterator
>
5159 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5160 _InputIterator2 __first2
, _InputIterator2 __last2
,
5161 _OutputIterator __result
)
5163 typedef typename iterator_traits
<_InputIterator1
>::value_type
5165 typedef typename iterator_traits
<_InputIterator2
>::value_type
5168 // concept requirements
5169 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5170 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5171 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5173 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5174 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5175 __glibcxx_requires_sorted(__first1
, __last1
);
5176 __glibcxx_requires_sorted(__first2
, __last2
);
5178 while (__first1
!= __last1
&& __first2
!= __last2
)
5179 if (*__first1
< *__first2
)
5181 else if (*__first2
< *__first1
)
5185 *__result
= *__first1
;
5194 * @brief Return the intersection of two sorted ranges using comparison
5196 * @param first1 Start of first range.
5197 * @param last1 End of first range.
5198 * @param first2 Start of second range.
5199 * @param last2 End of second range.
5200 * @param comp The comparison functor.
5201 * @return End of the output range.
5202 * @ingroup setoperations
5204 * This operation iterates over both ranges, copying elements present in
5205 * both ranges in order to the output range. Iterators increment for each
5206 * range. When the current element of one range is less than the other
5207 * according to @a comp, that iterator advances. If an element is
5208 * contained in both ranges according to @a comp, the element from the
5209 * first range is copied and both ranges advance. The output range may not
5210 * overlap either input range.
5212 template<typename _InputIterator1
, typename _InputIterator2
,
5213 typename _OutputIterator
, typename _Compare
>
5215 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5216 _InputIterator2 __first2
, _InputIterator2 __last2
,
5217 _OutputIterator __result
, _Compare __comp
)
5219 typedef typename iterator_traits
<_InputIterator1
>::value_type
5221 typedef typename iterator_traits
<_InputIterator2
>::value_type
5224 // concept requirements
5225 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5226 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5227 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5229 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5230 _ValueType1
, _ValueType2
>)
5231 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5232 _ValueType2
, _ValueType1
>)
5233 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5234 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5236 while (__first1
!= __last1
&& __first2
!= __last2
)
5237 if (__comp(*__first1
, *__first2
))
5239 else if (__comp(*__first2
, *__first1
))
5243 *__result
= *__first1
;
5252 * @brief Return the difference of two sorted ranges.
5253 * @param first1 Start of first range.
5254 * @param last1 End of first range.
5255 * @param first2 Start of second range.
5256 * @param last2 End of second range.
5257 * @return End of the output range.
5258 * @ingroup setoperations
5260 * This operation iterates over both ranges, copying elements present in
5261 * the first range but not the second in order to the output range.
5262 * Iterators increment for each range. When the current element of the
5263 * first range is less than the second, that element is copied and the
5264 * iterator advances. If the current element of the second range is less,
5265 * the iterator advances, but no element is copied. If an element is
5266 * contained in both ranges, no elements are copied and both ranges
5267 * advance. The output range may not overlap either input range.
5269 template<typename _InputIterator1
, typename _InputIterator2
,
5270 typename _OutputIterator
>
5272 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5273 _InputIterator2 __first2
, _InputIterator2 __last2
,
5274 _OutputIterator __result
)
5276 typedef typename iterator_traits
<_InputIterator1
>::value_type
5278 typedef typename iterator_traits
<_InputIterator2
>::value_type
5281 // concept requirements
5282 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5283 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5284 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5286 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5287 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5288 __glibcxx_requires_sorted(__first1
, __last1
);
5289 __glibcxx_requires_sorted(__first2
, __last2
);
5291 while (__first1
!= __last1
&& __first2
!= __last2
)
5292 if (*__first1
< *__first2
)
5294 *__result
= *__first1
;
5298 else if (*__first2
< *__first1
)
5305 return std::copy(__first1
, __last1
, __result
);
5309 * @brief Return the difference of two sorted ranges using comparison
5311 * @param first1 Start of first range.
5312 * @param last1 End of first range.
5313 * @param first2 Start of second range.
5314 * @param last2 End of second range.
5315 * @param comp The comparison functor.
5316 * @return End of the output range.
5317 * @ingroup setoperations
5319 * This operation iterates over both ranges, copying elements present in
5320 * the first range but not the second in order to the output range.
5321 * Iterators increment for each range. When the current element of the
5322 * first range is less than the second according to @a comp, that element
5323 * is copied and the iterator advances. If the current element of the
5324 * second range is less, no element is copied and the iterator advances.
5325 * If an element is contained in both ranges according to @a comp, no
5326 * elements are copied and both ranges advance. The output range may not
5327 * overlap either input range.
5329 template<typename _InputIterator1
, typename _InputIterator2
,
5330 typename _OutputIterator
, typename _Compare
>
5332 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5333 _InputIterator2 __first2
, _InputIterator2 __last2
,
5334 _OutputIterator __result
, _Compare __comp
)
5336 typedef typename iterator_traits
<_InputIterator1
>::value_type
5338 typedef typename iterator_traits
<_InputIterator2
>::value_type
5341 // concept requirements
5342 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5343 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5344 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5346 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5347 _ValueType1
, _ValueType2
>)
5348 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5349 _ValueType2
, _ValueType1
>)
5350 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5351 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5353 while (__first1
!= __last1
&& __first2
!= __last2
)
5354 if (__comp(*__first1
, *__first2
))
5356 *__result
= *__first1
;
5360 else if (__comp(*__first2
, *__first1
))
5367 return std::copy(__first1
, __last1
, __result
);
5371 * @brief Return the symmetric difference of two sorted ranges.
5372 * @param first1 Start of first range.
5373 * @param last1 End of first range.
5374 * @param first2 Start of second range.
5375 * @param last2 End of second range.
5376 * @return End of the output range.
5377 * @ingroup setoperations
5379 * This operation iterates over both ranges, copying elements present in
5380 * one range but not the other in order to the output range. Iterators
5381 * increment for each range. When the current element of one range is less
5382 * than the other, that element is copied and the iterator advances. If an
5383 * element is contained in both ranges, no elements are copied and both
5384 * ranges advance. The output range may not overlap either input range.
5386 template<typename _InputIterator1
, typename _InputIterator2
,
5387 typename _OutputIterator
>
5389 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5390 _InputIterator2 __first2
, _InputIterator2 __last2
,
5391 _OutputIterator __result
)
5393 typedef typename iterator_traits
<_InputIterator1
>::value_type
5395 typedef typename iterator_traits
<_InputIterator2
>::value_type
5398 // concept requirements
5399 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5400 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5401 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5403 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5405 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5406 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5407 __glibcxx_requires_sorted(__first1
, __last1
);
5408 __glibcxx_requires_sorted(__first2
, __last2
);
5410 while (__first1
!= __last1
&& __first2
!= __last2
)
5411 if (*__first1
< *__first2
)
5413 *__result
= *__first1
;
5417 else if (*__first2
< *__first1
)
5419 *__result
= *__first2
;
5428 return std::copy(__first2
, __last2
, std::copy(__first1
,
5429 __last1
, __result
));
5433 * @brief Return the symmetric difference of two sorted ranges using
5434 * comparison functor.
5435 * @param first1 Start of first range.
5436 * @param last1 End of first range.
5437 * @param first2 Start of second range.
5438 * @param last2 End of second range.
5439 * @param comp The comparison functor.
5440 * @return End of the output range.
5441 * @ingroup setoperations
5443 * This operation iterates over both ranges, copying elements present in
5444 * one range but not the other in order to the output range. Iterators
5445 * increment for each range. When the current element of one range is less
5446 * than the other according to @a comp, that element is copied and the
5447 * iterator advances. If an element is contained in both ranges according
5448 * to @a comp, no elements are copied and both ranges advance. The output
5449 * range may not overlap either input range.
5451 template<typename _InputIterator1
, typename _InputIterator2
,
5452 typename _OutputIterator
, typename _Compare
>
5454 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5455 _InputIterator2 __first2
, _InputIterator2 __last2
,
5456 _OutputIterator __result
,
5459 typedef typename iterator_traits
<_InputIterator1
>::value_type
5461 typedef typename iterator_traits
<_InputIterator2
>::value_type
5464 // concept requirements
5465 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5466 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5467 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5469 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5471 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5472 _ValueType1
, _ValueType2
>)
5473 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5474 _ValueType2
, _ValueType1
>)
5475 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5476 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5478 while (__first1
!= __last1
&& __first2
!= __last2
)
5479 if (__comp(*__first1
, *__first2
))
5481 *__result
= *__first1
;
5485 else if (__comp(*__first2
, *__first1
))
5487 *__result
= *__first2
;
5496 return std::copy(__first2
, __last2
,
5497 std::copy(__first1
, __last1
, __result
));
5502 * @brief Return the minimum element in a range.
5503 * @param first Start of range.
5504 * @param last End of range.
5505 * @return Iterator referencing the first instance of the smallest value.
5507 template<typename _ForwardIterator
>
5509 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5511 // concept requirements
5512 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5513 __glibcxx_function_requires(_LessThanComparableConcept
<
5514 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5515 __glibcxx_requires_valid_range(__first
, __last
);
5517 if (__first
== __last
)
5519 _ForwardIterator __result
= __first
;
5520 while (++__first
!= __last
)
5521 if (*__first
< *__result
)
5527 * @brief Return the minimum element in a range using comparison functor.
5528 * @param first Start of range.
5529 * @param last End of range.
5530 * @param comp Comparison functor.
5531 * @return Iterator referencing the first instance of the smallest value
5532 * according to comp.
5534 template<typename _ForwardIterator
, typename _Compare
>
5536 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5539 // concept requirements
5540 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5541 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5542 typename iterator_traits
<_ForwardIterator
>::value_type
,
5543 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5544 __glibcxx_requires_valid_range(__first
, __last
);
5546 if (__first
== __last
)
5548 _ForwardIterator __result
= __first
;
5549 while (++__first
!= __last
)
5550 if (__comp(*__first
, *__result
))
5556 * @brief Return the maximum element in a range.
5557 * @param first Start of range.
5558 * @param last End of range.
5559 * @return Iterator referencing the first instance of the largest value.
5561 template<typename _ForwardIterator
>
5563 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5565 // concept requirements
5566 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5567 __glibcxx_function_requires(_LessThanComparableConcept
<
5568 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5569 __glibcxx_requires_valid_range(__first
, __last
);
5571 if (__first
== __last
)
5573 _ForwardIterator __result
= __first
;
5574 while (++__first
!= __last
)
5575 if (*__result
< *__first
)
5581 * @brief Return the maximum element in a range using comparison functor.
5582 * @param first Start of range.
5583 * @param last End of range.
5584 * @param comp Comparison functor.
5585 * @return Iterator referencing the first instance of the largest value
5586 * according to comp.
5588 template<typename _ForwardIterator
, typename _Compare
>
5590 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5593 // concept requirements
5594 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5595 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5596 typename iterator_traits
<_ForwardIterator
>::value_type
,
5597 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5598 __glibcxx_requires_valid_range(__first
, __last
);
5600 if (__first
== __last
) return __first
;
5601 _ForwardIterator __result
= __first
;
5602 while (++__first
!= __last
)
5603 if (__comp(*__result
, *__first
))
5608 _GLIBCXX_END_NESTED_NAMESPACE
5610 #endif /* _STL_ALGO_H */